@jini-ai/http-kit 0.7.1 → 0.7.3-next.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.
Files changed (70) hide show
  1. package/CHANGELOG.md +2 -2
  2. package/README.md +6 -6
  3. package/dist/adapter.d.ts +7 -50
  4. package/dist/adapter.js +12 -54
  5. package/dist/api-security-middleware.d.ts +0 -16
  6. package/dist/api-security-middleware.js +2 -18
  7. package/dist/index.d.ts +10 -15
  8. package/dist/index.js +7 -10
  9. package/dist/internal-error.d.ts +42 -0
  10. package/dist/internal-error.js +48 -0
  11. package/dist/local-daemon-request.d.ts +11 -4
  12. package/dist/local-daemon-request.js +1 -28
  13. package/dist/middleware.d.ts +1 -0
  14. package/dist/middleware.js +1 -0
  15. package/dist/node-bridge.d.ts +108 -0
  16. package/dist/node-bridge.js +356 -0
  17. package/dist/observability.d.ts +3 -14
  18. package/dist/origin.d.ts +13 -30
  19. package/dist/origin.js +13 -13
  20. package/dist/range.d.ts +53 -0
  21. package/dist/range.js +93 -0
  22. package/dist/rate-limit.d.ts +1 -13
  23. package/dist/rate-limit.js +5 -2
  24. package/dist/request.d.ts +2 -13
  25. package/dist/request.js +0 -13
  26. package/dist/response.d.ts +8 -7
  27. package/dist/response.js +0 -36
  28. package/dist/route-registration-guard.d.ts +14 -0
  29. package/dist/route-registration-guard.js +17 -0
  30. package/dist/run-credentials.d.ts +73 -0
  31. package/dist/run-credentials.js +71 -0
  32. package/dist/settings/contracts.d.ts +49 -0
  33. package/dist/settings/contracts.js +2 -0
  34. package/dist/settings/http-request.d.ts +36 -0
  35. package/dist/settings/http-request.js +50 -0
  36. package/dist/settings/http.d.ts +10 -0
  37. package/dist/settings/http.js +216 -0
  38. package/dist/settings/index.d.ts +4 -0
  39. package/dist/settings/index.js +3 -0
  40. package/dist/settings/shared.d.ts +16 -0
  41. package/dist/settings/shared.js +16 -0
  42. package/dist/settings/stream.d.ts +25 -0
  43. package/dist/settings/stream.js +180 -0
  44. package/dist/sse.d.ts +1 -15
  45. package/dist/sse.js +77 -72
  46. package/dist/trust-proxy.d.ts +36 -0
  47. package/dist/trust-proxy.js +45 -0
  48. package/dist/types.d.ts +4 -52
  49. package/dist/types.js +1 -4
  50. package/package.json +24 -14
  51. package/dist/raw-sse.d.ts +0 -49
  52. package/dist/raw-sse.js +0 -92
  53. package/dist/verified-origin/configured-origin.d.ts +0 -37
  54. package/dist/verified-origin/configured-origin.js +0 -99
  55. package/dist/verified-origin/index.d.ts +0 -7
  56. package/dist/verified-origin/index.js +0 -6
  57. package/dist/verified-origin/loopback-host.d.ts +0 -9
  58. package/dist/verified-origin/loopback-host.js +0 -17
  59. package/dist/verified-origin/origin.d.ts +0 -64
  60. package/dist/verified-origin/origin.js +0 -162
  61. package/dist/verified-origin/ports.d.ts +0 -38
  62. package/dist/verified-origin/ports.js +0 -2
  63. package/dist/verified-origin/repo.memory.d.ts +0 -32
  64. package/dist/verified-origin/repo.memory.js +0 -34
  65. package/dist/verified-origin/site-path.d.ts +0 -113
  66. package/dist/verified-origin/site-path.js +0 -188
  67. package/dist/verified-origin/types.d.ts +0 -33
  68. package/dist/verified-origin/types.js +0 -30
  69. package/dist/verified-origin.d.ts +0 -2
  70. package/dist/verified-origin.js +0 -2
