Matching by email skips admins and anyone with a connected account, in both directions. A connected directory account is only swapped by an admin, and an OpenID connect finishes only in the browser whose session asked.
This commit is contained in:
1 parent
8a1f17099d
commit
1db5aa22dc
5 files changed
+147
-26
No files matched your search
@@ -1,12 +1,13 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
// 12 §4.3: who a directory entry may connect to by email, who may disconnect it (§4.5), and a
|
||||
// directory that's down counting as a failed sign-in (§4.2).
|
||||
// directory that's down counting as a failed sign-in (§4.2). 08 §7: no OpenID user by email, no
|
||||
// swapping the connected account.
|
||||
import { eq } from 'drizzle-orm';
|
||||
import { afterAll, beforeAll, describe, expect, it } from 'vitest';
|
||||
import { resetLoginLimiter } from '../src/auth/login-limiter.ts';
|
||||
import { userIdentities, users } from '../src/db/schema.ts';
|
||||
import { setSetting } from '../src/services/settings.ts';
|
||||
import { directorySignIn } from '../src/sso/directory.ts';
|
||||
import { connectDirectory, directorySignIn } from '../src/sso/directory.ts';
|
||||
import type { DirectoryEntry } from '../src/sso/ldap.ts';
|
||||
import { hasTestDb } from './db.ts';
|
||||
import { ADMIN, H, createTestApp } from './app.ts';
|
||||
@@ -52,6 +53,38 @@ describe.skipIf(!hasTestDb)('directory accounts by email (12 §4.3)', () => {
|
||||
}
|
||||
});
|
||||
|
||||
it('never connects by email someone who signs in with OpenID (08 §7 #1)', async () => {
|
||||
// A directory that lets people edit their own mail: Mallory's entry, the victim's email.
|
||||
const victim = await t.addUser('oidc-only@example.com', 'unused-pw-2');
|
||||
await t.db.update(users).set({ passwordHash: null }).where(eq(users.id, victim.id));
|
||||
await t.db
|
||||
.insert(userIdentities)
|
||||
.values({ userId: victim.id, kind: 'oidc', subject: 'https://idp victim-sub' });
|
||||
expect(await directorySignIn(t.db, log, entry(victim.email), '127.0.0.1')).toBeNull();
|
||||
expect(await connected(victim.id)).toBe(1); // the OpenID account only
|
||||
});
|
||||
|
||||
it('a connected directory account is only swapped by an admin (08 §7 #5)', async () => {
|
||||
const u = await t.addUser('swapper@example.com', 'swapper-password-1');
|
||||
expect(await connectDirectory(t.db, log, u.id, entry(u.email), '127.0.0.1')).toBe('connected');
|
||||
const [first] = await t.db.select().from(userIdentities).where(eq(userIdentities.userId, u.id));
|
||||
// The same account again is fine; another one (a shared mailbox) is not.
|
||||
expect(
|
||||
await connectDirectory(
|
||||
t.db,
|
||||
log,
|
||||
u.id,
|
||||
{ ...entry(u.email), id: first!.subject },
|
||||
'127.0.0.1',
|
||||
),
|
||||
).toBe('connected');
|
||||
expect(await connectDirectory(t.db, log, u.id, entry('shared@example.com'), '127.0.0.1')).toBe(
|
||||
'connected_other',
|
||||
);
|
||||
const now = await t.db.select().from(userIdentities).where(eq(userIdentities.userId, u.id));
|
||||
expect(now.map((i) => i.subject)).toEqual([first!.subject]);
|
||||
});
|
||||
|
||||
it('only an admin can disconnect a directory account (a leaver must stay checked)', async () => {
|
||||
const u = await t.addUser('leaver@example.com', 'leaver-password-1');
|
||||
await t.db.insert(userIdentities).values({ userId: u.id, kind: 'ldap', subject: 'leaver-id' });
|
||||
|
||||
@@ -207,6 +207,56 @@ for (const alg of ['RS256', 'ES256'] as const)
|
||||
expect(ids).toMatchObject([{ kind: 'oidc', subject: `${idp.issuer} u-cara` }]);
|
||||
});
|
||||
|
||||
it('never matches by email an admin, or someone with a directory account (08 §7 #2)', async () => {
|
||||
const [boss, dir] = await t.db
|
||||
.insert(users)
|
||||
.values([
|
||||
{ email: 'diradmin@example.com', displayName: 'Boss', passwordHash: null, role: 'admin' },
|
||||
{ email: 'diruser@example.com', displayName: 'Dir', passwordHash: null },
|
||||
])
|
||||
.returning();
|
||||
await t.db
|
||||
.insert(userIdentities)
|
||||
.values({ userId: dir!.id, kind: 'ldap', subject: 'dir-user-id' });
|
||||
for (const u of [boss!, dir!]) {
|
||||
idp.st.user = { sub: `u-${u.email}`, email: u.email, email_verified: true };
|
||||
const { callback } = await flow();
|
||||
expect(errorOf(callback!)).toBe('not_connected');
|
||||
expect(session(callback!)).toBeNull();
|
||||
}
|
||||
});
|
||||
|
||||
it('connect finishes only in the browser whose session asked (08 §7 #8)', async () => {
|
||||
// Mallory starts Connect, then plants her sp_oidc cookie in Vic's browser and sends him on.
|
||||
await t.addUser('mallory@example.com', 'mallory-password-1');
|
||||
const [m] = await t.db.select().from(users).where(eq(users.email, 'mallory@example.com'));
|
||||
await t.addUser('vic@example.com', 'vic-password-123');
|
||||
const mallory = await t.login('mallory@example.com', 'mallory-password-1');
|
||||
const vic = await t.login('vic@example.com', 'vic-password-123');
|
||||
idp.st.user = { sub: 'u-vic', email: 'vic@example.com', email_verified: true };
|
||||
for (const theirs of [vic, '']) {
|
||||
const start = await t.app.inject({
|
||||
method: 'POST',
|
||||
url: '/api/auth/oidc/connect',
|
||||
headers: { ...H, cookie: mallory },
|
||||
payload: {},
|
||||
});
|
||||
const pending = start.cookies.find((c) => c.name === 'sp_oidc')!;
|
||||
const atIdp = await fetch((start.json() as { url: string }).url, { redirect: 'manual' });
|
||||
const back = new URL(atIdp.headers.get('location')!);
|
||||
const callback = await t.app.inject({
|
||||
url: back.pathname + back.search,
|
||||
headers: { cookie: [`sp_oidc=${pending.value}`, theirs].filter(Boolean).join('; ') },
|
||||
});
|
||||
expect(errorOf(callback)).toBe('expired');
|
||||
const ids = await t.db
|
||||
.select()
|
||||
.from(userIdentities)
|
||||
.where(eq(userIdentities.userId, m!.id));
|
||||
expect(ids).toEqual([]);
|
||||
}
|
||||
});
|
||||
|
||||
it('trustEmail matches an unverified email', async () => {
|
||||
await t.db
|
||||
.insert(users)
|
||||
|
||||
Reference in new issue
Block a user