@lornatang/dsh-codex-security 0.1.8 → 0.1.10
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 +150 -118
- package/README.md +1 -1
- package/README.zh.md +1 -1
- package/lib/constants.js +1 -1
- package/lib/contracts.js +12 -2
- package/lib/deep-coordinator.js +23 -12
- package/lib/deep-worker.js +26 -2
- package/lib/git.js +2 -0
- package/lib/routes.js +29 -1
- package/lib/service.js +45 -26
- package/lib/store.js +17 -3
- package/lib/target.js +32 -3
- package/lib/tools.js +4 -12
- package/lib/types/constants.d.ts +1 -1
- package/lib/types/deep-coordinator.d.ts +3 -1
- package/lib/types/deep-worker.d.ts +4 -0
- package/lib/types/index.d.ts +1 -1
- package/lib/util.js +131 -10
- package/package.json +131 -131
package/lib/service.js
CHANGED
|
@@ -8,7 +8,7 @@ import { FINDING_STATUSES, SEVERITIES } from './types.js';
|
|
|
8
8
|
import { ScanStore, defaultStateDir } from './store.js';
|
|
9
9
|
import { readEvidenceLine, resolveTarget } from './target.js';
|
|
10
10
|
import { assertArtifactContract } from './schema.js';
|
|
11
|
-
import { canonicalProspectivePath, ensureDir, joinWithin, newId, normalizeLine, nowIso, readFullText, readJson, sanitizeRelative, sha256, truncate, writeFileAtomic, writeJsonAtomic } from './util.js';
|
|
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';
|
|
13
13
|
import { acquireStateLock } from './state-lock.js';
|
|
14
14
|
import { WorkerRegistry } from './deep-worker.js';
|
|
@@ -93,9 +93,17 @@ function manifestThreatModel(value) {
|
|
|
93
93
|
return { format: 'markdown', content: renderThreatModel(value) };
|
|
94
94
|
}
|
|
95
95
|
function validateThreatModel(value) {
|
|
96
|
-
if (
|
|
97
|
-
|
|
98
|
-
|
|
96
|
+
if (typeof value === 'string') {
|
|
97
|
+
if (value.trim())
|
|
98
|
+
return;
|
|
99
|
+
throw new Error('codex-security: threatModel must be a non-empty string or an object');
|
|
100
|
+
}
|
|
101
|
+
const object = asObject(value);
|
|
102
|
+
if (!object)
|
|
103
|
+
throw new Error('codex-security: threatModel must be a non-empty string or an object');
|
|
104
|
+
if (object['format'] === 'markdown' && (typeof object['content'] !== 'string' || object['content'].trim().length === 0)) {
|
|
105
|
+
throw new Error('codex-security: threatModel markdown content must be a non-empty string');
|
|
106
|
+
}
|
|
99
107
|
}
|
|
100
108
|
export class SecurityService {
|
|
101
109
|
store;
|
|
@@ -555,9 +563,16 @@ export class SecurityService {
|
|
|
555
563
|
const location = finding.locations?.[0];
|
|
556
564
|
if (!location)
|
|
557
565
|
continue;
|
|
558
|
-
|
|
566
|
+
let path;
|
|
567
|
+
try {
|
|
568
|
+
path = sanitizeRelative(location.path);
|
|
569
|
+
}
|
|
570
|
+
catch {
|
|
571
|
+
continue;
|
|
572
|
+
}
|
|
573
|
+
const key = path + ':' + location.startLine;
|
|
559
574
|
if (!keys.has(key))
|
|
560
|
-
keys.set(key, { path
|
|
575
|
+
keys.set(key, { path, line: location.startLine });
|
|
561
576
|
if (keys.size >= 500)
|
|
562
577
|
break;
|
|
563
578
|
}
|
|
@@ -965,6 +980,8 @@ export class SecurityService {
|
|
|
965
980
|
}
|
|
966
981
|
}
|
|
967
982
|
const threatModel = draft.input.threatModel ?? record.threatModel;
|
|
983
|
+
if (threatModel !== undefined)
|
|
984
|
+
validateThreatModel(threatModel);
|
|
968
985
|
const sealedAt = nowIso();
|
|
969
986
|
const findingsDocument = buildFindingsDocument(scanId, findings);
|
|
970
987
|
const sarif = toSarif(record, findings);
|
|
@@ -972,28 +989,20 @@ export class SecurityService {
|
|
|
972
989
|
validateFindingEvidence(findings);
|
|
973
990
|
assertArtifactContract('findings', findingsDocument);
|
|
974
991
|
assertArtifactContract('coverage', coverage);
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
const sealFiles = [
|
|
984
|
-
{ path: 'findings.json', mediaType: 'application/json' },
|
|
985
|
-
{ path: 'coverage.json', mediaType: 'application/json' },
|
|
986
|
-
{ path: 'sarif.json', mediaType: 'application/sarif+json' },
|
|
987
|
-
{ path: 'report.md', mediaType: 'text/markdown' },
|
|
992
|
+
// Build every sealed document in memory and validate the manifest before
|
|
993
|
+
// publishing any file, so a schema failure can never leave sealed artifacts
|
|
994
|
+
// behind on a scan that is still running.
|
|
995
|
+
const sealedFiles = [
|
|
996
|
+
{ path: 'findings.json', mediaType: 'application/json', content: JSON.stringify(findingsDocument, null, 2) + '\n' },
|
|
997
|
+
{ path: 'coverage.json', mediaType: 'application/json', content: JSON.stringify(coverage, null, 2) + '\n' },
|
|
998
|
+
{ path: 'sarif.json', mediaType: 'application/sarif+json', content: JSON.stringify(sarif, null, 2) + '\n' },
|
|
999
|
+
{ path: 'report.md', mediaType: 'text/markdown', content: report },
|
|
988
1000
|
];
|
|
989
|
-
if (threatModel !== undefined)
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
for (const file of sealFiles) {
|
|
993
|
-
options?.signal?.throwIfAborted();
|
|
994
|
-
const data = await readFile(joinWithin(dir, file.path));
|
|
995
|
-
artifacts.push({ path: file.path, sha256: sha256(data), mediaType: file.mediaType });
|
|
1001
|
+
if (threatModel !== undefined) {
|
|
1002
|
+
const rendered = renderThreatModel(threatModel);
|
|
1003
|
+
sealedFiles.push({ path: 'threat-model.md', mediaType: 'text/markdown', content: rendered }, { path: 'threatmodel.md', mediaType: 'text/markdown', content: rendered });
|
|
996
1004
|
}
|
|
1005
|
+
const artifacts = sealedFiles.map((file) => ({ path: file.path, sha256: sha256(file.content), mediaType: file.mediaType }));
|
|
997
1006
|
for (const artifact of record.artifacts) {
|
|
998
1007
|
options?.signal?.throwIfAborted();
|
|
999
1008
|
const data = await readFile(joinWithin(dir, artifact.path));
|
|
@@ -1041,6 +1050,10 @@ export class SecurityService {
|
|
|
1041
1050
|
},
|
|
1042
1051
|
};
|
|
1043
1052
|
assertArtifactContract('scan-manifest', manifest);
|
|
1053
|
+
for (const file of sealedFiles) {
|
|
1054
|
+
options?.signal?.throwIfAborted();
|
|
1055
|
+
await writeFileAtomic(joinWithin(dir, file.path), file.content);
|
|
1056
|
+
}
|
|
1044
1057
|
await writeJsonAtomic(join(dir, 'scan-manifest.json'), manifest);
|
|
1045
1058
|
const counts = severityCounts(findings);
|
|
1046
1059
|
options?.signal?.throwIfAborted();
|
|
@@ -1262,6 +1275,12 @@ export class SecurityService {
|
|
|
1262
1275
|
const requestedOutput = requested
|
|
1263
1276
|
? resolvePath(requested)
|
|
1264
1277
|
: join(this.store.stateDir, 'exports', scanId + '.' + extension);
|
|
1278
|
+
const stateRoot = await canonicalProspectivePath(this.store.stateDir);
|
|
1279
|
+
const exportsRoot = await canonicalProspectivePath(join(this.store.stateDir, 'exports'));
|
|
1280
|
+
const canonicalOutput = await canonicalProspectivePath(requestedOutput);
|
|
1281
|
+
if (isWithin(stateRoot, canonicalOutput) && !isWithin(exportsRoot, canonicalOutput)) {
|
|
1282
|
+
throw new Error('codex-security: refusing to export into scan storage outside the exports directory: ' + requestedOutput);
|
|
1283
|
+
}
|
|
1265
1284
|
const allowedRoots = await Promise.all([this.store.stateDir, process.cwd(), record.target.rootPath].map(canonicalProspectivePath));
|
|
1266
1285
|
const scanRoot = join(this.store.stateDir, 'repositories');
|
|
1267
1286
|
let requireGit = record.target.kind !== 'directory_snapshot';
|
package/lib/store.js
CHANGED
|
@@ -5,6 +5,19 @@ import { acquireStateLock } from './state-lock.js';
|
|
|
5
5
|
/** Files owned by the seal step; agents cannot overwrite them through save_artifact. */
|
|
6
6
|
const SEALED_FILES = new Set(['scan.json', 'findings.json', 'coverage.json', 'scan-manifest.json', 'sarif.json', 'report.md', 'threat-model.md', 'threatmodel.md']);
|
|
7
7
|
const INTERNAL_FILES = new Set([...SEALED_FILES, 'candidates.json', 'validations.json', 'attack-paths.json', 'review-items.json', 'triage.json']);
|
|
8
|
+
/**
|
|
9
|
+
* Fold an artifact path into the form Win32 uses when resolving it. Windows
|
|
10
|
+
* filesystems are case-insensitive and silently drop trailing dots and spaces,
|
|
11
|
+
* so 'SCAN.JSON', 'Scan.json.' and 'checkpoints\scan-draft.json' all address
|
|
12
|
+
* the same file as their lowercase spelling. Compare guard names in this folded
|
|
13
|
+
* form while keeping the original spelling on disk.
|
|
14
|
+
*/
|
|
15
|
+
function guardName(relativePath) {
|
|
16
|
+
return relativePath
|
|
17
|
+
.split('/')
|
|
18
|
+
.map((segment) => segment.replace(/[ .]+$/, '').toLowerCase())
|
|
19
|
+
.join('/');
|
|
20
|
+
}
|
|
8
21
|
export function defaultStateDir(patchPath, configured) {
|
|
9
22
|
if (configured.length > 0)
|
|
10
23
|
return configured;
|
|
@@ -211,7 +224,8 @@ export class ScanStore {
|
|
|
211
224
|
if (found.record.status !== 'running')
|
|
212
225
|
throw new Error('codex-security: cannot change artifacts after scan is ' + found.record.status);
|
|
213
226
|
const clean = sanitizeRelative(relativePath);
|
|
214
|
-
|
|
227
|
+
const guarded = guardName(clean);
|
|
228
|
+
if (INTERNAL_FILES.has(guarded) || guarded === 'checkpoints' || guarded.startsWith('checkpoints/'))
|
|
215
229
|
throw new Error('codex-security: cannot overwrite internal scan path: ' + clean);
|
|
216
230
|
const target = joinWithin(found.dir, clean);
|
|
217
231
|
const data = Buffer.from(content, 'utf8');
|
|
@@ -249,7 +263,7 @@ export class ScanStore {
|
|
|
249
263
|
for (const entry of entries) {
|
|
250
264
|
const rel = relBase.length > 0 ? relBase + '/' + entry.name : entry.name;
|
|
251
265
|
if (entry.isDirectory()) {
|
|
252
|
-
if (relBase.length === 0 && entry.name === 'checkpoints')
|
|
266
|
+
if (relBase.length === 0 && guardName(entry.name) === 'checkpoints')
|
|
253
267
|
continue;
|
|
254
268
|
await walk(join(dir, entry.name), rel);
|
|
255
269
|
continue;
|
|
@@ -258,7 +272,7 @@ export class ScanStore {
|
|
|
258
272
|
continue;
|
|
259
273
|
if (/^\.\d+\.(?:\d+\.[a-f0-9]{8}|[a-f0-9]{24})\.tmp$/.test(entry.name))
|
|
260
274
|
continue;
|
|
261
|
-
if (relBase.length === 0 && SEALED_FILES.has(entry.name))
|
|
275
|
+
if (relBase.length === 0 && SEALED_FILES.has(guardName(entry.name)))
|
|
262
276
|
continue;
|
|
263
277
|
const { stat } = await import('node:fs/promises');
|
|
264
278
|
const info = await stat(join(dir, entry.name)).catch((error) => {
|
package/lib/target.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { lstat, realpath, stat } from 'node:fs/promises';
|
|
2
2
|
import { basename, dirname, join, relative, resolve, sep } from 'node:path';
|
|
3
3
|
import { createHash } from 'node:crypto';
|
|
4
|
-
import { isProbablyBinary, languageOf, listFiles, matchesAny, matchesScopePath, normalizeLine, stableTargetId, truncate } from './util.js';
|
|
4
|
+
import { isProbablyBinary, isWithin, joinWithin, languageOf, listFiles, matchesAny, matchesScopePath, normalizeLine, stableTargetId, truncate } from './util.js';
|
|
5
5
|
import { changedFiles, currentBranch, fileDigest, headRevision, remoteUrl, repoRoot, resolveRevision, statusEntries, trackedFiles } from './git.js';
|
|
6
6
|
const DEFAULT_EXCLUDES = ['.git/**', 'node_modules/**', 'dist/**', 'build/**', 'out/**', 'coverage/**', '.next/**', '.nuxt/**', 'vendor/**', 'target/**', '.venv/**', '__pycache__/**', '*.min.js', '*.min.css', '*.map', '*.lock', 'package-lock.json', 'pnpm-lock.yaml', 'yarn.lock', 'Cargo.lock', 'poetry.lock', 'uv.lock', 'composer.lock', 'go.sum'];
|
|
7
7
|
export async function resolveTarget(options) {
|
|
@@ -179,12 +179,41 @@ export async function resolveDiff(rootPath, options) {
|
|
|
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
181
|
const { open } = await import('node:fs/promises');
|
|
182
|
+
const { constants } = await import('node:fs');
|
|
182
183
|
if (!Number.isFinite(line) || line < 1)
|
|
183
184
|
return undefined;
|
|
184
|
-
|
|
185
|
+
if (typeof path !== 'string' || path.length === 0)
|
|
186
|
+
return undefined;
|
|
187
|
+
let absolute;
|
|
188
|
+
try {
|
|
189
|
+
absolute = joinWithin(rootPath, path);
|
|
190
|
+
}
|
|
191
|
+
catch {
|
|
192
|
+
return undefined;
|
|
193
|
+
}
|
|
194
|
+
const realRoot = await realpath(rootPath).catch(() => undefined);
|
|
195
|
+
if (!realRoot)
|
|
196
|
+
return undefined;
|
|
185
197
|
let handle;
|
|
186
198
|
try {
|
|
187
|
-
|
|
199
|
+
const before = await lstat(absolute).catch(() => undefined);
|
|
200
|
+
if (!before || !before.isFile())
|
|
201
|
+
return undefined;
|
|
202
|
+
const flags = constants.O_RDONLY | (constants.O_NOFOLLOW ?? 0) | (constants.O_NONBLOCK ?? 0);
|
|
203
|
+
handle = await open(absolute, flags);
|
|
204
|
+
const opened = await handle.stat();
|
|
205
|
+
if (!opened.isFile() || opened.dev !== before.dev || opened.ino !== before.ino || opened.size !== before.size)
|
|
206
|
+
return undefined;
|
|
207
|
+
// Parent directories are not covered by O_NOFOLLOW, so a symlinked parent
|
|
208
|
+
// could still redirect the read outside the target. Resolve the opened
|
|
209
|
+
// path and require it to stay inside the real target root and to still
|
|
210
|
+
// name the inode we actually opened.
|
|
211
|
+
const canonical = await realpath(absolute).catch(() => undefined);
|
|
212
|
+
if (!canonical || !isWithin(realRoot, canonical))
|
|
213
|
+
return undefined;
|
|
214
|
+
const current = await lstat(canonical).catch(() => undefined);
|
|
215
|
+
if (!current || !current.isFile() || current.dev !== opened.dev || current.ino !== opened.ino || current.size !== opened.size)
|
|
216
|
+
return undefined;
|
|
188
217
|
const buffer = Buffer.alloc(Math.min(maxBytes, 256 * 1024));
|
|
189
218
|
const { bytesRead } = await handle.read(buffer, 0, buffer.length, 0);
|
|
190
219
|
const text = buffer.subarray(0, bytesRead).toString('utf8');
|
package/lib/tools.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { compileToolArguments } from './schema.js';
|
|
2
|
-
import { readDeepStatus } from './deep-coordinator.js';
|
|
2
|
+
import { hasDeepJournal, readDeepStatus } from './deep-coordinator.js';
|
|
3
3
|
const OUTPUT_SCHEMA = { type: 'object', additionalProperties: true };
|
|
4
4
|
const STRING = { type: 'string' };
|
|
5
5
|
const STRING_ARRAY = { type: 'array', items: { type: 'string' } };
|
|
@@ -84,7 +84,7 @@ export function createSecurityTools(service, getCoordinator = () => undefined) {
|
|
|
84
84
|
}, ['scanId'], (args, exec) => coordinator().resume(args, exec), 60000),
|
|
85
85
|
tool('codex_security_get_deep_scan_status', 'Read durable Deep orchestration status and budget usage without exposing the recovery claim.', {
|
|
86
86
|
scanId: SCAN_ID,
|
|
87
|
-
}, ['scanId'], (args) => readDeepStatus(service, String(args['scanId'])), 60000),
|
|
87
|
+
}, ['scanId'], (args, exec) => readDeepStatus(service, String(args['scanId']), exec), 60000),
|
|
88
88
|
tool('codex_security_get_scan_context', 'Read the current state of a scan: record, progress, draft presence, candidate/validation/attack-path counts, review items and saved artifacts. Call this when resuming work or after a context loss.', { scanId: SCAN_ID }, ['scanId'], (args) => service.getScanContext(args), 60000),
|
|
89
89
|
tool('codex_security_update_scan_context', 'Append notes or store a threat model. The bound include/exclude scope cannot be changed.', {
|
|
90
90
|
scanId: SCAN_ID, notes: STRING, threatModel: JSON_OBJECT, includePaths: INCLUDE_PATHS, excludePaths: EXCLUDE_PATHS,
|
|
@@ -136,16 +136,8 @@ export function createSecurityTools(service, getCoordinator = () => undefined) {
|
|
|
136
136
|
}, ['scanId'], async (args, exec) => {
|
|
137
137
|
const record = await service.store.get(String(args['scanId']));
|
|
138
138
|
let managed = record?.record.managedParent === args['scanId'];
|
|
139
|
-
if (!record)
|
|
140
|
-
|
|
141
|
-
await readDeepStatus(service, String(args['scanId']));
|
|
142
|
-
managed = true;
|
|
143
|
-
}
|
|
144
|
-
catch (error) {
|
|
145
|
-
if (!(error instanceof Error) || !error.message.includes('Deep scan journal not found'))
|
|
146
|
-
throw error;
|
|
147
|
-
}
|
|
148
|
-
}
|
|
139
|
+
if (!record && await hasDeepJournal(service, String(args['scanId'])))
|
|
140
|
+
managed = true;
|
|
149
141
|
return managed ? coordinator().cancel(args, exec) : service.cancelScan(args);
|
|
150
142
|
}, 60000),
|
|
151
143
|
tool('codex_security_list_scans', 'List saved scans with status, mode, target, finding counts and severity counts. Filter by status/mode/target substring. Use it to find previous scans for comparison or triage.', {
|
package/lib/types/constants.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
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.10";
|
|
4
4
|
export declare const ROUTE_PREFIX = "/plugins/codex-security";
|
|
5
5
|
export declare const PRODUCER: {
|
|
6
6
|
name: string;
|
|
@@ -26,7 +26,9 @@ export interface DeepHost {
|
|
|
26
26
|
}): Promise<DeepActivation>;
|
|
27
27
|
}
|
|
28
28
|
/** Public status omits the recovery-claim digest, source snapshots and worker output. */
|
|
29
|
-
export declare function readDeepStatus(service: SecurityService, scanId: string): Promise<Json>;
|
|
29
|
+
export declare function readDeepStatus(service: SecurityService, scanId: string, actor: DeepActor): Promise<Json>;
|
|
30
|
+
/** Whether a Deep journal exists for a scan id, without disclosing its contents. */
|
|
31
|
+
export declare function hasDeepJournal(service: SecurityService, scanId: string): Promise<boolean>;
|
|
30
32
|
/** Owns a Deep campaign until it seals or is interrupted. State lives on disk, not in this instance. */
|
|
31
33
|
export declare class DeepCoordinator {
|
|
32
34
|
private readonly service;
|
|
@@ -44,6 +44,10 @@ export declare class WorkerRegistry {
|
|
|
44
44
|
acceptFinalStep(childId: string, position: StepPosition): void;
|
|
45
45
|
read(childId: string, path: string): Promise<string>;
|
|
46
46
|
private readText;
|
|
47
|
+
/** Reuse the bytes verified by the preceding read; fall back to a fresh verified read. */
|
|
48
|
+
private cachedText;
|
|
49
|
+
/** Cache the verified text so chunk acceptance does not re-read whole files. */
|
|
50
|
+
private cacheText;
|
|
47
51
|
readChunk(childId: string, path: string, offset: number): Promise<{
|
|
48
52
|
path: string;
|
|
49
53
|
content: string;
|
package/lib/types/index.d.ts
CHANGED
|
@@ -6,7 +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.
|
|
9
|
+
export declare const version = "0.1.10";
|
|
10
10
|
export declare const packageName = "@lornatang/dsh-codex-security";
|
|
11
11
|
export { SecurityService };
|
|
12
12
|
export type { ConfigType };
|
package/lib/util.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { createHash, randomBytes } from 'node:crypto';
|
|
2
|
-
import { link, mkdir, open, readFile, readdir, realpath, rename, rm
|
|
2
|
+
import { link, mkdir, open, readFile, readdir, realpath, rename, rm } from 'node:fs/promises';
|
|
3
3
|
import { basename, dirname, isAbsolute, join, normalize, relative, resolve, sep } from 'node:path';
|
|
4
4
|
import { setTimeout as delay } from 'node:timers/promises';
|
|
5
5
|
export function sha256(value) {
|
|
@@ -23,9 +23,15 @@ export async function ensureDir(path) {
|
|
|
23
23
|
}
|
|
24
24
|
export async function writeFileAtomic(path, data) {
|
|
25
25
|
await ensureDir(dirname(path));
|
|
26
|
-
const tmp = join(dirname(path), '.' + process.pid + '.' +
|
|
26
|
+
const tmp = join(dirname(path), '.' + process.pid + '.' + randomBytes(12).toString('hex') + '.tmp');
|
|
27
|
+
const handle = await open(tmp, 'wx', 0o600);
|
|
27
28
|
try {
|
|
28
|
-
|
|
29
|
+
try {
|
|
30
|
+
await handle.writeFile(data);
|
|
31
|
+
}
|
|
32
|
+
finally {
|
|
33
|
+
await handle.close();
|
|
34
|
+
}
|
|
29
35
|
const deadline = Date.now() + 2000;
|
|
30
36
|
let pause = 10;
|
|
31
37
|
for (;;) {
|
|
@@ -113,15 +119,22 @@ export async function readJson(path) {
|
|
|
113
119
|
}
|
|
114
120
|
}
|
|
115
121
|
export async function readText(path, limit = 1024 * 1024) {
|
|
122
|
+
let handle;
|
|
116
123
|
try {
|
|
117
|
-
|
|
118
|
-
|
|
124
|
+
handle = await open(path, 'r');
|
|
125
|
+
const size = Math.max(0, Math.floor(limit));
|
|
126
|
+
const buffer = Buffer.alloc(size);
|
|
127
|
+
const { bytesRead } = await handle.read(buffer, 0, size, 0);
|
|
128
|
+
return buffer.subarray(0, bytesRead).toString('utf8');
|
|
119
129
|
}
|
|
120
130
|
catch (error) {
|
|
121
131
|
if (error.code === 'ENOENT')
|
|
122
132
|
return undefined;
|
|
123
133
|
throw error;
|
|
124
134
|
}
|
|
135
|
+
finally {
|
|
136
|
+
await handle?.close().catch(() => undefined);
|
|
137
|
+
}
|
|
125
138
|
}
|
|
126
139
|
export async function readFullText(path) {
|
|
127
140
|
try {
|
|
@@ -194,20 +207,128 @@ export function globToRegExp(pattern) {
|
|
|
194
207
|
else
|
|
195
208
|
out += char;
|
|
196
209
|
}
|
|
210
|
+
// Collapse adjacent identical quantifiers so crafted globs cannot trigger catastrophic backtracking.
|
|
211
|
+
out = out.replace(/(?:\.\*){2,}/g, '.*').replace(/(?:\[\^\/\]\*){2,}/g, '[^/]*');
|
|
197
212
|
const prefix = anchored ? '^' : '(?:^|.*/)';
|
|
198
213
|
const re = new RegExp(prefix + out + '$');
|
|
199
214
|
return negate ? new RegExp('^(?!' + re.source.replace(/^\^/, '') + ').*$') : re;
|
|
200
215
|
}
|
|
216
|
+
const GLOB_CACHE = new Map();
|
|
217
|
+
const GLOB_CACHE_LIMIT = 4096;
|
|
218
|
+
const GLOB_MAX_LENGTH = 1024;
|
|
219
|
+
/** Compile a glob once into tokens; matching uses a linear-time DP that cannot backtrack exponentially. */
|
|
220
|
+
function compileGlob(pattern) {
|
|
221
|
+
const cached = GLOB_CACHE.get(pattern);
|
|
222
|
+
if (cached !== undefined || GLOB_CACHE.has(pattern))
|
|
223
|
+
return cached ?? null;
|
|
224
|
+
let compiled = null;
|
|
225
|
+
try {
|
|
226
|
+
let value = pattern.trim();
|
|
227
|
+
if (value.length > GLOB_MAX_LENGTH)
|
|
228
|
+
throw new Error('glob pattern too long');
|
|
229
|
+
if (value.startsWith('./'))
|
|
230
|
+
value = value.slice(2);
|
|
231
|
+
let negate = false;
|
|
232
|
+
if (value.startsWith('!')) {
|
|
233
|
+
negate = true;
|
|
234
|
+
value = value.slice(1);
|
|
235
|
+
}
|
|
236
|
+
if (value.endsWith('/'))
|
|
237
|
+
value += '**';
|
|
238
|
+
const anchored = value.includes('/');
|
|
239
|
+
const tokens = [];
|
|
240
|
+
let literal = '';
|
|
241
|
+
const flush = () => { if (literal) {
|
|
242
|
+
tokens.push({ kind: 'literal', value: literal });
|
|
243
|
+
literal = '';
|
|
244
|
+
} };
|
|
245
|
+
for (let i = 0; i < value.length; i++) {
|
|
246
|
+
const char = value[i];
|
|
247
|
+
if (char === '*') {
|
|
248
|
+
if (value[i + 1] === '*') {
|
|
249
|
+
flush();
|
|
250
|
+
tokens.push({ kind: 'globstar' });
|
|
251
|
+
i += 1;
|
|
252
|
+
if (value[i + 1] === '/')
|
|
253
|
+
i += 1;
|
|
254
|
+
}
|
|
255
|
+
else {
|
|
256
|
+
flush();
|
|
257
|
+
tokens.push({ kind: 'star' });
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
else if (char === '?') {
|
|
261
|
+
flush();
|
|
262
|
+
tokens.push({ kind: 'qmark' });
|
|
263
|
+
}
|
|
264
|
+
else
|
|
265
|
+
literal += char;
|
|
266
|
+
}
|
|
267
|
+
flush();
|
|
268
|
+
compiled = { negate, anchored, tokens };
|
|
269
|
+
}
|
|
270
|
+
catch {
|
|
271
|
+
compiled = null;
|
|
272
|
+
}
|
|
273
|
+
if (GLOB_CACHE.size >= GLOB_CACHE_LIMIT)
|
|
274
|
+
GLOB_CACHE.clear();
|
|
275
|
+
GLOB_CACHE.set(pattern, compiled);
|
|
276
|
+
return compiled;
|
|
277
|
+
}
|
|
278
|
+
/** Full-match a token sequence against str in O(tokens x length) time (no catastrophic backtracking). */
|
|
279
|
+
function globBodyMatch(tokens, str) {
|
|
280
|
+
const n = str.length;
|
|
281
|
+
let dp = new Uint8Array(n + 1);
|
|
282
|
+
dp[0] = 1;
|
|
283
|
+
for (const token of tokens) {
|
|
284
|
+
const next = new Uint8Array(n + 1);
|
|
285
|
+
if (token.kind === 'literal') {
|
|
286
|
+
const lit = token.value;
|
|
287
|
+
const len = lit.length;
|
|
288
|
+
for (let j = 0; j + len <= n; j++)
|
|
289
|
+
if (dp[j] && str.startsWith(lit, j))
|
|
290
|
+
next[j + len] = 1;
|
|
291
|
+
}
|
|
292
|
+
else if (token.kind === 'qmark') {
|
|
293
|
+
for (let j = 0; j < n; j++)
|
|
294
|
+
if (dp[j] && str[j] !== '/')
|
|
295
|
+
next[j + 1] = 1;
|
|
296
|
+
}
|
|
297
|
+
else if (token.kind === 'star') {
|
|
298
|
+
next[0] = dp[0] ?? 0;
|
|
299
|
+
for (let j = 1; j <= n; j++)
|
|
300
|
+
next[j] = (dp[j] ?? 0) || (str[j - 1] !== '/' && (next[j - 1] ?? 0) ? 1 : 0);
|
|
301
|
+
}
|
|
302
|
+
else {
|
|
303
|
+
next[0] = dp[0] ?? 0;
|
|
304
|
+
for (let j = 1; j <= n; j++)
|
|
305
|
+
next[j] = (dp[j] ?? 0) || (next[j - 1] ?? 0);
|
|
306
|
+
}
|
|
307
|
+
dp = next;
|
|
308
|
+
}
|
|
309
|
+
return dp[n] === 1;
|
|
310
|
+
}
|
|
311
|
+
function globPositiveMatch(compiled, path) {
|
|
312
|
+
if (compiled.anchored)
|
|
313
|
+
return globBodyMatch(compiled.tokens, path);
|
|
314
|
+
if (globBodyMatch(compiled.tokens, path))
|
|
315
|
+
return true;
|
|
316
|
+
for (let i = 0; i < path.length; i++)
|
|
317
|
+
if (path[i] === '/' && globBodyMatch(compiled.tokens, path.slice(i + 1)))
|
|
318
|
+
return true;
|
|
319
|
+
return false;
|
|
320
|
+
}
|
|
201
321
|
export function matchesAny(relPath, patterns) {
|
|
202
322
|
const path = relPath.split(sep).join('/');
|
|
203
323
|
for (const pattern of patterns) {
|
|
204
324
|
if (!pattern)
|
|
205
325
|
continue;
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
326
|
+
const compiled = compileGlob(pattern);
|
|
327
|
+
if (!compiled)
|
|
328
|
+
continue;
|
|
329
|
+
const matched = globPositiveMatch(compiled, path);
|
|
330
|
+
if (compiled.negate ? !matched : matched)
|
|
331
|
+
return true;
|
|
211
332
|
}
|
|
212
333
|
return false;
|
|
213
334
|
}
|