@akai-workflow-builder/cli-sdk 0.11.0 → 0.11.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -4,6 +4,14 @@ 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.11.1](https://github.com/akai-workflow-builder/akai-cli-sdk/compare/cli-sdk-v0.11.0...cli-sdk-v0.11.1) (2026-09-25)
8
+
9
+
10
+ ### Bug Fixes
11
+
12
+ * **ctx:** check PEM body structure, not just charset (AKAI-2989) ([763dae4](https://github.com/akai-workflow-builder/akai-cli-sdk/commit/763dae45afe25634490995979eae466938186333))
13
+ * **ctx:** repair or reject a malformed clientCert PEM before the handshake (AKAI-2989) ([3519b83](https://github.com/akai-workflow-builder/akai-cli-sdk/commit/3519b83cc3a47c86c65d745972cd5f6c81985997))
14
+
7
15
  ## [0.11.0](https://github.com/akai-workflow-builder/akai-cli-sdk/compare/cli-sdk-v0.10.0...cli-sdk-v0.11.0) (2026-09-23)
8
16
 
9
17
 
@@ -1 +1 @@
1
- {"version":3,"file":"build-ctx.d.ts","sourceRoot":"","sources":["../../src/ctx/build-ctx.ts"],"names":[],"mappings":"AAQA,OAAO,EAGL,KAAK,cAAc,EACnB,KAAK,YAAY,EAClB,MAAM,cAAc,CAAC;AAItB,OAAO,KAAK,EAAc,MAAM,EAAiB,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAI1F;;;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;I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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;CACvC;AAoFD;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,QAAQ,CAAC,MAAM,EAAE,cAAc,GAAG,OAAO,CAwExD"}
@@ -7,6 +7,7 @@ import { createReadFileStream } from './read-file-stream.js';
7
7
  import { createWriteFile, unavailableWriteFile } from './write-file.js';
8
8
  import { AkaiSecretError } from '../errors.js';
9
9
  import { resolveDynamicEgressHosts } from './dynamic-egress.js';
10
+ import { normalizePem } from './pem.js';
10
11
  function resolveTool(cli, toolName) {
11
12
  const t = cli.tools[toolName];
12
13
  if (!t) {
@@ -48,7 +49,14 @@ function resolveClientCert(tool, secrets, cliId, toolName) {
48
49
  ];
49
50
  throw new AkaiSecretError(`buildCtx: tool \`${cliId}.${toolName}\` requires a mutual-TLS client certificate but secret(s) ${missing.join(', ')} are unset — set them on the connection`, { cliId, toolName, missing });
50
51
  }
51
- return { cert, key };
52
+ // Shape-check here, not at fetch time: OpenSSL's `PEM routines::no start
53
+ // line` from inside the first request names neither the secret nor what it
54
+ // wanted, and a collapsed-newline PEM is repairable (see ./pem.ts).
55
+ const where = { cliId, toolName };
56
+ return {
57
+ cert: normalizePem(cert, { ...where, secretKey: ref.certSecretKey, kind: 'certificate' }),
58
+ key: normalizePem(key, { ...where, secretKey: ref.keySecretKey, kind: 'private key' }),
59
+ };
52
60
  }
53
61
  /**
54
62
  * Native tools have no network surface — `ctx.fetch` is wired to a
@@ -0,0 +1,43 @@
1
+ /**
2
+ * Shape-check and normalise the PEM material a tool's `network.clientCert`
3
+ * references, before it reaches the TLS handshake.
4
+ *
5
+ * Node hands `cert`/`key` strings to OpenSSL as-is, and OpenSSL's answer to
6
+ * anything that is not line-wrapped PEM is `PEM routines::no start line` —
7
+ * raised from inside the first `ctx.fetch`, naming neither the secret nor the
8
+ * shape it wanted. That is what an operator saw after pasting a gov.br
9
+ * certificate into the connection (AKAI-2989). The common ways a secret ends
10
+ * up in that state:
11
+ *
12
+ * - a `.pfx` / DER blob pasted as base64 rather than converted to PEM
13
+ * - a PEM whose newlines were collapsed to spaces, or escaped to a literal
14
+ * `\n`, somewhere between the file and the secret store
15
+ * - the certificate and key pasted into each other's secret
16
+ * - a passphrase-protected key (`ENCRYPTED PRIVATE KEY`), which Node cannot
17
+ * use without a passphrase the SDK has nowhere to take
18
+ *
19
+ * Only the collapsed-newline case can be repaired, and it is: the blocks are
20
+ * parsed out and re-emitted as 64-column PEM. Everything else is rejected up
21
+ * front as an `AkaiSecretError` that names the secret and the `openssl`
22
+ * command that produces the right value from the issued `.pfx`.
23
+ */
24
+ export type PemKind = 'certificate' | 'private key';
25
+ export interface PemRef {
26
+ /** Secret key the value came from — named in every error. */
27
+ readonly secretKey: string;
28
+ readonly cliId: string;
29
+ readonly toolName: string;
30
+ readonly kind: PemKind;
31
+ }
32
+ /**
33
+ * Return `raw` as well-formed PEM of the expected `kind`, or throw an
34
+ * `AkaiSecretError` explaining what was found instead.
35
+ *
36
+ * The output is rebuilt from the parsed blocks, so already-good input comes
37
+ * back byte-for-byte equivalent to OpenSSL (headers, 64-column body, one
38
+ * block per line group) and a collapsed one comes back repaired. Text outside
39
+ * the blocks — `openssl pkcs12`'s "Bag Attributes" preamble, for instance —
40
+ * is dropped; OpenSSL ignores it anyway.
41
+ */
42
+ export declare function normalizePem(raw: string, ref: PemRef): string;
43
+ //# sourceMappingURL=pem.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pem.d.ts","sourceRoot":"","sources":["../../src/ctx/pem.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AAIH,MAAM,MAAM,OAAO,GAAG,aAAa,GAAG,aAAa,CAAC;AAEpD,MAAM,WAAW,MAAM;IACrB,6DAA6D;IAC7D,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,QAAQ,CAAC,IAAI,EAAE,OAAO,CAAC;CACxB;AA4DD;;;;;;;;;GASG;AACH,wBAAgB,YAAY,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,MAAM,CA2C7D"}
@@ -0,0 +1,116 @@
1
+ /**
2
+ * Shape-check and normalise the PEM material a tool's `network.clientCert`
3
+ * references, before it reaches the TLS handshake.
4
+ *
5
+ * Node hands `cert`/`key` strings to OpenSSL as-is, and OpenSSL's answer to
6
+ * anything that is not line-wrapped PEM is `PEM routines::no start line` —
7
+ * raised from inside the first `ctx.fetch`, naming neither the secret nor the
8
+ * shape it wanted. That is what an operator saw after pasting a gov.br
9
+ * certificate into the connection (AKAI-2989). The common ways a secret ends
10
+ * up in that state:
11
+ *
12
+ * - a `.pfx` / DER blob pasted as base64 rather than converted to PEM
13
+ * - a PEM whose newlines were collapsed to spaces, or escaped to a literal
14
+ * `\n`, somewhere between the file and the secret store
15
+ * - the certificate and key pasted into each other's secret
16
+ * - a passphrase-protected key (`ENCRYPTED PRIVATE KEY`), which Node cannot
17
+ * use without a passphrase the SDK has nowhere to take
18
+ *
19
+ * Only the collapsed-newline case can be repaired, and it is: the blocks are
20
+ * parsed out and re-emitted as 64-column PEM. Everything else is rejected up
21
+ * front as an `AkaiSecretError` that names the secret and the `openssl`
22
+ * command that produces the right value from the issued `.pfx`.
23
+ */
24
+ import { AkaiSecretError } from '../errors.js';
25
+ /** `-----BEGIN X-----` … `-----END X-----`, tolerant of any whitespace (or none) around the body. */
26
+ const BLOCK_RE = /-----BEGIN ([A-Z0-9 ]+?)-----([\s\S]*?)-----END \1-----/g;
27
+ /**
28
+ * Strict base64: whole quartets, with `=` padding only where the last quartet
29
+ * needs it. Charset alone would pass `A===`, `AAAA=` or a lone `A`, which
30
+ * OpenSSL rejects later — exactly the late, unnamed failure this module exists
31
+ * to prevent.
32
+ */
33
+ const STRICT_BASE64_RE = /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/;
34
+ const BASE64_CHARSET_RE = /^[A-Za-z0-9+/=]+$/;
35
+ const PEM_LINE_WIDTH = 64;
36
+ /** How to get the right value out of the issued `.pfx` — quoted in every rejection. */
37
+ const CONVERT_HINT = {
38
+ certificate: 'openssl pkcs12 -in issued.pfx -clcerts -nokeys',
39
+ 'private key': 'openssl pkcs12 -in issued.pfx -nocerts -nodes',
40
+ };
41
+ function reject(ref, why) {
42
+ const hint = `${CONVERT_HINT[ref.kind]} (add -legacy if OpenSSL 3 refuses the file) and paste the whole PEM, headers included`;
43
+ throw new AkaiSecretError(`buildCtx: tool \`${ref.cliId}.${ref.toolName}\` mutual-TLS ${ref.kind} in secret ${ref.secretKey} ${why}. Produce it with: ${hint}.`, { cliId: ref.cliId, toolName: ref.toolName, secretKey: ref.secretKey, kind: ref.kind });
44
+ }
45
+ function parseBlocks(text) {
46
+ const blocks = [];
47
+ for (const m of text.matchAll(BLOCK_RE)) {
48
+ blocks.push({ label: m[1], body: m[2].replace(/\s+/g, '') });
49
+ }
50
+ return blocks;
51
+ }
52
+ function looksLikeBareBase64(text) {
53
+ const compact = text.replace(/\s+/g, '');
54
+ // DER-encoded ASN.1 (a .cer, .pfx or raw key) starts with a SEQUENCE tag,
55
+ // which base64 as "MI".
56
+ return compact.length > 0 && BASE64_CHARSET_RE.test(compact) && compact.startsWith('MI');
57
+ }
58
+ function emit(blocks) {
59
+ return (blocks
60
+ .map((b) => {
61
+ const lines = b.body.match(new RegExp(`.{1,${PEM_LINE_WIDTH}}`, 'g')) ?? [];
62
+ return `-----BEGIN ${b.label}-----\n${lines.join('\n')}\n-----END ${b.label}-----`;
63
+ })
64
+ .join('\n') + '\n');
65
+ }
66
+ /**
67
+ * Return `raw` as well-formed PEM of the expected `kind`, or throw an
68
+ * `AkaiSecretError` explaining what was found instead.
69
+ *
70
+ * The output is rebuilt from the parsed blocks, so already-good input comes
71
+ * back byte-for-byte equivalent to OpenSSL (headers, 64-column body, one
72
+ * block per line group) and a collapsed one comes back repaired. Text outside
73
+ * the blocks — `openssl pkcs12`'s "Bag Attributes" preamble, for instance —
74
+ * is dropped; OpenSSL ignores it anyway.
75
+ */
76
+ export function normalizePem(raw, ref) {
77
+ const text = raw
78
+ .replace(/\r\n?/g, '\n')
79
+ // A PEM that went through a JSON string or a single-line form field
80
+ // arrives with its newlines as the two characters `\` `n`.
81
+ .replace(/\\n/g, '\n')
82
+ .trim();
83
+ const blocks = parseBlocks(text);
84
+ if (blocks.length === 0) {
85
+ if (looksLikeBareBase64(text)) {
86
+ reject(ref, 'is base64 of a binary certificate or .pfx, not PEM — there is no -----BEGIN header');
87
+ }
88
+ reject(ref, 'has no -----BEGIN … ----- block, so it is not PEM');
89
+ }
90
+ const labels = blocks.map((b) => b.label);
91
+ if (ref.kind === 'certificate') {
92
+ if (labels.some((l) => l.endsWith('PRIVATE KEY'))) {
93
+ reject(ref, 'contains a private key — that belongs in the key secret, and this one wants the certificate');
94
+ }
95
+ if (!labels.includes('CERTIFICATE')) {
96
+ reject(ref, `holds ${labels.join(', ')} blocks but no CERTIFICATE block`);
97
+ }
98
+ }
99
+ else {
100
+ if (labels.includes('ENCRYPTED PRIVATE KEY')) {
101
+ reject(ref, 'is passphrase-protected (ENCRYPTED PRIVATE KEY), which cannot be used without a passphrase — export it unencrypted');
102
+ }
103
+ if (labels.includes('CERTIFICATE')) {
104
+ reject(ref, 'contains a certificate — that belongs in the certificate secret, and this one wants the private key');
105
+ }
106
+ if (!labels.some((l) => l.endsWith('PRIVATE KEY'))) {
107
+ reject(ref, `holds ${labels.join(', ')} blocks but no PRIVATE KEY block`);
108
+ }
109
+ }
110
+ for (const b of blocks) {
111
+ if (b.body.length === 0 || !STRICT_BASE64_RE.test(b.body)) {
112
+ reject(ref, `has a ${b.label} block whose body is not base64`);
113
+ }
114
+ }
115
+ return emit(blocks);
116
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@akai-workflow-builder/cli-sdk",
3
- "version": "0.11.0",
3
+ "version": "0.11.1",
4
4
  "description": "Authoring SDK for atomic, agent-executable CLIs and tools.",
5
5
  "keywords": [
6
6
  "akai",