π¨ The ultimate agentic social media scheduling tool π€
TypeScript nextjsopen-sourceopen-source-social-media-scheduling-toolosspostiz-cloudpostiz-hostedredisscheduling-toolsocial-media-scheduling-tooltypescript
feat: security fixes
Nevo David30e8b7770981
diff --git a/apps/backend/src/api/routes/public.controller.ts b/apps/backend/src/api/routes/public.controller.tsindex 018ad0f44..698f5c751 100644--- a/apps/backend/src/api/routes/public.controller.ts+++ b/apps/backend/src/api/routes/public.controller.ts@@ -10,7 +10,6 @@ import {StreamableFile,} from '@nestjs/common';import { ApiTags } from '@nestjs/swagger';-import { AgenciesService } from '@gitroom/nestjs-libraries/database/prisma/agencies/agencies.service';import { PostsService } from '@gitroom/nestjs-libraries/database/prisma/posts/posts.service';import { TrackService } from '@gitroom/nestjs-libraries/track/track.service';import { RealIP } from 'nestjs-real-ip';@@ -27,6 +26,7 @@ import { pricing } from '@gitroom/nestjs-libraries/database/prisma/subscriptionsimport { Readable, pipeline } from 'stream';import { promisify } from 'util';import { OnlyURL } from '@gitroom/nestjs-libraries/dtos/webhooks/webhooks.dto';+import { isSafePublicHttpsUrl } from '@gitroom/nestjs-libraries/dtos/webhooks/webhook.url.validator';const pump = promisify(pipeline);@@ -34,7 +34,6 @@ const pump = promisify(pipeline);@Controller('/public')export class PublicController {constructor(- private _agenciesService: AgenciesService,private _trackService: TrackService,private _agentGraphInsertService: AgentGraphInsertService,private _postsService: PostsService,@@ -53,26 +52,6 @@ export class PublicController {return this._agentGraphInsertService.newPost(body.text);}- @Get('/agencies-list')- async getAgencyByUser() {- return this._agenciesService.getAllAgencies();- }-- @Get('/agencies-list-slug')- async getAgencySlug() {- return this._agenciesService.getAllAgenciesSlug();- }-- @Get('/agencies-information/:agency')- async getAgencyInformation(@Param('agency') agency: string) {- return this._agenciesService.getAgencyInformation(agency);- }-- @Get('/agencies-list-count')- async getAgenciesCount() {- return this._agenciesService.getCount();- }-@Get(`/posts/:id`)async getPreview(@Param('id') id: string) {return (await this._postsService.getPostsRecursively(id, true)).map(@@ -198,7 +177,45 @@ export class PublicController {req.on('aborted', onClose);res.on('close', onClose);- const r = await fetch(url, { signal: ac.signal });+ // Manually follow redirects so every hop is re-validated against+ // the SSRF blocklist (see GHSA-34w8-5j2v-h6ww). `fetch` defaults to+ // `redirect: 'follow'`, which bypasses the DTO-level URL check.+ const MAX_REDIRECTS = 5;+ let currentUrl = url;+ let r: globalThis.Response | undefined;+ for (let hop = 0; hop <= MAX_REDIRECTS; hop++) {+ if (!(await isSafePublicHttpsUrl(currentUrl))) {+ return res.status(400).send('Blocked URL');+ }++ r = await fetch(currentUrl, {+ signal: ac.signal,+ redirect: 'manual',+ });++ if (r.status >= 300 && r.status < 400) {+ const location = r.headers.get('location');+ if (!location) {+ return res.status(502).send('Redirect without Location');+ }+ try {+ currentUrl = new URL(location, currentUrl).toString();+ } catch {+ return res.status(400).send('Invalid redirect target');+ }+ continue;+ }++ break;+ }++ if (!r) {+ return res.status(502).send('No upstream response');+ }++ if (r.status >= 300 && r.status < 400) {+ return res.status(508).send('Too many redirects');+ }if (!r.ok && r.status !== 206) {res.status(r.status);diff --git a/apps/backend/src/services/auth/auth.service.ts b/apps/backend/src/services/auth/auth.service.tsindex e8e37fa01..5d4dd8187 100644--- a/apps/backend/src/services/auth/auth.service.ts+++ b/apps/backend/src/services/auth/auth.service.ts@@ -309,6 +309,9 @@ export class AuthService {}private async jwt(user: User) {+ if (user.password) {+ delete user.password;+ }return AuthChecker.signJWT(user);}}diff --git a/libraries/nestjs-libraries/src/dtos/webhooks/webhook.url.validator.ts b/libraries/nestjs-libraries/src/dtos/webhooks/webhook.url.validator.tsindex 3ce284333..d79be0e6b 100644--- a/libraries/nestjs-libraries/src/dtos/webhooks/webhook.url.validator.ts+++ b/libraries/nestjs-libraries/src/dtos/webhooks/webhook.url.validator.ts@@ -9,108 +9,112 @@ 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;- }+export function 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+ );+}- let parsed: URL;- try {- parsed = new URL(value);- } catch {- return false;- }+export function 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+ );+}- if (parsed.protocol !== 'https:') {- return false;+export function isBlockedIp(ip: string): boolean {+ const version = net.isIP(ip);+ if (version === 4) {+ return isBlockedIPv4(ip);+ }+ if (version === 6) {+ // IPv4-mapped IPv6 (::ffff:a.b.c.d) β extract and check as IPv4+ const mapped = ip.toLowerCase().match(/^::ffff:(\d+\.\d+\.\d+\.\d+)$/);+ if (mapped) {+ return isBlockedIPv4(mapped[1]);}+ return isBlockedIPv6(ip);+ }+ return true;+}- if (!parsed.hostname) {- return false;- }+export async function isSafePublicHttpsUrl(value: unknown): Promise<boolean> {+ if (typeof value !== 'string' || !value.trim()) {+ return false;+ }- const hostname = parsed.hostname.toLowerCase();+ let parsed: URL;+ try {+ parsed = new URL(value);+ } catch {+ return false;+ }- if (hostname === 'localhost') {- return false;- }+ if (parsed.protocol !== 'https:') {+ return false;+ }- // Optional: block explicit ports other than 443- // if (parsed.port && parsed.port !== '443') {- // return false;- // }+ if (!parsed.hostname) {+ return false;+ }- // If user supplied a literal IP directly, validate it immediately- const literalIpVersion = net.isIP(hostname);- if (literalIpVersion) {- return !this.isBlockedIp(hostname);- }+ const hostname = parsed.hostname.toLowerCase().replace(/^\[|\]$/g, '');- try {- const records = await dns.lookup(hostname, { all: true });+ if (hostname === 'localhost') {+ return false;+ }- if (!records.length) {- return false;- }+ // If user supplied a literal IP directly, validate it immediately+ const literalIpVersion = net.isIP(hostname);+ if (literalIpVersion) {+ return !isBlockedIp(hostname);+ }- for (const record of records) {- if (this.isBlockedIp(record.address)) {- return false;- }- }+ try {+ const records = await dns.lookup(hostname, { all: true });- return true;- } catch {+ if (!records.length) {return false;}- }-- defaultMessage(_args: ValidationArguments): string {- return '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);+ for (const record of records) {+ if (isBlockedIp(record.address)) {+ return false;+ }}+return true;+ } catch {+ return false;}+}- 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- );+@ValidatorConstraint({ name: 'IsSafeWebhookUrl', async: true })+export class IsSafeWebhookUrlConstraint implements ValidatorConstraintInterface {+ async validate(value: unknown, _args: ValidationArguments): Promise<boolean> {+ return isSafePublicHttpsUrl(value);}- 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- );+ defaultMessage(_args: ValidationArguments): string {+ return 'URL must be a public HTTPS URL and must not resolve to localhost, private, loopback, or link-local addresses';}}