@ocap/util 1.31.8 → 1.32.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/esm/client-ip.d.mts +55 -0
- package/esm/client-ip.mjs +117 -0
- package/esm/deposit-v2-bridge-fields.mjs +1 -0
- package/esm/sql-timing.d.mts +47 -0
- package/esm/sql-timing.mjs +58 -0
- package/lib/client-ip.cjs +123 -0
- package/lib/client-ip.d.cts +55 -0
- package/lib/deposit-v2-bridge-fields.cjs +1 -0
- package/lib/sql-timing.cjs +62 -0
- package/lib/sql-timing.d.cts +47 -0
- package/package.json +2 -2
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
//#region src/client-ip.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Client IP of an HTTP request for logs and per-client rate limits (#1922).
|
|
4
|
+
*
|
|
5
|
+
* Order: the first trusted proxy header that holds a well-formed IP, then `req.ip` (express; equals the socket address
|
|
6
|
+
* unless the app set `trust proxy`), then the socket address.
|
|
7
|
+
*
|
|
8
|
+
* Trusted headers default to `x-real-ip` only: the Blocklet Server nginx sets it from `$remote_addr` but passes a
|
|
9
|
+
* client-supplied `cf-connecting-ip` through, so trusting `cf-connecting-ip` there would let a client pick its own
|
|
10
|
+
* address. A Cloudflare Tunnel deployment opts in with `CHAIN_CLIENT_IP_HEADERS=cf-connecting-ip`.
|
|
11
|
+
*
|
|
12
|
+
* Proxy headers are trusted only when the immediate peer is a loopback address. A chain process listens on
|
|
13
|
+
* 127.0.0.1 and is reached through the cloudflared daemon on the same host (#1875 decision E4), so a loopback
|
|
14
|
+
* peer is that local proxy; Cloudflare's edge sets `cf-connecting-ip` to the real client address and rejects a
|
|
15
|
+
* request that already carries one. A non-loopback peer talks to the process directly (for example a
|
|
16
|
+
* node bound to 0.0.0.0), so any header it sends is its own claim and is ignored: the socket address is used.
|
|
17
|
+
*
|
|
18
|
+
* A socket whose address is missing (Node clears `remoteAddress` once the client resets the connection) is an
|
|
19
|
+
* unknown peer: headers are ignored. Only a request object with no socket at all (fetch runtimes, synthetic
|
|
20
|
+
* contexts) uses an adapter-provided `ip`, then the headers.
|
|
21
|
+
*
|
|
22
|
+
* Any process on the same host can connect over loopback and so name any client IP; that is accepted by #1875 E4.
|
|
23
|
+
* `CHAIN_CLIENT_IP_HEADERS=none` (or empty) trusts no header at all.
|
|
24
|
+
*
|
|
25
|
+
* No node built-ins: this module is also bundled for browsers and Workers.
|
|
26
|
+
*/
|
|
27
|
+
declare const CLIENT_IP_HEADERS_ENV = "CHAIN_CLIENT_IP_HEADERS";
|
|
28
|
+
declare const DEFAULT_CLIENT_IP_HEADERS: readonly string[];
|
|
29
|
+
type ClientIpHeaders = Record<string, string | string[] | undefined>;
|
|
30
|
+
interface ClientIpSource {
|
|
31
|
+
headers?: ClientIpHeaders;
|
|
32
|
+
/** express `req.ip` */
|
|
33
|
+
ip?: string;
|
|
34
|
+
socket?: {
|
|
35
|
+
remoteAddress?: string;
|
|
36
|
+
} | null;
|
|
37
|
+
connection?: {
|
|
38
|
+
remoteAddress?: string;
|
|
39
|
+
} | null;
|
|
40
|
+
_remoteAddress?: string;
|
|
41
|
+
}
|
|
42
|
+
interface ResolveClientIpOptions {
|
|
43
|
+
/** Trusted header names, in order. Defaults to `getTrustedClientIpHeaders()`. */
|
|
44
|
+
headers?: readonly string[];
|
|
45
|
+
}
|
|
46
|
+
/** Trusted proxy header names from `CHAIN_CLIENT_IP_HEADERS` (comma-separated); unset → the default list. */
|
|
47
|
+
declare function getTrustedClientIpHeaders(raw?: string | undefined): string[];
|
|
48
|
+
/** True for 127.0.0.0/8, ::1 and IPv4-mapped 127.0.0.0/8. */
|
|
49
|
+
declare function isLoopbackAddress(addr: string | undefined | null): boolean;
|
|
50
|
+
/** Address of the immediate TCP peer, if the request object exposes one. */
|
|
51
|
+
declare function getPeerAddress(src: ClientIpSource | undefined | null): string | undefined;
|
|
52
|
+
/** See the module comment for the trust rule. Returns `''` when nothing is known. */
|
|
53
|
+
declare function resolveClientIp(src: ClientIpSource | undefined | null, options?: ResolveClientIpOptions): string;
|
|
54
|
+
//#endregion
|
|
55
|
+
export { CLIENT_IP_HEADERS_ENV, ClientIpHeaders, ClientIpSource, DEFAULT_CLIENT_IP_HEADERS, ResolveClientIpOptions, getPeerAddress, getTrustedClientIpHeaders, isLoopbackAddress, resolveClientIp };
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
//#region src/client-ip.ts
|
|
2
|
+
/**
|
|
3
|
+
* Client IP of an HTTP request for logs and per-client rate limits (#1922).
|
|
4
|
+
*
|
|
5
|
+
* Order: the first trusted proxy header that holds a well-formed IP, then `req.ip` (express; equals the socket address
|
|
6
|
+
* unless the app set `trust proxy`), then the socket address.
|
|
7
|
+
*
|
|
8
|
+
* Trusted headers default to `x-real-ip` only: the Blocklet Server nginx sets it from `$remote_addr` but passes a
|
|
9
|
+
* client-supplied `cf-connecting-ip` through, so trusting `cf-connecting-ip` there would let a client pick its own
|
|
10
|
+
* address. A Cloudflare Tunnel deployment opts in with `CHAIN_CLIENT_IP_HEADERS=cf-connecting-ip`.
|
|
11
|
+
*
|
|
12
|
+
* Proxy headers are trusted only when the immediate peer is a loopback address. A chain process listens on
|
|
13
|
+
* 127.0.0.1 and is reached through the cloudflared daemon on the same host (#1875 decision E4), so a loopback
|
|
14
|
+
* peer is that local proxy; Cloudflare's edge sets `cf-connecting-ip` to the real client address and rejects a
|
|
15
|
+
* request that already carries one. A non-loopback peer talks to the process directly (for example a
|
|
16
|
+
* node bound to 0.0.0.0), so any header it sends is its own claim and is ignored: the socket address is used.
|
|
17
|
+
*
|
|
18
|
+
* A socket whose address is missing (Node clears `remoteAddress` once the client resets the connection) is an
|
|
19
|
+
* unknown peer: headers are ignored. Only a request object with no socket at all (fetch runtimes, synthetic
|
|
20
|
+
* contexts) uses an adapter-provided `ip`, then the headers.
|
|
21
|
+
*
|
|
22
|
+
* Any process on the same host can connect over loopback and so name any client IP; that is accepted by #1875 E4.
|
|
23
|
+
* `CHAIN_CLIENT_IP_HEADERS=none` (or empty) trusts no header at all.
|
|
24
|
+
*
|
|
25
|
+
* No node built-ins: this module is also bundled for browsers and Workers.
|
|
26
|
+
*/
|
|
27
|
+
const CLIENT_IP_HEADERS_ENV = "CHAIN_CLIENT_IP_HEADERS";
|
|
28
|
+
const DEFAULT_CLIENT_IP_HEADERS = Object.freeze(["x-real-ip"]);
|
|
29
|
+
const readEnv = () => {
|
|
30
|
+
return globalThis.process?.env?.[CLIENT_IP_HEADERS_ENV];
|
|
31
|
+
};
|
|
32
|
+
/** Trusted proxy header names from `CHAIN_CLIENT_IP_HEADERS` (comma-separated); unset → the default list. */
|
|
33
|
+
function getTrustedClientIpHeaders(raw = readEnv()) {
|
|
34
|
+
if (raw === void 0) return [...DEFAULT_CLIENT_IP_HEADERS];
|
|
35
|
+
const list = raw.split(",").map((s) => s.trim().toLowerCase()).filter(Boolean);
|
|
36
|
+
return list.length === 1 && list[0] === "none" ? [] : list;
|
|
37
|
+
}
|
|
38
|
+
const stripBrackets = (addr) => addr.trim().replace(/^\[|\]$/g, "");
|
|
39
|
+
const isIpv4 = (s) => {
|
|
40
|
+
const parts = s.split(".");
|
|
41
|
+
return parts.length === 4 && parts.every((p) => /^\d{1,3}$/.test(p) && Number(p) <= 255);
|
|
42
|
+
};
|
|
43
|
+
/** Expands an IPv6 literal into 8 numeric groups, or null when it is not one (embedded IPv4 tails excluded). */
|
|
44
|
+
const ipv6Groups = (s) => {
|
|
45
|
+
if (!/^[0-9a-f:]+$/i.test(s)) return null;
|
|
46
|
+
const halves = s.split("::");
|
|
47
|
+
if (halves.length > 2) return null;
|
|
48
|
+
const head = halves[0] ? halves[0].split(":") : [];
|
|
49
|
+
const tail = halves.length === 2 && halves[1] ? halves[1].split(":") : [];
|
|
50
|
+
const missing = 8 - head.length - tail.length;
|
|
51
|
+
if (halves.length === 1 ? missing !== 0 : missing < 1) return null;
|
|
52
|
+
const groups = [
|
|
53
|
+
...head,
|
|
54
|
+
...Array(halves.length === 2 ? missing : 0).fill("0"),
|
|
55
|
+
...tail
|
|
56
|
+
];
|
|
57
|
+
if (!groups.every((g) => /^[0-9a-f]{1,4}$/i.test(g))) return null;
|
|
58
|
+
return groups.map((g) => Number.parseInt(g, 16));
|
|
59
|
+
};
|
|
60
|
+
/** True for 127.0.0.0/8, ::1 and IPv4-mapped 127.0.0.0/8. */
|
|
61
|
+
function isLoopbackAddress(addr) {
|
|
62
|
+
if (!addr) return false;
|
|
63
|
+
let s = stripBrackets(addr).toLowerCase();
|
|
64
|
+
const mapped = /^::ffff:(\d{1,3}(?:\.\d{1,3}){3})$/.exec(s);
|
|
65
|
+
if (mapped) s = mapped[1];
|
|
66
|
+
if (isIpv4(s)) return s.split(".")[0] === "127";
|
|
67
|
+
const groups = ipv6Groups(s);
|
|
68
|
+
if (groups === null) return false;
|
|
69
|
+
return groups.slice(0, 7).every((g) => g === 0) && groups[7] === 1;
|
|
70
|
+
}
|
|
71
|
+
/** Only bare IP literals: no spaces, commas, newlines or hostnames can reach a log line or a rate-limit key. */
|
|
72
|
+
const isIpLiteral = (s) => {
|
|
73
|
+
if (s.length < 2 || s.length > 45) return false;
|
|
74
|
+
if (isIpv4(s)) return true;
|
|
75
|
+
if (!s.includes(":")) return false;
|
|
76
|
+
const mapped = /^(.*:)(\d{1,3}(?:\.\d{1,3}){3})$/.exec(s);
|
|
77
|
+
if (mapped) return isIpv4(mapped[2]) && /^[0-9a-f:]+$/i.test(mapped[1]);
|
|
78
|
+
return ipv6Groups(s) !== null;
|
|
79
|
+
};
|
|
80
|
+
const readHeader = (headers, name) => {
|
|
81
|
+
if (!headers) return void 0;
|
|
82
|
+
let value = headers[name];
|
|
83
|
+
if (value === void 0) {
|
|
84
|
+
for (const [k, v] of Object.entries(headers)) if (k.toLowerCase() === name) {
|
|
85
|
+
value = v;
|
|
86
|
+
break;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
const first = Array.isArray(value) ? value[0] : value;
|
|
90
|
+
return typeof first === "string" ? first.trim() : void 0;
|
|
91
|
+
};
|
|
92
|
+
/** Address of the immediate TCP peer, if the request object exposes one. */
|
|
93
|
+
function getPeerAddress(src) {
|
|
94
|
+
return src?.socket?.remoteAddress || src?.connection?.remoteAddress || src?._remoteAddress || void 0;
|
|
95
|
+
}
|
|
96
|
+
/** See the module comment for the trust rule. Returns `''` when nothing is known. */
|
|
97
|
+
function resolveClientIp(src, options = {}) {
|
|
98
|
+
if (!src) return "";
|
|
99
|
+
const trusted = options.headers ?? getTrustedClientIpHeaders();
|
|
100
|
+
const fromHeaders = () => {
|
|
101
|
+
for (const name of trusted) {
|
|
102
|
+
const value = readHeader(src.headers, name.toLowerCase());
|
|
103
|
+
if (value && isIpLiteral(value)) return value;
|
|
104
|
+
}
|
|
105
|
+
return "";
|
|
106
|
+
};
|
|
107
|
+
if (!(src.socket != null || src.connection != null || src._remoteAddress !== void 0)) return src.ip || fromHeaders();
|
|
108
|
+
const peer = getPeerAddress(src);
|
|
109
|
+
if (peer !== void 0 && isLoopbackAddress(peer)) {
|
|
110
|
+
const ip = fromHeaders();
|
|
111
|
+
if (ip) return ip;
|
|
112
|
+
}
|
|
113
|
+
return src.ip || peer || "";
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
//#endregion
|
|
117
|
+
export { CLIENT_IP_HEADERS_ENV, DEFAULT_CLIENT_IP_HEADERS, getPeerAddress, getTrustedClientIpHeaders, isLoopbackAddress, resolveClientIp };
|
|
@@ -71,6 +71,7 @@ function walk(node, anyDepth) {
|
|
|
71
71
|
depth += 1;
|
|
72
72
|
if (depth > MAX_ANY_DEPTH) return;
|
|
73
73
|
if (isDepositTokenV2Type(anyType) && isPlainObject(node.value)) stripOwnDefaults(node.value);
|
|
74
|
+
else if ((anyType === "ItxStub" || anyType === "fg:x:itx_stub" || anyType === "fg:t:itx_stub") && isPlainObject(node.value) && isPlainObject(node.value.depositToken)) stripOwnDefaults(node.value.depositToken);
|
|
74
75
|
}
|
|
75
76
|
for (const child of Object.values(node)) walk(child, depth);
|
|
76
77
|
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
//#region src/sql-timing.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* SQLite execution time, attributed to whoever asked for it (#1891).
|
|
4
|
+
*
|
|
5
|
+
* `@ocap/gql` (while its slow-request log is on) runs every root field call inside an `AsyncLocalStorage` whose store
|
|
6
|
+
* is an `ISqlTimingSink`, and publishes that storage under `globalThis[SQL_TIMING_KEY]`. The SQLite backends
|
|
7
|
+
* (`@ocap/indexdb-sqlite`, `@ocap/statedb-sqlite`) pass every Kysely connection they open through
|
|
8
|
+
* `instrumentSqlTiming`, which adds the synchronous duration of each `executeQuery` call to the sink current at that
|
|
9
|
+
* moment. better-sqlite3 and bun:sqlite run the whole statement (prepare, every step, row objects) inside that call,
|
|
10
|
+
* so the sum is the field's SQLite time no matter how many `await`s come before the query: Kysely itself awaits its
|
|
11
|
+
* connection mutex before every statement, so the query never runs in the resolver's first synchronous segment.
|
|
12
|
+
*
|
|
13
|
+
* The key is a registered symbol, so separately bundled copies of these packages still meet. Without a published
|
|
14
|
+
* storage (logging off, or no `AsyncLocalStorage` on the runtime) the per-statement cost is one global property read.
|
|
15
|
+
*/
|
|
16
|
+
declare const SQL_TIMING_KEY: unique symbol;
|
|
17
|
+
/**
|
|
18
|
+
* `globalThis[SQL_TIMING_BACKEND_KEY] === true` once any connection in the process is timed, so `@ocap/gql` reports
|
|
19
|
+
* SQL time only where a timed backend exists (not, e.g., on the untimed Durable Object backend).
|
|
20
|
+
*/
|
|
21
|
+
declare const SQL_TIMING_BACKEND_KEY: unique symbol;
|
|
22
|
+
interface ISqlTimingSink {
|
|
23
|
+
/** Sum of the synchronous `executeQuery` time of the statements run in this context, ms. */
|
|
24
|
+
sqlMs: number;
|
|
25
|
+
/** Number of statements. */
|
|
26
|
+
sqlQueries: number;
|
|
27
|
+
/** Set by the owner when it has read the sink; later statements (work that outlived it) are not added. */
|
|
28
|
+
closed?: boolean;
|
|
29
|
+
}
|
|
30
|
+
/** The part of `AsyncLocalStorage` the backends need. */
|
|
31
|
+
interface ISqlTimingContext {
|
|
32
|
+
getStore(): ISqlTimingSink | undefined;
|
|
33
|
+
}
|
|
34
|
+
/** The sink of the current async context, if a timing context is published and the sink is still open. */
|
|
35
|
+
declare function currentSqlTimingSink(): ISqlTimingSink | undefined;
|
|
36
|
+
interface IQueryConnection {
|
|
37
|
+
executeQuery(...args: any[]): unknown;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Time the synchronous part of every `executeQuery` call on `connection` (a Kysely `DatabaseConnection`) into the
|
|
41
|
+
* current sink. Patches the instance in place, so connection identity is kept; idempotent. Meant for drivers whose
|
|
42
|
+
* `executeQuery` runs the statement synchronously and returns a settled promise (Kysely's `SqliteDialect` over
|
|
43
|
+
* better-sqlite3, `kysely-bun-sqlite`).
|
|
44
|
+
*/
|
|
45
|
+
declare function instrumentSqlTiming<T extends IQueryConnection>(connection: T): T;
|
|
46
|
+
//#endregion
|
|
47
|
+
export { ISqlTimingContext, ISqlTimingSink, SQL_TIMING_BACKEND_KEY, SQL_TIMING_KEY, currentSqlTimingSink, instrumentSqlTiming };
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
//#region src/sql-timing.ts
|
|
2
|
+
/**
|
|
3
|
+
* SQLite execution time, attributed to whoever asked for it (#1891).
|
|
4
|
+
*
|
|
5
|
+
* `@ocap/gql` (while its slow-request log is on) runs every root field call inside an `AsyncLocalStorage` whose store
|
|
6
|
+
* is an `ISqlTimingSink`, and publishes that storage under `globalThis[SQL_TIMING_KEY]`. The SQLite backends
|
|
7
|
+
* (`@ocap/indexdb-sqlite`, `@ocap/statedb-sqlite`) pass every Kysely connection they open through
|
|
8
|
+
* `instrumentSqlTiming`, which adds the synchronous duration of each `executeQuery` call to the sink current at that
|
|
9
|
+
* moment. better-sqlite3 and bun:sqlite run the whole statement (prepare, every step, row objects) inside that call,
|
|
10
|
+
* so the sum is the field's SQLite time no matter how many `await`s come before the query: Kysely itself awaits its
|
|
11
|
+
* connection mutex before every statement, so the query never runs in the resolver's first synchronous segment.
|
|
12
|
+
*
|
|
13
|
+
* The key is a registered symbol, so separately bundled copies of these packages still meet. Without a published
|
|
14
|
+
* storage (logging off, or no `AsyncLocalStorage` on the runtime) the per-statement cost is one global property read.
|
|
15
|
+
*/
|
|
16
|
+
const SQL_TIMING_KEY = Symbol.for("@ocap/sql-timing");
|
|
17
|
+
/**
|
|
18
|
+
* `globalThis[SQL_TIMING_BACKEND_KEY] === true` once any connection in the process is timed, so `@ocap/gql` reports
|
|
19
|
+
* SQL time only where a timed backend exists (not, e.g., on the untimed Durable Object backend).
|
|
20
|
+
*/
|
|
21
|
+
const SQL_TIMING_BACKEND_KEY = Symbol.for("@ocap/sql-timing/backend");
|
|
22
|
+
const INSTRUMENTED = Symbol.for("@ocap/sql-timing/instrumented");
|
|
23
|
+
const now = typeof performance !== "undefined" && typeof performance.now === "function" ? () => performance.now() : () => Date.now();
|
|
24
|
+
/** The sink of the current async context, if a timing context is published and the sink is still open. */
|
|
25
|
+
function currentSqlTimingSink() {
|
|
26
|
+
const ctx = globalThis[SQL_TIMING_KEY];
|
|
27
|
+
if (ctx === void 0) return void 0;
|
|
28
|
+
const sink = ctx.getStore();
|
|
29
|
+
return sink !== void 0 && sink.closed !== true ? sink : void 0;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Time the synchronous part of every `executeQuery` call on `connection` (a Kysely `DatabaseConnection`) into the
|
|
33
|
+
* current sink. Patches the instance in place, so connection identity is kept; idempotent. Meant for drivers whose
|
|
34
|
+
* `executeQuery` runs the statement synchronously and returns a settled promise (Kysely's `SqliteDialect` over
|
|
35
|
+
* better-sqlite3, `kysely-bun-sqlite`).
|
|
36
|
+
*/
|
|
37
|
+
function instrumentSqlTiming(connection) {
|
|
38
|
+
const c = connection;
|
|
39
|
+
if (c[INSTRUMENTED]) return connection;
|
|
40
|
+
const original = connection.executeQuery;
|
|
41
|
+
c.executeQuery = function executeQueryTimed(...args) {
|
|
42
|
+
const sink = currentSqlTimingSink();
|
|
43
|
+
if (sink === void 0) return original.apply(this, args);
|
|
44
|
+
const t = now();
|
|
45
|
+
try {
|
|
46
|
+
return original.apply(this, args);
|
|
47
|
+
} finally {
|
|
48
|
+
sink.sqlMs += now() - t;
|
|
49
|
+
sink.sqlQueries += 1;
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
Object.defineProperty(c, INSTRUMENTED, { value: true });
|
|
53
|
+
globalThis[SQL_TIMING_BACKEND_KEY] = true;
|
|
54
|
+
return connection;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
//#endregion
|
|
58
|
+
export { SQL_TIMING_BACKEND_KEY, SQL_TIMING_KEY, currentSqlTimingSink, instrumentSqlTiming };
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
|
|
2
|
+
//#region src/client-ip.ts
|
|
3
|
+
/**
|
|
4
|
+
* Client IP of an HTTP request for logs and per-client rate limits (#1922).
|
|
5
|
+
*
|
|
6
|
+
* Order: the first trusted proxy header that holds a well-formed IP, then `req.ip` (express; equals the socket address
|
|
7
|
+
* unless the app set `trust proxy`), then the socket address.
|
|
8
|
+
*
|
|
9
|
+
* Trusted headers default to `x-real-ip` only: the Blocklet Server nginx sets it from `$remote_addr` but passes a
|
|
10
|
+
* client-supplied `cf-connecting-ip` through, so trusting `cf-connecting-ip` there would let a client pick its own
|
|
11
|
+
* address. A Cloudflare Tunnel deployment opts in with `CHAIN_CLIENT_IP_HEADERS=cf-connecting-ip`.
|
|
12
|
+
*
|
|
13
|
+
* Proxy headers are trusted only when the immediate peer is a loopback address. A chain process listens on
|
|
14
|
+
* 127.0.0.1 and is reached through the cloudflared daemon on the same host (#1875 decision E4), so a loopback
|
|
15
|
+
* peer is that local proxy; Cloudflare's edge sets `cf-connecting-ip` to the real client address and rejects a
|
|
16
|
+
* request that already carries one. A non-loopback peer talks to the process directly (for example a
|
|
17
|
+
* node bound to 0.0.0.0), so any header it sends is its own claim and is ignored: the socket address is used.
|
|
18
|
+
*
|
|
19
|
+
* A socket whose address is missing (Node clears `remoteAddress` once the client resets the connection) is an
|
|
20
|
+
* unknown peer: headers are ignored. Only a request object with no socket at all (fetch runtimes, synthetic
|
|
21
|
+
* contexts) uses an adapter-provided `ip`, then the headers.
|
|
22
|
+
*
|
|
23
|
+
* Any process on the same host can connect over loopback and so name any client IP; that is accepted by #1875 E4.
|
|
24
|
+
* `CHAIN_CLIENT_IP_HEADERS=none` (or empty) trusts no header at all.
|
|
25
|
+
*
|
|
26
|
+
* No node built-ins: this module is also bundled for browsers and Workers.
|
|
27
|
+
*/
|
|
28
|
+
const CLIENT_IP_HEADERS_ENV = "CHAIN_CLIENT_IP_HEADERS";
|
|
29
|
+
const DEFAULT_CLIENT_IP_HEADERS = Object.freeze(["x-real-ip"]);
|
|
30
|
+
const readEnv = () => {
|
|
31
|
+
return globalThis.process?.env?.[CLIENT_IP_HEADERS_ENV];
|
|
32
|
+
};
|
|
33
|
+
/** Trusted proxy header names from `CHAIN_CLIENT_IP_HEADERS` (comma-separated); unset → the default list. */
|
|
34
|
+
function getTrustedClientIpHeaders(raw = readEnv()) {
|
|
35
|
+
if (raw === void 0) return [...DEFAULT_CLIENT_IP_HEADERS];
|
|
36
|
+
const list = raw.split(",").map((s) => s.trim().toLowerCase()).filter(Boolean);
|
|
37
|
+
return list.length === 1 && list[0] === "none" ? [] : list;
|
|
38
|
+
}
|
|
39
|
+
const stripBrackets = (addr) => addr.trim().replace(/^\[|\]$/g, "");
|
|
40
|
+
const isIpv4 = (s) => {
|
|
41
|
+
const parts = s.split(".");
|
|
42
|
+
return parts.length === 4 && parts.every((p) => /^\d{1,3}$/.test(p) && Number(p) <= 255);
|
|
43
|
+
};
|
|
44
|
+
/** Expands an IPv6 literal into 8 numeric groups, or null when it is not one (embedded IPv4 tails excluded). */
|
|
45
|
+
const ipv6Groups = (s) => {
|
|
46
|
+
if (!/^[0-9a-f:]+$/i.test(s)) return null;
|
|
47
|
+
const halves = s.split("::");
|
|
48
|
+
if (halves.length > 2) return null;
|
|
49
|
+
const head = halves[0] ? halves[0].split(":") : [];
|
|
50
|
+
const tail = halves.length === 2 && halves[1] ? halves[1].split(":") : [];
|
|
51
|
+
const missing = 8 - head.length - tail.length;
|
|
52
|
+
if (halves.length === 1 ? missing !== 0 : missing < 1) return null;
|
|
53
|
+
const groups = [
|
|
54
|
+
...head,
|
|
55
|
+
...Array(halves.length === 2 ? missing : 0).fill("0"),
|
|
56
|
+
...tail
|
|
57
|
+
];
|
|
58
|
+
if (!groups.every((g) => /^[0-9a-f]{1,4}$/i.test(g))) return null;
|
|
59
|
+
return groups.map((g) => Number.parseInt(g, 16));
|
|
60
|
+
};
|
|
61
|
+
/** True for 127.0.0.0/8, ::1 and IPv4-mapped 127.0.0.0/8. */
|
|
62
|
+
function isLoopbackAddress(addr) {
|
|
63
|
+
if (!addr) return false;
|
|
64
|
+
let s = stripBrackets(addr).toLowerCase();
|
|
65
|
+
const mapped = /^::ffff:(\d{1,3}(?:\.\d{1,3}){3})$/.exec(s);
|
|
66
|
+
if (mapped) s = mapped[1];
|
|
67
|
+
if (isIpv4(s)) return s.split(".")[0] === "127";
|
|
68
|
+
const groups = ipv6Groups(s);
|
|
69
|
+
if (groups === null) return false;
|
|
70
|
+
return groups.slice(0, 7).every((g) => g === 0) && groups[7] === 1;
|
|
71
|
+
}
|
|
72
|
+
/** Only bare IP literals: no spaces, commas, newlines or hostnames can reach a log line or a rate-limit key. */
|
|
73
|
+
const isIpLiteral = (s) => {
|
|
74
|
+
if (s.length < 2 || s.length > 45) return false;
|
|
75
|
+
if (isIpv4(s)) return true;
|
|
76
|
+
if (!s.includes(":")) return false;
|
|
77
|
+
const mapped = /^(.*:)(\d{1,3}(?:\.\d{1,3}){3})$/.exec(s);
|
|
78
|
+
if (mapped) return isIpv4(mapped[2]) && /^[0-9a-f:]+$/i.test(mapped[1]);
|
|
79
|
+
return ipv6Groups(s) !== null;
|
|
80
|
+
};
|
|
81
|
+
const readHeader = (headers, name) => {
|
|
82
|
+
if (!headers) return void 0;
|
|
83
|
+
let value = headers[name];
|
|
84
|
+
if (value === void 0) {
|
|
85
|
+
for (const [k, v] of Object.entries(headers)) if (k.toLowerCase() === name) {
|
|
86
|
+
value = v;
|
|
87
|
+
break;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
const first = Array.isArray(value) ? value[0] : value;
|
|
91
|
+
return typeof first === "string" ? first.trim() : void 0;
|
|
92
|
+
};
|
|
93
|
+
/** Address of the immediate TCP peer, if the request object exposes one. */
|
|
94
|
+
function getPeerAddress(src) {
|
|
95
|
+
return src?.socket?.remoteAddress || src?.connection?.remoteAddress || src?._remoteAddress || void 0;
|
|
96
|
+
}
|
|
97
|
+
/** See the module comment for the trust rule. Returns `''` when nothing is known. */
|
|
98
|
+
function resolveClientIp(src, options = {}) {
|
|
99
|
+
if (!src) return "";
|
|
100
|
+
const trusted = options.headers ?? getTrustedClientIpHeaders();
|
|
101
|
+
const fromHeaders = () => {
|
|
102
|
+
for (const name of trusted) {
|
|
103
|
+
const value = readHeader(src.headers, name.toLowerCase());
|
|
104
|
+
if (value && isIpLiteral(value)) return value;
|
|
105
|
+
}
|
|
106
|
+
return "";
|
|
107
|
+
};
|
|
108
|
+
if (!(src.socket != null || src.connection != null || src._remoteAddress !== void 0)) return src.ip || fromHeaders();
|
|
109
|
+
const peer = getPeerAddress(src);
|
|
110
|
+
if (peer !== void 0 && isLoopbackAddress(peer)) {
|
|
111
|
+
const ip = fromHeaders();
|
|
112
|
+
if (ip) return ip;
|
|
113
|
+
}
|
|
114
|
+
return src.ip || peer || "";
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
//#endregion
|
|
118
|
+
exports.CLIENT_IP_HEADERS_ENV = CLIENT_IP_HEADERS_ENV;
|
|
119
|
+
exports.DEFAULT_CLIENT_IP_HEADERS = DEFAULT_CLIENT_IP_HEADERS;
|
|
120
|
+
exports.getPeerAddress = getPeerAddress;
|
|
121
|
+
exports.getTrustedClientIpHeaders = getTrustedClientIpHeaders;
|
|
122
|
+
exports.isLoopbackAddress = isLoopbackAddress;
|
|
123
|
+
exports.resolveClientIp = resolveClientIp;
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
//#region src/client-ip.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Client IP of an HTTP request for logs and per-client rate limits (#1922).
|
|
4
|
+
*
|
|
5
|
+
* Order: the first trusted proxy header that holds a well-formed IP, then `req.ip` (express; equals the socket address
|
|
6
|
+
* unless the app set `trust proxy`), then the socket address.
|
|
7
|
+
*
|
|
8
|
+
* Trusted headers default to `x-real-ip` only: the Blocklet Server nginx sets it from `$remote_addr` but passes a
|
|
9
|
+
* client-supplied `cf-connecting-ip` through, so trusting `cf-connecting-ip` there would let a client pick its own
|
|
10
|
+
* address. A Cloudflare Tunnel deployment opts in with `CHAIN_CLIENT_IP_HEADERS=cf-connecting-ip`.
|
|
11
|
+
*
|
|
12
|
+
* Proxy headers are trusted only when the immediate peer is a loopback address. A chain process listens on
|
|
13
|
+
* 127.0.0.1 and is reached through the cloudflared daemon on the same host (#1875 decision E4), so a loopback
|
|
14
|
+
* peer is that local proxy; Cloudflare's edge sets `cf-connecting-ip` to the real client address and rejects a
|
|
15
|
+
* request that already carries one. A non-loopback peer talks to the process directly (for example a
|
|
16
|
+
* node bound to 0.0.0.0), so any header it sends is its own claim and is ignored: the socket address is used.
|
|
17
|
+
*
|
|
18
|
+
* A socket whose address is missing (Node clears `remoteAddress` once the client resets the connection) is an
|
|
19
|
+
* unknown peer: headers are ignored. Only a request object with no socket at all (fetch runtimes, synthetic
|
|
20
|
+
* contexts) uses an adapter-provided `ip`, then the headers.
|
|
21
|
+
*
|
|
22
|
+
* Any process on the same host can connect over loopback and so name any client IP; that is accepted by #1875 E4.
|
|
23
|
+
* `CHAIN_CLIENT_IP_HEADERS=none` (or empty) trusts no header at all.
|
|
24
|
+
*
|
|
25
|
+
* No node built-ins: this module is also bundled for browsers and Workers.
|
|
26
|
+
*/
|
|
27
|
+
declare const CLIENT_IP_HEADERS_ENV = "CHAIN_CLIENT_IP_HEADERS";
|
|
28
|
+
declare const DEFAULT_CLIENT_IP_HEADERS: readonly string[];
|
|
29
|
+
type ClientIpHeaders = Record<string, string | string[] | undefined>;
|
|
30
|
+
interface ClientIpSource {
|
|
31
|
+
headers?: ClientIpHeaders;
|
|
32
|
+
/** express `req.ip` */
|
|
33
|
+
ip?: string;
|
|
34
|
+
socket?: {
|
|
35
|
+
remoteAddress?: string;
|
|
36
|
+
} | null;
|
|
37
|
+
connection?: {
|
|
38
|
+
remoteAddress?: string;
|
|
39
|
+
} | null;
|
|
40
|
+
_remoteAddress?: string;
|
|
41
|
+
}
|
|
42
|
+
interface ResolveClientIpOptions {
|
|
43
|
+
/** Trusted header names, in order. Defaults to `getTrustedClientIpHeaders()`. */
|
|
44
|
+
headers?: readonly string[];
|
|
45
|
+
}
|
|
46
|
+
/** Trusted proxy header names from `CHAIN_CLIENT_IP_HEADERS` (comma-separated); unset → the default list. */
|
|
47
|
+
declare function getTrustedClientIpHeaders(raw?: string | undefined): string[];
|
|
48
|
+
/** True for 127.0.0.0/8, ::1 and IPv4-mapped 127.0.0.0/8. */
|
|
49
|
+
declare function isLoopbackAddress(addr: string | undefined | null): boolean;
|
|
50
|
+
/** Address of the immediate TCP peer, if the request object exposes one. */
|
|
51
|
+
declare function getPeerAddress(src: ClientIpSource | undefined | null): string | undefined;
|
|
52
|
+
/** See the module comment for the trust rule. Returns `''` when nothing is known. */
|
|
53
|
+
declare function resolveClientIp(src: ClientIpSource | undefined | null, options?: ResolveClientIpOptions): string;
|
|
54
|
+
//#endregion
|
|
55
|
+
export { CLIENT_IP_HEADERS_ENV, ClientIpHeaders, ClientIpSource, DEFAULT_CLIENT_IP_HEADERS, ResolveClientIpOptions, getPeerAddress, getTrustedClientIpHeaders, isLoopbackAddress, resolveClientIp };
|
|
@@ -72,6 +72,7 @@ function walk(node, anyDepth) {
|
|
|
72
72
|
depth += 1;
|
|
73
73
|
if (depth > MAX_ANY_DEPTH) return;
|
|
74
74
|
if (isDepositTokenV2Type(anyType) && isPlainObject(node.value)) stripOwnDefaults(node.value);
|
|
75
|
+
else if ((anyType === "ItxStub" || anyType === "fg:x:itx_stub" || anyType === "fg:t:itx_stub") && isPlainObject(node.value) && isPlainObject(node.value.depositToken)) stripOwnDefaults(node.value.depositToken);
|
|
75
76
|
}
|
|
76
77
|
for (const child of Object.values(node)) walk(child, depth);
|
|
77
78
|
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
|
|
2
|
+
//#region src/sql-timing.ts
|
|
3
|
+
/**
|
|
4
|
+
* SQLite execution time, attributed to whoever asked for it (#1891).
|
|
5
|
+
*
|
|
6
|
+
* `@ocap/gql` (while its slow-request log is on) runs every root field call inside an `AsyncLocalStorage` whose store
|
|
7
|
+
* is an `ISqlTimingSink`, and publishes that storage under `globalThis[SQL_TIMING_KEY]`. The SQLite backends
|
|
8
|
+
* (`@ocap/indexdb-sqlite`, `@ocap/statedb-sqlite`) pass every Kysely connection they open through
|
|
9
|
+
* `instrumentSqlTiming`, which adds the synchronous duration of each `executeQuery` call to the sink current at that
|
|
10
|
+
* moment. better-sqlite3 and bun:sqlite run the whole statement (prepare, every step, row objects) inside that call,
|
|
11
|
+
* so the sum is the field's SQLite time no matter how many `await`s come before the query: Kysely itself awaits its
|
|
12
|
+
* connection mutex before every statement, so the query never runs in the resolver's first synchronous segment.
|
|
13
|
+
*
|
|
14
|
+
* The key is a registered symbol, so separately bundled copies of these packages still meet. Without a published
|
|
15
|
+
* storage (logging off, or no `AsyncLocalStorage` on the runtime) the per-statement cost is one global property read.
|
|
16
|
+
*/
|
|
17
|
+
const SQL_TIMING_KEY = Symbol.for("@ocap/sql-timing");
|
|
18
|
+
/**
|
|
19
|
+
* `globalThis[SQL_TIMING_BACKEND_KEY] === true` once any connection in the process is timed, so `@ocap/gql` reports
|
|
20
|
+
* SQL time only where a timed backend exists (not, e.g., on the untimed Durable Object backend).
|
|
21
|
+
*/
|
|
22
|
+
const SQL_TIMING_BACKEND_KEY = Symbol.for("@ocap/sql-timing/backend");
|
|
23
|
+
const INSTRUMENTED = Symbol.for("@ocap/sql-timing/instrumented");
|
|
24
|
+
const now = typeof performance !== "undefined" && typeof performance.now === "function" ? () => performance.now() : () => Date.now();
|
|
25
|
+
/** The sink of the current async context, if a timing context is published and the sink is still open. */
|
|
26
|
+
function currentSqlTimingSink() {
|
|
27
|
+
const ctx = globalThis[SQL_TIMING_KEY];
|
|
28
|
+
if (ctx === void 0) return void 0;
|
|
29
|
+
const sink = ctx.getStore();
|
|
30
|
+
return sink !== void 0 && sink.closed !== true ? sink : void 0;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Time the synchronous part of every `executeQuery` call on `connection` (a Kysely `DatabaseConnection`) into the
|
|
34
|
+
* current sink. Patches the instance in place, so connection identity is kept; idempotent. Meant for drivers whose
|
|
35
|
+
* `executeQuery` runs the statement synchronously and returns a settled promise (Kysely's `SqliteDialect` over
|
|
36
|
+
* better-sqlite3, `kysely-bun-sqlite`).
|
|
37
|
+
*/
|
|
38
|
+
function instrumentSqlTiming(connection) {
|
|
39
|
+
const c = connection;
|
|
40
|
+
if (c[INSTRUMENTED]) return connection;
|
|
41
|
+
const original = connection.executeQuery;
|
|
42
|
+
c.executeQuery = function executeQueryTimed(...args) {
|
|
43
|
+
const sink = currentSqlTimingSink();
|
|
44
|
+
if (sink === void 0) return original.apply(this, args);
|
|
45
|
+
const t = now();
|
|
46
|
+
try {
|
|
47
|
+
return original.apply(this, args);
|
|
48
|
+
} finally {
|
|
49
|
+
sink.sqlMs += now() - t;
|
|
50
|
+
sink.sqlQueries += 1;
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
Object.defineProperty(c, INSTRUMENTED, { value: true });
|
|
54
|
+
globalThis[SQL_TIMING_BACKEND_KEY] = true;
|
|
55
|
+
return connection;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
//#endregion
|
|
59
|
+
exports.SQL_TIMING_BACKEND_KEY = SQL_TIMING_BACKEND_KEY;
|
|
60
|
+
exports.SQL_TIMING_KEY = SQL_TIMING_KEY;
|
|
61
|
+
exports.currentSqlTimingSink = currentSqlTimingSink;
|
|
62
|
+
exports.instrumentSqlTiming = instrumentSqlTiming;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
//#region src/sql-timing.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* SQLite execution time, attributed to whoever asked for it (#1891).
|
|
4
|
+
*
|
|
5
|
+
* `@ocap/gql` (while its slow-request log is on) runs every root field call inside an `AsyncLocalStorage` whose store
|
|
6
|
+
* is an `ISqlTimingSink`, and publishes that storage under `globalThis[SQL_TIMING_KEY]`. The SQLite backends
|
|
7
|
+
* (`@ocap/indexdb-sqlite`, `@ocap/statedb-sqlite`) pass every Kysely connection they open through
|
|
8
|
+
* `instrumentSqlTiming`, which adds the synchronous duration of each `executeQuery` call to the sink current at that
|
|
9
|
+
* moment. better-sqlite3 and bun:sqlite run the whole statement (prepare, every step, row objects) inside that call,
|
|
10
|
+
* so the sum is the field's SQLite time no matter how many `await`s come before the query: Kysely itself awaits its
|
|
11
|
+
* connection mutex before every statement, so the query never runs in the resolver's first synchronous segment.
|
|
12
|
+
*
|
|
13
|
+
* The key is a registered symbol, so separately bundled copies of these packages still meet. Without a published
|
|
14
|
+
* storage (logging off, or no `AsyncLocalStorage` on the runtime) the per-statement cost is one global property read.
|
|
15
|
+
*/
|
|
16
|
+
declare const SQL_TIMING_KEY: unique symbol;
|
|
17
|
+
/**
|
|
18
|
+
* `globalThis[SQL_TIMING_BACKEND_KEY] === true` once any connection in the process is timed, so `@ocap/gql` reports
|
|
19
|
+
* SQL time only where a timed backend exists (not, e.g., on the untimed Durable Object backend).
|
|
20
|
+
*/
|
|
21
|
+
declare const SQL_TIMING_BACKEND_KEY: unique symbol;
|
|
22
|
+
interface ISqlTimingSink {
|
|
23
|
+
/** Sum of the synchronous `executeQuery` time of the statements run in this context, ms. */
|
|
24
|
+
sqlMs: number;
|
|
25
|
+
/** Number of statements. */
|
|
26
|
+
sqlQueries: number;
|
|
27
|
+
/** Set by the owner when it has read the sink; later statements (work that outlived it) are not added. */
|
|
28
|
+
closed?: boolean;
|
|
29
|
+
}
|
|
30
|
+
/** The part of `AsyncLocalStorage` the backends need. */
|
|
31
|
+
interface ISqlTimingContext {
|
|
32
|
+
getStore(): ISqlTimingSink | undefined;
|
|
33
|
+
}
|
|
34
|
+
/** The sink of the current async context, if a timing context is published and the sink is still open. */
|
|
35
|
+
declare function currentSqlTimingSink(): ISqlTimingSink | undefined;
|
|
36
|
+
interface IQueryConnection {
|
|
37
|
+
executeQuery(...args: any[]): unknown;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Time the synchronous part of every `executeQuery` call on `connection` (a Kysely `DatabaseConnection`) into the
|
|
41
|
+
* current sink. Patches the instance in place, so connection identity is kept; idempotent. Meant for drivers whose
|
|
42
|
+
* `executeQuery` runs the statement synchronously and returns a settled promise (Kysely's `SqliteDialect` over
|
|
43
|
+
* better-sqlite3, `kysely-bun-sqlite`).
|
|
44
|
+
*/
|
|
45
|
+
declare function instrumentSqlTiming<T extends IQueryConnection>(connection: T): T;
|
|
46
|
+
//#endregion
|
|
47
|
+
export { ISqlTimingContext, ISqlTimingSink, SQL_TIMING_BACKEND_KEY, SQL_TIMING_KEY, currentSqlTimingSink, instrumentSqlTiming };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ocap/util",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.32.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "utils shared across multiple forge js libs, works in both node.js and browser",
|
|
6
6
|
"keywords": [
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
"access": "public"
|
|
13
13
|
},
|
|
14
14
|
"dependencies": {
|
|
15
|
-
"@ocap/types": "1.
|
|
15
|
+
"@ocap/types": "1.32.0",
|
|
16
16
|
"@types/bn.js": "^5.2.0",
|
|
17
17
|
"base64-url": "^2.3.3",
|
|
18
18
|
"bn.js": "5.2.3",
|