@skillsmith/core 0.11.4 → 0.11.5
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 +13 -0
- package/README.md +10 -7
- package/dist/.tsbuildinfo +1 -1
- package/dist/src/api/client.d.ts.map +1 -1
- package/dist/src/api/client.js +15 -5
- package/dist/src/api/client.js.map +1 -1
- package/dist/src/api/client.private-registry.d.ts +80 -0
- package/dist/src/api/client.private-registry.d.ts.map +1 -0
- package/dist/src/api/client.private-registry.js +97 -0
- package/dist/src/api/client.private-registry.js.map +1 -0
- package/dist/src/api/client.private-registry.test.d.ts +9 -0
- package/dist/src/api/client.private-registry.test.d.ts.map +1 -0
- package/dist/src/api/client.private-registry.test.js +127 -0
- package/dist/src/api/client.private-registry.test.js.map +1 -0
- package/dist/src/api/client.toSkill.test.d.ts +25 -0
- package/dist/src/api/client.toSkill.test.d.ts.map +1 -0
- package/dist/src/api/client.toSkill.test.js +97 -0
- package/dist/src/api/client.toSkill.test.js.map +1 -0
- package/dist/src/api/client.token-refresh.d.ts +8 -0
- package/dist/src/api/client.token-refresh.d.ts.map +1 -1
- package/dist/src/api/client.token-refresh.js +26 -0
- package/dist/src/api/client.token-refresh.js.map +1 -1
- package/dist/src/api/client.token-refresh.test.js +82 -0
- package/dist/src/api/client.token-refresh.test.js.map +1 -1
- package/dist/src/api/index.d.ts +2 -0
- package/dist/src/api/index.d.ts.map +1 -1
- package/dist/src/api/index.js +9 -0
- package/dist/src/api/index.js.map +1 -1
- package/dist/src/api/security-summary.d.ts +73 -0
- package/dist/src/api/security-summary.d.ts.map +1 -0
- package/dist/src/api/security-summary.js +88 -0
- package/dist/src/api/security-summary.js.map +1 -0
- package/dist/src/api/security-summary.test.d.ts +9 -0
- package/dist/src/api/security-summary.test.d.ts.map +1 -0
- package/dist/src/api/security-summary.test.js +94 -0
- package/dist/src/api/security-summary.test.js.map +1 -0
- package/dist/src/config/config-atomic-write.d.ts +30 -10
- package/dist/src/config/config-atomic-write.d.ts.map +1 -1
- package/dist/src/config/config-atomic-write.js +33 -80
- package/dist/src/config/config-atomic-write.js.map +1 -1
- package/dist/src/config/config-atomic-write.test.d.ts +8 -0
- package/dist/src/config/config-atomic-write.test.d.ts.map +1 -1
- package/dist/src/config/config-atomic-write.test.js +64 -12
- package/dist/src/config/config-atomic-write.test.js.map +1 -1
- package/dist/src/config/owned-lock.acquire.d.ts +53 -0
- package/dist/src/config/owned-lock.acquire.d.ts.map +1 -0
- package/dist/src/config/owned-lock.acquire.js +140 -0
- package/dist/src/config/owned-lock.acquire.js.map +1 -0
- package/dist/src/config/owned-lock.claim.d.ts +84 -0
- package/dist/src/config/owned-lock.claim.d.ts.map +1 -0
- package/dist/src/config/owned-lock.claim.js +310 -0
- package/dist/src/config/owned-lock.claim.js.map +1 -0
- package/dist/src/config/owned-lock.d.ts +113 -0
- package/dist/src/config/owned-lock.d.ts.map +1 -0
- package/dist/src/config/owned-lock.js +121 -0
- package/dist/src/config/owned-lock.js.map +1 -0
- package/dist/src/config/owned-lock.test.d.ts +12 -0
- package/dist/src/config/owned-lock.test.d.ts.map +1 -0
- package/dist/src/config/owned-lock.test.js +412 -0
- package/dist/src/config/owned-lock.test.js.map +1 -0
- package/dist/src/config/owned-lock.types.d.ts +78 -0
- package/dist/src/config/owned-lock.types.d.ts.map +1 -0
- package/dist/src/config/owned-lock.types.js +30 -0
- package/dist/src/config/owned-lock.types.js.map +1 -0
- package/dist/src/db/createDatabase.d.ts.map +1 -1
- package/dist/src/db/createDatabase.js +19 -7
- package/dist/src/db/createDatabase.js.map +1 -1
- package/dist/src/embeddings/hnsw-store.exports.d.ts +11 -0
- package/dist/src/embeddings/hnsw-store.exports.d.ts.map +1 -0
- package/dist/src/embeddings/hnsw-store.exports.js +10 -0
- package/dist/src/embeddings/hnsw-store.exports.js.map +1 -0
- package/dist/src/embeddings/index.d.ts +1 -2
- package/dist/src/embeddings/index.d.ts.map +1 -1
- package/dist/src/embeddings/index.js +17 -3
- package/dist/src/embeddings/index.js.map +1 -1
- package/dist/src/embeddings/probe.d.ts.map +1 -1
- package/dist/src/embeddings/probe.js +4 -8
- package/dist/src/embeddings/probe.js.map +1 -1
- package/dist/src/exports/services.d.ts +4 -1
- package/dist/src/exports/services.d.ts.map +1 -1
- package/dist/src/exports/services.js +22 -0
- package/dist/src/exports/services.js.map +1 -1
- package/dist/src/index.d.ts +3 -3
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +9 -3
- package/dist/src/index.js.map +1 -1
- package/dist/src/install/index.d.ts +1 -1
- package/dist/src/install/index.d.ts.map +1 -1
- package/dist/src/install/index.js +1 -1
- package/dist/src/install/index.js.map +1 -1
- package/dist/src/install/paths.d.ts +10 -0
- package/dist/src/install/paths.d.ts.map +1 -1
- package/dist/src/install/paths.js +19 -0
- package/dist/src/install/paths.js.map +1 -1
- package/dist/src/security/scanner/multiline-category-closure.test.d.ts +80 -0
- package/dist/src/security/scanner/multiline-category-closure.test.d.ts.map +1 -0
- package/dist/src/security/scanner/multiline-category-closure.test.js +287 -0
- package/dist/src/security/scanner/multiline-category-closure.test.js.map +1 -0
- package/dist/src/services/recommend-guard.d.ts +28 -0
- package/dist/src/services/recommend-guard.d.ts.map +1 -0
- package/dist/src/services/recommend-guard.js +34 -0
- package/dist/src/services/recommend-guard.js.map +1 -0
- package/dist/src/services/recommend-guard.test.d.ts +2 -0
- package/dist/src/services/recommend-guard.test.d.ts.map +1 -0
- package/dist/src/services/recommend-guard.test.js +28 -0
- package/dist/src/services/recommend-guard.test.js.map +1 -0
- package/dist/src/services/skill-installation.content.d.ts +88 -0
- package/dist/src/services/skill-installation.content.d.ts.map +1 -0
- package/dist/src/services/skill-installation.content.js +319 -0
- package/dist/src/services/skill-installation.content.js.map +1 -0
- package/dist/src/services/skill-installation.helpers.d.ts +32 -1
- package/dist/src/services/skill-installation.helpers.d.ts.map +1 -1
- package/dist/src/services/skill-installation.helpers.js +39 -6
- package/dist/src/services/skill-installation.helpers.js.map +1 -1
- package/dist/src/services/skill-installation.helpers.test.js +34 -2
- package/dist/src/services/skill-installation.helpers.test.js.map +1 -1
- package/dist/src/services/skill-installation.io.d.ts.map +1 -1
- package/dist/src/services/skill-installation.io.js +74 -1
- package/dist/src/services/skill-installation.io.js.map +1 -1
- package/dist/src/services/skill-installation.service.d.ts +20 -1
- package/dist/src/services/skill-installation.service.d.ts.map +1 -1
- package/dist/src/services/skill-installation.service.js +26 -4
- package/dist/src/services/skill-installation.service.js.map +1 -1
- package/dist/src/services/skill-installation.types.d.ts +42 -1
- package/dist/src/services/skill-installation.types.d.ts.map +1 -1
- package/dist/src/services/skill-installation.types.js.map +1 -1
- package/dist/src/services/skill-resolution.d.ts +67 -0
- package/dist/src/services/skill-resolution.d.ts.map +1 -0
- package/dist/src/services/skill-resolution.js +67 -0
- package/dist/src/services/skill-resolution.js.map +1 -0
- package/dist/src/services/skill-resolution.test.d.ts +2 -0
- package/dist/src/services/skill-resolution.test.d.ts.map +1 -0
- package/dist/src/services/skill-resolution.test.js +143 -0
- package/dist/src/services/skill-resolution.test.js.map +1 -0
- package/dist/src/utils/index.d.ts +1 -0
- package/dist/src/utils/index.d.ts.map +1 -1
- package/dist/src/utils/index.js +4 -0
- package/dist/src/utils/index.js.map +1 -1
- package/dist/src/utils/quiet-mode.d.ts +21 -0
- package/dist/src/utils/quiet-mode.d.ts.map +1 -0
- package/dist/src/utils/quiet-mode.js +26 -0
- package/dist/src/utils/quiet-mode.js.map +1 -0
- package/dist/tests/EmbeddingService.test.js +58 -1
- package/dist/tests/EmbeddingService.test.js.map +1 -1
- package/dist/tests/SecurityScanner.performance.test.js +5 -2
- package/dist/tests/SecurityScanner.performance.test.js.map +1 -1
- package/dist/tests/db/database-abstraction.test.js +45 -1
- package/dist/tests/db/database-abstraction.test.js.map +1 -1
- package/dist/tests/helpers/deterministic-dead-pid.d.ts +13 -0
- package/dist/tests/helpers/deterministic-dead-pid.d.ts.map +1 -0
- package/dist/tests/helpers/deterministic-dead-pid.js +31 -0
- package/dist/tests/helpers/deterministic-dead-pid.js.map +1 -0
- package/dist/tests/integration/owned-lock-lost-update.test.d.ts +12 -0
- package/dist/tests/integration/owned-lock-lost-update.test.d.ts.map +1 -0
- package/dist/tests/integration/owned-lock-lost-update.test.js +52 -0
- package/dist/tests/integration/owned-lock-lost-update.test.js.map +1 -0
- package/dist/tests/integration/owned-lock-reclaim-race.test.d.ts +19 -0
- package/dist/tests/integration/owned-lock-reclaim-race.test.d.ts.map +1 -0
- package/dist/tests/integration/owned-lock-reclaim-race.test.js +177 -0
- package/dist/tests/integration/owned-lock-reclaim-race.test.js.map +1 -0
- package/dist/tests/unit/services/skill-installation.content.test.d.ts +10 -0
- package/dist/tests/unit/services/skill-installation.content.test.d.ts.map +1 -0
- package/dist/tests/unit/services/skill-installation.content.test.js +290 -0
- package/dist/tests/unit/services/skill-installation.content.test.js.map +1 -0
- package/dist/tests/unit/services/skill-installation.io.symlink.test.d.ts +8 -0
- package/dist/tests/unit/services/skill-installation.io.symlink.test.d.ts.map +1 -0
- package/dist/tests/unit/services/skill-installation.io.symlink.test.js +72 -0
- package/dist/tests/unit/services/skill-installation.io.symlink.test.js.map +1 -0
- package/dist/tests/unit/services/skill-installation.service.error-codes.test.d.ts +15 -0
- package/dist/tests/unit/services/skill-installation.service.error-codes.test.d.ts.map +1 -0
- package/dist/tests/unit/services/skill-installation.service.error-codes.test.js +190 -0
- package/dist/tests/unit/services/skill-installation.service.error-codes.test.js.map +1 -0
- package/dist/tests/unit/services/skill-installation.service.multi-client.test.d.ts +17 -0
- package/dist/tests/unit/services/skill-installation.service.multi-client.test.d.ts.map +1 -0
- package/dist/tests/unit/services/skill-installation.service.multi-client.test.js +126 -0
- package/dist/tests/unit/services/skill-installation.service.multi-client.test.js.map +1 -0
- package/dist/tests/unit/services/skill-installation.service.test.js +7 -112
- package/dist/tests/unit/services/skill-installation.service.test.js.map +1 -1
- package/dist/tests/utils/quiet-mode.test.d.ts +12 -0
- package/dist/tests/utils/quiet-mode.test.d.ts.map +1 -0
- package/dist/tests/utils/quiet-mode.test.js +42 -0
- package/dist/tests/utils/quiet-mode.test.js.map +1 -0
- package/package.json +17 -5
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SMI-5879 Wave 3 item 2 — structural closure test for cohort E's exclusion
|
|
3
|
+
* proof (design doc `smi-5879-edge-twin-parity-design.md` §8.3.1.2.2).
|
|
4
|
+
* @module @skillsmith/core/security/scanner/multiline-category-closure.test
|
|
5
|
+
*
|
|
6
|
+
* WHY THIS TEST EXISTS
|
|
7
|
+
*
|
|
8
|
+
* The SMI-5879 census (comparing pre-port vs post-port `skills.security_score`
|
|
9
|
+
* across ~344k rows) needs to fully re-simulate every row scoring `>= 8`
|
|
10
|
+
* (cohort C1) but can structurally EXCLUDE every row scoring `0-7` (cohort E)
|
|
11
|
+
* from that expensive re-simulation — *if and only if* three properties about
|
|
12
|
+
* how the multiline pass routes patterns hold. The design doc's §8.3.1.2.1
|
|
13
|
+
* proves those properties in prose ("P1/P2/P3"); this file promotes that proof
|
|
14
|
+
* to a machine-checked gate (design doc gate G-5) so a future source change
|
|
15
|
+
* that silently invalidates the premise fails CI instead of silently
|
|
16
|
+
* corrupting the census's soundness.
|
|
17
|
+
*
|
|
18
|
+
* THE +32 ARITHMETIC THIS TEST PROTECTS (not itself re-derived here — that is
|
|
19
|
+
* `weights.ts` + the design doc's §8.3.1.2 arithmetic; this file only proves
|
|
20
|
+
* the ROUTING facts the arithmetic depends on):
|
|
21
|
+
* max joint contribution = 100 * CATEGORY_WEIGHTS.jailbreak.coefficient (0.20)
|
|
22
|
+
* + 100 * CATEGORY_WEIGHTS.ai_defence.coefficient (0.12)
|
|
23
|
+
* = 32.0
|
|
24
|
+
* S <= 7 => S' <= S + 32 <= 39 < 40 (QUARANTINE_THRESHOLD) => verdict
|
|
25
|
+
* cannot flip to quarantined for any row starting at or below 7.
|
|
26
|
+
* That bound is sound ONLY IF the multiline pass can never inflate any
|
|
27
|
+
* category's subtotal OTHER than jailbreak / ai_defence. The three assertions
|
|
28
|
+
* below are exactly what closes that "only if".
|
|
29
|
+
*
|
|
30
|
+
* THE DOCUMENTED TRAP (design doc §8.3.1.2.3) — DELIBERATELY NOT REPEATED HERE
|
|
31
|
+
*
|
|
32
|
+
* The design doc warns against writing assertion 3 as "every pattern for
|
|
33
|
+
* which `isMultilinePattern()` returns true belongs to an AI-category array"
|
|
34
|
+
* — that predicate is syntactic (it fires on a NEGATED class `[^\n]`, the
|
|
35
|
+
* literal opposite of "spans lines") and is false today for 9 patterns across
|
|
36
|
+
* two non-AI arrays. This file does not use that predicate at all: assertions
|
|
37
|
+
* 1 and 3 are asserted over the multiline pass's CALL SITES (which pattern
|
|
38
|
+
* array is physically wired to `scanPatternsWithMultilineSupport`'s `patterns:`
|
|
39
|
+
* config field), not over any per-pattern syntactic test. That is the
|
|
40
|
+
* "routing, not predicate" distinction the trap section requires.
|
|
41
|
+
*
|
|
42
|
+
* SMI-5881 ADAPTATION (judgment call — flagged, not silently resolved)
|
|
43
|
+
*
|
|
44
|
+
* The design doc's assertion 2 is written directly against `isMultilinePattern()`
|
|
45
|
+
* as the definition of "PASS-1 pattern". SMI-5881 (merged before this file was
|
|
46
|
+
* written) DELETED `isMultilinePattern()` entirely — no compatibility shim —
|
|
47
|
+
* and replaced the syntactic heuristic with `PATTERN_SCOPE` /
|
|
48
|
+
* `resolvePatternScope()` (`./patterns.scope.ts`), an explicit per-pattern
|
|
49
|
+
* `'line' | 'content' | 'both'` declaration. `isMultilinePattern` cannot be
|
|
50
|
+
* imported here; it no longer exists anywhere under `packages/core/src`
|
|
51
|
+
* (guarded by `packages/core/tests/security/pattern-scope.test.ts`).
|
|
52
|
+
*
|
|
53
|
+
* `scanPatternsWithMultilineSupport`'s pass 1 (SecurityScanner.helpers.ts) now
|
|
54
|
+
* reads: `if (resolvePatternScope(pattern) === 'line') continue` — i.e. pass 1
|
|
55
|
+
* tests every pattern whose resolved scope is NOT `'line'` (`'content'` or
|
|
56
|
+
* `'both'`). That predicate is the faithful, current-source equivalent of the
|
|
57
|
+
* design doc's `routed.filter(isMultilinePattern)` — same set membership
|
|
58
|
+
* question ("which patterns does pass 1 actually read"), expressed against
|
|
59
|
+
* the model that replaced the deleted one. Assertion 2 below uses
|
|
60
|
+
* `resolvePatternScope(p) !== 'line'` for exactly this reason.
|
|
61
|
+
*
|
|
62
|
+
* CODE-REVIEW REMEDIATION (Finding 1 — NO-GO, fixed)
|
|
63
|
+
*
|
|
64
|
+
* A first version of this file enumerated call sites with a bounded
|
|
65
|
+
* text-window regex scan (`CALL_TOKEN = 'scanPatternsWithMultilineSupport('`
|
|
66
|
+
* + an `EXTRACTION_WINDOW`-char lookahead for `type:`/`patterns:`). Codex
|
|
67
|
+
* (gpt-5.6-sol) review flagged that as a silent-false-negative hazard: a new
|
|
68
|
+
* call site using extra whitespace before `(`, a locally-aliased identifier,
|
|
69
|
+
* or a renamed import would be invisible to that regex while the existing two
|
|
70
|
+
* call sites kept the count green — defeating the point of a closure proof
|
|
71
|
+
* meant to catch exactly this class of drift. `extractMultilineCallSites`
|
|
72
|
+
* below instead walks the real TypeScript AST (`ts.createSourceFile` +
|
|
73
|
+
* `CallExpression` traversal resolved through `collectAliases`), which is
|
|
74
|
+
* exhaustive by construction against all three variants. Verified via mutation
|
|
75
|
+
* testing: a temporarily-added call through a whitespace-before-paren AND a
|
|
76
|
+
* locally-aliased form was both correctly detected (count became 3, assertion
|
|
77
|
+
* 1 failed) before being reverted.
|
|
78
|
+
*/
|
|
79
|
+
import { readFileSync } from 'node:fs';
|
|
80
|
+
import { join } from 'node:path';
|
|
81
|
+
import { fileURLToPath } from 'node:url';
|
|
82
|
+
import { describe, it, expect } from 'vitest';
|
|
83
|
+
import * as ts from 'typescript';
|
|
84
|
+
import { JAILBREAK_PATTERNS, AI_DEFENCE_PATTERNS } from './patterns.jailbreak.js';
|
|
85
|
+
import { EVIDENCE_TYPE_BY_PATTERN } from './patterns.jailbreak.evidence.js';
|
|
86
|
+
import { resolvePatternScope } from './patterns.scope.js';
|
|
87
|
+
const __dirname = fileURLToPath(new URL('.', import.meta.url));
|
|
88
|
+
const SCANNER_PATH = join(__dirname, 'SecurityScanner.ts');
|
|
89
|
+
const SCANNER_SRC = readFileSync(SCANNER_PATH, 'utf-8');
|
|
90
|
+
/** The only two pattern arrays a `scanPatternsWithMultilineSupport` call site may name. */
|
|
91
|
+
const AI_CATEGORY_ARRAY_NAMES = ['JAILBREAK_PATTERNS', 'AI_DEFENCE_PATTERNS'];
|
|
92
|
+
/**
|
|
93
|
+
* Every OTHER pattern array in the scanner (design doc §8.3.1.2.2 assertion 3's
|
|
94
|
+
* explicit exclusion list). `BLOCKED_PATTERNS` is not a real exported const
|
|
95
|
+
* (blocked patterns are an instance-level `ScannerOptions.blockedPatterns`,
|
|
96
|
+
* `SecurityScanner.ts:73`) — kept in the list anyway because the design doc
|
|
97
|
+
* names it explicitly and the check is "this identifier never appears as a
|
|
98
|
+
* `patterns:` value", which is true (and meaningfully checked) regardless of
|
|
99
|
+
* whether the identifier is otherwise reachable.
|
|
100
|
+
*/
|
|
101
|
+
const NON_AI_ARRAY_NAMES = [
|
|
102
|
+
'SUSPICIOUS_PATTERNS',
|
|
103
|
+
'DATA_EXFILTRATION_PATTERNS',
|
|
104
|
+
'PRIVILEGE_ESCALATION_PATTERNS',
|
|
105
|
+
'CODE_EXECUTION_PATTERNS',
|
|
106
|
+
'BLOCKED_PATTERNS',
|
|
107
|
+
];
|
|
108
|
+
const TARGET_FUNCTION_NAME = 'scanPatternsWithMultilineSupport';
|
|
109
|
+
/** The textual callee name of a call expression: `foo(` -> "foo"; `x.bar(` -> "bar". */
|
|
110
|
+
function calleeName(expr) {
|
|
111
|
+
if (ts.isIdentifier(expr))
|
|
112
|
+
return expr.text;
|
|
113
|
+
if (ts.isPropertyAccessExpression(expr))
|
|
114
|
+
return expr.name.text;
|
|
115
|
+
return undefined;
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Every local identifier in `sourceFile` that resolves to `targetName` — the
|
|
119
|
+
* name itself, any `import { targetName as Local }` rename, and any simple
|
|
120
|
+
* `const Local = <alias>` variable alias (resolved transitively, to a fixed
|
|
121
|
+
* point, so a chain like `const a = target; const b = a` also resolves).
|
|
122
|
+
* This is what makes the call-site walk below exhaustive against the "an
|
|
123
|
+
* alias" failure mode the code review named: a call reached through a
|
|
124
|
+
* renamed import or a locally-aliased identifier still resolves back to
|
|
125
|
+
* `targetName` and is detected, where a literal-token text scan would miss it.
|
|
126
|
+
*/
|
|
127
|
+
function collectAliases(sourceFile, targetName) {
|
|
128
|
+
const aliases = new Set([targetName]);
|
|
129
|
+
for (const stmt of sourceFile.statements) {
|
|
130
|
+
if (!ts.isImportDeclaration(stmt) || !stmt.importClause?.namedBindings)
|
|
131
|
+
continue;
|
|
132
|
+
const bindings = stmt.importClause.namedBindings;
|
|
133
|
+
if (!ts.isNamedImports(bindings))
|
|
134
|
+
continue;
|
|
135
|
+
for (const el of bindings.elements) {
|
|
136
|
+
const imported = el.propertyName?.text ?? el.name.text;
|
|
137
|
+
if (imported === targetName)
|
|
138
|
+
aliases.add(el.name.text);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
let changed = true;
|
|
142
|
+
while (changed) {
|
|
143
|
+
changed = false;
|
|
144
|
+
const visit = (node) => {
|
|
145
|
+
if (ts.isVariableDeclaration(node) &&
|
|
146
|
+
ts.isIdentifier(node.name) &&
|
|
147
|
+
node.initializer &&
|
|
148
|
+
ts.isIdentifier(node.initializer) &&
|
|
149
|
+
aliases.has(node.initializer.text) &&
|
|
150
|
+
!aliases.has(node.name.text)) {
|
|
151
|
+
aliases.add(node.name.text);
|
|
152
|
+
changed = true;
|
|
153
|
+
}
|
|
154
|
+
ts.forEachChild(node, visit);
|
|
155
|
+
};
|
|
156
|
+
ts.forEachChild(sourceFile, visit);
|
|
157
|
+
}
|
|
158
|
+
return aliases;
|
|
159
|
+
}
|
|
160
|
+
/** The textual name of an object-literal member (identifier or string-literal key). */
|
|
161
|
+
function memberName(prop) {
|
|
162
|
+
if (!prop.name)
|
|
163
|
+
return undefined;
|
|
164
|
+
if (ts.isIdentifier(prop.name))
|
|
165
|
+
return prop.name.text;
|
|
166
|
+
if (ts.isStringLiteral(prop.name))
|
|
167
|
+
return prop.name.text;
|
|
168
|
+
return undefined;
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Extract every `scanPatternsWithMultilineSupport(...)` call site in `source`
|
|
172
|
+
* (at path `filePath`, used only for error messages), together with its
|
|
173
|
+
* config object's `type:` and `patterns:` values — via full TypeScript AST
|
|
174
|
+
* traversal (`ts.createSourceFile` + a `CallExpression` walk resolved through
|
|
175
|
+
* `collectAliases`), NOT text/regex matching over a bounded window. This is
|
|
176
|
+
* exhaustive by construction: extra whitespace before `(`, a call reached
|
|
177
|
+
* through a renamed import or a locally-aliased identifier, and a config
|
|
178
|
+
* object appearing in any argument position are all still found — closing
|
|
179
|
+
* the class of silent false-negative a bounded text-window regex scan cannot
|
|
180
|
+
* rule out (the defect the code review's Finding 1 identified).
|
|
181
|
+
*
|
|
182
|
+
* Throws (rather than silently skipping) when a matched call's arguments
|
|
183
|
+
* don't have the expected `{ type: '<string literal>', patterns: <bare
|
|
184
|
+
* identifier> }` shape — an unparseable/unexpected call site must fail
|
|
185
|
+
* loudly, not be dropped from the count assertion 1 depends on, exactly as
|
|
186
|
+
* the prior text-based version did for its own class of unparseable input.
|
|
187
|
+
*/
|
|
188
|
+
function extractMultilineCallSites(filePath, source) {
|
|
189
|
+
const sourceFile = ts.createSourceFile(filePath, source, ts.ScriptTarget.Latest, true, ts.ScriptKind.TS);
|
|
190
|
+
const aliases = collectAliases(sourceFile, TARGET_FUNCTION_NAME);
|
|
191
|
+
const sites = [];
|
|
192
|
+
const visit = (node) => {
|
|
193
|
+
if (ts.isCallExpression(node)) {
|
|
194
|
+
const name = calleeName(node.expression);
|
|
195
|
+
if (name && aliases.has(name)) {
|
|
196
|
+
sites.push(extractCallSiteConfig(node, sourceFile, filePath));
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
ts.forEachChild(node, visit);
|
|
200
|
+
};
|
|
201
|
+
visit(sourceFile);
|
|
202
|
+
return sites;
|
|
203
|
+
}
|
|
204
|
+
/** Extract and validate the `{ type, patterns }` config object of a matched call. */
|
|
205
|
+
function extractCallSiteConfig(call, sourceFile, filePath) {
|
|
206
|
+
const offset = call.getStart(sourceFile);
|
|
207
|
+
const configArg = call.arguments.find((arg) => ts.isObjectLiteralExpression(arg) &&
|
|
208
|
+
arg.properties.some((p) => memberName(p) === 'type') &&
|
|
209
|
+
arg.properties.some((p) => memberName(p) === 'patterns'));
|
|
210
|
+
if (!configArg) {
|
|
211
|
+
throw new Error(`[multiline-category-closure] ${TARGET_FUNCTION_NAME} call at ${filePath}:${offset} has no ` +
|
|
212
|
+
`object-literal argument with both 'type' and 'patterns' properties. Either the call shape ` +
|
|
213
|
+
`changed (update this extraction) or this is a genuine new/malformed call site — both ` +
|
|
214
|
+
`invalidate the closure proof and must be resolved before this test can pass.`);
|
|
215
|
+
}
|
|
216
|
+
const typeProp = configArg.properties.find((p) => memberName(p) === 'type');
|
|
217
|
+
const patternsProp = configArg.properties.find((p) => memberName(p) === 'patterns');
|
|
218
|
+
if (!typeProp ||
|
|
219
|
+
!ts.isPropertyAssignment(typeProp) ||
|
|
220
|
+
!ts.isStringLiteral(typeProp.initializer)) {
|
|
221
|
+
throw new Error(`[multiline-category-closure] ${TARGET_FUNCTION_NAME} call at ${filePath}:${offset} has a ` +
|
|
222
|
+
`'type' property that is not a plain string literal — this is exactly the "expression ` +
|
|
223
|
+
`argument" shape this AST census exists to catch. Resolve explicitly before this test can pass.`);
|
|
224
|
+
}
|
|
225
|
+
if (!patternsProp ||
|
|
226
|
+
!ts.isPropertyAssignment(patternsProp) ||
|
|
227
|
+
!ts.isIdentifier(patternsProp.initializer)) {
|
|
228
|
+
throw new Error(`[multiline-category-closure] ${TARGET_FUNCTION_NAME} call at ${filePath}:${offset} has a ` +
|
|
229
|
+
`'patterns' property that is not a bare identifier — either a property-access / expression ` +
|
|
230
|
+
`argument was introduced (the exact "silent miss" class this test exists to catch) or the ` +
|
|
231
|
+
`call shape genuinely changed. Resolve explicitly before this test can pass.`);
|
|
232
|
+
}
|
|
233
|
+
return {
|
|
234
|
+
offset,
|
|
235
|
+
type: typeProp.initializer.text,
|
|
236
|
+
patternsIdent: patternsProp.initializer.text,
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
describe('SMI-5879 Wave 3 item 2 — multiline-pass category closure (design doc §8.3.1.2.2)', () => {
|
|
240
|
+
const callSites = extractMultilineCallSites(SCANNER_PATH, SCANNER_SRC);
|
|
241
|
+
// --------------------------------------------------------------------
|
|
242
|
+
// Assertion 1 — the multiline pass has exactly two call sites, both AI-category
|
|
243
|
+
// --------------------------------------------------------------------
|
|
244
|
+
describe('assertion 1 — exactly two call sites, both AI-category (design doc P1/P2)', () => {
|
|
245
|
+
it('scanPatternsWithMultilineSupport is called exactly twice in SecurityScanner.ts', () => {
|
|
246
|
+
expect(callSites).toHaveLength(2);
|
|
247
|
+
});
|
|
248
|
+
it('the extracted type: multiset equals exactly [ai_defence, jailbreak] — no more, no fewer, no substitution', () => {
|
|
249
|
+
const types = callSites.map((s) => s.type).sort();
|
|
250
|
+
expect(types).toEqual(['ai_defence', 'jailbreak']);
|
|
251
|
+
});
|
|
252
|
+
});
|
|
253
|
+
// --------------------------------------------------------------------
|
|
254
|
+
// Assertion 2 — every PASS-1 pattern is explicitly classified
|
|
255
|
+
// --------------------------------------------------------------------
|
|
256
|
+
describe('assertion 2 — every pass-1 pattern is explicitly classified (SMI-5881-adapted)', () => {
|
|
257
|
+
// "Pass 1" = every pattern scanPatternsWithMultilineSupport's first pass
|
|
258
|
+
// actually reads: resolvePatternScope(pattern) !== 'line' (see the
|
|
259
|
+
// SMI-5881 ADAPTATION module-doc comment above for why this replaces the
|
|
260
|
+
// design doc's isMultilinePattern-filter wording).
|
|
261
|
+
const routed = [...JAILBREAK_PATTERNS, ...AI_DEFENCE_PATTERNS];
|
|
262
|
+
const pass1 = routed.filter((p) => resolvePatternScope(p) !== 'line');
|
|
263
|
+
it('the pass-1 subset is non-empty — the pass is not vacuous', () => {
|
|
264
|
+
expect(pass1.length).toBeGreaterThan(0);
|
|
265
|
+
});
|
|
266
|
+
it('every pass-1 pattern has an explicit EVIDENCE_TYPE_BY_PATTERN entry (object identity, not fail-closed default)', () => {
|
|
267
|
+
for (const p of pass1) {
|
|
268
|
+
expect(EVIDENCE_TYPE_BY_PATTERN.has(p), `pattern /${p.source}/${p.flags} has no explicit evidence-tier entry`).toBe(true);
|
|
269
|
+
}
|
|
270
|
+
});
|
|
271
|
+
});
|
|
272
|
+
// --------------------------------------------------------------------
|
|
273
|
+
// Assertion 3 — no non-AI pattern array is routed through the multiline pass
|
|
274
|
+
// --------------------------------------------------------------------
|
|
275
|
+
describe('assertion 3 — no non-AI pattern array is routed through the multiline pass (design doc P3, NOT the isMultilinePattern predicate)', () => {
|
|
276
|
+
it('every call site patterns: identifier is one of the two AI-category arrays', () => {
|
|
277
|
+
for (const site of callSites) {
|
|
278
|
+
expect(AI_CATEGORY_ARRAY_NAMES, `call site at offset ${site.offset} routes ${site.patternsIdent}, not an AI-category array`).toContain(site.patternsIdent);
|
|
279
|
+
}
|
|
280
|
+
});
|
|
281
|
+
it.each(NON_AI_ARRAY_NAMES)('%s never appears as a patterns: identifier at any call site', (arrayName) => {
|
|
282
|
+
const idents = callSites.map((s) => s.patternsIdent);
|
|
283
|
+
expect(idents).not.toContain(arrayName);
|
|
284
|
+
});
|
|
285
|
+
});
|
|
286
|
+
});
|
|
287
|
+
//# sourceMappingURL=multiline-category-closure.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"multiline-category-closure.test.js","sourceRoot":"","sources":["../../../../src/security/scanner/multiline-category-closure.test.ts"],"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;gBAC7B,MAAM,CACJ,uBAA4C,EAC5C,uBAAuB,IAAI,CAAC,MAAM,WAAW,IAAI,CAAC,aAAa,4BAA4B,CAC5F,CAAC,SAAS,CAAC,IAAI,CAAC,aAAa,CAAC,CAAA;YACjC,CAAC;QACH,CAAC,CAAC,CAAA;QAEF,EAAE,CAAC,IAAI,CAAC,kBAAkB,CAAC,CACzB,6DAA6D,EAC7D,CAAC,SAAS,EAAE,EAAE;YACZ,MAAM,MAAM,GAAG,SAAS,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,aAAa,CAAC,CAAA;YACpD,MAAM,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,SAAS,CAAC,SAAS,CAAC,CAAA;QACzC,CAAC,CACF,CAAA;IACH,CAAC,CAAC,CAAA;AACJ,CAAC,CAAC,CAAA"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Shared empty-derived-stack guard for skill recommendations.
|
|
3
|
+
* @module @skillsmith/core/services/recommend-guard
|
|
4
|
+
* @see SMI-5896 (Wave 3, discovery-tool consistency): CLI `recommend` and MCP
|
|
5
|
+
* `skill_recommend` each derive a technology "stack" client-side — CLI from
|
|
6
|
+
* codebase analysis (`buildStackFromAnalysis`), MCP from installed skills +
|
|
7
|
+
* project context keywords — and either can legitimately derive `[]` (a
|
|
8
|
+
* non-Node stack, an all-devDeps project, or an unsupported language). The
|
|
9
|
+
* `skills-recommend` edge function hard-rejects an empty `stack` with a 400
|
|
10
|
+
* (defense in depth, intentionally unchanged by this fix — see the plan's
|
|
11
|
+
* Wave 3 Step 2 "decided contract").
|
|
12
|
+
*
|
|
13
|
+
* Both callers now detect the empty-stack case BEFORE calling the API
|
|
14
|
+
* instead of letting the guaranteed-400 round trip happen and reacting to
|
|
15
|
+
* the failure after the fact — this is the one guidance string they both
|
|
16
|
+
* surface, so it can't drift between CLI and MCP wording the way the two
|
|
17
|
+
* tools' pre-fix behavior already had (a hard `process.exit(1)` crash on
|
|
18
|
+
* CLI vs a silently-swallowed warning on MCP for the exact same input
|
|
19
|
+
* shape).
|
|
20
|
+
*/
|
|
21
|
+
/**
|
|
22
|
+
* Guidance shown when no technology stack could be derived for
|
|
23
|
+
* recommendations. Written to read naturally from both a terminal (CLI) and
|
|
24
|
+
* an MCP tool response consumed by a calling agent — neither surface names
|
|
25
|
+
* its own flag here, since the same string serves both.
|
|
26
|
+
*/
|
|
27
|
+
export declare function buildEmptyStackGuidance(): string;
|
|
28
|
+
//# sourceMappingURL=recommend-guard.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"recommend-guard.d.ts","sourceRoot":"","sources":["../../../src/services/recommend-guard.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AAEH;;;;;GAKG;AACH,wBAAgB,uBAAuB,IAAI,MAAM,CAQhD"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Shared empty-derived-stack guard for skill recommendations.
|
|
3
|
+
* @module @skillsmith/core/services/recommend-guard
|
|
4
|
+
* @see SMI-5896 (Wave 3, discovery-tool consistency): CLI `recommend` and MCP
|
|
5
|
+
* `skill_recommend` each derive a technology "stack" client-side — CLI from
|
|
6
|
+
* codebase analysis (`buildStackFromAnalysis`), MCP from installed skills +
|
|
7
|
+
* project context keywords — and either can legitimately derive `[]` (a
|
|
8
|
+
* non-Node stack, an all-devDeps project, or an unsupported language). The
|
|
9
|
+
* `skills-recommend` edge function hard-rejects an empty `stack` with a 400
|
|
10
|
+
* (defense in depth, intentionally unchanged by this fix — see the plan's
|
|
11
|
+
* Wave 3 Step 2 "decided contract").
|
|
12
|
+
*
|
|
13
|
+
* Both callers now detect the empty-stack case BEFORE calling the API
|
|
14
|
+
* instead of letting the guaranteed-400 round trip happen and reacting to
|
|
15
|
+
* the failure after the fact — this is the one guidance string they both
|
|
16
|
+
* surface, so it can't drift between CLI and MCP wording the way the two
|
|
17
|
+
* tools' pre-fix behavior already had (a hard `process.exit(1)` crash on
|
|
18
|
+
* CLI vs a silently-swallowed warning on MCP for the exact same input
|
|
19
|
+
* shape).
|
|
20
|
+
*/
|
|
21
|
+
/**
|
|
22
|
+
* Guidance shown when no technology stack could be derived for
|
|
23
|
+
* recommendations. Written to read naturally from both a terminal (CLI) and
|
|
24
|
+
* an MCP tool response consumed by a calling agent — neither surface names
|
|
25
|
+
* its own flag here, since the same string serves both.
|
|
26
|
+
*/
|
|
27
|
+
export function buildEmptyStackGuidance() {
|
|
28
|
+
return ('No technology stack could be derived for recommendations — this usually ' +
|
|
29
|
+
'means a non-Node project, a stack with no production dependencies, or an ' +
|
|
30
|
+
'unsupported language, not a backend or registry problem. Provide project ' +
|
|
31
|
+
'context (a short description of the project or its tooling) or an ' +
|
|
32
|
+
'explicit list of installed/currently-used skills, then try again.');
|
|
33
|
+
}
|
|
34
|
+
//# sourceMappingURL=recommend-guard.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"recommend-guard.js","sourceRoot":"","sources":["../../../src/services/recommend-guard.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AAEH;;;;;GAKG;AACH,MAAM,UAAU,uBAAuB;IACrC,OAAO,CACL,0EAA0E;QAC1E,2EAA2E;QAC3E,2EAA2E;QAC3E,oEAAoE;QACpE,mEAAmE,CACpE,CAAA;AACH,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"recommend-guard.test.d.ts","sourceRoot":"","sources":["../../../src/services/recommend-guard.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Unit tests for the shared empty-derived-stack guidance string
|
|
3
|
+
* (SMI-5896 Wave 3 Step 2). Both CLI `recommend` and MCP `skill_recommend`
|
|
4
|
+
* surface this exact string so their empty-stack messaging can't drift.
|
|
5
|
+
* @module @skillsmith/core/services/recommend-guard.test
|
|
6
|
+
*/
|
|
7
|
+
import { describe, expect, it } from 'vitest';
|
|
8
|
+
import { buildEmptyStackGuidance } from './recommend-guard.js';
|
|
9
|
+
describe('buildEmptyStackGuidance (SMI-5896 Wave 3 Step 2)', () => {
|
|
10
|
+
it('returns a non-empty string', () => {
|
|
11
|
+
const guidance = buildEmptyStackGuidance();
|
|
12
|
+
expect(typeof guidance).toBe('string');
|
|
13
|
+
expect(guidance.length).toBeGreaterThan(0);
|
|
14
|
+
});
|
|
15
|
+
it('explains the empty stack is a legitimate under-detection, not a backend fault', () => {
|
|
16
|
+
const guidance = buildEmptyStackGuidance();
|
|
17
|
+
expect(guidance).toContain('No technology stack could be derived');
|
|
18
|
+
});
|
|
19
|
+
it('guides the caller toward providing context or an installed-skills list', () => {
|
|
20
|
+
const guidance = buildEmptyStackGuidance();
|
|
21
|
+
expect(guidance.toLowerCase()).toContain('project context');
|
|
22
|
+
expect(guidance.toLowerCase()).toContain('installed');
|
|
23
|
+
});
|
|
24
|
+
it('is deterministic (identical wording every call) so CLI and MCP never drift apart', () => {
|
|
25
|
+
expect(buildEmptyStackGuidance()).toBe(buildEmptyStackGuidance());
|
|
26
|
+
});
|
|
27
|
+
});
|
|
28
|
+
//# sourceMappingURL=recommend-guard.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"recommend-guard.test.js","sourceRoot":"","sources":["../../../src/services/recommend-guard.test.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,QAAQ,CAAA;AAE7C,OAAO,EAAE,uBAAuB,EAAE,MAAM,sBAAsB,CAAA;AAE9D,QAAQ,CAAC,kDAAkD,EAAE,GAAG,EAAE;IAChE,EAAE,CAAC,4BAA4B,EAAE,GAAG,EAAE;QACpC,MAAM,QAAQ,GAAG,uBAAuB,EAAE,CAAA;QAC1C,MAAM,CAAC,OAAO,QAAQ,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAA;QACtC,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,eAAe,CAAC,CAAC,CAAC,CAAA;IAC5C,CAAC,CAAC,CAAA;IAEF,EAAE,CAAC,+EAA+E,EAAE,GAAG,EAAE;QACvF,MAAM,QAAQ,GAAG,uBAAuB,EAAE,CAAA;QAC1C,MAAM,CAAC,QAAQ,CAAC,CAAC,SAAS,CAAC,sCAAsC,CAAC,CAAA;IACpE,CAAC,CAAC,CAAA;IAEF,EAAE,CAAC,wEAAwE,EAAE,GAAG,EAAE;QAChF,MAAM,QAAQ,GAAG,uBAAuB,EAAE,CAAA;QAC1C,MAAM,CAAC,QAAQ,CAAC,WAAW,EAAE,CAAC,CAAC,SAAS,CAAC,iBAAiB,CAAC,CAAA;QAC3D,MAAM,CAAC,QAAQ,CAAC,WAAW,EAAE,CAAC,CAAC,SAAS,CAAC,WAAW,CAAC,CAAA;IACvD,CAAC,CAAC,CAAA;IAEF,EAAE,CAAC,kFAAkF,EAAE,GAAG,EAAE;QAC1F,MAAM,CAAC,uBAAuB,EAAE,CAAC,CAAC,IAAI,CAAC,uBAAuB,EAAE,CAAC,CAAA;IACnE,CAAC,CAAC,CAAA;AACJ,CAAC,CAAC,CAAA"}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Content-based skill install path (private registry).
|
|
3
|
+
* @module @skillsmith/core/services/skill-installation.content
|
|
4
|
+
* @see SMI-5905 Wave 1: installFromContent() + resolveFreshAccessToken() extraction
|
|
5
|
+
* @see docs/internal/implementation/private-registry-skill-install.md
|
|
6
|
+
*
|
|
7
|
+
* `install()` (skill-installation.service.ts) only knows how to fetch a
|
|
8
|
+
* skill from GitHub. This module adds a second entry point,
|
|
9
|
+
* `installFromContent()`, for skills whose content has already been resolved
|
|
10
|
+
* elsewhere (a private-registry `content` JSONB column, read via a
|
|
11
|
+
* per-team-member Supabase session or the `private-registry-get` Edge
|
|
12
|
+
* Function — Waves 2/3 wire those transports; this module only needs the
|
|
13
|
+
* already-fetched `{skillId, version, content}` triple).
|
|
14
|
+
*
|
|
15
|
+
* SCOPE TRIM (Sol review #5 — read before extending this function):
|
|
16
|
+
* `installFromContent()` deliberately reuses only two things from the
|
|
17
|
+
* `install()` policy chain:
|
|
18
|
+
* 1. `writeInstallFiles()`'s disk-write + rollback-on-partial-write handling
|
|
19
|
+
* (skill-installation.io.ts), plus this module's own manifest-driven
|
|
20
|
+
* already-installed/force gate (mirrors `install()`'s ALREADY_INSTALLED
|
|
21
|
+
* check).
|
|
22
|
+
* 2. A security scan at the `community` trust tier, via the same
|
|
23
|
+
* `classifyBundledFile()`/`isRejectableScan()` policy `install()` uses
|
|
24
|
+
* for optional bundled files (skill-installation.policy.ts) — applied
|
|
25
|
+
* here to every content-map entry (including "SKILL.md" itself, which
|
|
26
|
+
* falls through to the conservative 'structured' default class since it
|
|
27
|
+
* has no BUNDLED_SCAN_FILES entry).
|
|
28
|
+
*
|
|
29
|
+
* It deliberately does NOT route through the rest of `install()`'s chain:
|
|
30
|
+
* - `skipScan` is NOT an accepted option here. Registry-content installs
|
|
31
|
+
* always scan — there is no opt-out, unlike `install()`'s tier-gated
|
|
32
|
+
* skipScan.
|
|
33
|
+
* - Dependency-intelligence extraction/persistence (extractDepIntel,
|
|
34
|
+
* persistDependencies, checkDepsAgainstQuarantine), risk-history
|
|
35
|
+
* recording, AI-defence feedback, and co-install-session recording are
|
|
36
|
+
* all OUT of v1 scope — a known, intentional gap, not a silent drop.
|
|
37
|
+
* Wave 3's MCP `install` action and Wave 4's CLI command do not get
|
|
38
|
+
* these signals for private-registry installs yet.
|
|
39
|
+
* - Frontmatter/manifest validation on `SKILL.md` itself DOES still run
|
|
40
|
+
* (`validateSkillMd()`), same as any other install.
|
|
41
|
+
*
|
|
42
|
+
* PATH VALIDATION (Sol review #2, critical, confirmed exploitable): every key
|
|
43
|
+
* in `content` is attacker-controlled — any team member with publish access
|
|
44
|
+
* chooses these filenames — and flows into `writeInstallFiles()`'s
|
|
45
|
+
* `path.join(installPath, subSkill.filename)` (skill-installation.io.ts:167)
|
|
46
|
+
* with NO containment check of its own. `path.join('/a/b', '../../etc/passwd')`
|
|
47
|
+
* escapes `installPath` after normalization, so every key is validated here,
|
|
48
|
+
* BEFORE any disk write — see `validateContentKeys()`.
|
|
49
|
+
*/
|
|
50
|
+
import type { Database } from '../db/database-interface.js';
|
|
51
|
+
import type { ClientId } from '../install/paths.js';
|
|
52
|
+
import type { ProgressCallback, InstallFromContentOptions, InstallResult } from './skill-installation.types.js';
|
|
53
|
+
import type { ManifestManager } from './skill-manifest.js';
|
|
54
|
+
/**
|
|
55
|
+
* Validate every key in a content map before any disk write.
|
|
56
|
+
*
|
|
57
|
+
* Rejects (as a single combined error, first offense wins):
|
|
58
|
+
* - path-traversal (`..` segment), absolute paths (posix or win32/UNC),
|
|
59
|
+
* NUL bytes, backslashes, empty/bare-"."/".git"-adjacent keys;
|
|
60
|
+
* - any key that, after `path.resolve()` against `installPath`, resolves
|
|
61
|
+
* outside `installPath` (a lexical backstop behind the checks above,
|
|
62
|
+
* matching `writeInstallFiles()`'s own lexical escape check);
|
|
63
|
+
* - two keys that `path.normalize()` to the same on-disk path (a
|
|
64
|
+
* collision — e.g. "SKILL.md" and "./SKILL.md").
|
|
65
|
+
*/
|
|
66
|
+
export declare function validateContentKeys(content: Record<string, string>, installPath: string): {
|
|
67
|
+
valid: true;
|
|
68
|
+
} | {
|
|
69
|
+
valid: false;
|
|
70
|
+
error: string;
|
|
71
|
+
};
|
|
72
|
+
/** Internal params bag: the caller's InstallFromContentOptions plus the service state
|
|
73
|
+
* installFromContent() needs (mirrors performUninstall()'s flattened-params convention
|
|
74
|
+
* in skill-installation.helpers.ts). */
|
|
75
|
+
export interface InstallFromContentParams extends InstallFromContentOptions {
|
|
76
|
+
db: Database;
|
|
77
|
+
skillsDir: string;
|
|
78
|
+
manifest: ManifestManager;
|
|
79
|
+
client: ClientId;
|
|
80
|
+
onProgress: ProgressCallback;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Install an already-resolved private-registry skill's content to disk.
|
|
84
|
+
*
|
|
85
|
+
* See this file's header doc comment for the full scope trim vs. `install()`.
|
|
86
|
+
*/
|
|
87
|
+
export declare function installFromContent(params: InstallFromContentParams): Promise<InstallResult>;
|
|
88
|
+
//# sourceMappingURL=skill-installation.content.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"skill-installation.content.d.ts","sourceRoot":"","sources":["../../../src/services/skill-installation.content.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgDG;AAIH,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,6BAA6B,CAAA;AAC3D,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAA;AACnD,OAAO,KAAK,EACV,gBAAgB,EAChB,yBAAyB,EACzB,aAAa,EACd,MAAM,+BAA+B,CAAA;AAEtC,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAA;AA8C1D;;;;;;;;;;;GAWG;AACH,wBAAgB,mBAAmB,CACjC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,EAC/B,WAAW,EAAE,MAAM,GAClB;IAAE,KAAK,EAAE,IAAI,CAAA;CAAE,GAAG;IAAE,KAAK,EAAE,KAAK,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,CAgCnD;AA8DD;;yCAEyC;AACzC,MAAM,WAAW,wBAAyB,SAAQ,yBAAyB;IACzE,EAAE,EAAE,QAAQ,CAAA;IACZ,SAAS,EAAE,MAAM,CAAA;IACjB,QAAQ,EAAE,eAAe,CAAA;IACzB,MAAM,EAAE,QAAQ,CAAA;IAChB,UAAU,EAAE,gBAAgB,CAAA;CAC7B;AAED;;;;GAIG;AACH,wBAAsB,kBAAkB,CAAC,MAAM,EAAE,wBAAwB,GAAG,OAAO,CAAC,aAAa,CAAC,CAkKjG"}
|