@akai-workflow-builder/cli-sdk 0.18.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.
Files changed (42) hide show
  1. package/CHANGELOG.md +27 -0
  2. package/dist/ctx/blocked-domains.d.ts +23 -0
  3. package/dist/ctx/blocked-domains.d.ts.map +1 -0
  4. package/dist/ctx/blocked-domains.js +59 -0
  5. package/dist/ctx/build-ctx.d.ts.map +1 -1
  6. package/dist/ctx/build-ctx.js +9 -3
  7. package/dist/ctx/egress-refusal.d.ts +47 -0
  8. package/dist/ctx/egress-refusal.d.ts.map +1 -0
  9. package/dist/ctx/egress-refusal.js +85 -0
  10. package/dist/ctx/pinned-get.d.ts +16 -0
  11. package/dist/ctx/pinned-get.d.ts.map +1 -0
  12. package/dist/ctx/pinned-get.js +58 -0
  13. package/dist/ctx/read-file-stream.d.ts +5 -2
  14. package/dist/ctx/read-file-stream.d.ts.map +1 -1
  15. package/dist/ctx/read-file-stream.js +4 -10
  16. package/dist/ctx/read-file.d.ts +8 -3
  17. package/dist/ctx/read-file.d.ts.map +1 -1
  18. package/dist/ctx/read-file.js +7 -11
  19. package/dist/ctx/safe-fetch.d.ts +15 -0
  20. package/dist/ctx/safe-fetch.d.ts.map +1 -1
  21. package/dist/ctx/safe-fetch.js +70 -28
  22. package/dist/ctx/sftp-egress.d.ts +33 -6
  23. package/dist/ctx/sftp-egress.d.ts.map +1 -1
  24. package/dist/ctx/sftp-egress.js +86 -10
  25. package/dist/ctx/sftp.d.ts +6 -1
  26. package/dist/ctx/sftp.d.ts.map +1 -1
  27. package/dist/ctx/sftp.js +38 -2
  28. package/dist/ctx/url-input.d.ts +38 -0
  29. package/dist/ctx/url-input.d.ts.map +1 -0
  30. package/dist/ctx/url-input.js +107 -0
  31. package/dist/file-input.d.ts +3 -4
  32. package/dist/file-input.d.ts.map +1 -1
  33. package/dist/file-input.js +3 -4
  34. package/dist/index.d.ts +2 -2
  35. package/dist/index.d.ts.map +1 -1
  36. package/dist/index.js +1 -1
  37. package/dist/manifest/to-schema.js +10 -4
  38. package/dist/manifest/types.d.ts +1 -1
  39. package/dist/runtime/executor/index.js +1 -1
  40. package/dist/runtime/registry/wire.js +8 -4
  41. package/dist/types.d.ts +4 -4
  42. package/package.json +1 -1
