socket-function 1.2.25 → 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/package.json +1 -1
- package/src/callHTTPHandler.ts +33 -3
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,
|