@akai-workflow-builder/cli-sdk 0.19.0 → 0.20.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/CHANGELOG.md +15 -0
- package/dist/ctx/blocked-domains.d.ts +23 -0
- package/dist/ctx/blocked-domains.d.ts.map +1 -0
- package/dist/ctx/blocked-domains.js +59 -0
- package/dist/ctx/build-ctx.d.ts.map +1 -1
- package/dist/ctx/build-ctx.js +8 -2
- package/dist/ctx/egress-refusal.d.ts +47 -0
- package/dist/ctx/egress-refusal.d.ts.map +1 -0
- package/dist/ctx/egress-refusal.js +85 -0
- package/dist/ctx/pinned-get.d.ts +16 -0
- package/dist/ctx/pinned-get.d.ts.map +1 -0
- package/dist/ctx/pinned-get.js +58 -0
- package/dist/ctx/read-file-stream.d.ts +5 -2
- package/dist/ctx/read-file-stream.d.ts.map +1 -1
- package/dist/ctx/read-file-stream.js +4 -10
- package/dist/ctx/read-file.d.ts +8 -3
- package/dist/ctx/read-file.d.ts.map +1 -1
- package/dist/ctx/read-file.js +7 -11
- package/dist/ctx/safe-fetch.d.ts +15 -0
- package/dist/ctx/safe-fetch.d.ts.map +1 -1
- package/dist/ctx/safe-fetch.js +70 -28
- package/dist/ctx/sftp.d.ts +3 -0
- package/dist/ctx/sftp.d.ts.map +1 -1
- package/dist/ctx/sftp.js +2 -0
- package/dist/ctx/url-input.d.ts +38 -0
- package/dist/ctx/url-input.d.ts.map +1 -0
- package/dist/ctx/url-input.js +107 -0
- package/dist/file-input.d.ts +3 -4
- package/dist/file-input.d.ts.map +1 -1
- package/dist/file-input.js +3 -4
- package/dist/types.d.ts +3 -3
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -4,6 +4,21 @@ All notable changes to `@akai-workflow-builder/cli-sdk` are documented here.
|
|
|
4
4
|
|
|
5
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
|
|
6
6
|
|
|
7
|
+
## [0.20.0](https://github.com/akai-workflow-builder/akai-cli-sdk/compare/cli-sdk-v0.19.0...cli-sdk-v0.20.0) (2026-11-26)
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
### Features
|
|
11
|
+
|
|
12
|
+
* **ctx:** log every refused egress as egress.refused (AKAI-4277) ([066a4b1](https://github.com/akai-workflow-builder/akai-cli-sdk/commit/066a4b16d427422b5e1631bfa749507649bb48b9))
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
### Bug Fixes
|
|
16
|
+
|
|
17
|
+
* **ctx:** refuse private addresses in ctx.readFile URL inputs (AKAI-4277) ([9de3501](https://github.com/akai-workflow-builder/akai-cli-sdk/commit/9de350186443627cdd7fb1d560abd5daa68ad892))
|
|
18
|
+
* **ctx:** refuse webhook.site, interact.sh, oast.* and pipedream.net by name (AKAI-4277) ([95cd398](https://github.com/akai-workflow-builder/akai-cli-sdk/commit/95cd398cb1ebf5b11731d34704d3d7c61a1e88ba))
|
|
19
|
+
* **safe-fetch:** refuse DNS-rebinding and callback domains by name (AKAI-4277) ([fde25a1](https://github.com/akai-workflow-builder/akai-cli-sdk/commit/fde25a1efd455a3c7d246ab8b6cf2b1aabb44f94))
|
|
20
|
+
* **safe-fetch:** refuse every private and reserved address range (AKAI-4268) ([806b07e](https://github.com/akai-workflow-builder/akai-cli-sdk/commit/806b07eb62cb87a18d6b78f5b3f6e9720dfd3fa7))
|
|
21
|
+
|
|
7
22
|
## [0.19.0](https://github.com/akai-workflow-builder/akai-cli-sdk/compare/cli-sdk-v0.18.0...cli-sdk-v0.19.0) (2026-11-25)
|
|
8
23
|
|
|
9
24
|
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Domains a tool's `ctx.fetch` never connects to, nor even resolves (AKAI-4277), whatever its egress
|
|
3
|
+
* allowlist says. Attackers use them in two ways:
|
|
4
|
+
*
|
|
5
|
+
* - wildcard and rebinding DNS: a name such as `10.0.0.1.nip.io` answers with the address it spells,
|
|
6
|
+
* or alternates between a public and an internal one, to walk a request into a private network;
|
|
7
|
+
* - out-of-band callbacks: a request or a DNS lookup to such a service tells the attacker that a
|
|
8
|
+
* blind SSRF worked, and the name itself can carry stolen data (`<data>.dnslog.cn`).
|
|
9
|
+
*
|
|
10
|
+
* So the check is by name and comes before any lookup: resolving `<data>.dnslog.cn` already leaks
|
|
11
|
+
* `<data>`. It holds in development too. akai-app (lib/utils/blocked-domains.ts) and the worker
|
|
12
|
+
* (src/cli/lib/blocked-domains.js) keep the same list; test/ctx/fixtures/blocked-domains-contract.json,
|
|
13
|
+
* which each repo holds byte for byte, pins all three.
|
|
14
|
+
*/
|
|
15
|
+
export declare const BLOCKED_DOMAINS: readonly string[];
|
|
16
|
+
/**
|
|
17
|
+
* The listed domain `hostname` is, or is under (`a.b.nip.io` is under `nip.io`), else null. Case,
|
|
18
|
+
* IPv6 brackets and a trailing root dot (`nip.io.`) do not change the answer.
|
|
19
|
+
*/
|
|
20
|
+
export declare function blockedDomainOf(hostname: string): string | null;
|
|
21
|
+
/** blockedDomainOf for a URL's host; null for a URL that does not parse (its fetch fails on its own). */
|
|
22
|
+
export declare function blockedDomainOfUrl(url: string): string | null;
|
|
23
|
+
//# sourceMappingURL=blocked-domains.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"blocked-domains.d.ts","sourceRoot":"","sources":["../../src/ctx/blocked-domains.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AACH,eAAO,MAAM,eAAe,EAAE,SAAS,MAAM,EA2B5C,CAAC;AAEF;;;GAGG;AACH,wBAAgB,eAAe,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAG/D;AAED,yGAAyG;AACzG,wBAAgB,kBAAkB,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAM7D"}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Domains a tool's `ctx.fetch` never connects to, nor even resolves (AKAI-4277), whatever its egress
|
|
3
|
+
* allowlist says. Attackers use them in two ways:
|
|
4
|
+
*
|
|
5
|
+
* - wildcard and rebinding DNS: a name such as `10.0.0.1.nip.io` answers with the address it spells,
|
|
6
|
+
* or alternates between a public and an internal one, to walk a request into a private network;
|
|
7
|
+
* - out-of-band callbacks: a request or a DNS lookup to such a service tells the attacker that a
|
|
8
|
+
* blind SSRF worked, and the name itself can carry stolen data (`<data>.dnslog.cn`).
|
|
9
|
+
*
|
|
10
|
+
* So the check is by name and comes before any lookup: resolving `<data>.dnslog.cn` already leaks
|
|
11
|
+
* `<data>`. It holds in development too. akai-app (lib/utils/blocked-domains.ts) and the worker
|
|
12
|
+
* (src/cli/lib/blocked-domains.js) keep the same list; test/ctx/fixtures/blocked-domains-contract.json,
|
|
13
|
+
* which each repo holds byte for byte, pins all three.
|
|
14
|
+
*/
|
|
15
|
+
export const BLOCKED_DOMAINS = [
|
|
16
|
+
'nip.io',
|
|
17
|
+
'sslip.io',
|
|
18
|
+
'xip.io',
|
|
19
|
+
'rbndr.us',
|
|
20
|
+
'1u.ms',
|
|
21
|
+
'traefik.me',
|
|
22
|
+
'rebind.network',
|
|
23
|
+
'dnslog.cn',
|
|
24
|
+
'burpcollaborator.net',
|
|
25
|
+
'ntfy.sh',
|
|
26
|
+
'httpbin.org',
|
|
27
|
+
'requestcatcher.com',
|
|
28
|
+
'webhook.site',
|
|
29
|
+
'interact.sh',
|
|
30
|
+
'oast.fun',
|
|
31
|
+
'oast.me',
|
|
32
|
+
'oast.pro',
|
|
33
|
+
'oast.live',
|
|
34
|
+
'oast.site',
|
|
35
|
+
'oast.online',
|
|
36
|
+
'pipedream.net',
|
|
37
|
+
'localtest.me',
|
|
38
|
+
'lvh.me',
|
|
39
|
+
'vcap.me',
|
|
40
|
+
'localhost.direct',
|
|
41
|
+
'lacolhost.com',
|
|
42
|
+
];
|
|
43
|
+
/**
|
|
44
|
+
* The listed domain `hostname` is, or is under (`a.b.nip.io` is under `nip.io`), else null. Case,
|
|
45
|
+
* IPv6 brackets and a trailing root dot (`nip.io.`) do not change the answer.
|
|
46
|
+
*/
|
|
47
|
+
export function blockedDomainOf(hostname) {
|
|
48
|
+
const host = hostname.trim().toLowerCase().replace(/^\[|\]$/g, '').replace(/\.+$/, '');
|
|
49
|
+
return BLOCKED_DOMAINS.find((domain) => host === domain || host.endsWith(`.${domain}`)) ?? null;
|
|
50
|
+
}
|
|
51
|
+
/** blockedDomainOf for a URL's host; null for a URL that does not parse (its fetch fails on its own). */
|
|
52
|
+
export function blockedDomainOfUrl(url) {
|
|
53
|
+
try {
|
|
54
|
+
return blockedDomainOf(new URL(url).hostname);
|
|
55
|
+
}
|
|
56
|
+
catch {
|
|
57
|
+
return null;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"build-ctx.d.ts","sourceRoot":"","sources":["../../src/ctx/build-ctx.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AAE5D,OAAO,EAGL,KAAK,cAAc,EACnB,KAAK,YAAY,EAClB,MAAM,cAAc,CAAC;AAItB,OAAO,KAAK,EAAc,MAAM,EAAiB,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"build-ctx.d.ts","sourceRoot":"","sources":["../../src/ctx/build-ctx.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AAE5D,OAAO,EAGL,KAAK,cAAc,EACnB,KAAK,YAAY,EAClB,MAAM,cAAc,CAAC;AAItB,OAAO,KAAK,EAAc,MAAM,EAAiB,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAQ1F;;;GAGG;AACH,MAAM,WAAW,cAAc;IAC7B,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,6GAA6G;IAC7G,QAAQ,CAAC,YAAY,EAAE,YAAY,CAAC;IACpC,sGAAsG;IACtG,QAAQ,CAAC,cAAc,CAAC,EAAE,cAAc,CAAC;IACzC,QAAQ,CAAC,MAAM,EAAE,UAAU,CAAC;IAC5B,QAAQ,CAAC,MAAM,EAAE,WAAW,CAAC;IAC7B,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;IAC5B,QAAQ,CAAC,QAAQ,CAAC,EAAE,MAAM,CAAC;IAC3B;;;;OAIG;IACH,QAAQ,CAAC,kBAAkB,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;IAChD;;;;;;OAMG;IACH,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,CAAC;IAC5B,2CAA2C;IAC3C,QAAQ,CAAC,gBAAgB,CAAC,EAAE,MAAM,CAAC;IACnC,8CAA8C;IAC9C,QAAQ,CAAC,mBAAmB,CAAC,EAAE,MAAM,CAAC;IACtC;;;;;;;OAOG;IACH,QAAQ,CAAC,aAAa,CAAC,EAAE,SAAS,gBAAgB,EAAE,CAAC;CACtD;AAmHD;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,QAAQ,CAAC,MAAM,EAAE,cAAc,GAAG,OAAO,CAmGxD"}
|
package/dist/ctx/build-ctx.js
CHANGED
|
@@ -10,6 +10,7 @@ import { resolveBinding, resolveDynamicEgressHosts } from './dynamic-egress.js';
|
|
|
10
10
|
import { parseSftpEgress } from './sftp-egress.js';
|
|
11
11
|
import { createSftp, unavailableSftp } from './sftp.js';
|
|
12
12
|
import { normalizePem } from './pem.js';
|
|
13
|
+
import { egressRefusalLogger } from './egress-refusal.js';
|
|
13
14
|
function resolveTool(cli, toolName) {
|
|
14
15
|
const t = cli.tools[toolName];
|
|
15
16
|
if (!t) {
|
|
@@ -119,6 +120,9 @@ export function buildCtx(params) {
|
|
|
119
120
|
const secrets = buildScopedSecrets(params.cli.secrets, tool.options.secretKeys, params.secretValues, { oauthPresent: typeof params.oauthAccessToken === 'string' && params.oauthAccessToken !== '' });
|
|
120
121
|
const properties = buildScopedProperties(params.cli.properties, tool.options.propertyKeys, params.propertyValues ?? {});
|
|
121
122
|
const clientCert = resolveClientCert(tool, secrets, params.cli.id, params.toolName);
|
|
123
|
+
// Every guard below reports a request it refused for where it would go (a blocked domain, a private
|
|
124
|
+
// address) through this one `logger.warn`, which Akai's security alerts count (egress-refusal.ts).
|
|
125
|
+
const onEgressRefused = egressRefusalLogger(params.logger);
|
|
122
126
|
const isSftp = tool.transport === 'sftp' && tool.options.network !== undefined;
|
|
123
127
|
const sftp = isSftp
|
|
124
128
|
? createSftp({
|
|
@@ -127,6 +131,7 @@ export function buildCtx(params) {
|
|
|
127
131
|
toolName: params.toolName,
|
|
128
132
|
signal: params.signal,
|
|
129
133
|
maxReadBytes: tool.options.producesFiles === true ? PRODUCES_FILES_MAX_RESPONSE_BYTES : DEFAULT_MAX_RESPONSE_BYTES,
|
|
134
|
+
onEgressRefused,
|
|
130
135
|
})
|
|
131
136
|
: unavailableSftp();
|
|
132
137
|
const fetch = tool.transport === 'native' || tool.transport === 'sftp' || tool.options.network === undefined
|
|
@@ -155,14 +160,15 @@ export function buildCtx(params) {
|
|
|
155
160
|
...(tool.options.network.trustedCa !== undefined
|
|
156
161
|
? { trustedCa: tool.options.network.trustedCa }
|
|
157
162
|
: {}),
|
|
163
|
+
onEgressRefused,
|
|
158
164
|
});
|
|
159
165
|
const safePath = createSafePath([
|
|
160
166
|
params.workdir,
|
|
161
167
|
tmpdir(),
|
|
162
168
|
...(params.extraSafePathRoots ?? []),
|
|
163
169
|
]);
|
|
164
|
-
const readFile = createReadFile({ safePath, signal: params.signal });
|
|
165
|
-
const readFileStream = createReadFileStream({ safePath, signal: params.signal });
|
|
170
|
+
const readFile = createReadFile({ safePath, signal: params.signal, onEgressRefused });
|
|
171
|
+
const readFileStream = createReadFileStream({ safePath, signal: params.signal, onEgressRefused });
|
|
166
172
|
// Real writer only when the tool opted in (producesFiles) AND the host gave
|
|
167
173
|
// an outputDir — enforces the opt-in at the SDK boundary; else a throwing stub.
|
|
168
174
|
const writeFile = tool.options.producesFiles === true && params.outputDir !== undefined
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type { ToolLogger } from '../types.js';
|
|
2
|
+
/**
|
|
3
|
+
* One log entry for each request a ctx guard refused because of where it would go (AKAI-4268,
|
|
4
|
+
* AKAI-4277): a host on the blocked-domain list, or one that resolves to a private address. Akai's
|
|
5
|
+
* security alerts count these entries by their `event`. The worker and akai-app write the same entry
|
|
6
|
+
* from their own guards; a JSON fixture each repo holds byte for byte pins its shape
|
|
7
|
+
* (test/ctx/fixtures/egress-refusal-log-contract.json here).
|
|
8
|
+
*
|
|
9
|
+
* The SDK writes nothing itself: the guard reports through `onEgressRefused`, and buildCtx turns that
|
|
10
|
+
* into one `logger.warn(fields, msg)` on the logger its host gave it, which decides where the line goes.
|
|
11
|
+
*/
|
|
12
|
+
export type EgressRefusalReason = 'blocked_domain' | 'private_address';
|
|
13
|
+
export interface EgressRefusal {
|
|
14
|
+
/** The guard that refused: `sdk_fetch` (ctx.fetch), `sdk_read_file` (ctx.readFile, ctx.readFileStream) or `sdk_sftp`. */
|
|
15
|
+
readonly via: string;
|
|
16
|
+
/**
|
|
17
|
+
* The host the guard refused, as the request named it: never the URL, but every label of the name.
|
|
18
|
+
* Log it only through {@link egressRefusalEntry}, which drops the labels under a blocked domain.
|
|
19
|
+
*/
|
|
20
|
+
readonly host: string;
|
|
21
|
+
readonly reason: EgressRefusalReason;
|
|
22
|
+
/** The private address the host resolved to, when the guard read one. */
|
|
23
|
+
readonly address?: string;
|
|
24
|
+
}
|
|
25
|
+
/** The entry as it is logged: the message, then the fields. */
|
|
26
|
+
export interface EgressRefusalEntry {
|
|
27
|
+
readonly msg: string;
|
|
28
|
+
readonly event: typeof EGRESS_REFUSED_EVENT;
|
|
29
|
+
readonly via: string;
|
|
30
|
+
readonly host: string;
|
|
31
|
+
readonly reason: EgressRefusalReason;
|
|
32
|
+
readonly address?: string;
|
|
33
|
+
}
|
|
34
|
+
export declare const EGRESS_REFUSED_EVENT = "egress.refused";
|
|
35
|
+
/** The entry for one refusal. */
|
|
36
|
+
export declare function egressRefusalEntry(refusal: EgressRefusal): EgressRefusalEntry;
|
|
37
|
+
/**
|
|
38
|
+
* The `onEgressRefused` buildCtx hands its guards: one `logger.warn(fields, msg)` per refusal. Within
|
|
39
|
+
* one ctx, the same refusal (one listed domain, or one private address, per guard) is logged once a
|
|
40
|
+
* minute at most, so a tool that retries in a loop cannot flood the log. The memory belongs to this
|
|
41
|
+
* reporter, not to the process: in a host that serves many tenants, one tenant's refusal never hides
|
|
42
|
+
* another's. Never throws.
|
|
43
|
+
*/
|
|
44
|
+
export declare function egressRefusalLogger(logger: ToolLogger, now?: () => number): (refusal: EgressRefusal) => void;
|
|
45
|
+
/** Call a guard's `onEgressRefused`, when it has one, without letting it throw into the refusal. */
|
|
46
|
+
export declare function reportEgressRefusal(onEgressRefused: ((refusal: EgressRefusal) => void) | undefined, refusal: EgressRefusal): void;
|
|
47
|
+
//# sourceMappingURL=egress-refusal.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"egress-refusal.d.ts","sourceRoot":"","sources":["../../src/ctx/egress-refusal.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAE9C;;;;;;;;;GASG;AAEH,MAAM,MAAM,mBAAmB,GAAG,gBAAgB,GAAG,iBAAiB,CAAC;AAEvE,MAAM,WAAW,aAAa;IAC5B,yHAAyH;IACzH,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB;;;OAGG;IACH,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,MAAM,EAAE,mBAAmB,CAAC;IACrC,yEAAyE;IACzE,QAAQ,CAAC,OAAO,CAAC,EAAE,MAAM,CAAC;CAC3B;AAED,+DAA+D;AAC/D,MAAM,WAAW,kBAAkB;IACjC,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,QAAQ,CAAC,KAAK,EAAE,OAAO,oBAAoB,CAAC;IAC5C,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,MAAM,EAAE,mBAAmB,CAAC;IACrC,QAAQ,CAAC,OAAO,CAAC,EAAE,MAAM,CAAC;CAC3B;AAED,eAAO,MAAM,oBAAoB,mBAAmB,CAAC;AAyBrD,iCAAiC;AACjC,wBAAgB,kBAAkB,CAAC,OAAO,EAAE,aAAa,GAAG,kBAAkB,CAqB7E;AAED;;;;;;GAMG;AACH,wBAAgB,mBAAmB,CAAC,MAAM,EAAE,UAAU,EAAE,GAAG,GAAE,MAAM,MAAiB,GAAG,CAAC,OAAO,EAAE,aAAa,KAAK,IAAI,CAgBtH;AAED,oGAAoG;AACpG,wBAAgB,mBAAmB,CAAC,eAAe,EAAE,CAAC,CAAC,OAAO,EAAE,aAAa,KAAK,IAAI,CAAC,GAAG,SAAS,EAAE,OAAO,EAAE,aAAa,GAAG,IAAI,CAOjI"}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { blockedDomainOf } from './blocked-domains.js';
|
|
2
|
+
export const EGRESS_REFUSED_EVENT = 'egress.refused';
|
|
3
|
+
const MAX_HOST_CHARS = 253; // the longest DNS name
|
|
4
|
+
const ONCE_PER_MS = 60_000;
|
|
5
|
+
const MAX_REMEMBERED = 200;
|
|
6
|
+
/**
|
|
7
|
+
* Logged in place of a name the guard refused by the blocked-domain list but this check cannot place on
|
|
8
|
+
* it (the guards use the same list, so it should not happen): never the name itself.
|
|
9
|
+
*/
|
|
10
|
+
const UNNAMED_BLOCKED_HOST = 'blocked-domain';
|
|
11
|
+
/**
|
|
12
|
+
* The host as the entry may name it. A host on or under a blocked domain becomes the listed domain
|
|
13
|
+
* (`dnslog.cn`), whatever the reason: on a callback service the labels under it are the data being sent
|
|
14
|
+
* out (`<cookie>.dnslog.cn`), and a log keeps them for weeks. The match runs on the whole name, before
|
|
15
|
+
* any cut, so a long name cannot push the listed domain out of reach. Any other host loses its IPv6
|
|
16
|
+
* brackets and is cut to the longest DNS name.
|
|
17
|
+
*/
|
|
18
|
+
function loggedHost(host, reason) {
|
|
19
|
+
const name = host.toLowerCase().replace(/^\[|\]$/g, '');
|
|
20
|
+
const listed = blockedDomainOf(name);
|
|
21
|
+
if (listed !== null)
|
|
22
|
+
return listed;
|
|
23
|
+
return reason === 'blocked_domain' ? UNNAMED_BLOCKED_HOST : name.slice(0, MAX_HOST_CHARS);
|
|
24
|
+
}
|
|
25
|
+
/** The entry for one refusal. */
|
|
26
|
+
export function egressRefusalEntry(refusal) {
|
|
27
|
+
const { via, reason, address } = refusal;
|
|
28
|
+
const host = loggedHost(refusal.host, reason);
|
|
29
|
+
if (reason === 'blocked_domain') {
|
|
30
|
+
return {
|
|
31
|
+
msg: `[egress] ${via} refused a ${host} name: it is on Akai's blocked-domain list`,
|
|
32
|
+
event: EGRESS_REFUSED_EVENT,
|
|
33
|
+
via,
|
|
34
|
+
host,
|
|
35
|
+
reason,
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
const why = address ? `it resolves to the private address ${address}` : 'it is a private address';
|
|
39
|
+
return {
|
|
40
|
+
msg: `[egress] ${via} refused ${host}: ${why}`,
|
|
41
|
+
event: EGRESS_REFUSED_EVENT,
|
|
42
|
+
via,
|
|
43
|
+
host,
|
|
44
|
+
reason,
|
|
45
|
+
...(address ? { address } : {}),
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* The `onEgressRefused` buildCtx hands its guards: one `logger.warn(fields, msg)` per refusal. Within
|
|
50
|
+
* one ctx, the same refusal (one listed domain, or one private address, per guard) is logged once a
|
|
51
|
+
* minute at most, so a tool that retries in a loop cannot flood the log. The memory belongs to this
|
|
52
|
+
* reporter, not to the process: in a host that serves many tenants, one tenant's refusal never hides
|
|
53
|
+
* another's. Never throws.
|
|
54
|
+
*/
|
|
55
|
+
export function egressRefusalLogger(logger, now = Date.now) {
|
|
56
|
+
const lastLogged = new Map(); // `${via} ${reason} ${address | host}` -> when it was last logged
|
|
57
|
+
return (refusal) => {
|
|
58
|
+
try {
|
|
59
|
+
const { msg, ...fields } = egressRefusalEntry(refusal);
|
|
60
|
+
const key = `${fields.via} ${fields.reason} ${fields.address ?? fields.host}`;
|
|
61
|
+
const at = now();
|
|
62
|
+
const last = lastLogged.get(key);
|
|
63
|
+
if (last !== undefined && at - last < ONCE_PER_MS)
|
|
64
|
+
return;
|
|
65
|
+
if (lastLogged.size >= MAX_REMEMBERED)
|
|
66
|
+
lastLogged.clear();
|
|
67
|
+
lastLogged.set(key, at);
|
|
68
|
+
logger.warn(fields, msg);
|
|
69
|
+
}
|
|
70
|
+
catch {
|
|
71
|
+
// Best-effort: the refusal itself is thrown to the tool either way.
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
/** Call a guard's `onEgressRefused`, when it has one, without letting it throw into the refusal. */
|
|
76
|
+
export function reportEgressRefusal(onEgressRefused, refusal) {
|
|
77
|
+
if (onEgressRefused === undefined)
|
|
78
|
+
return;
|
|
79
|
+
try {
|
|
80
|
+
onEgressRefused(refusal);
|
|
81
|
+
}
|
|
82
|
+
catch {
|
|
83
|
+
// Best-effort, as above.
|
|
84
|
+
}
|
|
85
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* GET `url` over `node:http` / `node:https`, connecting only to `addresses` —
|
|
3
|
+
* the answer the caller already checked — never to a fresh DNS answer, so a
|
|
4
|
+
* name that answers public first and private second (DNS rebinding) cannot
|
|
5
|
+
* move the socket. Global fetch cannot do this: it resolves the host again and
|
|
6
|
+
* takes no per-request `lookup`.
|
|
7
|
+
*
|
|
8
|
+
* - The URL's hostname still names the request: it is the `Host` header and,
|
|
9
|
+
* over TLS, the SNI name the certificate is verified against.
|
|
10
|
+
* - `agent: false` opens a fresh connection through the pinned `lookup` rather
|
|
11
|
+
* than reusing a pooled socket dialled under another answer.
|
|
12
|
+
* - Redirects come back as responses; the caller decides whether to follow one.
|
|
13
|
+
* - No `accept-encoding` is sent, so the body arrives as the server stored it.
|
|
14
|
+
*/
|
|
15
|
+
export declare function pinnedGet(url: URL, addresses: readonly string[], signal?: AbortSignal): Promise<Response>;
|
|
16
|
+
//# sourceMappingURL=pinned-get.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pinned-get.d.ts","sourceRoot":"","sources":["../../src/ctx/pinned-get.ts"],"names":[],"mappings":"AAQA;;;;;;;;;;;;;GAaG;AACH,wBAAgB,SAAS,CAAC,GAAG,EAAE,GAAG,EAAE,SAAS,EAAE,SAAS,MAAM,EAAE,EAAE,MAAM,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,QAAQ,CAAC,CAmCzG"}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { request as httpRequest } from 'node:http';
|
|
2
|
+
import { request as httpsRequest } from 'node:https';
|
|
3
|
+
import { Readable } from 'node:stream';
|
|
4
|
+
import { pinnedLookup } from './safe-fetch.js';
|
|
5
|
+
/** Response statuses that must not carry a body (the Response constructor throws otherwise). */
|
|
6
|
+
const NULL_BODY_STATUSES = new Set([204, 205, 304]);
|
|
7
|
+
/**
|
|
8
|
+
* GET `url` over `node:http` / `node:https`, connecting only to `addresses` —
|
|
9
|
+
* the answer the caller already checked — never to a fresh DNS answer, so a
|
|
10
|
+
* name that answers public first and private second (DNS rebinding) cannot
|
|
11
|
+
* move the socket. Global fetch cannot do this: it resolves the host again and
|
|
12
|
+
* takes no per-request `lookup`.
|
|
13
|
+
*
|
|
14
|
+
* - The URL's hostname still names the request: it is the `Host` header and,
|
|
15
|
+
* over TLS, the SNI name the certificate is verified against.
|
|
16
|
+
* - `agent: false` opens a fresh connection through the pinned `lookup` rather
|
|
17
|
+
* than reusing a pooled socket dialled under another answer.
|
|
18
|
+
* - Redirects come back as responses; the caller decides whether to follow one.
|
|
19
|
+
* - No `accept-encoding` is sent, so the body arrives as the server stored it.
|
|
20
|
+
*/
|
|
21
|
+
export function pinnedGet(url, addresses, signal) {
|
|
22
|
+
if (addresses.length === 0)
|
|
23
|
+
return Promise.reject(new Error(`no address to connect to for ${url.hostname}`));
|
|
24
|
+
const request = url.protocol === 'https:' ? httpsRequest : httpRequest;
|
|
25
|
+
const options = {
|
|
26
|
+
method: 'GET',
|
|
27
|
+
lookup: pinnedLookup(addresses),
|
|
28
|
+
agent: false,
|
|
29
|
+
...(signal ? { signal } : {}),
|
|
30
|
+
};
|
|
31
|
+
return new Promise((resolve, reject) => {
|
|
32
|
+
const req = request(url, options, (res) => {
|
|
33
|
+
// A throw in here (e.g. Response refusing a status) must reject the
|
|
34
|
+
// promise, not escape into the http module's event handling.
|
|
35
|
+
try {
|
|
36
|
+
const status = res.statusCode ?? 0;
|
|
37
|
+
// rawHeaders keeps repeated headers that the object form would join.
|
|
38
|
+
const headers = new Headers();
|
|
39
|
+
for (let i = 0; i + 1 < res.rawHeaders.length; i += 2) {
|
|
40
|
+
headers.append(res.rawHeaders[i], res.rawHeaders[i + 1]);
|
|
41
|
+
}
|
|
42
|
+
const nullBody = NULL_BODY_STATUSES.has(status);
|
|
43
|
+
if (nullBody)
|
|
44
|
+
res.resume();
|
|
45
|
+
// node:stream/web's ReadableStream and lib.dom's are the same object at
|
|
46
|
+
// runtime but distinct nominal types, so bridge through unknown.
|
|
47
|
+
const body = nullBody ? null : Readable.toWeb(res);
|
|
48
|
+
resolve(new Response(body, { status, statusText: res.statusMessage ?? '', headers }));
|
|
49
|
+
}
|
|
50
|
+
catch (err) {
|
|
51
|
+
res.resume();
|
|
52
|
+
reject(err);
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
req.on('error', reject);
|
|
56
|
+
req.end();
|
|
57
|
+
});
|
|
58
|
+
}
|
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
import { Readable } from 'node:stream';
|
|
2
|
+
import type { EgressRefusal } from './egress-refusal.js';
|
|
2
3
|
export interface CreateReadFileStreamOptions {
|
|
3
4
|
/** Resolves and sandboxes local paths. Same function injected as `ctx.safePath`. */
|
|
4
5
|
readonly safePath: (userPath: string) => string;
|
|
5
6
|
/** Aborts in-flight `http(s)://` fetches when the tool's invocation is cancelled. */
|
|
6
7
|
readonly signal: AbortSignal;
|
|
8
|
+
/** One call per URL refused for its blocked domain or a private address (egress-refusal.ts); buildCtx logs it. */
|
|
9
|
+
readonly onEgressRefused?: (refusal: EgressRefusal) => void;
|
|
7
10
|
}
|
|
8
11
|
/**
|
|
9
12
|
* Build a `ctx.readFileStream(value)` implementation — the streaming twin of
|
|
@@ -14,8 +17,8 @@ export interface CreateReadFileStreamOptions {
|
|
|
14
17
|
* in-process instead of on an isolated pod. Dispatches on the value's scheme
|
|
15
18
|
* exactly as `createReadFile`:
|
|
16
19
|
*
|
|
17
|
-
* - `http://` / `https://` →
|
|
18
|
-
*
|
|
20
|
+
* - `http://` / `https://` → the {@link fetchUrlInput} body as a `Readable`
|
|
21
|
+
* (no egress allowlist; a blocked-domain or private-address host is refused).
|
|
19
22
|
* - bare path (or `file://`) → open `safePath(value)`, then stream it.
|
|
20
23
|
*
|
|
21
24
|
* `safePath` escape attempts surface as `AkaiPathError`; HTTP failures as `Error`
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"read-file-stream.d.ts","sourceRoot":"","sources":["../../src/ctx/read-file-stream.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"read-file-stream.d.ts","sourceRoot":"","sources":["../../src/ctx/read-file-stream.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AACvC,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAGzD,MAAM,WAAW,2BAA2B;IAC1C,oFAAoF;IACpF,QAAQ,CAAC,QAAQ,EAAE,CAAC,QAAQ,EAAE,MAAM,KAAK,MAAM,CAAC;IAChD,qFAAqF;IACrF,QAAQ,CAAC,MAAM,EAAE,WAAW,CAAC;IAC7B,kHAAkH;IAClH,QAAQ,CAAC,eAAe,CAAC,EAAE,CAAC,OAAO,EAAE,aAAa,KAAK,IAAI,CAAC;CAC7D;AAED;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,oBAAoB,CAAC,IAAI,EAAE,2BAA2B,GAAG,CAAC,KAAK,EAAE,MAAM,KAAK,OAAO,CAAC,QAAQ,CAAC,CA6B5G"}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { open } from 'node:fs/promises';
|
|
2
2
|
import { Readable } from 'node:stream';
|
|
3
|
+
import { fetchUrlInput } from './url-input.js';
|
|
3
4
|
/**
|
|
4
5
|
* Build a `ctx.readFileStream(value)` implementation — the streaming twin of
|
|
5
6
|
* `ctx.readFile`. Returns a `Readable` instead of buffering the whole file, so a
|
|
@@ -9,8 +10,8 @@ import { Readable } from 'node:stream';
|
|
|
9
10
|
* in-process instead of on an isolated pod. Dispatches on the value's scheme
|
|
10
11
|
* exactly as `createReadFile`:
|
|
11
12
|
*
|
|
12
|
-
* - `http://` / `https://` →
|
|
13
|
-
*
|
|
13
|
+
* - `http://` / `https://` → the {@link fetchUrlInput} body as a `Readable`
|
|
14
|
+
* (no egress allowlist; a blocked-domain or private-address host is refused).
|
|
14
15
|
* - bare path (or `file://`) → open `safePath(value)`, then stream it.
|
|
15
16
|
*
|
|
16
17
|
* `safePath` escape attempts surface as `AkaiPathError`; HTTP failures as `Error`
|
|
@@ -28,14 +29,7 @@ export function createReadFileStream(opts) {
|
|
|
28
29
|
throw new Error('ctx.readFileStream: value must be a non-empty string');
|
|
29
30
|
}
|
|
30
31
|
if (value.startsWith('http://') || value.startsWith('https://')) {
|
|
31
|
-
|
|
32
|
-
try {
|
|
33
|
-
response = await fetch(value, { signal });
|
|
34
|
-
}
|
|
35
|
-
catch (err) {
|
|
36
|
-
const cause = err instanceof Error ? err.message : String(err);
|
|
37
|
-
throw new Error(`ctx.readFileStream: ${value}: ${cause}`);
|
|
38
|
-
}
|
|
32
|
+
const response = await fetchUrlInput(value, { label: 'ctx.readFileStream', signal, onEgressRefused: opts.onEgressRefused });
|
|
39
33
|
if (!response.ok) {
|
|
40
34
|
throw new Error(`ctx.readFileStream: ${value}: HTTP ${response.status} ${response.statusText}`);
|
|
41
35
|
}
|
package/dist/ctx/read-file.d.ts
CHANGED
|
@@ -1,16 +1,21 @@
|
|
|
1
|
+
import type { EgressRefusal } from './egress-refusal.js';
|
|
1
2
|
export interface CreateReadFileOptions {
|
|
2
3
|
/** Resolves and sandboxes local paths. Same function injected as `ctx.safePath`. */
|
|
3
4
|
readonly safePath: (userPath: string) => string;
|
|
4
5
|
/** Aborts in-flight `http(s)://` fetches when the tool's invocation is cancelled. */
|
|
5
6
|
readonly signal: AbortSignal;
|
|
7
|
+
/** One call per URL refused for its blocked domain or a private address (egress-refusal.ts); buildCtx logs it. */
|
|
8
|
+
readonly onEgressRefused?: (refusal: EgressRefusal) => void;
|
|
6
9
|
}
|
|
7
10
|
/**
|
|
8
11
|
* Build a `ctx.readFile(value)` implementation that dispatches on the value's
|
|
9
12
|
* scheme:
|
|
10
13
|
*
|
|
11
|
-
* - `http://` / `https://` →
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
+
* - `http://` / `https://` → fetched via {@link fetchUrlInput} → `Buffer`. The
|
|
15
|
+
* URL comes from the agent's tool argument, so there is no egress allowlist
|
|
16
|
+
* but the private-address rule of ctx.fetch applies: a host on the
|
|
17
|
+
* blocked-domain list, or one that resolves to a private address, is refused
|
|
18
|
+
* and reported (url-input.ts).
|
|
14
19
|
* - `file://` → strip prefix, then resolve via {@link CreateReadFileOptions.safePath}
|
|
15
20
|
* and read.
|
|
16
21
|
* - bare path → resolve via `safePath` and read.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"read-file.d.ts","sourceRoot":"","sources":["../../src/ctx/read-file.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"read-file.d.ts","sourceRoot":"","sources":["../../src/ctx/read-file.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAGzD,MAAM,WAAW,qBAAqB;IACpC,oFAAoF;IACpF,QAAQ,CAAC,QAAQ,EAAE,CAAC,QAAQ,EAAE,MAAM,KAAK,MAAM,CAAC;IAChD,qFAAqF;IACrF,QAAQ,CAAC,MAAM,EAAE,WAAW,CAAC;IAC7B,kHAAkH;IAClH,QAAQ,CAAC,eAAe,CAAC,EAAE,CAAC,OAAO,EAAE,aAAa,KAAK,IAAI,CAAC;CAC7D;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,cAAc,CAAC,IAAI,EAAE,qBAAqB,GAAG,CAAC,KAAK,EAAE,MAAM,KAAK,OAAO,CAAC,MAAM,CAAC,CAmB9F"}
|
package/dist/ctx/read-file.js
CHANGED
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
import { readFile as fsReadFile } from 'node:fs/promises';
|
|
2
|
+
import { fetchUrlInput } from './url-input.js';
|
|
2
3
|
/**
|
|
3
4
|
* Build a `ctx.readFile(value)` implementation that dispatches on the value's
|
|
4
5
|
* scheme:
|
|
5
6
|
*
|
|
6
|
-
* - `http://` / `https://` →
|
|
7
|
-
*
|
|
8
|
-
*
|
|
7
|
+
* - `http://` / `https://` → fetched via {@link fetchUrlInput} → `Buffer`. The
|
|
8
|
+
* URL comes from the agent's tool argument, so there is no egress allowlist
|
|
9
|
+
* but the private-address rule of ctx.fetch applies: a host on the
|
|
10
|
+
* blocked-domain list, or one that resolves to a private address, is refused
|
|
11
|
+
* and reported (url-input.ts).
|
|
9
12
|
* - `file://` → strip prefix, then resolve via {@link CreateReadFileOptions.safePath}
|
|
10
13
|
* and read.
|
|
11
14
|
* - bare path → resolve via `safePath` and read.
|
|
@@ -20,14 +23,7 @@ export function createReadFile(opts) {
|
|
|
20
23
|
throw new Error('ctx.readFile: value must be a non-empty string');
|
|
21
24
|
}
|
|
22
25
|
if (value.startsWith('http://') || value.startsWith('https://')) {
|
|
23
|
-
|
|
24
|
-
try {
|
|
25
|
-
response = await fetch(value, { signal });
|
|
26
|
-
}
|
|
27
|
-
catch (err) {
|
|
28
|
-
const cause = err instanceof Error ? err.message : String(err);
|
|
29
|
-
throw new Error(`ctx.readFile: ${value}: ${cause}`);
|
|
30
|
-
}
|
|
26
|
+
const response = await fetchUrlInput(value, { label: 'ctx.readFile', signal, onEgressRefused: opts.onEgressRefused });
|
|
31
27
|
if (!response.ok) {
|
|
32
28
|
throw new Error(`ctx.readFile: ${value}: HTTP ${response.status} ${response.statusText}`);
|
|
33
29
|
}
|
package/dist/ctx/safe-fetch.d.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
+
import { type LookupAddress } from 'node:dns';
|
|
1
2
|
import { type LookupFunction } from 'node:net';
|
|
3
|
+
import { type EgressRefusal } from './egress-refusal.js';
|
|
2
4
|
import { type NormalizedPrivateEgressPin, type PrivateEgressPin } from './private-egress.js';
|
|
3
5
|
/**
|
|
4
6
|
* Default per-response byte cap for JSON/text `ctx.fetch` calls (override via
|
|
@@ -65,8 +67,20 @@ export interface SafeFetchBinding {
|
|
|
65
67
|
readonly privateEgress?: readonly PrivateEgressPin[];
|
|
66
68
|
/** One call per ignored {@link privateEgress} entry, with a human-readable reason. */
|
|
67
69
|
readonly onPrivateEgressInvalid?: (message: string) => void;
|
|
70
|
+
/**
|
|
71
|
+
* One call per request refused for where it would go: a blocked domain, or a private address no
|
|
72
|
+
* operator pin allows (egress-refusal.ts). buildCtx logs it; a throw here never reaches the caller.
|
|
73
|
+
* `refusal.host` holds every label of the name: log it through `egressRefusalEntry`, never as is.
|
|
74
|
+
*/
|
|
75
|
+
readonly onEgressRefused?: (refusal: EgressRefusal) => void;
|
|
68
76
|
}
|
|
69
77
|
export type SafeFetch = (url: string, init?: RequestInit) => Promise<Response>;
|
|
78
|
+
/**
|
|
79
|
+
* Whether `ip` is an address no tool may reach: loopback, unspecified, RFC 1918, link-local (the
|
|
80
|
+
* metadata address), carrier-grade NAT, IETF protocol assignments, benchmarking, multicast and the
|
|
81
|
+
* reserved 240/4 with broadcast; for IPv6 also ULA, site-local, multicast, and any form that carries a
|
|
82
|
+
* private IPv4 address. Kept in step with the worker's src/cli/lib/safe-fetch.js (AKAI-4268).
|
|
83
|
+
*/
|
|
70
84
|
export declare function isPrivateIp(ip: string): boolean;
|
|
71
85
|
/** What {@link matchPrivatePin} found: the pin that allows the call, and the addresses it is pinned to. */
|
|
72
86
|
export interface PrivatePinMatch {
|
|
@@ -113,6 +127,7 @@ type RedirectMode = 'error' | 'follow' | 'manual';
|
|
|
113
127
|
* redirect`.
|
|
114
128
|
*/
|
|
115
129
|
export declare function resolveRedirect(requested: RedirectMode | undefined): RedirectMode;
|
|
130
|
+
export declare function lookupWithSignal(hostname: string, signal: AbortSignal | undefined): Promise<LookupAddress[]>;
|
|
116
131
|
export declare function createSafeFetch(binding: SafeFetchBinding): SafeFetch;
|
|
117
132
|
/**
|
|
118
133
|
* A `lookup` that answers with the addresses safe-fetch already checked, so the
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"safe-fetch.d.ts","sourceRoot":"","sources":["../../src/ctx/safe-fetch.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"safe-fetch.d.ts","sourceRoot":"","sources":["../../src/ctx/safe-fetch.ts"],"names":[],"mappings":"AAAA,OAAO,EAAmB,KAAK,aAAa,EAAE,MAAM,UAAU,CAAC;AAE/D,OAAO,EAAkB,KAAK,cAAc,EAAE,MAAM,UAAU,CAAC;AAK/D,OAAO,EAAuB,KAAK,aAAa,EAAE,MA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AAC,EAAE,WAAW,KAAK,OAAO,CAAC,QAAQ,CAAC,CAAC;AA6C/E;;;;;GAKG;AACH,wBAAgB,WAAW,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CA0B/C;AASD,2GAA2G;AAC3G,MAAM,WAAW,eAAe;IAC9B,QAAQ,CAAC,GAAG,EAAE,0BAA0B,CAAC;IACzC,+GAA+G;IAC/G,QAAQ,CAAC,SAAS,EAAE,SAAS,MAAM,EAAE,CAAC;CACvC;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,eAAe,CAC7B,IAAI,EAAE,SAAS,0BAA0B,EAAE,EAC3C,KAAK,EAAE,MAAM,EACb,GAAG,EAAE,GAAG,EACR,SAAS,EAAE,SAAS,MAAM,EAAE,GAC3B,eAAe,GAAG,IAAI,CAsBxB;AAED,6EAA6E;AAC7E,KAAK,YAAY,GAAG,OAAO,GAAG,QAAQ,GAAG,QAAQ,CAAC;AAElD;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,eAAe,CAAC,SAAS,EAAE,YAAY,GAAG,SAAS,GAAG,YAAY,CAEjF;AASD,wBAAsB,gBAAgB,CACpC,QAAQ,EAAE,MAAM,EAChB,MAAM,EAAE,WAAW,GAAG,SAAS,GAC9B,OAAO,CAAC,aAAa,EAAE,CAAC,CAgB1B;AAkBD,wBAAgB,eAAe,CAAC,OAAO,EAAE,gBAAgB,GAAG,SAAS,CAmJpE;AA6DD;;;;;GAKG;AACH,wBAAgB,YAAY,CAAC,SAAS,EAAE,SAAS,MAAM,EAAE,GAAG,cAAc,CASzE"}
|
package/dist/ctx/safe-fetch.js
CHANGED
|
@@ -4,6 +4,8 @@ import { isIPv4, isIPv6 } from 'node:net';
|
|
|
4
4
|
import { randomUUID } from 'node:crypto';
|
|
5
5
|
import { Readable } from 'node:stream';
|
|
6
6
|
import { AkaiEgressError, AkaiResponseTooLargeError } from '../errors.js';
|
|
7
|
+
import { blockedDomainOf } from './blocked-domains.js';
|
|
8
|
+
import { reportEgressRefusal } from './egress-refusal.js';
|
|
7
9
|
import { canonicalIp, cidrContains, isForbiddenPinAddress, normalizePrivateEgress, } from './private-egress.js';
|
|
8
10
|
import { ensureTrustedCa } from './trusted-ca.js';
|
|
9
11
|
/**
|
|
@@ -42,23 +44,44 @@ function canonicalizeIPv6(ip) {
|
|
|
42
44
|
return ip;
|
|
43
45
|
}
|
|
44
46
|
}
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
47
|
+
/** The eight 16-bit groups of a canonical (RFC 5952, hex-only) IPv6 address, or null. */
|
|
48
|
+
function ipv6Groups(v) {
|
|
49
|
+
const halves = v.split('::');
|
|
50
|
+
if (halves.length > 2)
|
|
51
|
+
return null;
|
|
52
|
+
const head = halves[0] ? halves[0].split(':') : [];
|
|
53
|
+
const tail = halves.length === 2 && halves[1] ? halves[1].split(':') : [];
|
|
54
|
+
const fill = halves.length === 2 ? Array(8 - head.length - tail.length).fill('0') : [];
|
|
55
|
+
const groups = [...head, ...fill, ...tail].map((g) => (/^[0-9a-f]{1,4}$/.test(g) ? parseInt(g, 16) : NaN));
|
|
56
|
+
return groups.length === 8 && groups.every((g) => Number.isInteger(g)) ? groups : null;
|
|
57
|
+
}
|
|
58
|
+
const v4FromGroups = (hi, lo) => `${hi >> 8}.${hi & 0xff}.${lo >> 8}.${lo & 0xff}`;
|
|
59
|
+
const startsWithGroups = (g, prefix) => prefix.every((v, i) => g[i] === v);
|
|
60
|
+
// IPv6 forms that carry an IPv4 address in their last 32 bits: IPv4-compatible (::/96, which also covers
|
|
61
|
+
// :: and ::1), IPv4-mapped (::ffff:0:0/96), IPv4-translated (::ffff:0:0:0/96) and NAT64 (64:ff9b::/96).
|
|
62
|
+
const V4_IN_LAST_TWO_GROUPS = [
|
|
63
|
+
[0, 0, 0, 0, 0, 0],
|
|
64
|
+
[0, 0, 0, 0, 0, 0xffff],
|
|
65
|
+
[0, 0, 0, 0, 0xffff, 0],
|
|
66
|
+
[0x64, 0xff9b, 0, 0, 0, 0],
|
|
67
|
+
];
|
|
68
|
+
/** The IPv4 address an IPv6 address carries — in its last 32 bits, or after a 6to4 2002::/16 prefix — or null. */
|
|
69
|
+
function embeddedIPv4(g) {
|
|
70
|
+
if (V4_IN_LAST_TWO_GROUPS.some((prefix) => startsWithGroups(g, prefix)))
|
|
71
|
+
return v4FromGroups(g[6], g[7]);
|
|
72
|
+
if (g[0] === 0x2002)
|
|
73
|
+
return v4FromGroups(g[1], g[2]);
|
|
55
74
|
return null;
|
|
56
75
|
}
|
|
76
|
+
/**
|
|
77
|
+
* Whether `ip` is an address no tool may reach: loopback, unspecified, RFC 1918, link-local (the
|
|
78
|
+
* metadata address), carrier-grade NAT, IETF protocol assignments, benchmarking, multicast and the
|
|
79
|
+
* reserved 240/4 with broadcast; for IPv6 also ULA, site-local, multicast, and any form that carries a
|
|
80
|
+
* private IPv4 address. Kept in step with the worker's src/cli/lib/safe-fetch.js (AKAI-4268).
|
|
81
|
+
*/
|
|
57
82
|
export function isPrivateIp(ip) {
|
|
58
83
|
if (isIPv4(ip)) {
|
|
59
|
-
const
|
|
60
|
-
const a = parts[0];
|
|
61
|
-
const b = parts[1];
|
|
84
|
+
const [a, b, c] = ip.split('.').map(Number);
|
|
62
85
|
if (a === 0 || a === 10 || a === 127)
|
|
63
86
|
return true;
|
|
64
87
|
if (a === 169 && b === 254)
|
|
@@ -67,21 +90,31 @@ export function isPrivateIp(ip) {
|
|
|
67
90
|
return true;
|
|
68
91
|
if (a === 192 && b === 168)
|
|
69
92
|
return true;
|
|
70
|
-
|
|
93
|
+
if (a === 100 && b >= 64 && b <= 127)
|
|
94
|
+
return true; // 100.64.0.0/10 carrier-grade NAT
|
|
95
|
+
if (a === 192 && b === 0 && c === 0)
|
|
96
|
+
return true; // 192.0.0.0/24 IETF protocol assignments
|
|
97
|
+
if (a === 198 && (b === 18 || b === 19))
|
|
98
|
+
return true; // 198.18.0.0/15 benchmarking
|
|
99
|
+
return a >= 224; // 224/4 multicast, 240/4 reserved, 255.255.255.255
|
|
71
100
|
}
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
if (
|
|
80
|
-
return
|
|
81
|
-
|
|
82
|
-
if (
|
|
83
|
-
return true;
|
|
84
|
-
|
|
101
|
+
// A zone id (`%eth0`) is not part of the address: WHATWG URL refuses it, so it is cut before canonicalizing.
|
|
102
|
+
const unscoped = ip.replace(/%.*$/, '');
|
|
103
|
+
if (isIPv6(unscoped)) {
|
|
104
|
+
const g = ipv6Groups(canonicalizeIPv6(unscoped).toLowerCase());
|
|
105
|
+
if (!g)
|
|
106
|
+
return true; // unparseable → fail closed
|
|
107
|
+
const embedded = embeddedIPv4(g);
|
|
108
|
+
if (embedded !== null)
|
|
109
|
+
return isPrivateIp(embedded);
|
|
110
|
+
const first = g[0];
|
|
111
|
+
if ((first & 0xfe00) === 0xfc00)
|
|
112
|
+
return true; // fc00::/7 unique local
|
|
113
|
+
if ((first & 0xffc0) === 0xfe80)
|
|
114
|
+
return true; // fe80::/10 link-local
|
|
115
|
+
if ((first & 0xffc0) === 0xfec0)
|
|
116
|
+
return true; // fec0::/10 site-local
|
|
117
|
+
return first >> 8 === 0xff; // ff00::/8 multicast
|
|
85
118
|
}
|
|
86
119
|
return false;
|
|
87
120
|
}
|
|
@@ -160,7 +193,7 @@ function abortError(signal) {
|
|
|
160
193
|
// `dns.lookup` has no native AbortSignal support. Race it so an abort during
|
|
161
194
|
// preflight surfaces promptly (and as the caller's signal.reason — matching
|
|
162
195
|
// fetch's contract) instead of being shadowed by a DNS result.
|
|
163
|
-
async function lookupWithSignal(hostname, signal) {
|
|
196
|
+
export async function lookupWithSignal(hostname, signal) {
|
|
164
197
|
if (!signal)
|
|
165
198
|
return dns.lookup(hostname, { all: true });
|
|
166
199
|
return new Promise((resolve, reject) => {
|
|
@@ -224,6 +257,14 @@ export function createSafeFetch(binding) {
|
|
|
224
257
|
egress,
|
|
225
258
|
});
|
|
226
259
|
}
|
|
260
|
+
// A host on the blocked-domain list is refused by name, whatever the egress
|
|
261
|
+
// allowlist says and in development too, before the lookup: resolving a DNS
|
|
262
|
+
// logger's name already sends it the data the name carries (blocked-domains.ts).
|
|
263
|
+
const blockedDomain = blockedDomainOf(u.hostname);
|
|
264
|
+
if (blockedDomain !== null) {
|
|
265
|
+
reportEgressRefusal(binding.onEgressRefused, { via: 'sdk_fetch', host: u.hostname, reason: 'blocked_domain' });
|
|
266
|
+
throw new AkaiEgressError(`safe-fetch: refusing ${u.hostname}: ${blockedDomain} is on Akai's blocked-domain list`, { hostname: u.hostname, blockedDomain });
|
|
267
|
+
}
|
|
227
268
|
let addresses;
|
|
228
269
|
try {
|
|
229
270
|
addresses = await lookupWithSignal(u.hostname, signal);
|
|
@@ -250,6 +291,7 @@ export function createSafeFetch(binding) {
|
|
|
250
291
|
pinned = matchPrivatePin(privatePins, cliId, u, addresses.map((entry) => entry.address));
|
|
251
292
|
if (pinned !== null)
|
|
252
293
|
break;
|
|
294
|
+
reportEgressRefusal(binding.onEgressRefused, { via: 'sdk_fetch', host: u.hostname, reason: 'private_address', address });
|
|
253
295
|
throw new AkaiEgressError(`safe-fetch: ${u.hostname} resolved to private address ${address}`, { hostname: u.hostname, address, allResolved: addresses.map((entry) => entry.address) });
|
|
254
296
|
}
|
|
255
297
|
}
|
package/dist/ctx/sftp.d.ts
CHANGED
|
@@ -27,6 +27,7 @@
|
|
|
27
27
|
* load it.
|
|
28
28
|
*/
|
|
29
29
|
import { Readable } from 'node:stream';
|
|
30
|
+
import { type EgressRefusal } from './egress-refusal.js';
|
|
30
31
|
import { type SftpEgressEntry } from './sftp-egress.js';
|
|
31
32
|
/** Credentials and target for {@link SftpApi.connect}. */
|
|
32
33
|
export interface SftpConnectOptions {
|
|
@@ -89,6 +90,8 @@ export interface SftpBinding {
|
|
|
89
90
|
readonly loadSsh2?: () => Promise<typeof import('ssh2')>;
|
|
90
91
|
/** Test seam: resolve a hostname to all its addresses. */
|
|
91
92
|
readonly resolve?: (hostname: string) => Promise<string[]>;
|
|
93
|
+
/** One call per host refused for a private address (egress-refusal.ts); buildCtx logs it. */
|
|
94
|
+
readonly onEgressRefused?: (refusal: EgressRefusal) => void;
|
|
92
95
|
}
|
|
93
96
|
/** For tools whose transport is not sftp: every call explains what to declare instead. */
|
|
94
97
|
export declare function unavailableSftp(): SftpApi;
|
package/dist/ctx/sftp.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sftp.d.ts","sourceRoot":"","sources":["../../src/ctx/sftp.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AAMH,OAAO,EAAE,QAAQ,EAAa,MAAM,aAAa,CAAC;AAYlD,OAAO,EAAyB,KAAK,eAAe,EAAwB,MAAM,kBAAkB,CAAC;AAErG,0DAA0D;AAC1D,MAAM,WAAW,kBAAkB;IACjC,mFAAmF;IACnF,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC;IACvB,iFAAiF;IACjF,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,QAAQ,CAAC,QAAQ,CAAC,EAAE,MAAM,CAAC;IAC3B,gGAAgG;IAChG,QAAQ,CAAC,UAAU,CAAC,EAAE,MAAM,CAAC;IAC7B,QAAQ,CAAC,UAAU,CAAC,EAAE,MAAM,CAAC;CAC9B;AAED,MAAM,MAAM,aAAa,GAAG,MAAM,GAAG,WAAW,GAAG,SAAS,GAAG,OAAO,CAAC;AAEvE,MAAM,WAAW,SAAS;IACxB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,IAAI,EAAE,aAAa,CAAC;IAC7B,QAAQ,CAAC,IAAI,E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C,OAAO,EAAE,aAAa,KAAK,IAAI,CAAC;CAC7D;AA4BD,0FAA0F;AAC1F,wBAAgB,eAAe,IAAI,OAAO,CASzC;AAED,wBAAgB,UAAU,CAAC,OAAO,EAAE,WAAW,GAAG,OAAO,CA8FxD;AAqDD;;;;;GAKG;AACH,wBAAgB,sBAAsB,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAO1D"}
|
package/dist/ctx/sftp.js
CHANGED
|
@@ -34,6 +34,7 @@ import { Readable, Transform } from 'node:stream';
|
|
|
34
34
|
import { pipeline } from 'node:stream/promises';
|
|
35
35
|
import { AkaiEgressError, AkaiInputError, AkaiResponseTooLargeError, AkaiSecretError, AkaiToolError, } from '../errors.js';
|
|
36
36
|
import { isPrivateIp } from './safe-fetch.js';
|
|
37
|
+
import { reportEgressRefusal } from './egress-refusal.js';
|
|
37
38
|
import { formatSftpEgressEntry } from './sftp-egress.js';
|
|
38
39
|
const READY_TIMEOUT_MS = 20_000;
|
|
39
40
|
/**
|
|
@@ -243,6 +244,7 @@ async function resolvePublicAddress(host, binding) {
|
|
|
243
244
|
if (process.env.NODE_ENV !== 'development') {
|
|
244
245
|
for (const address of addresses) {
|
|
245
246
|
if (isPrivateIp(address)) {
|
|
247
|
+
reportEgressRefusal(binding.onEgressRefused, { via: 'sdk_sftp', host, reason: 'private_address', address });
|
|
246
248
|
throw new AkaiEgressError(`sftp: ${host} resolved to private address ${address}`, { hostname: host, address, allResolved: addresses });
|
|
247
249
|
}
|
|
248
250
|
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { type EgressRefusal } from './egress-refusal.js';
|
|
2
|
+
/** How many redirects one URL input may follow. Each hop is checked like the first. */
|
|
3
|
+
export declare const MAX_URL_INPUT_REDIRECTS = 5;
|
|
4
|
+
export interface UrlInputOptions {
|
|
5
|
+
/** Prefix for every error, e.g. `ctx.readFile`. */
|
|
6
|
+
readonly label: string;
|
|
7
|
+
/** Aborts the lookup and the request when the tool's invocation is cancelled. */
|
|
8
|
+
readonly signal: AbortSignal;
|
|
9
|
+
/** One call per refused host (egress-refusal.ts); buildCtx logs it as `egress.refused`. */
|
|
10
|
+
readonly onEgressRefused?: (refusal: EgressRefusal) => void;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Fetch an `http(s)://` file input for `ctx.readFile` / `ctx.readFileStream`
|
|
14
|
+
* and return the final response, whatever its status — the caller reads it.
|
|
15
|
+
*
|
|
16
|
+
* The URL is whatever the agent passed for the file argument: the worker hands
|
|
17
|
+
* an `http(s)://` value to the tool unchanged. There is no per-tool egress
|
|
18
|
+
* allowlist here — a file can live anywhere public — but the URL may not reach
|
|
19
|
+
* inside Akai's network, the same rule ctx.fetch and ctx.sftp apply:
|
|
20
|
+
*
|
|
21
|
+
* - a host on the blocked-domain list is refused by name, before any lookup;
|
|
22
|
+
* - the host is resolved and EVERY address checked: one private address
|
|
23
|
+
* (loopback, RFC 1918, link-local incl. the cloud metadata address, cluster
|
|
24
|
+
* service ranges, IPv6 ULA, …) refuses the call;
|
|
25
|
+
* - the connection goes only to the addresses that were checked (`pinnedGet`),
|
|
26
|
+
* so a second DNS answer cannot redirect it;
|
|
27
|
+
* - a redirect is followed by hand, at most {@link MAX_URL_INPUT_REDIRECTS}
|
|
28
|
+
* times, and every hop is checked the same way before it is dialled.
|
|
29
|
+
*
|
|
30
|
+
* Each refusal is reported through `onEgressRefused` as `sdk_read_file`, with
|
|
31
|
+
* the host and never the URL (a presigned URL carries its signature).
|
|
32
|
+
*
|
|
33
|
+
* With `NODE_ENV=development` only the blocked-domain check applies and the
|
|
34
|
+
* request goes through global fetch, so the local stack's object store on
|
|
35
|
+
* localhost keeps working.
|
|
36
|
+
*/
|
|
37
|
+
export declare function fetchUrlInput(value: string, opts: UrlInputOptions): Promise<Response>;
|
|
38
|
+
//# sourceMappingURL=url-input.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"url-input.d.ts","sourceRoot":"","sources":["../../src/ctx/url-input.ts"],"names":[],"mappings":"AAEA,OAAO,EAAuB,KAAK,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAI9E,uFAAuF;AACvF,eAAO,MAAM,uBAAuB,IAAI,CAAC;AAIzC,MAAM,WAAW,eAAe;IAC9B,mDAAmD;IACnD,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,iFAAiF;IACjF,QAAQ,CAAC,MAAM,EAAE,WAAW,CAAC;IAC7B,2FAA2F;IAC3F,QAAQ,CAAC,eAAe,CAAC,EAAE,CAAC,OAAO,EAAE,aAAa,KAAK,IAAI,CAAC;CAC7D;AAMD;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,wBAAsB,aAAa,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,eAAe,GAAG,OAAO,CAAC,QAAQ,CAAC,CAsE3F"}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { blockedDomainOf } from './blocked-domains.js';
|
|
2
|
+
import { reportEgressRefusal } from './egress-refusal.js';
|
|
3
|
+
import { pinnedGet } from './pinned-get.js';
|
|
4
|
+
import { isPrivateIp, lookupWithSignal } from './safe-fetch.js';
|
|
5
|
+
/** How many redirects one URL input may follow. Each hop is checked like the first. */
|
|
6
|
+
export const MAX_URL_INPUT_REDIRECTS = 5;
|
|
7
|
+
const REDIRECT_STATUSES = new Set([301, 302, 303, 307, 308]);
|
|
8
|
+
function causeOf(err) {
|
|
9
|
+
return err instanceof Error ? err.message : String(err);
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Fetch an `http(s)://` file input for `ctx.readFile` / `ctx.readFileStream`
|
|
13
|
+
* and return the final response, whatever its status — the caller reads it.
|
|
14
|
+
*
|
|
15
|
+
* The URL is whatever the agent passed for the file argument: the worker hands
|
|
16
|
+
* an `http(s)://` value to the tool unchanged. There is no per-tool egress
|
|
17
|
+
* allowlist here — a file can live anywhere public — but the URL may not reach
|
|
18
|
+
* inside Akai's network, the same rule ctx.fetch and ctx.sftp apply:
|
|
19
|
+
*
|
|
20
|
+
* - a host on the blocked-domain list is refused by name, before any lookup;
|
|
21
|
+
* - the host is resolved and EVERY address checked: one private address
|
|
22
|
+
* (loopback, RFC 1918, link-local incl. the cloud metadata address, cluster
|
|
23
|
+
* service ranges, IPv6 ULA, …) refuses the call;
|
|
24
|
+
* - the connection goes only to the addresses that were checked (`pinnedGet`),
|
|
25
|
+
* so a second DNS answer cannot redirect it;
|
|
26
|
+
* - a redirect is followed by hand, at most {@link MAX_URL_INPUT_REDIRECTS}
|
|
27
|
+
* times, and every hop is checked the same way before it is dialled.
|
|
28
|
+
*
|
|
29
|
+
* Each refusal is reported through `onEgressRefused` as `sdk_read_file`, with
|
|
30
|
+
* the host and never the URL (a presigned URL carries its signature).
|
|
31
|
+
*
|
|
32
|
+
* With `NODE_ENV=development` only the blocked-domain check applies and the
|
|
33
|
+
* request goes through global fetch, so the local stack's object store on
|
|
34
|
+
* localhost keeps working.
|
|
35
|
+
*/
|
|
36
|
+
export async function fetchUrlInput(value, opts) {
|
|
37
|
+
const { label, signal } = opts;
|
|
38
|
+
const fail = (why) => new Error(`${label}: ${value}: ${why}`);
|
|
39
|
+
let url;
|
|
40
|
+
try {
|
|
41
|
+
url = new URL(value);
|
|
42
|
+
}
|
|
43
|
+
catch {
|
|
44
|
+
throw fail('not a valid URL');
|
|
45
|
+
}
|
|
46
|
+
const refuseBlockedDomain = (hop) => {
|
|
47
|
+
const blocked = blockedDomainOf(hop.hostname);
|
|
48
|
+
if (blocked === null)
|
|
49
|
+
return;
|
|
50
|
+
reportEgressRefusal(opts.onEgressRefused, { via: 'sdk_read_file', host: hop.hostname, reason: 'blocked_domain' });
|
|
51
|
+
throw fail(`${blocked} is on Akai's blocked-domain list`);
|
|
52
|
+
};
|
|
53
|
+
if (process.env.NODE_ENV === 'development') {
|
|
54
|
+
refuseBlockedDomain(url);
|
|
55
|
+
try {
|
|
56
|
+
return await fetch(value, { signal });
|
|
57
|
+
}
|
|
58
|
+
catch (err) {
|
|
59
|
+
throw fail(causeOf(err));
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
for (let redirects = 0;; redirects++) {
|
|
63
|
+
if (url.protocol !== 'http:' && url.protocol !== 'https:') {
|
|
64
|
+
throw fail(`refusing a redirect to a ${url.protocol} URL`);
|
|
65
|
+
}
|
|
66
|
+
refuseBlockedDomain(url);
|
|
67
|
+
// WHATWG URL keeps an IPv6 literal's brackets; the resolver wants the bare address.
|
|
68
|
+
const hostname = url.hostname.replace(/^\[|\]$/g, '');
|
|
69
|
+
let addresses;
|
|
70
|
+
try {
|
|
71
|
+
addresses = await lookupWithSignal(hostname, signal);
|
|
72
|
+
}
|
|
73
|
+
catch (err) {
|
|
74
|
+
throw fail(signal.aborted ? causeOf(err) : `DNS lookup failed for ${url.hostname}: ${causeOf(err)}`);
|
|
75
|
+
}
|
|
76
|
+
for (const { address } of addresses) {
|
|
77
|
+
if (isPrivateIp(address)) {
|
|
78
|
+
reportEgressRefusal(opts.onEgressRefused, {
|
|
79
|
+
via: 'sdk_read_file',
|
|
80
|
+
host: url.hostname,
|
|
81
|
+
reason: 'private_address',
|
|
82
|
+
address,
|
|
83
|
+
});
|
|
84
|
+
throw fail(`${url.hostname} resolves to the private address ${address}`);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
let response;
|
|
88
|
+
try {
|
|
89
|
+
response = await pinnedGet(url, addresses.map((entry) => entry.address), signal);
|
|
90
|
+
}
|
|
91
|
+
catch (err) {
|
|
92
|
+
throw fail(causeOf(err));
|
|
93
|
+
}
|
|
94
|
+
const location = response.headers.get('location');
|
|
95
|
+
if (!REDIRECT_STATUSES.has(response.status) || location === null)
|
|
96
|
+
return response;
|
|
97
|
+
await response.body?.cancel().catch(() => { });
|
|
98
|
+
if (redirects >= MAX_URL_INPUT_REDIRECTS)
|
|
99
|
+
throw fail(`more than ${MAX_URL_INPUT_REDIRECTS} redirects`);
|
|
100
|
+
try {
|
|
101
|
+
url = new URL(location, url);
|
|
102
|
+
}
|
|
103
|
+
catch {
|
|
104
|
+
throw fail(`HTTP ${response.status} redirects to an invalid URL`);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
package/dist/file-input.d.ts
CHANGED
|
@@ -13,10 +13,9 @@ export declare const FILE_INPUT_META_KEY = "x-akai-file-input";
|
|
|
13
13
|
* - An absolute path (resolved via `ctx.safePath`).
|
|
14
14
|
* - A relative path (resolved against the worker's `workdir` allowlist).
|
|
15
15
|
* - A `file://` URL (prefix stripped, then resolved as a path).
|
|
16
|
-
* - An `http(s)://` URL
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
* fetches without an egress allowlist.
|
|
16
|
+
* - An `http(s)://` URL, as the agent passed it. `ctx.readFile` fetches it
|
|
17
|
+
* without an egress allowlist but refuses a blocked-domain host and any
|
|
18
|
+
* host that resolves to a private address.
|
|
20
19
|
*
|
|
21
20
|
* The schema is a plain `z.string().min(1)` (so existing path-only
|
|
22
21
|
* consumers keep working), branded with `x-akai-file-input: true` in its
|
package/dist/file-input.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"file-input.d.ts","sourceRoot":"","sources":["../src/file-input.ts"],"names":[],"mappings":"AAAA,OAAO,EAAK,KAAK,SAAS,EAAE,MAAM,KAAK,CAAC;AAExC;;;;;GAKG;AACH,eAAO,MAAM,mBAAmB,sBAAsB,CAAC;AAEvD
|
|
1
|
+
{"version":3,"file":"file-input.d.ts","sourceRoot":"","sources":["../src/file-input.ts"],"names":[],"mappings":"AAAA,OAAO,EAAK,KAAK,SAAS,EAAE,MAAM,KAAK,CAAC;AAExC;;;;;GAKG;AACH,eAAO,MAAM,mBAAmB,sBAAsB,CAAC;AAEvD;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,QAAQ,IAAI,SAAS,CAEpC"}
|
package/dist/file-input.js
CHANGED
|
@@ -13,10 +13,9 @@ export const FILE_INPUT_META_KEY = 'x-akai-file-input';
|
|
|
13
13
|
* - An absolute path (resolved via `ctx.safePath`).
|
|
14
14
|
* - A relative path (resolved against the worker's `workdir` allowlist).
|
|
15
15
|
* - A `file://` URL (prefix stripped, then resolved as a path).
|
|
16
|
-
* - An `http(s)://` URL
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
* fetches without an egress allowlist.
|
|
16
|
+
* - An `http(s)://` URL, as the agent passed it. `ctx.readFile` fetches it
|
|
17
|
+
* without an egress allowlist but refuses a blocked-domain host and any
|
|
18
|
+
* host that resolves to a private address.
|
|
20
19
|
*
|
|
21
20
|
* The schema is a plain `z.string().min(1)` (so existing path-only
|
|
22
21
|
* consumers keep working), branded with `x-akai-file-input: true` in its
|
package/dist/types.d.ts
CHANGED
|
@@ -164,9 +164,9 @@ export interface ToolCtx<S extends string = string, P extends string = string> {
|
|
|
164
164
|
safePath: (userPath: string) => string;
|
|
165
165
|
/**
|
|
166
166
|
* Read a file referenced by either a local path (resolved via {@link safePath})
|
|
167
|
-
* or an `http(s)://` URL (fetched
|
|
168
|
-
*
|
|
169
|
-
*
|
|
167
|
+
* or an `http(s)://` URL (fetched with no egress allowlist, but a host on the
|
|
168
|
+
* blocked-domain list or one that resolves to a private address is refused,
|
|
169
|
+
* as in {@link fetch}; the URL is whatever the agent passed). Strips `file://`
|
|
170
170
|
* before resolving as a local path.
|
|
171
171
|
*
|
|
172
172
|
* Pairs with `filePath()` from the SDK: tool authors switch
|