Skip to content

Commit fbe5bfd

Browse files
committed
test(auth): pin every gate of the invitation proof door
Each gate refuses before anything is written and leaves the proof usable; the anonymous session-mode refusal asserts its code. Claude-Session: https://claude.ai/code/session_01FEkQ5nNrLRm8ssuwWcBVcw Co-authored-by: Claude <noreply@anthropic.com>
1 parent 9b95ba9 commit fbe5bfd

1 file changed

Lines changed: 180 additions & 1 deletion

File tree

‎packages/plugins/plugin-auth/src/invitation-one-step-acceptance-34257.test.ts‎

Lines changed: 180 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
// plugin, audience gate, membership reconciler, ObjectQL adapter), over the
77
// same minimal engine double the other end-to-end auth-manager suites use.
88

9+
import { createHash } from 'node:crypto';
910
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
1011
import { assertEngineDeleteDispatch, assertEngineUpdateDispatch, assertEngineFindOnePredicate } from '@objectstack/objectql';
1112
import { AuthManager } from './auth-manager';
@@ -165,7 +166,7 @@ const proofOf = (mail: SentMail): string => {
165166
return decodeURIComponent(match![1]!);
166167
};
167168

168-
const boot = async () => {
169+
const boot = async (options: Record<string, unknown> = {}) => {
169170
const engine = createMemoryEngine();
170171
engine.tables.set('sys_organization', [
171172
{ id: DEFAULT_ORG, name: 'Default', slug: 'default' },
@@ -177,6 +178,7 @@ const boot = async () => {
177178
dataEngine: engine as any,
178179
membershipPolicy: 'auto',
179180
getTenancy: () => singleOrgTenancy(),
181+
...options,
180182
});
181183
const sent: SentMail[] = [];
182184
manager.setEmailService({
@@ -283,6 +285,7 @@ describe('#34257 — accepting with the proof', () => {
283285

284286
const anonymous = await door(manager, { invitationId: invitation.id, name: 'X', password: PASSWORD });
285287
expect(anonymous.status).toBe(401);
288+
expect((await anonymous.json()).code).toBe('UNAUTHENTICATED');
286289

287290
expect(userRow(engine, 'unverified@example.com')?.email_verified).toBeFalsy();
288291
expect(invitationOf(engine, 'unverified@example.com')?.status).toBe('pending');
@@ -435,3 +438,179 @@ describe('#34257 — a signed-in return accepts only a verified address', () =>
435438
expect(invitationOf(engine, 'unsure@example.com')?.status).toBe('pending');
436439
});
437440
});
441+
442+
// The tables a redemption writes: the account, its credential, its sessions,
443+
// the membership, the invitation and the proof row.
444+
const WRITTEN_TABLES = ['sys_user', 'sys_account', 'sys_session', 'sys_member', 'sys_invitation', 'sys_verification'];
445+
const writtenState = (engine: MemoryEngine) =>
446+
JSON.stringify(WRITTEN_TABLES.map((table) => [table, engine.tables.get(table) ?? []]));
447+
448+
const expectAnswer = async (res: Response, status: number, code: string) => {
449+
const text = await res.clone().text();
450+
expect(res.status, text).toBe(status);
451+
expect(JSON.parse(text).code, text).toBe(code);
452+
};
453+
454+
const expectSignIn = async (res: Response) => {
455+
const text = await res.clone().text();
456+
expect(res.status, text).toBe(200);
457+
expect(JSON.parse(text).data).toEqual({ outcome: 'sign_in' });
458+
};
459+
460+
const expectAccepted = async (res: Response) => {
461+
const text = await res.clone().text();
462+
expect(res.status, text).toBe(200);
463+
expect(JSON.parse(text).data).toEqual({ outcome: 'accepted', organizationId: PARTNER_ORG });
464+
};
465+
466+
describe('#34257 — every gate refuses before anything is written, and the proof stays usable', () => {
467+
beforeEach(() => {
468+
vi.spyOn(console, 'info').mockImplementation(() => {});
469+
vi.spyOn(console, 'warn').mockImplementation(() => {});
470+
vi.spyOn(console, 'error').mockImplementation(() => {});
471+
});
472+
afterEach(() => vi.restoreAllMocks());
473+
474+
it('a new address is refused where email sign-up is switched off', async () => {
475+
const { engine, manager, invite } = await boot({ emailAndPassword: { disableSignUp: true } });
476+
const { invitation, proof } = await invite('closed@example.com');
477+
const before = writtenState(engine);
478+
479+
await expectAnswer(await door(manager, { invitationId: invitation.id, proof, name: 'C', password: PASSWORD }), 403, 'PERMISSION_DENIED');
480+
expect(writtenState(engine)).toBe(before);
481+
482+
manager.applyConfigPatch({ emailAndPassword: { disableSignUp: false } } as never);
483+
await expectAccepted(await door(manager, { invitationId: invitation.id, proof, name: 'C', password: PASSWORD }));
484+
});
485+
486+
it('a new address is refused in SSO-only mode', async () => {
487+
const { engine, manager, invite } = await boot({ ssoOnlyMode: true });
488+
const { invitation, proof } = await invite('sso-new@example.com');
489+
const before = writtenState(engine);
490+
491+
await expectAnswer(await door(manager, { invitationId: invitation.id, proof, name: 'S', password: PASSWORD }), 403, 'PERMISSION_DENIED');
492+
expect(writtenState(engine)).toBe(before);
493+
494+
manager.applyConfigPatch({ ssoOnlyMode: false } as never);
495+
await expectAccepted(await door(manager, { invitationId: invitation.id, proof, name: 'S', password: PASSWORD }));
496+
});
497+
498+
it('an unverified account in SSO-only mode signs in as today, and the proof stays unspent', async () => {
499+
const { engine, manager, invite } = await boot();
500+
const { invitation, proof } = await invite('sso-held@example.com');
501+
await signUp(manager, engine, 'sso-held@example.com');
502+
manager.applyConfigPatch({ ssoOnlyMode: true } as never);
503+
const before = writtenState(engine);
504+
505+
await expectSignIn(await door(manager, { invitationId: invitation.id, proof, name: 'S', password: NEW_PASSWORD }));
506+
expect(writtenState(engine)).toBe(before);
507+
508+
manager.applyConfigPatch({ ssoOnlyMode: false } as never);
509+
await expectAccepted(await door(manager, { invitationId: invitation.id, proof, name: 'S', password: NEW_PASSWORD }));
510+
});
511+
512+
it('an unverified account with a second factor signs in as today, and the proof stays unspent', async () => {
513+
const { engine, manager, invite } = await boot({ plugins: { twoFactor: true } });
514+
const { invitation, proof } = await invite('two-factor@example.com');
515+
await signUp(manager, engine, 'two-factor@example.com');
516+
userRow(engine, 'two-factor@example.com')!.two_factor_enabled = true;
517+
const before = writtenState(engine);
518+
519+
await expectSignIn(await door(manager, { invitationId: invitation.id, proof, name: 'T', password: NEW_PASSWORD }));
520+
expect(writtenState(engine)).toBe(before);
521+
522+
userRow(engine, 'two-factor@example.com')!.two_factor_enabled = false;
523+
await expectAccepted(await door(manager, { invitationId: invitation.id, proof, name: 'T', password: NEW_PASSWORD }));
524+
});
525+
526+
it.each([
527+
['provisioned by an identity provider', 'idp_provisioned'],
528+
['pending its first SSO sign-in', 'idp_pending'],
529+
])('an unverified account %s signs in as today, and the proof stays unspent', async (_label, source) => {
530+
const { engine, manager, invite } = await boot();
531+
const email = `directory-${source}@example.com`;
532+
const { invitation, proof } = await invite(email);
533+
await signUp(manager, engine, email);
534+
userRow(engine, email)!.source = source;
535+
const before = writtenState(engine);
536+
537+
await expectSignIn(await door(manager, { invitationId: invitation.id, proof, name: 'D', password: NEW_PASSWORD }));
538+
expect(writtenState(engine)).toBe(before);
539+
540+
userRow(engine, email)!.source = null;
541+
await expectAccepted(await door(manager, { invitationId: invitation.id, proof, name: 'D', password: NEW_PASSWORD }));
542+
});
543+
544+
it('a password under the minimum length is refused', async () => {
545+
const { engine, manager, invite } = await boot();
546+
const { invitation, proof } = await invite('short@example.com');
547+
const before = writtenState(engine);
548+
549+
await expectAnswer(await door(manager, { invitationId: invitation.id, proof, name: 'S', password: 'Ab1!' }), 400, 'PASSWORD_POLICY_VIOLATION');
550+
expect(writtenState(engine)).toBe(before);
551+
552+
await expectAccepted(await door(manager, { invitationId: invitation.id, proof, name: 'S', password: PASSWORD }));
553+
});
554+
555+
it('a password the complexity policy refuses is refused', async () => {
556+
const { engine, manager, invite } = await boot({ passwordRequireComplexity: true });
557+
const { invitation, proof } = await invite('simple@example.com');
558+
const before = writtenState(engine);
559+
560+
await expectAnswer(
561+
await door(manager, { invitationId: invitation.id, proof, name: 'S', password: 'onlylowercaseletters' }),
562+
400,
563+
'PASSWORD_POLICY_VIOLATION',
564+
);
565+
expect(writtenState(engine)).toBe(before);
566+
567+
await expectAccepted(await door(manager, { invitationId: invitation.id, proof, name: 'S', password: PASSWORD }));
568+
});
569+
570+
it('a breached password is refused, and the proof stays usable', async () => {
571+
const BREACHED = 'Br3ached!Passw0rd-list';
572+
const digest = createHash('sha1').update(BREACHED).digest('hex').toUpperCase();
573+
vi.spyOn(globalThis, 'fetch').mockImplementation(async (input: any) => {
574+
const url = String(input instanceof Request ? input.url : input);
575+
if (!url.startsWith('https://api.pwnedpasswords.com/range/')) throw new Error(`unexpected fetch: ${url}`);
576+
const listed = url.endsWith(`/${digest.slice(0, 5)}`) ? `${digest.slice(5)}:42` : '0000000000000000000000000000000000A:1';
577+
return new Response(listed, { status: 200, headers: { 'content-type': 'text/plain' } });
578+
});
579+
const { engine, manager, invite } = await boot({ plugins: { passwordRejectBreached: true } });
580+
const { invitation, proof } = await invite('breached@example.com');
581+
const before = writtenState(engine);
582+
583+
await expectAnswer(await door(manager, { invitationId: invitation.id, proof, name: 'B', password: BREACHED }), 400, 'PASSWORD_COMPROMISED');
584+
expect(writtenState(engine)).toBe(before);
585+
586+
await expectAccepted(await door(manager, { invitationId: invitation.id, proof, name: 'B', password: PASSWORD }));
587+
});
588+
589+
it('a new address the audience admission refuses is refused', async () => {
590+
const { engine, manager, invite } = await boot();
591+
const { invitation, proof } = await invite('refused@example.com');
592+
// A posture outside the vocabulary refuses every self-serve creation. The
593+
// entry validation refuses one at the door, so it is set behind it.
594+
const config = (manager as unknown as { config: Record<string, unknown> }).config;
595+
const standing = config.audience;
596+
config.audience = { posture: 'not_a_posture' };
597+
const before = writtenState(engine);
598+
599+
await expectAnswer(await door(manager, { invitationId: invitation.id, proof, name: 'R', password: PASSWORD }), 403, 'AUTH_CONFIG_ERROR');
600+
expect(writtenState(engine)).toBe(before);
601+
602+
config.audience = standing;
603+
await expectAccepted(await door(manager, { invitationId: invitation.id, proof, name: 'R', password: PASSWORD }));
604+
});
605+
606+
it('a new address with no name is refused', async () => {
607+
const { engine, manager, invite } = await boot();
608+
const { invitation, proof } = await invite('nameless@example.com');
609+
const before = writtenState(engine);
610+
611+
await expectAnswer(await door(manager, { invitationId: invitation.id, proof, name: ' ', password: PASSWORD }), 400, 'VALIDATION_ERROR');
612+
expect(writtenState(engine)).toBe(before);
613+
614+
await expectAccepted(await door(manager, { invitationId: invitation.id, proof, name: 'Named', password: PASSWORD }));
615+
});
616+
});

0 commit comments

Comments
 (0)