@stacksjs/server 0.75.672 → 0.75.683
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/dist/security-headers.d.ts +57 -1
- package/dist/security-headers.js +1 -1
- package/package.json +13 -13
|
@@ -1,3 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Whether `APP_URL` is an https address on a real host, which is when HSTS is
|
|
3
|
+
* safe to send. Over http a browser ignores HSTS anyway; on a local host it
|
|
4
|
+
* would pin `*.localhost` or a `.test` domain to HTTPS for a year, which is
|
|
5
|
+
* why this path never sent it before.
|
|
6
|
+
*/
|
|
7
|
+
export declare function servesHttpsPublicly(appUrl: string | undefined): boolean;
|
|
8
|
+
/**
|
|
9
|
+
* The page security rules from `config.server.security` and `APP_URL`
|
|
10
|
+
* (stacksjs/stacks#3358). Resolved once at boot, like the embeddable rules.
|
|
11
|
+
*/
|
|
12
|
+
export declare function resolveViewSecurityRules(security: ViewSecurityOptions | undefined, appUrl: string | undefined): ViewSecurityRules;
|
|
13
|
+
/**
|
|
14
|
+
* The CSP header value for one response: the defaults, then the app's
|
|
15
|
+
* directives over them, with `nonce` added to `script-src`. An embeddable
|
|
16
|
+
* path has no `frame-ancestors`, the CSP twin of its missing X-Frame-Options.
|
|
17
|
+
*/
|
|
18
|
+
export declare function pageContentSecurityPolicy(directives: CspDirectives, nonce: string, embeddable: boolean, reportUri?: string): string;
|
|
1
19
|
/**
|
|
2
20
|
* Normalise `config.server.security.embeddable` into the rules the
|
|
3
21
|
* per-response check reads.
|
|
@@ -24,7 +42,7 @@ export declare function isEmbeddablePath(pathname: string, rules: EmbeddableRule
|
|
|
24
42
|
* `X-Frame-Options` in a template or a proxy has said something more
|
|
25
43
|
* specific.
|
|
26
44
|
*/
|
|
27
|
-
export declare function applyViewSecurityHeaders(req: Request, response: Response, rules: EmbeddableRules): Response | undefined;
|
|
45
|
+
export declare function applyViewSecurityHeaders(req: Request, response: Response, rules: EmbeddableRules | ViewSecurityRules): Response | undefined;
|
|
28
46
|
/** Test helper - reset the cached env-derived flag. */
|
|
29
47
|
export declare function __resetViewSecurityHeadersCache(): void;
|
|
30
48
|
/** Paths another origin is allowed to frame. */
|
|
@@ -32,3 +50,41 @@ export declare interface EmbeddableRules {
|
|
|
32
50
|
paths: string[]
|
|
33
51
|
prefixes: string[]
|
|
34
52
|
}
|
|
53
|
+
/**
|
|
54
|
+
* Everything the views server stamps on a page, resolved once at boot.
|
|
55
|
+
*
|
|
56
|
+
* A `null` header is not sent. `resolveEmbeddableRules()` alone still gives
|
|
57
|
+
* the original three headers, so a caller that never resolved these is
|
|
58
|
+
* unchanged.
|
|
59
|
+
*/
|
|
60
|
+
export declare interface ViewSecurityRules extends EmbeddableRules {
|
|
61
|
+
hsts?: string | null
|
|
62
|
+
crossOriginOpenerPolicy?: string | null
|
|
63
|
+
permissionsPolicy?: string | null
|
|
64
|
+
csp?: ResolvedPageCsp | null
|
|
65
|
+
}
|
|
66
|
+
declare interface ResolvedPageCsp {
|
|
67
|
+
reportOnly: boolean
|
|
68
|
+
directives: CspDirectives
|
|
69
|
+
reportUri?: string
|
|
70
|
+
}
|
|
71
|
+
/** The page security settings in `config.server.security`. */
|
|
72
|
+
export declare interface ViewSecurityOptions {
|
|
73
|
+
embeddable?: readonly string[]
|
|
74
|
+
hsts?: boolean | string
|
|
75
|
+
crossOriginOpenerPolicy?: string | false
|
|
76
|
+
permissionsPolicy?: Record<string, string> | false
|
|
77
|
+
csp?: PageCspOption
|
|
78
|
+
}
|
|
79
|
+
/** CSP directives: each a list of sources, or `false` to drop a default one. */
|
|
80
|
+
export type CspDirectives = Record<string, readonly string[] | false>;
|
|
81
|
+
/** `config.server.security.csp`. */
|
|
82
|
+
export type PageCspOption = boolean | 'report-only' | {
|
|
83
|
+
/** Send `Content-Security-Policy-Report-Only`, which reports and blocks nothing. */
|
|
84
|
+
reportOnly?: boolean
|
|
85
|
+
/** Merged over the defaults; the nonce is always added to `script-src`. */
|
|
86
|
+
directives?: CspDirectives
|
|
87
|
+
/** Where the browser posts violation reports. */
|
|
88
|
+
reportUri?: string
|
|
89
|
+
}
|
|
90
|
+
export {};
|
package/dist/security-headers.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import process from"node:process";let _isDisabledCache;function isDisabled(){if(_isDisabledCache!==undefined)return _isDisabledCache;_isDisabledCache=process.env.STACKS_SECURITY_HEADERS_DISABLE==="true";return _isDisabledCache}export function resolveEmbeddableRules(input){const paths=[];const prefixes=[];for(const raw of input??[]){const entry=String(raw).trim();if(!entry||!entry.startsWith("/"))continue;if(entry.endsWith("/")&&entry.length>1){if(!prefixes.includes(entry))prefixes.push(entry);continue}if(!paths.includes(entry))paths.push(entry)}return{paths,prefixes}}function matchesPrefix(pathname,prefix){return pathname.startsWith(prefix)||pathname===prefix.slice(0,-1)}export function isEmbeddablePath(pathname,rules){if(rules.paths.includes(pathname))return true;return rules.prefixes.some((prefix)=>matchesPrefix(pathname,prefix))}export function applyViewSecurityHeaders(req,response,rules){if(isDisabled())return;const embeddable=isEmbeddablePath(new URL(req.url).pathname,rules);const wanted=[["X-Content-Type-Options","nosniff"],["Referrer-Policy","strict-origin-when-cross-origin"]];if(!embeddable)wanted.push(["X-Frame-Options","SAMEORIGIN"]);const missing=wanted.filter(([name])=>!response.headers.has(name));if(missing.length
|
|
1
|
+
import process from"node:process";const HSTS_DEFAULT="max-age=31536000; includeSubDomains";const PERMISSIONS_POLICY_DEFAULT={"browsing-topics":"()",usb:"()",serial:"()",hid:"()",bluetooth:"()",midi:"()"};export function servesHttpsPublicly(appUrl){if(!appUrl)return false;let url;try{url=new URL(appUrl.includes("://")?appUrl:`https://${appUrl}`)}catch{return false}if(url.protocol!=="https:")return false;const host=url.hostname.replace(/^\[|\]$/g,"");return!(host==="localhost"||host.endsWith(".localhost")||host.endsWith(".test")||host==="::1"||/^127\.\d+\.\d+\.\d+$/.test(host))}export function resolveViewSecurityRules(security,appUrl){const rules=resolveEmbeddableRules(security?.embeddable);const hsts=security?.hsts;rules.hsts=hsts===false?null:typeof hsts==="string"?hsts:servesHttpsPublicly(appUrl)?HSTS_DEFAULT:null;const coop=security?.crossOriginOpenerPolicy;rules.crossOriginOpenerPolicy=coop===false?null:coop||"same-origin-allow-popups";const permissions=security?.permissionsPolicy;rules.permissionsPolicy=permissions===false?null:Object.entries({...PERMISSIONS_POLICY_DEFAULT,...permissions}).map(([feature,allow])=>`${feature}=${allow}`).join(", ");const csp=security?.csp;rules.csp=!csp?null:csp===true||csp==="report-only"?{reportOnly:csp==="report-only",directives:{}}:{reportOnly:csp.reportOnly===true,directives:csp.directives??{},reportUri:csp.reportUri};return rules}const PAGE_CSP_DEFAULTS={"default-src":["'self'"],"script-src":["'self'"],"style-src":["'self'","'unsafe-inline'"],"img-src":["'self'","data:","blob:","https:"],"font-src":["'self'","data:"],"connect-src":["'self'"],"object-src":["'none'"],"base-uri":["'self'"],"form-action":["'self'"],"frame-ancestors":["'self'"]};export function pageContentSecurityPolicy(directives,nonce,embeddable,reportUri){const merged={...PAGE_CSP_DEFAULTS,...directives};if(embeddable&&!directives["frame-ancestors"])merged["frame-ancestors"]=false;const parts=[];for(const[name,sources]of Object.entries(merged)){if(sources===false)continue;const list=name==="script-src"?[...sources,`'nonce-${nonce}'`]:sources;parts.push(list.length>0?`${name} ${list.join(" ")}`:name)}if(reportUri)parts.push(`report-uri ${reportUri}`);return parts.join("; ")}function newNonce(){return Buffer.from(crypto.getRandomValues(new Uint8Array(16))).toString("base64")}function withPageCsp(response,csp,embeddable){const header=csp.reportOnly?"Content-Security-Policy-Report-Only":"Content-Security-Policy";if(!/^text\/html\b/i.test(response.headers.get("Content-Type")??"")||response.headers.has("Content-Security-Policy")||response.headers.has("Content-Security-Policy-Report-Only"))return response;const nonce=newNonce();const rewritten=new HTMLRewriter().on("script",{element(element){element.setAttribute("nonce",nonce)}}).transform(response);const headers=new Headers(rewritten.headers);headers.set(header,pageContentSecurityPolicy(csp.directives,nonce,embeddable,csp.reportUri));headers.delete("Content-Length");return new Response(rewritten.body,{status:response.status,statusText:response.statusText,headers})}let _isDisabledCache;function isDisabled(){if(_isDisabledCache!==undefined)return _isDisabledCache;_isDisabledCache=process.env.STACKS_SECURITY_HEADERS_DISABLE==="true";return _isDisabledCache}export function resolveEmbeddableRules(input){const paths=[];const prefixes=[];for(const raw of input??[]){const entry=String(raw).trim();if(!entry||!entry.startsWith("/"))continue;if(entry.endsWith("/")&&entry.length>1){if(!prefixes.includes(entry))prefixes.push(entry);continue}if(!paths.includes(entry))paths.push(entry)}return{paths,prefixes}}function matchesPrefix(pathname,prefix){return pathname.startsWith(prefix)||pathname===prefix.slice(0,-1)}export function isEmbeddablePath(pathname,rules){if(rules.paths.includes(pathname))return true;return rules.prefixes.some((prefix)=>matchesPrefix(pathname,prefix))}export function applyViewSecurityHeaders(req,response,rules){if(isDisabled())return;const embeddable=isEmbeddablePath(new URL(req.url).pathname,rules);const wanted=[["X-Content-Type-Options","nosniff"],["Referrer-Policy","strict-origin-when-cross-origin"]];if(!embeddable)wanted.push(["X-Frame-Options","SAMEORIGIN"]);const view=rules;if(view.hsts)wanted.push(["Strict-Transport-Security",view.hsts]);if(view.crossOriginOpenerPolicy)wanted.push(["Cross-Origin-Opener-Policy",view.crossOriginOpenerPolicy]);if(view.permissionsPolicy)wanted.push(["Permissions-Policy",view.permissionsPolicy]);const missing=wanted.filter(([name])=>!response.headers.has(name));let secured;if(missing.length>0){try{for(const[name,value]of missing)response.headers.set(name,value)}catch{const headers=new Headers(response.headers);for(const[name,value]of missing)headers.set(name,value);secured=new Response(response.body,{status:response.status,statusText:response.statusText,headers})}}if(!view.csp)return secured;const current=secured??response;const withCsp=withPageCsp(current,view.csp,embeddable);return withCsp===current?secured:withCsp}export function __resetViewSecurityHeadersCache(){_isDisabledCache=undefined}
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@stacksjs/server",
|
|
3
3
|
"type": "module",
|
|
4
4
|
"sideEffects": false,
|
|
5
|
-
"version": "0.75.
|
|
5
|
+
"version": "0.75.683",
|
|
6
6
|
"description": "Local development and production-ready.",
|
|
7
7
|
"author": "Chris Breuer",
|
|
8
8
|
"contributors": [
|
|
@@ -58,22 +58,22 @@
|
|
|
58
58
|
"prepublishOnly": "bun run build"
|
|
59
59
|
},
|
|
60
60
|
"dependencies": {
|
|
61
|
-
"@stacksjs/auth": "0.75.
|
|
61
|
+
"@stacksjs/auth": "0.75.683",
|
|
62
62
|
"@stacksjs/bun-router": "^0.1.24",
|
|
63
|
-
"@stacksjs/config": "0.75.
|
|
64
|
-
"@stacksjs/events": "0.75.
|
|
65
|
-
"@stacksjs/i18n": "0.75.
|
|
66
|
-
"@stacksjs/logging": "0.75.
|
|
67
|
-
"@stacksjs/orm": "0.75.
|
|
68
|
-
"@stacksjs/path": "0.75.
|
|
69
|
-
"@stacksjs/payments": "0.75.
|
|
70
|
-
"@stacksjs/router": "0.75.
|
|
71
|
-
"@stacksjs/storage": "0.75.
|
|
72
|
-
"@stacksjs/stx": "^0.2.
|
|
63
|
+
"@stacksjs/config": "0.75.683",
|
|
64
|
+
"@stacksjs/events": "0.75.683",
|
|
65
|
+
"@stacksjs/i18n": "0.75.683",
|
|
66
|
+
"@stacksjs/logging": "0.75.683",
|
|
67
|
+
"@stacksjs/orm": "0.75.683",
|
|
68
|
+
"@stacksjs/path": "0.75.683",
|
|
69
|
+
"@stacksjs/payments": "0.75.683",
|
|
70
|
+
"@stacksjs/router": "0.75.683",
|
|
71
|
+
"@stacksjs/storage": "0.75.683",
|
|
72
|
+
"@stacksjs/stx": "^0.2.934",
|
|
73
73
|
"bun-plugin-auto-imports": "^0.4.4"
|
|
74
74
|
},
|
|
75
75
|
"devDependencies": {
|
|
76
|
-
"@stacksjs/validation": "0.75.
|
|
76
|
+
"@stacksjs/validation": "0.75.683",
|
|
77
77
|
"better-dx": "^0.2.39"
|
|
78
78
|
}
|
|
79
79
|
}
|