Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions apps/web/lib/url-guard.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ describe("validateExternalUrl (SSRF guard)", () => {
it("blocks localhost", () => {
expect(validateExternalUrl("http://localhost/x").ok).toBe(false);
});
it("blocks internal hostnames with DNS root dots", () => {
expect(validateExternalUrl("http://localhost./x").ok).toBe(false);
expect(validateExternalUrl("http://app.localhost./x").ok).toBe(false);
expect(validateExternalUrl("http://metadata.google.internal./x").ok).toBe(false);
});
it("blocks private 10.x", () => {
expect(validateExternalUrl("http://10.0.0.5/x").ok).toBe(false);
});
Expand Down
3 changes: 2 additions & 1 deletion apps/web/lib/url-guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ function isBlockedHost(host: string): boolean {
const ipv4 = ipv4FromMappedIPv6(host) ?? host;
return (
ipv4 === "localhost" ||
ipv4.endsWith(".localhost") ||
ipv4 === "0.0.0.0" ||
ipv4 === "::1" ||
ipv4 === "::" ||
Expand Down Expand Up @@ -42,7 +43,7 @@ export function validateExternalUrl(raw: string): { ok: true; url: URL } | { ok:
if (url.protocol !== "http:" && url.protocol !== "https:") {
return { ok: false, error: "Only http and https URLs are allowed." };
}
const host = url.hostname.toLowerCase().replace(/^\[(.*)\]$/, "$1");
const host = url.hostname.toLowerCase().replace(/^\[(.*)\]$/, "$1").replace(/\.+$/, "");
if (isBlockedHost(host)) {
return { ok: false, error: "That host is not allowed." };
}
Expand Down
Loading