π¨ The ultimate agentic social media scheduling tool π€
TypeScript nextjsopen-sourceopen-source-social-media-scheduling-toolosspostiz-cloudpostiz-hostedredisscheduling-toolsocial-media-scheduling-tooltypescript
feat: protect webhooks
Nevo David0ad89ccd26b1
diff --git a/apps/backend/src/api/routes/webhooks.controller.ts b/apps/backend/src/api/routes/webhooks.controller.tsindex 2b141887f..0cc1c66a8 100644--- a/apps/backend/src/api/routes/webhooks.controller.ts+++ b/apps/backend/src/api/routes/webhooks.controller.ts@@ -14,8 +14,7 @@ import { ApiTags } from '@nestjs/swagger';import { WebhooksService } from '@gitroom/nestjs-libraries/database/prisma/webhooks/webhooks.service';import { CheckPolicies } from '@gitroom/backend/services/auth/permissions/permissions.ability';import {- UpdateDto,- WebhooksDto,+ OnlyURL, UpdateDto, WebhooksDto} from '@gitroom/nestjs-libraries/dtos/webhooks/webhooks.dto';import { AuthorizationActions, Sections } from '@gitroom/backend/services/auth/permissions/permission.exception.class';@@ -55,9 +54,9 @@ export class WebhookController {}@Post('/send')- async sendWebhook(@Body() body: any, @Query('url') url: string) {+ async sendWebhook(@Body() body: any, @Query() query: OnlyURL) {try {- await fetch(url, {+ await fetch(query.url, {method: 'POST',body: JSON.stringify(body),headers: { 'Content-Type': 'application/json' },diff --git a/apps/frontend/src/components/webhooks/webhooks.tsx b/apps/frontend/src/components/webhooks/webhooks.tsxindex 194dbb25e..de11083b5 100644--- a/apps/frontend/src/components/webhooks/webhooks.tsx+++ b/apps/frontend/src/components/webhooks/webhooks.tsx@@ -203,7 +203,7 @@ export const AddOrEditWebhook: FC<{await fetch(`/webhooks/send?url=${encodeURIComponent(url)}`, {method: 'POST',headers: {- contentType: 'application/json',+ 'Content-Type': 'application/json',},body: JSON.stringify([{diff --git a/libraries/nestjs-libraries/src/dtos/webhooks/webhook.url.validator.ts b/libraries/nestjs-libraries/src/dtos/webhooks/webhook.url.validator.tsnew file mode 100644index 000000000..4a949ad17--- /dev/null+++ b/libraries/nestjs-libraries/src/dtos/webhooks/webhook.url.validator.ts@@ -0,0 +1,126 @@+import {+ registerDecorator,+ ValidationArguments,+ ValidationOptions,+ ValidatorConstraint,+ ValidatorConstraintInterface,+} from 'class-validator';+import { URL } from 'node:url';+import dns from 'node:dns/promises';+import net from 'node:net';++@ValidatorConstraint({ name: 'IsSafeWebhookUrl', async: true })+export class IsSafeWebhookUrlConstraint implements ValidatorConstraintInterface {+ async validate(value: unknown, _args: ValidationArguments): Promise<boolean> {+ if (typeof value !== 'string' || !value.trim()) {+ return false;+ }++ let parsed: URL;+ try {+ parsed = new URL(value);+ } catch {+ return false;+ }++ if (parsed.protocol !== 'https:') {+ return false;+ }++ if (!parsed.hostname) {+ return false;+ }++ const hostname = parsed.hostname.toLowerCase();++ if (hostname === 'localhost') {+ return false;+ }++ // Optional: block explicit ports other than 443+ // if (parsed.port && parsed.port !== '443') {+ // return false;+ // }++ // If user supplied a literal IP directly, validate it immediately+ const literalIpVersion = net.isIP(hostname);+ if (literalIpVersion) {+ return !this.isBlockedIp(hostname);+ }++ try {+ const records = await dns.lookup(hostname, { all: true });++ if (!records.length) {+ return false;+ }++ for (const record of records) {+ if (this.isBlockedIp(record.address)) {+ return false;+ }+ }++ return true;+ } catch {+ return false;+ }+ }++ defaultMessage(_args: ValidationArguments): string {+ return 'Webhook URL must be a public HTTPS URL and must not resolve to localhost, private, loopback, or link-local addresses';+ }++ private isBlockedIp(ip: string): boolean {+ const version = net.isIP(ip);+ if (version === 4) {+ return this.isBlockedIPv4(ip);+ }+ if (version === 6) {+ return this.isBlockedIPv6(ip);+ }+ return true;+ }++ private isBlockedIPv4(ip: string): boolean {+ const [a, b] = ip.split('.').map(Number);++ if ([a, b].some((n) => Number.isNaN(n))) return true;++ return (+ a === 0 || // 0.0.0.0/8+ a === 10 || // 10.0.0.0/8+ a === 127 || // 127.0.0.0/8+ (a === 169 && b === 254) || // 169.254.0.0/16+ (a === 172 && b >= 16 && b <= 31) || // 172.16.0.0/12+ (a === 192 && b === 168) || // 192.168.0.0/16+ (a === 100 && b >= 64 && b <= 127) || // 100.64.0.0/10+ (a === 198 && (b === 18 || b === 19)) || // 198.18.0.0/15+ a >= 224 // multicast/reserved+ );+ }++ private isBlockedIPv6(ip: string): boolean {+ const normalized = ip.toLowerCase();++ return (+ normalized === '::1' || // loopback+ normalized === '::' || // unspecified+ normalized.startsWith('fe80:') || // link-local+ normalized.startsWith('fc') || // unique local fc00::/7+ normalized.startsWith('fd') || // unique local fd00::/7+ normalized.startsWith('ff') // multicast+ );+ }+}++export function IsSafeWebhookUrl(validationOptions?: ValidationOptions) {+ return function (object: object, propertyName: string) {+ registerDecorator({+ target: object.constructor,+ propertyName,+ options: validationOptions,+ validator: IsSafeWebhookUrlConstraint,+ });+ };+}\ No newline at end of filediff --git a/libraries/nestjs-libraries/src/dtos/webhooks/webhooks.dto.ts b/libraries/nestjs-libraries/src/dtos/webhooks/webhooks.dto.tsindex d05db43b1..37808ee1e 100644--- a/libraries/nestjs-libraries/src/dtos/webhooks/webhooks.dto.ts+++ b/libraries/nestjs-libraries/src/dtos/webhooks/webhooks.dto.ts@@ -1,5 +1,6 @@import { IsDefined, IsOptional, IsString, IsUrl } from 'class-validator';import { Type } from 'class-transformer';+import { IsSafeWebhookUrl } from '@gitroom/nestjs-libraries/dtos/webhooks/webhook.url.validator';export class WebhooksIntegrationDto {@IsString()@@ -24,6 +25,17 @@ export class WebhooksDto {integrations: WebhooksIntegrationDto[];}+export class OnlyURL {+ @IsString()+ @IsUrl()+ @IsDefined()+ @IsSafeWebhookUrl({+ message:+ 'Webhook URL must be a public HTTPS URL and cannot point to internal network addresses',+ })+ url: string;+}+export class UpdateDto {@IsString()@IsDefined()@@ -36,6 +48,10 @@ export class UpdateDto {@IsString()@IsUrl()@IsDefined()+ @IsSafeWebhookUrl({+ message:+ 'Webhook URL must be a public HTTPS URL and cannot point to internal network addresses',+ })url: string;@Type(() => WebhooksIntegrationDto)