-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbridge-fetch-hardening.js
More file actions
65 lines (59 loc) · 2.11 KB
/
Copy pathbridge-fetch-hardening.js
File metadata and controls
65 lines (59 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
(function installBridgeFetchHardening() {
if (window.__signalShareBridgeFetchHardeningInstalled || typeof window.fetch !== "function") return;
window.__signalShareBridgeFetchHardeningInstalled = true;
const nativeFetch = window.fetch.bind(window);
const BRIDGE_PATH_PATTERN = /^\/api\/(?:local-llm|llm|system-media|system|tools|assistant|security)(?:\/|$)/i;
function parseRequestUrl(input) {
try {
const raw = typeof input === "string" ? input : `${input?.url || ""}`;
if (!raw) return null;
return new URL(raw, window.location.href);
} catch (_error) {
return null;
}
}
function isLoopbackHost(hostname = "") {
const host = `${hostname || ""}`.trim().toLowerCase();
return host === "localhost" || host === "127.0.0.1" || host === "::1" || host === "[::1]";
}
function isBridgeRequest(input) {
const url = parseRequestUrl(input);
if (!url) return false;
return isLoopbackHost(url.hostname) && BRIDGE_PATH_PATTERN.test(url.pathname || "");
}
function makeBridgeOfflineResponse(input, reason = "Local bridge is unreachable.") {
const url = parseRequestUrl(input);
return new Response(JSON.stringify({
ok: false,
error: reason,
message: reason,
bridgeUnavailable: true,
route: url?.pathname || ""
}), {
status: 503,
statusText: "Bridge unavailable",
headers: {
"Content-Type": "application/json; charset=utf-8",
"X-Signal-Share-Bridge-Fallback": "1"
}
});
}
window.fetch = async function signalShareBridgeSafeFetch(input, init) {
const bridgeRequest = isBridgeRequest(input);
try {
const response = await nativeFetch(input, init);
if (!response && bridgeRequest) {
return makeBridgeOfflineResponse(input, "Local bridge returned no response.");
}
if (!response) {
throw new TypeError("Fetch returned no response.");
}
return response;
} catch (error) {
if (bridgeRequest) {
return makeBridgeOfflineResponse(input, error?.message || "Local bridge request failed.");
}
throw error;
}
};
})();