@procedures/http 0.52.3 → 0.53.1

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.
@@ -0,0 +1,51 @@
1
+ /**
2
+ * The frame rules of a response. A page can refuse to show in a frame of another page. Two headers tell the
3
+ * refusal: `Content-Security-Policy` with the directive `frame-ancestors` (CSP3), and `X-Frame-Options` (the HTML
4
+ * standard). These functions read the headers and give the decision of a browser. They use no network.
5
+ */
6
+ /** A policy of `Content-Security-Policy`: the source list of each directive, by its name in lower case. */
7
+ export type CspPolicy = ReadonlyMap<string, readonly string[]>;
8
+ /** The headers of a response: a `Headers` object, or a record of header values as Node gives them. */
9
+ export type HeaderSource = Headers | Readonly<Record<string, string | readonly string[] | undefined>>;
10
+ /** The decision of `frameRefusal`. `by` names the header that refuses the frame, or it is `null`. */
11
+ export interface FrameRefusal {
12
+ readonly refused: boolean;
13
+ readonly by: "csp" | "x-frame-options" | null;
14
+ readonly detail: string;
15
+ }
16
+ /** The two origins of a frame: the page in the frame, and the page that holds the frame. */
17
+ export interface FrameOrigins {
18
+ /** The origin of the page that holds the frame, for example `https://a.example`. */
19
+ readonly ancestor: string | URL;
20
+ /** The address of the page in the frame, after each redirect. Its origin is `'self'`. */
21
+ readonly page: string | URL;
22
+ }
23
+ /**
24
+ * This function gives the policies of the values of `Content-Security-Policy` headers. A comma divides the policies
25
+ * of one value. The first copy of a directive in a policy wins, as in a browser. A policy with no directive is not
26
+ * in the result.
27
+ */
28
+ export declare function parseCsp(values: readonly string[]): CspPolicy[];
29
+ /**
30
+ * This function tells if one source expression of CSP3 accepts an origin. `self` is the origin of the protected page.
31
+ * The function compares origins, so it ignores the path part of an expression, as `frame-ancestors` does.
32
+ * A nonce, a hash and each other keyword accept no origin.
33
+ */
34
+ export declare function sourceAllows(source: string, origin: string | URL, self: string | URL): boolean;
35
+ /**
36
+ * This function tells if a source list of CSP3 accepts an origin. An empty list and the list `'none'` accept no origin.
37
+ * Thus `frame-ancestors` with no value refuses each frame.
38
+ */
39
+ export declare function sourceListAllows(list: readonly string[], origin: string | URL, self: string | URL): boolean;
40
+ /**
41
+ * This function tells if a browser refuses to show a page in a frame of the ancestor. It obeys these rules:
42
+ *
43
+ * - An enforced policy with `frame-ancestors` decides, and the function ignores `X-Frame-Options`. Each such policy must accept the ancestor.
44
+ * - Else `X-Frame-Options` decides, by the rules of the HTML standard. `DENY` refuses, and `SAMEORIGIN` refuses an ancestor of another origin.
45
+ * - Two or more different values, with `DENY`, `SAMEORIGIN` or `ALLOWALL` in them, refuse each frame. `ALLOW-FROM` and unknown values do not refuse.
46
+ * - A header `Content-Security-Policy-Report-Only` does not refuse.
47
+ *
48
+ * A browser examines each ancestor of a nested frame. To do the same, give each ancestor to this function.
49
+ */
50
+ export declare function frameRefusal(headers: HeaderSource, { ancestor, page }: FrameOrigins): FrameRefusal;
51
+ //# sourceMappingURL=frame.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"frame.d.ts","sourceRoot":"","sources":["../src/frame.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,2GAA2G;AAC3G,MAAM,MAAM,SAAS,GAAG,WAAW,CAAC,MAAM,EAAE,SAAS,MAAM,EAAE,CAAC,CAAC;AAE/D,sGAAsG;AACtG,MAAM,MAAM,YAAY,GAAG,OAAO,GAAG,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,MAAM,EAAE,GAAG,SAAS,CAAC,CAAC,CAAC;AAEtG,qGAAqG;AACrG,MAAM,WAAW,YAAY;IAC3B,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC;IAC1B,QAAQ,CAAC,EAAE,EAAE,KAAK,GAAG,iBAAiB,GAAG,IAAI,CAAC;IAC9C,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;CACzB;AAED,4FAA4F;AAC5F,MAAM,WAAW,YAAY;IAC3B,oFAAoF;IACpF,QAAQ,CAAC,QAAQ,EAAE,MAAM,GAAG,GAAG,CAAC;IAChC,yFAAyF;IACzF,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,GAAG,CAAC;CAC7B;AAQD;;;;GAIG;AACH,wBAAgB,QAAQ,CAAC,MAAM,EAAE,SAAS,MAAM,EAAE,GAAG,SAAS,EAAE,CAe/D;AAiED;;;;GAIG;AACH,wBAAgB,YAAY,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,GAAG,EAAE,IAAI,EAAE,MAAM,GAAG,GAAG,GAAG,OAAO,CAE9F;AAED;;;GAGG;AACH,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,SAAS,MAAM,EAAE,EAAE,MAAM,EAAE,MAAM,GAAG,GAAG,EAAE,IAAI,EAAE,MAAM,GAAG,GAAG,GAAG,OAAO,CAK3G;AAsBD;;;;;;;;;GASG;AACH,wBAAgB,YAAY,CAAC,OAAO,EAAE,YAAY,EAAE,EAAE,QAAQ,EAAE,IAAI,EAAE,EAAE,YAAY,GAAG,YAAY,CAyBlG"}
package/dist/frame.js ADDED
@@ -0,0 +1,175 @@
1
+ /**
2
+ * The frame rules of a response. A page can refuse to show in a frame of another page. Two headers tell the
3
+ * refusal: `Content-Security-Policy` with the directive `frame-ancestors` (CSP3), and `X-Frame-Options` (the HTML
4
+ * standard). These functions read the headers and give the decision of a browser. They use no network.
5
+ */
6
+ const ASCII_WHITESPACE = /[\t\n\f\r ]+/;
7
+ const DIRECTIVE_NAME = /^[a-z0-9-]+$/;
8
+ const SCHEME_SOURCE = /^([a-z][a-z0-9+.-]*):$/i;
9
+ const HOST_SOURCE = /^(?:([a-z][a-z0-9+.-]*):\/\/)?(\*|(?:\*\.)?[a-z0-9-]+(?:\.[a-z0-9-]+)*\.?)(?::(\*|[0-9]+))?(\/[^;,]*)?$/i;
10
+ const DEFAULT_PORTS = { http: 80, https: 443, ws: 80, wss: 443, ftp: 21 };
11
+ /**
12
+ * This function gives the policies of the values of `Content-Security-Policy` headers. A comma divides the policies
13
+ * of one value. The first copy of a directive in a policy wins, as in a browser. A policy with no directive is not
14
+ * in the result.
15
+ */
16
+ export function parseCsp(values) {
17
+ const policies = [];
18
+ for (const value of values) {
19
+ for (const serialized of value.split(",")) {
20
+ const policy = new Map();
21
+ for (const token of serialized.split(";")) {
22
+ const [name, ...list] = token.trim().split(ASCII_WHITESPACE);
23
+ const directive = (name ?? "").toLowerCase();
24
+ if (!DIRECTIVE_NAME.test(directive) || policy.has(directive))
25
+ continue;
26
+ policy.set(directive, list);
27
+ }
28
+ if (policy.size > 0)
29
+ policies.push(policy);
30
+ }
31
+ }
32
+ return policies;
33
+ }
34
+ function partsOf(origin) {
35
+ let url;
36
+ try {
37
+ url = typeof origin === "string" ? new URL(origin) : origin;
38
+ }
39
+ catch {
40
+ return undefined;
41
+ }
42
+ return { scheme: url.protocol.slice(0, -1).toLowerCase(), host: url.hostname === "" ? null : url.hostname.toLowerCase(), port: url.port === "" ? null : Number(url.port) };
43
+ }
44
+ /** This function is the scheme-part match of CSP3: the same scheme, or a secure upgrade of it. */
45
+ function schemeMatches(expression, scheme) {
46
+ const a = expression.toLowerCase();
47
+ if (a === scheme)
48
+ return true;
49
+ if (a === "http")
50
+ return scheme === "https";
51
+ if (a === "ws")
52
+ return scheme === "wss" || scheme === "http" || scheme === "https";
53
+ if (a === "wss")
54
+ return scheme === "https";
55
+ return false;
56
+ }
57
+ /** This function is the host-part match of CSP3. `*.example.com` matches each subdomain, but not `example.com`. */
58
+ function hostMatches(pattern, host) {
59
+ const lower = pattern.toLowerCase();
60
+ if (lower.startsWith("*"))
61
+ return host.endsWith(lower.slice(1));
62
+ return lower === host;
63
+ }
64
+ /** This function is the port-part match of CSP3. No port in the expression matches the default port of the scheme. */
65
+ function portMatches(input, url) {
66
+ if (input === "*")
67
+ return true;
68
+ const port = input === undefined ? null : Number(input);
69
+ if (port === url.port)
70
+ return true;
71
+ return url.port === null && port === (DEFAULT_PORTS[url.scheme] ?? null);
72
+ }
73
+ function partsAllow(source, url, self) {
74
+ if (url === undefined)
75
+ return false;
76
+ const keyword = source.toLowerCase();
77
+ if (keyword === "*")
78
+ return url.scheme === "http" || url.scheme === "https" || url.scheme === self?.scheme;
79
+ if (keyword === "'self'") {
80
+ if (self === undefined || self.host === null || self.host !== url.host)
81
+ return false;
82
+ if (self.scheme === url.scheme && self.port === url.port)
83
+ return true;
84
+ // CSP3 lets 'self' match a secure upgrade of the scheme, with the same port or the default ports
85
+ if (self.port !== url.port)
86
+ return false;
87
+ return url.scheme === "https" || url.scheme === "wss" || (self.scheme === "http" && (url.scheme === "http" || url.scheme === "ws"));
88
+ }
89
+ const scheme = SCHEME_SOURCE.exec(source);
90
+ if (scheme !== null)
91
+ return schemeMatches(scheme[1] ?? "", url.scheme);
92
+ const host = HOST_SOURCE.exec(source);
93
+ if (host === null || url.host === null)
94
+ return false;
95
+ const [, hostScheme, hostPart = "", port] = host;
96
+ if (hostScheme === undefined ? self === undefined || !schemeMatches(self.scheme, url.scheme) : !schemeMatches(hostScheme, url.scheme))
97
+ return false;
98
+ // An origin has no path, so the path part of the expression changes nothing
99
+ return hostMatches(hostPart, url.host) && portMatches(port, url);
100
+ }
101
+ /**
102
+ * This function tells if one source expression of CSP3 accepts an origin. `self` is the origin of the protected page.
103
+ * The function compares origins, so it ignores the path part of an expression, as `frame-ancestors` does.
104
+ * A nonce, a hash and each other keyword accept no origin.
105
+ */
106
+ export function sourceAllows(source, origin, self) {
107
+ return partsAllow(source, partsOf(origin), partsOf(self));
108
+ }
109
+ /**
110
+ * This function tells if a source list of CSP3 accepts an origin. An empty list and the list `'none'` accept no origin.
111
+ * Thus `frame-ancestors` with no value refuses each frame.
112
+ */
113
+ export function sourceListAllows(list, origin, self) {
114
+ if (list.length === 1 && list[0]?.toLowerCase() === "'none'")
115
+ return false;
116
+ const url = partsOf(origin);
117
+ const own = partsOf(self);
118
+ return list.some((source) => partsAllow(source, url, own));
119
+ }
120
+ /** This function gives the values of a header, as a list. */
121
+ function valuesOf(headers, name) {
122
+ if (typeof headers.get === "function" && typeof headers.forEach === "function") {
123
+ const value = headers.get(name);
124
+ return value === null ? [] : [value];
125
+ }
126
+ const values = [];
127
+ for (const [key, value] of Object.entries(headers)) {
128
+ if (key.toLowerCase() !== name || value === undefined)
129
+ continue;
130
+ if (typeof value === "string")
131
+ values.push(value);
132
+ else
133
+ values.push(...value);
134
+ }
135
+ return values;
136
+ }
137
+ const sameOrigin = (a, b) => a !== undefined && b !== undefined && a.host !== null && a.scheme === b.scheme && a.host === b.host && a.port === b.port;
138
+ const serialize = (origin) => (typeof origin === "string" ? origin : origin.origin);
139
+ /**
140
+ * This function tells if a browser refuses to show a page in a frame of the ancestor. It obeys these rules:
141
+ *
142
+ * - An enforced policy with `frame-ancestors` decides, and the function ignores `X-Frame-Options`. Each such policy must accept the ancestor.
143
+ * - Else `X-Frame-Options` decides, by the rules of the HTML standard. `DENY` refuses, and `SAMEORIGIN` refuses an ancestor of another origin.
144
+ * - Two or more different values, with `DENY`, `SAMEORIGIN` or `ALLOWALL` in them, refuse each frame. `ALLOW-FROM` and unknown values do not refuse.
145
+ * - A header `Content-Security-Policy-Report-Only` does not refuse.
146
+ *
147
+ * A browser examines each ancestor of a nested frame. To do the same, give each ancestor to this function.
148
+ */
149
+ export function frameRefusal(headers, { ancestor, page }) {
150
+ const policies = parseCsp(valuesOf(headers, "content-security-policy")).filter((policy) => policy.has("frame-ancestors"));
151
+ if (policies.length > 0) {
152
+ const index = policies.findIndex((policy) => !sourceListAllows(policy.get("frame-ancestors") ?? [], ancestor, page));
153
+ if (index === -1)
154
+ return { refused: false, by: null, detail: `Each frame-ancestors directive allows ${serialize(ancestor)}.` };
155
+ const list = (policies[index]?.get("frame-ancestors") ?? []).join(" ") || "(empty)";
156
+ return { refused: true, by: "csp", detail: `The frame-ancestors directive "${list}" does not allow ${serialize(ancestor)}.` };
157
+ }
158
+ const options = new Set(valuesOf(headers, "x-frame-options")
159
+ .flatMap((value) => value.split(","))
160
+ .map((value) => value.trim().toLowerCase()));
161
+ const values = [...options].join(", ");
162
+ if (options.size > 1) {
163
+ if (options.has("deny") || options.has("sameorigin") || options.has("allowall")) {
164
+ return { refused: true, by: "x-frame-options", detail: `X-Frame-Options has values that do not agree: ${values}.` };
165
+ }
166
+ return { refused: false, by: null, detail: `X-Frame-Options has no value that a browser uses: ${values}.` };
167
+ }
168
+ if (options.has("deny"))
169
+ return { refused: true, by: "x-frame-options", detail: "X-Frame-Options is DENY." };
170
+ if (options.has("sameorigin") && !sameOrigin(partsOf(ancestor), partsOf(page))) {
171
+ return { refused: true, by: "x-frame-options", detail: `X-Frame-Options is SAMEORIGIN, and ${serialize(ancestor)} is another origin.` };
172
+ }
173
+ return { refused: false, by: null, detail: options.size === 0 ? "No header refuses a frame." : `X-Frame-Options allows ${serialize(ancestor)}.` };
174
+ }
175
+ //# sourceMappingURL=frame.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"frame.js","sourceRoot":"","sources":["../src/frame.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAuBH,MAAM,gBAAgB,GAAG,cAAc,CAAC;AACxC,MAAM,cAAc,GAAG,cAAc,CAAC;AACtC,MAAM,aAAa,GAAG,yBAAyB,CAAC;AAChD,MAAM,WAAW,GAAG,0GAA0G,CAAC;AAC/H,MAAM,aAAa,GAAqC,EAAE,IAAI,EAAE,EAAE,EAAE,KAAK,EAAE,GAAG,EAAE,EAAE,EAAE,EAAE,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,EAAE,EAAE,CAAC;AAE5G;;;;GAIG;AACH,MAAM,UAAU,QAAQ,CAAC,MAAyB;IAChD,MAAM,QAAQ,GAAgB,EAAE,CAAC;IACjC,KAAK,MAAM,KAAK,IAAI,MAAM,EAAE,CAAC;QAC3B,KAAK,MAAM,UAAU,IAAI,KAAK,CAAC,KAAK,CAAC,GAAG,CAAC,EAAE,CAAC;YAC1C,MAAM,MAAM,GAAG,IAAI,GAAG,EAA6B,CAAC;YACpD,KAAK,MAAM,KA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and HEAD requests that goes through the host policy (audit pass 2, O55).
3
3
  *
