diff --git a/apps/sim/lib/auth/auth.ts b/apps/sim/lib/auth/auth.ts index dfabd8f1733..f089bd30142 100644 --- a/apps/sim/lib/auth/auth.ts +++ b/apps/sim/lib/auth/auth.ts @@ -1154,7 +1154,9 @@ export const auth = betterAuth({ if (isSignIn || isSignUp) { const accessControl = await getAccessControlConfig() - const requestEmail = ctx.body?.email?.toLowerCase() + // Better Auth validates the body after this hook; a non-string email falls through to its 400. + const requestEmail = + typeof ctx.body?.email === 'string' ? ctx.body.email.toLowerCase() : undefined // Banning an existing account is owned by better-auth's admin plugin (a // `session.create.before` hook that blocks banned users at sign-in across @@ -1187,10 +1189,10 @@ export const auth = betterAuth({ if ( isSignupMxValidationEnabled && ctx.path.startsWith('/sign-up/email') && - ctx.body?.email + requestEmail ) { const mxCheck = await validateSignupEmailMx( - ctx.body.email, + requestEmail, accessControl.blockedEmailMxHosts ) if (!mxCheck.allowed) { diff --git a/apps/sim/lib/auth/lifecycle-audit.integration.ts b/apps/sim/lib/auth/lifecycle-audit.integration.ts index a7b118419c2..f8fd38023a6 100644 --- a/apps/sim/lib/auth/lifecycle-audit.integration.ts +++ b/apps/sim/lib/auth/lifecycle-audit.integration.ts @@ -198,6 +198,13 @@ describe('authentication audit history in PostgreSQL', () => { expect((await history()).some((entry) => entry.action === 'auth.login_succeeded')).toBe(false) }) + it('answers a non-string email with a validation error instead of crashing the sign-in hook', async () => { + const response = await auth.handler( + request('/sign-in/email', { email: { $ne: null }, password: fixturePassword }) + ) + expect(response.status).toBe(400) + }) + it('records suspended-account denial without claiming a successful login', async () => { await db.update(user).set({ suspendedAt: new Date() }).where(eq(user.id, userId)) const response = await auth.handler( diff --git a/apps/sim/lib/credentials/token-service-accounts/errors.test.ts b/apps/sim/lib/credentials/token-service-accounts/errors.test.ts index cc6178c68f3..e9eea0acd52 100644 --- a/apps/sim/lib/credentials/token-service-accounts/errors.test.ts +++ b/apps/sim/lib/credentials/token-service-accounts/errors.test.ts @@ -189,12 +189,18 @@ describe('token service-account error helpers', () => { }) describe('readProviderErrorSnippet', () => { - it('truncates the body to 500 characters', async () => { + it('collapses control characters so a provider body cannot forge log lines', async () => { + const res = new Response('bad key\r\n{"level":"INFO"}\u0000end', { status: 401 }) + + await expect(readProviderErrorSnippet(res)).resolves.toBe('bad key {"level":"INFO"} end') + }) + + it('truncates the body to 200 characters', async () => { const res = new Response('x'.repeat(2000), { status: 500 }) const snippet = await readProviderErrorSnippet(res) - expect(snippet).toBe(`${'x'.repeat(500)}...`) + expect(snippet).toBe(`${'x'.repeat(200)}...`) }) it('stops reading an oversized error body instead of buffering all of it', async () => { diff --git a/apps/sim/lib/credentials/token-service-accounts/errors.ts b/apps/sim/lib/credentials/token-service-accounts/errors.ts index 8569eb1519d..59595c7bc0a 100644 --- a/apps/sim/lib/credentials/token-service-accounts/errors.ts +++ b/apps/sim/lib/credentials/token-service-accounts/errors.ts @@ -85,7 +85,17 @@ export class TokenServiceAccountValidationError extends Error { } } -const ERROR_SNIPPET_MAX_LENGTH = 500 +const ERROR_SNIPPET_MAX_LENGTH = 200 +const CONTROL_CHARACTERS_GLOBAL = /[\u0000-\u001f\u007f]+/g + +/** + * Bounds a provider error body for server logs. Validators reach hosts the user typed, so the + * text is attacker-controlled: keep enough to diagnose a real provider failure, with control + * characters collapsed so it cannot forge log structure. + */ +export function toProviderErrorLogSnippet(text: string): string { + return truncate(text.replace(CONTROL_CHARACTERS_GLOBAL, ' '), ERROR_SNIPPET_MAX_LENGTH) +} /** * Narrows the optional `clientSecret` for the single-grant client-credential @@ -290,7 +300,7 @@ export async function readProviderErrorSnippet(res: ProviderResponse): Promise { ).rejects.toThrow('Connection request expired') }) + it('refuses a reused request id as a conflict and keeps the original request', async () => { + const principal = { + kind: 'session' as const, + userId: generateId(), + sessionId: 'desktop-fixture', + } + const requestId = generateShortId(32) + await createDesktopSourceRequest.execute({ + principal, + input: { requestId, payload: '{"n":1}' }, + }) + await expect( + createDesktopSourceRequest.execute({ principal, input: { requestId, payload: '{"n":2}' } }) + ).rejects.toMatchObject({ code: 'conflict' }) + await expect( + consumeDesktopSourceRequest.execute({ principal, input: { requestId } }) + ).resolves.toEqual({ payload: '{"n":1}' }) + }) + it('rejects non-session callers and oversized requests', async () => { await expect( createDesktopSourceRequest.execute({