@claude-flow/security 3.0.0-alpha.9 → 3.0.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/dist/CVE-REMEDIATION.d.ts +17 -6
- package/dist/CVE-REMEDIATION.d.ts.map +1 -1
- package/dist/CVE-REMEDIATION.js +328 -12
- package/dist/CVE-REMEDIATION.js.map +1 -1
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +26 -0
- package/dist/index.js.map +1 -1
- package/dist/keychain-adapter.d.ts +48 -0
- package/dist/keychain-adapter.d.ts.map +1 -0
- package/dist/keychain-adapter.js +0 -0
- package/dist/keychain-adapter.js.map +1 -0
- package/dist/mcp-caller-identity.d.ts +101 -0
- package/dist/mcp-caller-identity.d.ts.map +1 -0
- package/dist/mcp-caller-identity.js +0 -0
- package/dist/mcp-caller-identity.js.map +1 -0
- package/dist/mcp-composition-inspector.d.ts +133 -0
- package/dist/mcp-composition-inspector.d.ts.map +1 -0
- package/dist/mcp-composition-inspector.js +254 -0
- package/dist/mcp-composition-inspector.js.map +1 -0
- package/dist/oauth/browser.d.ts +31 -0
- package/dist/oauth/browser.d.ts.map +1 -0
- package/dist/oauth/browser.js +55 -0
- package/dist/oauth/browser.js.map +1 -0
- package/dist/oauth/callback-server.d.ts +37 -0
- package/dist/oauth/callback-server.d.ts.map +1 -0
- package/dist/oauth/callback-server.js +86 -0
- package/dist/oauth/callback-server.js.map +1 -0
- package/dist/oauth/client.d.ts +57 -0
- package/dist/oauth/client.d.ts.map +1 -0
- package/dist/oauth/client.js +120 -0
- package/dist/oauth/client.js.map +1 -0
- package/dist/oauth/pkce.d.ts +25 -0
- package/dist/oauth/pkce.d.ts.map +1 -0
- package/dist/oauth/pkce.js +36 -0
- package/dist/oauth/pkce.js.map +1 -0
- package/dist/path-validator.d.ts +23 -0
- package/dist/path-validator.d.ts.map +1 -1
- package/dist/path-validator.js +128 -5
- package/dist/path-validator.js.map +1 -1
- package/dist/policy/canonical.d.ts +5 -0
- package/dist/policy/canonical.d.ts.map +1 -0
- package/dist/policy/canonical.js +30 -0
- package/dist/policy/canonical.js.map +1 -0
- package/dist/policy/engine.d.ts +34 -0
- package/dist/policy/engine.d.ts.map +1 -0
- package/dist/policy/engine.js +285 -0
- package/dist/policy/engine.js.map +1 -0
- package/dist/policy/envelope.d.ts +9 -0
- package/dist/policy/envelope.d.ts.map +1 -0
- package/dist/policy/envelope.js +90 -0
- package/dist/policy/envelope.js.map +1 -0
- package/dist/policy/evaluator.d.ts +4 -0
- package/dist/policy/evaluator.d.ts.map +1 -0
- package/dist/policy/evaluator.js +119 -0
- package/dist/policy/evaluator.js.map +1 -0
- package/dist/policy/index.d.ts +7 -0
- package/dist/policy/index.d.ts.map +1 -0
- package/dist/policy/index.js +7 -0
- package/dist/policy/index.js.map +1 -0
- package/dist/policy/product-plane.d.ts +238 -0
- package/dist/policy/product-plane.d.ts.map +1 -0
- package/dist/policy/product-plane.js +1022 -0
- package/dist/policy/product-plane.js.map +1 -0
- package/dist/policy/types.d.ts +169 -0
- package/dist/policy/types.d.ts.map +1 -0
- package/dist/policy/types.js +2 -0
- package/dist/policy/types.js.map +1 -0
- package/dist/safe-git.d.ts +22 -0
- package/dist/safe-git.d.ts.map +1 -0
- package/dist/safe-git.js +63 -0
- package/dist/safe-git.js.map +1 -0
- package/package.json +17 -5
- package/.claude-flow/data/pending-insights.jsonl +0 -6
- package/.claude-flow/neural/stats.json +0 -6
- package/__tests__/acceptance/security-compliance.test.ts +0 -674
- package/__tests__/authorization-propagator.test.ts +0 -155
- package/__tests__/credential-generator.test.ts +0 -310
- package/__tests__/fixtures/configurations.ts +0 -425
- package/__tests__/fixtures/index.ts +0 -21
- package/__tests__/helpers/create-mock.ts +0 -469
- package/__tests__/helpers/index.ts +0 -32
- package/__tests__/input-validator.test.ts +0 -382
- package/__tests__/integration/security-flow.test.ts +0 -606
- package/__tests__/password-hasher.test.ts +0 -239
- package/__tests__/path-validator.test.ts +0 -302
- package/__tests__/plugin-integrity-verifier.test.ts +0 -199
- package/__tests__/safe-executor.test.ts +0 -292
- package/__tests__/token-generator.test.ts +0 -371
- package/__tests__/tool-output-guardrail.test.ts +0 -256
- package/__tests__/unit/credential-generator.test.ts +0 -182
- package/__tests__/unit/password-hasher.test.ts +0 -359
- package/__tests__/unit/path-validator.test.ts +0 -509
- package/__tests__/unit/safe-executor.test.ts +0 -667
- package/__tests__/unit/token-generator.test.ts +0 -310
- package/ruvector.db +0 -0
- package/src/CVE-REMEDIATION.ts +0 -251
- package/src/application/index.ts +0 -10
- package/src/application/services/security-application-service.ts +0 -193
- package/src/authorization/propagator.ts +0 -279
- package/src/credential-generator.ts +0 -368
- package/src/domain/entities/security-context.ts +0 -173
- package/src/domain/index.ts +0 -17
- package/src/domain/services/security-domain-service.ts +0 -296
- package/src/index.ts +0 -314
- package/src/input-validator.ts +0 -466
- package/src/password-hasher.ts +0 -277
- package/src/path-validator.ts +0 -525
- package/src/plugins/integrity-verifier.ts +0 -265
- package/src/safe-executor.ts +0 -546
- package/src/token-generator.ts +0 -463
- package/src/tool-output-guardrail.ts +0 -360
- package/tmp.json +0 -0
- package/tsconfig.json +0 -9
- package/tsconfig.tsbuildinfo +0 -1
- package/vitest.config.ts +0 -20
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Ephemeral-port localhost callback server for the browser-based OAuth flow
|
|
3
|
+
* (ADR-306). A minimal single-request HTTP server — deliberately not a full
|
|
4
|
+
* framework for one GET route that lives for one request and then shuts
|
|
5
|
+
* down. A TypeScript port of meta-proxy's `src/oauth/callback_server.rs`.
|
|
6
|
+
*
|
|
7
|
+
* @module v3/security/oauth/callback-server
|
|
8
|
+
*/
|
|
9
|
+
export interface CallbackResult {
|
|
10
|
+
code: string | null;
|
|
11
|
+
state: string | null;
|
|
12
|
+
error: string | null;
|
|
13
|
+
}
|
|
14
|
+
export declare class CallbackTimeoutError extends Error {
|
|
15
|
+
constructor();
|
|
16
|
+
}
|
|
17
|
+
export declare class CallbackServer {
|
|
18
|
+
private readonly server;
|
|
19
|
+
readonly redirectUri: string;
|
|
20
|
+
readonly port: number;
|
|
21
|
+
private constructor();
|
|
22
|
+
/**
|
|
23
|
+
* Binds `127.0.0.1:0` (OS-assigned ephemeral port) and returns the server
|
|
24
|
+
* plus the exact `redirect_uri` to send in the authorize request — must be
|
|
25
|
+
* the `http://127.0.0.1:<port>/oauth/callback` shape identity's
|
|
26
|
+
* `validate_redirect_uri` accepts.
|
|
27
|
+
*/
|
|
28
|
+
static bind(): Promise<CallbackServer>;
|
|
29
|
+
/**
|
|
30
|
+
* Waits for exactly one `GET /oauth/callback?...` request, replies with the
|
|
31
|
+
* browser success page, closes the server, and returns the parsed query
|
|
32
|
+
* parameters. Times out after `waitForMs` so a login attempt the user
|
|
33
|
+
* abandons in the browser doesn't hang the CLI forever.
|
|
34
|
+
*/
|
|
35
|
+
awaitCallback(waitForMs?: number): Promise<CallbackResult>;
|
|
36
|
+
}
|
|
37
|
+
//# sourceMappingURL=callback-server.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"callback-server.d.ts","sourceRoot":"","sources":["../../src/oauth/callback-server.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAKH,MAAM,WAAW,cAAc;IAC7B,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;IACpB,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IACrB,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;CACtB;AAeD,qBAAa,oBAAqB,SAAQ,KAAK;;CAK9C;AAED,qBAAa,cAAc;IAEvB,OAAO,CAAC,QAAQ,CAAC,MAAM;aACP,WAAW,EAAE,MAAM;aACnB,IAAI,EAAE,MAAM;IAH9B,OAAO;IAMP;;;;;OAKG;WACU,IAAI,IAAI,OAAO,CAAC,cAAc,CAAC;IAa5C;;;;;OAKG;IACG,aAAa,CAAC,SAAS,SAAqB,GAAG,OAAO,CAAC,cAAc,CAAC;CA0B7E"}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Ephemeral-port localhost callback server for the browser-based OAuth flow
|
|
3
|
+
* (ADR-306). A minimal single-request HTTP server — deliberately not a full
|
|
4
|
+
* framework for one GET route that lives for one request and then shuts
|
|
5
|
+
* down. A TypeScript port of meta-proxy's `src/oauth/callback_server.rs`.
|
|
6
|
+
*
|
|
7
|
+
* @module v3/security/oauth/callback-server
|
|
8
|
+
*/
|
|
9
|
+
import { createServer } from 'node:http';
|
|
10
|
+
const SUCCESS_PAGE = `<html>
|
|
11
|
+
<body style="background:#0a0a0a; color:#f5f5f5; font-family:sans-serif;
|
|
12
|
+
display:flex; align-items:center; justify-content:center;
|
|
13
|
+
height:100vh; margin:0;">
|
|
14
|
+
<div style="text-align:center;">
|
|
15
|
+
<h1>✓ ruflo login successful</h1>
|
|
16
|
+
<p>You can close this tab and return to your terminal.</p>
|
|
17
|
+
</div>
|
|
18
|
+
</body>
|
|
19
|
+
</html>`;
|
|
20
|
+
const DEFAULT_TIMEOUT_MS = 5 * 60 * 1000; // 5 minutes, matches meta-proxy's CALLBACK_TIMEOUT
|
|
21
|
+
export class CallbackTimeoutError extends Error {
|
|
22
|
+
constructor() {
|
|
23
|
+
super('timed out waiting for OAuth callback');
|
|
24
|
+
this.name = 'CallbackTimeoutError';
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
export class CallbackServer {
|
|
28
|
+
server;
|
|
29
|
+
redirectUri;
|
|
30
|
+
port;
|
|
31
|
+
constructor(server, redirectUri, port) {
|
|
32
|
+
this.server = server;
|
|
33
|
+
this.redirectUri = redirectUri;
|
|
34
|
+
this.port = port;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Binds `127.0.0.1:0` (OS-assigned ephemeral port) and returns the server
|
|
38
|
+
* plus the exact `redirect_uri` to send in the authorize request — must be
|
|
39
|
+
* the `http://127.0.0.1:<port>/oauth/callback` shape identity's
|
|
40
|
+
* `validate_redirect_uri` accepts.
|
|
41
|
+
*/
|
|
42
|
+
static async bind() {
|
|
43
|
+
return new Promise((resolve, reject) => {
|
|
44
|
+
const server = createServer();
|
|
45
|
+
server.once('error', reject);
|
|
46
|
+
server.listen(0, '127.0.0.1', () => {
|
|
47
|
+
server.removeListener('error', reject);
|
|
48
|
+
const addr = server.address();
|
|
49
|
+
const redirectUri = `http://127.0.0.1:${addr.port}/oauth/callback`;
|
|
50
|
+
resolve(new CallbackServer(server, redirectUri, addr.port));
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Waits for exactly one `GET /oauth/callback?...` request, replies with the
|
|
56
|
+
* browser success page, closes the server, and returns the parsed query
|
|
57
|
+
* parameters. Times out after `waitForMs` so a login attempt the user
|
|
58
|
+
* abandons in the browser doesn't hang the CLI forever.
|
|
59
|
+
*/
|
|
60
|
+
async awaitCallback(waitForMs = DEFAULT_TIMEOUT_MS) {
|
|
61
|
+
return new Promise((resolve, reject) => {
|
|
62
|
+
const timer = setTimeout(() => {
|
|
63
|
+
this.server.close();
|
|
64
|
+
reject(new CallbackTimeoutError());
|
|
65
|
+
}, waitForMs);
|
|
66
|
+
this.server.once('request', (req, res) => {
|
|
67
|
+
clearTimeout(timer);
|
|
68
|
+
const url = new URL(req.url ?? '/oauth/callback', 'http://127.0.0.1');
|
|
69
|
+
const result = {
|
|
70
|
+
code: url.searchParams.get('code'),
|
|
71
|
+
state: url.searchParams.get('state'),
|
|
72
|
+
error: url.searchParams.get('error'),
|
|
73
|
+
};
|
|
74
|
+
res.writeHead(200, {
|
|
75
|
+
'content-type': 'text/html; charset=utf-8',
|
|
76
|
+
connection: 'close',
|
|
77
|
+
});
|
|
78
|
+
res.end(SUCCESS_PAGE, () => {
|
|
79
|
+
this.server.close();
|
|
80
|
+
resolve(result);
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
//# sourceMappingURL=callback-server.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"callback-server.js","sourceRoot":"","sources":["../../src/oauth/callback-server.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,YAAY,EAAe,MAAM,WAAW,CAAC;AAStD,MAAM,YAAY,GAAG;;;;;;;;;QASb,CAAC;AAET,MAAM,kBAAkB,GAAG,CAAC,GAAG,EAAE,GAAG,IAAI,CAAC,CAAC,mDAAmD;AAE7F,MAAM,OAAO,oBAAqB,SAAQ,KAAK;IAC7C;QACE,KAAK,CAAC,sCAAsC,CAAC,CAAC;QAC9C,IAAI,CAAC,IAAI,GAAG,sBAAsB,CAAC;IACrC,CAAC;CACF;AAED,MAAM,OAAO,cAAc;IAEN;IACD;IACA;IAHlB,YACmB,MAAc,EACf,WAAmB,EACnB,IAAY;QAFX,WAAM,GAAN,MAAM,CAAQ;QACf,gBAAW,GAAX,WAAW,CAAQ;QACnB,SAAI,GAAJ,IAAI,CAAQ;IAC3B,CAAC;IAEJ;;;;;OAKG;IACH,MAAM,CAAC,KAAK,CAAC,IAAI;QACf,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;YACrC,MAAM,MAAM,GAAG,YAAY,EAAE,CAAC;YAC9B,MAAM,CAAC,IAAI,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;YAC7B,MAAM,CAAC,MAAM,CAAC,CAAC,EAAE,WAAW,EAAE,GAAG,EAAE;gBACjC,MAAM,CAAC,cAAc,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;gBACvC,MAAM,IAAI,GAAG,MAAM,CAAC,OAAO,EAAiB,CAAC;gBAC7C,MAAM,WAAW,GAAG,oBAAoB,IAAI,CAAC,IAAI,iBAAiB,CAAC;gBACnE,OAAO,CAAC,IAAI,cAAc,CAAC,MAAM,EAAE,WAAW,EAAE,IAAI,CAAC,I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+1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HTTP client for `auth.cognitum.one`'s OAuth surface (ADR-306): authorize
|
|
3
|
+
* URL construction, `POST /oauth/token` (`authorization_code` and
|
|
4
|
+
* `refresh_token` grants), and `POST /v1/oauth/code-exchange` (OOB fallback).
|
|
5
|
+
*
|
|
6
|
+
* A TypeScript port of meta-proxy's `src/oauth/client.rs`
|
|
7
|
+
* (cognitum-one/meta-proxy) — same base URL, same endpoints, same
|
|
8
|
+
* form/query parameter names, confirmed live 2026-07-16 (a real
|
|
9
|
+
* `GET /oauth/authorize?...&client_id=meta-proxy&redirect_uri=<ruflo-controlled
|
|
10
|
+
* loopback>` returns a working consent page, not a redirect_uri-mismatch
|
|
11
|
+
* error — so this client reuses meta-proxy's registered `client_id` rather
|
|
12
|
+
* than requiring a new one).
|
|
13
|
+
*
|
|
14
|
+
* Deliberately NOT `api.cognitum.one/v1/auth/*` — that's what ruflo's own
|
|
15
|
+
* checked-in OpenAPI spec (v3/docs/api/cognitum-v1.openapi.yaml, ADR-308)
|
|
16
|
+
* describes, but it does not match what the real identity server serves.
|
|
17
|
+
* This client targets the proven, live surface instead.
|
|
18
|
+
*
|
|
19
|
+
* @module v3/security/oauth/client
|
|
20
|
+
*/
|
|
21
|
+
export declare const CLIENT_ID = "meta-proxy";
|
|
22
|
+
export declare const SCOPE = "inference";
|
|
23
|
+
/** RFC 8252 out-of-band sentinel — the OOB/manual-paste flow's `redirect_uri`. */
|
|
24
|
+
export declare const OOB_REDIRECT_URI = "urn:ietf:wg:oauth:2.0:oob";
|
|
25
|
+
export interface TokenResponse {
|
|
26
|
+
access_token: string;
|
|
27
|
+
token_type: string;
|
|
28
|
+
account_email?: string;
|
|
29
|
+
refresh_token?: string;
|
|
30
|
+
expires_in?: number;
|
|
31
|
+
}
|
|
32
|
+
export declare class OAuthError extends Error {
|
|
33
|
+
readonly code: 'network' | 'protocol' | 'unexpected_shape';
|
|
34
|
+
readonly oauthError?: string | undefined;
|
|
35
|
+
readonly oauthDescription?: string | undefined;
|
|
36
|
+
constructor(message: string, code: 'network' | 'protocol' | 'unexpected_shape', oauthError?: string | undefined, oauthDescription?: string | undefined);
|
|
37
|
+
}
|
|
38
|
+
/** Builds the `/oauth/authorize` URL for the standard loopback-redirect flow. */
|
|
39
|
+
export declare function authorizeUrl(redirectUri: string, state: string, codeChallenge: string): string;
|
|
40
|
+
/** `POST /oauth/token` with `grant_type=authorization_code`. */
|
|
41
|
+
export declare function exchangeCode(code: string, codeVerifier: string, redirectUri: string, base?: string): Promise<TokenResponse>;
|
|
42
|
+
/**
|
|
43
|
+
* `POST /oauth/token` with `grant_type=refresh_token`. identity rotates
|
|
44
|
+
* refresh tokens with reuse detection: presenting a refresh token returns a
|
|
45
|
+
* NEW refresh token and revokes the old one, and re-presenting a spent one
|
|
46
|
+
* revokes the whole session family. Callers MUST persist the returned
|
|
47
|
+
* `refresh_token` atomically before using the new access token, and must
|
|
48
|
+
* never retry a failed refresh with the same token.
|
|
49
|
+
*/
|
|
50
|
+
export declare function refreshToken(refreshTokenValue: string, base?: string): Promise<TokenResponse>;
|
|
51
|
+
/**
|
|
52
|
+
* `POST /v1/oauth/code-exchange` — the OOB manual-entry fallback for
|
|
53
|
+
* headless/SSH/container environments where no browser round-trip to
|
|
54
|
+
* `127.0.0.1` is reachable.
|
|
55
|
+
*/
|
|
56
|
+
export declare function exchangeManualCode(code: string, codeVerifier: string, base?: string): Promise<TokenResponse>;
|
|
57
|
+
//# sourceMappingURL=client.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../src/oauth/client.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AAEH,eAAO,MAAM,SAAS,eAAe,CAAC;AACtC,eAAO,MAAM,KAAK,cAAc,CAAC;AACjC,kFAAkF;AAClF,eAAO,MAAM,gBAAgB,8BAA8B,CAAC;AAO5D,MAAM,WAAW,aAAa;IAC5B,YAAY,EAAE,MAAM,CAAC;IACrB,UAAU,EAAE,MAAM,CAAC;IACnB,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB;AAOD,qBAAa,UAAW,SAAQ,KAAK;aAGjB,IAAI,EAAE,SAAS,GAAG,UAAU,GAAG,kBAAkB;aACjD,UAAU,CAAC,EAAE,MAAM;aACnB,gBAAgB,CAAC,EAAE,MAAM;gBAHzC,OAAO,EAAE,MAAM,EACC,IAAI,EAAE,SAAS,GAAG,UAAU,GAAG,kBAAkB,EACjD,UAAU,CAAC,EAAE,MAAM,YAAA,EACnB,gBAAgB,CAAC,EAAE,MAAM,YAAA;CAK5C;AAED,iFAAiF;AACjF,wBAAgB,YAAY,CAAC,WAAW,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,GAAG,MAAM,CAU9F;AAsCD,gEAAgE;AAChE,wBAAsB,YAAY,CAChC,IAAI,EAAE,MAAM,EACZ,YAAY,EAAE,MAAM,EACpB,WAAW,EAAE,MAAM,EACnB,IAAI,SAAgB,GACnB,OAAO,CAAC,aAAa,CAAC,CAMxB;AAED;;;;;;;GAOG;AACH,wBAAsB,YAAY,CAAC,iBAAiB,EAAE,MAAM,EAAE,IAAI,SAAgB,GAAG,OAAO,CAAC,aAAa,CAAC,CAE1G;AAED;;;;GAIG;AACH,wBAAsB,kBAAkB,CACtC,IAAI,EAAE,MAAM,EACZ,YAAY,EAAE,MAAM,EACpB,IAAI,SAAgB,GACnB,OAAO,CAAC,aAAa,CAAC,CAYxB"}
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HTTP client for `auth.cognitum.one`'s OAuth surface (ADR-306): authorize
|
|
3
|
+
* URL construction, `POST /oauth/token` (`authorization_code` and
|
|
4
|
+
* `refresh_token` grants), and `POST /v1/oauth/code-exchange` (OOB fallback).
|
|
5
|
+
*
|
|
6
|
+
* A TypeScript port of meta-proxy's `src/oauth/client.rs`
|
|
7
|
+
* (cognitum-one/meta-proxy) — same base URL, same endpoints, same
|
|
8
|
+
* form/query parameter names, confirmed live 2026-07-16 (a real
|
|
9
|
+
* `GET /oauth/authorize?...&client_id=meta-proxy&redirect_uri=<ruflo-controlled
|
|
10
|
+
* loopback>` returns a working consent page, not a redirect_uri-mismatch
|
|
11
|
+
* error — so this client reuses meta-proxy's registered `client_id` rather
|
|
12
|
+
* than requiring a new one).
|
|
13
|
+
*
|
|
14
|
+
* Deliberately NOT `api.cognitum.one/v1/auth/*` — that's what ruflo's own
|
|
15
|
+
* checked-in OpenAPI spec (v3/docs/api/cognitum-v1.openapi.yaml, ADR-308)
|
|
16
|
+
* describes, but it does not match what the real identity server serves.
|
|
17
|
+
* This client targets the proven, live surface instead.
|
|
18
|
+
*
|
|
19
|
+
* @module v3/security/oauth/client
|
|
20
|
+
*/
|
|
21
|
+
export const CLIENT_ID = 'meta-proxy';
|
|
22
|
+
export const SCOPE = 'inference';
|
|
23
|
+
/** RFC 8252 out-of-band sentinel — the OOB/manual-paste flow's `redirect_uri`. */
|
|
24
|
+
export const OOB_REDIRECT_URI = 'urn:ietf:wg:oauth:2.0:oob';
|
|
25
|
+
function authBaseUrl() {
|
|
26
|
+
const override = process.env.COGNITUM_AUTH_URL;
|
|
27
|
+
return override && override.trim() ? override : 'https://auth.cognitum.one';
|
|
28
|
+
}
|
|
29
|
+
export class OAuthError extends Error {
|
|
30
|
+
code;
|
|
31
|
+
oauthError;
|
|
32
|
+
oauthDescription;
|
|
33
|
+
constructor(message, code, oauthError, oauthDescription) {
|
|
34
|
+
super(message);
|
|
35
|
+
this.code = code;
|
|
36
|
+
this.oauthError = oauthError;
|
|
37
|
+
this.oauthDescription = oauthDescription;
|
|
38
|
+
this.name = 'OAuthError';
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
/** Builds the `/oauth/authorize` URL for the standard loopback-redirect flow. */
|
|
42
|
+
export function authorizeUrl(redirectUri, state, codeChallenge) {
|
|
43
|
+
const url = new URL(`${authBaseUrl()}/oauth/authorize`);
|
|
44
|
+
url.searchParams.set('response_type', 'code');
|
|
45
|
+
url.searchParams.set('client_id', CLIENT_ID);
|
|
46
|
+
url.searchParams.set('redirect_uri', redirectUri);
|
|
47
|
+
url.searchParams.set('scope', SCOPE);
|
|
48
|
+
url.searchParams.set('state', state);
|
|
49
|
+
url.searchParams.set('code_challenge', codeChallenge);
|
|
50
|
+
url.searchParams.set('code_challenge_method', 'S256');
|
|
51
|
+
return url.toString();
|
|
52
|
+
}
|
|
53
|
+
async function parseTokenResponse(resp) {
|
|
54
|
+
if (resp.ok) {
|
|
55
|
+
try {
|
|
56
|
+
return (await resp.json());
|
|
57
|
+
}
|
|
58
|
+
catch {
|
|
59
|
+
throw new OAuthError('unexpected response shape from the server', 'unexpected_shape');
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
try {
|
|
63
|
+
const body = (await resp.json());
|
|
64
|
+
throw new OAuthError(`oauth error: ${body.error} — ${body.error_description}`, 'protocol', body.error, body.error_description);
|
|
65
|
+
}
|
|
66
|
+
catch (e) {
|
|
67
|
+
if (e instanceof OAuthError)
|
|
68
|
+
throw e;
|
|
69
|
+
throw new OAuthError('unexpected response shape from the server', 'unexpected_shape');
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
async function postForm(path, form, base = authBaseUrl()) {
|
|
73
|
+
let resp;
|
|
74
|
+
try {
|
|
75
|
+
resp = await fetch(`${base}${path}`, {
|
|
76
|
+
method: 'POST',
|
|
77
|
+
headers: { 'content-type': 'application/x-www-form-urlencoded' },
|
|
78
|
+
body: new URLSearchParams(form).toString(),
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
catch (e) {
|
|
82
|
+
throw new OAuthError(`network error: ${e instanceof Error ? e.message : String(e)}`, 'network');
|
|
83
|
+
}
|
|
84
|
+
return parseTokenResponse(resp);
|
|
85
|
+
}
|
|
86
|
+
/** `POST /oauth/token` with `grant_type=authorization_code`. */
|
|
87
|
+
export async function exchangeCode(code, codeVerifier, redirectUri, base = authBaseUrl()) {
|
|
88
|
+
return postForm('/oauth/token', { grant_type: 'authorization_code', code, code_verifier: codeVerifier, client_id: CLIENT_ID, redirect_uri: redirectUri }, base);
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* `POST /oauth/token` with `grant_type=refresh_token`. identity rotates
|
|
92
|
+
* refresh tokens with reuse detection: presenting a refresh token returns a
|
|
93
|
+
* NEW refresh token and revokes the old one, and re-presenting a spent one
|
|
94
|
+
* revokes the whole session family. Callers MUST persist the returned
|
|
95
|
+
* `refresh_token` atomically before using the new access token, and must
|
|
96
|
+
* never retry a failed refresh with the same token.
|
|
97
|
+
*/
|
|
98
|
+
export async function refreshToken(refreshTokenValue, base = authBaseUrl()) {
|
|
99
|
+
return postForm('/oauth/token', { grant_type: 'refresh_token', refresh_token: refreshTokenValue, client_id: CLIENT_ID }, base);
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* `POST /v1/oauth/code-exchange` — the OOB manual-entry fallback for
|
|
103
|
+
* headless/SSH/container environments where no browser round-trip to
|
|
104
|
+
* `127.0.0.1` is reachable.
|
|
105
|
+
*/
|
|
106
|
+
export async function exchangeManualCode(code, codeVerifier, base = authBaseUrl()) {
|
|
107
|
+
let resp;
|
|
108
|
+
try {
|
|
109
|
+
resp = await fetch(`${base}/v1/oauth/code-exchange`, {
|
|
110
|
+
method: 'POST',
|
|
111
|
+
headers: { 'content-type': 'application/json' },
|
|
112
|
+
body: JSON.stringify({ code, code_verifier: codeVerifier, client_id: CLIENT_ID }),
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
catch (e) {
|
|
116
|
+
throw new OAuthError(`network error: ${e instanceof Error ? e.message : String(e)}`, 'network');
|
|
117
|
+
}
|
|
118
|
+
return parseTokenResponse(resp);
|
|
119
|
+
}
|
|
120
|
+
//# source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AAE,cAAc,EAAE,kBAAkB,EAAE;YAC/C,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,EAAE,IAAI,EAAE,aAAa,EAAE,YAAY,EAAE,SAAS,EAAE,SAAS,EAAE,CAAC;SAClF,CAAC,CAAC;IACL,CAAC;IAAC,OAAO,CAAC,EAAE,CAAC;QACX,MAAM,IAAI,UAAU,CAAC,kBAAkB,CAAC,YAAY,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,SAAS,CAAC,CAAC;IAClG,CAAC;IACD,OAAO,kBAAkB,CAAC,IAAI,CAAC,CAAC;AAClC,CAAC"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OAuth 2.0 + PKCE (RFC 7636) client-side generation (ADR-306).
|
|
3
|
+
*
|
|
4
|
+
* A TypeScript port of meta-proxy's `src/oauth/pkce.rs` (cognitum-one/meta-proxy,
|
|
5
|
+
* itself ported from `dashboard/apps/cli/src/pkce.rs`) so a verifier generated
|
|
6
|
+
* here validates against the same `services/identity` authorization server
|
|
7
|
+
* meta-proxy already talks to successfully — same byte lengths, same encoding,
|
|
8
|
+
* same hash. Intentionally duplicated rather than imported from
|
|
9
|
+
* `@claude-flow/mcp/src/oauth.ts` (a different, unrelated OAuth client in this
|
|
10
|
+
* monorepo) — ADR-306 assigns PKCE ownership to this package specifically.
|
|
11
|
+
*
|
|
12
|
+
* @module v3/security/oauth/pkce
|
|
13
|
+
*/
|
|
14
|
+
export interface PkceRequest {
|
|
15
|
+
state: string;
|
|
16
|
+
codeVerifier: string;
|
|
17
|
+
codeChallenge: string;
|
|
18
|
+
}
|
|
19
|
+
export declare function challengeFromVerifier(verifier: string): string;
|
|
20
|
+
/**
|
|
21
|
+
* Generates a fresh `state` + PKCE verifier/challenge pair for one login
|
|
22
|
+
* attempt.
|
|
23
|
+
*/
|
|
24
|
+
export declare function generate(): PkceRequest;
|
|
25
|
+
//# sourceMappingURL=pkce.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pkce.d.ts","sourceRoot":"","sources":["../../src/oauth/pkce.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAIH,MAAM,WAAW,WAAW;IAC1B,KAAK,EAAE,MAAM,CAAC;IACd,YAAY,EAAE,MAAM,CAAC;IACrB,aAAa,EAAE,MAAM,CAAC;CACvB;AAWD,wBAAgB,qBAAqB,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,CAE9D;AAED;;;GAGG;AACH,wBAAgB,QAAQ,IAAI,WAAW,CAKtC"}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OAuth 2.0 + PKCE (RFC 7636) client-side generation (ADR-306).
|
|
3
|
+
*
|
|
4
|
+
* A TypeScript port of meta-proxy's `src/oauth/pkce.rs` (cognitum-one/meta-proxy,
|
|
5
|
+
* itself ported from `dashboard/apps/cli/src/pkce.rs`) so a verifier generated
|
|
6
|
+
* here validates against the same `services/identity` authorization server
|
|
7
|
+
* meta-proxy already talks to successfully — same byte lengths, same encoding,
|
|
8
|
+
* same hash. Intentionally duplicated rather than imported from
|
|
9
|
+
* `@claude-flow/mcp/src/oauth.ts` (a different, unrelated OAuth client in this
|
|
10
|
+
* monorepo) — ADR-306 assigns PKCE ownership to this package specifically.
|
|
11
|
+
*
|
|
12
|
+
* @module v3/security/oauth/pkce
|
|
13
|
+
*/
|
|
14
|
+
import { randomBytes, createHash } from 'crypto';
|
|
15
|
+
/**
|
|
16
|
+
* 32 random bytes, base64url-encoded (no padding) — comfortably within RFC
|
|
17
|
+
* 7636's 43-128 character verifier length requirement (32 bytes -> 43 chars).
|
|
18
|
+
* Matches the Rust port's `random_url_safe_token(32)` exactly.
|
|
19
|
+
*/
|
|
20
|
+
function randomUrlSafeToken(byteLen) {
|
|
21
|
+
return randomBytes(byteLen).toString('base64url');
|
|
22
|
+
}
|
|
23
|
+
export function challengeFromVerifier(verifier) {
|
|
24
|
+
return createHash('sha256').update(verifier).digest('base64url');
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Generates a fresh `state` + PKCE verifier/challenge pair for one login
|
|
28
|
+
* attempt.
|
|
29
|
+
*/
|
|
30
|
+
export function generate() {
|
|
31
|
+
const state = randomUrlSafeToken(32);
|
|
32
|
+
const codeVerifier = randomUrlSafeToken(32);
|
|
33
|
+
const codeChallenge = challengeFromVerifier(codeVerifier);
|
|
34
|
+
return { state, codeVerifier, codeChallenge };
|
|
35
|
+
}
|
|
36
|
+
//# sourceMappingURL=pkce.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pkce.js","sourceRoot":"","sources":["../../src/oauth/pkce.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,QAAQ,CAAC;AAQjD;;;;GAIG;AACH,SAAS,kBAAkB,CAAC,OAAe;IACzC,OAAO,WAAW,CAAC,OAAO,CAAC,CAAC,QAAQ,CAAC,WAAW,CAAC,CAAC;AACpD,CAAC;AAED,MAAM,UAAU,qBAAqB,CAAC,QAAgB;IACpD,OAAO,UAAU,CAAC,QAAQ,CAAC,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC;AACnE,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,QAAQ;IACtB,MAAM,KAAK,GAAG,kBAAkB,CAAC,EAAE,CAAC,CAAC;IACrC,MAAM,YAAY,GAAG,kBAAkB,CAAC,EAAE,CAAC,CAAC;IAC5C,MAAM,aAAa,GAAG,qBAAqB,CAAC,YAAY,CAAC,CAAC;IAC1D,OAAO,EAAE,KAAK,EAAE,YAAY,EAAE,aAAa,EAAE,CAAC;AAChD,CAAC"}
|
package/dist/path-validator.d.ts
CHANGED
|
@@ -13,6 +13,11 @@
|
|
|
13
13
|
* - Symlink resolution (optional)
|
|
14
14
|
* - Traversal pattern detection
|
|
15
15
|
*
|
|
16
|
+
* Canonicalization is symmetric: a candidate is only ever compared against
|
|
17
|
+
* prefixes held in the same form it was resolved in. Canonicalizing one side
|
|
18
|
+
* and not the other silently denies everything under a symlinked prefix
|
|
19
|
+
* (async) or silently accepts an escape through one (sync).
|
|
20
|
+
*
|
|
16
21
|
* @module v3/security/path-validator
|
|
17
22
|
*/
|
|
18
23
|
export interface PathValidatorConfig {
|
|
@@ -84,7 +89,25 @@ export declare class PathValidatorError extends Error {
|
|
|
84
89
|
*/
|
|
85
90
|
export declare class PathValidator {
|
|
86
91
|
private readonly config;
|
|
92
|
+
/** Lexically resolved prefixes — compared against lexically resolved candidates. */
|
|
87
93
|
private readonly resolvedPrefixes;
|
|
94
|
+
/**
|
|
95
|
+
* Symlink-resolved prefixes — compared against symlink-resolved candidates.
|
|
96
|
+
*
|
|
97
|
+
* Kept as a SECOND list rather than replacing `resolvedPrefixes`, because
|
|
98
|
+
* the two comparison sites derive their candidate differently: `validate()`
|
|
99
|
+
* canonicalizes through the filesystem, `validateSync()`/`isWithinAllowed()`
|
|
100
|
+
* are documented as lexical-only. Canonicalizing one side without the other
|
|
101
|
+
* is exactly the bug this fixes, in whichever direction it is done.
|
|
102
|
+
*
|
|
103
|
+
* Resolved once, at construction — so an allowed prefix denotes the
|
|
104
|
+
* directory it named when the validator was configured, and retargeting a
|
|
105
|
+
* symlink afterwards cannot silently redirect the allowlist. Re-resolving on
|
|
106
|
+
* every call would hand that control to whoever can write the link, and cost
|
|
107
|
+
* a syscall per validation. Callers needing to follow a moved target should
|
|
108
|
+
* construct a new validator.
|
|
109
|
+
*/
|
|
110
|
+
private readonly canonicalPrefixes;
|
|
88
111
|
constructor(config: PathValidatorConfig);
|
|
89
112
|
/**
|
|
90
113
|
* Validates a path against security rules.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"path-validator.d.ts","sourceRoot":"","sources":["../src/path-valid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G,OAAO,CAAC,oBAAoB,CAAC;IA+JhE;;;;;;OAMG;IACG,eAAe,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAczD;;;;;OAKG;IACH,YAAY,CAAC,SAAS,EAAE,MAAM,GAAG,oBAAoB;IA4FrD;;;;;;OAMG;IACG,UAAU,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,QAAQ,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,MAAM,CAAC;IAQxE;;;;OAIG;IACH,SAAS,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI;IAW/B;;OAEG;IACH,kBAAkB,IAAI,SAAS,MAAM,EAAE;IAIvC;;OAEG;IACH,eAAe,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO;CAU5C;AAED;;;;;GAKG;AACH,wBAAgB,0BAA0B,CAAC,WAAW,EAAE,MAAM,GAAG,aAAa,CAS7E;AAED;;;;;GAKG;AACH,wBAAgB,8BAA8B,CAAC,WAAW,EAAE,MAAM,GAAG,aAAa,CASjF"}
|
package/dist/path-validator.js
CHANGED
|
@@ -13,10 +13,16 @@
|
|
|
13
13
|
* - Symlink resolution (optional)
|
|
14
14
|
* - Traversal pattern detection
|
|
15
15
|
*
|
|
16
|
+
* Canonicalization is symmetric: a candidate is only ever compared against
|
|
17
|
+
* prefixes held in the same form it was resolved in. Canonicalizing one side
|
|
18
|
+
* and not the other silently denies everything under a symlinked prefix
|
|
19
|
+
* (async) or silently accepts an escape through one (sync).
|
|
20
|
+
*
|
|
16
21
|
* @module v3/security/path-validator
|
|
17
22
|
*/
|
|
18
23
|
import * as path from 'path';
|
|
19
24
|
import * as fs from 'fs/promises';
|
|
25
|
+
import { realpathSync } from 'fs';
|
|
20
26
|
export class PathValidatorError extends Error {
|
|
21
27
|
code;
|
|
22
28
|
path;
|
|
@@ -75,6 +81,71 @@ const DEFAULT_BLOCKED_NAMES = [
|
|
|
75
81
|
'.npmrc',
|
|
76
82
|
'.docker',
|
|
77
83
|
];
|
|
84
|
+
/**
|
|
85
|
+
* Canonicalizes a path that may not exist yet, by resolving the longest
|
|
86
|
+
* ancestor that DOES exist and re-attaching the remaining segments.
|
|
87
|
+
*
|
|
88
|
+
* Plain `realpath` throws ENOENT for a path whose leaf is missing, which
|
|
89
|
+
* would leave the caller comparing a canonical prefix against a merely
|
|
90
|
+
* lexical candidate — the asymmetry this whole helper exists to avoid.
|
|
91
|
+
* Walking up also means a symlinked PARENT is still resolved, so a
|
|
92
|
+
* not-yet-created file under a symlinked directory canonicalizes to its
|
|
93
|
+
* real location instead of its lexical one.
|
|
94
|
+
*
|
|
95
|
+
* Falls back to the lexical path if the ancestor walk itself fails (EACCES,
|
|
96
|
+
* ELOOP, …), preserving the previous behaviour rather than throwing out of
|
|
97
|
+
* a validation call.
|
|
98
|
+
*/
|
|
99
|
+
async function canonicalize(resolvedPath) {
|
|
100
|
+
try {
|
|
101
|
+
return await fs.realpath(resolvedPath);
|
|
102
|
+
}
|
|
103
|
+
catch (error) {
|
|
104
|
+
if (error?.code !== 'ENOENT') {
|
|
105
|
+
return resolvedPath;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
const parent = path.dirname(resolvedPath);
|
|
109
|
+
// dirname() of a root ('/' or 'C:\\') is itself — stop instead of recursing.
|
|
110
|
+
if (parent === resolvedPath) {
|
|
111
|
+
return resolvedPath;
|
|
112
|
+
}
|
|
113
|
+
return path.join(await canonicalize(parent), path.basename(resolvedPath));
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Synchronous twin of {@link canonicalize}, for the constructor and
|
|
117
|
+
* {@link PathValidator.addPrefix} which cannot await.
|
|
118
|
+
*/
|
|
119
|
+
function canonicalizeSync(resolvedPath) {
|
|
120
|
+
try {
|
|
121
|
+
return realpathSync(resolvedPath);
|
|
122
|
+
}
|
|
123
|
+
catch (error) {
|
|
124
|
+
if (error?.code !== 'ENOENT') {
|
|
125
|
+
return resolvedPath;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
const parent = path.dirname(resolvedPath);
|
|
129
|
+
if (parent === resolvedPath) {
|
|
130
|
+
return resolvedPath;
|
|
131
|
+
}
|
|
132
|
+
return path.join(canonicalizeSync(parent), path.basename(resolvedPath));
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* True if `resolvedPath` is the prefix itself or sits underneath it.
|
|
136
|
+
*
|
|
137
|
+
* The separator has to be appended to anchor the match at a path boundary, so
|
|
138
|
+
* `/srv/app-secrets` does not count as inside `/srv/app` — but a root prefix
|
|
139
|
+
* (`/`, `C:\`, a UNC share root) already ends in one, and appending a second
|
|
140
|
+
* looked for `//`, which rejected every descendant of a root prefix.
|
|
141
|
+
*/
|
|
142
|
+
function isWithinPrefix(resolvedPath, prefix) {
|
|
143
|
+
if (resolvedPath === prefix) {
|
|
144
|
+
return true;
|
|
145
|
+
}
|
|
146
|
+
const boundary = prefix.endsWith(path.sep) ? prefix : prefix + path.sep;
|
|
147
|
+
return resolvedPath.startsWith(boundary);
|
|
148
|
+
}
|
|
78
149
|
/**
|
|
79
150
|
* Path validator that prevents traversal attacks.
|
|
80
151
|
*
|
|
@@ -95,7 +166,25 @@ const DEFAULT_BLOCKED_NAMES = [
|
|
|
95
166
|
*/
|
|
96
167
|
export class PathValidator {
|
|
97
168
|
config;
|
|
169
|
+
/** Lexically resolved prefixes — compared against lexically resolved candidates. */
|
|
98
170
|
resolvedPrefixes;
|
|
171
|
+
/**
|
|
172
|
+
* Symlink-resolved prefixes — compared against symlink-resolved candidates.
|
|
173
|
+
*
|
|
174
|
+
* Kept as a SECOND list rather than replacing `resolvedPrefixes`, because
|
|
175
|
+
* the two comparison sites derive their candidate differently: `validate()`
|
|
176
|
+
* canonicalizes through the filesystem, `validateSync()`/`isWithinAllowed()`
|
|
177
|
+
* are documented as lexical-only. Canonicalizing one side without the other
|
|
178
|
+
* is exactly the bug this fixes, in whichever direction it is done.
|
|
179
|
+
*
|
|
180
|
+
* Resolved once, at construction — so an allowed prefix denotes the
|
|
181
|
+
* directory it named when the validator was configured, and retargeting a
|
|
182
|
+
* symlink afterwards cannot silently redirect the allowlist. Re-resolving on
|
|
183
|
+
* every call would hand that control to whoever can write the link, and cost
|
|
184
|
+
* a syscall per validation. Callers needing to follow a moved target should
|
|
185
|
+
* construct a new validator.
|
|
186
|
+
*/
|
|
187
|
+
canonicalPrefixes;
|
|
99
188
|
constructor(config) {
|
|
100
189
|
this.config = {
|
|
101
190
|
allowedPrefixes: config.allowedPrefixes,
|
|
@@ -109,8 +198,19 @@ export class PathValidator {
|
|
|
109
198
|
if (this.config.allowedPrefixes.length === 0) {
|
|
110
199
|
throw new PathValidatorError('At least one allowed prefix must be specified', 'EMPTY_PREFIXES');
|
|
111
200
|
}
|
|
112
|
-
// Pre-resolve all prefixes
|
|
201
|
+
// Pre-resolve all prefixes, lexically and canonically. #3010 — validate()
|
|
202
|
+
// canonicalizes the *candidate* through fs.realpath (when resolveSymlinks
|
|
203
|
+
// is on, the default) but prefixes were only ever path.resolve()d, never
|
|
204
|
+
// realpath'd. On any platform where the prefix itself is reached through a
|
|
205
|
+
// symlink (e.g. macOS os.tmpdir() -> /var/folders/... while /var is itself
|
|
206
|
+
// a symlink to /private/var), the realpath'd candidate can never match the
|
|
207
|
+
// non-realpath'd prefix, and every path under that prefix is rejected as
|
|
208
|
+
// "outside allowed directories" — including the prefix's own contents.
|
|
113
209
|
this.resolvedPrefixes = this.config.allowedPrefixes.map(p => path.resolve(p));
|
|
210
|
+
// Always a distinct array — `addPrefix` appends to both, so aliasing the
|
|
211
|
+
// lexical list would push twice. With resolveSymlinks off no candidate is
|
|
212
|
+
// ever canonicalized and nothing reads this list, so skip the syscall.
|
|
213
|
+
this.canonicalPrefixes = this.resolvedPrefixes.map(p => this.config.resolveSymlinks ? canonicalizeSync(p) : p);
|
|
114
214
|
}
|
|
115
215
|
/**
|
|
116
216
|
* Validates a path against security rules.
|
|
@@ -154,12 +254,17 @@ export class PathValidator {
|
|
|
154
254
|
}
|
|
155
255
|
// Resolve the path
|
|
156
256
|
let resolvedPath;
|
|
257
|
+
// Which prefix list this candidate may legitimately be compared against:
|
|
258
|
+
// only a symlink-resolved candidate may be matched against symlink-resolved
|
|
259
|
+
// prefixes. See `canonicalPrefixes`.
|
|
260
|
+
let symlinksResolved = false;
|
|
157
261
|
try {
|
|
158
262
|
resolvedPath = path.resolve(inputPath);
|
|
159
263
|
// Optionally resolve symlinks
|
|
160
264
|
if (this.config.resolveSymlinks) {
|
|
161
265
|
try {
|
|
162
266
|
resolvedPath = await fs.realpath(resolvedPath);
|
|
267
|
+
symlinksResolved = true;
|
|
163
268
|
}
|
|
164
269
|
catch (error) {
|
|
165
270
|
// Path doesn't exist yet - use resolved path
|
|
@@ -171,6 +276,20 @@ export class PathValidator {
|
|
|
171
276
|
errors.push(`Failed to resolve path: ${error.message}`);
|
|
172
277
|
}
|
|
173
278
|
}
|
|
279
|
+
if (error.code === 'ENOENT' && this.config.allowNonExistent) {
|
|
280
|
+
// The leaf does not exist, but its ancestors may still be
|
|
281
|
+
// symlinks. Canonicalize what exists so the candidate is in the
|
|
282
|
+
// same form as `canonicalPrefixes` — otherwise every not-yet-
|
|
283
|
+
// created path under a symlinked prefix (macOS `/var` ->
|
|
284
|
+
// `/private/var`, `/tmp` -> `/private/tmp`) fails to match.
|
|
285
|
+
//
|
|
286
|
+
// Only when non-existent paths are actually allowed: with
|
|
287
|
+
// `allowNonExistent: false` this call is already a failure, and
|
|
288
|
+
// canonicalizing anyway would change the `resolvedPath` and
|
|
289
|
+
// `matchedPrefix` reported to callers for no benefit.
|
|
290
|
+
resolvedPath = await canonicalize(resolvedPath);
|
|
291
|
+
symlinksResolved = true;
|
|
292
|
+
}
|
|
174
293
|
}
|
|
175
294
|
}
|
|
176
295
|
}
|
|
@@ -187,8 +306,11 @@ export class PathValidator {
|
|
|
187
306
|
let matchedPrefix = '';
|
|
188
307
|
let relativePath = '';
|
|
189
308
|
let prefixMatched = false;
|
|
190
|
-
|
|
191
|
-
|
|
309
|
+
const prefixes = symlinksResolved
|
|
310
|
+
? this.canonicalPrefixes
|
|
311
|
+
: this.resolvedPrefixes;
|
|
312
|
+
for (const prefix of prefixes) {
|
|
313
|
+
if (isWithinPrefix(resolvedPath, prefix)) {
|
|
192
314
|
prefixMatched = true;
|
|
193
315
|
matchedPrefix = prefix;
|
|
194
316
|
relativePath = resolvedPath.slice(prefix.length);
|
|
@@ -299,7 +421,7 @@ export class PathValidator {
|
|
|
299
421
|
let relativePath = '';
|
|
300
422
|
let prefixMatched = false;
|
|
301
423
|
for (const prefix of this.resolvedPrefixes) {
|
|
302
|
-
if (resolvedPath
|
|
424
|
+
if (isWithinPrefix(resolvedPath, prefix)) {
|
|
303
425
|
prefixMatched = true;
|
|
304
426
|
matchedPrefix = prefix;
|
|
305
427
|
relativePath = resolvedPath.slice(prefix.length);
|
|
@@ -366,6 +488,7 @@ export class PathValidator {
|
|
|
366
488
|
if (!this.resolvedPrefixes.includes(resolved)) {
|
|
367
489
|
this.config.allowedPrefixes.push(prefix);
|
|
368
490
|
this.resolvedPrefixes.push(resolved);
|
|
491
|
+
this.canonicalPrefixes.push(this.config.resolveSymlinks ? canonicalizeSync(resolved) : resolved);
|
|
369
492
|
}
|
|
370
493
|
}
|
|
371
494
|
/**
|
|
@@ -380,7 +503,7 @@ export class PathValidator {
|
|
|
380
503
|
isWithinAllowed(inputPath) {
|
|
381
504
|
try {
|
|
382
505
|
const resolved = path.resolve(inputPath);
|
|
383
|
-
return this.resolvedPrefixes.some(prefix => resolved
|
|
506
|
+
return this.resolvedPrefixes.some(prefix => isWithinPrefix(resolved, prefix));
|
|
384
507
|
}
|
|
385
508
|
catch {
|
|
386
509
|
return false;
|