fix(auth): start no new session for a password change over a Bearer token

A password change ends every session of the user and re-issues one for
the device it was made on, handed out as the session cookie. A caller
that authenticated with a Bearer token never reads that cookie, so the
new session sat in the session list as a live device nobody held until
it expired.

The controller now asks for the replacement session only when the
request came with the session cookie, the same check the sliding
renewal makes. The response body is unchanged.
This commit is contained in:
Maurice
2026-10-08 17:55:20 +02:00
parent 648f63c626
commit be751ad521
6 changed files with 55 additions and 11 deletions
+8 -4
View File
@@ -111,10 +111,14 @@ export class AuthController {
async changePassword(@CurrentUser() user: User, @Body() body: ChangePasswordDto, @Req() req: Request, @Res({ passthrough: true }) res: Response) {
await this.limit('login', req, 5);
// Carry the session's remember choice into the re-issued token/cookie so a
// "remember me" login survives a password change (#1927). Bearer callers
// have no cookie → undefined → the historical default duration.
const remember = decodeSessionClaims((req.cookies as Record<string, string> | undefined)?.trek_session)?.remember;
const result = await this.auth.changePassword(user.id, user.email, body, remember, sessionClientFrom(req));
// "remember me" login survives a password change (#1927). The guard
// verified the cookie whenever there is one (extractToken prefers it), the
// same check the renewal interceptor makes. A Bearer caller has none: it
// would never see a re-issued token, so no new session is started for it.
const cookie = (req.cookies as Record<string, string> | undefined)?.trek_session;
const remember = decodeSessionClaims(cookie)?.remember;
const cookieSession = typeof cookie === 'string' && cookie.length > 0;
const result = await this.auth.changePassword(user.id, user.email, body, remember, sessionClientFrom(req), cookieSession);
if (result.error) {
throw new HttpException({ error: result.error }, result.status!);
}
+5 -5
View File
@@ -624,6 +624,7 @@ export class AuthService {
rawBody: unknown,
remember?: boolean,
client?: SessionClient,
issueSession = true,
): Promise<{ error?: string; status?: number; success?: boolean; token?: string }> {
const body = rawBody as { current_password?: string; new_password?: string };
if (await this.isOidcOnlyMode()) {
@@ -654,9 +655,7 @@ export class AuthService {
// before sessions were tracked) and prunes the MCP static token and OAuth
// bearer-token stores to match, the same set the password reset revokes.
await this.mcpTokens.deleteAllForUser(userId);
try {
await this.oauthTokens.revokeAllForUser(userId);
} catch { /* oauth_tokens table may not exist in very old installs */ }
try { await this.oauthTokens.revokeAllForUser(userId); } catch { /* no oauth_tokens in very old installs */ }
// Push devices keep receiving notifications without any session, so they
// go too. The device the change was made on registers again right away:
// the client reloads the user after the change, and that re-syncs its
@@ -667,8 +666,9 @@ export class AuthService {
try { revokeUserSessions?.(userId); } catch { /* best-effort */ }
// Re-issue a session for the current device only, keeping the login's remember
// choice instead of downgrading it to the default duration (#1927).
// Re-issue a session for the current device only, with the login's remember choice (#1927).
// Not for a Bearer caller (`issueSession` false): it never receives the token (see the controller).
if (!issueSession) return { success: true };
const token = await this.generateToken({ id: userId, password_version: newPv }, remember, client);
return { success: true, token };
}