socket-function 1.2.24 → 1.2.26
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.d.ts +4 -3
- package/package.json +1 -1
- package/src/callHTTPHandler.ts +33 -3
- package/src/forwardPort.d.ts +4 -3
- package/src/forwardPort.ts +17 -3
package/index.d.ts
CHANGED
|
@@ -991,11 +991,12 @@ declare module "socket-function/src/forwardPort" {
|
|
|
991
991
|
export declare function listPortMappings(): Promise<PortMapping[]>;
|
|
992
992
|
/** Outcome of forwardPort. `owned` is true once we hold the router mapping for the port. When
|
|
993
993
|
* false, `reason` says why: "declined" = noPortStealing and another host holds the port (the
|
|
994
|
-
* caller should try a different port); "
|
|
995
|
-
* nothing
|
|
994
|
+
* caller should try a different port); "notBehindNat" = we have a public/directly-reachable
|
|
995
|
+
* address so there's nothing to forward; "error" = UPnP unreachable / create failed (best-effort,
|
|
996
|
+
* nothing forwarded but the caller can carry on). Only "declined" warrants trying another port. */
|
|
996
997
|
export type ForwardPortResult = {
|
|
997
998
|
owned: boolean;
|
|
998
|
-
reason?: "declined" | "error";
|
|
999
|
+
reason?: "declined" | "notBehindNat" | "error";
|
|
999
1000
|
};
|
|
1000
1001
|
export declare function forwardPort(config: {
|
|
1001
1002
|
externalPort: number;
|
package/package.json
CHANGED
package/src/callHTTPHandler.ts
CHANGED
|
@@ -63,16 +63,29 @@ export async function httpCallHandler(request: http.IncomingMessage, response: h
|
|
|
63
63
|
try {
|
|
64
64
|
// Always set x-frame-options, to prevent iframe embedding click hijacking
|
|
65
65
|
response.setHeader("X-Frame-Options", "SAMEORIGIN");
|
|
66
|
+
// Only frame-ancestors, as a full CSP would break our eval-based module loading
|
|
67
|
+
response.setHeader("Content-Security-Policy", "frame-ancestors 'self'");
|
|
68
|
+
response.setHeader("X-Content-Type-Options", "nosniff");
|
|
69
|
+
// Without this, the full URL (including classGuid/functionName/args in the query) leaks in the Referer to any third-party resource
|
|
70
|
+
response.setHeader("Referrer-Policy", "strict-origin-when-cross-origin");
|
|
66
71
|
// Don't keep alive, to prevent issues with zombie sockets.
|
|
67
72
|
response.setHeader("Connection", "close");
|
|
68
73
|
|
|
69
74
|
// CORS bs (due to having to explictly allow subdomains)
|
|
75
|
+
let corsAllowed = false;
|
|
70
76
|
{
|
|
71
77
|
response.setHeader("Strict-Transport-Security", "max-age=63072000; includeSubDomains; preload");
|
|
72
78
|
response.setHeader("Cross-Origin-Opener-Policy", SocketFunction.COOP);
|
|
73
79
|
response.setHeader("Cross-Origin-Embedder-Policy", SocketFunction.COEP);
|
|
74
80
|
|
|
75
|
-
let origin = request.headers.origin
|
|
81
|
+
let origin = request.headers.origin;
|
|
82
|
+
if (!origin && request.headers.referer) {
|
|
83
|
+
// Referer is a full URL, but Access-Control-Allow-Origin must be a bare origin
|
|
84
|
+
try {
|
|
85
|
+
origin = new URL(request.headers.referer).origin;
|
|
86
|
+
} catch {
|
|
87
|
+
}
|
|
88
|
+
}
|
|
76
89
|
let allowed = false;
|
|
77
90
|
if (!origin) {
|
|
78
91
|
// I guess it's a script, so just allow it (as it could easily set any header it wanted anyways)
|
|
@@ -98,15 +111,28 @@ export async function httpCallHandler(request: http.IncomingMessage, response: h
|
|
|
98
111
|
response.setHeader("Cross-Origin-Resource-Policy", "same-site");
|
|
99
112
|
}
|
|
100
113
|
|
|
101
|
-
response.setHeader("
|
|
114
|
+
response.setHeader("Vary", "Origin, Access-Control-Request-Headers");
|
|
102
115
|
response.setHeader("Access-Control-Allow-Origin", allowed ? origin : "");
|
|
103
116
|
|
|
104
|
-
|
|
117
|
+
// Browsers reject Allow-Credentials combined with a "*" origin, so don't emit that pair
|
|
118
|
+
if (allowed && origin !== "*") {
|
|
105
119
|
response.setHeader("Access-Control-Allow-Credentials", "true");
|
|
106
120
|
response.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
|
|
107
121
|
response.setHeader("Access-Control-Allow-Headers", "Content-Type, Authorization, Content-Length, X-Requested-With, x-uncompressed-content-length, Cookie");
|
|
108
122
|
}
|
|
109
123
|
response.setHeader("Access-Control-Expose-Headers", "x-uncompressed-content-length");
|
|
124
|
+
corsAllowed = allowed;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// Preflights must never run the underlying call (they also arrive without credentials, so the call would be wrong anyways)
|
|
128
|
+
if (request.method === "OPTIONS") {
|
|
129
|
+
response.writeHead(204);
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
// The browser discards the response for disallowed origins anyways, so running the call would only produce side effects the caller can't even see
|
|
133
|
+
if (!corsAllowed) {
|
|
134
|
+
response.writeHead(403);
|
|
135
|
+
return;
|
|
110
136
|
}
|
|
111
137
|
|
|
112
138
|
|
|
@@ -235,6 +261,10 @@ export async function httpCallHandler(request: http.IncomingMessage, response: h
|
|
|
235
261
|
return;
|
|
236
262
|
}
|
|
237
263
|
}
|
|
264
|
+
// With nosniff set, an untyped response is unusable to the browser, so results without explicit headers need their actual type (JSON) declared
|
|
265
|
+
if (!response.getHeader("Content-Type")) {
|
|
266
|
+
response.setHeader("Content-Type", "application/json");
|
|
267
|
+
}
|
|
238
268
|
let uncompressedLength = resultBuffer.length;
|
|
239
269
|
if (SocketFunction.HTTP_COMPRESS && request.headers["accept-encoding"]?.includes("gzip") && !headers?.["Content-Encoding"]) {
|
|
240
270
|
// NOTE: This is a BIT slow. To speed it up, functions can use an internal cache, according to their function,
|
package/src/forwardPort.d.ts
CHANGED
|
@@ -26,11 +26,12 @@ export interface PortMapping {
|
|
|
26
26
|
export declare function listPortMappings(): Promise<PortMapping[]>;
|
|
27
27
|
/** Outcome of forwardPort. `owned` is true once we hold the router mapping for the port. When
|
|
28
28
|
* false, `reason` says why: "declined" = noPortStealing and another host holds the port (the
|
|
29
|
-
* caller should try a different port); "
|
|
30
|
-
* nothing
|
|
29
|
+
* caller should try a different port); "notBehindNat" = we have a public/directly-reachable
|
|
30
|
+
* address so there's nothing to forward; "error" = UPnP unreachable / create failed (best-effort,
|
|
31
|
+
* nothing forwarded but the caller can carry on). Only "declined" warrants trying another port. */
|
|
31
32
|
export type ForwardPortResult = {
|
|
32
33
|
owned: boolean;
|
|
33
|
-
reason?: "declined" | "error";
|
|
34
|
+
reason?: "declined" | "notBehindNat" | "error";
|
|
34
35
|
};
|
|
35
36
|
export declare function forwardPort(config: {
|
|
36
37
|
externalPort: number;
|
package/src/forwardPort.ts
CHANGED
|
@@ -18,6 +18,13 @@ export async function resolveGateway(): Promise<{
|
|
|
18
18
|
let internalIP = await getOutboundIP();
|
|
19
19
|
if (!internalIP) throw new Error("Could not determine our local network address");
|
|
20
20
|
|
|
21
|
+
// A public/directly-reachable outbound address means there's no NAT and therefore no UPnP
|
|
22
|
+
// gateway to reach — SSDP would just multicast into the void and time out after 2s. Fail fast
|
|
23
|
+
// and clearly instead, so callers (and forwardPort) never spend that time on a public host.
|
|
24
|
+
if (!isPrivateIPv4(internalIP)) {
|
|
25
|
+
throw new Error(`Not behind a NAT (outbound address ${internalIP} is public) — no UPnP gateway to reach, skipping SSDP discovery.`);
|
|
26
|
+
}
|
|
27
|
+
|
|
21
28
|
// The gateway that answers SSDP discovery is the router we forward through; take its IP
|
|
22
29
|
// and control port straight from the discovered device URL rather than parsing routes.
|
|
23
30
|
let gateway = await discoverGateway(internalIP);
|
|
@@ -89,11 +96,12 @@ const SUPERSEDE_CHECK_INTERVAL = timeInMinute * 30;
|
|
|
89
96
|
|
|
90
97
|
/** Outcome of forwardPort. `owned` is true once we hold the router mapping for the port. When
|
|
91
98
|
* false, `reason` says why: "declined" = noPortStealing and another host holds the port (the
|
|
92
|
-
* caller should try a different port); "
|
|
93
|
-
* nothing
|
|
99
|
+
* caller should try a different port); "notBehindNat" = we have a public/directly-reachable
|
|
100
|
+
* address so there's nothing to forward; "error" = UPnP unreachable / create failed (best-effort,
|
|
101
|
+
* nothing forwarded but the caller can carry on). Only "declined" warrants trying another port. */
|
|
94
102
|
export type ForwardPortResult = {
|
|
95
103
|
owned: boolean;
|
|
96
|
-
reason?: "declined" | "error";
|
|
104
|
+
reason?: "declined" | "notBehindNat" | "error";
|
|
97
105
|
};
|
|
98
106
|
|
|
99
107
|
export async function forwardPort(config: {
|
|
@@ -113,6 +121,12 @@ export async function forwardPort(config: {
|
|
|
113
121
|
let duration = config.duration ?? PERMANENT_LEASE;
|
|
114
122
|
let permanent = duration === PERMANENT_LEASE;
|
|
115
123
|
|
|
124
|
+
// Forwarding only makes sense behind a NAT. On a directly-reachable public host there's no
|
|
125
|
+
// gateway, so skip UPnP entirely (no SSDP) rather than discovering-and-timing-out.
|
|
126
|
+
if (!await isBehindNAT()) {
|
|
127
|
+
return { owned: false, reason: "notBehindNat" };
|
|
128
|
+
}
|
|
129
|
+
|
|
116
130
|
// Take ownership of the router's mapping for this external port: delete whatever's there (a
|
|
117
131
|
// stale mapping of ours, or another host's) so our AddPortMapping isn't rejected as a conflict
|
|
118
132
|
// (718), then install our mapping. This makes the last writer win — whichever application
|