4
- * A procedure of another package that reads a URL of its caller uses this function in place of
5
- * the global `fetch`: `weather.forecast`, and the modules of `@procedures/geo` and
4
+ * A procedure of another package uses this function in place of the global `fetch` for a URL of
5
+ * its caller. `weather.forecast` uses it, and so do the modules of `@procedures/geo` and
6
6
  * `@procedures/ics` that a host makes with it. The request has the same checks as `http.request`:
7
7
  *
8
8
  * - The host of the URL and of each redirect must pass the host policy. The default policy
9
9
  * refuses loopback, private, link-local and the other special addresses.
10
- * - The connection uses only the addresses that the check allowed (DNS rebinding).
10
+ * - The connection uses only the addresses that passed the check (DNS rebinding).
11
11
  * - The body of the response has the size limit of the settings.
12
+ * - A `HEAD` request reads only the headers. Thus a caller that needs the headers of a large page,
13
+ * for example for `frameRefusal`, does not read its body.
12
14
  *
13
15
  * A refusal is `FORBIDDEN` with the reason `host-not-allowed` (`isHostNotAllowed`). When the
14
16
  * signal of the caller aborts, the function throws the reason of the signal, as `fetch` does.
@@ -16,6 +18,8 @@
16
18
  import type { HttpModuleOptions } from "./module.js";
