mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-25 22:27:32 +00:00
fix(oidc): a sign-in links to an account by email only when the address was not set by the account itself
An SSO login that matched no account by its subject took the account with the same email address and linked the provider identity to it. That address proves nothing when the account typed it in itself: anyone could sign up with, or switch their profile to, a colleague's address and receive the colleague's first SSO login. The provider switch branch (#2110) relinked a row on any email match without even asking for email_verified, and also for another subject of the same provider. users.email_self_asserted remembers an address the account set itself: signup (not the first account) and an email change in the profile settings set it, an admin entering a new address clears it, and accounts the provider creates start without it. Existing accounts keep linking, except those whose address moved away from the registered one without an admin editing it. A login by email now needs a verified address and an unmarked account, and is refused for another subject of the same issuer, with email_link_blocked and a translated message on the login screen. An email change in the profile settings writes a user.email_change audit row. Refs #2110
This commit is contained in:
@@ -196,6 +196,11 @@ export class AuthController {
|
||||
async updateSettings(@CurrentUser() user: User, @Body() body: SettingsUpdateDto, @Req() req: Request) {
|
||||
const result = await this.profile.updateSettings(user.id, body);
|
||||
await this.auditApiKeys(user.id, result.changedKeys ?? [], req);
|
||||
// The address itself stays out of the row, like the key values above; the row
|
||||
// is what tells an admin later that the account chose its current address.
|
||||
if (result.emailChanged) {
|
||||
await this.audit.writeAudit({ userId: user.id, action: 'user.email_change', ip: getClientIp(req) });
|
||||
}
|
||||
return { success: result.success, user: result.user };
|
||||
}
|
||||
|
||||
|
||||
@@ -468,10 +468,9 @@ export class AuthService {
|
||||
const password_hash = bcrypt.hashSync(password, BCRYPT_COST);
|
||||
|
||||
try {
|
||||
// One transaction for the whole signup: a mid-sequence throw (invite
|
||||
// bookkeeping, trip auto-join) must not leave a half-registered user.
|
||||
// The collision check and the first-user count run inside it too, so two
|
||||
// signups at once cannot both pass the check or both become admin.
|
||||
// One transaction for the whole signup: a mid-sequence throw (invite bookkeeping, trip auto-join)
|
||||
// must not leave a half-registered user. The collision check and the first-user count run inside
|
||||
// it too, so two signups at once cannot both pass the check or both become admin.
|
||||
return await this.uow.transactional(async () => {
|
||||
// Ignore guests (#1362): their synthetic username/email must never block a real signup.
|
||||
const existingUserId = await this.usersRepo.findIdByEmailOrUsernameCI(email, username);
|
||||
@@ -482,6 +481,7 @@ export class AuthService {
|
||||
const role = isFirstUser ? 'admin' : 'user';
|
||||
const inserted = await this.usersRepo.insertUser({
|
||||
username, email, password_hash, role, first_seen_version: readEnv().app.appVersion || '0.0.0',
|
||||
email_self_asserted: isFirstUser ? 0 : 1,
|
||||
});
|
||||
|
||||
const user = { id: inserted.id, username, email, role, avatar: null, mfa_enabled: false };
|
||||
|
||||
@@ -210,7 +210,7 @@ export class UserProfileService {
|
||||
async updateSettings(
|
||||
userId: number,
|
||||
rawBody: unknown,
|
||||
): Promise<{ success?: boolean; user?: Record<string, unknown>; changedKeys?: string[] }> {
|
||||
): Promise<{ success?: boolean; user?: Record<string, unknown>; changedKeys?: string[]; emailChanged?: boolean }> {
|
||||
const body = rawBody as {
|
||||
maps_api_key?: string;
|
||||
openweather_api_key?: string;
|
||||
@@ -261,6 +261,14 @@ export class UserProfileService {
|
||||
if (username !== undefined) changes.username = username.trim();
|
||||
if (email !== undefined) changes.email = email.trim();
|
||||
|
||||
// A new address typed in here is the account's own word for it, so an SSO
|
||||
// sign-in no longer links to it by email (see OidcService.refreshLinkedUser).
|
||||
// Changing only the case keeps the address and its standing.
|
||||
const previousEmail = changes.email !== undefined ? await this.usersRepo.getEmail(userId) : null;
|
||||
const emailChanged =
|
||||
changes.email !== undefined && changes.email.toLowerCase() !== (previousEmail ?? '').toLowerCase();
|
||||
if (emailChanged) changes.email_self_asserted = 1;
|
||||
|
||||
// Read before the write, so the comparison sees the old value; the role in
|
||||
// the same row decides whether the two instance-wide names travel with it.
|
||||
const current = await this.currentKeys(userId);
|
||||
@@ -297,6 +305,7 @@ export class UserProfileService {
|
||||
avatar_url: avatarUrl(updated || {}),
|
||||
},
|
||||
changedKeys,
|
||||
emailChanged,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user