mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-03 09:38:54 +00:00
feat(server): validate notification request bodies
notifications.dto.ts wraps the @trek/shared request schemas via createZodDto and types all five @Body() params; the five NotificationsController entries leave the body-contract allow-list (ratchet gate enforced). Contract catch: the client sends server/token: null on test-ntfy to mean 'use the saved value', so testNtfyRequestSchema gains .nullable() there (spec pinned). The inline response-enum and url-type checks die — malformed bodies now get the pipe's standard envelope; valid bodies behave byte-identically. Two integration cases pinning pre-ratchet tolerances (flat notify_* preferences body, body-less test-smtp POST) update to the current wire contract.
This commit is contained in:
@@ -54,11 +54,6 @@ export const BODY_CONTRACT_ALLOW_LIST: string[] = [
|
||||
'JourneyController.uploadEntryPhotos',
|
||||
'JourneyController.uploadGalleryVideo',
|
||||
'LlmLocalController.pull',
|
||||
'NotificationsController.respond',
|
||||
'NotificationsController.setPreferences',
|
||||
'NotificationsController.testNtfy',
|
||||
'NotificationsController.testSmtp',
|
||||
'NotificationsController.testWebhook',
|
||||
'OauthApiController.authorize',
|
||||
'OauthApiController.createClient',
|
||||
'PluginRoutesController.route',
|
||||
|
||||
@@ -14,6 +14,13 @@ import {
|
||||
import type { ChannelTestResult, UnreadCountResult } from '@trek/shared';
|
||||
import type { User } from '../../types';
|
||||
import { NotificationsService } from './notifications.service';
|
||||
import {
|
||||
PreferencesUpdateDto,
|
||||
TestSmtpDto,
|
||||
TestWebhookDto,
|
||||
TestNtfyDto,
|
||||
NotificationRespondDto,
|
||||
} from './notifications.dto';
|
||||
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
|
||||
import { CurrentUser } from '../auth/current-user.decorator';
|
||||
|
||||
@@ -31,6 +38,10 @@ const MASKED = '••••••••';
|
||||
* codes. POSTs that answer with res.json stay 200 (Nest would default to 201).
|
||||
* The static /in-app/read-all and /in-app/all routes are declared before the
|
||||
* /in-app/:id routes so they win over the param, matching the legacy order.
|
||||
* Bodies validate via notifications.dto.ts (@trek/shared schemas through the
|
||||
* global ZodValidationPipe) — malformed bodies now get the pipe's standard
|
||||
* { error: 'field: message; …' } envelope instead of the old inline checks
|
||||
* (the sanctioned ratchet behavior); valid bodies behave byte-identically.
|
||||
*/
|
||||
@Controller('api/notifications')
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@@ -43,24 +54,24 @@ export class NotificationsController {
|
||||
}
|
||||
|
||||
@Put('preferences')
|
||||
setPreferences(@CurrentUser() user: User, @Body() body: Record<string, Record<string, boolean>>) {
|
||||
setPreferences(@CurrentUser() user: User, @Body() body: PreferencesUpdateDto) {
|
||||
this.notifications.setPreferences(user.id, body);
|
||||
return this.notifications.getPreferences(user.id, user.role);
|
||||
}
|
||||
|
||||
@Post('test-smtp')
|
||||
@HttpCode(200)
|
||||
async testSmtp(@CurrentUser() user: User, @Body('email') email?: string): Promise<ChannelTestResult> {
|
||||
async testSmtp(@CurrentUser() user: User, @Body() body: TestSmtpDto): Promise<ChannelTestResult> {
|
||||
if (user.role !== 'admin') {
|
||||
throw new HttpException({ error: 'Admin only' }, 403);
|
||||
}
|
||||
return this.notifications.testSmtp(email || user.email);
|
||||
return this.notifications.testSmtp(body.email || user.email);
|
||||
}
|
||||
|
||||
@Post('test-webhook')
|
||||
@HttpCode(200)
|
||||
async testWebhook(@CurrentUser() user: User, @Body('url') urlInput?: unknown): Promise<ChannelTestResult> {
|
||||
let url = urlInput;
|
||||
async testWebhook(@CurrentUser() user: User, @Body() body: TestWebhookDto): Promise<ChannelTestResult> {
|
||||
let url: string | null | undefined = body.url;
|
||||
if (!url || url === MASKED) {
|
||||
url = this.notifications.userWebhookUrl(user.id);
|
||||
if (!url && user.role === 'admin') url = this.notifications.adminWebhookUrl();
|
||||
@@ -68,9 +79,6 @@ export class NotificationsController {
|
||||
throw new HttpException({ error: 'No webhook URL configured' }, 400);
|
||||
}
|
||||
}
|
||||
if (typeof url !== 'string') {
|
||||
throw new HttpException({ error: 'url must be a string' }, 400);
|
||||
}
|
||||
try {
|
||||
new URL(url);
|
||||
} catch {
|
||||
@@ -81,12 +89,8 @@ export class NotificationsController {
|
||||
|
||||
@Post('test-ntfy')
|
||||
@HttpCode(200)
|
||||
async testNtfy(
|
||||
@CurrentUser() user: User,
|
||||
@Body('topic') topic?: string,
|
||||
@Body('server') server?: string,
|
||||
@Body('token') token?: string,
|
||||
): Promise<ChannelTestResult> {
|
||||
async testNtfy(@CurrentUser() user: User, @Body() body: TestNtfyDto): Promise<ChannelTestResult> {
|
||||
const { topic, server, token } = body;
|
||||
const userCfg = this.notifications.userNtfyConfig(user.id);
|
||||
const adminCfg = this.notifications.adminNtfyConfig();
|
||||
|
||||
@@ -176,13 +180,10 @@ export class NotificationsController {
|
||||
async respond(
|
||||
@CurrentUser() user: User,
|
||||
@Param('id') idParam: string,
|
||||
@Body('response') response?: unknown,
|
||||
@Body() body: NotificationRespondDto,
|
||||
): Promise<{ success: boolean; notification: unknown }> {
|
||||
const id = this.parseId(idParam);
|
||||
if (response !== 'positive' && response !== 'negative') {
|
||||
throw new HttpException({ error: 'response must be "positive" or "negative"' }, 400);
|
||||
}
|
||||
const result = await this.notifications.respond(id, user.id, response);
|
||||
const result = await this.notifications.respond(id, user.id, body.response);
|
||||
if (!result.success) {
|
||||
throw new HttpException({ error: result.error }, 400);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
import { createZodDto } from 'nestjs-zod';
|
||||
import {
|
||||
preferencesUpdateRequestSchema,
|
||||
testSmtpRequestSchema,
|
||||
testWebhookRequestSchema,
|
||||
testNtfyRequestSchema,
|
||||
notificationRespondRequestSchema,
|
||||
} from '@trek/shared';
|
||||
|
||||
/**
|
||||
* Server-side createZodDto wrappers over the @trek/shared notification
|
||||
* contracts. The global ZodValidationPipe (APP_PIPE in app.module.ts)
|
||||
* validates any @Body() parameter typed with one of these classes by
|
||||
* metatype — the Zod schemas in shared/ remain the single source of truth
|
||||
* for the wire contract.
|
||||
*/
|
||||
export class PreferencesUpdateDto extends createZodDto(preferencesUpdateRequestSchema) {}
|
||||
export class TestSmtpDto extends createZodDto(testSmtpRequestSchema) {}
|
||||
export class TestWebhookDto extends createZodDto(testWebhookRequestSchema) {}
|
||||
export class TestNtfyDto extends createZodDto(testNtfyRequestSchema) {}
|
||||
export class NotificationRespondDto extends createZodDto(notificationRespondRequestSchema) {}
|
||||
Reference in New Issue
Block a user