mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-01 08:53:13 +00:00
feat(server): validate auth request bodies
DTO ratchet for the auth domain (collections sibling-commit precedent):
- shared/src/auth/auth.schema.ts gains the six missing request schemas —
mapsKey (nullable: the client clears with an explicit null), apiKeys and
settings (per-key partials, nullable key values), appSettings (open
record — the server's ADMIN_SETTINGS_KEYS allow-list and its bespoke
lockout/self-MFA 400s stay service-owned), mfaDisable, resourceToken
(the 'Invalid purpose' 400 stays service-owned) — plus spec cases.
- New nest/auth/auth.dto.ts createZodDto wrappers; all 14 raw @Body()
params typed (9 AuthController + 5 AuthPublicController), validated by
the global ZodValidationPipe.
- All 14 auth entries removed from body-contract-allow-list.ts
(PasskeyController's 4 stay until passkeyService migrates).
- auth.e2e mirrors the production pipe and pins the { error: 'field: ...' }
envelope on a shapeless login body; auth.controller.ts drops its
duplicated avatarDir const in favour of the auth.helpers export.
This commit is contained in:
@@ -6,7 +6,13 @@ import {
|
||||
changePasswordRequestSchema,
|
||||
mfaVerifyLoginRequestSchema,
|
||||
mfaEnableRequestSchema,
|
||||
mfaDisableRequestSchema,
|
||||
mcpTokenCreateRequestSchema,
|
||||
mapsKeyUpdateRequestSchema,
|
||||
apiKeysUpdateRequestSchema,
|
||||
settingsUpdateRequestSchema,
|
||||
appSettingsUpdateRequestSchema,
|
||||
resourceTokenRequestSchema,
|
||||
} from './auth.schema';
|
||||
|
||||
import { describe, it, expect } from 'vitest';
|
||||
@@ -59,7 +65,42 @@ describe('mfa + mcp-token schemas', () => {
|
||||
expect(mfaVerifyLoginRequestSchema.safeParse({ mfa_token: 't', code: '123456' }).success).toBe(true);
|
||||
expect(mfaVerifyLoginRequestSchema.safeParse({ mfa_token: 't' }).success).toBe(false);
|
||||
expect(mfaEnableRequestSchema.safeParse({ code: '123456' }).success).toBe(true);
|
||||
expect(mfaDisableRequestSchema.safeParse({ password: 'pw', code: '123456' }).success).toBe(true);
|
||||
expect(mfaDisableRequestSchema.safeParse({ code: '123456' }).success).toBe(false);
|
||||
expect(mcpTokenCreateRequestSchema.safeParse({ name: 'CLI' }).success).toBe(true);
|
||||
expect(mcpTokenCreateRequestSchema.safeParse({}).success).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('api-key / settings schemas', () => {
|
||||
it('mapsKey accepts a string, an explicit null (clear) and omission', () => {
|
||||
expect(mapsKeyUpdateRequestSchema.safeParse({ maps_api_key: 'k' }).success).toBe(true);
|
||||
expect(mapsKeyUpdateRequestSchema.safeParse({ maps_api_key: null }).success).toBe(true);
|
||||
expect(mapsKeyUpdateRequestSchema.safeParse({}).success).toBe(true);
|
||||
expect(mapsKeyUpdateRequestSchema.safeParse({ maps_api_key: 7 }).success).toBe(false);
|
||||
});
|
||||
|
||||
it('apiKeys is a per-key partial with nullable values', () => {
|
||||
expect(apiKeysUpdateRequestSchema.safeParse({ unsplash_api_key: null }).success).toBe(true);
|
||||
expect(apiKeysUpdateRequestSchema.safeParse({ maps_api_key: 'k', openweather_api_key: 'w' }).success).toBe(true);
|
||||
expect(apiKeysUpdateRequestSchema.safeParse({}).success).toBe(true);
|
||||
});
|
||||
|
||||
it('settings accepts profile + key fields, all optional', () => {
|
||||
expect(settingsUpdateRequestSchema.safeParse({ username: 'alice' }).success).toBe(true);
|
||||
expect(settingsUpdateRequestSchema.safeParse({ email: 'a@b.c', maps_api_key: null }).success).toBe(true);
|
||||
expect(settingsUpdateRequestSchema.safeParse({ username: 42 }).success).toBe(false);
|
||||
});
|
||||
|
||||
it('appSettings is an open string-keyed map (the server owns the key allow-list)', () => {
|
||||
expect(appSettingsUpdateRequestSchema.safeParse({ require_mfa: true, smtp_host: 'mail' }).success).toBe(true);
|
||||
expect(appSettingsUpdateRequestSchema.safeParse({}).success).toBe(true);
|
||||
expect(appSettingsUpdateRequestSchema.safeParse('nope').success).toBe(false);
|
||||
});
|
||||
|
||||
it('resourceToken keeps purpose an optional string (the 400 is a service rule)', () => {
|
||||
expect(resourceTokenRequestSchema.safeParse({ purpose: 'download' }).success).toBe(true);
|
||||
expect(resourceTokenRequestSchema.safeParse({}).success).toBe(true);
|
||||
expect(resourceTokenRequestSchema.safeParse({ purpose: 5 }).success).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -64,3 +64,47 @@ export const mcpTokenCreateRequestSchema = z.object({
|
||||
name: z.string().optional(),
|
||||
});
|
||||
export type McpTokenCreateRequest = z.infer<typeof mcpTokenCreateRequestSchema>;
|
||||
|
||||
// The client sends an explicit `null` to clear the stored key; optional keeps
|
||||
// the legacy accept-anything omission path (omitting also clears — service rule).
|
||||
export const mapsKeyUpdateRequestSchema = z.object({
|
||||
maps_api_key: z.string().nullable().optional(),
|
||||
});
|
||||
export type MapsKeyUpdateRequest = z.infer<typeof mapsKeyUpdateRequestSchema>;
|
||||
|
||||
// Per-key partial update: an absent key keeps the stored value, `null` clears it.
|
||||
export const apiKeysUpdateRequestSchema = z.object({
|
||||
maps_api_key: z.string().nullable().optional(),
|
||||
openweather_api_key: z.string().nullable().optional(),
|
||||
unsplash_api_key: z.string().nullable().optional(),
|
||||
});
|
||||
export type ApiKeysUpdateRequest = z.infer<typeof apiKeysUpdateRequestSchema>;
|
||||
|
||||
export const settingsUpdateRequestSchema = z.object({
|
||||
maps_api_key: z.string().nullable().optional(),
|
||||
openweather_api_key: z.string().nullable().optional(),
|
||||
unsplash_api_key: z.string().nullable().optional(),
|
||||
username: z.string().optional(),
|
||||
email: z.string().optional(),
|
||||
});
|
||||
export type SettingsUpdateRequest = z.infer<typeof settingsUpdateRequestSchema>;
|
||||
|
||||
// Deliberately an open map: the accepted keys are the server's ADMIN_SETTINGS_KEYS
|
||||
// allow-list (unknown keys are ignored there), and values arrive as strings or
|
||||
// booleans (`require_mfa` toggles) — the service owns the per-key coercion and
|
||||
// the lockout/self-MFA guards with their bespoke 400 strings.
|
||||
export const appSettingsUpdateRequestSchema = z.record(z.string(), z.unknown());
|
||||
export type AppSettingsUpdateRequest = z.infer<typeof appSettingsUpdateRequestSchema>;
|
||||
|
||||
export const mfaDisableRequestSchema = z.object({
|
||||
password: z.string(),
|
||||
code: z.string(),
|
||||
});
|
||||
export type MfaDisableRequest = z.infer<typeof mfaDisableRequestSchema>;
|
||||
|
||||
// `purpose` must be the literal 'download' today, but the 400 'Invalid purpose'
|
||||
// answer is a service rule (kept bespoke), so the wire type stays a plain string.
|
||||
export const resourceTokenRequestSchema = z.object({
|
||||
purpose: z.string().optional(),
|
||||
});
|
||||
export type ResourceTokenRequest = z.infer<typeof resourceTokenRequestSchema>;
|
||||
|
||||
Reference in New Issue
Block a user