Skip to content
Merged
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
12 changes: 7 additions & 5 deletions packages/loopover-engine/src/review/safe-url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
//
// Rejects non-HTTPS (isSafeHttpUrl), localhost / `*.localhost` / .local / .internal, and private/
// loopback/link-local IPs in any literal notation (decimal `2130706433`, hex `0x7f000001`, octal,
// short `127.1`, and the IPv6 forms). isSafeEndpointUrl additionally permits wss:/ws: for base-layer
// chain endpoints.
// short `127.1`, and the IPv6 forms). isSafeEndpointUrl additionally permits wss: (not plain ws:,
// #8017) for base-layer chain endpoints.

function parseIpv4Component(part: string): number | null {
if (/^0x[0-9a-f]+$/i.test(part)) return parseInt(part, 16);
Expand Down Expand Up @@ -109,15 +109,17 @@ export function isSafeHttpUrl(raw: string): boolean {
return !hostIsPrivateOrLocal(url.hostname);
}

/** Like isSafeHttpUrl but also permits secure WebSocket endpoints (`wss:`, `ws:`) — base-layer chain
* endpoints (subtensor RPC/WSS/archive) are probed via JSON-RPC, not HTTP. Same SSRF host/IP guard. */
/** Like isSafeHttpUrl but also permits the secure WebSocket endpoint (`wss:`) — base-layer chain endpoints
* (subtensor RPC/WSS/archive) are probed via JSON-RPC, not HTTP. Same SSRF host/IP guard. Plain `ws:` is
* REJECTED (#8017): it is the plaintext counterpart to `wss:`, the same relationship `http:`/`https:` has —
* and `isSafeHttpUrl` above already rejects `http:` for exactly that reason. */
export function isSafeEndpointUrl(raw: string): boolean {
let url: URL;
try {
url = new URL(raw);
} catch {
return false;
}
if (!["https:", "wss:", "ws:"].includes(url.protocol)) return false;
if (!["https:", "wss:"].includes(url.protocol)) return false;
return !hostIsPrivateOrLocal(url.hostname);
}
12 changes: 7 additions & 5 deletions src/review/content-lane/safe-url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
//
// Rejects non-HTTPS (isSafeHttpUrl), localhost / `*.localhost` / .local / .internal, and private/
// loopback/link-local IPs in any literal notation (decimal `2130706433`, hex `0x7f000001`, octal,
// short `127.1`, and the IPv6 forms). isSafeEndpointUrl additionally permits wss:/ws: for base-layer
// chain endpoints.
// short `127.1`, and the IPv6 forms). isSafeEndpointUrl additionally permits wss: (not plain ws:,
// #8017) for base-layer chain endpoints.

function parseIpv4Component(part: string): number | null {
if (/^0x[0-9a-f]+$/i.test(part)) return parseInt(part, 16);
Expand Down Expand Up @@ -109,15 +109,17 @@ export function isSafeHttpUrl(raw: string): boolean {
return !hostIsPrivateOrLocal(url.hostname);
}

/** Like isSafeHttpUrl but also permits secure WebSocket endpoints (`wss:`, `ws:`) — base-layer chain
* endpoints (subtensor RPC/WSS/archive) are probed via JSON-RPC, not HTTP. Same SSRF host/IP guard. */
/** Like isSafeHttpUrl but also permits the secure WebSocket endpoint (`wss:`) — base-layer chain endpoints
* (subtensor RPC/WSS/archive) are probed via JSON-RPC, not HTTP. Same SSRF host/IP guard. Plain `ws:` is
* REJECTED (#8017): it is the plaintext counterpart to `wss:`, the same relationship `http:`/`https:` has —
* and `isSafeHttpUrl` above already rejects `http:` for exactly that reason. */
export function isSafeEndpointUrl(raw: string): boolean {
let url: URL;
try {
url = new URL(raw);
} catch {
return false;
}
if (!["https:", "wss:", "ws:"].includes(url.protocol)) return false;
if (!["https:", "wss:"].includes(url.protocol)) return false;
return !hostIsPrivateOrLocal(url.hostname);
}
9 changes: 6 additions & 3 deletions test/unit/content-lane-safe-url.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,8 @@ describe("isSafeHttpUrl", () => {
});

describe("isSafeEndpointUrl", () => {
it("additionally permits wss / ws for chain endpoints", () => {
it("additionally permits wss for chain endpoints", () => {
expect(isSafeEndpointUrl("wss://entrypoint.example.com")).toBe(true);
expect(isSafeEndpointUrl("ws://node.example.com")).toBe(true);
expect(isSafeEndpointUrl("https://api.example.com")).toBe(true);
});

Expand All @@ -155,7 +154,11 @@ describe("isSafeEndpointUrl", () => {
expect(isSafeEndpointUrl("wss://localhost")).toBe(false);
});

it("rejects non-ws/https protocols", () => {
// #8017: plain ws: is the plaintext counterpart to wss:, the same relationship http:/https: has -- and
// isSafeHttpUrl already rejects http: for exactly that reason. The doc comment above always said "secure
// WebSocket endpoints"; the code just didn't match it until now.
it("rejects non-wss/https protocols, including plain unencrypted ws: (#8017)", () => {
expect(isSafeEndpointUrl("ws://node.example.com")).toBe(false);
expect(isSafeEndpointUrl("http://example.com")).toBe(false);
expect(isSafeEndpointUrl("ftp://example.com")).toBe(false);
});
Expand Down
9 changes: 6 additions & 3 deletions test/unit/safe-url-engine.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,8 @@ describe("isSafeHttpUrl", () => {
});

describe("isSafeEndpointUrl", () => {
it("additionally permits wss / ws for chain endpoints", () => {
it("additionally permits wss for chain endpoints", () => {
expect(isSafeEndpointUrl("wss://entrypoint.example.com")).toBe(true);
expect(isSafeEndpointUrl("ws://node.example.com")).toBe(true);
expect(isSafeEndpointUrl("https://api.example.com")).toBe(true);
});

Expand All @@ -156,7 +155,11 @@ describe("isSafeEndpointUrl", () => {
expect(isSafeEndpointUrl("wss://localhost")).toBe(false);
});

it("rejects non-ws/https protocols", () => {
// #8017: plain ws: is the plaintext counterpart to wss:, the same relationship http:/https: has -- and
// isSafeHttpUrl already rejects http: for exactly that reason. The doc comment above always said "secure
// WebSocket endpoints"; the code just didn't match it until now.
it("rejects non-wss/https protocols, including plain unencrypted ws: (#8017)", () => {
expect(isSafeEndpointUrl("ws://node.example.com")).toBe(false);
expect(isSafeEndpointUrl("http://example.com")).toBe(false);
expect(isSafeEndpointUrl("ftp://example.com")).toBe(false);
});
Expand Down