mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-05 10:24:05 +00:00
fix(oidc): verify mailbox identity before linking accounts
Preserve the provider mailbox on new accounts. For existing accounts, use the stored issuer and subject or check mailbox identity before linking. Keep the candidate lookup and identity update inside the same transaction.
This commit is contained in:
@@ -349,34 +349,6 @@ describe('UsersRepository', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('findByEmailLoweredBind (O4) — OidcService.findOrCreateUser, caller already JS-lowers', () => {
|
||||
it('USERSREPO-018c: matches an already-lowered bind against a mixed-case stored email, excludes guests', async () => {
|
||||
const { user } = createUser(testDb, { email: 'Case.Test@Example.com' });
|
||||
const row = await users.findByEmailLoweredBind('case.test@example.com');
|
||||
expect(row?.id).toBe(user.id);
|
||||
expect(await users.findByEmailLoweredBind('nope@example.com')).toBeNull();
|
||||
});
|
||||
|
||||
it('USERSREPO-018d: a guest with a matching email is not returned', async () => {
|
||||
const { user } = createUser(testDb, { email: 'guest2b@example.com' });
|
||||
await updateRows(t, Users, { id: user.id }, { is_guest: 1 });
|
||||
expect(await users.findByEmailLoweredBind('guest2b@example.com')).toBeNull();
|
||||
});
|
||||
|
||||
// O4's own legacy shape: `LOWER(email) = ?` — SQLite only lowers the
|
||||
// COLUMN; the caller's already-lowered bind is compared as-is. A stored
|
||||
// non-ASCII email folds to 'josÉ@x.com' under SQLite's ASCII-only
|
||||
// LOWER(); a JS-lowered bind of the exact same spelling is
|
||||
// 'josé@x.com' — genuinely different strings, so THIS method correctly
|
||||
// does not match it. That is O4's real (pre-existing, unchanged)
|
||||
// behaviour, not a regression — findByEmailCI above is the method
|
||||
// OidcService's non-JS-lowered callers must use instead.
|
||||
it('USERSREPO-018e: a JS-lowered bind against a non-ASCII stored email does not match (this is O4 parity, not a bug)', async () => {
|
||||
createUser(testDb, { email: 'JOSÉ@x.com' });
|
||||
expect(await users.findByEmailLoweredBind('josé@x.com')).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
// Plan 3b Task 1 fix round item 7 — AuthService.demoLogin's exact statement
|
||||
// (`SELECT * FROM users WHERE email = ?`), missing from the inventory,
|
||||
// added now so Task 5 has it.
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
import { sameMailbox } from '../../../src/nest/oidc/oidc-mailbox';
|
||||
|
||||
import { describe, expect, it } from 'vitest';
|
||||
|
||||
describe('sameMailbox', () => {
|
||||
it.each([
|
||||
['Team@example.test', 'Team@example.test', true],
|
||||
['Team@example.test', 'Team@EXAMPLE.TEST', true],
|
||||
['Team@example.test', 'team@example.test', false],
|
||||
['Team@example.test', 'Team@other.test', false],
|
||||
['"team@work"@example.test', '"team@work"@EXAMPLE.TEST', true],
|
||||
['', 'Team@example.test', false],
|
||||
['Team@example.test', '', false],
|
||||
['Team@', 'Team@example.test', false],
|
||||
['Team@example.test', 'Team@', false],
|
||||
] as const)('compares %s with %s', (stored, claimed, expected) => {
|
||||
expect(sameMailbox(stored, claimed)).toBe(expected);
|
||||
});
|
||||
});
|
||||
@@ -599,13 +599,13 @@ describe('findOrCreateUser', () => {
|
||||
expect((result as { user: any }).user.id).toBe(user.id);
|
||||
});
|
||||
|
||||
it('OIDC-SVC-022: creates new user when registration is open', async () => {
|
||||
it('OIDC-SVC-022: creates a new user with the original mailbox when registration is open', async () => {
|
||||
const result = await svc.findOrCreateUser(
|
||||
{ sub: 'sub-new-1', email: 'newuser@example.com', name: 'New User' },
|
||||
{ sub: 'sub-new-1', email: 'NewUser@example.com', name: 'New User' },
|
||||
MOCK_CONFIG,
|
||||
);
|
||||
expect('user' in result).toBe(true);
|
||||
expect(await userByEmail('newuser@example.com')).not.toBeNull();
|
||||
expect(result).toMatchObject({ user: { email: 'NewUser@example.com' } });
|
||||
expect(await userByEmail('NewUser@example.com')).not.toBeNull();
|
||||
});
|
||||
|
||||
it('OIDC-SVC-023: first user gets admin role', async () => {
|
||||
@@ -673,12 +673,12 @@ describe('findOrCreateUser', () => {
|
||||
});
|
||||
|
||||
it('OIDC-SVC-025: links oidc_sub when existing user has none (verified email)', async () => {
|
||||
const { user } = createUser(testDb, { email: 'charlie@example.com' });
|
||||
const { user } = createUser(testDb, { email: 'Charlé@example.com' });
|
||||
// Ensure no oidc_sub set
|
||||
await setUserColumns(user.id, { oidc_sub: null, oidc_issuer: null });
|
||||
|
||||
await svc.findOrCreateUser(
|
||||
{ sub: 'sub-charlie-linked', email: 'charlie@example.com', name: 'Charlie', email_verified: true },
|
||||
{ sub: 'sub-charlie-linked', email: 'Charlé@EXAMPLE.COM', name: 'Charlie', email_verified: true },
|
||||
MOCK_CONFIG,
|
||||
);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user