socket-function 0.66.0 → 0.67.0

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "socket-function",
3
- "version": "0.66.0",
3
+ "version": "0.67.0",
4
4
  "main": "index.js",
5
5
  "license": "MIT",
6
6
  "dependencies": {
@@ -134,8 +134,9 @@ class RequireControllerBase {
134
134
 
135
135
  public async requireHTML(config?: {
136
136
  requireCalls?: string[];
137
+ cacheTime?: number;
137
138
  }) {
138
- let { requireCalls } = config || {};
139
+ let { requireCalls, cacheTime } = config || {};
139
140
  let result = resolvedHTMLFile();
140
141
  if (beforeEntryText.length > 0) {
141
142
  let resolved: string[] = [];
@@ -166,7 +167,13 @@ class RequireControllerBase {
166
167
  } else {
167
168
  result = result.replace(ENTRY_TEMPLATE, "");
168
169
  }
169
- return setHTTPResultHeaders(Buffer.from(result), { "Content-Type": "text/html" });
170
+ let headers: Record<string, string> = {
171
+ "Content-Type": "text/html"
172
+ };
173
+ if (cacheTime) {
174
+ headers["Cache-Control"] = `max-age=${Math.floor(cacheTime / 1000)}`;
175
+ }
176
+ return setHTTPResultHeaders(Buffer.from(result), headers);
170
177
  }
171
178
 
172
179
  public async getModules(
@@ -61,6 +61,53 @@ export async function httpCallHandler(request: http.IncomingMessage, response: h
61
61
  // Don't keep alive, to prevent issues with zombie sockets.
62
62
  response.setHeader("Connection", "close");
63
63
 
64
+ // CORS bs (due to having to explictly allow subdomains)
65
+ {
66
+ response.setHeader("Strict-Transport-Security", "max-age=63072000; includeSubDomains; preload");
67
+ response.setHeader("Cross-Origin-Opener-Policy", "same-origin-allow-popups");
68
+ // NOTE: "credentialless" would work here too
69
+ response.setHeader("Cross-Origin-Embedder-Policy", "require-corp");
70
+
71
+ let origin = request.headers.origin || request.headers.referer;
72
+ let allowed = false;
73
+ if (!origin) {
74
+ // I guess it's a script, so just allow it (as it could easily set any header it wanted anyways)
75
+ allowed = true;
76
+ origin = "*";
77
+ } else {
78
+ let host = request.headers.host;
79
+ if (!host) {
80
+ throw new Error("Missing host in request headers");
81
+ }
82
+ function rootDomain(hostname: string) {
83
+ let parts = hostname.split(".");
84
+ if (parts.length > 2) {
85
+ return parts.slice(-2).join(".");
86
+ }
87
+ return hostname;
88
+ }
89
+ allowed = rootDomain(host) === rootDomain(origin);
90
+ }
91
+
92
+ // Allow if it has no origin, as that means it isn't a CORS request?
93
+ if (allowed) {
94
+ response.setHeader("Cross-Origin-Resource-Policy", "cross-origin");
95
+ } else {
96
+ response.setHeader("Cross-Origin-Resource-Policy", "same-site");
97
+ }
98
+
99
+ response.setHeader("vary", "Access-Control-Request-Headers");
100
+ response.setHeader("Access-Control-Allow-Origin", allowed ? origin : "");
101
+
102
+ if (allowed) {
103
+ response.setHeader("Access-Control-Allow-Credentials", "true");
104
+ response.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
105
+ response.setHeader("Access-Control-Allow-Headers", "Content-Type, Authorization, Content-Length, X-Requested-With, x-uncompressed-content-length, Cookie");
106
+ }
107
+ response.setHeader("Access-Control-Expose-Headers", "x-uncompressed-content-length");
108
+ }
109
+
110
+
64
111
  let urlBase = request.url;
65
112
  if (!urlBase) {
66
113
  throw new Error("Missing URL");
File without changes
@@ -0,0 +1,41 @@
1
+ {
2
+ "instructions": [
3
+ {
4
+ "messages": [
5
+ {
6
+ "role": "user",
7
+ "header": "# user",
8
+ "content": "How secure is comparing the Host and Origin headers, checking if they have the same root domain, and then adding \"Access-Control-Allow-Origin\"\n\n",
9
+ "slot": "",
10
+ "chStart": 0,
11
+ "chEnd": 7
12
+ }
13
+ ],
14
+ "params": {
15
+ "model": "openrouter-deepseek/deepseek-chat",
16
+ "max_tokens": 9999
17
+ },
18
+ "startIndex": 150,
19
+ "endIndex": 150
20
+ },
21
+ {
22
+ "messages": [
23
+ {
24
+ "role": "user",
25
+ "header": "# user",
26
+ "content": "How secure is comparing the Host and Origin headers, checking if they have the same root domain, and then adding \"Access-Control-Allow-Origin\"\n\n",
27
+ "slot": "",
28
+ "chStart": 0,
29
+ "chEnd": 7
30
+ }
31
+ ],
32
+ "params": {
33
+ "model": "openrouter-deepseek/deepseek-r1",
34
+ "max_tokens": 9999
35
+ },
36
+ "startIndex": 219,
37
+ "endIndex": 219
38
+ }
39
+ ],
40
+ "rawResult": "Comparing the `Host` and `Origin` headers to check if they have the same root domain and then dynamically setting the `Access-Control-Allow-Origin` header can be a reasonable approach to enforce cross-origin resource sharing (CORS) policies, but it has its nuances and potential pitfalls. Here's a detailed analysis of its security implications:\n\n### How It Works\n1. **Host Header**: Represents the domain of the server receiving the request.\n2. **Origin Header**: Represents the domain from which the request originates (e.g., in a cross-origin request).\n3. **Root Domain Comparison**: Check if the `Origin` and `Host` share the same root domain (e.g., `example.com` and `api.example.com`).\n4. **Access-Control-Allow-Origin**: If they match, set this header to the value of the `Origin` header to allow the request.\n\n### Security Considerations\n\n#### **1. Same-Origin Policy**\nThis approach ensures that only requests from the same root domain are allowed, which aligns with the same-origin policy. This can prevent unauthorized cross-origin requests.\n\n#### **2. Root Domain Matching**\nComparing the root domain ensures that subdomains of the same root domain are allowed. For example, `api.example.com` and `app.example.com` share the same root domain (`example.com`). This can be useful for microservices or APIs hosted on subdomains.\n\n#### **3. Potential Misconfigurations**\n- **Weak Root Domain Matching**: If the root domain matching logic is not strict, it could lead to vulnerabilities. For example, improperly parsing domains could allow `example.com.evil.com` to be treated as the same root domain.\n- **HTTP Header Spoofing**: If the server blindly trusts the `Origin` header, an attacker could spoof it to bypass CORS restrictions. Always validate the `Origin` header against a whitelist of allowed domains.\n\n#### **4. Security Best Practices**\n- **Whitelist Allowed Origins**: Instead of dynamically allowing any origin that matches the root domain, maintain a whitelist of trusted origins. This reduces the risk of allowing unintended domains.\n- **Validate Headers**: Ensure that the `Origin` and `Host` headers are properly validated to prevent spoofing or injection attacks.\n- **Use HTTPS**: Always enforce HTTPS to prevent man-in-the-middle attacks that could tamper with headers.\n- **Limit Scopes**: Only allow CORS for specific endpoints that require it, and restrict methods (e.g., `GET`, `POST`) and headers as needed.\n- **Avoid Wildcards**: Avoid using `Access-Control-Allow-Origin: *` unless absolutely necessary, as it allows access from any domain.\n\n#### **5. Additional Security Headers**\n- **Access-Control-Allow-Credentials**: Only set this to `true` if the request includes credentials (e.g., cookies) and you explicitly need to allow them.\n- **Vary: Origin**: Include this header to prevent caching issues with CORS responses.\n\n### Example Implementation (Pseudocode)\n```pseudocode\nif request.headers['Origin'] is not null:\n root_domain_origin = extract_root_domain(request.headers['Origin'])\n root_domain_host = extract_root_domain(request.headers['Host'])\n \n if root_domain_origin == root_domain_host:\n response.headers['Access-Control-Allow-Origin'] = request.headers['Origin']\n response.headers['Vary'] = 'Origin'\n else:\n deny_request()\nelse:\n deny_request()\n```\n\n### Conclusion\nThis approach can be secure if implemented carefully, but it's essential to follow best practices like strict domain validation, whitelisting origins, and enforcing HTTPS. Always test your CORS implementation thoroughly to ensure it doesn't introduce vulnerabilities."
41
+ }