@@ -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
- function mappedIPv4(ipv6Lower) {
46
- const dotted = ipv6Lower.match(/^::ffff:(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$/);
47
- if (dotted)
48
- return dotted[1];
49
- const hex = ipv6Lower.match(/^::ffff:([0-9a-f]{1,4}):([0-9a-f]{1,4})$/);
50
- if (hex) {
51
- const hi = parseInt(hex[1], 16);
52
- const lo = parseInt(hex[2], 16);
53
- return `${(hi >> 8) & 0xff}.${hi & 0xff}.${(lo >> 8) & 0xff}.${lo & 0xff}`;
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 parts = ip.split('.').map(Number);
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
- return false;
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
- if (isIPv6(ip)) {
73
- const v = canonicalizeIPv6(ip).toLowerCase();
74
- const peeled = mappedIPv4(v);
75
- if (peeled)
76
- return isPrivateIp(peeled);
77
- if (v === '::' || v === '::1')
78
- return true;
79
- if (v.startsWith('fc') || v.startsWith('fd'))
80
- return true;
81
- // fe80::/10 — link-local; second hextet's top 2 bits are 10 → 8/9/a/b.
82
- if (v.startsWith('fe8') || v.startsWith('fe9') || v.startsWith('fea') || v.startsWith('feb'))
83
- return true;
84
- return false;
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
  }
@@ -3,27 +3,50 @@
3
3
  *
4
4
  * An http tool's egress is a list of hostnames; an sftp tool also needs the
5
5
  * port (SFTP servers commonly listen somewhere other than 22) and, optionally,
6
- * the server's pinned host key. Each allowlist entry therefore reads:
6
+ * the server's pinned host key. Each allowlist entry reads one of:
7
7
  *
8
- * host[:port][ SHA256:<fingerprint>]
8
+ * host[:port]
9
+ * host[:port] SHA256:<fingerprint>
10
+ * host[:port] <keyType> SHA256:<fingerprint>
11
+ * host[:port] <keyType> <base64 public key> [comment...]
9
12
  *
10
13
  * e.g. `sftp.example.com`, `files.acme.io:2222`,
11
- * `203.0.113.7:22 SHA256:47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU`. An
14
+ * `203.0.113.7:22 SHA256:47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU`,
15
+ * `sftp.acme.com:22 ssh-rsa SHA256:47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU`,
16
+ * `sftp.acme.com ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAA… root@sftp`. An
12
17
  * `sftp://` prefix is accepted and dropped. The port defaults to 22. The
13
18
  * fingerprint is the one OpenSSH prints (`ssh-keygen -lf`): `SHA256:` and the
14
19
  * unpadded base64 of the SHA-256 of the server's public host key.
15
20
  *
21
+ * A server usually holds several host keys (ed25519, ecdsa, rsa) and presents
22
+ * whichever one the client's host-key algorithm negotiation lands on, so a
23
+ * fingerprint alone may pin a key the server never shows us. The key type
24
+ * (`ssh-ed25519`, `ssh-rsa`, `ecdsa-sha2-nistp256|384|521`) tells the client
25
+ * which host-key algorithm to offer, so the server presents the pinned key.
26
+ * The last form takes a whole OpenSSH public-key line (what `ssh-keyscan` and
27
+ * provider dashboards print): its fingerprint is computed, the key type inside
28
+ * the blob must match the stated one, and it canonicalises to the
29
+ * `<keyType> SHA256:<fingerprint>` form.
30
+ *
16
31
  * The allowlist lives in the egress binding — a property the host runtime
17
32
  * injects — never in tool input, so a handler cannot widen it or drop a pin.
18
33
  * Parsing is all or nothing: one bad entry rejects the whole value, the same
19
34
  * fail-closed rule http's dynamic egress follows.
20
35
  */
36
+ /** Host-key types an entry may pin, as OpenSSH names them in a public-key line. */
37
+ export declare const SFTP_HOST_KEY_TYPES: readonly ["ssh-ed25519", "ssh-rsa", "ecdsa-sha2-nistp256", "ecdsa-sha2-nistp384", "ecdsa-sha2-nistp521"];
38
+ export type SftpHostKeyType = (typeof SFTP_HOST_KEY_TYPES)[number];
21
39
  export interface SftpEgressEntry {
22
40
  /** Lowercase hostname or IPv4 literal. */
23
41
  readonly host: string;
24
42
  readonly port: number;
25
43
  /** `SHA256:<43 base64 chars>` when the entry pins the server's host key. */
26
44
  readonly hostKeySha256?: string;
45
+ /**
46
+ * The pinned key's type, when the entry names it. Only set alongside
47
+ * `hostKeySha256`; the client then offers only this host-key algorithm.
48
+ */
49
+ readonly hostKeyType?: SftpHostKeyType;
27
50
  }
28
51
  export declare const DEFAULT_SFTP_PORT = 22;
29
52
  /**
@@ -32,16 +55,20 @@ export declare const DEFAULT_SFTP_PORT = 22;
32
55
  * Returns undefined for anything else.
33
56
  */
34
57
  export declare function normalizeHostKeyFingerprint(raw: string): string | undefined;
58
+ export declare function isSftpHostKeyType(value: string): value is SftpHostKeyType;
35
59
  /** Parse one allowlist entry, or return undefined when it is not valid. */
36
60
  export declare function parseSftpEgressEntry(raw: string): SftpEgressEntry | undefined;
37
61
  /**
38
62
  * Parse a whole allowlist value (entries separated by commas or newlines).
39
63
  * Returns undefined when it holds no entry or any entry is invalid. An entry
40
64
  * repeated for the same host and port keeps its first appearance; two entries
41
- * for one host and port that disagree on the pin are invalid, since either
42
- * reading would silently drop a pin someone set.
65
+ * for one host and port that disagree on the pin or its key type are invalid,
66
+ * since either reading would silently drop a pin someone set.
43
67
  */
44
68
  export declare function parseSftpEgress(raw: string): SftpEgressEntry[] | undefined;
45
- /** The canonical text of an entry: `host:port`, plus ` SHA256:…` when pinned. */
69
+ /**
70
+ * The canonical text of an entry: `host:port`, plus ` SHA256:…` when pinned,
71
+ * or ` <keyType> SHA256:…` when the pin names its key type.
72
+ */
46
73
  export declare function formatSftpEgressEntry(entry: SftpEgressEntry): string;
47
74
  //# sourceMappingURL=sftp-egress.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"sftp-egress.d.ts","sourceRoot":"","sources":["../../src/ctx/sftp-egress.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AAIH,MAAM,WAAW,eAAe;IAC9B,0CA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;AAED,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,MAAM,GAAG,KAAK,IAAI,eAAe,CAEzE;AAmBD,2EAA2E;AAC3E,wBAAgB,oBAAoB,CAAC,GAAG,EAAE,MAAM,GAAG,eAAe,GAAG,SAAS,CA4C7E;AAED;;;;;;GAMG;AACH,wBAAgB,eAAe,CAAC,GAAG,EAAE,MAAM,GAAG,eAAe,EAAE,GAAG,SAAS,CAc1E;AAED;;;GAGG;AACH,wBAAgB,qBAAqB,CAAC,KAAK,EAAE,eAAe,GAAG,MAAM,CAMpE"}
@@ -3,22 +3,46 @@
3
3
  *
4
4
  * An http tool's egress is a list of hostnames; an sftp tool also needs the
5
5
  * port (SFTP servers commonly listen somewhere other than 22) and, optionally,
6
- * the server's pinned host key. Each allowlist entry therefore reads:
6
+ * the server's pinned host key. Each allowlist entry reads one of:
7
7
  *
8
- * host[:port][ SHA256:<fingerprint>]
8
+ * host[:port]
9
+ * host[:port] SHA256:<fingerprint>
10
+ * host[:port] <keyType> SHA256:<fingerprint>
11
+ * host[:port] <keyType> <base64 public key> [comment...]
9
12
  *
10
13
  * e.g. `sftp.example.com`, `files.acme.io:2222`,
11
- * `203.0.113.7:22 SHA256:47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU`. An
14
+ * `203.0.113.7:22 SHA256:47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU`,
15
+ * `sftp.acme.com:22 ssh-rsa SHA256:47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU`,
16
+ * `sftp.acme.com ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAA… root@sftp`. An
12
17
  * `sftp://` prefix is accepted and dropped. The port defaults to 22. The
13
18
  * fingerprint is the one OpenSSH prints (`ssh-keygen -lf`): `SHA256:` and the
14
19
  * unpadded base64 of the SHA-256 of the server's public host key.
15
20
  *
21
+ * A server usually holds several host keys (ed25519, ecdsa, rsa) and presents
22
+ * whichever one the client's host-key algorithm negotiation lands on, so a
23
+ * fingerprint alone may pin a key the server never shows us. The key type
24
+ * (`ssh-ed25519`, `ssh-rsa`, `ecdsa-sha2-nistp256|384|521`) tells the client
25
+ * which host-key algorithm to offer, so the server presents the pinned key.
26
+ * The last form takes a whole OpenSSH public-key line (what `ssh-keyscan` and
27
+ * provider dashboards print): its fingerprint is computed, the key type inside
28
+ * the blob must match the stated one, and it canonicalises to the
29
+ * `<keyType> SHA256:<fingerprint>` form.
30
+ *
16
31
  * The allowlist lives in the egress binding — a property the host runtime
17
32
  * injects — never in tool input, so a handler cannot widen it or drop a pin.
18
33
  * Parsing is all or nothing: one bad entry rejects the whole value, the same
19
34
  * fail-closed rule http's dynamic egress follows.
20
35
  */
36
+ import { createHash } from 'node:crypto';
21
37
  import { isIPv4 } from 'node:net';
38
+ /** Host-key types an entry may pin, as OpenSSH names them in a public-key line. */
39
+ export const SFTP_HOST_KEY_TYPES = [
40
+ 'ssh-ed25519',
41
+ 'ssh-rsa',
42
+ 'ecdsa-sha2-nistp256',
43
+ 'ecdsa-sha2-nistp384',
44
+ 'ecdsa-sha2-nistp521',
45
+ ];
22
46
  export const DEFAULT_SFTP_PORT = 22;
23
47
  // RFC 1123 labels; at least one dot; last label starts with a letter so a bare
24
48
  // number never reads as a host. IPv4 literals are matched separately.
@@ -35,17 +59,60 @@ export function normalizeHostKeyFingerprint(raw) {
35
59
  const m = FINGERPRINT_RE.exec(value.startsWith('SHA256:') ? value : `SHA256:${value}`);
36
60
  return m ? `SHA256:${m[1]}` : undefined;
37
61
  }
62
+ export function isSftpHostKeyType(value) {
63
+ return SFTP_HOST_KEY_TYPES.includes(value);
64
+ }
65
+ /**
66
+ * The fingerprint of an OpenSSH public-key blob (the base64 field of a
67
+ * `ssh-ed25519 AAAA…` line), checking that the key type embedded in the blob —
68
+ * its first SSH string — is `keyType`. Returns undefined when the blob is not
69
+ * valid base64, is truncated, or names another type.
70
+ */
71
+ function fingerprintPublicKeyBlob(keyType, base64) {
72
+ if (!/^[A-Za-z0-9+/]+={0,2}$/.test(base64) || base64.length % 4 !== 0)
73
+ return undefined;
74
+ const blob = Buffer.from(base64, 'base64');
75
+ if (blob.length < 4)
76
+ return undefined;
77
+ const typeLength = blob.readUInt32BE(0);
78
+ // The type string, then at least some key material after it.
79
+ if (typeLength === 0 || 4 + typeLength >= blob.length)
80
+ return undefined;
81
+ if (blob.subarray(4, 4 + typeLength).toString('latin1') !== keyType)
82
+ return undefined;
83
+ return `SHA256:${createHash('sha256').update(blob).digest('base64').replace(/=+$/, '')}`;
84
+ }
38
85
  /** Parse one allowlist entry, or return undefined when it is not valid. */
39
86
  export function parseSftpEgressEntry(raw) {
40
87
  const parts = raw.trim().split(/\s+/);
41
- if (parts.length === 0 || parts.length > 2 || parts[0] === '')
88
+ if (parts.length === 0 || parts[0] === '')
42
89
  return undefined;
43
90
  let hostKeySha256;
91
+ let hostKeyType;
44
92
  if (parts.length === 2) {
93
+ // host SHA256:<fp>
45
94
  hostKeySha256 = normalizeHostKeyFingerprint(parts[1]);
46
95
  if (hostKeySha256 === undefined)
47
96
  return undefined;
48
97
  }
98
+ else if (parts.length >= 3) {
99
+ // host <keyType> SHA256:<fp> | host <keyType> <blob> [comment...]
100
+ const keyType = parts[1];
101
+ if (!isSftpHostKeyType(keyType))
102
+ return undefined;
103
+ hostKeyType = keyType;
104
+ const key = parts[2];
105
+ if (key.startsWith('SHA256:')) {
106
+ if (parts.length > 3)
107
+ return undefined;
108
+ hostKeySha256 = normalizeHostKeyFingerprint(key);
109
+ }
110
+ else {
111
+ hostKeySha256 = fingerprintPublicKeyBlob(keyType, key);
112
+ }
113
+ if (hostKeySha256 === undefined)
114
+ return undefined;
115
+ }
49
116
  let authority = parts[0].toLowerCase();
50
117
  if (authority.startsWith('sftp://'))
51
118
  authority = authority.slice('sftp://'.length);
@@ -65,14 +132,16 @@ export function parseSftpEgressEntry(raw) {
65
132
  }
66
133
  if (host.length > 253 || !(HOSTNAME_RE.test(host) || isIPv4(host)))
67
134
  return undefined;
68
- return hostKeySha256 === undefined ? { host, port } : { host, port, hostKeySha256 };
135
+ if (hostKeySha256 === undefined)
136
+ return { host, port };
137
+ return hostKeyType === undefined ? { host, port, hostKeySha256 } : { host, port, hostKeySha256, hostKeyType };
69
138
  }
70
139
  /**
71
140
  * Parse a whole allowlist value (entries separated by commas or newlines).
72
141
  * Returns undefined when it holds no entry or any entry is invalid. An entry
73
142
  * repeated for the same host and port keeps its first appearance; two entries
74
- * for one host and port that disagree on the pin are invalid, since either
75
- * reading would silently drop a pin someone set.
143
+ * for one host and port that disagree on the pin or its key type are invalid,
144
+ * since either reading would silently drop a pin someone set.
76
145
  */
77
146
  export function parseSftpEgress(raw) {
78
147
  const entries = [];
@@ -86,14 +155,21 @@ export function parseSftpEgress(raw) {
86
155
  if (same === undefined) {
87
156
  entries.push(entry);
88
157
  }
89
- else if (same.hostKeySha256 !== entry.hostKeySha256) {
158
+ else if (same.hostKeySha256 !== entry.hostKeySha256 || same.hostKeyType !== entry.hostKeyType) {
90
159
  return undefined;
91
160
  }
92
161
  }
93
162
  return entries.length > 0 ? entries : undefined;
94
163
  }
95
- /** The canonical text of an entry: `host:port`, plus ` SHA256:…` when pinned. */
164
+ /**
165
+ * The canonical text of an entry: `host:port`, plus ` SHA256:…` when pinned,
166
+ * or ` <keyType> SHA256:…` when the pin names its key type.
167
+ */
96
168
  export function formatSftpEgressEntry(entry) {
97
169
  const base = `${entry.host}:${entry.port}`;
98
- return entry.hostKeySha256 === undefined ? base : `${base} ${entry.hostKeySha256}`;
170
+ if (entry.hostKeySha256 === undefined)
171
+ return base;
172
+ return entry.hostKeyType === undefined
173
+ ? `${base} ${entry.hostKeySha256}`
174
+ : `${base} ${entry.hostKeyType} ${entry.hostKeySha256}`;
99
175
  }
@@ -12,7 +12,9 @@
12
12
  * and the socket is dialled to the address that was checked, so a second
13
13
  * DNS answer cannot redirect it;
14
14
  * - when the entry pins a host key, a server presenting any other key is
15
- * refused before credentials are sent. Unpinned, the presented fingerprint
15
+ * refused before credentials are sent. When the pin names its key type,
16
+ * only that host-key algorithm is offered, so a server holding several
17
+ * host keys presents the pinned one. Unpinned, the presented fingerprint
16
18
  * is reported on the session so it can be pinned;
17
19
  * - the invocation's AbortSignal closes the connection.
18
20
  *
@@ -25,6 +27,7 @@
25
27
  * load it.
26
28
  */
27
29
  import { Readable } from 'node:stream';
30
+ import { type EgressRefusal } from './egress-refusal.js';
28
31
  import { type SftpEgressEntry } from './sftp-egress.js';
29
32
  /** Credentials and target for {@link SftpApi.connect}. */
30
33
  export interface SftpConnectOptions {
@@ -87,6 +90,8 @@ export interface SftpBinding {
87
90
  readonly loadSsh2?: () => Promise<typeof import('ssh2')>;
88
91
  /** Test seam: resolve a hostname to all its addresses. */
89
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;
90
95
  }
91
96
  /** For tools whose transport is not sftp: every call explains what to declare instead. */
92
97
  export declare function unavailableSftp(): SftpApi;
@@ -1 +1 @@
1
- {"version":3,"file":"sftp.d.ts","sourceRoot":"","sources":["../../src/ctx/sftp.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AAMH,OAAO,EAAE,QAAQ,EAAa,MAAM,aAAa,CAAC;AAYlD,OAAO,EAAyB,KAAK,eAAe,EAAE,MAAM,kBAAkB,CAAC;AAE/E,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,EA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O,EAAE,SAAS,eAAe,EAAE,CAAC;IAC7C,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,QAAQ,CAAC,MAAM,EAAE,WAAW,CAAC;IAC7B,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAC;IAC9B,kCAAkC;IAClC,QAAQ,CAAC,QAAQ,CAAC,EAAE,MAAM,OAAO,CAAC,cAAc,MAAM,CAAC,CAAC,CAAC;IACzD,0DAA0D;IAC1D,QAAQ,CAAC,OAAO,CAAC,EAAE,CAAC,QAAQ,EAAE,MAAM,KAAK,OAAO,CAAC,MAAM,EAAE,CAAC,CAAC;IAC3D,6FAA6F;IAC7F,QAAQ,CAAC,eAAe,CAAC,EAAE,CAAC,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
@@ -12,7 +12,9 @@
12
12
  * and the socket is dialled to the address that was checked, so a second
13
13
  * DNS answer cannot redirect it;
14
14
  * - when the entry pins a host key, a server presenting any other key is
15
- * refused before credentials are sent. Unpinned, the presented fingerprint
15
+ * refused before credentials are sent. When the pin names its key type,
16
+ * only that host-key algorithm is offered, so a server holding several
17
+ * host keys presents the pinned one. Unpinned, the presented fingerprint
16
18
  * is reported on the session so it can be pinned;
17
19
  * - the invocation's AbortSignal closes the connection.
18
20
  *
@@ -32,8 +34,27 @@ import { Readable, Transform } from 'node:stream';
32
34
  import { pipeline } from 'node:stream/promises';
33
35
  import { AkaiEgressError, AkaiInputError, AkaiResponseTooLargeError, AkaiSecretError, AkaiToolError, } from '../errors.js';
34
36
  import { isPrivateIp } from './safe-fetch.js';
37
+ import { reportEgressRefusal } from './egress-refusal.js';
35
38
  import { formatSftpEgressEntry } from './sftp-egress.js';
36
39
  const READY_TIMEOUT_MS = 20_000;
40
+ /**
41
+ * The ssh2 host-key algorithms that make a server present a key of the given
42
+ * type. An RSA key is signed with any of three algorithms; the SHA-2 ones
43
+ * first, falling back to SHA-1 `ssh-rsa` for old servers.
44
+ */
45
+ const SERVER_HOST_KEY_ALGORITHMS = {
46
+ 'ssh-ed25519': ['ssh-ed25519'],
47
+ 'ssh-rsa': ['rsa-sha2-512', 'rsa-sha2-256', 'ssh-rsa'],
48
+ 'ecdsa-sha2-nistp256': ['ecdsa-sha2-nistp256'],
49
+ 'ecdsa-sha2-nistp384': ['ecdsa-sha2-nistp384'],
50
+ 'ecdsa-sha2-nistp521': ['ecdsa-sha2-nistp521'],
51
+ };
52
+ /** The ssh2 `algorithms` restriction for an entry: only its pinned key type, or none. */
53
+ function sftpHostKeyAlgorithms(entry) {
54
+ if (entry.hostKeyType === undefined)
55
+ return undefined;
56
+ return { serverHostKey: [...SERVER_HOST_KEY_ALGORITHMS[entry.hostKeyType]] };
57
+ }
37
58
  const KEEPALIVE_INTERVAL_MS = 10_000;
38
59
  // SSH_FX_* status codes ssh2 puts on SFTP errors.
39
60
  const SFTP_NO_SUCH_FILE = 2;
@@ -70,6 +91,7 @@ export function createSftp(binding) {
70
91
  checkPrivateKey(ssh2, auth.privateKey, auth.passphrase, where);
71
92
  const client = new ssh2.Client();
72
93
  let presented = '';
94
+ const algorithms = sftpHostKeyAlgorithms(entry);
73
95
  const config = {
74
96
  host: address,
75
97
  port: entry.port,
@@ -80,6 +102,7 @@ export function createSftp(binding) {
80
102
  ...(auth.password !== undefined ? { password: auth.password } : {}),
81
103
  ...(auth.privateKey !== undefined ? { privateKey: auth.privateKey } : {}),
82
104
  ...(auth.passphrase !== undefined ? { passphrase: auth.passphrase } : {}),
105
+ ...(algorithms !== undefined ? { algorithms } : {}),
83
106
  hostVerifier: (key) => {
84
107
  presented = fingerprint(key);
85
108
  return entry.hostKeySha256 === undefined || entry.hostKeySha256 === presented;
@@ -221,6 +244,7 @@ async function resolvePublicAddress(host, binding) {
221
244
  if (process.env.NODE_ENV !== 'development') {
222
245
  for (const address of addresses) {
223
246
  if (isPrivateIp(address)) {
247
+ reportEgressRefusal(binding.onEgressRefused, { via: 'sdk_sftp', host, reason: 'private_address', address });
224
248
  throw new AkaiEgressError(`sftp: ${host} resolved to private address ${address}`, { hostname: host, address, allResolved: addresses });
225
249
  }
226
250
  }
@@ -238,7 +262,19 @@ function fingerprint(key) {
238
262
  function connectError(err, entry, presented) {
239
263
  const target = `${entry.host}:${entry.port}`;
240
264
  if (entry.hostKeySha256 !== undefined && presented !== '' && presented !== entry.hostKeySha256) {
241
- return new AkaiEgressError(`sftp: ${target} presented host key ${presented}, but the allowlist pins ${entry.hostKeySha256}. Refusing to log in. If the server's key really changed, an admin must update the pinned fingerprint.`, { host: entry.host, port: entry.port, presented, pinned: entry.hostKeySha256 });
265
+ const pinned = entry.hostKeyType === undefined ? entry.hostKeySha256 : `${entry.hostKeyType} ${entry.hostKeySha256}`;
266
+ return new AkaiEgressError(`sftp: ${target} presented host key ${presented}, but the allowlist pins ${pinned}. Refusing to log in. If the server's key really changed, an admin must update the pinned fingerprint.`, {
267
+ host: entry.host,
268
+ port: entry.port,
269
+ presented,
270
+ pinned: entry.hostKeySha256,
271
+ ...(entry.hostKeyType !== undefined ? { pinnedKeyType: entry.hostKeyType } : {}),
272
+ });
273
+ }
274
+ // ssh2: "Handshake failed: no matching host key format" — the server holds no
275
+ // key of the pinned type, so the restricted negotiation found nothing.
276
+ if (entry.hostKeyType !== undefined && /no matching host key/i.test(err.message)) {
277
+ return AkaiToolError.unavailable(`sftp: ${target} did not offer a ${entry.hostKeyType} host key, the key type the allowlist pins. Refusing to log in. Check the pinned key type and fingerprint against the keys the server actually publishes.`, { host: entry.host, port: entry.port, pinnedKeyType: entry.hostKeyType, pinned: entry.hostKeySha256 });
242
278
  }
243
279
  if (err.level === 'client-authentication') {
244
280
  return AkaiToolError.authExpired(`sftp: ${target} rejected the login. Check the username and password or private key on the connection.`);
@@ -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"}