@@ -1,188 +0,0 @@
1
- /**
2
- * @file Shared site-path checks: decoded, dot-resolved segments and origin tracking.
3
- *
4
- * Why the rule is not `path.startsWith("/admin")`:
5
- * - `/administer-survey` is ordinary site content and must keep working, so the comparison is per
6
- * SEGMENT, not per prefix.
7
- * - `/%61dmin` is compared AFTER one percent-decode, not on the raw bytes. Express (which routes on
8
- * the raw path) and browsers (which do not decode `%61` in a path) do NOT treat it as `/admin`; a
9
- * proxy or server that decodes before routing does. Refusing it is deliberate over-refusal — the
10
- * safe direction for a rule whose job is to keep public redirects and fetches off the admin
11
- * surface.
12
- * - `/ADMIN` reaches the same Express mount (Express matches paths case-insensitively by default),
13
- * so the comparison is case-folded.
14
- * - `/blog/../admin` is `/admin` by the time a browser issues the request, so dot segments are
15
- * resolved first.
16
- *
17
- */
18
- /**
19
- * Two hosts this deployment can never actually reach, used only so `new URL()` has a base to
20
- * resolve a site-relative reference against. `.invalid` is reserved by RFC 2606 precisely for this.
21
- *
22
- * There are TWO because one base is NAMEABLE (t85 independent review, 2026-09-16). With a single
23
- * base, `//reserved-path-probe.invalid/x` resolves to that very origin, so "did the origin survive"
24
- * answered yes for a reference that leaves the site — reachable as `/store/buy`'s `returnTo`, which
25
- * sent `Location: //reserved-path-probe.invalid/x`. Requiring the resolved origin to TRACK whichever
26
- * base it was given leaves no origin an input can name, which is a structural answer rather than a
27
- * denylist of the sentinel's spellings (the same reasoning as the probe itself — see
28
- * {@link checkSiteRelativeTarget}). Both are bare origins, so a reference resolves to the same
29
- * pathname against either.
30
- */
31
- // eslint-disable-next-line sonarjs/no-clear-text-protocols -- Reserved .invalid probe origin used only for URL parsing; no network transport is invoked.
32
- const PROBE_ORIGIN = "http://reserved-path-probe.invalid";
33
- // eslint-disable-next-line sonarjs/no-clear-text-protocols -- Reserved .invalid probe origin used only for URL parsing; no network transport is invoked.
34
- const SECOND_PROBE_ORIGIN = "http://reserved-path-probe-2.invalid";
35
- /**
36
- * Whether `value` contains a C0 or C1 control character.
37
- *
38
- * CR/LF in a request target is HTTP request splitting and in a `Location` header is response
39
- * splitting; NUL and friends are path-truncation tricks.
40
- *
41
- * @param required.value - Any string, already decoded or not.
42
- * @returns `true` if any code unit is in `U+0000`–`U+001F` or `U+007F`–`U+009F`.
43
- * @complexity O(n) in the string length.
44
- * @example hasControlCharacter({ value: "/ok" }, {}); // => false
45
- */
46
- export function hasControlCharacter({ value }, _optional = {}) {
47
- for (let index = 0; index < value.length; index += 1) {
48
- const code = value.charCodeAt(index);
49
- if (code <= 0x1f || (code >= 0x7f && code <= 0x9f))
50
- return true;
51
- }
52
- return false;
53
- }
54
- /**
55
- * Resolve `.` and `..` segments, RFC 3986 §5.2.4 style, on an already-decoded path.
56
- *
57
- * Done by hand rather than with a second `new URL()` pass because a decoded path can contain a
58
- * literal `?` or `#` (from `%3F`/`%23`), and the URL parser would treat those as a query/fragment
59
- * delimiter and TRUNCATE the path — turning `/x#/../admin` into `/x` and losing the very segment
60
- * this check exists to find. A browser does no such thing: it never decodes `%23` before splitting
61
- * the path, so the truncation would be a false negative, not a match for real behavior.
62
- *
63
- * Empty segments are dropped, so `//admin` collapses to `/admin`. That is deliberately stricter
64
- * than a URL parser (which preserves `//`); over-matching here only ever refuses more.
65
- *
66
- * @param path - A decoded path, expected to start with `/`.
67
- * @returns The path with dot segments removed, always starting with `/`.
68
- * @complexity O(n) in the path length.
69
- * @example resolveDotSegments("/blog/../admin"); // => "/admin"
70
- */
71
- function resolveDotSegments(path) {
72
- const out = [];
73
- for (const segment of path.split("/")) {
74
- if (segment === "" || segment === ".")
75
- continue;
76
- if (segment === "..")
77
- out.pop();
78
- else
79
- out.push(segment);
80
- }
81
- return `/${out.join("/")}`;
82
- }
83
- /**
84
- * The actual per-segment rule, on a pathname string that is guaranteed to have come from a real
85
- * `URL` (see {@link checkSitePathname}, this function's only caller). Kept private: a bare
86
- * string carries no proof it was ever parsed — the query/fragment stripping, dot-segment removal
87
- * relative to a real origin, etc. are exactly what `new URL()` already did before that caller
88
- * reaches this. Do not export this directly.
89
- *
90
- * Check the undecoded first segment too: Express mounts `/admin/..%2fpublic` under `/admin`,
91
- * even though decoding and resolving dot segments produces `/public`.
92
- * Order is load-bearing for the decoded check: decode ONCE, then resolve dot segments, then compare
93
- * the first segment case-folded. Decoding first is what catches `/%61dmin` (over-refusal for Express
94
- * and browsers, a real match for a decoding proxy — see the module doc); resolving dot segments after the decode is
95
- * what catches `/blog/%2e%2e/admin`. Exactly one decode pass models one decoding intermediary:
96
- * `/%2561dmin` would need two, so it stays allowed.
97
- *
98
- * @param required.pathname - An already-parsed `URL`'s `.pathname` (no query, no fragment).
99
- * @param required.reservedSegments - First segments reserved by the host, stored case-folded.
100
- * @returns `{ kind: "ok" }` when it is ordinary site content; otherwise the arm naming the refusal.
101
- * @complexity O(n) in the pathname length.
102
- */
103
- function checkDecodedSitePathname({ pathname, reservedSegments }, _optional = {}) {
104
- let decoded;
105
- try {
106
- decoded = decodeURIComponent(pathname);
107
- }
108
- catch {
109
- return { kind: "malformed-encoding" };
110
- }
111
- if (decoded.includes("\\") || hasControlCharacter({ value: decoded }, {}))
112
- return { kind: "disallowed-character" };
113
- const rawFirst = pathname.slice(1).split("/", 1).join("").toLowerCase();
114
- if (reservedSegments.has(rawFirst))
115
- return { kind: "reserved", surface: rawFirst };
116
- // `resolveDotSegments` always returns a path with a leading "/", so the first segment is
117
- // everything after it up to the next "/" — `split(..., 1)` yields exactly that one element.
118
- const first = resolveDotSegments(decoded).slice(1).split("/", 1).join("").toLowerCase();
119
- if (reservedSegments.has(first))
120
- return { kind: "reserved", surface: first };
121
- return { kind: "ok" };
122
- }
123
- /**
124
- * Classify an already-parsed URL's pathname against the admin application's URL space.
125
- *
126
- * Takes the {@link URL} itself, not a bare pathname string, so this cannot be handed a raw,
127
- * unparsed reference by accident: a string has no way to prove it went through `new URL()` first,
128
- * and this rule's whole design (see the module doc) depends on comparing the DECODED, dot-resolved
129
- * form a URL parser would actually produce. A caller that only has a raw string — a stored redirect
130
- * target, anything from user input — must go through {@link checkSiteRelativeTarget} instead, which
131
- * parses it (and rejects what doesn't parse) before this check ever runs.
132
- *
133
- * PRECONDITION: the caller has already established that `url` is on the site's own origin. This
134
- * function reads only `pathname`, so `new URL("//admin", base)` (host `admin`, pathname `/`)
135
- * returns `ok`. It deliberately does not compare origins itself: `platform/routing` cannot use
136
- * `features/origin`'s normalizer, and a string `URL.origin` comparison is wrong for redirect
137
- * candidates (`https://site./admin`). Callers: `checkSiteRelativeTarget` (probe-origin check),
138
- * `site-inspection`'s `resolveWithinOrigin`, and `features/redirects/reserved-destination.ts` (the
139
- * origin oracle's `isSameOrigin`).
140
- *
141
- * @param required.url - An already-parsed `URL`; only `url.pathname` is read.
142
- * @param required.reservedSegments - First segments reserved by the host, stored case-folded.
143
- * @returns `{ kind: "ok" }` when it is ordinary site content; otherwise the arm naming the refusal.
144
- * @complexity O(n) in the pathname length.
145
- * @example checkSitePathname({ url: new URL("/administer-survey", "http://x.invalid"), reservedSegments: new Set(["admin", "api"]) }, {}); // => { kind: "ok" }
146
- * @example checkSitePathname({ url: new URL("/%61dmin/settings", "http://x.invalid"), reservedSegments: new Set(["admin", "api"]) }, {}); // => { kind: "reserved", surface: "admin" }
147
- */
148
- export function checkSitePathname({ url, reservedSegments }, _optional = {}) {
149
- return checkDecodedSitePathname({ pathname: url.pathname, reservedSegments }, {});
150
- }
151
- /**
152
- * Classify a raw, site-relative reference (a stored redirect target, a caller-supplied path)
153
- * against the admin application's URL space, including the structural check that it is site-
154
- * relative at all.
155
- *
156
- * The structural pass runs FIRST and matters on its own: `/\evil.example` begins with exactly one
157
- * slash, so a `startsWith("//")` or scheme test calls it site-relative, while `new URL()` — and
158
- * every browser following a `Location` — reads the backslash as a separator and lands on
159
- * `evil.example`. Resolving against a probe origin and asserting the origin TRACKED that base is a
160
- * structural answer to that instead of a pattern someone has to keep correct — done against two
161
- * different bases so the probe origin itself is not a nameable way to pass (see
162
- * {@link PROBE_ORIGIN}).
163
- *
164
- * @param required.raw - The untrusted reference, expected to be site-relative.
165
- * @param required.reservedSegments - First segments reserved by the host, stored case-folded.
166
- * @returns `{ kind: "ok" }` when it stays on this site and names ordinary content; otherwise the
167
- * arm naming the refusal.
168
- * @complexity O(n) in the reference length.
169
- * @example checkSiteRelativeTarget({ raw: "/new", reservedSegments: new Set(["admin", "api"]) }, {}); // => { kind: "ok" }
170
- * @example checkSiteRelativeTarget({ raw: "/\\evil.example", reservedSegments: new Set(["admin", "api"]) }, {}); // => { kind: "off-origin" }
171
- */
172
- export function checkSiteRelativeTarget({ raw, reservedSegments }, _optional = {}) {
173
- if (hasControlCharacter({ value: raw }, {}))
174
- return { kind: "disallowed-character" };
175
- let parsed;
176
- let reparsed;
177
- try {
178
- parsed = new URL(raw, PROBE_ORIGIN);
179
- reparsed = new URL(raw, SECOND_PROBE_ORIGIN);
180
- }
181
- catch {
182
- return { kind: "unparseable" };
183
- }
184
- if (parsed.origin !== PROBE_ORIGIN || reparsed.origin !== SECOND_PROBE_ORIGIN)
185
- return { kind: "off-origin" };
186
- return checkSitePathname({ url: parsed, reservedSegments }, {});
187
- }
188
- //# sourceMappingURL=site-path.js.map
@@ -1,33 +0,0 @@
1
- /** Canonical links, redirect checks and egress decisions must use stored trust evidence,
2
- * never the raw request Host/:authority, which is attacker-controlled. */
3
- export type OriginScheme = "https" | "http";
4
- export type OriginSource = "workspace-setting" | "dev-capability";
5
- /** Stored trust evidence. Public origins use HTTPS; HTTP is limited to explicit development capabilities. */
6
- export interface VerifiedOrigin {
7
- scheme: OriginScheme;
8
- host: string;
9
- port?: number;
10
- basePath?: string;
11
- verifiedAt: string;
12
- source: OriginSource;
13
- }
14
- export declare class InsecureOriginSourceError extends Error {
15
- constructor({ message }: {
16
- message: string;
17
- });
18
- }
19
- /** Missing trust evidence is a hard precondition failure. Callers must fail closed rather than
20
- * guessing an origin from the request or another unverified value. */
21
- export declare class OriginNotVerifiedError extends Error {
22
- constructor({ message }: {
23
- message: string;
24
- });
25
- }
26
- /** Copy evidence, refusing HTTP public origins and treating public loopback origins as unset.
27
- * Both writers and registry reads use this boundary, including reads of legacy persisted rows.
28
- * @throws InsecureOriginSourceError for HTTP public evidence; OriginNotVerifiedError for loopback public evidence.
29
- * @complexity O(host bytes) time and space; pure, with no ownership or reachability probe.
30
- * @example createVerifiedOrigin({ scheme: "https", host: "example.com", verifiedAt, source: "workspace-setting" });
31
- */
32
- export declare function createVerifiedOrigin(input: VerifiedOrigin, _options?: Record<string, never>): VerifiedOrigin;
33
- //# sourceMappingURL=types.d.ts.map
@@ -1,30 +0,0 @@
1
- import { isLoopbackHost } from "./loopback-host.js";
2
- export class InsecureOriginSourceError extends Error {
3
- constructor({ message }) { super(message); }
4
- }
5
- /** Missing trust evidence is a hard precondition failure. Callers must fail closed rather than
6
- * guessing an origin from the request or another unverified value. */
7
- export class OriginNotVerifiedError extends Error {
8
- constructor({ message }) { super(message); }
9
- }
10
- /** Copy evidence, refusing HTTP public origins and treating public loopback origins as unset.
11
- * Both writers and registry reads use this boundary, including reads of legacy persisted rows.
12
- * @throws InsecureOriginSourceError for HTTP public evidence; OriginNotVerifiedError for loopback public evidence.
13
- * @complexity O(host bytes) time and space; pure, with no ownership or reachability probe.
14
- * @example createVerifiedOrigin({ scheme: "https", host: "example.com", verifiedAt, source: "workspace-setting" });
15
- */
16
- export function createVerifiedOrigin(input, _options = {}) {
17
- // Repositories and other adapters must construct evidence through this function; an object
18
- // literal can bypass the scheme/source invariant even though it satisfies the structural type.
19
- if (input.scheme === "http" && input.source !== "dev-capability") {
20
- throw new InsecureOriginSourceError({
21
- message: `origin scheme 'http' is only legal for source 'dev-capability' (got source '${input.source}')`
22
- });
23
- }
24
- // Explicit dev capabilities may address a listener; a public site URL never may.
25
- if (input.source === "workspace-setting" && isLoopbackHost({ host: input.host })) {
26
- throw new OriginNotVerifiedError({ message: "a loopback host is not a verified public origin" });
27
- }
28
- return { ...input };
29
- }
30
- //# sourceMappingURL=types.js.map
@@ -1,2 +0,0 @@
1
- export * from "./verified-origin/index.js";
2
- //# sourceMappingURL=verified-origin.d.ts.map
@@ -1,2 +0,0 @@
1
- export * from "./verified-origin/index.js";
2
- //# sourceMappingURL=verified-origin.js.map