Skip to content

πŸ“¨ The ultimate agentic social media scheduling tool πŸ€–

feat: ssrf fixes

Nevo David5f18f1ca68d9
diff --git a/libraries/nestjs-libraries/src/database/prisma/posts/posts.service.ts b/libraries/nestjs-libraries/src/database/prisma/posts/posts.service.ts
index b4b883c66..bf866441b 100644
--- a/libraries/nestjs-libraries/src/database/prisma/posts/posts.service.ts
+++ b/libraries/nestjs-libraries/src/database/prisma/posts/posts.service.ts
@@ -35,6 +35,7 @@ import axios from 'axios';
import sharp from 'sharp';
import { UploadFactory } from '@gitroom/nestjs-libraries/upload/upload.factory';
import { Readable } from 'stream';
+import { resolve, sep } from 'path';
import { OpenaiService } from '@gitroom/nestjs-libraries/openai/openai.service';
dayjs.extend(utc);
import * as Sentry from '@sentry/nestjs';
@@ -530,6 +531,7 @@ export class PostsService {
})
)
)
+ .filter((m) => this.isAllowedMediaPath(m?.path))
.map((m) => {
return {
...m,
@@ -609,8 +611,28 @@ export class PostsService {
return getImageList;
} catch (err: any) {
- return imagesList;
+ return (imagesList || []).filter((m) =>
+ this.isAllowedMediaPath(m?.path)
+ );
+ }
+ }
+
+ // a media path that isn't a remote url is read straight from the worker's
+ // disk, so it has to resolve inside UPLOAD_DIRECTORY
+ private isAllowedMediaPath(path: unknown) {
+ if (typeof path !== 'string' || /^https?:\/\//.test(path)) {
+ return true;
}
+
+ // containing "http" anywhere skips the UPLOAD_DIRECTORY prefix, and the
+ // providers would then read it relative to the working directory
+ if (path.indexOf('http') > -1) {
+ return false;
+ }
+
+ const base = resolve(String(process.env.UPLOAD_DIRECTORY));
+ const filePath = resolve(process.env.UPLOAD_DIRECTORY + path);
+ return filePath.startsWith(base + sep);
}
async getPostGroupDebugExport(orgId: string, group: string) {
diff --git a/libraries/nestjs-libraries/src/dtos/webhooks/webhook.url.validator.ts b/libraries/nestjs-libraries/src/dtos/webhooks/webhook.url.validator.ts
index d79be0e6b..f51f4f275 100644
--- a/libraries/nestjs-libraries/src/dtos/webhooks/webhook.url.validator.ts
+++ b/libraries/nestjs-libraries/src/dtos/webhooks/webhook.url.validator.ts
@@ -9,35 +9,46 @@ import { URL } from 'node:url';
import dns from 'node:dns/promises';
import net from 'node:net';
+const blockedIPv4Ranges: [string, number][] = [
+ ['0.0.0.0', 8],
+ ['10.0.0.0', 8],
+ ['127.0.0.0', 8],
+ ['169.254.0.0', 16],
+ ['172.16.0.0', 12],
+ ['192.168.0.0', 16],
+ ['100.64.0.0', 10],
+ ['198.18.0.0', 15],
+ ['224.0.0.0', 3], // multicast/reserved
+];
+
+// BlockList matches an IPv4 rule against the address in any notation,
+// including IPv4-mapped IPv6 like ::ffff:7f00:1 (what the URL parser turns
+// [::ffff:127.0.0.1] into), so the addresses are never compared as strings.
+const blockedIps = new net.BlockList();
+
+for (const [address, prefix] of blockedIPv4Ranges) {
+ blockedIps.addSubnet(address, prefix, 'ipv4');
+
+ // the same IPv4 range embedded in NAT64 (64:ff9b::/96) and 6to4 (2002::/16)
+ const [a, b, c, d] = address.split('.').map(Number);
+ const high = ((a << 8) | b).toString(16);
+ const low = ((c << 8) | d).toString(16);
+ blockedIps.addSubnet(`64:ff9b::${high}:${low}`, 96 + prefix, 'ipv6');
+ blockedIps.addSubnet(`2002:${high}:${low}::`, 16 + prefix, 'ipv6');
+}
+
+blockedIps.addSubnet('::', 96, 'ipv6'); // unspecified, loopback, IPv4-compatible
+blockedIps.addSubnet('fe80::', 10, 'ipv6'); // link-local
+blockedIps.addSubnet('fec0::', 10, 'ipv6'); // site-local (deprecated)
+blockedIps.addSubnet('fc00::', 7, 'ipv6'); // unique local
+blockedIps.addSubnet('ff00::', 8, 'ipv6'); // multicast
+
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
- );
+ return net.isIPv4(ip) ? blockedIps.check(ip, 'ipv4') : true;
}
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
- );
+ return net.isIPv6(ip) ? blockedIps.check(ip, 'ipv6') : true;
}
export function isBlockedIp(ip: string): boolean {
@@ -46,11 +57,6 @@ export function isBlockedIp(ip: string): boolean {
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;