fix(cloud): validate relay URL origins

Co-authored-by: codex <[email protected]>
This commit is contained in:
Julius Marmingeandcodex committed 2026-06-04 14:03:01 -07:00
1 parent 26f36ca992
commit 7b3bc331cc
11 files changed
+144 -26

No files matched your search

+26
View File
@@ -0,0 +1,26 @@
import { describe, expect, it } from "vitest";
import { isSecureRelayUrl, normalizeSecureRelayUrl } from "./relayUrl.ts";
describe("normalizeSecureRelayUrl", () => {
it("normalizes secure relay origins", () => {
expect(normalizeSecureRelayUrl(" https://relay.example.test/// ")).toBe(
"https://relay.example.test",
);
expect(normalizeSecureRelayUrl("https://relay.example.test:8443/")).toBe(
"https://relay.example.test:8443",
);
});
it.each([
"http://relay.example.test",
"https://user:[email protected]",
"https://relay.example.test/path",
"https://relay.example.test?query=value",
"https://relay.example.test#fragment",
"not a url",
])("rejects unsafe relay URL %s", (value) => {
expect(normalizeSecureRelayUrl(value)).toBeNull();
expect(isSecureRelayUrl(value)).toBe(false);
});
});
+22
View File
@@ -0,0 +1,22 @@
export function normalizeSecureRelayUrl(value: string): string | null {
try {
const url = new URL(value.trim());
if (
url.protocol !== "https:" ||
url.username.length > 0 ||
url.password.length > 0 ||
url.search.length > 0 ||
url.hash.length > 0 ||
!/^\/+$/u.test(url.pathname)
) {
return null;
}
return url.origin;
} catch {
return null;
}
}
export function isSecureRelayUrl(value: string): boolean {
return normalizeSecureRelayUrl(value) !== null;
}