@ocap/util 1.31.9 → 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/lib/client-ip.cjs +123 -0
- package/lib/client-ip.d.cts +55 -0
- package/lib/deposit-v2-bridge-fields.cjs +1 -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,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
|
}
|
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",
|