@lornatang/dsh-codex-security 0.1.10 → 0.1.12
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 +14 -0
- package/README.md +1 -1
- package/README.zh.md +1 -1
- package/lib/constants.js +1 -2
- package/lib/deep-coordinator.js +67 -3
- package/lib/deep-worker.js +4 -1
- package/lib/git.js +0 -21
- package/lib/index.js +0 -1
- package/lib/routes.js +0 -5
- package/lib/service.js +28 -27
- package/lib/store.js +36 -14
- package/lib/target.js +48 -6
- package/lib/tools.js +47 -1
- package/lib/types/constants.d.ts +1 -2
- package/lib/types/deep-coordinator.d.ts +10 -0
- package/lib/types/git.d.ts +0 -3
- package/lib/types/index.d.ts +1 -2
- package/lib/types/routes.d.ts +0 -2
- package/lib/types/service.d.ts +0 -1
- package/lib/types/store.d.ts +5 -1
- package/lib/types/target.d.ts +10 -0
- package/lib/types/util.d.ts +0 -5
- package/lib/util.js +1 -45
- package/package.json +1 -1
- package/references/core-scan.md +1 -1
- package/references/final-report.md +1 -1
- package/references/scan-contract.md +1 -1
- package/schemas/tools/scan-draft.schema.json +0 -766
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,19 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [0.1.12] - 2026-11-19
|
|
4
|
+
|
|
5
|
+
- Read chunked evidence from the already verified worker text cache instead of re-reading and hashing the whole file per chunk.
|
|
6
|
+
- Serve scan listings from a cached index invalidated by index.json metadata instead of rebuilding and locking on every read.
|
|
7
|
+
- Read scan triage, findings and artifacts from the resolved scan directory in one pass instead of resolving the index repeatedly.
|
|
8
|
+
- Offload Deep worker payloads to per-worker sidecar files so journal transitions no longer rewrite every worker findings on each save, and detect merge collisions with a set.
|
|
9
|
+
- Group draft evidence reads by source file behind one bounded reader, and normalize tool results without a JSON round-trip.
|
|
10
|
+
|
|
11
|
+
## [0.1.11] - 2026-11-19
|
|
12
|
+
|
|
13
|
+
- Remove the unused scan-draft JSON schema and the three reference links that pointed at it; draft validation is unchanged and still enforced by the host.
|
|
14
|
+
- Delete dead helpers, imports and fields across the target reader, git helpers, constants, routes, service and store modules.
|
|
15
|
+
- Stop exporting symbols that nothing outside their module consumes.
|
|
16
|
+
|
|
3
17
|
## [0.1.10] - 2026-11-18
|
|
4
18
|
|
|
5
19
|
### Security
|
package/README.md
CHANGED
|
@@ -34,7 +34,7 @@ dsh plugin --profile <name> add @lornatang/dsh-codex-security
|
|
|
34
34
|
dsh plugin --profile <name> remove @lornatang/dsh-codex-security
|
|
35
35
|
```
|
|
36
36
|
|
|
37
|
-
For the GitHub 0.1.
|
|
37
|
+
For the GitHub 0.1.12 release, use `dsh plugin --profile <name> add https://github.com/Lornatang/dsh-codex-security/releases/download/v0.1.12/lornatang-dsh-codex-security-0.1.12.tgz`. The npm registry version may be older.
|
|
38
38
|
|
|
39
39
|
The `add` command composes the layer into the profile: the profile's `cordis.patch.yml` gains an insert entry, row id `codex-security`, whose `config` starts from the bundle defaults `maxFiles: 5000`, `maxFileBytes: 1048576`, and `defaultMode: standard`; every other field takes its default from `src/config.ts`. The package manifest points `dsh.bundle.patch` at that document. The `remove` command deletes the row and leaves scans already written under the profile data directory untouched.
|
|
40
40
|
|
package/README.zh.md
CHANGED
|
@@ -34,7 +34,7 @@ dsh plugin --profile <name> add @lornatang/dsh-codex-security
|
|
|
34
34
|
dsh plugin --profile <name> remove @lornatang/dsh-codex-security
|
|
35
35
|
```
|
|
36
36
|
|
|
37
|
-
安装 GitHub 上的 0.1.
|
|
37
|
+
安装 GitHub 上的 0.1.12 版本,请运行 `dsh plugin --profile <name> add https://github.com/Lornatang/dsh-codex-security/releases/download/v0.1.12/lornatang-dsh-codex-security-0.1.12.tgz`。npm 仓库中的版本可能较旧。
|
|
38
38
|
|
|
39
39
|
`add` 命令把这一层组合进 profile:profile 的 `cordis.patch.yml` 中新增一条 insert 条目,row id 为 `codex-security`,其 `config` 从本包默认值 `maxFiles: 5000`、`maxFileBytes: 1048576`、`defaultMode: standard` 开始;其余字段取 `src/config.ts` 中的默认值。包清单把 `dsh.bundle.patch` 指向该文档。`remove` 命令删除该条目,已经写入 profile 数据目录的扫描保持不变。
|
|
40
40
|
|
package/lib/constants.js
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
export const PACKAGE_NAME = '@lornatang/dsh-codex-security';
|
|
2
2
|
export const PLUGIN_NAME = 'codex-security';
|
|
3
|
-
export const VERSION = '0.1.
|
|
3
|
+
export const VERSION = '0.1.12';
|
|
4
4
|
export const ROUTE_PREFIX = '/plugins/codex-security';
|
|
5
5
|
export const PRODUCER = { name: '@lornatang/dsh-codex-security', version: VERSION };
|
|
6
|
-
export const DEFAULT_EXCLUDES = ['.git/**', 'node_modules/**', 'dist/**', 'build/**', 'out/**', 'coverage/**', '.next/**', '.nuxt/**', 'vendor/**', 'target/**', '.venv/**', '__pycache__/**'];
|
|
7
6
|
export const SKILL_NAMES = ['security-scan', 'security-diff-scan', 'deep-security-scan', 'fix-finding', 'verify-fix', 'triage-finding', 'threat-model', 'track-findings', 'resolve-dependency-alert', 'vulnerability-writeup', 'assess-patch-risk', 'attack-path-analysis', 'define-security-policy', 'finding-discovery', 'propose-security-hardening', 'validation'];
|
|
8
7
|
export const TOOL_NAMES = [
|
|
9
8
|
'codex_security_inspect_setup', 'codex_security_inspect_target',
|
package/lib/deep-coordinator.js
CHANGED
|
@@ -6,7 +6,7 @@ import { assertWorkerSnapshotMatches, captureWorkerSnapshot } from './deep-worke
|
|
|
6
6
|
import { acquireStateLock } from './state-lock.js';
|
|
7
7
|
import { resolveTarget } from './target.js';
|
|
8
8
|
import { sourceRootIdentity } from './secure-read.js';
|
|
9
|
-
import { newId, nowIso, readJson, sha256, writeJsonAtomic } from './util.js';
|
|
9
|
+
import { ensureDir, newId, nowIso, readJson, sha256, writeJsonAtomic } from './util.js';
|
|
10
10
|
const OUTPUT_SCHEMA = {
|
|
11
11
|
type: 'object', properties: { draftJson: { type: 'string', description: 'JSON object with findings and reviewState.' } },
|
|
12
12
|
required: ['draftJson'], additionalProperties: false,
|
|
@@ -67,13 +67,73 @@ export class DeepCoordinator {
|
|
|
67
67
|
lockPath(scanId) { return join(this.service.store.stateDir, '.locks', 'deep-' + scanId + '.sqlite'); }
|
|
68
68
|
transitionPath(scanId) { return join(this.service.store.stateDir, '.locks', 'deep-transition-' + scanId + '.sqlite'); }
|
|
69
69
|
cancelPath(scanId) { return join(this.service.store.stateDir, 'deep', scanId + '.cancel.json'); }
|
|
70
|
-
|
|
70
|
+
runSidecarDir(scanId) { return join(this.service.store.stateDir, 'deep', scanId + '.runs'); }
|
|
71
|
+
runSidecarPath(scanId, childId) { return join(this.runSidecarDir(scanId), childId + '.json'); }
|
|
72
|
+
sidecarHashes = new Map();
|
|
73
|
+
/** Heavy per-worker results that no longer belong in the transition journal. */
|
|
74
|
+
runPayload(run) {
|
|
75
|
+
const payload = {};
|
|
76
|
+
if (run.draft !== undefined)
|
|
77
|
+
payload['draft'] = run.draft;
|
|
78
|
+
if (run.receipts !== undefined)
|
|
79
|
+
payload['receipts'] = run.receipts;
|
|
80
|
+
if (run.findings !== undefined)
|
|
81
|
+
payload['findings'] = run.findings;
|
|
82
|
+
if (run.coverage !== undefined)
|
|
83
|
+
payload['coverage'] = run.coverage;
|
|
84
|
+
if (run.reviewState !== undefined)
|
|
85
|
+
payload['reviewState'] = run.reviewState;
|
|
86
|
+
return payload;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Journal transitions are written frequently. Keep only light run metadata in
|
|
90
|
+
* the journal and offload each worker's findings/coverage/receipts to a
|
|
91
|
+
* per-worker sidecar, written only when its payload actually changed.
|
|
92
|
+
*/
|
|
93
|
+
async save(journal) {
|
|
94
|
+
if (journal.runs.some((run) => Object.keys(this.runPayload(run)).length > 0))
|
|
95
|
+
await ensureDir(this.runSidecarDir(journal.scanId));
|
|
96
|
+
for (const run of journal.runs) {
|
|
97
|
+
if (!/^dcs_[a-f0-9]{24}$/.test(run.childId))
|
|
98
|
+
throw new Error('codex-security: invalid Deep worker id');
|
|
99
|
+
const payload = this.runPayload(run);
|
|
100
|
+
if (Object.keys(payload).length === 0)
|
|
101
|
+
continue;
|
|
102
|
+
const text = JSON.stringify(payload);
|
|
103
|
+
const digest = sha256(text);
|
|
104
|
+
const cacheKey = journal.scanId + '\u0000' + run.childId;
|
|
105
|
+
if (this.sidecarHashes.get(cacheKey) === digest)
|
|
106
|
+
continue;
|
|
107
|
+
await writeJsonAtomic(this.runSidecarPath(journal.scanId, run.childId), payload);
|
|
108
|
+
this.sidecarHashes.set(cacheKey, digest);
|
|
109
|
+
}
|
|
110
|
+
const light = { ...journal, runs: journal.runs.map((run) => ({
|
|
111
|
+
number: run.number, childId: run.childId, scanId: run.scanId, state: run.state,
|
|
112
|
+
...(run.complete !== undefined ? { complete: run.complete } : {}),
|
|
113
|
+
...(run.error ? { error: run.error } : {}),
|
|
114
|
+
...(run.newCount !== undefined ? { newCount: run.newCount } : {}),
|
|
115
|
+
})) };
|
|
116
|
+
await writeJsonAtomic(this.journalPath(journal.scanId), light);
|
|
117
|
+
}
|
|
71
118
|
async read(scanId) {
|
|
72
119
|
if (!/^cs_[a-f0-9]{24}$/.test(scanId))
|
|
73
120
|
throw new Error('codex-security: invalid Deep scan id');
|
|
74
121
|
const journal = await readJson(this.journalPath(scanId));
|
|
75
122
|
if (!journal || journal.version !== 1 || journal.scanId !== scanId)
|
|
76
123
|
throw new Error('codex-security: Deep scan journal not found');
|
|
124
|
+
for (const run of journal.runs) {
|
|
125
|
+
if (!run || !/^dcs_[a-f0-9]{24}$/.test(run.childId))
|
|
126
|
+
continue;
|
|
127
|
+
if (run.draft !== undefined && run.receipts !== undefined && run.findings !== undefined)
|
|
128
|
+
continue;
|
|
129
|
+
const payload = await readJson(this.runSidecarPath(scanId, run.childId)).catch(() => undefined);
|
|
130
|
+
if (!payload || typeof payload !== 'object')
|
|
131
|
+
continue;
|
|
132
|
+
for (const key of ['draft', 'receipts', 'findings', 'coverage', 'reviewState']) {
|
|
133
|
+
if (run[key] === undefined && payload[key] !== undefined)
|
|
134
|
+
run[key] = payload[key];
|
|
135
|
+
}
|
|
136
|
+
}
|
|
77
137
|
return journal;
|
|
78
138
|
}
|
|
79
139
|
authorize(journal, actor, claim) {
|
|
@@ -405,6 +465,7 @@ export class DeepCoordinator {
|
|
|
405
465
|
}
|
|
406
466
|
merge(journal) {
|
|
407
467
|
const merged = new Map();
|
|
468
|
+
const fingerprints = new Set();
|
|
408
469
|
let collisions = 0;
|
|
409
470
|
for (const run of journal.runs) {
|
|
410
471
|
if (run.state !== 'processed' || !run.findings)
|
|
@@ -420,8 +481,11 @@ export class DeepCoordinator {
|
|
|
420
481
|
prior.runs.push(run.number);
|
|
421
482
|
continue;
|
|
422
483
|
}
|
|
423
|
-
|
|
484
|
+
// A previous key with the same fingerprint but a different semantic
|
|
485
|
+
// signature means the workers disagreed about one identity.
|
|
486
|
+
if (fingerprints.has(fingerprint))
|
|
424
487
|
collisions++;
|
|
488
|
+
fingerprints.add(fingerprint);
|
|
425
489
|
merged.set(key, { finding, signature, runs: [run.number] });
|
|
426
490
|
newCount++;
|
|
427
491
|
}
|
package/lib/deep-worker.js
CHANGED
|
@@ -166,7 +166,10 @@ export class WorkerRegistry {
|
|
|
166
166
|
async readChunk(childId, path, offset) {
|
|
167
167
|
if (!Number.isSafeInteger(offset) || offset < 0)
|
|
168
168
|
throw new Error('codex-security: invalid worker source offset');
|
|
169
|
-
|
|
169
|
+
// Chunks after the first were re-opening and re-hashing the whole file. The
|
|
170
|
+
// cache only ever holds bytes verified against the bound snapshot hash, so
|
|
171
|
+
// serving them cannot smuggle unverified content to the worker.
|
|
172
|
+
const content = this.cachedText(childId, path);
|
|
170
173
|
if (offset > content.length)
|
|
171
174
|
throw new Error('codex-security: worker source offset exceeds file length');
|
|
172
175
|
const end = Math.min(offset + 4096, content.length);
|
package/lib/git.js
CHANGED
|
@@ -2,7 +2,6 @@ import { execFile } from 'node:child_process';
|
|
|
2
2
|
import { promisify } from 'node:util';
|
|
3
3
|
import { createHash } from 'node:crypto';
|
|
4
4
|
import { createReadStream } from 'node:fs';
|
|
5
|
-
import { join } from 'node:path';
|
|
6
5
|
const run = promisify(execFile);
|
|
7
6
|
const MAX_BUFFER = 64 * 1024 * 1024;
|
|
8
7
|
export async function git(root, args, timeoutMs = 60000, signal) {
|
|
@@ -20,9 +19,6 @@ export async function gitString(root, args, timeoutMs, signal) {
|
|
|
20
19
|
const result = await git(root, args, timeoutMs, signal);
|
|
21
20
|
return result.ok ? result.stdout.trim() : undefined;
|
|
22
21
|
}
|
|
23
|
-
export async function isGitRepo(root) {
|
|
24
|
-
return (await gitString(root, ['rev-parse', '--is-inside-work-tree'])) === 'true';
|
|
25
|
-
}
|
|
26
22
|
export async function repoRoot(path, signal) {
|
|
27
23
|
return gitString(path, ['rev-parse', '--show-toplevel'], undefined, signal);
|
|
28
24
|
}
|
|
@@ -115,23 +111,6 @@ export async function changedFiles(root, options) {
|
|
|
115
111
|
}
|
|
116
112
|
return files;
|
|
117
113
|
}
|
|
118
|
-
/** Digest of the working tree diff including untracked files, used for snapshot identity. */
|
|
119
|
-
export async function worktreeDigest(root) {
|
|
120
|
-
const hash = createHash('sha256');
|
|
121
|
-
const status = await git(root, ['status', '--porcelain=v1', '-z', '--untracked-files=all']);
|
|
122
|
-
hash.update('status\u0000' + (status.ok ? status.stdout : ''));
|
|
123
|
-
const diff = await git(root, ['diff', 'HEAD']);
|
|
124
|
-
hash.update('diff\u0000' + (diff.ok ? diff.stdout : diff.stderr));
|
|
125
|
-
const staged = await git(root, ['diff', '--cached']);
|
|
126
|
-
hash.update('staged\u0000' + (staged.ok ? staged.stdout : ''));
|
|
127
|
-
const untracked = await git(root, ['ls-files', '--others', '--exclude-standard', '-z']);
|
|
128
|
-
if (untracked.ok) {
|
|
129
|
-
const names = untracked.stdout.split('\u0000').filter(Boolean).sort();
|
|
130
|
-
for (const name of names)
|
|
131
|
-
hash.update('untracked\u0000' + name + '\u0000' + (await fileDigest(join(root, name)) ?? 'unavailable'));
|
|
132
|
-
}
|
|
133
|
-
return hash.digest('hex');
|
|
134
|
-
}
|
|
135
114
|
export async function fileDigest(path, signal) {
|
|
136
115
|
try {
|
|
137
116
|
const hash = createHash('sha256');
|
package/lib/index.js
CHANGED
|
@@ -13,7 +13,6 @@ export const name = PLUGIN_NAME;
|
|
|
13
13
|
export const inject = ['tools', 'skills'];
|
|
14
14
|
export { Config };
|
|
15
15
|
export const version = VERSION;
|
|
16
|
-
export const packageName = PACKAGE_NAME;
|
|
17
16
|
export { SecurityService };
|
|
18
17
|
function parseFrontmatter(text) {
|
|
19
18
|
if (!text.startsWith('---'))
|
package/lib/routes.js
CHANGED
|
@@ -39,10 +39,6 @@ function sendFile(res, content, mediaType, filename) {
|
|
|
39
39
|
res.setHeader('content-disposition', 'attachment; filename="' + filename + '"');
|
|
40
40
|
res.end(content);
|
|
41
41
|
}
|
|
42
|
-
function scanIdFromPath(pathname) {
|
|
43
|
-
const parts = pathname.split('/').filter(Boolean);
|
|
44
|
-
return parts.length >= 2 ? parts[1] : undefined;
|
|
45
|
-
}
|
|
46
42
|
/** Mutating HTTP routes must be same-origin JSON requests, never cross-site form posts. */
|
|
47
43
|
function requireJsonMutation(req) {
|
|
48
44
|
const contentType = String(req.headers['content-type'] ?? '').split(';')[0].trim().toLowerCase();
|
|
@@ -156,4 +152,3 @@ export function bindRoutes(ctx, service) {
|
|
|
156
152
|
const off = ctx.on?.('internal/service', () => { register(); });
|
|
157
153
|
return () => { off?.(); disposeRoute?.(); disposeRoute = undefined; };
|
|
158
154
|
}
|
|
159
|
-
export { scanIdFromPath };
|
package/lib/service.js
CHANGED
|
@@ -6,7 +6,7 @@ import { buildCoverage, buildFindingsDocument, fingerprintFor, normalizeAnchor,
|
|
|
6
6
|
import { gitString } from './git.js';
|
|
7
7
|
import { FINDING_STATUSES, SEVERITIES } from './types.js';
|
|
8
8
|
import { ScanStore, defaultStateDir } from './store.js';
|
|
9
|
-
import { readEvidenceLine, resolveTarget } from './target.js';
|
|
9
|
+
import { readEvidenceLine, readEvidenceLines, resolveTarget } from './target.js';
|
|
10
10
|
import { assertArtifactContract } from './schema.js';
|
|
11
11
|
import { canonicalProspectivePath, ensureDir, isWithin, joinWithin, newId, normalizeLine, nowIso, readFullText, readJson, sanitizeRelative, sha256, truncate, writeFileAtomic, writeJsonAtomic } from './util.js';
|
|
12
12
|
import { createExportValidator, writeSecureExport } from './secure-export.js';
|
|
@@ -108,11 +108,9 @@ function validateThreatModel(value) {
|
|
|
108
108
|
export class SecurityService {
|
|
109
109
|
store;
|
|
110
110
|
config;
|
|
111
|
-
ctx;
|
|
112
111
|
scanLocks = new Map();
|
|
113
112
|
managedInvocations = new WeakSet();
|
|
114
113
|
constructor(ctx, options = {}) {
|
|
115
|
-
this.ctx = ctx;
|
|
116
114
|
this.config = { ...DEFAULT_CONFIG, ...(options.config ?? {}) };
|
|
117
115
|
const profile = ctx.get?.('profileContext') ?? (Object.hasOwn(ctx, 'profileContext') ? ctx.profileContext : undefined);
|
|
118
116
|
const patchPath = options.patchPath ?? profile?.patchPath;
|
|
@@ -576,18 +574,17 @@ export class SecurityService {
|
|
|
576
574
|
if (keys.size >= 500)
|
|
577
575
|
break;
|
|
578
576
|
}
|
|
577
|
+
if (keys.size === 0)
|
|
578
|
+
return new Map();
|
|
579
|
+
// Group by source path first so a draft citing several lines of one file
|
|
580
|
+
// pays one hardened read instead of one per line.
|
|
581
|
+
const requests = Array.from(keys.values());
|
|
582
|
+
const lines = await readEvidenceLines(record.target.rootPath, requests, this.config.maxFileBytes);
|
|
579
583
|
const evidence = new Map();
|
|
580
|
-
const
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
const lines = await Promise.all(batch.map(async ([key, value]) => {
|
|
585
|
-
const line = await readEvidenceLine(record.target.rootPath, value.path, value.line, this.config.maxFileBytes);
|
|
586
|
-
return { key, line };
|
|
587
|
-
}));
|
|
588
|
-
for (const entry of lines)
|
|
589
|
-
if (entry.line !== undefined)
|
|
590
|
-
evidence.set(entry.key, entry.line);
|
|
584
|
+
for (const [key, value] of keys) {
|
|
585
|
+
const line = lines.get(value.path + ':' + value.line);
|
|
586
|
+
if (line !== undefined)
|
|
587
|
+
evidence.set(key, line);
|
|
591
588
|
}
|
|
592
589
|
return evidence;
|
|
593
590
|
}
|
|
@@ -597,20 +594,19 @@ export class SecurityService {
|
|
|
597
594
|
const files = new Map();
|
|
598
595
|
const evidence = new Map();
|
|
599
596
|
const read = async (path) => {
|
|
600
|
-
let
|
|
601
|
-
if (
|
|
602
|
-
|
|
603
|
-
files.set(path,
|
|
597
|
+
let lines = files.get(path);
|
|
598
|
+
if (lines === undefined) {
|
|
599
|
+
lines = (await registry.read('verified-draft', path)).split('\n');
|
|
600
|
+
files.set(path, lines);
|
|
604
601
|
}
|
|
605
|
-
return
|
|
602
|
+
return lines;
|
|
606
603
|
};
|
|
607
604
|
try {
|
|
608
605
|
for (const finding of findings) {
|
|
609
606
|
if (!Array.isArray(finding.locations))
|
|
610
607
|
throw new Error('codex-security: finding locations are required');
|
|
611
608
|
for (const location of finding.locations) {
|
|
612
|
-
const
|
|
613
|
-
const lines = content.split('\n');
|
|
609
|
+
const lines = await read(location.path);
|
|
614
610
|
if (!Number.isInteger(location.startLine) || location.startLine < 1 || location.startLine > lines.length || (location.endLine !== undefined && location.endLine > lines.length))
|
|
615
611
|
throw new Error('codex-security: finding location is outside the bound source');
|
|
616
612
|
evidence.set(location.path + ':' + location.startLine, truncate(normalizeLine(lines[location.startLine - 1] ?? ''), 400));
|
|
@@ -619,12 +615,11 @@ export class SecurityService {
|
|
|
619
615
|
const item = asObject(raw);
|
|
620
616
|
if (!item || typeof item['path'] !== 'string' || typeof item['code'] !== 'string' || !item['code'])
|
|
621
617
|
throw new Error('codex-security: worker code evidence requires a bound source path and code');
|
|
622
|
-
const
|
|
618
|
+
const lines = await read(item['path']);
|
|
623
619
|
const start = item['startLine'] ?? item['start_line'];
|
|
624
620
|
const end = item['endLine'] ?? item['end_line'] ?? start;
|
|
625
|
-
let excerpt =
|
|
621
|
+
let excerpt = lines.join('\n');
|
|
626
622
|
if (start !== undefined) {
|
|
627
|
-
const lines = content.split('\n');
|
|
628
623
|
if (typeof start !== 'number' || typeof end !== 'number' || !Number.isInteger(start) || !Number.isInteger(end) || start < 1 || end < start || end > lines.length)
|
|
629
624
|
throw new Error('codex-security: code evidence location is outside the bound source');
|
|
630
625
|
excerpt = lines.slice(start - 1, end).join('\n');
|
|
@@ -1140,11 +1135,17 @@ export class SecurityService {
|
|
|
1140
1135
|
async getScan(args) {
|
|
1141
1136
|
const scanId = requiredStr(args, 'scanId');
|
|
1142
1137
|
const { record, dir } = await this.requireScan(scanId);
|
|
1143
|
-
|
|
1138
|
+
// Read the scan files that all belong to this one response from the resolved
|
|
1139
|
+
// directory instead of re-resolving the index (and re-reading scan.json)
|
|
1140
|
+
// through the store once per document.
|
|
1141
|
+
const triage = (await readJson(join(dir, 'triage.json'))) ?? {};
|
|
1142
|
+
const findings = ((await readJson(join(dir, 'findings.json')))?.findings ?? []).map((finding) => {
|
|
1143
|
+
const entry = triage[finding.findingId];
|
|
1144
|
+
return { ...finding, status: entry?.status ?? 'open', ...(entry ? { triage: entry } : {}) };
|
|
1145
|
+
});
|
|
1144
1146
|
const coverage = await readJson(join(dir, 'coverage.json'));
|
|
1145
1147
|
const manifest = await readJson(join(dir, 'scan-manifest.json'));
|
|
1146
|
-
const artifacts = await this.store.listArtifacts(scanId);
|
|
1147
|
-
const triage = await this.store.readTriage(scanId);
|
|
1148
|
+
const artifacts = await this.store.listArtifacts(scanId, dir);
|
|
1148
1149
|
const reviewItems = (await readJson(join(dir, 'review-items.json')))?.items ?? [];
|
|
1149
1150
|
return {
|
|
1150
1151
|
ok: true,
|
package/lib/store.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { readdir } from 'node:fs/promises';
|
|
1
|
+
import { readdir, stat } from 'node:fs/promises';
|
|
2
2
|
import { dirname, join } from 'node:path';
|
|
3
3
|
import { ensureDir, joinWithin, nowIso, readJson, readText, sanitizeRelative, writeFileAtomic, writeJsonAtomic, sha256 } from './util.js';
|
|
4
4
|
import { acquireStateLock } from './state-lock.js';
|
|
@@ -57,6 +57,8 @@ export function summarize(record) {
|
|
|
57
57
|
export class ScanStore {
|
|
58
58
|
stateDir;
|
|
59
59
|
queue = Promise.resolve();
|
|
60
|
+
indexCache;
|
|
61
|
+
indexStamp;
|
|
60
62
|
constructor(stateDir) {
|
|
61
63
|
this.stateDir = stateDir;
|
|
62
64
|
}
|
|
@@ -84,9 +86,30 @@ export class ScanStore {
|
|
|
84
86
|
this.queue = next.catch(() => undefined);
|
|
85
87
|
return next;
|
|
86
88
|
}
|
|
89
|
+
/** Cheap identity of index.json so a changed on-disk index invalidates the cache. */
|
|
90
|
+
async indexFileStamp() {
|
|
91
|
+
const info = await stat(this.indexPath).catch(() => undefined);
|
|
92
|
+
return info ? info.mtimeMs + ':' + info.size + ':' + info.ino : undefined;
|
|
93
|
+
}
|
|
87
94
|
async loadIndex() {
|
|
88
|
-
const
|
|
89
|
-
|
|
95
|
+
const stamp = await this.indexFileStamp();
|
|
96
|
+
if (stamp !== undefined) {
|
|
97
|
+
if (this.indexCache && this.indexStamp === stamp)
|
|
98
|
+
return this.indexCache;
|
|
99
|
+
const stored = await readJson(this.indexPath);
|
|
100
|
+
if (stored && stored.version === 1) {
|
|
101
|
+
this.indexCache = stored;
|
|
102
|
+
this.indexStamp = stamp;
|
|
103
|
+
return stored;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
else if (this.indexCache && this.indexStamp === undefined) {
|
|
107
|
+
return this.indexCache;
|
|
108
|
+
}
|
|
109
|
+
const rebuilt = await this.rebuildIndex();
|
|
110
|
+
this.indexCache = rebuilt;
|
|
111
|
+
this.indexStamp = undefined;
|
|
112
|
+
return rebuilt;
|
|
90
113
|
}
|
|
91
114
|
async rebuildIndex() {
|
|
92
115
|
const index = { version: 1, scans: {} };
|
|
@@ -106,6 +129,8 @@ export class ScanStore {
|
|
|
106
129
|
}
|
|
107
130
|
async saveIndex(index) {
|
|
108
131
|
await writeJsonAtomic(this.indexPath, index);
|
|
132
|
+
this.indexCache = index;
|
|
133
|
+
this.indexStamp = await this.indexFileStamp();
|
|
109
134
|
}
|
|
110
135
|
async create(input, options = {}) {
|
|
111
136
|
return this.serialize(async () => {
|
|
@@ -211,11 +236,10 @@ export class ScanStore {
|
|
|
211
236
|
return readJson(joinWithin(found.dir, name));
|
|
212
237
|
}
|
|
213
238
|
async list() {
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
});
|
|
239
|
+
// Read-only fast path: list never mutates scan state and must not serialize behind
|
|
240
|
+
// the index lock. The cached index is reused until index.json changes on disk.
|
|
241
|
+
const index = await this.loadIndex();
|
|
242
|
+
return Object.values(index.scans).sort((left, right) => right.createdAt.localeCompare(left.createdAt));
|
|
219
243
|
}
|
|
220
244
|
async saveArtifact(scanId, relativePath, content, mediaType) {
|
|
221
245
|
const found = await this.get(scanId);
|
|
@@ -243,13 +267,12 @@ export class ScanStore {
|
|
|
243
267
|
const content = await readText(target, limit);
|
|
244
268
|
if (content === undefined)
|
|
245
269
|
throw new Error('codex-security: artifact not found: ' + relativePath);
|
|
246
|
-
const { stat } = await import('node:fs/promises');
|
|
247
270
|
const info = await stat(target);
|
|
248
271
|
return { path: clean, content, bytes: info.size, truncated: info.size > limit };
|
|
249
272
|
}
|
|
250
|
-
async listArtifacts(scanId) {
|
|
251
|
-
const
|
|
252
|
-
if (!
|
|
273
|
+
async listArtifacts(scanId, knownDir) {
|
|
274
|
+
const scanDir = knownDir ?? (await this.get(scanId))?.dir;
|
|
275
|
+
if (!scanDir)
|
|
253
276
|
return [];
|
|
254
277
|
const results = [];
|
|
255
278
|
const walk = async (dir, relBase) => {
|
|
@@ -274,7 +297,6 @@ export class ScanStore {
|
|
|
274
297
|
continue;
|
|
275
298
|
if (relBase.length === 0 && SEALED_FILES.has(guardName(entry.name)))
|
|
276
299
|
continue;
|
|
277
|
-
const { stat } = await import('node:fs/promises');
|
|
278
300
|
const info = await stat(join(dir, entry.name)).catch((error) => {
|
|
279
301
|
if (error.code === 'ENOENT')
|
|
280
302
|
return undefined;
|
|
@@ -284,7 +306,7 @@ export class ScanStore {
|
|
|
284
306
|
results.push({ path: rel, bytes: info.size });
|
|
285
307
|
}
|
|
286
308
|
};
|
|
287
|
-
await walk(
|
|
309
|
+
await walk(scanDir, '');
|
|
288
310
|
return results.sort((left, right) => left.path.localeCompare(right.path));
|
|
289
311
|
}
|
|
290
312
|
async readTriage(scanId) {
|
package/lib/target.js
CHANGED
|
@@ -178,10 +178,55 @@ export async function resolveDiff(rootPath, options) {
|
|
|
178
178
|
}
|
|
179
179
|
/** Read one source line for anchor derivation without trusting it as instructions. */
|
|
180
180
|
export async function readEvidenceLine(rootPath, path, line, maxBytes) {
|
|
181
|
-
const { open } = await import('node:fs/promises');
|
|
182
|
-
const { constants } = await import('node:fs');
|
|
183
181
|
if (!Number.isFinite(line) || line < 1)
|
|
184
182
|
return undefined;
|
|
183
|
+
const lines = await readEvidenceFileLines(rootPath, path, maxBytes);
|
|
184
|
+
if (!lines)
|
|
185
|
+
return undefined;
|
|
186
|
+
const value = lines[Math.trunc(line) - 1];
|
|
187
|
+
return value === undefined ? undefined : truncate(normalizeLine(value), 400);
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Read many evidence lines with one hardened open per source file. The old
|
|
191
|
+
* per-line reader repeated the lstat/open/realpath verification and re-read the
|
|
192
|
+
* file prefix for every requested line, so a draft citing several lines of one
|
|
193
|
+
* file paid that cost repeatedly.
|
|
194
|
+
*/
|
|
195
|
+
export async function readEvidenceLines(rootPath, requests, maxBytes) {
|
|
196
|
+
const byPath = new Map();
|
|
197
|
+
for (const request of requests) {
|
|
198
|
+
if (!Number.isFinite(request.line) || request.line < 1)
|
|
199
|
+
continue;
|
|
200
|
+
const line = Math.trunc(request.line);
|
|
201
|
+
const bucket = byPath.get(request.path);
|
|
202
|
+
if (bucket) {
|
|
203
|
+
if (!bucket.includes(line))
|
|
204
|
+
bucket.push(line);
|
|
205
|
+
}
|
|
206
|
+
else
|
|
207
|
+
byPath.set(request.path, [line]);
|
|
208
|
+
}
|
|
209
|
+
const result = new Map();
|
|
210
|
+
const groups = [...byPath.entries()];
|
|
211
|
+
const concurrency = 8;
|
|
212
|
+
for (let index = 0; index < groups.length; index += concurrency) {
|
|
213
|
+
await Promise.all(groups.slice(index, index + concurrency).map(async ([path, lineNumbers]) => {
|
|
214
|
+
const lines = await readEvidenceFileLines(rootPath, path, maxBytes);
|
|
215
|
+
if (!lines)
|
|
216
|
+
return;
|
|
217
|
+
for (const line of lineNumbers) {
|
|
218
|
+
const value = lines[line - 1];
|
|
219
|
+
if (value !== undefined)
|
|
220
|
+
result.set(path + ':' + line, truncate(normalizeLine(value), 400));
|
|
221
|
+
}
|
|
222
|
+
}));
|
|
223
|
+
}
|
|
224
|
+
return result;
|
|
225
|
+
}
|
|
226
|
+
/** Hardened single-file read shared by the line readers. */
|
|
227
|
+
async function readEvidenceFileLines(rootPath, path, maxBytes) {
|
|
228
|
+
const { open } = await import('node:fs/promises');
|
|
229
|
+
const { constants } = await import('node:fs');
|
|
185
230
|
if (typeof path !== 'string' || path.length === 0)
|
|
186
231
|
return undefined;
|
|
187
232
|
let absolute;
|
|
@@ -216,10 +261,7 @@ export async function readEvidenceLine(rootPath, path, line, maxBytes) {
|
|
|
216
261
|
return undefined;
|
|
217
262
|
const buffer = Buffer.alloc(Math.min(maxBytes, 256 * 1024));
|
|
218
263
|
const { bytesRead } = await handle.read(buffer, 0, buffer.length, 0);
|
|
219
|
-
|
|
220
|
-
const lines = text.split('\n');
|
|
221
|
-
const value = lines[line - 1];
|
|
222
|
-
return value === undefined ? undefined : truncate(normalizeLine(value), 400);
|
|
264
|
+
return buffer.subarray(0, bytesRead).toString('utf8').split('\n');
|
|
223
265
|
}
|
|
224
266
|
catch {
|
|
225
267
|
return undefined;
|
package/lib/tools.js
CHANGED
|
@@ -13,6 +13,52 @@ const FORMAT_ENUM = ENUM('sarif', 'json', 'csv', 'markdown');
|
|
|
13
13
|
function render(_args, value) {
|
|
14
14
|
return [{ type: 'text', text: JSON.stringify(value, null, 2) }];
|
|
15
15
|
}
|
|
16
|
+
/**
|
|
17
|
+
* DSH rejects values containing undefined even though JSON.stringify drops them.
|
|
18
|
+
* A full JSON round-trip on every tool result allocates a second copy of large
|
|
19
|
+
* responses; walk the value instead and keep it when nothing needs normalizing.
|
|
20
|
+
*/
|
|
21
|
+
function normalizeToolResult(value) {
|
|
22
|
+
return normalizeValue(value, new Set());
|
|
23
|
+
}
|
|
24
|
+
function normalizeValue(value, seen) {
|
|
25
|
+
if (value === undefined || typeof value === 'function' || typeof value === 'symbol')
|
|
26
|
+
return undefined;
|
|
27
|
+
if (typeof value === 'number')
|
|
28
|
+
return Number.isFinite(value) ? value : null;
|
|
29
|
+
if (typeof value !== 'object' || value === null)
|
|
30
|
+
return value;
|
|
31
|
+
if (seen.has(value))
|
|
32
|
+
throw new TypeError('codex-security: tool result contains a circular structure');
|
|
33
|
+
if (Array.isArray(value)) {
|
|
34
|
+
seen.add(value);
|
|
35
|
+
let changed = false;
|
|
36
|
+
const normalized = value.map((item) => {
|
|
37
|
+
const next = normalizeValue(item, seen);
|
|
38
|
+
const result = next === undefined ? null : next;
|
|
39
|
+
if (result !== item)
|
|
40
|
+
changed = true;
|
|
41
|
+
return result;
|
|
42
|
+
});
|
|
43
|
+
seen.delete(value);
|
|
44
|
+
return changed ? normalized : value;
|
|
45
|
+
}
|
|
46
|
+
seen.add(value);
|
|
47
|
+
let changed = false;
|
|
48
|
+
const normalized = {};
|
|
49
|
+
for (const [key, item] of Object.entries(value)) {
|
|
50
|
+
const next = normalizeValue(item, seen);
|
|
51
|
+
if (next === undefined) {
|
|
52
|
+
changed = true;
|
|
53
|
+
continue;
|
|
54
|
+
}
|
|
55
|
+
if (next !== item)
|
|
56
|
+
changed = true;
|
|
57
|
+
normalized[key] = next;
|
|
58
|
+
}
|
|
59
|
+
seen.delete(value);
|
|
60
|
+
return changed ? normalized : value;
|
|
61
|
+
}
|
|
16
62
|
function tool(name, description, properties, required, run, timeoutMs = 180000) {
|
|
17
63
|
const parameters = { type: 'object', properties, ...(required.length > 0 ? { required } : {}), additionalProperties: false };
|
|
18
64
|
const validate = compileToolArguments(parameters);
|
|
@@ -24,7 +70,7 @@ function tool(name, description, properties, required, run, timeoutMs = 180000)
|
|
|
24
70
|
timeoutMs,
|
|
25
71
|
// DSH rejects values containing undefined even though JSON.stringify drops them.
|
|
26
72
|
// Normalize the whole response, including nested optional fields, at the boundary.
|
|
27
|
-
execute: async (args, exec) => { validate(args); return
|
|
73
|
+
execute: async (args, exec) => { validate(args); return normalizeToolResult(await run(args, exec)); },
|
|
28
74
|
};
|
|
29
75
|
}
|
|
30
76
|
const SCAN_ID = { type: 'string', description: 'Scan id returned by a codex_security_start_*_scan tool, e.g. cs_<24 hex chars>.' };
|
package/lib/types/constants.d.ts
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
export declare const PACKAGE_NAME = "@lornatang/dsh-codex-security";
|
|
2
2
|
export declare const PLUGIN_NAME = "codex-security";
|
|
3
|
-
export declare const VERSION = "0.1.
|
|
3
|
+
export declare const VERSION = "0.1.12";
|
|
4
4
|
export declare const ROUTE_PREFIX = "/plugins/codex-security";
|
|
5
5
|
export declare const PRODUCER: {
|
|
6
6
|
name: string;
|
|
7
7
|
version: string;
|
|
8
8
|
};
|
|
9
|
-
export declare const DEFAULT_EXCLUDES: string[];
|
|
10
9
|
export declare const SKILL_NAMES: string[];
|
|
11
10
|
export declare const TOOL_NAMES: string[];
|
|
@@ -42,6 +42,16 @@ export declare class DeepCoordinator {
|
|
|
42
42
|
private lockPath;
|
|
43
43
|
private transitionPath;
|
|
44
44
|
private cancelPath;
|
|
45
|
+
private runSidecarDir;
|
|
46
|
+
private runSidecarPath;
|
|
47
|
+
private readonly sidecarHashes;
|
|
48
|
+
/** Heavy per-worker results that no longer belong in the transition journal. */
|
|
49
|
+
private runPayload;
|
|
50
|
+
/**
|
|
51
|
+
* Journal transitions are written frequently. Keep only light run metadata in
|
|
52
|
+
* the journal and offload each worker's findings/coverage/receipts to a
|
|
53
|
+
* per-worker sidecar, written only when its payload actually changed.
|
|
54
|
+
*/
|
|
45
55
|
private save;
|
|
46
56
|
private read;
|
|
47
57
|
private authorize;
|
package/lib/types/git.d.ts
CHANGED
|
@@ -5,7 +5,6 @@ export interface GitResult {
|
|
|
5
5
|
}
|
|
6
6
|
export declare function git(root: string, args: string[], timeoutMs?: number, signal?: AbortSignal): Promise<GitResult>;
|
|
7
7
|
export declare function gitString(root: string, args: string[], timeoutMs?: number, signal?: AbortSignal): Promise<string | undefined>;
|
|
8
|
-
export declare function isGitRepo(root: string): Promise<boolean>;
|
|
9
8
|
export declare function repoRoot(path: string, signal?: AbortSignal): Promise<string | undefined>;
|
|
10
9
|
export declare function headRevision(root: string, signal?: AbortSignal): Promise<string | undefined>;
|
|
11
10
|
export declare function currentBranch(root: string, signal?: AbortSignal): Promise<string | undefined>;
|
|
@@ -27,6 +26,4 @@ export declare function changedFiles(root: string, options: {
|
|
|
27
26
|
staged?: boolean;
|
|
28
27
|
workingTree?: boolean;
|
|
29
28
|
}): Promise<ChangedFile[]>;
|
|
30
|
-
/** Digest of the working tree diff including untracked files, used for snapshot identity. */
|
|
31
|
-
export declare function worktreeDigest(root: string): Promise<string>;
|
|
32
29
|
export declare function fileDigest(path: string, signal?: AbortSignal): Promise<string | undefined>;
|
package/lib/types/index.d.ts
CHANGED
|
@@ -6,8 +6,7 @@ import type { PluginContext } from './types.js';
|
|
|
6
6
|
export declare const name = "codex-security";
|
|
7
7
|
export declare const inject: string[];
|
|
8
8
|
export { Config };
|
|
9
|
-
export declare const version = "0.1.
|
|
10
|
-
export declare const packageName = "@lornatang/dsh-codex-security";
|
|
9
|
+
export declare const version = "0.1.12";
|
|
11
10
|
export { SecurityService };
|
|
12
11
|
export type { ConfigType };
|
|
13
12
|
export declare function loadBundledSkills(packageRoot: string): SkillRegistration[];
|