feat(server): adopt zod DTOs for the trip-invite body contracts

New tripInviteLinkCreateRequestSchema in @trek/shared (single source of
truth), a createZodDto wrapper on the controller's raw @Body(), and the
TripInviteLinkController.create entry dropped from the body-contract
allow-list (the boot ratchet enforces it).

expires_in_days accepts number | digits-only string | null | absent — the
string branch is digits-only so garbage like "7abc" is a 400 instead of
silently parseInt-ing to 7; the empty string (blank form input, meaning no
expiry) stays accepted. The e2e registers ZodValidationPipe and pins both
the bounded-expiry and 400 paths.
This commit is contained in:
jubnl
2026-07-26 18:21:18 +02:00
parent cb0cd3300d
commit 9650ad4597
6 changed files with 64 additions and 2 deletions
@@ -145,7 +145,6 @@ export const BODY_CONTRACT_ALLOW_LIST: string[] = [
'SynologyMemoriesController.test',
'TagsController.create',
'TagsController.update',
'TripInviteLinkController.create',
'TripShareController.create',
'TripsController.addMember',
'TripsController.copy',
@@ -5,6 +5,7 @@ import { TripInviteService } from './trip-invite.service';
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
import { CurrentUser } from '../auth/current-user.decorator';
import { RateLimitService } from '../auth/rate-limit.service';
import { TripInviteLinkCreateDto } from './trip-invite.dto';
import { writeAudit, getClientIp } from '../../services/auditLog';
const RL_WINDOW = 15 * 60 * 1000;
@@ -40,7 +41,7 @@ export class TripInviteLinkController {
create(
@CurrentUser() user: User,
@Param('tripId') tripId: string,
@Body() body: { expires_in_days?: number | string | null },
@Body() body: TripInviteLinkCreateDto,
@Req() req: Request,
) {
this.requireManage(tripId, user);
@@ -0,0 +1,10 @@
import { createZodDto } from 'nestjs-zod';
import { tripInviteLinkCreateRequestSchema } from '@trek/shared';
/**
* Server-side createZodDto wrapper over the @trek/shared trip-invite contract.
* The global ZodValidationPipe (APP_PIPE in app.module.ts) validates any
* @Body() parameter typed with this class by metatype — the Zod schema in
* shared/ remains the single source of truth for the wire contract.
*/
export class TripInviteLinkCreateDto extends createZodDto(tripInviteLinkCreateRequestSchema) {}
+1
View File
@@ -33,6 +33,7 @@ export * from './assignment/assignment.schema';
export * from './place/place.schema';
export * from './collection/collection.schema';
export * from './trip/trip.schema';
export * from './trip-invite/trip-invite.schema';
export * from './collab/collab.schema';
export * from './file/file.schema';
export * from './journey/journey.schema';
@@ -0,0 +1,33 @@
import { tripInviteLinkCreateRequestSchema } from './trip-invite.schema';
import { describe, it, expect } from 'vitest';
describe('tripInviteLinkCreateRequestSchema', () => {
it('accepts an empty body (no expiry chosen)', () => {
expect(tripInviteLinkCreateRequestSchema.safeParse({}).success).toBe(true);
});
it('accepts an explicit null (the client always sends the key)', () => {
expect(tripInviteLinkCreateRequestSchema.safeParse({ expires_in_days: null }).success).toBe(true);
});
it('accepts a number and a digits-only string (legacy form input)', () => {
expect(tripInviteLinkCreateRequestSchema.safeParse({ expires_in_days: 7 }).success).toBe(true);
expect(tripInviteLinkCreateRequestSchema.safeParse({ expires_in_days: '7' }).success).toBe(true);
});
it('accepts the empty string (blank form input means no expiry)', () => {
expect(tripInviteLinkCreateRequestSchema.safeParse({ expires_in_days: '' }).success).toBe(true);
});
it('rejects non-numeric strings instead of parseInt-ing them', () => {
expect(tripInviteLinkCreateRequestSchema.safeParse({ expires_in_days: '7abc' }).success).toBe(false);
expect(tripInviteLinkCreateRequestSchema.safeParse({ expires_in_days: 'never' }).success).toBe(false);
});
it('rejects non-scalar values', () => {
expect(tripInviteLinkCreateRequestSchema.safeParse({ expires_in_days: {} }).success).toBe(false);
expect(tripInviteLinkCreateRequestSchema.safeParse({ expires_in_days: [7] }).success).toBe(false);
expect(tripInviteLinkCreateRequestSchema.safeParse({ expires_in_days: true }).success).toBe(false);
});
});
@@ -0,0 +1,18 @@
import { z } from 'zod';
/**
* Trip invite-link API contract (#1143).
*
* Members with 'share_manage' create/rotate the trip's single invite token
* under POST /api/trips/:tripId/invite-link. `expires_in_days` accepts
* number | digits-only string | null | absent on the wire: the client always
* sends the key (null when no expiry was chosen — see tripInviteApi.createLink),
* and the legacy raw-body route also took numeric strings from form input (the
* empty string meaning "no expiry"). The string branch is digits-only so
* garbage like "7abc" is a 400 instead of silently parseInt-ing to 7; the
* controller still owns the number coercion (non-positive → no expiry).
*/
export const tripInviteLinkCreateRequestSchema = z.object({
expires_in_days: z.union([z.number(), z.string().regex(/^\d*$/), z.null()]).optional(),
});
export type TripInviteLinkCreateRequest = z.infer<typeof tripInviteLinkCreateRequestSchema>;