Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
fix(web): block private hosts in S3 endpoint test
Richie McIlroy152954df00be
diff --git a/apps/web/app/api/desktop/[...route]/s3Config.ts b/apps/web/app/api/desktop/[...route]/s3Config.tsindex 13b592712..8f91001a5 100644--- a/apps/web/app/api/desktop/[...route]/s3Config.ts+++ b/apps/web/app/api/desktop/[...route]/s3Config.ts@@ -1,3 +1,5 @@+import { lookup } from "node:dns/promises";+import { isIP } from "node:net";import { HeadBucketCommand, S3Client } from "@aws-sdk/client-s3";import { db } from "@cap/database";import { decrypt, encrypt } from "@cap/database/crypto";@@ -57,6 +59,87 @@ const getS3ErrorMetadata = (error: unknown) => {return error.$metadata as { httpStatusCode?: number } | undefined;};+// SSRF protection for the user-supplied `endpoint` in /test: Cap's server can+// never legitimately reach a user's private-LAN S3 endpoint, so we reject any+// endpoint whose host is/resolves to loopback, private, link-local or reserved+// ranges (incl. the cloud metadata IP) before constructing the S3 client.+const isBlockedIp = (ip: string): boolean => {+ const version = isIP(ip);++ if (version === 4) {+ const octets = ip.split(".").map((part) => Number.parseInt(part, 10));+ if (octets.length !== 4 || octets.some((o) => Number.isNaN(o))) return true;+ const a = octets[0] ?? -1;+ const b = octets[1] ?? -1;+ if (a === 0) return true; // 0.0.0.0/8 (incl. 0.0.0.0)+ if (a === 127) return true; // 127.0.0.0/8 loopback+ if (a === 10) return true; // 10.0.0.0/8 private+ if (a === 172 && b >= 16 && b <= 31) return true; // 172.16.0.0/12 private+ if (a === 192 && b === 168) return true; // 192.168.0.0/16 private+ if (a === 169 && b === 254) return true; // 169.254.0.0/16 link-local (incl. metadata)+ if (a === 100 && b >= 64 && b <= 127) return true; // 100.64.0.0/10 CGNAT+ if (a >= 224) return true; // 224.0.0.0/4 multicast + 240.0.0.0/4 reserved+ return false;+ }++ if (version === 6) {+ // Normalise to lowercase and strip any zone id / IPv4-mapped prefix.+ const normalized = ip.toLowerCase().split("%")[0] ?? "";+ const mapped = normalized.replace(/^::ffff:/, "");+ if (isIP(mapped) === 4) return isBlockedIp(mapped); // IPv4-mapped IPv6+ if (normalized === "::1" || normalized === "::") return true; // loopback / unspecified+ if (normalized.startsWith("fe80")) return true; // fe80::/10 link-local+ const firstByte = Number.parseInt(normalized.slice(0, 2), 16);+ if (!Number.isNaN(firstByte) && (firstByte & 0xfe) === 0xfc) return true; // fc00::/7 unique-local+ return false;+ }++ return true;+};++const isBlockedHostname = (hostname: string): boolean => {+ const host = hostname.toLowerCase().replace(/\.$/, "");+ if (!host) return true;+ if (host === "localhost" || host.endsWith(".localhost")) return true;+ if (host.endsWith(".internal")) return true;+ return false;+};++const isBlockedEndpoint = async (endpoint: string): Promise<boolean> => {+ let url: URL;+ try {+ url = new URL(endpoint);+ } catch {+ return true;+ }++ if (url.protocol !== "http:" && url.protocol !== "https:") return true;++ const hostname = url.hostname.toLowerCase().replace(/\.$/, "");+ if (!hostname) return true;++ if (isBlockedHostname(hostname)) return true;++ // WHATWG URL keeps the surrounding brackets on IPv6 literals+ // (new URL("http://[::1]/").hostname === "[::1]"), so strip them before the+ // IP check — otherwise isIP() returns 0 and the literal IPv6 SSRF target+ // (e.g. [::1], [fc00::1], [::ffff:127.0.0.1]) would fall through to DNS.+ const ipCandidate = hostname.replace(/^\[/, "").replace(/\]$/, "");++ // Literal IP address: validate directly.+ if (isIP(ipCandidate) !== 0) return isBlockedIp(ipCandidate);++ // Hostname: resolve all addresses and block if any is private/reserved.+ try {+ const addresses = await lookup(hostname, { all: true });+ if (addresses.length === 0) return true;+ return addresses.some((addr) => isBlockedIp(addr.address));+ } catch {+ // Unresolvable host: let the S3 client surface the normal connection error.+ return false;+ }+};+app.post("/",zValidator(@@ -228,6 +311,18 @@ app.post(const data = c.req.valid("json");try {+ if (await isBlockedEndpoint(data.endpoint)) {+ return c.json(+ {+ error:+ "Invalid endpoint. Please provide a valid public S3-compatible endpoint URL.",+ details: "The provided endpoint is not allowed.",+ metadata: undefined,+ },+ { status: 400 },+ );+ }+const controller = new AbortController();const timeoutId = setTimeout(() => {controller.abort();