17
19
  /** The options of one request. */
18
20
  export interface HostFetchInit {
21
+ /** The method of the request. The default is `GET`. */
22
+ readonly method?: "GET" | "HEAD" | undefined;
19
23
  /** The headers of the request. */
20
24
  readonly headers?: Readonly<Record<string, string>> | undefined;
21
25
  /** The signal that stops the request. */
@@ -27,13 +31,15 @@ export interface HostFetchResponse {
27
31
  readonly status: number;
28
32
  /** The URL of the response: the last URL after the redirects. */
29
33
  readonly url: string;
34
+ /** The headers of the response, with names in lowercase. A header that repeats, for example `set-cookie`, is a list. */
35
+ readonly headers: Readonly<Record<string, string | readonly string[]>>;
30
36
  text(): Promise<string>;
31
37
  json(): Promise<unknown>;
32
38
  }
33
- /** A `fetch` for GET requests through the host policy. */
39
+ /** A `fetch` for GET and HEAD requests through the host policy. */
34
40
  export type HostFetch = (url: string, init?: HostFetchInit) => Promise<HostFetchResponse>;
35
41
  /**
36
- * This function gives a `fetch` for GET requests through the host policy of the options. The
42
+ * This function gives a `fetch` for GET and HEAD requests through the host policy of the options. The
37
43
  * options are the options of `createHttpModule()`. Without options, the policy is the default
38
44
  * policy: each public host, and no other host. A bad entry of `allow` throws here.
39
45
  *
@@ -1 +1 @@
1
- {"version":3,"file":"host-fetch.d.ts","sourceRoot":"","sources":["../src/host-fetch.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAOrD,kCAAkC;AAClC,MAAM,WAAW,aAAa;IAC5B,kCAAkC;IAClC,QAAQ,CAAC,OAAO,CAAC,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,GAAG,SAAS,CAAC;IAChE,yCAAyC;IACzC,QAAQ,CAAC,MAAM,CAAC,EAAE,WAAW,GAAG,SAAS,CAAC;CAC3C;AAED,+FAA+F;AAC/F,MAAM,WAAW,iBAAiB;IAChC,QAAQ,CAAC,EAAE,EAAE,OAAO,CAAC;IACrB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,iEAAiE;IACjE,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,IAAI,IAAI,OAAO,CAAC,MAAM,CAAC,CAAC;IACxB,IAAI,IAAI,OAAO,CAAC,OAAO,CAAC,CAAC;CAC1B;AAED,0DAA0D;AAC1D,MAAM,MAAM,SAAS,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,aAAa,KAAK,OAAO,CAAC,iBAAiB,CAAC,CAAC;AAE1F;;;;;;;;GAQG;AACH,wBAAgB,eAAe,CAAC,OAAO,GAAE,iBAAsB,GAAG,SAAS,CAuB1E"}
1
+ {"version":3,"file":"host-fetch.d.ts","sourceRoot":"","sources":["../src/host-fetch.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAEH,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAOrD,kCAAkC;AAClC,MAAM,WAAW,aAAa;IAC5B,uDAAuD;IACvD,QAAQ,CAAC,MAAM,CAAC,EAAE,KAAK,GAAG,MAAM,GAAG,SAAS,CAAC;IAC7C,kCAAkC;IAClC,QAAQ,CAAC,OAAO,CAAC,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,GAAG,SAAS,CAAC;IAChE,yCAAyC;IACzC,QAAQ,CAAC,MAAM,CAAC,EAAE,WAAW,GAAG,SAAS,CAAC;CAC3C;AAED,+FAA+F;AAC/F,MAAM,WAAW,iBAAiB;IAChC,QAAQ,CAAC,EAAE,EAAE,OAAO,CAAC;IACrB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,iEAAiE;IACjE,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,wHAAwH;IACxH,QAAQ,CAAC,OAAO,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,MAAM,EAAE,CAAC,CAAC,CAAC;IACvE,IAAI,IAAI,OAAO,CAAC,MAAM,CAAC,CAAC;IACxB,IAAI,IAAI,OAAO,CAAC,OAAO,CAAC,CAAC;CAC1B;AAED,mEAAmE;AACnE,MAAM,MAAM,SAAS,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,aAAa,KAAK,OAAO,CAAC,iBAAiB,CAAC,CAAC;AAE1F;;;;;;;;GAQG;AACH,wBAAgB,eAAe,CAAC,OAAO,GAAE,iBAAsB,GAAG,SAAS,CAwB1E"}
@@ -1,14 +1,16 @@
1
1
  /**
2
- * A `fetch` for GET requests that goes through the host policy (audit pass 2, O55).
2
+ * A `fetch` for GET and HEAD requests that goes through the host policy (audit pass 2, O55).
3
3
  *
4
- * A procedure of another package that reads a URL of its caller uses this function in place of
5
- * the global `fetch`: `weather.forecast`, and the modules of `@procedures/geo` and
4
+ * A procedure of another package uses this function in place of the global `fetch` for a URL of
5
+ * its caller. `weather.forecast` uses it, and so do the modules of `@procedures/geo` and
6
6
  * `@procedures/ics` that a host makes with it. The request has the same checks as `http.request`:
7
7
  *
8
8
  * - The host of the URL and of each redirect must pass the host policy. The default policy
9
9
  * refuses loopback, private, link-local and the other special addresses.
10
- * - The connection uses only the addresses that the check allowed (DNS rebinding).
10
+ * - The connection uses only the addresses that passed the check (DNS rebinding).
11
11
  * - The body of the response has the size limit of the settings.
12
+ * - A `HEAD` request reads only the headers. Thus a caller that needs the headers of a large page,
13
+ * for example for `frameRefusal`, does not read its body.
12
14
  *
13
15
  * A refusal is `FORBIDDEN` with the reason `host-not-allowed` (`isHostNotAllowed`). When the
14
16
  * signal of the caller aborts, the function throws the reason of the signal, as `fetch` does.
@@ -18,7 +20,7 @@ import { httpRequest } from "./procedures/http/request.js";
18
20
  /** The longest time of a request of `http.request`: the signal of the caller is the real limit. */
19
21
  const LONGEST_MS = 86_400_000;
20
22
  /**
21
- * This function gives a `fetch` for GET requests through the host policy of the options. The
23
+ * This function gives a `fetch` for GET and HEAD requests through the host policy of the options. The
22
24
  * options are the options of `createHttpModule()`. Without options, the policy is the default
23
25
  * policy: each public host, and no other host. A bad entry of `allow` throws here.
24
26
  *
@@ -31,7 +33,7 @@ export function createHostFetch(options = {}) {
31
33
  return async (url, init = {}) => {
32
34
  let output;
33
35
  try {
34
- output = await httpRequest({ url, method: "GET", ...(init.headers !== undefined ? { headers: { ...init.headers } } : {}), responseType: "text", timeoutMs: LONGEST_MS }, { signal: init.signal }, settings);
36
+ output = await httpRequest({ url, method: init.method ?? "GET", ...(init.headers !== undefined ? { headers: { ...init.headers } } : {}), responseType: "text", timeoutMs: LONGEST_MS }, { signal: init.signal }, settings);
35
37
  }
36
38
  catch (error) {
37
39
  if (init.signal?.aborted)
@@ -43,6 +45,7 @@ export function createHostFetch(options = {}) {
43
45
  ok: output.ok,
44
46
  status: output.status,
45
47
  url: output.url,
48
+ headers: output.headers,
46
49
  text: async ()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AAM,CAAC;YACnD,MAAM,KAAK,CAAC;QACd,CAAC;QACD,MAAM,IAAI,GAAG,OAAO,MAAM,CAAC,IAAI,KAAK,QAAQ,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC;QAChE,OAAO;YACL,EAAE,EAAE,MAAM,CAAC,EAAE;YACb,MAAM,EAAE,MAAM,CAAC,MAAM;YACrB,GAAG,EAAE,MAAM,CAAC,GAAG;YACf,OAAO,EAAE,MAAM,CAAC,OAAO;YACvB,IAAI,EAAE,KAAK,IAAI,EAAE,CAAC,IAAI;YACtB,IAAI,EAAE,KAAK,IAAI,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAY;SAC9C,CAAC;IACJ,CAAC,CAAC;AACJ,CAAC"}
package/dist/index.d.ts CHANGED
@@ -8,4 +8,6 @@ export { isPublicAddress, parseAddress, specialClass } from "./address.js";
8
8
  export { HttpError, type HttpErrorCode } from "./errors.js";
9
9
  export { DEFAULT_MAX_BYTES, DEFAULT_ITEM_BYTES, DEFAULT_TIMEOUT_MS, DEFAULT_SETTINGS, type HttpSettings } from "./exchange.js";
10
10
  export { ndjsonLines, sseEvents, type NdjsonLine, type ServerSentEvent } from "./parsers.js";
11
+ export { frameRefusal, parseCsp, sourceAllows, sourceListAllows } from "./frame.js";
12
+ export type { CspPolicy, FrameOrigins, FrameRefusal, HeaderSource } from "./frame.js";
11
13
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,cAAc,aAAa,CAAC;AAC5B,cAAc,YAAY,CAAC;AAC3B,OAAO,EAAE,WAAW,EAAE,QAAQ,EAAE,UAAU,EAAE,OAAO,EAAE,MAAM,4BAA4B,CAAC;AAExF,OAAO,EAAE,gBAAgB,EAAE,mBAAmB,EAAE,gBAAgB,EAAE,SAAS,EAAE,gBAAgB,EAAE,cAAc,EAAE,MA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M,aAAa,CAAC;AAC5D,OAAO,EAAE,iBAAiB,EAAE,kBAAkB,EAAE,kBAAkB,EAAE,gBAAgB,EAAE,KAAK,YAAY,EAAE,MAAM,eAAe,CAAC;AAC/H,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,KAAK,UAAU,EAAE,KAAK,eAAe,EAAE,MAAM,cAAc,CAAC;AAE7F,OAAO,EAAE,YAAY,EAAE,QAAQ,EAAE,YAAY,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AACpF,YAAY,EAAE,SAAS,EAAE,YAAY,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC"}
package/dist/index.js CHANGED
@@ -10,4 +10,6 @@ export { isPublicAddress, parseAddress, specialClass } from "./address.js";
10
10
  export { HttpError } from "./errors.js";
11
11
  export { DEFAULT_MAX_BYTES, DEFAULT_ITEM_BYTES, DEFAULT_TIMEOUT_MS, DEFAULT_SETTINGS } from "./exchange.js";
12
12
  export { ndjsonLines, sseEvents } from "./parsers.js";
13
+ // The frame rules of a response: frame-ancestors of CSP3 and X-Frame-Options
14
+ export { frameRefusal, parseCsp, sourceAllows, sourceListAllows } from "./frame.js";
13
15
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,kGAAkG;AAClG,cAAc,aAAa,CAAC;AAC5B,cAAc,YAAY,CAAC;AAC3B,OAAO,EAAE,WAAW,EAAE,QAAQ,EAAE,UAAU,EAAE,OAAO,EAAE,MAAM,4BAA4B,CAAC;AACxF,gCAAgC;AAChC,OAAO,EAAE,gBAAgB,EAAE,mBAAmB,EAAE,gBAAgB,EAAE,SAAS,EAAE,gBAAgB,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AACnI,+FAA+F;AAC/F,OAAO,EAAE,eAAe,EAA8D,MAAM,iBAAiB,CAAC;AAE9G,OAAO,EAAE,eAAe,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAC3E,OAAO,EAAE,SAAS,EAAsB,MAAM,aAAa,CAAC;AAC5D,OAAO,EAAE,iBAAiB,EAAE,kBAAkB,EAAE,kBAAkB,EAAE,gBAAgB,EAAqB,MAAM,eAAe,CAAC;AAC/H,OAAO,EAAE,WAAW,EAAE,SAAS,EAAyC,MAAM,cAAc,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,kGAAkG;AAClG,cAAc,aAAa,CAAC;AAC5B,cAAc,YAAY,CAAC;AAC3B,OAAO,EAAE,WAAW,EAAE,QAAQ,EAAE,UAAU,EAAE,OAAO,EAAE,MAAM,4BAA4B,CAAC;AACxF,gCAAgC;AAChC,OAAO,EAAE,gBAAgB,EAAE,mBAAmB,EAAE,gBAAgB,EAAE,SAAS,EAAE,gBAAgB,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AACnI,+FAA+F;AAC/F,OAAO,EAAE,eAAe,EAA8D,MAAM,iBAAiB,CAAC;AAE9G,OAAO,EAAE,eAAe,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAC3E,OAAO,EAAE,SAAS,EAAsB,MAAM,aAAa,CAAC;AAC5D,OAAO,EAAE,iBAAiB,EAAE,kBAAkB,EAAE,kBAAkB,EAAE,gBAAgB,EAAqB,MAAM,eAAe,CAAC;AAC/H,OAAO,EAAE,WAAW,EAAE,SAAS,EAAyC,MAAM,cAAc,CAAC;AAC7F,6EAA6E;AAC7E,OAAO,EAAE,YAAY,EAAE,QAAQ,EAAE,YAAY,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "$schema": "https://json.schemastore.org/package",
3
3
  "name": "@procedures/http",
4
- "version": "0.52.3",
4
+ "version": "0.53.1",
5
5
  "description": "HTTP as procedures: request, JSON, NDJSON and server-sent events, with a host allowlist that refuses private addresses, size limits and the deadline of the call.",
6
6
  "keywords": [
7
7
  "procedures",
@@ -56,6 +56,7 @@
56
56
  "typescript": "^7.0.2",
57
57
  "@types/node": "^22.0.0",
58
58
  "@mark1russell7/cue": "^2.1.1",
59
+ "fast-check": "^4.10.2",
59
60
  "vitest": "^5.0.3"
60
61
  },
61
62
  "engines": {
@@ -69,7 +70,7 @@
69
70
  "procedures": "./dist/register.js"
70
71
  },
71
72
  "dependencies": {
72
- "@procedures/client": "0.52.3",
73
+ "@procedures/client": "0.53.1",
73
74
  "zod": "^4.6.5"
74
75
  },
75
76
  "scripts": {
package/src/frame.ts ADDED
@@ -0,0 +1,194 @@
1
+ /**
2
+ * The frame rules of a response. A page can refuse to show in a frame of another page. Two headers tell the
3
+ * refusal: `Content-Security-Policy` with the directive `frame-ancestors` (CSP3), and `X-Frame-Options` (the HTML
4
+ * standard). These functions read the headers and give the decision of a browser. They use no network.
5
+ */
6
+
7
+ /** A policy of `Content-Security-Policy`: the source list of each directive, by its name in lower case. */
8
+ export type CspPolicy = ReadonlyMap<string, readonly string[]>;
9
+
10
+ /** The headers of a response: a `Headers` object, or a record of header values as Node gives them. */
11
+ export type HeaderSource = Headers | Readonly<Record<string, string | readonly string[] | undefined>>;
12
+
13
+ /** The decision of `frameRefusal`. `by` names the header that refuses the frame, or it is `null`. */
14
+ export interface FrameRefusal {
15
+ readonly refused: boolean;
16
+ readonly by: "csp" | "x-frame-options" | null;
17
+ readonly detail: string;
18
+ }
19
+
20
+ /** The two origins of a frame: the page in the frame, and the page that holds the frame. */
21
+ export interface FrameOrigins {
22
+ /** The origin of the page that holds the frame, for example `https://a.example`. */
23
+ readonly ancestor: string | URL;
24
+ /** The address of the page in the frame, after each redirect. Its origin is `'self'`. */
25
+ readonly page: string | URL;
26
+ }
27
+
28
+ const ASCII_WHITESPACE = /[\t\n\f\r ]+/;
29
+ const DIRECTIVE_NAME = /^[a-z0-9-]+$/;
30
+ const SCHEME_SOURCE = /^([a-z][a-z0-9+.-]*):$/i;
31
+ const HOST_SOURCE = /^(?:([a-z][a-z0-9+.-]*):\/\/)?(\*|(?:\*\.)?[a-z0-9-]+(?:\.[a-z0-9-]+)*\.?)(?::(\*|[0-9]+))?(\/[^;,]*)?$/i;
32
+ const DEFAULT_PORTS: Readonly<Record<string, number>> = { http: 80, https: 443, ws: 80, wss: 443, ftp: 21 };
33
+
34
+ /**
35
+ * This function gives the policies of the values of `Content-Security-Policy` headers. A comma divides the policies
36
+ * of one value. The first copy of a directive in a policy wins, as in a browser. A policy with no directive is not
37
+ * in the result.
38
+ */
39
+ export function parseCsp(values: readonly string[]): CspPolicy[] {
40
+ const policies: CspPolicy[] = [];
41
+ for (const value of values) {
42
+ for (const serialized of value.split(",")) {
43
+ const policy = new Map<string, readonly string[]>();
44
+ for (const token of serialized.split(";")) {
45
+ const [name, ...list] = token.trim().split(ASCII_WHITESPACE);
46
+ const directive = (name ?? "").toLowerCase();
47
+ if (!DIRECTIVE_NAME.test(directive) || policy.has(directive)) continue;
48
+ policy.set(directive, list);
49
+ }
50
+ if (policy.size > 0) policies.push(policy);
51
+ }
52
+ }
53
+ return policies;
54
+ }
55
+
56
+ /** The parts of an origin that the rules compare. A port is `null` when it is the default port of the scheme. */
57
+ interface OriginParts {
58
+ readonly scheme: string;
59
+ readonly host: string | null;
60
+ readonly port: number | null;
61
+ }
62
+
63
+ function partsOf(origin: string | URL): OriginParts | undefined {
64
+ let url: URL;
65
+ try {
66
+ url = typeof origin === "string" ? new URL(origin) : origin;
67
+ } catch {
68
+ return undefined;
69
+ }
70
+ return { scheme: url.protocol.slice(0, -1).toLowerCase(), host: url.hostname === "" ? null : url.hostname.toLowerCase(), port: url.port === "" ? null : Number(url.port) };
71
+ }
72
+
73
+ /** This function is the scheme-part match of CSP3: the same scheme, or a secure upgrade of it. */
74
+ function schemeMatches(expression: string, scheme: string): boolean {
75
+ const a = expression.toLowerCase();
76
+ if (a === scheme) return true;
77
+ if (a === "http") return scheme === "https";
78
+ if (a === "ws") return scheme === "wss" || scheme === "http" || scheme === "https";
79
+ if (a === "wss") return scheme === "https";
80
+ return false;
81
+ }
82
+
83
+ /** This function is the host-part match of CSP3. `*.example.com` matches each subdomain, but not `example.com`. */
84
+ function hostMatches(pattern: string, host: string): boolean {
85
+ const lower = pattern.toLowerCase();
86
+ if (lower.startsWith("*")) return host.endsWith(lower.slice(1));
87
+ return lower === host;
88
+ }
89
+
90
+ /** This function is the port-part match of CSP3. No port in the expression matches the default port of the scheme. */
91
+ function portMatches(input: string | undefined, url: OriginParts): boolean {
92
+ if (input === "*") return true;
93
+ const port = input === undefined ? null : Number(input);
94
+ if (port === url.port) return true;
95
+ return url.port === null && port === (DEFAULT_PORTS[url.scheme] ?? null);
96
+ }
97
+
98
+ function partsAllow(source: string, url: OriginParts | undefined, self: OriginParts | undefined): boolean {
99
+ if (url === undefined) return false;
100
+ const keyword = source.toLowerCase();
101
+ if (keyword === "*") return url.scheme === "http" || url.scheme === "https" || url.scheme === self?.scheme;
102
+ if (keyword === "'self'") {
103
+ if (self === undefined || self.host === null || self.host !== url.host) return false;
104
+ if (self.scheme === url.scheme && self.port === url.port) return true;
105
+ // CSP3 lets 'self' match a secure upgrade of the scheme, with the same port or the default ports
106
+ if (self.port !== url.port) return false;
107
+ return url.scheme === "https" || url.scheme === "wss" || (self.scheme === "http" && (url.scheme === "http" || url.scheme === "ws"));
108
+ }
109
+ const scheme = SCHEME_SOURCE.exec(source);
110
+ if (scheme !== null) return schemeMatches(scheme[1] ?? "", url.scheme);
111
+ const host = HOST_SOURCE.exec(source);
112
+ if (host === null || url.host === null) return false;
113
+ const [, hostScheme, hostPart = "", port] = host;
114
+ if (hostScheme === undefined ? self === undefined || !schemeMatches(self.scheme, url.scheme) : !schemeMatches(hostScheme, url.scheme)) return false;
115
+ // An origin has no path, so the path part of the expression changes nothing
116
+ return hostMatches(hostPart, url.host) && portMatches(port, url);
117
+ }
118
+
119
+ /**
120
+ * This function tells if one source expression of CSP3 accepts an origin. `self` is the origin of the protected page.
121
+ * The function compares origins, so it ignores the path part of an expression, as `frame-ancestors` does.
122
+ * A nonce, a hash and each other keyword accept no origin.
123
+ */
124
+ export function sourceAllows(source: string, origin: string | URL, self: string | URL): boolean {
125
+ return partsAllow(source, partsOf(origin), partsOf(self));
126
+ }
127
+
128
+ /**
129
+ * This function tells if a source list of CSP3 accepts an origin. An empty list and the list `'none'` accept no origin.
130
+ * Thus `frame-ancestors` with no value refuses each frame.
131
+ */
132
+ export function sourceListAllows(list: readonly string[], origin: string | URL, self: string | URL): boolean {
133
+ if (list.length === 1 && list[0]?.toLowerCase() === "'none'") return false;
134
+ const url = partsOf(origin);
135
+ const own = partsOf(self);
136
+ return list.some((source) => partsAllow(source, url, own));
137
+ }
138
+
139
+ /** This function gives the values of a header, as a list. */
140
+ function valuesOf(headers: HeaderSource, name: string): string[] {
141
+ if (typeof (headers as Headers).get === "function" && typeof (headers as Headers).forEach === "function") {
142
+ const value = (headers as Headers).get(name);
143
+ return value === null ? [] : [value];
144
+ }
145
+ const values: string[] = [];
146
+ for (const [key, value] of Object.entries(headers as Readonly<Record<string, string | readonly string[] | undefined>>)) {
147
+ if (key.toLowerCase() !== name || value === undefined) continue;
148
+ if (typeof value === "string") values.push(value);
149
+ else values.push(...value);
150
+ }
151
+ return values;
152
+ }
153
+
154
+ const sameOrigin = (a: OriginParts | undefined, b: OriginParts | undefined): boolean =>
155
+ a !== undefined && b !== undefined && a.host !== null && a.scheme === b.scheme && a.host === b.host && a.port === b.port;
156
+
157
+ const serialize = (origin: string | URL): string => (typeof origin === "string" ? origin : origin.origin);
158
+
159
+ /**
160
+ * This function tells if a browser refuses to show a page in a frame of the ancestor. It obeys these rules:
161
+ *
162
+ * - An enforced policy with `frame-ancestors` decides, and the function ignores `X-Frame-Options`. Each such policy must accept the ancestor.
163
+ * - Else `X-Frame-Options` decides, by the rules of the HTML standard. `DENY` refuses, and `SAMEORIGIN` refuses an ancestor of another origin.
164
+ * - Two or more different values, with `DENY`, `SAMEORIGIN` or `ALLOWALL` in them, refuse each frame. `ALLOW-FROM` and unknown values do not refuse.
165
+ * - A header `Content-Security-Policy-Report-Only` does not refuse.
166
+ *
167
+ * A browser examines each ancestor of a nested frame. To do the same, give each ancestor to this function.
168
+ */
169
+ export function frameRefusal(headers: HeaderSource, { ancestor, page }: FrameOrigins): FrameRefusal {
170
+ const policies = parseCsp(valuesOf(headers, "content-security-policy")).filter((policy) => policy.has("frame-ancestors"));
171
+ if (policies.length > 0) {
172
+ const index = policies.findIndex((policy) => !sourceListAllows(policy.get("frame-ancestors") ?? [], ancestor, page));
173
+ if (index === -1) return { refused: false, by: null, detail: `Each frame-ancestors directive allows ${serialize(ancestor)}.` };
174
+ const list = (policies[index]?.get("frame-ancestors") ?? []).join(" ") || "(empty)";
175
+ return { refused: true, by: "csp", detail: `The frame-ancestors directive "${list}" does not allow ${serialize(ancestor)}.` };
176
+ }
177
+ const options = new Set(
178
+ valuesOf(headers, "x-frame-options")
179
+ .flatMap((value) => value.split(","))
180
+ .map((value) => value.trim().toLowerCase()),
181
+ );
182
+ const values = [...options].join(", ");
183
+ if (options.size > 1) {
184
+ if (options.has("deny") || options.has("sameorigin") || options.has("allowall")) {
185
+ return { refused: true, by: "x-frame-options", detail: `X-Frame-Options has values that do not agree: ${values}.` };
186
+ }
187
+ return { refused: false, by: null, detail: `X-Frame-Options has no value that a browser uses: ${values}.` };
188
+ }
189
+ if (options.has("deny")) return { refused: true, by: "x-frame-options", detail: "X-Frame-Options is DENY." };
190
+ if (options.has("sameorigin") && !sameOrigin(partsOf(ancestor), partsOf(page))) {
191
+ return { refused: true, by: "x-frame-options", detail: `X-Frame-Options is SAMEORIGIN, and ${serialize(ancestor)} is another origin.` };
192
+ }
193
+ return { refused: false, by: null, detail: options.size === 0 ? "No header refuses a frame." : `X-Frame-Options allows ${serialize(ancestor)}.` };
194
+ }