mirror of
https://github.com/garrytan/gstack.git
synced 2026-10-03 09:56:57 +02:00
v1.91.12.0 v1.91.12.0: audit fix wave, ~11-minute paid eval lanes, eval reliability policy (#2999)
* test: delete test-infrastructure dead code (G) - exit-propagation drives the runner's real strict verdict (BunTestOutputClassifier + strictTestExitCode); delete the unused shardRunLooksTruncated predicate. - delete skill-coverage-matrix registry + its gate (nothing reads it; the floor already iterates skillCensus()). - delete touchfiles-facade export-parity tests (Bun fails missing imports at link time) and the duplicated E2E_TIERS tier-value test. - delete brain-cache-spec TRANSPORT_DEFAULT_POLICY, SKILL_RUN_RETENTION_DAYS and the now-unused BrainTrustPolicy type with their literal tests. AUTOPLAN_PREFLIGHT_BUDGET_BYTES stays: skill-preflight-budget enforces it against real resolver output. - delete audit-compliance's JSDoc-comment grep. * test: replace product tests that fake the product with real-boundary tests (F) - design: serve.test.ts drove an inline mirror server; now two tests run the real serve() on an ephemeral port (reload confinement, submit exit 0). - setup-gbrain: rollback + voyage tests execute the template-extracted init blocks (3 sites) instead of drifted local bash copies. - terminal-agent: internalHandler source greps replaced by a behavioral /internal/grant + /internal/revoke auth matrix (no/wrong/valid token). - /health: server-security-surface and the server-auth / security-audit-r2 / sidebar-tabs source greps fold into one liveness-only check on the real body; the L4 sidecar wiring gets a behavioral /pty-inject-scan test. - delete tautologies (browser-manager onDisconnect, memory-command #12), ios swiftui tap fixture self-check, memory-ingest put_page grep, detach source greps, sidebar-agent absence pins, dead-CSS pins + the dead CSS, security-audit-r2 Task 1 + the test-only meta-commands re-export, duplicate generated-SKILL.md checks. - make-pdf coverage-gaps cases move into their owner test files. * test: delete tests of dead eval code (A) - A1: the retired Eng lexical oracle (evaluateEngSeedCoverage, isEngSeedDecisionAUQ), the completion-handoff detector and the retained corpus had no paid caller since v1.87.6; delete their 26 replay files, ~2.6k helper LOC and fixtures, and the dead blocks in 8 mixed files (live hasNativePlanTerminal / batching assertions stay). - A2: dead viewport approvers in autoplan-artifact-permission and their 11 replay files + fixtures; recorder/launcher cases stay. - A3: never-wired oracles and seeders (autoplan-phase-order, eng-finding-fixture, ceo-paired-fixture, design-ui-scope, plan-skill-completion, pty-current-screen, required-reads, transcript-section-logger); plan-seed-submission now decodes through the production createPtyScreen; section manifests name their actual guard. - A4: zero-reference helper exports, plus execGit and invokeAndObserve found by the reachability pass. - 52 fixtures orphaned by the deletions; touchfile and selection-table entries for every deleted path. * test: clean up the paid eval lane (B1-B4, B6, B7) - B1: delete paid files that assert nothing or cannot pass meaningfully: skill-llm-eval-spec and skill-e2e-spec-execute (test.todo), gemini-e2e (+ gemini-session-runner; no gemini CLI in CI), ship-idempotency (red since v1.63), the two opus-4-7 *-sonnet overlay wrappers, conductor-prose (+ its source-evaluation replay), codex-e2e-plan-format; drop their keys, scripts and census rows. - B2: skill-llm-eval grades browse/sections/command-list.md with one union judge that also carries the baseline score pin; regression-vs-baseline deleted (paid run: pass, c4/c4/a4). - B3: memory-pipeline, ios-qa, ios-qa-swift-build and plan-tune-cathedral make no model calls; renamed out of the paid glob so they run on every PR. Swift builds need GSTACK_TEST_SWIFT=1; device stub deleted. - B4: codex-e2e*, outside-voice, aside and ios-device cannot run in the CI image; excluded from the weekly lane with a tracked re-entry condition. - B6: fold opus-47's negative routing controls into skill-routing-e2e journey-negatives (paid run: 3/3 unrouted) and delete the file. - B7: delete the never-green brain-privacy-gate eval; a free gstack-skill-start test now proves consent precedes artifacts egress. * test: retire the finding-count cluster and trim its helpers (C) - C0/C1: the five never-green evals (skill-e2e-autoplan-chain and skill-e2e-plan-{ceo,eng,design,devex}-finding-count) failed on harness and budget, never on skill behavior; delete them, their touchfile/tier ids, AUTOPLAN_CHAIN_BUDGET and the dedicated eighth periodic slice (--slices 7). - C2: delete the helper groups whose only paid consumers were those files (11 modules), trim claude-pty-runner and eng-seeded-coverage to the paid closure, and delete the free replay tests whose assertions exercised only that dead code (89 files, 135 orphaned fixtures). Blocks that used dead code only as input for a live subject keep their assertions: the multiSelect default moved to plan-review-decisions, runner PTY tests use inline caller policies, and the timer-safe budget checks moved to eng-finding-retry-budget. - The eight production-touching files stay except ceo-current-decision-record (its template read only feeds the retired counter). - CARVE_GUARDS.autoplan is behavioral 'none'; TODOS records the lost chain and per-finding cadence coverage with their re-entry tests. * test: fold per-incident replay series into their detector owners (D) Twelve detector families move into one owner test each: 73 incident files become describe blocks in ceo-section-loading-fixture (stale-fill race), model-overlays, coverage-audit-evidence, autoplan-phase-observer, native-auto-decide, outside-voice-evidence, eng-first-review, plan-count-completion, plan-count-file-permission, ceo-mode-option, plan-scope-selection and plan-count-prerequisite. Each block keeps its original code and fixture, so every case still runs; only tests asserting the incident file's own touchfile registration are dropped (41). Touchfile lists that named an incident now name its owner. * test: start the plan-count history PTY on its readiness marker (H) The fake CLI prints a startup marker and the runner waits for it instead of the fixed 8 s startup sleep (8.6 s -> 0.9 s locally). eng-semantic-terminal's sleeping registration cases went with C; plan-count-timeout keeps the fixed wait because it asserts deadline behavior. * test: derive paid touchfiles from each eval's static closure (E) touchfiles.test.ts now checks, per key, that the paid file's static test/helpers and test/fixtures closure (plus fixture paths it names in string literals) is covered, and names the file, path, chain and key to fix when it is not. Free *.test.ts files are no longer touchfiles, so editing a free replay test stops selecting paid evals: 950 entries removed, 653 real closure paths added. The hand-copied inventories go: periodic-fixture-selection, fake-impeccable-touchfiles and 45 per-file selection examples. Selection for the sample edits (plan-eng-review template, claude-pty-runner, plan-count-fixture, gstack-config) loses no case under either profile. CONTRIBUTING documents the rule and its lower bound. * test: skip hollow tier shards and census judges in the paid planner (B5) A paid file is now skipped for a tier lane only when every E2E id it registers is known statically and none has that tier; ids come from the touchfile registrations and literal testName/*IfSelected arguments, so a comment or skill path that quotes another id cannot unschedule it, and computed names keep today's scheduling. --list and the manifest show each skip as "skipped: no E2E_TIERS id has tier <tier>". The weekly gate census drops the LLM judges (--skip-judges); they still run in the periodic census and PR gate lanes. Gate lane 52 -> 42 files, census 41; periodic 77 -> 69. * test: run seven paid evals on the current default capture model (B8) skill-e2e-{auq-matrix,plan-format,qa-bugs,retro,workflow} pinned claude-opus-4-7 and skill-e2e-office-hours plus -brain-writeback pinned claude-sonnet-4-6; none tests a historical model, so they now capture with resolveEvalModel('capture'), and the free harness tests that execute these registrations receive the same resolver. The paid re-pin run passed all of them. skill-e2e-{design,office-hours-phase4,plan-prosons,plan} keep claude-opus-4-7: six of their cases failed on the default model (three timeouts, a missing report file, a format miss and a posture score of 3), so per the plan's fallback they keep their pins with a TODOS entry. The pre-spend estimate and drop threshold are in docs/test-audit-2026-09.md. * test: guard the reduced suite against new test-of-test files - test/test-of-test-ratchet.test.ts records the 228 free tests that import only test/ code and fails on a new one, naming the owner test to extend instead; a stale baseline entry fails with the remove instruction. - test/helpers/resolve-repo-path.ts is the one specifier/literal resolver for the ratchet and the touchfile closure invariant, with its own unit tests. - CONTRIBUTING "Test tiers" describes the paid-failure workflow (fix, then one row in the detector's owner test) and the ratchet; TEST_PORTFOLIO gains the detector -> owner-test table and no longer claims an Autoplan chain eval. - TODOS: automatic exclusion policy for chronically red periodic files (P3), the deferred native-completion table collapse, the unused CEO payment seeder; the PTY readiness item is narrowed to the paid runner. - docs/test-audit-2026-09.md collects the triage, security mapping, inventories, selection proof, behavior-commit decisions and retained false positives. * v1.91.8.0 test: smaller suite, derived paid selection, retired never-green evals Release metadata for the test-reduction branch: VERSION 1.91.8.0 (1.91.7.0 is claimed by #2983), CHANGELOG with the measured before/after table and a contributor section, durations re-recorded on Ubicloud standard-16 (857 files, 0 failures), the agents digest, CONTRIBUTING's after-measurement row, the B8 fallback TODOS entry, and the after metrics, kept-vs-plan notes, B8 run and census estimate in docs/test-audit-2026-09.md. * fix(ubicloud): skip retrieval globs that match nothing instead of reporting a failed pull * test: pin DISABLE_AUTOUPDATER in hermetic env and capture corrupt-seed warning Both EVALS_HERMETIC branches of buildHermeticEnv now carry DISABLE_AUTOUPDATER=1 (the allowlist scrubbed the workflow's copy, so every PTY screen showed the updater's npm-prefix failure). Per-test overrides still win. The corrupt durations-seed test now captures its expected warning and restores the console spy. * style(cso): format lib/cso TypeScript with pinned Prettier Mechanical reformat only. Minified transpile output is byte-identical for 21 of 22 files; witness.ts differs only in three regex flag orders (/mi -> /im), which JavaScript canonicalizes. Source-text assertions over lib/cso now compare whitespace-insensitively with the same tokens. * fix(cso): import join for compiled-launcher assertion witnesses Compiled installs always take the non-Bun branch, which called an unimported join and threw before any runtime-tested assertion could be witnessed. The child command selection is now a pure, platform-aware function; a missing sibling launcher fails with its expected path. * fix(browse): make connect --supervise actually respawn a crashed server The supervisor respawned with a block-scoped env that no longer existed, so every attempt threw and the loop gave up after five tries. The headed env is now one pure helper used by connect and respawn, the loop is an injectable runHeadedSupervisor with behavioral tests, failures name the daemon log and relaunch command, and connect's usage advertises --supervise. * test: one finite PR world for the shared-libs fixture; name dual-voice probe evidence The shared-libs shim served 2 PRs for pulls?state=all and endless full pages for state=open. gh pr list, pulls?state=open|all|closed (per_page/page, short last page, direction) and search/issues now page one deterministic table: PR 7, 600 older open PRs, PR 42 and 3 closed PRs, so five 100-item open-metadata pages still leave older open PRs unchecked. The Contents API lists pinned directories (the captured attempt got 404 for contents/ and contents/src while files resolved, then fell back to a raw host), unknown endpoints return 404 instead of repo metadata, and the read-only detector is unchanged. Free tests cover view agreement, the budget bound, gh/curl agreement and the empty world. Dual-voice outside-voice failures now report probeToolUseId, probeMode and the canonical-match result with the reason the probe output was rejected. * feat: require a zero-error product typecheck and a test type-debt ratchet Adds tsconfig.json (strict) over product code, fixes its remaining 90 diagnostics (type-only, interface corrections, and explicit narrowing), and adds a typecheck job to the required free-tests aggregate running bun run typecheck, the test-code ratchet (identity -> count baseline, fails on new, repeated, or unlocked fixed diagnostics), and the lib/cso format check. Reuses fixes from #2447 where they still applied. * test: follow the headed env helper and the typecheck gate in source-shape checks * fix(test): pin the package.json change kind in shared-input selection tests computePaidCaseSelection read the version-only exemption from git even when changed files were injected, so the shared-input test failed on main and on version-only branches. The exemption is now an optional input; the test pins a real package.json change and covers the version-only case. * test: judge plan-count completion on structured evidence, not wording Replaying run 36385945043's two Design attempts showed the existing routes rejected correct endings: attempt 1 at the typed-completion path field ('- Reviewed plan written to …' is not a 'Plan written to' line), attempt 2 at the leading-fence veto (its final message opens with the dashboard). nativePlanTerminalPreconditions is the structural prefix of hasNativePlanTerminal (behavior unchanged). structuredPlanCompletion adds, inside the existing nativeSummary branch: a complete report (Design binding for Design), a completed review-log row for the expected skill appended during this attempt under the child's GSTACK_HOME/project slug (resolved with bin/gstack-slug) and stamped with the fixture commit, timed between the report/last answer (second resolution) and the final native message, a final message with stop_reason end_turn (now carried on public transcript messages), and no visible question or permission prompt. Timeout summaries add idleFor and lastTerminalCandidate. Terminal and throw captures copy the plan file and review-log rows into the artifact directory; copies are best-effort and recorded in evidence-copy.json. Free regressions: both captured Design endings (trimmed fixture with provenance; report, row and end_turn reconstructed and labelled), the negative controls, and real-PTY completion/timeout runs through the real review logger. * test: structural Design count boundary; TODO proposals are not findings Replaying run 36385945043 through the Design count predicates: routing, focus and learnings setup was not recognized as setup, Issue 1 was counted pre-review in both attempts (the boundary fired on it), and attempt 2 counted the Font TODO proposal as a finding (review=4 and review=5 for five issues). The paid caller now starts review at the first answered native decision that is not setup (recognized packet, or setup header/question ID), a completion handoff, artifact rendering or a TODO proposal (the review's Add to TODOS.md / Skip / Build it now menu). TODO proposals are recorded as administrative extra decisions. The replay asserts each counted call: both attempts review=5 (Issues 1-5). isDesignCountFirstReview and its controls are unchanged. * test: CEO classifier throws name the question and matched predicates Replaying run 36385945043's FAN-1 and ERR-1 throws (ledger rows reconstructed from rendered diffs) through ceoPaymentFinding: the email obligation's row, subject, option and proposal predicates pass and the ELI10 explanation-defect predicate fails first ('lets that exception fly out', 'the error bubbles up'). Binding the defect to the named ledger row instead (the planned fix) was tried and reverted: scoped to the email seed it flips 30+ existing cf74 still-rejects replays, which require a vocabulary-free, ledger-bound email question to earn credit only through a complete saved comparison. With FAN-1's rendered currentDecision payload reconstructed, the recorded- decision path counts it, so the real saved plan (not uploaded) must have differed; failure artifacts now retain it. The classifier stays fail-closed and unchanged. Its throw now prints the header, the first 200 question characters and each obligation's predicate results. Free regressions with provenance and negative controls: an unrelated question, an email question whose row says it is already rescued, and a ledger ID whose row belongs to another seed. * chore: regenerate the test type-debt baseline on top of #2994 * fix(typecheck): strip the checkout root from ratchet diagnostic identities * fix(test): recognize ledger row-ID split candidates so collection stops at the last ACK Run 36385945043's split-overflow case asked all five candidate decisions by 8m55s, but the live candidate check required the question to open with "E1:" and every option to be a known disposition. The skill cited ledger row IDs ("D2.1 — R-E1: …") and offered "Hold, discuss first", so no candidate was recognized and the attempt ran the whole review (1302s). Identity now comes from the native header; the question must open with that candidate's ledger reference, name only that candidate, and offer exactly one include, defer and cut disposition. The selected answer must still be one of those three. The semantic evaluator and every existing negative control are unchanged; a trimmed capture from the run adds the positive case and four row-ID negative controls. * fix(test): stop the eng batching eval once its floor is proven The case's only verdict is reviewCount >= FLOOR (3). Run 36385945043 had three distinct acknowledged review decisions at 6m41s but kept answering until the ceiling (7) at 12m13s. The registration now passes the runner's existing isCollectionComplete stop once FLOOR non-setup, non-administrative review decisions are acknowledged; the floor check, ceiling, budget and counter are unchanged. A child-process registration test proves the stop predicate and that below-floor and timeout outcomes still fail. * test: add the non-blocking 'marathon' E2E tier Full start-to-finish flows move out of the blocking lanes. E2E_TIERS and E2ETier gain 'marathon'; describeE2ETier('marathon') is enabled only when EVALS_TIER=marathon, so the gate/PR and periodic lanes (and the gate census) never run those cases. The PR profile accepts marathon ids as scheduled elsewhere and defers them with their own reason, even on full fallback. * test: move the full office-hours workflow to marathon; add a periodic design-draft checkpoint The full startup workflow runs 1–3 real spec-review rounds (~280s each) and hit its 1200s capture in run 36385945043 at finalize. Review depth is the product's loop, so the case cannot fit a blocking lane without cutting rounds. It is now marathon tier with every assertion unchanged. skill-e2e-office-hours-design-draft.test.ts (periodic) runs the same fixed interview only through the Write that creates the design (269s in that run) and applies the full validator's design-draft checks, the required section reads and the launch/foreign-skill-read guards. validateOfficeHoursDesignDraft is extracted from validateOfficeHoursCompletion, which still applies it. Selection: office-hours-design-draft is registered periodic; the marathon-only file is already excluded from the gate and periodic plans by the B5 planner rule. Tier-alignment regexes and the valid-tier check accept 'marathon'. A type-only cast in plan-scope-selection.test.ts removes a diagnostic whose union print order made the ratchet identity unstable; baseline tightened. * test: supply the split-overflow fixture's HOLD SCOPE mode as a prerequisite The split actor always answered 0E's mode question with HOLD SCOPE. The skill skips that question on an explicit choice, so the fixture now states it and the attempt starts at the five candidate decisions (about 1.5 min earlier in run 36385945043). Candidates, actor policy, floor and semantic evaluation are unchanged; the fixture test pins the supplied choice. * test: start the eng batching eval with its setup prerequisites supplied Routing setup and cross-project learnings (D1/D2 in run 36385945043) are never counted and are not what the case measures. The registration now uses the runner's existing preconfiguredReviewActor so the attempt starts at the review; engSetupAUQ still vetoes any late setup question. The registration test pins the option. * test: count the design-draft paid file and defer marathon ids in PR selection pins The discovered paid-file census grows by one (skill-e2e-office-hours-design-draft). Full-fallback PR selection defers every non-gate id; the shared-input pins now expect periodic and marathon ids there. * fix(review): resolve the judged revalidation, setup-authority, plan-gate and findings-record ambiguities The census review workflow judge scored clarity/actionability 3 on both attempts: smoke-clock limits appeared to forbid post-repair revalidation, the caller deadline was undefined, 'ask for setup' conflicted with the report-only browser rule, fallback-sourced HIGH discrepancies had no gate decision, and the Step 5.8 record omitted adversarial findings. * fix(office-hours): load the builder section for every builder-mode reply Both census builder-wildness attempts answered a direct request for adjacent unlocks without reading phase-2b-builder-brainstorm.md, whose trigger read as applying only to the generative questions. * fix(sync-gbrain): define Step 4 helper args and one atomic write path Both census read-ready attempts spent turns reading the helper source to resolve <user-args>, inspecting fixture internals kept inside the repo, and reconciling 'Read + Edit' with the tmp+mv atomic write, then hit max turns before the verdict. * refactor(evals): share the import-closure walker and add the E2E shard reuse identity sourceDependencyClosure moves from the workflow-judge adapter into scripts/eval-input-cache.ts unchanged, so judge keys stay byte-identical. scripts/e2e-shard-reuse.ts builds the consumed-input identity of one PR-lane E2E shard (test import closure, every registered case's touchfiles, globals, runner/workflow/setup actions, child env pins, CI image, Claude CLI) and fails closed on anything unknown. Marathon joins the always-fresh purposes. * feat(evals): ~12-minute blocking paid lanes and a non-blocking marathon lane - Planner budget mode (--slice-budget S --jobs J): recorded per-tier wall times pack into as many ~9-minute executors as the work needs; the plan records per-slice estimates and the CI job timeout (supervised worst case + 20 min). evals.yml and evals-periodic.yml derive matrix size and timeout-minutes from it; max-parallel covers every slice at once. - Case shards: plan/design/review-army/shared-libs(-paths) run one registered case per process (<file>#<case id>, exact name pattern, exactly one case). - Retry rule: a timed-out attempt is a verdict. Only files whose every case budget is CAPTURE tier or shorter keep one retry; walls shrink to match. - Marathon tier: positive selection, excluded from gate/periodic planners, run by the new evals-marathon.yml (weekly + dispatch, fresh, own report). - PR-lane E2E reuse of verified first-attempt passes on identical inputs; the report rejects reuse outside the fast PR profile. - Duration seed from census run 36385945043, per tier and per case shard. * docs: blocking lane budget, marathon lane, retry policy and E2E reuse * chore(typecheck): lock in two fixed test diagnostics * fix(ci): drop a duplicated env/jobs block in evals-marathon.yml * test(ship-docsync): shard the doc-sync lifecycle by case and drop the duplicate dispatch-only case ship-docsync ran the same fixture and prompt as ship-docsync-completion and asserted a subset of it. The file now runs one case per process, so its lane wall is its longest case instead of half the sum of thirteen. * fix(evals): plan CI-unrunnable cases as excluded entries, not empty case shards design-review-fix drives the Aside browser and registers test.skip on Linux runners, so its case shard executed zero cases and failed the exact-one-case check in proof census 36597762183 (eval-slices 6). CASE_CI_EXCLUDE (reason + tracking, beside PERIODIC_CI_EXCLUDE) now turns such cases into excluded manifest entries that --list and the manifest surface; every planned case shard still must execute exactly its case. * docs(todos): list the case-level Aside exclusion with the CI-unrunnable evals * fix(plan-ceo-review): restore experience-first expansion framing, require the mode handoff, skip pacing menus Census 36597762183: both mode-routing runs logged provenance and moved on without the mandated handoff chat; the EXPANSION run asked an unauthorized batch/narrow pacing menu instead of the first per-addition question; the expansion-energy proposals led with the spec because v1.87.6.0 dropped 'lead with the felt experience'. The HOLD review detector also rejected a decision whose grounding line named no plan file although the owned source Read binds it. * test(outside-plan-disabled): bind quoted prior-record values by their sentence, not phrase order The parent obeyed the off switch and twice named the seeded completed record as pre-existing, once with the quotation after its owner and once with slash separators; the order-specific stripper counted both as current completion. Timestamp, location, current-claim and value-match controls still reject. * test(outside-plan-disabled): compare named record timestamps as instants; negated authorship is not a current claim The repair rerun named the seeded record by its ISO second (2026-09-29T16:58:52Z vs .727Z) and said 'I did not write'; both were misread as a foreign timestamp and a current write. * test(ceo-section-loading): recognize an arrow-ordered stale-fill execution by event roles The census review traced the seeded race as 'R1 miss -> R1 store read (v1) -> W commit v2 -> W cache.delete -> W fulfills -> R1 cache.set(v1) -> R2 (begun after W) hits v1', but the in-flight gate only accepted race vocabulary or fixed sentence shapes. Order, actor, version and dismissal mutations still fail. * test(design-floor): answer the seed-declared all-seven 0D focus menu while it is pending The actor declares 'Design: review all seven dimensions', but its picker reused designReviewSetupAUQ, which only matches already-answered calls (and a narrower header/label set), so the pending D1 focus menu was never answered and the case waited out its 609 s deadline. The skill's Step 0D requires asking; the fixture now answers it. * test(ceo-mode-routing): accept the skill-mandated Note form and Recommendation reason as HOLD posture HOLD Defer/Keep briefs must use 'Note: options differ in kind' (preamble), but the answered-HOLD path demanded a Completeness score, rejected a one-line Net with a semicolon, and read posture only from ELI10. The rerun's brief applied HOLD SCOPE in its Recommendation reason. Revert the ineffective 'always'/'handoff chat' wording: two runs still skipped the mode handoff. * test(qa-bugs): keep claude-opus-4-7 after qa-b6-static stalled on the default model qa-b6-static timed out on claude-fable-5-1 in census 36597762183 and in one of two targeted reruns. Both times the stream stopped mid-message with no pending tool, right after the model found the disabled submit button, and stayed silent until the 300 s deadline. Per the B8 fallback, re-pin with a TODOS entry; budgets and retries are unchanged. A rerun on opus-4-7 passed (125 s, 5/5 detected). * test(evals): add E2E_KINDS, BEHAVIOR_WHY, EVAL_POLICY and CASE_QUARANTINE skeletons Every E2E_TIERS and LLM_JUDGE_TOUCHFILES key starts as 'rule'; BEHAVIOR_WHY and CASE_QUARANTINE start empty. EVAL_POLICY pre-registers the approved panel (3, majority 2), quarantine entry 0.95/10 and exit 0.97/10, 10% cap, 8-weekly-run expiry, Fisher drift alarm and one INFRA re-dispatch. * test(evals): add trial records, panelVerdict, expectContract and trial-outcomes JSONL EvalTestEntry gains case_id, kind, trial, panel, failure_class and policy_version, stamped from the runner's TRIAL_ENV on isolated trial shards. panelVerdict() is the single verdict function (INCOMPLETE on missing or duplicate trials, contract veto at any count, quarantine hard-break rule, INFRA/INCOMPLETE machine classification). expectContract() records failure_class 'contract' on the collector entry and a sidecar before throwing. trial-outcomes JSONL has a fail-closed writer and a data-only reader. * test(evals): pin the fail-closed rule-shard gate through the real --report path Synthetic slice artifacts for rule fail, timeout, missing slice, unreported entry, hollow, never-started, collector failure and wrong-slice reports all exit red before the panel-verdict gate change lands. * test(evals): retire every paid automatic retry Paid evals never retry (approved 2026-09-29): delete SHORT_CASE_RETRY_FILES and retriesWithinCaseCap, drop the retry fields from the registered wall rows (walls now cover one run plus reserve), make retriesForFiles return 0, pass --retry 0 explicitly, and drop --retry 1 from the package.json paid scripts. Add the eval:pass-rates alias. Tests that pinned the old retry allowance are updated as a policy change; review-finalization-budget now proves late-result recording under the production zero-retry arguments. * test(llm-judge): sample every judge as a pre-registered 3-sample panel Each of the 24 skill-llm-eval judges now draws EVAL_POLICY.judge.samples independent samples of the same prompt concurrently inside the unchanged JUDGE_MS budget. Numeric dimensions gate on the per-dimension panel mean against the unchanged threshold; booleans (would_browse, consistent) on a strict majority. An erroring sample fails the whole panel and is never resampled; a refusal is an unscored panel only when every sample refused. callJudge's 429 backoff stays: it is transport before any model output. The workflow-judge cache stores and validates only complete panels, and its identity now records the panel and zero file retries. Harness tests that pinned one provider call per case now pin the panel size. * test(evals): classify every live case and re-select a case when its kind changes E2E_KINDS: rule by default (191 E2E ids), 22 behavior cases whose verdict is a live model choice with an acceptable sub-100% per-trial rate, each with a BEHAVIOR_WHY tolerance, and 25 judge entries (the 24 workflow judges plus the fixed-fixture llm-judge-recommendation rubric check). Contract-shaped cases (ask-before-decide, plan-mode no-writes, mandated steps, secrets, the batching floor) stay rule. Behavior requires a known literal registration and an exact Bun test name so the case runs as its own trial shard. Map-diff selection now diffs E2E_KINDS and BEHAVIOR_WHY per key, and a base revision without them selects every key, so a kind flip runs the panel it introduces. test/eval-kinds.test.ts enforces coverage, tolerances, isolatability and the reviewed counts, printing the literal to add. * feat(evals): per-case pass rates with Wilson intervals, identity series and quarantine policy scripts/eval-flake-rank.ts becomes eval:pass-rates (eval:flake-rank stays an alias, and the legacy aggregate stays exported). It reads eval-store's trial-outcomes JSONL from the last N completed evals-periodic runs on this branch and main (gh, downloading only the trial-outcomes artifact, cached and size-capped, parsed as data), plus local eval dirs, and prints per-case per-trial pass rates with 95% Wilson intervals. A series is a case's own touchfiles minus GLOBAL_TOUCHFILES (caseSeriesIdentities, for the report job to stamp), per model, CLI version and policy version. Labels: INCONCLUSIVE, BROKEN, FLAKY, FAILING, PASSING. --backfill imports legacy slice artifacts as pre-policy trials (first attempt only, attributed by registry id, never guessed) for display only. --gate fails with ACTION REQUIRED on post-policy evidence only: drift below the quarantine entry rule, a rule case behaving like behavior, a one-sided Fisher drop against the previous identity (Holm-controlled), and quarantine entries that met their exit rule, expired after 8 weekly runs, broke the 10% tier cap or are invalid. CASE_QUARANTINE entries now carry a failureClass (detector, harness or model-latency); a product defect has no class and is never quarantined. The policy test pins EVAL_POLICY's approved constants. * feat(eval-pass-rates): attribute legacy records by the exact slug of their display name * ci(image): pin Claude Code 2.1.284 so the eval model is recognized 2.1.251 logs [claude-code:unrecognized_model] for claude-fable-5-1, the eval capture/judge default. 2.1.284 does not. The gate PTY smoke subset (plan-ceo/plan-devex plan-mode, plan-mode-no-op) parses on the new TUI; plan-design-review-plan-mode passed at 293 s on 2.1.284 and timed out at 300 s on 2.1.251 on the same tree. * test(eng-batching): grade the floor once the review report is complete A completed GSTACK REVIEW REPORT ends the review, so the review-question count is final there. Run 36606688266 wrote its report at 1,248 s and closed the session at 1,318 s; the case now stops collection and applies the unchanged floor at the report instead of waiting out the session. No budget changes. * test(eng-batching): bind unsourced native briefs through the report's target Run 36606688266 asked ten separate native review questions (D1-D9 bound to ledger records R1-R9) and failed reviewCount=0 < FLOOR=3: its briefs named the plan by title instead of citing PLAN.md, its report declared 'Review target (fixed): PLAN.md' under '# Engineering review: <plan>', and it kept an unfenced copy of the plan's own H1. The named-source route now accepts those spellings and non-inline ledger briefs. The same replay rejects a foreign, mixed, duplicate or missing target, another plan's title or copied H1, a brief naming another plan or file, a mismatched saved brief, and re-asks. The run-36597762183 capture still counts 3. * fix(plan-design-review): treat a designer with no API key as unavailable Both proof runs (36597762183, 36606688266) printed DESIGN_READY, hit 'No OpenAI API key found' on the first $D variants call, then hand-built HTML/CSS wireframes, screenshots and a comparison board for ~195-245 s before the first review question; the second run timed out at 600 s. A failed first generation now takes the existing text-only path, and the skill forbids substituting hand-built mockups. * fix(deslop-shared-libs): read related sources together within the turn limit Run 36606688266's opportunity audit read sixteen sources one per turn and stopped at error_max_turns; the passing run 36597762183 read the same files in three batched commands. The skill now says turns are bounded and asks for parallel reads or one read-only command per step. * test(ceo-mode-routing): submit a mode review that scrolled past the viewport Run 36606688266 bundled routing, learnings and the mode choice into one native call. Its review panel was taller than the terminal, so the tab bar scrolled off, ceoModeSubmissionInput returned null for 240 s and HOLD SCOPE was never submitted ('no posture match'). With no bar on screen the viewport must still end at the focused Submit prompt, and the accumulated screen text supplies the one complete panel, authenticated exactly as before. Replay controls reject another mode, an unoffered answer, an altered question, a quoted panel, trailing output, a moved cursor and an answered or changed call. * docs(evals): document the pre-registered verdict policy, quarantine, pass-rate history and arithmetic AGENTS.md replaces the retry rule with the approved policy text (no retries; kind fixes trials; no added trials, samples or dispatches after a result; quarantine by CASE_QUARANTINE only; one INFRA/INCOMPLETE re-dispatch) and notes that a pre-registered fixed panel is not rejudging. CONTRIBUTING gains the kind rules, the judge panel, eval:pass-rates and an 'Add a paid eval' checklist. TESTING_INTERNALS describes verdicts, quarantine, history and the arithmetic, including the rule term: 1 trial vs 2-of-3 red rates at p = 0.99/0.95/0.90/0.70/0.30 and lane all-green probabilities for the current 191 rule / 22 behavior / 25 judge registry. * feat(evals): trial planner, slice exit split and panel-verdict report Planner: behavior and quarantined cases become panels of isolated trial shards (<file>#<id>~t<N>) bound by EVALS_SELECTION_JSON=[id] and the exact test name; the file shard excludes them by name. Trials of one case never share a slice, result slugs are unique, panels are validated whole, unknown registrations throw, and the planner prints a capacity preflight. Executor: each trial shard gets its TRIAL_ENV identity and a trial record (outcome, failure class, cause, cost); every shard writes a JUnit report. The slice exit now means execution completeness: a failed rule shard or a trial without a record reds the runner, a failed trial does not. Report: panelVerdict() decides every panel of the first run attempt (later attempts are reported, never replacing it); rule shards keep the unchanged fail-closed checks; collector records all count (no last-attempt wins); census runs enforce the quarantine cap and expiry. It writes collector-outcomes v2, trial-outcomes.jsonl (trials plus JUnit rule/judge cases), report-summary.md, and one headline + failure block with rerun commands, and flags INFRA/INCOMPLETE-only reds for the one re-dispatch. The fail-open suite gains the panel cases: behavior 1/3 red, 2/3 green with its failed trial shown, missing trial INCOMPLETE, contract at 2/3 red, quarantined 1/3 green, 0/3 and contract red, missing slice red, and a later attempt never replacing the first. * chore(evals): refresh paid duration seeds from proof runs 36597762183 and 36606688266 Both tiers, merged in run order (the later run wins). Notable: split-overflow 1332s -> 504s, section-loading 604s -> 342s, mode-routing 575s -> 444s; multi-finding-batching 734s -> 1318s (its red path in run 36606688266). * feat(evals): stamp trial series identities and fit panels to the live registry - scripts/eval-trial-series.ts stamps series_identity (eval-flake-rank's caseSeriesIdentities) on a report's trial-outcomes JSONL as its own step, keeping the history tool out of the paid runner's closure; TrialOutcomeRecord gains the optional series_identity field. - Slice-count plans let a registered trial spill into an ordinary lane when its siblings hold every long lane, so panels never share a runner. - Re-audited test-selection.ts (Stream B added the E2E_KINDS/BEHAVIOR_WHY map-diff; no new module loading) and repinned its hash. - Detach and release floors now count trial shards (66 periodic trials in 22 panels): periodic floor 33,821s, still under eval:bg:periodic's 67,380s. - Coordination fixtures supply the executor's trial records. * ci(evals): attempt-scoped artifacts, verdict-v2 PR comment, weekly pass-rate gate and one INFRA re-dispatch - Slice, census and marathon artifacts carry -a<run_attempt>; reports download them per artifact (no merge), so records never overwrite and a re-run never replaces the first attempt's verdict. - Planners pass --max-parallel for the capacity preflight (24/16 unchanged: the refreshed periodic plan needs 24 slices, the gate census 12). - PR comment: jq-only job reads collector-outcomes v2 (headline, sanitized failure block); the group_by(.name)|last recomputation is gone. - Reports stamp series identities, upload trial-outcomes-* for history, and shard logs upload always (a failed trial no longer reds its runner). - Weekly report: headline + failure block of both lanes in the issue body, the eval:pass-rates --gate step (fails closed without history), close the issue on a green run, and UC-E1: when every red is machine-classified INFRA/INCOMPLETE, one re-dispatch as a new run in its own concurrency group (redispatch_of), both runs reported. * feat(evals): planner-side whole-panel reuse and negative receipts The planner job restores this PR's receipt store once and ships a single filtered set with the plan: a pass or panel receipt with a same-or-newer FAIL for its input identity is dropped, and a panel receipt ships only as a whole PASS panel (re-verified with panelVerdict) from one run. Executors read only that set (no per-slice cache restore or save), so every trial of a panel sees the same receipts; a trial reuses its own record from the panel receipt, keeping a split PASS's failed trial. Trial identities drop the trial index (run-scoped) and bind the panel policy. Executed shards carry their input identity; the report turns a whole fresh PASS panel into a panel receipt and a FAIL panel or failed rule shard into a negative receipt, and marks a panel that mixes reused and fresh trials INCOMPLETE. The report job merges plan, slice and report receipts (newest per file) and saves one store per run. Also fixes two TS2352 casts in browse/test/dia-macos-qualification.test.ts whose diagnostic text drifted with program order (baseline locked, fix only). * feat(evals): --case/--trials local diagnosis and panels in local sharded runs bun run scripts/test-paid-shards.ts --case <id> [--trials N] runs N independent trials of one case through the CI panel runner (trial shards, TRIAL_ENV identity, name-pattern isolation) and prints its panelVerdict(); N defaults to the case's policy panel and CI never reads it. The local sharded path (test:gate:sharded, test:periodic:sharded) now plans the same trial shards and exclusions as CI and exits on execution completeness plus panel verdicts. * test(pty): grant an owned Create pane whose title row is cropped The targeted batching rerun on Claude Code 2.1.284 left its first report Write unanswered for 1,372 s and timed out: the viewport began at the pane's relative file row and rule, with the 'Create file' title cropped above, so the preview parser rejected the file row as foreign. That row must now resolve to the owned path and is skipped before the unchanged line-by-line preview match. Replay controls reject another file, another directory and an edited preview row. * fix(evals): tsx-safe generics in eval-flake-rank, legacy artifact names, no-retry wall docs * test(evals): record the read-only and detector-row invariants as contracts shared-libs-opportunity-judgment and review-design-lite are behavior cases: their recommendation and checklist judgments may vary, but the read-only invariant (commands, provider requests, fixture bytes, hooks, state) and the deterministic fake-engine detector rows are contracts. Both now go through expectContract, so any failure vetoes the panel. * test(judges): sample the recommendation rubric as a panel; never re-ask armJudge llm-judge-recommendation is a judge case: each fixture now draws a 3-sample judgePanel, gates reason_substance on the panel mean and the present/commits/has_because checks on a 2-of-3 majority, thresholds unchanged. armJudge no longer re-asks on a malformed verdict; it is a failed sample, as the judge policy requires. * test(evals): record a pre-turn API or CLI failure as infra recordE2E sets failure_class 'infra' on a failed session whose runner reports error_api, timeout_startup, error_output_stream or a non-zero CLI exit with zero turns and no assistant event. A model refusal, a timeout after model work, max turns, or an explicit caller pass/class keeps its ordinary classification. * test: pin every-record outcome counts and the twelve doc-sync callbacks * test(eng-batching): read the report target as a field, not a spelling The next targeted rerun (Claude Code 2.1.284) again asked eleven separate native questions and again counted zero: its briefs named no plan and its report declared '- **Review target (fixed):** `/abs/PLAN.md`' under '# Eng Review — PLAN.md: <plan>'. An unsourced brief now inherits the one current target field that names a PLAN.md file, whatever its list or emphasis markup; its ledger record still supplies the cited finding and must reproduce the brief exactly. A brief that names its plan must still match the report title. Replays of all three captures count 9, 9 and 3; controls reject a foreign, duplicate or missing target and an archived title. * fix(evals): --case list mode and name precheck; case-shard qa-callers; refresh batching and design-with-ui seeds * chore(release): v1.91.9.0 * test: settle the post-response composer before seeding; give the TPA recorder adapter its infra helper submitPlanSeed accepted a stale empty composer when the transcript recorded end_turn before the CLI repainted (late-repaint-typed-current fails 5/5 on the old helper, passes 5/5 now). The TPA recording fixture extracted recordE2E without isPreTurnInfraFailure, so every failed case threw before recording. * test(autoplan-dual-voice): unwrap Claude Code 2.1.284 subagent hand-back frames; accept read-only probe diagnostics; record before asserting Census run 36626737820: the native CEO report arrived framed and indented, so its INPUT line never matched, and the model's exact probe plus two variable echoes was not canonical. A column-zero line inside a frame, command substitution, backticks, redirects, assignments, CODEX_MODE echoes and output line-count mismatches stay rejected. The failure now records before asserting. * ci(image): keep Claude Code 2.1.251; test(ceo-mode-routing): keep HOLD's own deferrals in scope before assessing its rigor decision 2.1.284 enables per-turn effort for claude-fable-5-1: in gate census 36626737820, 66 of 84 sessions ran longer than on 2.1.251 (+20% session time, +32% thinking tokens) and 11 cases timed out on unchanged budgets. HOLD SCOPE's 0G step asks its own defer/keep menu; the actor answered it Defer and the assessment then judged that scope question as the rigor decision. The actor now answers that menu Keep and assesses the next one. * test: attribute quoted prior-record field lists, state the judge reason bound in its schema, move split-overflow to marathon Census 36629958451 reds: - outside-plan-disabled-no-fallback: the model quoted the pre-existing record as a parenthesized field list with its exact timestamp; attribution now requires that exact timestamp and the record's own field values. - plan-devex-peer-comparison-classification: the judge correctly returned missing but wrote a 1069-character reason, voiding the judgment; structured outputs cannot enforce maxLength, so the bound is stated on the field. - plan-ceo-split-overflow ran 504-1188 s as one PTY flow and set the periodic lane's wall clock; it now runs weekly in the marathon lane. * test: supply holdDeferKeepIndex to the CEO routing mocks and follow split-overflow into the marathon lane The registered-callback fixtures mock ceo-mode-option and lacked the new export; the split fixtures asserted the periodic tier; the registered-budget check looked for split-overflow only in the periodic manifest. * fix(qa): checkpoint receipts print the report link for their exploration file qa-functional-webhook-report failed in two of three censuses because the report linked .qa-evidence/NNN capture folders as "checkpoints" and never linked exploration-NNN.json. The checkpoint receipt now prints link: [checkpoint NNN](exploration-NNN.json), and the functional report template says capture folders are not checkpoints. * docs: final census numbers in the v1.91.9.0 entry; file the paid-eval follow-ups * ci(evals): name the PR-comment loop's unused fields so shellcheck passes (SC2034) * fix(plan-ceo-review): tighten expansion pacing wording to fit the skeleton cap after the main merge The merged skeleton measured 80,166 bytes against its unchanged 80,150 cap. Same instructions: ask separately for each addition, in turn, with no pacing menu; lead each proposal with the felt experience, then shape, effort and impact. * fix(eval-pass-rates): match trial-outcome files by basename so Windows backslash paths are read * fix(evals): repair proof-run reds in design-consultation, document-release, design and QA fixtures - design-consultation Phase 1 asks one brief that confirms context and decides research; the confirm-only first question scored substance 2. - document-release defines ship-owned inputs, exact steps and the JSON result, and drops stale spawned-from-/ship text (judge actionability 3.67 -> 4/4/4). - plan-design-with-ui accepts the Step 0D focus menu the same way the shared picker does ("focus on specific ones?"). - plan-design-review plan-mode saves in three Edits instead of one final Write. - QA functional annotations ask for the full 40-character revision. - Outside-disabled attribution judges quoted prior-record data by its exact timestamp or a dated, pre-existing-record sentence; four captured phrasings replay clean and current claims still fail. - --case can select autoplan-dual-voice by its literal test name. * test(design): revert the three-Edit plan-mode flow A focused paid run still timed out at 300 s: the first three passes alone took 150 s of thinking. The case stays a named timeout red rather than cutting review depth. * test: accept 'review mode = X' auto-decide declarations and parenthetical scope exclusions in the shared-libs actor auto-decide-preserved: the product auto-decided HOLD SCOPE and said "Decision: review mode = HOLD SCOPE"; the grammar knew only "is" and ":". shared-libs-plan-callers: the recommended option said "(no hardening)" and the actor read "hardening" as an expansion. Both replay the captured text, keep negative controls, and passed focused paid runs. * fix(review): pass Review Army checklists by path, run research alongside dispatch, always probe the design detector; state review-log invocation and statuses in the caller fixture - review-army-perf-n-plus-one: the parent copied full checklists into agent prompts and ran web research before dispatch (290 s on a 12-line diff); 212 s now. - review-design-lite: 5 of 6 captured trials reported the detector absent without probing; the probe is mandatory and its first line is reported, and the contract credits only fake-engine rule ids the checklist never names. - review-exploratory-small-cli: the fixture never gave review-log's direct invocation or status vocabulary; the model ran it through bun and wrote status "blocked". The prompt states both and the validator rejects out-of-vocabulary review statuses. Each case passed a focused paid run after repair. * docs(changelog): proof-run product fixes * fix(ship): always run the design-lite detector probe; test(shared-libs): credit a failed first file view and deferred-reuse Skip wording - /ship design-lite: the probe is mandatory and any non-ready first line is stated, matching /review (5 of 6 captured /review trials had skipped it). - shared-libs-pr-coverage: the first PR 42 page-1 read printed only a jq error, so the one refetch is a legitimate recovery, charged to the same budget. - shared-libs-review-prior-coverage: the Skip option said a future review can "reuse it once snapshot coverage holds"; a conditional tail on the recorded decision is not product work. Captured-text regressions and negative controls. * fix(ship,qa,document-release): repair proof-run regressions and fixture gaps - ship-docsync-completion: yesterday's audit-scope result dropped the section's status, so /ship spliced one in; the section now opens with **Status:**. - ship-docsync-missing-asset: a missing section or old Ship-owned mode blocks before launch. - ship-docsync-late-result: the invocation record says prepare already saves the candidate selection (no extra Read; budget unchanged). - qa exploratory: await the method Reads before the first probe. - qa-callers fixture: quote the real review-log record template; allow the git log command plan-completion prescribes. - qa functional observer: a receipt caught mid-link(2) is checked at stop instead of failing with ENOENT (reproduced from CI). Each repaired case passed a focused paid run. * ci(image): pin Claude Code 2.1.284, the version users run Request-body capture shows both 2.1.251 and 2.1.284 send effort "high" to claude-fable-5-1; 2.1.284 adds the model's own profile. The slower 2.1.284 census was mostly API latency: its SDK-only judges were 25% slower too. Nine previously slow cases pass on 2.1.284 within unchanged budgets. * test: one owner per case id, a structural devex 0B setup rule, and correct design/gbrain actors - plan-design-review-plan-mode was registered by two files; the PTY smoke is now plan-design-review-plan-mode-smoke, and a registry test requires one owner per case in case-sharded files. - plan-devex-finding-floor: the template's 0B narrative-confirmation question is classified as setup structurally instead of timing out a Haiku assessor. - setup-gbrain-remote: the actor accepted 'skip' on the MCP-registration question the test asserts; it now accepts that question and declines others. - design-review-plugin-handoff: the fake engine cited a file absent from the fixture repo and index.html linked a missing styles.css. Captured-question regressions with negative controls; each case passed a focused paid run. * test: PTY harness handles clipped reviews and bundled setup tabs; AUQ judge uses structured output; design-consultation carve declines optional outside voices - ceo mode routing: a Submit review taller than the viewport, a setup tab bundled after the mode tab, and a clip through the mode question each hung or misread the run; the native answer is still verified after Submit. - judgeRecommendation requests a 1-5 enum schema; a malformed Haiku reply had scored substance 0 for a 4/5 brief. Judge failures now propagate. - carve section-loading for design-consultation declines the optional outside voices (a supported path) and treats DESIGN.md as the report; timeout unchanged. The Step 0E handoff defect is not fixed (0/15 samples across four wordings, none shipped) and is filed in TODOS. * test: fold the design-consultation completion replay into carve-section-sharding (test-of-test ratchet) * docs(todos): record the pre-push hook shard-order hang * test(qa-callers): disable git auto maintenance in the fixture repo (same guard as shared-libs; from #3002) * test(office-hours-attempt): the fake judge SDK response carries stop_reason like the real API (structured judge requires end_turn) * fix(qa): the caller STOP line says to await the method Reads before any probe ship-exploratory-plan-checks: the model read exploratory.md and sent a capture in the same response, before seeing the section's own await rule. * fix(qa): number the qa value-bar questions from 1 and say reproduced bugs already answer the first two * fix(qa): define evidence.json where it is built, point the preparation gate at the next section, name measured command durations in the report template Recurring qa/qa-only workflow-judge complaints in CI (clarity/actionability 3.33). * fix(plan-eng-review,review): a disallowed question tool is not headless; report kept tests only when some were skipped * fix(plan-eng-review): keep the headless-rule contract phrases adjacent * fix(evals): cut path variance at its measured sources - gstack-qa-evidence capture prints startedAt/completedAt/durationMs and, for --deadline captures, remainingMs; the functional report takes durations from them. The section clock notice asks for one clock read up front instead of one after every checkpoint (QA runs spent 7-14% of tool calls on date -u). - ship plan-completion: skip the audit dispatch when discovery already found no plan (the dispatch-vs-skip conflict produced an optional 60-100 s subagent). - materialize/checkpoint validation errors state the expected schema, so a rejected annotations file is fixable in one call instead of blocking the phase. - session-runner counts turns from the transcript when a run times out, so timeouts stop reporting 'turn 0'. * fix(evals): count timeout turns only from object transcript events * test(qa-callers): deterministic child transport, completion-time handoff reads, compact phase report The exploratory caller cases exist to prove the caller starts and bounds exploratory QA. Their native adversarial reviewer (review) and plan audit (ship plan-checks) now come from recorded child outputs instead of a live subagent, handoff freshness reads are required before completion records rather than every bookkeeping log, and the phase report is compact. Measured: 194-257 s per case against 208-284 s before, no subagent calls. * test(ship-docsync): seed fault cases at their gate instead of replaying attempt 1 The post-dispatch fault cases (missing-marker, launch-failure, timeout-unsettled, late-result, stale-before, stale-after, recovery) now start from a fixture-owned attempt 1: the real actor prepares and dispatches it, its verbatim output is saved once, and the invocation journal carries its pre-dispatch entry with the child asset hashes. The model resumes at Parent processing with a trimmed read list, inspect named as the authoritative repository observation, and recovery's intermediate checkpoint folded into the next attempt's pre-dispatch entry. Assertions count only parent-issued transport events and require a read of the saved attempt-1 output; missing-asset and the legacy failure case keep the full model-driven first attempt, and their prompts are byte-identical. * test(ship-docsync): name the seeded read list and cap journal/report length The first seeded stale-before run spent calls locating documentation.md (two ls sweeps), reading through cat and re-Reading the record before Edit, and ~40 s composing 1.5-2.2 KB entries and report. Name every seeded read path, ask for native Read, and bound entry/report length. * test(ship-docsync): trim the seeded parent's measured model time Measured on the seeded runs: one read the 78 KB ship/SKILL.md, the post-child freshness comparison spent 18-32 s of thinking over full inspect contents, and the final response restated the report (~1.1 KB). Say the phase excerpt stands in for ship/SKILL.md, compare hashes first and read content only for changed paths, and end with one status line. * feat(qa-evidence): enforce the checkpoint sequence and fill report bookkeeping in code - capture refuses to run another probe until a checkpoint anchored on the latest complete capture names this capture as its next command, and every complete capture prints that requirement. - materialize fills revision, runtime, cwd and learning (checkpoints whose next native command differs) when omitted and prints the reportLinks the report must include; the QA section shrinks accordingly. * test(qa-callers): hand the caller phase its invocation-start observations and review token; fix(next-version): fetch without auto maintenance - Every caller case receives the diff, status, log, untracked list, HEAD and an already-captured review start token, so the phase spends its budget on the contract under test instead of re-running setup reads. - gstack-next-version's fetches pass --no-auto-maintenance. On git 2.55 a completed fetch forks detached maintenance in the caller's repository; the free suite's live smoke test ran it inside the CI checkout, and every shard-12 pre-push hook hang so far followed a completed smoke fetch. * feat(deslop-shared-libs): route every Git read through bin/gstack-safe-git The skill made the model retype a long safe-Git prefix on each call and a dropped flag failed shared-libs-read-only. bin/gstack-safe-git applies the fixed env + flag prefix, adds --no-ext-diff --no-textconv to log/show/diff, allows diff only between two explicit object IDs and ls-files only in the NUL-delimited overlay form, and refuses every other shape with one line naming the allowed forms. The template now points at the installed helper (host global runtime via {{SAFE_GIT}}) and drops the prose it enforces. Fixtures resolve the helper to this checkout, the git shim records the safety environment, and isGuardedGitRequest requires the complete prefix (env included) for every repository read. * test(shared-libs): tee to a discard device is not a file write Paid shared-libs-opportunity-judgment t1 on 1213b01 failed read-only on '... | tee /dev/null | sha256sum'. The detector flagged any tee operand while the same devices are allowed for redirection. tee now fails only when an operand is a real file; tee to a file, -a file and -- -a stay violations. * fix(qa-evidence,observer): reject placeholder metadata and replay-only learning; declare the docs atomic-write target - materialize measures revision, runtime and cwd itself and rejects supplied values that differ (CI run wrote revision "HEAD" and runtime "bun"), and refuses learning checkpoints that replay the same probe, naming the fix. - The docs write observer treats Claude Code's atomic temp for the authorized doc target as transient, so a temp renamed before its per-file watch no longer marks the observation incomplete (ship-docsync-completion flake). Per-file monitoring outside declared targets stays fail-closed. * test(qa-functional): fix mode requires only the happy scenario from the model (carried byte-identical from #3002 183b01f4..3e6074b4) verifyQANativeRegression already reruns all eight webhook scenarios on the repaired source, so the model-side eight-scenario requirement in fix mode duplicated harness coverage and pushed qa-functional-webhook-fix past its budget. qa-only still requires every scenario. * fix(deslop-shared-libs): probe the audited repository with -C <repo> A CI run probed safe-git from the session directory above the target repo, so the capability probe never touched the repository and the run fell back to the API without a local attempt. The probe (and any call from elsewhere) now names the audited repository. * test(qa-deadline): never attach a reader to the full-pipe fixture's stdout The full-pipe receipt test attached a 'data' listener (flowing mode) and then paused; on CI the reader could drain the 2 MB write before the pause, so the receipt write never blocked and the helper exited 0 in ~126 ms. The stdout pipe now stays unread until the assertion, which is what the test means to model. * feat(qa): helpers answer --help, and the QA eval interfaces declare it Approved by Garry: asking gstack-qa-evidence or gstack-qa-deadline for usage is read-only, so both helpers print usage and exit 0 on --help (the evidence usage now names the annotation shape), and the functional and caller command allowlists accept exactly 'bun <path>/bin/gstack-qa-{evidence,deadline} --help'. Two CI runs failed only on that call. * fix(qa): after an input change, a probe is affected unless shown otherwise CI late-input run finished in time but revalidated only the happy probe after the locale input changed and reported the stale adverse probe green. The revalidation step now treats any probe not shown to be unaffected as affected. * test(shared-libs): seed the lifecycle replay's first Step 3 pass instead of replaying it shared-libs-review-lifecycle ran ~88% of its 300 s session budget (12-run census median 265 s, 4/24 sessions timed out). The fixture now executes pass 1's Step 3 once with the real logger and Git: a real unused REVIEW_START, then the diff, inventories, attributes/config/index flags, gstack-review-read output and every file's bytes and sha256, saved to one observation. The model resumes at Step 4 with an exact four-file first read, the observation named as the authoritative pass-1 repository read, one post-fix verification, an explicit pass-2 read list and a twelve-line summary. Pass 2 still runs its own --start, diff, reads, fingerprint and stage actor before --finish. The actor scope now states that a current settled final-pass actor result supplies the replaced QA/adversarial prerequisites and that the no-credit disclosure is a reporting label: one r1 session persisted completed:false from that ambiguity. New assertions: the final binding never uses the seeded token's start or tree, and the observation was read; free controls finish the seeded token (binding changed) and omit the observation read, and both fail. * test(shared-libs): trim the resumed review replays' setup and report Every sibling review session (revalidation, path-eligibility, index-flags, prior-coverage) loaded qa/sections/exploratory.md and often scope.md although its QA and native adversarial results are supplied synthetic inputs, then spent a second request on shared-code-reuse.md and base metadata. The resumed scope now states that the supplied results replace Step 4's QA method loading; the revalidation contract names one first response (workflow, checklist, finding, prerequisites, shared-code-reuse.md, base metadata) and caps the summary at twelve lines. Receipt order, direct source reads, the checker, the question and final persistence are unchanged. * fix(review): define what a Step 5c Skip option says Step 5c named "B) Skip" without saying what its description may claim. Two CI captures (path-eligibility on131d43be, index-flags on4643cb85) offered a Skip whose description added effects beyond declining: "The extraction can be applied in a later editing review pass" and "replacing the invalidated prior Skip". Those read as change commitments, so the no-change actor refused both. Step 5c now says to describe Skip only as no code/index change with the Skip recorded; adjacent lines are compacted so the review parity caps hold unchanged. Both exact packets are kept as a free regression: still refused, and accepted once Skip follows the rule. The actor's classifier is unchanged. * fix(qa-evidence): every complete capture needs an evidence row; test(tpa): accept the hyphenated app-specific-password spelling - materialize refuses when a complete capture has no evidence row and is not named in limits (CI cli-report omitted capture 004), naming the missing IDs. - tpa-apple-ban's detector required 'app-specific password' with a space; the CI answer said 'app-specific-password path' and was otherwise correct. * test(qa-observer): fix mode treats atomic temps of authorized src/test writes as transient CI webhook-fix failed with 'Could not watch test/worker.regression-1.test.ts.tmp...': Claude Code's Write renamed its temp before the per-file watch was added. The functional eval now tells the observer its mode, and a temp whose target that mode may write is observed through its directory watch. Report-only mode and undeclared paths keep failing closed. * feat(qa-evidence): refuse evidence observed on an older input snapshot than the latest capture When native probe output declares a top-level input snapshot, materialize compares each evidence row with the latest capture's snapshot and refuses stale rows unless they are classified superseded, naming the captures to rerun. ship-exploratory-late-input kept reporting a pre-change adverse probe green after the input changed. * test(qa-functional): point the fixture at the helper's --help instead of its source A CI webhook-fix run spent three turns reading lib/qa-evidence.ts to learn the interface and timed out just before materialize (agreed with #3002's owner). * feat(qa-evidence): captures list the caller's declared-but-unrun required probes GSTACK_QA_REQUIRED_PROBES (a JSON array of native child commands) makes every capture print requiredRemaining; it never judges pass or fail. The functional eval passes the webhook list from QA_WEBHOOK_REQUIRED_SCENARIOS, which the verdict now reads too, so the nudge and the verdict share one source (agreed with #3002's owner). CI webhook-report kept stopping with scenarios unrun. * test(review-army): record N+1's pre-dispatch stages and scope the session to Step 4.5 review-army-perf-n-plus-one timed out in 7 of 13 CI runs on this branch (passing 245-280 s of 300). Each session spent ~95 s on setup (the full extracted SKILL, checklist, section greps, exploratory.md, diff-scope/stats/learnings, tooling checks), ran Step 4's core pass, a search-before-recommending WebSearch, and wrote a 10-16 KB report (~100 s after the Red Team returned). The fixture now stages only review/sections/review-army.md plus the performance and red-team checklists, and hands the session the recorded detect-scope, specialist-stats and learnings outputs and the diff. The caller passes --performance (every CI parent already treated the prompt as that force flag against the <50-line skip), declares the core pass, QA, adversarial review, web research, Fix-First and persistence out of scope, and caps the report at the selection line, the SPECIALIST REVIEW block and the Red Team result (30 lines). The Performance specialist and the conditional Red Team are still real foreground subagents, and the report still has to surface the N+1. New assertion: a foreground Performance specialist dispatch precedes the Red Team dispatch. Free controls omit the Performance dispatch or background it, and both fail; the budget lifecycle adapter supplies the current result shape. Touchfiles now include the .rb fixture the case reads. * test(review-army): share the recorded Step 4.5 staging with consensus and supply its Red Team review-army-consensus (periodic) timed out in 2 of 13 census sessions; passing runs took 213-297 s of 300. Like N+1 it spent ~30-50 s reading the whole extracted SKILL, checklist and every specialist file, sometimes dispatched an unrequested Maintainability specialist, then ran a Red Team (60-70 s) and a second merge before writing a 9-15 KB report. The N+1 staging and scope text move into stageReviewArmySession / reviewArmyScope / reviewArmyChecklists (the N+1 prompt renders byte-identical). Consensus now records its detect-scope, stats, learnings and diff, stages the Review Army section with the security and testing checklists, forces --security --testing, and caps the report like N+1. Its Red Team is outside the multi-specialist contract, so the fixture supplies a labeled synthetic NO FINDINGS result instead of a dispatch. The existing SQL-finding and browser-error assertions are unchanged; the lifecycle adapter's spawnSync now returns the git output the staging reads. * docs(changelog): v1.91.10.0 records the flake census and its repairs * test(strict-output): give the spool-prefix child time to finish before the pending stream times out windows-free-tests failed on9a7a7e54: the 150 ms shared deadline raced Bun startup on Windows, so the child was killed mid-write and the spool held a partial payload. Only the never-released extra stream should time out; the child now has 3 s. * fix(qa-evidence): accept a single limits string; test(qa-callers): read the handoff first when a probe snapshot changes CI late-input spent a turn rewriting limits as an array after materialize refused a string, and a ten-read sweep hunting for the changed input before it read reports/HANDOFF.md, then timed out at 300 s. * test(autoplan-dual-voice): unwrap the framed native report before Claude Code 2.1.284's agentId/usage trailer * test(section-loading): credit a Bash print that contains every line of the carved section * test(auto-decide): ask for the selected mode in the skill's mode handoff line, not a separate public decision * test(plan-ceo floor): scope preservation approves no premise, approach or remedy * test(autoplan-dual-voice): the fixture declares that delivered bash blocks run alone, diagnostics separately * test(coverage-audit): a fenced plain-word caption in a successful && read chain is display only Census 36776104571 plan-eng capture read both owned files with cat -n in one successful && chain; the caption 'echo "=== git diff main --stat ==="' fell outside the two-token caption grammar, so both reads lost credit. Accept a fenced caption of plain words; unfenced command strings, expansions, redirection, -e escapes and ; / || tails stay rejected. * test(office-hours): a fork whose outer options are the seeded shapes is the Phase 4 question Census trials 1-2 captured complete Phase 4 forks (A) Server-side B) Client-side C) Hybrid, recommendation with because) whose prose used none of the vocabulary words. Accept two seeded shapes as outer options as Phase 4 specificity; the earlier-phase, nested, fenced and single-shape controls still fail. * fix(review): design-lite rows keep the detector's [rule-id]; the e2e detector rows point at the diff The output template had no rule-id slot, so rows merged with checklist items dropped the detector id (census t2, local t1). Rows now carry [rule-id]. The fake engine's sample rows named a foreign fixture path at line 0; the e2e remaps them to landing.html/styles.css so trials stop spending turns reconciling it. * test(shared-libs): the plan actor reads scheduler parity and unchanged-scope lists Census 36776104571's question preserved the contract ('behaving exactly like the scheduler', 'scheduler parity holds by construction') and excluded work with 'Existing copies and helper hardening stay unchanged'. Accept exactly/parity as preservation (negated forms refuse) and a bare noun list that stays unchanged as an exclusion for the expansion scan only; verb-led clauses still refuse. * fix(qa-only,qa): name the exploratory read point and finalization order; judge qa with its browser assets qa-only judges cited 'next section' pointing at the wrong heading, an exploratory trigger that contradicted its read point, clock ownership in mixed runs and the unstated order of exploratory section 4 vs reporting. The qa judge penalized the absent qa-report-template and issue-taxonomy that qa-patterns loads; with them in, it found issue-taxonomy's dangling 'rule 13' (the consent rule is browser rule 3). * test(ship-docsync): seeded attempt 1 counts toward the limit; transport counts ignore calls that never reached the state file - CI launch-failure retried after the seeded attempt 1 as if that attempt were the fixture's; the seeded prompt now says attempt 1 is this invocation's and a further attempt needs what Blocked recovery requires. - A late-result run typo'd the state path once (ENOENT, the actor never ran), then repeated the call correctly; the per-action count compared both calls with one actor event. Only calls naming the real state file are counted. * fix(plan-eng-review): show the accepted dedicated read form for coverage-diagram sources CI plan-eng-coverage-audit mixed package/config and git diff into the source read; the review variant, whose prompt shows the && display form, does not. The plan trace step now shows it too, within the unchanged size cap. * test(sync-gbrain-readiness): a negation earlier in the claim clause is not a search/write readiness claim The census unknown actor wrote 'nothing about read, search, or write capability is confirmed either way' after a YELLOW/WARN verdict. The claim window started at 'write', so the leading 'nothing' was outside it. Check the clause subject for nothing/neither/none/no; keep the original in-claim negations. Replay of the captured output passes; positive controls still flag an unnegated claim. * fix(office-hours): a forcing question's recommendation takes the position the founder's words support auq-matrix office-hours asked D1 Demand as options about the founder's own evidence and, with no rule for that shape, recommended 'answer whichever is TRUE — A is marked recommended only because it is the strongest position' (substance 2). Say what such a recommendation is: the option the founder's own words support, why it matters for the next step, and what would change it. * fix(plan-ceo-review): name the mode preference command and the exact handoff line auto-decide-preserved at6fcb0981: the model never ran the preference check, read 'check ... through the preamble' as already done, auto-selected 'per your preference setting', and wrote 'Selected mode: HOLD SCOPE, auto-decided from your tuned preference' instead of the AUTO_DECIDE handoff line. At9a7a7e54it ran the check but wrote 'Decision: HOLD SCOPE is the review mode for ...'. Neither matched the handoff template the observer recognizes. Name gstack-question-preference --check at the point of use and say the handoff begins with the exact matching line. Collapse the audit block's comment padding to stay within the unchanged 80150-byte skeleton cap. * test(section-loading): record the CEO capture's report and transcript The6fcb0981census failed hasStaleFillRaceFinding (line 98), but the case records nothing beyond junit, so the report the detector judged is gone. Return the SkillTestResult from captureSectionReads and record it, with the full saved report, through the eval collector on pass and fail. * test(design): plan-mode names its read list and caps its additions and summary At6fcb0981plan-design-review-plan-mode timed out at 300 s (9 turns): 22 cat/sed chunk reads (~50 s), then a 28 KB plan Write (~150 s), before the read-back finished. The9a7a7e54pass took 240 s with a 24.6 KB Write. Read SKILL.md, review-sections.md and plan.md natively in one response, keep additions under 14,000 characters and the summary within ten lines. Budgets unchanged. * test(plan-mode-no-op): require prose evidence before a waiting verdict ends eng/design runs (carried byte-identical from #3002) With the prose fallback forced, the gate renders as a lettered menu; a judge 'waiting' verdict on a spinner-only frame ended the run as 'asked' before the menu rendered, so the scope-gate check failed on unchanged behavior. * feat(qa-evidence): materialize computes the phase verdict; callers must report it Approved by Garry: the helper, not the model, decides whether evidence can pass. materialize writes verdict {status, open} into evidence.json and prints it: fail or blocked from row classifications, inconclusive while any row is superseded, a complete capture is withheld, a declared required probe is unrun or there is no evidence, else pass. The caller fixture requires receipt.status to equal that verdict. CI late-input kept reporting pass with a superseded happy probe. * test(qa-callers): compare the receipt with the helper verdict only when evidence.json was materialized The producer free tests run captures without materialize; evidence.json is optional for callers, so its absence is not a verdict mismatch. * test(llm-judge): run the ship workflow judge at medium effort so its panel fits JUDGE_MS claude-fable-5-1 accepts only adaptive thinking (thinking.type.enabled with budget_tokens returns 400), so effort is the available thinking control. Measured on the exact ship judge request (105,301 input tokens): - default effort, 18 samples: thinking 5,086-10,881 tokens, 75.9-144.7 s; 3 of 18 passed the 120 s deadline (about 42% of 3-sample panels). - medium effort, 18 samples: thinking 2,749-5,762, output at most 6,144 tokens, 43.1-77.9 s; scores 4/4/4 in 16 of 18 (clarity 3 in two), versus 14 of 18 at default. callJudge gains an effort option sent as output_config.effort; only the ship judge sets it. Rubric, floors, panel size, deadline, model and max_tokens are unchanged. The cache identity records effort. * test(llm-judge): ask frontier workflow judges for 120-word reasoning under the unchanged 150-word check Told "under 150 words", the ship judge's reasoning landed at 130-156 words (3 of 18 probe samples at 152-156), so the structured-response check failed about one panel in three independent of effort. The prompt's frontier block and the response schema description now say under 120 words; the validator still rejects 150 words or more. The changed prompt bytes reach only the two frontier judges: ship/SKILL.md workflow (prompt and schema) and review/SKILL.md workflow (prompt). * test(llm-judge): type the stream transport mock call * test(plan-ceo floor): the request answers only the questions it names PR lane 36794871032 (head20d6e98f): the CEO floor ran 608 s without a question. Its Step 0 recorded the premise gap and approach choice as unresolved ledger rows, then said "this session supplies all answers up front, so no decision brief was dispatched" and wrote Sections 1-11.2734e203stopped scope preservation from approving the premise; this time the actor block (declined setup, recall, outside voices, HOLD SCOPE) and the fixture's "complete user request is available from the start" were read as pre-answering every review question. The CEO actor now states that the request answers only the routing, recall, outside-reviewer and review-mode questions it names. * test(plan-devex floor): a 'Partly wrong' 0B answer is the narrative confirmation PR lane 36794871032: the DX floor asked its D1 narrative confirmation (Accurate, proceed / Partly wrong, correct it / Way off, actual is...). The deterministic setup rule accepted only 'Some ... wrong', so the question went to the LLM assessor, which hit its 30 s spawnSync cap (ETIMEDOUT) and ended the case as assessment_error at 141 s, the same failure as census 36641820398. The rule now accepts 'partly' beside 'some'; the captured question is a free regression and the remedy-option controls still go to the assessor. * test(design-review plugin handoff): quoted report text is not an install command PR lane 36794871032: every behavioral check passed except noInstallOrOverride, which matched "no `npx impeccable`" inside the quoted heredoc that wrote detector-output.md. Nothing was installed or downloaded. The check now drops quoted-delimiter heredoc bodies (literal data) before matching; unquoted bodies, which can expand $(...), and unterminated bodies stay checked. Free controls cover the captured write, bare npx, an IMPECCABLE_BIN override, an unquoted $(npx ...), npx after the delimiter and an unterminated body. * test(review-army delivery audit): stage only the plan-completion section and record its git reads PR lane 36794871032: the case timed out at its 120 s budget after 7 turns (previous lane passed in 45 s). The session read the 46 KB extracted SKILL in three passes (cat to persisted output, grep, sed), ran its own git reads, wrote a 74-line report, then inspected and ran gstack-learnings-log and rewrote the report's Learnings section. As in the Step 4.5 cases (17ee2e54/2bd4651c), the fixture now stages only review/sections/plan-completion.md, hands the session the recorded git log and diff, declares the HIGH-impact question, its Scope Check, learnings logging and later steps outside the capture, and caps the report at the audit block and its DISCREPANCY entries (30 lines). The NOT DONE and email assertions are unchanged. * feat(qa-evidence): one capture call records the causal note for the previous capture capture R NNN [--public] (--deadline D|--timeout-ms MS) --after PREV --hypothesis 'TEXT' -- CMD publishes exploration-NNN.json {observationCapture, observationArgv, observed, hypothesis, nextCapture, nextArgv} before running CMD, refusing unless PREV is the latest complete capture. The receipt carries checkpoint/checkpointSha256; validators bind the note to the transcript's capture calls by capture ID and receipt hash instead of exact command strings. The separate checkpoint command and the capture guard keep working; materialize learning accepts both note shapes and still rejects same-probe replays. Prose and eval fixture prompts teach the merged form. * fix(qa-evidence): a superseded row stops holding the verdict open once its probe is rerun on current inputs materialize requires an old-snapshot row to be classified superseded, and its verdict kept every superseded row open, so rerunning the probe (what its own error tells the model to do) could never reach pass; late-input reran 3 and 9 on the new snapshot and still got inconclusive. A superseded row now closes only when a non-superseded row with the same captured argv observed the current snapshot. Re-materializing an already-published evidence.json names the cause instead of failing generically. * test(plan-eng batching): count saved decisions whose label drops the (recommended) marker or whose report is titled 'Eng Review Report — <plan>' * fix(qa): browser-only runs skip annotations/materialize; only Q captures can anchor evidence rows * test(design): plan-mode length is a drafting target, not a check to measure and trim * test(llm-judge): structured output for doc, outcome and posture judges so reasoning quotes cannot break JSON * test(ship-docsync): steer skill file reads to Read; large cat output becomes an unpageable preview * docs(changelog): browser-only QA evidence and structured judge output * test(qa-only cleanup): refusal scenarios get a 1 s budget and an absolute worker deadline; 300 ms starved under parallel load * fix(office-hours, design-consultation): ask the goal question and read the mode section first; ask the memorable-thing question on its own * test(outside-disabled): a record named by the retained record's own clock and then disowned owns its completed status * test(context-skills): install gstack-paths in the fixture bin; without it the model guessed the checkpoint root * test(ceo mode routing): SCOPE EXPANSION posture credits plural 'expansions' * test(ship-docsync): name the unmet atomic-replacement check on a forbidden temp-file write * fix(qa): browser-only runs materialize an empty evidence list with checkpoints in limits, matching /qa-only * test(qa callers): an accepted review-log record may cite checkpoints as finding evidence * fix(plan-eng-review): state that a disallowed question tool never qualifies as headless before the headless action * merge follow-up: re-record paid CLI parity for #2999's flags; trim merged review, qa-only and plan-eng wording toward the size caps * test(golden): refresh codex/factory ship goldens for the trimmed caller QA wording * test(coverage-audit fixture): disable git auto maintenance so cleanup is not racing a detached git writer * test(parity): raise review, qa and plan-eng caps to the measured merged size of #2999 and #3002 (each fit alone), documented per cap * fix(qa-evidence): materialize rejects an unrecognized classification before publishing, so the one-shot verdict cannot be locked inconclusive by a descriptive label
This commit is contained in:
1 parent
df89475b17
commit
7fca42ad8b
340 files changed
+34342
-6789
No files matched your search
+2
-2
@@ -296,7 +296,7 @@ export function serveDir(root: string, nonce: string = randomBytes(16).toString(
|
||||
// ─── Async spawn (keeps the loopback server's event loop free) ────────────────
|
||||
|
||||
async function runProc(cmd: string, args: string[], timeoutMs: number): Promise<{ code: number | null; stdout: string; stderr: string; error?: string }> {
|
||||
let child: ReturnType<typeof Bun.spawn>;
|
||||
let child: Bun.Subprocess<'ignore', 'pipe', 'pipe'>;
|
||||
try {
|
||||
child = Bun.spawn([cmd, ...args], { stdout: 'pipe', stderr: 'pipe', stdin: 'ignore' });
|
||||
} catch (e) {
|
||||
@@ -608,7 +608,7 @@ export const NO_BROWSER_HELP = "open the Aside app (macOS 15+, aside.com), or ru
|
||||
export type EngineChoice =
|
||||
| { engine: 'aside'; version: string }
|
||||
| { engine: 'browse'; bin: string }
|
||||
| { engine: null; probe: AsideProbe; error: string };
|
||||
| { engine: null; probe: Extract<AsideProbe, { ok: false }>; error: string };
|
||||
|
||||
let chosen: EngineChoice | undefined;
|
||||
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"printWidth": 110,
|
||||
"singleQuote": true,
|
||||
"trailingComma": "all",
|
||||
"semi": true
|
||||
}
|
||||
+421
-113
@@ -6,160 +6,468 @@ import { CsoError, sha256 } from './contracts';
|
||||
import { discardAtomicNoReplaceTemp, recoverAtomicNoReplaceJson, secureDirectory } from './state';
|
||||
import { atomicWriteSync } from '../fs-atomic';
|
||||
|
||||
export const GROUP_LIMITS = { cpu: 2, memoryMiB: 4096, pids: 256, writableMiB: 2048, outputBytes: 1024 * 1024 } as const;
|
||||
export const GROUP_LIMITS = {
|
||||
cpu: 2,
|
||||
memoryMiB: 4096,
|
||||
pids: 256,
|
||||
writableMiB: 2048,
|
||||
outputBytes: 1024 * 1024,
|
||||
} as const;
|
||||
export const ROLE_LIMITS = {
|
||||
anchor: {cpu:.05,memoryMiB:64,pids:8,writableMiB:16},
|
||||
app: {cpu:.85,memoryMiB:2304,pids:96,writableMiB:1264},
|
||||
verifier: {cpu:.55,memoryMiB:512,pids:32,writableMiB:256},
|
||||
tests: {cpu:.55,memoryMiB:1280,pids:64,writableMiB:1024},
|
||||
postgres: {cpu:.25,memoryMiB:1024,pids:96,writableMiB:512},
|
||||
browser: {cpu:.30,memoryMiB:512,pids:16,writableMiB:256},
|
||||
anchor: { cpu: 0.05, memoryMiB: 64, pids: 8, writableMiB: 16 },
|
||||
app: { cpu: 0.85, memoryMiB: 2304, pids: 96, writableMiB: 1264 },
|
||||
verifier: { cpu: 0.55, memoryMiB: 512, pids: 32, writableMiB: 256 },
|
||||
tests: { cpu: 0.55, memoryMiB: 1280, pids: 64, writableMiB: 1024 },
|
||||
postgres: { cpu: 0.25, memoryMiB: 1024, pids: 96, writableMiB: 512 },
|
||||
browser: { cpu: 0.3, memoryMiB: 512, pids: 16, writableMiB: 256 },
|
||||
} as const;
|
||||
export type Role = keyof typeof ROLE_LIMITS;
|
||||
export interface Lease { endpoint: string; slot: number; path: string; runId: string; ownerPid: number; expiresAt: number; token:string; supervised:boolean }
|
||||
function alive(pid: number): boolean { try { process.kill(pid,0); return true; } catch { return false; } }
|
||||
function processIdentity(pid:number):string|undefined{if(process.platform!=='linux')return;try{const raw=fs.readFileSync(`/proc/${pid}/stat`,'utf8'),tail=raw.slice(raw.lastIndexOf(')')+2).trim().split(/\s+/);return /^\d+$/.test(tail[19]??'')?`linux:${tail[19]}`:undefined;}catch{return;}}
|
||||
function sameDirectory(left:fs.Stats,right:fs.Stats):boolean{return left.dev===right.dev&&left.ino===right.ino&&left.uid===right.uid&&left.mode===right.mode;}
|
||||
function sameFile(left:fs.Stats,right:fs.Stats):boolean{return left.dev===right.dev&&left.ino===right.ino&&left.uid===right.uid&&left.mode===right.mode&&left.nlink===right.nlink;}
|
||||
function privateDirectory(path:string,label:string):fs.Stats{const stat=fs.lstatSync(path);if(!stat.isDirectory()||stat.isSymbolicLink()||(process.getuid&&stat.uid!==process.getuid())||(stat.mode&0o077)!==0)throw new CsoError('UNSAFE_PATH',`${label} is not a private owned directory`);return stat;}
|
||||
function privateFile(path:string,label:string):fs.Stats{const stat=fs.lstatSync(path);if(!stat.isFile()||stat.isSymbolicLink()||stat.nlink!==1||(process.getuid&&stat.uid!==process.getuid())||(stat.mode&0o077)!==0||stat.size>1024*1024)throw new CsoError('UNSAFE_PATH',`${label} is not a private regular file`);return stat;}
|
||||
type Claim={path:string;token:string;identity:fs.Stats;pid:number;processIdentity:string|null};
|
||||
type ClaimOwner={pid:number;processIdentity:string|null;token:string;createdAt:number};
|
||||
function validateClaimOwner(value:unknown,expectedToken?:string,publisherPid?:number):ClaimOwner{
|
||||
if(!value||typeof value!=='object'||Array.isArray(value))throw new CsoError('INCOMPATIBLE_INPUT','Reproduction recovery owner is invalid');
|
||||
const owner=value as Record<string,unknown>;
|
||||
if(Object.keys(owner).sort().join(',')!=='createdAt,pid,processIdentity,token'||!Number.isSafeInteger(owner.pid)||Number(owner.pid)<=1||
|
||||
typeof owner.token!=='string'||!/^[a-f0-9]{32}$/.test(owner.token)||(expectedToken!==undefined&&owner.token!==expectedToken)||
|
||||
!Number.isFinite(owner.createdAt)||Number(owner.createdAt)<0||!(owner.processIdentity===null||(typeof owner.processIdentity==='string'&&/^linux:\d+$/.test(owner.processIdentity)))||
|
||||
(publisherPid!==undefined&&Number(owner.pid)!==publisherPid))throw new CsoError('INCOMPATIBLE_INPUT','Reproduction recovery owner is invalid');
|
||||
return{pid:Number(owner.pid),processIdentity:owner.processIdentity as string|null,token:owner.token,createdAt:Number(owner.createdAt)};
|
||||
export interface Lease {
|
||||
endpoint: string;
|
||||
slot: number;
|
||||
path: string;
|
||||
runId: string;
|
||||
ownerPid: number;
|
||||
expiresAt: number;
|
||||
token: string;
|
||||
supervised: boolean;
|
||||
}
|
||||
function inspectClaim(path:string,expectedToken?:string):Claim{
|
||||
const before=privateFile(path,'Reproduction recovery claim');if(before.size<=0||before.size>4096)throw new CsoError('UNSAFE_PATH','Reproduction recovery claim has an invalid size');
|
||||
let owner:any;try{owner=JSON.parse(fs.readFileSync(path,'utf8'));}catch{throw new CsoError('INCOMPATIBLE_INPUT','Reproduction recovery owner is invalid');}
|
||||
const after=privateFile(path,'Reproduction recovery claim');if(!sameFile(before,after))throw new CsoError('INCOMPATIBLE_INPUT','Reproduction recovery owner is invalid');
|
||||
owner=validateClaimOwner(owner,expectedToken);
|
||||
return{path,token:owner.token,identity:after,pid:owner.pid,processIdentity:owner.processIdentity};
|
||||
function alive(pid: number): boolean {
|
||||
try {
|
||||
process.kill(pid, 0);
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
function releaseClaim(claim:Claim):void{
|
||||
const current=inspectClaim(claim.path,claim.token);if(!sameFile(current.identity,claim.identity))throw new CsoError('PERSISTENCE_FAILED','Reproduction recovery ownership changed');
|
||||
const final=privateFile(claim.path,'Reproduction recovery claim');if(!sameFile(final,claim.identity))throw new CsoError('PERSISTENCE_FAILED','Reproduction recovery ownership changed');
|
||||
function processIdentity(pid: number): string | undefined {
|
||||
if (process.platform !== 'linux') return;
|
||||
try {
|
||||
const raw = fs.readFileSync(`/proc/${pid}/stat`, 'utf8'),
|
||||
tail = raw
|
||||
.slice(raw.lastIndexOf(')') + 2)
|
||||
.trim()
|
||||
.split(/\s+/);
|
||||
return /^\d+$/.test(tail[19] ?? '') ? `linux:${tail[19]}` : undefined;
|
||||
} catch {
|
||||
return;
|
||||
}
|
||||
}
|
||||
function sameDirectory(left: fs.Stats, right: fs.Stats): boolean {
|
||||
return (
|
||||
left.dev === right.dev && left.ino === right.ino && left.uid === right.uid && left.mode === right.mode
|
||||
);
|
||||
}
|
||||
function sameFile(left: fs.Stats, right: fs.Stats): boolean {
|
||||
return (
|
||||
left.dev === right.dev &&
|
||||
left.ino === right.ino &&
|
||||
left.uid === right.uid &&
|
||||
left.mode === right.mode &&
|
||||
left.nlink === right.nlink
|
||||
);
|
||||
}
|
||||
function privateDirectory(path: string, label: string): fs.Stats {
|
||||
const stat = fs.lstatSync(path);
|
||||
if (
|
||||
!stat.isDirectory() ||
|
||||
stat.isSymbolicLink() ||
|
||||
(process.getuid && stat.uid !== process.getuid()) ||
|
||||
(stat.mode & 0o077) !== 0
|
||||
)
|
||||
throw new CsoError('UNSAFE_PATH', `${label} is not a private owned directory`);
|
||||
return stat;
|
||||
}
|
||||
function privateFile(path: string, label: string): fs.Stats {
|
||||
const stat = fs.lstatSync(path);
|
||||
if (
|
||||
!stat.isFile() ||
|
||||
stat.isSymbolicLink() ||
|
||||
stat.nlink !== 1 ||
|
||||
(process.getuid && stat.uid !== process.getuid()) ||
|
||||
(stat.mode & 0o077) !== 0 ||
|
||||
stat.size > 1024 * 1024
|
||||
)
|
||||
throw new CsoError('UNSAFE_PATH', `${label} is not a private regular file`);
|
||||
return stat;
|
||||
}
|
||||
type Claim = { path: string; token: string; identity: fs.Stats; pid: number; processIdentity: string | null };
|
||||
type ClaimOwner = { pid: number; processIdentity: string | null; token: string; createdAt: number };
|
||||
function validateClaimOwner(value: unknown, expectedToken?: string, publisherPid?: number): ClaimOwner {
|
||||
if (!value || typeof value !== 'object' || Array.isArray(value))
|
||||
throw new CsoError('INCOMPATIBLE_INPUT', 'Reproduction recovery owner is invalid');
|
||||
const owner = value as Record<string, unknown>;
|
||||
if (
|
||||
Object.keys(owner).sort().join(',') !== 'createdAt,pid,processIdentity,token' ||
|
||||
!Number.isSafeInteger(owner.pid) ||
|
||||
Number(owner.pid) <= 1 ||
|
||||
typeof owner.token !== 'string' ||
|
||||
!/^[a-f0-9]{32}$/.test(owner.token) ||
|
||||
(expectedToken !== undefined && owner.token !== expectedToken) ||
|
||||
!Number.isFinite(owner.createdAt) ||
|
||||
Number(owner.createdAt) < 0 ||
|
||||
!(
|
||||
owner.processIdentity === null ||
|
||||
(typeof owner.processIdentity === 'string' && /^linux:\d+$/.test(owner.processIdentity))
|
||||
) ||
|
||||
(publisherPid !== undefined && Number(owner.pid) !== publisherPid)
|
||||
)
|
||||
throw new CsoError('INCOMPATIBLE_INPUT', 'Reproduction recovery owner is invalid');
|
||||
return {
|
||||
pid: Number(owner.pid),
|
||||
processIdentity: owner.processIdentity as string | null,
|
||||
token: owner.token,
|
||||
createdAt: Number(owner.createdAt),
|
||||
};
|
||||
}
|
||||
function inspectClaim(path: string, expectedToken?: string): Claim {
|
||||
const before = privateFile(path, 'Reproduction recovery claim');
|
||||
if (before.size <= 0 || before.size > 4096)
|
||||
throw new CsoError('UNSAFE_PATH', 'Reproduction recovery claim has an invalid size');
|
||||
let owner: any;
|
||||
try {
|
||||
owner = JSON.parse(fs.readFileSync(path, 'utf8'));
|
||||
} catch {
|
||||
throw new CsoError('INCOMPATIBLE_INPUT', 'Reproduction recovery owner is invalid');
|
||||
}
|
||||
const after = privateFile(path, 'Reproduction recovery claim');
|
||||
if (!sameFile(before, after))
|
||||
throw new CsoError('INCOMPATIBLE_INPUT', 'Reproduction recovery owner is invalid');
|
||||
owner = validateClaimOwner(owner, expectedToken);
|
||||
return {
|
||||
path,
|
||||
token: owner.token,
|
||||
identity: after,
|
||||
pid: owner.pid,
|
||||
processIdentity: owner.processIdentity,
|
||||
};
|
||||
}
|
||||
function releaseClaim(claim: Claim): void {
|
||||
const current = inspectClaim(claim.path, claim.token);
|
||||
if (!sameFile(current.identity, claim.identity))
|
||||
throw new CsoError('PERSISTENCE_FAILED', 'Reproduction recovery ownership changed');
|
||||
const final = privateFile(claim.path, 'Reproduction recovery claim');
|
||||
if (!sameFile(final, claim.identity))
|
||||
throw new CsoError('PERSISTENCE_FAILED', 'Reproduction recovery ownership changed');
|
||||
fs.unlinkSync(claim.path);
|
||||
}
|
||||
function acquireClaim(parent:string,expected:fs.Stats):Claim{
|
||||
const path=join(parent,'.recovery'),assertParent=()=>{const current=privateDirectory(parent,'Reproduction lease slot');if(!sameDirectory(expected,current))throw new CsoError('INSUFFICIENT_CAPACITY','Reproduction lease changed during recovery');};
|
||||
const recoverPublications=()=>{
|
||||
const pattern=/^\.recovery\.tmp\.(\d{1,10})\.[a-f0-9]{8}$/;
|
||||
for(const name of fs.readdirSync(parent)){
|
||||
const match=name.match(pattern);if(!match)continue;
|
||||
const publisherPid=Number(match[1]),temporary=join(parent,name),options={label:'Reproduction recovery claim',maxBytes:4096,
|
||||
validate:(value:unknown,pid:number)=>{validateClaimOwner(value,undefined,pid);}};
|
||||
assertParent();if(fs.existsSync(path))recoverAtomicNoReplaceJson(path,options);if(fs.existsSync(temporary))discardAtomicNoReplaceTemp(temporary,publisherPid,options);assertParent();
|
||||
function acquireClaim(parent: string, expected: fs.Stats): Claim {
|
||||
const path = join(parent, '.recovery'),
|
||||
assertParent = () => {
|
||||
const current = privateDirectory(parent, 'Reproduction lease slot');
|
||||
if (!sameDirectory(expected, current))
|
||||
throw new CsoError('INSUFFICIENT_CAPACITY', 'Reproduction lease changed during recovery');
|
||||
};
|
||||
const recoverPublications = () => {
|
||||
const pattern = /^\.recovery\.tmp\.(\d{1,10})\.[a-f0-9]{8}$/;
|
||||
for (const name of fs.readdirSync(parent)) {
|
||||
const match = name.match(pattern);
|
||||
if (!match) continue;
|
||||
const publisherPid = Number(match[1]),
|
||||
temporary = join(parent, name),
|
||||
options = {
|
||||
label: 'Reproduction recovery claim',
|
||||
maxBytes: 4096,
|
||||
validate: (value: unknown, pid: number) => {
|
||||
validateClaimOwner(value, undefined, pid);
|
||||
},
|
||||
};
|
||||
assertParent();
|
||||
if (fs.existsSync(path)) recoverAtomicNoReplaceJson(path, options);
|
||||
if (fs.existsSync(temporary)) discardAtomicNoReplaceTemp(temporary, publisherPid, options);
|
||||
assertParent();
|
||||
}
|
||||
};
|
||||
for(let attempt=0;attempt<64;attempt++){
|
||||
assertParent();recoverPublications();const token=randomBytes(16).toString('hex');
|
||||
try{
|
||||
atomicWriteSync(path,JSON.stringify({pid:process.pid,processIdentity:processIdentity(process.pid)??null,token,createdAt:Date.now()})+'\n',{mode:0o600,noReplace:true});
|
||||
const claim=inspectClaim(path,token);try{assertParent();}catch(error){try{releaseClaim(claim);}catch{}throw error;}return claim;
|
||||
}catch(error:any){
|
||||
if(error instanceof CsoError)throw error;
|
||||
if(error?.code!=='EEXIST')throw new CsoError('PERSISTENCE_FAILED','Reproduction recovery claim could not be created');
|
||||
for (let attempt = 0; attempt < 64; attempt++) {
|
||||
assertParent();
|
||||
recoverPublications();
|
||||
const token = randomBytes(16).toString('hex');
|
||||
try {
|
||||
atomicWriteSync(
|
||||
path,
|
||||
JSON.stringify({
|
||||
pid: process.pid,
|
||||
processIdentity: processIdentity(process.pid) ?? null,
|
||||
token,
|
||||
createdAt: Date.now(),
|
||||
}) + '\n',
|
||||
{ mode: 0o600, noReplace: true },
|
||||
);
|
||||
const claim = inspectClaim(path, token);
|
||||
try {
|
||||
assertParent();
|
||||
} catch (error) {
|
||||
try {
|
||||
releaseClaim(claim);
|
||||
} catch {}
|
||||
throw error;
|
||||
}
|
||||
return claim;
|
||||
} catch (error: any) {
|
||||
if (error instanceof CsoError) throw error;
|
||||
if (error?.code !== 'EEXIST')
|
||||
throw new CsoError('PERSISTENCE_FAILED', 'Reproduction recovery claim could not be created');
|
||||
}
|
||||
assertParent();
|
||||
const observed = inspectClaim(path),
|
||||
isAlive = alive(observed.pid),
|
||||
identity = isAlive ? processIdentity(observed.pid) : undefined;
|
||||
if (
|
||||
isAlive &&
|
||||
!(
|
||||
typeof observed.processIdentity === 'string' &&
|
||||
identity !== undefined &&
|
||||
identity !== observed.processIdentity
|
||||
)
|
||||
)
|
||||
throw new CsoError('INSUFFICIENT_CAPACITY', 'Another helper is recovering the reproduction lease');
|
||||
try {
|
||||
releaseClaim(observed);
|
||||
} catch (error) {
|
||||
if (error instanceof CsoError && error.code === 'PERSISTENCE_FAILED') continue;
|
||||
throw error;
|
||||
}
|
||||
assertParent();const observed=inspectClaim(path),isAlive=alive(observed.pid),identity=isAlive?processIdentity(observed.pid):undefined;
|
||||
if(isAlive&&!(typeof observed.processIdentity==='string'&&identity!==undefined&&identity!==observed.processIdentity))throw new CsoError('INSUFFICIENT_CAPACITY','Another helper is recovering the reproduction lease');
|
||||
try{releaseClaim(observed);}catch(error){if(error instanceof CsoError&&error.code==='PERSISTENCE_FAILED')continue;throw error;}
|
||||
}
|
||||
throw new CsoError('INSUFFICIENT_CAPACITY','Reproduction recovery claim changed repeatedly');
|
||||
throw new CsoError('INSUFFICIENT_CAPACITY', 'Reproduction recovery claim changed repeatedly');
|
||||
}
|
||||
/** One host-user pool shared by every workspace/state root on this machine. */
|
||||
export function machinePoolRoot():string{
|
||||
const uid=process.getuid?.()??userInfo().uid;
|
||||
return secureDirectory(join(fs.realpathSync(tmpdir()),`gstack-cso-pool-${uid}`));
|
||||
export function machinePoolRoot(): string {
|
||||
const uid = process.getuid?.() ?? userInfo().uid;
|
||||
return secureDirectory(join(fs.realpathSync(tmpdir()), `gstack-cso-pool-${uid}`));
|
||||
}
|
||||
function reclaimSlot(path:string,pool:string,slot:number,observed:fs.Stats,expectedToken?:string):boolean{
|
||||
let claim:Claim;try{claim=acquireClaim(path,observed);}catch(error){if(error instanceof CsoError&&error.code==='INSUFFICIENT_CAPACITY')return false;throw error;}
|
||||
try{const current=privateDirectory(path,'Reproduction lease slot');if(!sameDirectory(observed,current)){releaseClaim(claim);return false;}if(expectedToken){privateFile(join(path,'lease.json'),'Reproduction lease');const lease=JSON.parse(fs.readFileSync(join(path,'lease.json'),'utf8'));if(lease.token!==expectedToken){releaseClaim(claim);return false;}}
|
||||
const tomb=join(pool,`.slot-${slot}.stale-${process.pid}-${randomBytes(8).toString('hex')}`);fs.renameSync(path,tomb);const moved=privateDirectory(tomb,'Reproduction lease tomb');if(!sameDirectory(observed,moved))throw new CsoError('SNAPSHOT_RACE','Reproduction lease changed while quarantined');fs.mkdirSync(path,{mode:0o700});releaseClaim({...claim,path:join(tomb,'.recovery')});for(const name of fs.readdirSync(tomb)){if(!['lease.json','lease.token'].includes(name)&&!/^lease\.json\.tmp\.\d+\.[a-f0-9]{8}$/.test(name)&&!/^\.recovery\.tmp\.\d+\.[a-f0-9]{8}$/.test(name))throw new CsoError('UNSAFE_PATH','Stale reproduction lease contains an unexpected object');privateFile(join(tomb,name),'Stale reproduction lease file');fs.unlinkSync(join(tomb,name));}fs.rmdirSync(tomb);return true;
|
||||
}catch(error){if(error instanceof CsoError)throw error;return false;}
|
||||
function reclaimSlot(
|
||||
path: string,
|
||||
pool: string,
|
||||
slot: number,
|
||||
observed: fs.Stats,
|
||||
expectedToken?: string,
|
||||
): boolean {
|
||||
let claim: Claim;
|
||||
try {
|
||||
claim = acquireClaim(path, observed);
|
||||
} catch (error) {
|
||||
if (error instanceof CsoError && error.code === 'INSUFFICIENT_CAPACITY') return false;
|
||||
throw error;
|
||||
}
|
||||
try {
|
||||
const current = privateDirectory(path, 'Reproduction lease slot');
|
||||
if (!sameDirectory(observed, current)) {
|
||||
releaseClaim(claim);
|
||||
return false;
|
||||
}
|
||||
if (expectedToken) {
|
||||
privateFile(join(path, 'lease.json'), 'Reproduction lease');
|
||||
const lease = JSON.parse(fs.readFileSync(join(path, 'lease.json'), 'utf8'));
|
||||
if (lease.token !== expectedToken) {
|
||||
releaseClaim(claim);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
const tomb = join(pool, `.slot-${slot}.stale-${process.pid}-${randomBytes(8).toString('hex')}`);
|
||||
fs.renameSync(path, tomb);
|
||||
const moved = privateDirectory(tomb, 'Reproduction lease tomb');
|
||||
if (!sameDirectory(observed, moved))
|
||||
throw new CsoError('SNAPSHOT_RACE', 'Reproduction lease changed while quarantined');
|
||||
fs.mkdirSync(path, { mode: 0o700 });
|
||||
releaseClaim({ ...claim, path: join(tomb, '.recovery') });
|
||||
for (const name of fs.readdirSync(tomb)) {
|
||||
if (
|
||||
!['lease.json', 'lease.token'].includes(name) &&
|
||||
!/^lease\.json\.tmp\.\d+\.[a-f0-9]{8}$/.test(name) &&
|
||||
!/^\.recovery\.tmp\.\d+\.[a-f0-9]{8}$/.test(name)
|
||||
)
|
||||
throw new CsoError('UNSAFE_PATH', 'Stale reproduction lease contains an unexpected object');
|
||||
privateFile(join(tomb, name), 'Stale reproduction lease file');
|
||||
fs.unlinkSync(join(tomb, name));
|
||||
}
|
||||
fs.rmdirSync(tomb);
|
||||
return true;
|
||||
} catch (error) {
|
||||
if (error instanceof CsoError) throw error;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
function slotControl(pool:string,slot:number):{path:string;stat:fs.Stats}{
|
||||
const path=join(pool,`.slot-${slot}.control`);
|
||||
try{fs.mkdirSync(path,{mode:0o700});}catch(error:any){if(error?.code!=='EEXIST')throw new CsoError('PERSISTENCE_FAILED','Reproduction slot control directory could not be created');}
|
||||
const stat=privateDirectory(path,'Reproduction slot control directory');for(const name of fs.readdirSync(path))if(name!=='.recovery'&&!/^\.recovery\.tmp\.\d+\.[a-f0-9]{8}$/.test(name))throw new CsoError('UNSAFE_PATH','Reproduction slot control directory contains an unexpected object');return{path,stat};
|
||||
function slotControl(pool: string, slot: number): { path: string; stat: fs.Stats } {
|
||||
const path = join(pool, `.slot-${slot}.control`);
|
||||
try {
|
||||
fs.mkdirSync(path, { mode: 0o700 });
|
||||
} catch (error: any) {
|
||||
if (error?.code !== 'EEXIST')
|
||||
throw new CsoError('PERSISTENCE_FAILED', 'Reproduction slot control directory could not be created');
|
||||
}
|
||||
const stat = privateDirectory(path, 'Reproduction slot control directory');
|
||||
for (const name of fs.readdirSync(path))
|
||||
if (name !== '.recovery' && !/^\.recovery\.tmp\.\d+\.[a-f0-9]{8}$/.test(name))
|
||||
throw new CsoError('UNSAFE_PATH', 'Reproduction slot control directory contains an unexpected object');
|
||||
return { path, stat };
|
||||
}
|
||||
function writeLease(lease:Lease):void{
|
||||
const keys=Object.keys(lease).sort().join(','),expected='endpoint,expiresAt,ownerPid,path,runId,slot,supervised,token';
|
||||
if(keys!==expected||!/^unix:\/\/[/.A-Za-z0-9_-]+$/.test(lease.endpoint)||![0,1].includes(lease.slot)||
|
||||
!/^[A-Za-z0-9_.-]{1,100}$/.test(lease.runId)||lease.ownerPid!==process.pid||!Number.isSafeInteger(lease.expiresAt)||
|
||||
!/^[a-f0-9]{32}$/.test(lease.token)||typeof lease.supervised!=='boolean')
|
||||
throw new CsoError('PERSISTENCE_FAILED','Reproduction lease metadata is invalid');
|
||||
const expectedPath=join(machinePoolRoot(),sha256(lease.endpoint).slice(0,24),`slot-${lease.slot}`);
|
||||
if(lease.path!==expectedPath)throw new CsoError('PERSISTENCE_FAILED','Reproduction lease path is invalid');
|
||||
function writeLease(lease: Lease): void {
|
||||
const keys = Object.keys(lease).sort().join(','),
|
||||
expected = 'endpoint,expiresAt,ownerPid,path,runId,slot,supervised,token';
|
||||
if (
|
||||
keys !== expected ||
|
||||
!/^unix:\/\/[/.A-Za-z0-9_-]+$/.test(lease.endpoint) ||
|
||||
![0, 1].includes(lease.slot) ||
|
||||
!/^[A-Za-z0-9_.-]{1,100}$/.test(lease.runId) ||
|
||||
lease.ownerPid !== process.pid ||
|
||||
!Number.isSafeInteger(lease.expiresAt) ||
|
||||
!/^[a-f0-9]{32}$/.test(lease.token) ||
|
||||
typeof lease.supervised !== 'boolean'
|
||||
)
|
||||
throw new CsoError('PERSISTENCE_FAILED', 'Reproduction lease metadata is invalid');
|
||||
const expectedPath = join(machinePoolRoot(), sha256(lease.endpoint).slice(0, 24), `slot-${lease.slot}`);
|
||||
if (lease.path !== expectedPath)
|
||||
throw new CsoError('PERSISTENCE_FAILED', 'Reproduction lease path is invalid');
|
||||
// This exact helper-owned schema contains only control metadata. In
|
||||
// particular, its random capability may resemble a wallet address and must
|
||||
// remain byte-identical to lease.token; untrusted reports still use writeJson.
|
||||
atomicWriteSync(join(lease.path,'lease.json'),JSON.stringify(lease)+'\n',{mode:0o600});
|
||||
atomicWriteSync(join(lease.path, 'lease.json'), JSON.stringify(lease) + '\n', { mode: 0o600 });
|
||||
}
|
||||
export function admit(endpoint: string, runId: string, deadline: number): Lease {
|
||||
if (!/^unix:\/\/[/.A-Za-z0-9_-]+$/.test(endpoint)) throw new CsoError('ISOLATION_FAILED','Only a pinned local Unix Docker endpoint is admitted on this host');
|
||||
const pool = secureDirectory(join(machinePoolRoot(),sha256(endpoint).slice(0,24)));
|
||||
for (let slot=0;slot<2;slot++) {
|
||||
const path=join(pool,`slot-${slot}`),control=slotControl(pool,slot);let mutation:Claim;
|
||||
try{mutation=acquireClaim(control.path,control.stat);}catch(error){if(error instanceof CsoError&&error.code==='INSUFFICIENT_CAPACITY')continue;throw error;}
|
||||
try{
|
||||
if (!/^unix:\/\/[/.A-Za-z0-9_-]+$/.test(endpoint))
|
||||
throw new CsoError(
|
||||
'ISOLATION_FAILED',
|
||||
'Only a pinned local Unix Docker endpoint is admitted on this host',
|
||||
);
|
||||
const pool = secureDirectory(join(machinePoolRoot(), sha256(endpoint).slice(0, 24)));
|
||||
for (let slot = 0; slot < 2; slot++) {
|
||||
const path = join(pool, `slot-${slot}`),
|
||||
control = slotControl(pool, slot);
|
||||
let mutation: Claim;
|
||||
try {
|
||||
mutation = acquireClaim(control.path, control.stat);
|
||||
} catch (error) {
|
||||
if (error instanceof CsoError && error.code === 'INSUFFICIENT_CAPACITY') continue;
|
||||
throw error;
|
||||
}
|
||||
try {
|
||||
try {
|
||||
fs.mkdirSync(path,{mode:0o700});
|
||||
} catch(error:any) {
|
||||
if(error?.code!=='EEXIST')throw new CsoError('PERSISTENCE_FAILED','Reproduction lease slot could not be created');
|
||||
fs.mkdirSync(path, { mode: 0o700 });
|
||||
} catch (error: any) {
|
||||
if (error?.code !== 'EEXIST')
|
||||
throw new CsoError('PERSISTENCE_FAILED', 'Reproduction lease slot could not be created');
|
||||
try {
|
||||
const observed=privateDirectory(path,'Reproduction lease slot');
|
||||
const old = JSON.parse(fs.readFileSync(join(path,'lease.json'),'utf8'));
|
||||
const observed = privateDirectory(path, 'Reproduction lease slot');
|
||||
const old = JSON.parse(fs.readFileSync(join(path, 'lease.json'), 'utf8'));
|
||||
// A supervised lease is removed only after its watchdog or owner has
|
||||
// confirmed exact-resource cleanup. This preserves the two-group cap
|
||||
// through supervisor death and daemon outages.
|
||||
if (old.supervised === true || (typeof old.ownerPid === 'number' && alive(old.ownerPid))) continue;
|
||||
// Unsupervised stale slots cannot have created containers: supervision
|
||||
// is acknowledged before the anchor create call.
|
||||
if(!reclaimSlot(path,pool,slot,observed,typeof old.token==='string'?old.token:undefined))continue;
|
||||
} catch(recoveryError) {
|
||||
if(recoveryError instanceof CsoError)throw recoveryError;
|
||||
if (!reclaimSlot(path, pool, slot, observed, typeof old.token === 'string' ? old.token : undefined))
|
||||
continue;
|
||||
} catch (recoveryError) {
|
||||
if (recoveryError instanceof CsoError) throw recoveryError;
|
||||
// No live initializer can publish into this path while this stable
|
||||
// slot-control claim is held. Recover a crashed partial publication
|
||||
// only after the compatibility grace period.
|
||||
let stat:fs.Stats;try{stat=privateDirectory(path,'Reproduction lease slot');}catch(statError){if(statError instanceof CsoError)throw statError;continue;}
|
||||
if(Date.now()-stat.mtimeMs<=5000)continue;
|
||||
if(!reclaimSlot(path,pool,slot,stat))continue;
|
||||
let stat: fs.Stats;
|
||||
try {
|
||||
stat = privateDirectory(path, 'Reproduction lease slot');
|
||||
} catch (statError) {
|
||||
if (statError instanceof CsoError) throw statError;
|
||||
continue;
|
||||
}
|
||||
if (Date.now() - stat.mtimeMs <= 5000) continue;
|
||||
if (!reclaimSlot(path, pool, slot, stat)) continue;
|
||||
}
|
||||
}
|
||||
// Both authenticated records become visible as one logical publication
|
||||
// when the stable slot-control claim is released.
|
||||
const lease:Lease={endpoint,slot,path,runId,ownerPid:process.pid,expiresAt:deadline,token:randomBytes(16).toString('hex'),supervised:false};writeLease(lease);fs.writeFileSync(join(path,'lease.token'),lease.token+'\n',{mode:0o600,flag:'wx'});return lease;
|
||||
}finally{releaseClaim(mutation);}
|
||||
const lease: Lease = {
|
||||
endpoint,
|
||||
slot,
|
||||
path,
|
||||
runId,
|
||||
ownerPid: process.pid,
|
||||
expiresAt: deadline,
|
||||
token: randomBytes(16).toString('hex'),
|
||||
supervised: false,
|
||||
};
|
||||
writeLease(lease);
|
||||
fs.writeFileSync(join(path, 'lease.token'), lease.token + '\n', { mode: 0o600, flag: 'wx' });
|
||||
return lease;
|
||||
} finally {
|
||||
releaseClaim(mutation);
|
||||
}
|
||||
}
|
||||
throw new CsoError('INSUFFICIENT_CAPACITY','Two reproduction groups are already admitted for this Docker endpoint');
|
||||
throw new CsoError(
|
||||
'INSUFFICIENT_CAPACITY',
|
||||
'Two reproduction groups are already admitted for this Docker endpoint',
|
||||
);
|
||||
}
|
||||
export function markSupervised(lease:Lease):void{
|
||||
const current=JSON.parse(fs.readFileSync(join(lease.path,'lease.json'),'utf8'));
|
||||
if(current.token!==lease.token||current.ownerPid!==lease.ownerPid)throw new CsoError('INSUFFICIENT_CAPACITY','Reproduction lease changed before watchdog supervision');
|
||||
lease.supervised=true;writeLease(lease);
|
||||
export function markSupervised(lease: Lease): void {
|
||||
const current = JSON.parse(fs.readFileSync(join(lease.path, 'lease.json'), 'utf8'));
|
||||
if (current.token !== lease.token || current.ownerPid !== lease.ownerPid)
|
||||
throw new CsoError('INSUFFICIENT_CAPACITY', 'Reproduction lease changed before watchdog supervision');
|
||||
lease.supervised = true;
|
||||
writeLease(lease);
|
||||
}
|
||||
export function release(lease: Lease): void {
|
||||
let observed:fs.Stats;try{observed=privateDirectory(lease.path,'Reproduction lease slot');}catch(error:any){if(error?.code==='ENOENT')throw new CsoError('PERSISTENCE_FAILED','Exact reproduction lease was already missing');throw error;}
|
||||
const claim=acquireClaim(lease.path,observed);
|
||||
try{
|
||||
const currentStat=privateDirectory(lease.path,'Reproduction lease slot');if(!sameDirectory(observed,currentStat))throw new CsoError('PERSISTENCE_FAILED','Reproduction lease changed before exact release');
|
||||
const names=fs.readdirSync(lease.path).filter(name=>name!=='.recovery'&&!/^\.recovery\.tmp\.\d+\.[a-f0-9]{8}$/.test(name)).sort();if(names.join('\0')!=='lease.json\0lease.token')throw new CsoError('PERSISTENCE_FAILED','Reproduction lease contents changed before exact release');
|
||||
privateFile(join(lease.path,'lease.json'),'Reproduction lease');privateFile(join(lease.path,'lease.token'),'Reproduction lease token');
|
||||
const current=JSON.parse(fs.readFileSync(join(lease.path,'lease.json'),'utf8')),token=fs.readFileSync(join(lease.path,'lease.token'),'utf8').trim();
|
||||
if(current.runId!==lease.runId||current.ownerPid!==lease.ownerPid||current.token!==lease.token||token!==lease.token)throw new CsoError('PERSISTENCE_FAILED','Reproduction lease ownership changed before exact release');
|
||||
fs.unlinkSync(join(lease.path,'lease.token'));fs.unlinkSync(join(lease.path,'lease.json'));releaseClaim(claim);fs.rmdirSync(lease.path);
|
||||
if(fs.existsSync(lease.path))throw new CsoError('PERSISTENCE_FAILED','Exact reproduction lease removal could not be proven');
|
||||
}catch(error){try{if(fs.existsSync(claim.path))releaseClaim(claim);}catch{}if(error instanceof CsoError)throw error;throw new CsoError('PERSISTENCE_FAILED','Exact reproduction lease removal failed');}
|
||||
let observed: fs.Stats;
|
||||
try {
|
||||
observed = privateDirectory(lease.path, 'Reproduction lease slot');
|
||||
} catch (error: any) {
|
||||
if (error?.code === 'ENOENT')
|
||||
throw new CsoError('PERSISTENCE_FAILED', 'Exact reproduction lease was already missing');
|
||||
throw error;
|
||||
}
|
||||
const claim = acquireClaim(lease.path, observed);
|
||||
try {
|
||||
const currentStat = privateDirectory(lease.path, 'Reproduction lease slot');
|
||||
if (!sameDirectory(observed, currentStat))
|
||||
throw new CsoError('PERSISTENCE_FAILED', 'Reproduction lease changed before exact release');
|
||||
const names = fs
|
||||
.readdirSync(lease.path)
|
||||
.filter((name) => name !== '.recovery' && !/^\.recovery\.tmp\.\d+\.[a-f0-9]{8}$/.test(name))
|
||||
.sort();
|
||||
if (names.join('\0') !== 'lease.json\0lease.token')
|
||||
throw new CsoError('PERSISTENCE_FAILED', 'Reproduction lease contents changed before exact release');
|
||||
privateFile(join(lease.path, 'lease.json'), 'Reproduction lease');
|
||||
privateFile(join(lease.path, 'lease.token'), 'Reproduction lease token');
|
||||
const current = JSON.parse(fs.readFileSync(join(lease.path, 'lease.json'), 'utf8')),
|
||||
token = fs.readFileSync(join(lease.path, 'lease.token'), 'utf8').trim();
|
||||
if (
|
||||
current.runId !== lease.runId ||
|
||||
current.ownerPid !== lease.ownerPid ||
|
||||
current.token !== lease.token ||
|
||||
token !== lease.token
|
||||
)
|
||||
throw new CsoError('PERSISTENCE_FAILED', 'Reproduction lease ownership changed before exact release');
|
||||
fs.unlinkSync(join(lease.path, 'lease.token'));
|
||||
fs.unlinkSync(join(lease.path, 'lease.json'));
|
||||
releaseClaim(claim);
|
||||
fs.rmdirSync(lease.path);
|
||||
if (fs.existsSync(lease.path))
|
||||
throw new CsoError('PERSISTENCE_FAILED', 'Exact reproduction lease removal could not be proven');
|
||||
} catch (error) {
|
||||
try {
|
||||
if (fs.existsSync(claim.path)) releaseClaim(claim);
|
||||
} catch {}
|
||||
if (error instanceof CsoError) throw error;
|
||||
throw new CsoError('PERSISTENCE_FAILED', 'Exact reproduction lease removal failed');
|
||||
}
|
||||
}
|
||||
export function total(roles: Role[]) {
|
||||
const value = roles.reduce((a,r) => ({cpu:a.cpu+ROLE_LIMITS[r].cpu,memoryMiB:a.memoryMiB+ROLE_LIMITS[r].memoryMiB,pids:a.pids+ROLE_LIMITS[r].pids,writableMiB:a.writableMiB+ROLE_LIMITS[r].writableMiB}), {cpu:0,memoryMiB:0,pids:0,writableMiB:0});
|
||||
if (value.cpu > GROUP_LIMITS.cpu || value.memoryMiB > GROUP_LIMITS.memoryMiB || value.pids > GROUP_LIMITS.pids || value.writableMiB > GROUP_LIMITS.writableMiB)
|
||||
throw new CsoError('INSUFFICIENT_CAPACITY','Requested sidecars exceed the aggregate reproduction-group limit');
|
||||
const value = roles.reduce(
|
||||
(a, r) => ({
|
||||
cpu: a.cpu + ROLE_LIMITS[r].cpu,
|
||||
memoryMiB: a.memoryMiB + ROLE_LIMITS[r].memoryMiB,
|
||||
pids: a.pids + ROLE_LIMITS[r].pids,
|
||||
writableMiB: a.writableMiB + ROLE_LIMITS[r].writableMiB,
|
||||
}),
|
||||
{ cpu: 0, memoryMiB: 0, pids: 0, writableMiB: 0 },
|
||||
);
|
||||
if (
|
||||
value.cpu > GROUP_LIMITS.cpu ||
|
||||
value.memoryMiB > GROUP_LIMITS.memoryMiB ||
|
||||
value.pids > GROUP_LIMITS.pids ||
|
||||
value.writableMiB > GROUP_LIMITS.writableMiB
|
||||
)
|
||||
throw new CsoError(
|
||||
'INSUFFICIENT_CAPACITY',
|
||||
'Requested sidecars exceed the aggregate reproduction-group limit',
|
||||
);
|
||||
return value;
|
||||
}
|
||||
+54
-11
@@ -2,15 +2,58 @@ import * as fs from 'node:fs';
|
||||
import { CsoError } from './contracts';
|
||||
|
||||
/** Read one caller-supplied control file without following or blocking on a raced special file. */
|
||||
export function readBoundedStable(path:string,max:number,label:string):Buffer{
|
||||
let named:fs.Stats,fd:number|undefined;try{named=fs.lstatSync(path);}catch{throw new CsoError('MISSING_INPUT',`${label} does not exist`);}
|
||||
if(named.isSymbolicLink()||!named.isFile()||named.nlink!==1||named.size>max)throw new CsoError('MISSING_INPUT',`${label} must be one bounded regular file`);
|
||||
try{
|
||||
fd=fs.openSync(path,fs.constants.O_RDONLY|(fs.constants.O_NOFOLLOW??0)|(fs.constants.O_NONBLOCK??0));const opened=fs.fstatSync(fd);
|
||||
if(!opened.isFile()||opened.nlink!==1||opened.dev!==named.dev||opened.ino!==named.ino||opened.mode!==named.mode||opened.size!==named.size)throw new CsoError('SNAPSHOT_RACE',`${label} changed before it could be read`);
|
||||
const data=Buffer.alloc(max+1);let bytes=0,count=0;while(bytes<data.length&&(count=fs.readSync(fd,data,bytes,data.length-bytes,null))>0)bytes+=count;
|
||||
const after=fs.fstatSync(fd),current=fs.lstatSync(path);if(bytes>max)throw new CsoError('MISSING_INPUT',`${label} exceeds the ${max}-byte limit`);
|
||||
if(!current.isFile()||current.isSymbolicLink()||current.nlink!==1||current.dev!==opened.dev||current.ino!==opened.ino||current.mode!==opened.mode||after.size!==opened.size||after.mtimeMs!==opened.mtimeMs||after.ctimeMs!==opened.ctimeMs)throw new CsoError('SNAPSHOT_RACE',`${label} changed while it was read`);
|
||||
return data.subarray(0,bytes);
|
||||
}catch(error){if(error instanceof CsoError)throw error;const code=(error as NodeJS.ErrnoException).code;if(['ELOOP','ENOENT','ENOTDIR','ENXIO'].includes(code??''))throw new CsoError('SNAPSHOT_RACE',`${label} changed before it could be opened`);throw new CsoError('MISSING_INPUT',`${label} is missing or unreadable`);}finally{if(fd!==undefined)fs.closeSync(fd);}
|
||||
export function readBoundedStable(path: string, max: number, label: string): Buffer {
|
||||
let named: fs.Stats, fd: number | undefined;
|
||||
try {
|
||||
named = fs.lstatSync(path);
|
||||
} catch {
|
||||
throw new CsoError('MISSING_INPUT', `${label} does not exist`);
|
||||
}
|
||||
if (named.isSymbolicLink() || !named.isFile() || named.nlink !== 1 || named.size > max)
|
||||
throw new CsoError('MISSING_INPUT', `${label} must be one bounded regular file`);
|
||||
try {
|
||||
fd = fs.openSync(
|
||||
path,
|
||||
fs.constants.O_RDONLY | (fs.constants.O_NOFOLLOW ?? 0) | (fs.constants.O_NONBLOCK ?? 0),
|
||||
);
|
||||
const opened = fs.fstatSync(fd);
|
||||
if (
|
||||
!opened.isFile() ||
|
||||
opened.nlink !== 1 ||
|
||||
opened.dev !== named.dev ||
|
||||
opened.ino !== named.ino ||
|
||||
opened.mode !== named.mode ||
|
||||
opened.size !== named.size
|
||||
)
|
||||
throw new CsoError('SNAPSHOT_RACE', `${label} changed before it could be read`);
|
||||
const data = Buffer.alloc(max + 1);
|
||||
let bytes = 0,
|
||||
count = 0;
|
||||
while (bytes < data.length && (count = fs.readSync(fd, data, bytes, data.length - bytes, null)) > 0)
|
||||
bytes += count;
|
||||
const after = fs.fstatSync(fd),
|
||||
current = fs.lstatSync(path);
|
||||
if (bytes > max) throw new CsoError('MISSING_INPUT', `${label} exceeds the ${max}-byte limit`);
|
||||
if (
|
||||
!current.isFile() ||
|
||||
current.isSymbolicLink() ||
|
||||
current.nlink !== 1 ||
|
||||
current.dev !== opened.dev ||
|
||||
current.ino !== opened.ino ||
|
||||
current.mode !== opened.mode ||
|
||||
after.size !== opened.size ||
|
||||
after.mtimeMs !== opened.mtimeMs ||
|
||||
after.ctimeMs !== opened.ctimeMs
|
||||
)
|
||||
throw new CsoError('SNAPSHOT_RACE', `${label} changed while it was read`);
|
||||
return data.subarray(0, bytes);
|
||||
} catch (error) {
|
||||
if (error instanceof CsoError) throw error;
|
||||
const code = (error as NodeJS.ErrnoException).code;
|
||||
if (['ELOOP', 'ENOENT', 'ENOTDIR', 'ENXIO'].includes(code ?? ''))
|
||||
throw new CsoError('SNAPSHOT_RACE', `${label} changed before it could be opened`);
|
||||
throw new CsoError('MISSING_INPUT', `${label} is missing or unreadable`);
|
||||
} finally {
|
||||
if (fd !== undefined) fs.closeSync(fd);
|
||||
}
|
||||
}
|
||||
+486
-169
File diff suppressed because it is too large.
Load diff
+2624
-398
File diff suppressed because it is too large.
Load diff
+856
-236
File diff suppressed because it is too large.
Load diff
+940
-230
File diff suppressed because it is too large.
Load diff
+87
-36
@@ -1,49 +1,100 @@
|
||||
/** Decode the two Git path tokens in a `diff --git` header. */
|
||||
function token(source:string,offset:number):{value:string;next:number}|undefined{
|
||||
if(source[offset]!=='"'){
|
||||
const end=source.indexOf(' ',offset),next=end<0?source.length:end;
|
||||
if(next===offset)return;
|
||||
return{value:source.slice(offset,next),next};
|
||||
function token(source: string, offset: number): { value: string; next: number } | undefined {
|
||||
if (source[offset] !== '"') {
|
||||
const end = source.indexOf(' ', offset),
|
||||
next = end < 0 ? source.length : end;
|
||||
if (next === offset) return;
|
||||
return { value: source.slice(offset, next), next };
|
||||
}
|
||||
const bytes:number[]=[];let at=offset+1;
|
||||
const append=(value:string)=>bytes.push(...new TextEncoder().encode(value));
|
||||
while(at<source.length){
|
||||
const value=source[at++];
|
||||
if(value==='"')return{value:new TextDecoder('utf-8',{fatal:true}).decode(Uint8Array.from(bytes)),next:at};
|
||||
if(value!=='\\'){append(value);continue;}
|
||||
if(at>=source.length)return;
|
||||
const escaped=source[at++],mapped:{[key:string]:string}={a:'\x07',b:'\b',f:'\f',n:'\n',r:'\r',t:'\t',v:'\v','\\':'\\','"':'"'};
|
||||
if(mapped[escaped]!==undefined){append(mapped[escaped]);continue;}
|
||||
if(/[0-7]/.test(escaped)&&/^[0-7]{2}/.test(source.slice(at,at+2))){bytes.push(Number.parseInt(escaped+source.slice(at,at+2),8));at+=2;continue;}
|
||||
const bytes: number[] = [];
|
||||
let at = offset + 1;
|
||||
const append = (value: string) => bytes.push(...new TextEncoder().encode(value));
|
||||
while (at < source.length) {
|
||||
const value = source[at++];
|
||||
if (value === '"')
|
||||
return { value: new TextDecoder('utf-8', { fatal: true }).decode(Uint8Array.from(bytes)), next: at };
|
||||
if (value !== '\\') {
|
||||
append(value);
|
||||
continue;
|
||||
}
|
||||
if (at >= source.length) return;
|
||||
const escaped = source[at++],
|
||||
mapped: { [key: string]: string } = {
|
||||
a: '\x07',
|
||||
b: '\b',
|
||||
f: '\f',
|
||||
n: '\n',
|
||||
r: '\r',
|
||||
t: '\t',
|
||||
v: '\v',
|
||||
'\\': '\\',
|
||||
'"': '"',
|
||||
};
|
||||
if (mapped[escaped] !== undefined) {
|
||||
append(mapped[escaped]);
|
||||
continue;
|
||||
}
|
||||
if (/[0-7]/.test(escaped) && /^[0-7]{2}/.test(source.slice(at, at + 2))) {
|
||||
bytes.push(Number.parseInt(escaped + source.slice(at, at + 2), 8));
|
||||
at += 2;
|
||||
continue;
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
export function gitDiffHeaderPaths(line:string):[string,string]|undefined{
|
||||
const prefix='diff --git ';if(!line.startsWith(prefix))return;
|
||||
try{
|
||||
const left=token(line,prefix.length);if(!left||line[left.next]!==' ')return;
|
||||
const right=token(line,left.next+1);if(!right||right.next!==line.length)return;
|
||||
return[left.value,right.value];
|
||||
}catch{return;}
|
||||
export function gitDiffHeaderPaths(line: string): [string, string] | undefined {
|
||||
const prefix = 'diff --git ';
|
||||
if (!line.startsWith(prefix)) return;
|
||||
try {
|
||||
const left = token(line, prefix.length);
|
||||
if (!left || line[left.next] !== ' ') return;
|
||||
const right = token(line, left.next + 1);
|
||||
if (!right || right.next !== line.length) return;
|
||||
return [left.value, right.value];
|
||||
} catch {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/** Return only exact path hunks, keeping one commit preamble per matching commit. */
|
||||
export function historyForPath(raw:string,path:string):string|undefined{
|
||||
const expected=new Set([`a/${path}`,`b/${path}`]),output:string[]=[],lines=raw.split('\n');
|
||||
let preamble:string[]=[],section:string[]|undefined,include=false,preambleEmitted=false;
|
||||
const flush=()=>{
|
||||
if(section&&include){if(!preambleEmitted){output.push(...preamble);preambleEmitted=true;}output.push(...section);}
|
||||
section=undefined;include=false;
|
||||
};
|
||||
for(const line of lines){
|
||||
if(line.startsWith('commit ')){flush();preamble=[line];preambleEmitted=false;continue;}
|
||||
if(line.startsWith('diff --git ')){
|
||||
flush();section=[line];const paths=gitDiffHeaderPaths(line);include=Boolean(paths&&(expected.has(paths[0])||expected.has(paths[1])));continue;
|
||||
export function historyForPath(raw: string, path: string): string | undefined {
|
||||
const expected = new Set([`a/${path}`, `b/${path}`]),
|
||||
output: string[] = [],
|
||||
lines = raw.split('\n');
|
||||
let preamble: string[] = [],
|
||||
section: string[] | undefined,
|
||||
include = false,
|
||||
preambleEmitted = false;
|
||||
const flush = () => {
|
||||
if (section && include) {
|
||||
if (!preambleEmitted) {
|
||||
output.push(...preamble);
|
||||
preambleEmitted = true;
|
||||
}
|
||||
output.push(...section);
|
||||
}
|
||||
if(section)section.push(line);else preamble.push(line);
|
||||
section = undefined;
|
||||
include = false;
|
||||
};
|
||||
for (const line of lines) {
|
||||
if (line.startsWith('commit ')) {
|
||||
flush();
|
||||
preamble = [line];
|
||||
preambleEmitted = false;
|
||||
continue;
|
||||
}
|
||||
if (line.startsWith('diff --git ')) {
|
||||
flush();
|
||||
section = [line];
|
||||
const paths = gitDiffHeaderPaths(line);
|
||||
include = Boolean(paths && (expected.has(paths[0]) || expected.has(paths[1])));
|
||||
continue;
|
||||
}
|
||||
if (section) section.push(line);
|
||||
else preamble.push(line);
|
||||
}
|
||||
flush();
|
||||
while(output.at(-1)==='')output.pop();
|
||||
return output.length?output.join('\n'):undefined;
|
||||
while (output.at(-1) === '') output.pop();
|
||||
return output.length ? output.join('\n') : undefined;
|
||||
}
|
||||
+360
-122
@@ -8,175 +8,413 @@ import { validateScannerCatalog, type ScannerCatalog } from './scanner-catalog';
|
||||
import { secureDirectory } from './state';
|
||||
|
||||
export interface QualifiedCatalogImage {
|
||||
kind:'runtime'|'scanner';
|
||||
id:string;
|
||||
image:string;
|
||||
platform:RuntimePlatform;
|
||||
kind: 'runtime' | 'scanner';
|
||||
id: string;
|
||||
image: string;
|
||||
platform: RuntimePlatform;
|
||||
}
|
||||
export interface CatalogImageSession {
|
||||
readonly docker:{endpoint:string;version:string;security:string[]};
|
||||
present(entry:QualifiedCatalogImage,deadline?:number):Promise<boolean>;
|
||||
pull(entry:QualifiedCatalogImage,deadline?:number):Promise<void>;
|
||||
close():void;
|
||||
readonly docker: { endpoint: string; version: string; security: string[] };
|
||||
present(entry: QualifiedCatalogImage, deadline?: number): Promise<boolean>;
|
||||
pull(entry: QualifiedCatalogImage, deadline?: number): Promise<void>;
|
||||
close(): void;
|
||||
}
|
||||
/** Doctor performs concurrent, read-only checks inside its 30-second contract. */
|
||||
export const CATALOG_IMAGE_INSPECTION_BUDGET_MS=30_000;
|
||||
export const DEFAULT_CATALOG_IMAGE_BUDGET_MS=30_000;
|
||||
export const MIN_CATALOG_IMAGE_BUDGET_SECONDS=5;
|
||||
export const MAX_CATALOG_IMAGE_BUDGET_SECONDS=300;
|
||||
export const MAX_CATALOG_IMAGE_PROVISIONING_BUDGET_MS=60*60_000;
|
||||
const CATALOG_IMAGE_ADMISSION_BUDGET_MS=30_000;
|
||||
export interface CatalogImageProvisioningPolicy {perImageMs:number;aggregateMs:number;}
|
||||
export const CATALOG_IMAGE_INSPECTION_BUDGET_MS = 30_000;
|
||||
export const DEFAULT_CATALOG_IMAGE_BUDGET_MS = 30_000;
|
||||
export const MIN_CATALOG_IMAGE_BUDGET_SECONDS = 5;
|
||||
export const MAX_CATALOG_IMAGE_BUDGET_SECONDS = 300;
|
||||
export const MAX_CATALOG_IMAGE_PROVISIONING_BUDGET_MS = 60 * 60_000;
|
||||
const CATALOG_IMAGE_ADMISSION_BUDGET_MS = 30_000;
|
||||
export interface CatalogImageProvisioningPolicy {
|
||||
perImageMs: number;
|
||||
aggregateMs: number;
|
||||
}
|
||||
/**
|
||||
* Give every declared native-platform image a bounded opportunity to download.
|
||||
* The one-hour ceiling admits the current eleven-image catalog even at the
|
||||
* maximum configurable five-minute allowance.
|
||||
*/
|
||||
export function catalogImageProvisioningPolicy(imageCount:number,requestedSeconds?:string):CatalogImageProvisioningPolicy{
|
||||
if(!Number.isSafeInteger(imageCount)||imageCount<0)throw new CsoError('INVALID_ARGUMENT','Catalog image count is invalid');
|
||||
let seconds=DEFAULT_CATALOG_IMAGE_BUDGET_MS/1000;
|
||||
if(requestedSeconds!==undefined){
|
||||
if(!/^[0-9]+$/.test(requestedSeconds))throw new CsoError('INVALID_ARGUMENT','--per-image-seconds requires a whole number');
|
||||
seconds=Number(requestedSeconds);
|
||||
if(seconds<MIN_CATALOG_IMAGE_BUDGET_SECONDS||seconds>MAX_CATALOG_IMAGE_BUDGET_SECONDS)throw new CsoError('INVALID_ARGUMENT',`--per-image-seconds must be ${MIN_CATALOG_IMAGE_BUDGET_SECONDS}..${MAX_CATALOG_IMAGE_BUDGET_SECONDS}`);
|
||||
export function catalogImageProvisioningPolicy(
|
||||
imageCount: number,
|
||||
requestedSeconds?: string,
|
||||
): CatalogImageProvisioningPolicy {
|
||||
if (!Number.isSafeInteger(imageCount) || imageCount < 0)
|
||||
throw new CsoError('INVALID_ARGUMENT', 'Catalog image count is invalid');
|
||||
let seconds = DEFAULT_CATALOG_IMAGE_BUDGET_MS / 1000;
|
||||
if (requestedSeconds !== undefined) {
|
||||
if (!/^[0-9]+$/.test(requestedSeconds))
|
||||
throw new CsoError('INVALID_ARGUMENT', '--per-image-seconds requires a whole number');
|
||||
seconds = Number(requestedSeconds);
|
||||
if (seconds < MIN_CATALOG_IMAGE_BUDGET_SECONDS || seconds > MAX_CATALOG_IMAGE_BUDGET_SECONDS)
|
||||
throw new CsoError(
|
||||
'INVALID_ARGUMENT',
|
||||
`--per-image-seconds must be ${MIN_CATALOG_IMAGE_BUDGET_SECONDS}..${MAX_CATALOG_IMAGE_BUDGET_SECONDS}`,
|
||||
);
|
||||
}
|
||||
const perImageMs=seconds*1000,aggregateMs=CATALOG_IMAGE_ADMISSION_BUDGET_MS+imageCount*perImageMs;
|
||||
if(!Number.isSafeInteger(aggregateMs)||aggregateMs>MAX_CATALOG_IMAGE_PROVISIONING_BUDGET_MS)throw new CsoError('INCOMPATIBLE_INPUT','Qualified image catalog exceeds the bounded setup preload capacity');
|
||||
return{perImageMs,aggregateMs};
|
||||
const perImageMs = seconds * 1000,
|
||||
aggregateMs = CATALOG_IMAGE_ADMISSION_BUDGET_MS + imageCount * perImageMs;
|
||||
if (!Number.isSafeInteger(aggregateMs) || aggregateMs > MAX_CATALOG_IMAGE_PROVISIONING_BUDGET_MS)
|
||||
throw new CsoError(
|
||||
'INCOMPATIBLE_INPUT',
|
||||
'Qualified image catalog exceeds the bounded setup preload capacity',
|
||||
);
|
||||
return { perImageMs, aggregateMs };
|
||||
}
|
||||
export type CatalogImageSessionFactory=(deadline:number)=>Promise<CatalogImageSession>;
|
||||
export type CatalogImageSessionFactory = (deadline: number) => Promise<CatalogImageSession>;
|
||||
export interface CatalogImageAvailability extends QualifiedCatalogImage {
|
||||
status:'available'|'unavailable';
|
||||
reason?:string;
|
||||
status: 'available' | 'unavailable';
|
||||
reason?: string;
|
||||
}
|
||||
export interface CatalogImageInspection {
|
||||
docker:{status:'ready'|'missing';detail:unknown};
|
||||
images:CatalogImageAvailability[];
|
||||
docker: { status: 'ready' | 'missing'; detail: unknown };
|
||||
images: CatalogImageAvailability[];
|
||||
}
|
||||
export interface CatalogImageProvisionResult {
|
||||
schemaVersion:1;
|
||||
status:'complete'|'partial'|'not_available';
|
||||
downloads:true;
|
||||
platform:RuntimePlatform;
|
||||
requested:number;
|
||||
inspected:number;
|
||||
alreadyPresent:number;
|
||||
downloaded:number;
|
||||
deadlineReached:boolean;
|
||||
unavailable:CatalogImageAvailability[];
|
||||
summary:string;
|
||||
schemaVersion: 1;
|
||||
status: 'complete' | 'partial' | 'not_available';
|
||||
downloads: true;
|
||||
platform: RuntimePlatform;
|
||||
requested: number;
|
||||
inspected: number;
|
||||
alreadyPresent: number;
|
||||
downloaded: number;
|
||||
deadlineReached: boolean;
|
||||
unavailable: CatalogImageAvailability[];
|
||||
summary: string;
|
||||
}
|
||||
|
||||
export function qualifiedCatalogImages(runtimeCatalog:RuntimeCatalog,scannerCatalog:ScannerCatalog,platform:RuntimePlatform):QualifiedCatalogImage[]{
|
||||
validateRuntimeCatalog(runtimeCatalog);validateScannerCatalog(scannerCatalog);
|
||||
const entries:QualifiedCatalogImage[]=[
|
||||
...runtimeCatalog.runtimes.filter(item=>item.platform===platform).map(item=>({kind:'runtime' as const,id:item.id,image:item.image,platform:item.platform})),
|
||||
...scannerCatalog.scanners.filter(item=>item.platform===platform).map(item=>({kind:'scanner' as const,id:item.id,image:item.image,platform:item.platform})),
|
||||
export function qualifiedCatalogImages(
|
||||
runtimeCatalog: RuntimeCatalog,
|
||||
scannerCatalog: ScannerCatalog,
|
||||
platform: RuntimePlatform,
|
||||
): QualifiedCatalogImage[] {
|
||||
validateRuntimeCatalog(runtimeCatalog);
|
||||
validateScannerCatalog(scannerCatalog);
|
||||
const entries: QualifiedCatalogImage[] = [
|
||||
...runtimeCatalog.runtimes
|
||||
.filter((item) => item.platform === platform)
|
||||
.map((item) => ({ kind: 'runtime' as const, id: item.id, image: item.image, platform: item.platform })),
|
||||
...scannerCatalog.scanners
|
||||
.filter((item) => item.platform === platform)
|
||||
.map((item) => ({ kind: 'scanner' as const, id: item.id, image: item.image, platform: item.platform })),
|
||||
];
|
||||
const identities=new Set<string>();
|
||||
for(const entry of entries){
|
||||
const identity=`${entry.kind}:${entry.id}`;
|
||||
if(identities.has(identity))throw new CsoError('INCOMPATIBLE_INPUT','Qualified image catalogs contain a duplicate identity');
|
||||
const identities = new Set<string>();
|
||||
for (const entry of entries) {
|
||||
const identity = `${entry.kind}:${entry.id}`;
|
||||
if (identities.has(identity))
|
||||
throw new CsoError('INCOMPATIBLE_INPUT', 'Qualified image catalogs contain a duplicate identity');
|
||||
identities.add(identity);
|
||||
}
|
||||
return entries.sort((left,right)=>`${left.kind}:${left.id}`.localeCompare(`${right.kind}:${right.id}`));
|
||||
return entries.sort((left, right) => `${left.kind}:${left.id}`.localeCompare(`${right.kind}:${right.id}`));
|
||||
}
|
||||
|
||||
function controlledReason(error:unknown,fallback:string):string{
|
||||
return error instanceof CsoError?error.message:fallback;
|
||||
function controlledReason(error: unknown, fallback: string): string {
|
||||
return error instanceof CsoError ? error.message : fallback;
|
||||
}
|
||||
export async function inspectCatalogImages(entries:QualifiedCatalogImage[],open:CatalogImageSessionFactory,deadline=Date.now()+CATALOG_IMAGE_INSPECTION_BUDGET_MS):Promise<CatalogImageInspection>{
|
||||
let session:CatalogImageSession;
|
||||
try{session=await open(deadline);}catch(error){
|
||||
const detail=controlledReason(error,'Local Docker is unavailable for exact catalog image inspection');
|
||||
return{docker:{status:'missing',detail},images:entries.map(entry=>({...entry,status:'unavailable',reason:detail}))};
|
||||
export async function inspectCatalogImages(
|
||||
entries: QualifiedCatalogImage[],
|
||||
open: CatalogImageSessionFactory,
|
||||
deadline = Date.now() + CATALOG_IMAGE_INSPECTION_BUDGET_MS,
|
||||
): Promise<CatalogImageInspection> {
|
||||
let session: CatalogImageSession;
|
||||
try {
|
||||
session = await open(deadline);
|
||||
} catch (error) {
|
||||
const detail = controlledReason(error, 'Local Docker is unavailable for exact catalog image inspection');
|
||||
return {
|
||||
docker: { status: 'missing', detail },
|
||||
images: entries.map((entry) => ({ ...entry, status: 'unavailable', reason: detail })),
|
||||
};
|
||||
}
|
||||
try{
|
||||
try {
|
||||
// Read-only daemon lookups run together so doctor remains within its
|
||||
// 30-second contract even when a local Docker client is slow to fail.
|
||||
const images=await Promise.all(entries.map(async(entry):Promise<CatalogImageAvailability>=>{
|
||||
try{const present=await session.present(entry);if(Date.now()>=deadline)throw new CsoError('DEADLINE','Exact image inspection reached the aggregate image-provisioning deadline');return{...entry,status:present?'available':'unavailable',...(present?{}:{reason:'Exact qualified image is not present in the local Docker daemon'})};}
|
||||
catch(error){return{...entry,status:'unavailable',reason:controlledReason(error,'Exact qualified image could not be inspected safely')};}
|
||||
}));
|
||||
return{docker:{status:'ready',detail:session.docker},images};
|
||||
}finally{session.close();}
|
||||
const images = await Promise.all(
|
||||
entries.map(async (entry): Promise<CatalogImageAvailability> => {
|
||||
try {
|
||||
const present = await session.present(entry);
|
||||
if (Date.now() >= deadline)
|
||||
throw new CsoError(
|
||||
'DEADLINE',
|
||||
'Exact image inspection reached the aggregate image-provisioning deadline',
|
||||
);
|
||||
return {
|
||||
...entry,
|
||||
status: present ? 'available' : 'unavailable',
|
||||
...(present ? {} : { reason: 'Exact qualified image is not present in the local Docker daemon' }),
|
||||
};
|
||||
} catch (error) {
|
||||
return {
|
||||
...entry,
|
||||
status: 'unavailable',
|
||||
reason: controlledReason(error, 'Exact qualified image could not be inspected safely'),
|
||||
};
|
||||
}
|
||||
}),
|
||||
);
|
||||
return { docker: { status: 'ready', detail: session.docker }, images };
|
||||
} finally {
|
||||
session.close();
|
||||
}
|
||||
}
|
||||
|
||||
export async function provisionCatalogImages(entries:QualifiedCatalogImage[],platform:RuntimePlatform,open:CatalogImageSessionFactory,deadline=Date.now()+catalogImageProvisioningPolicy(entries.length).aggregateMs,perImageBudgetMs=DEFAULT_CATALOG_IMAGE_BUDGET_MS):Promise<CatalogImageProvisionResult>{
|
||||
if(!entries.length)return{schemaVersion:1,status:'complete',downloads:true,platform,requested:0,inspected:0,alreadyPresent:0,downloaded:0,deadlineReached:false,unavailable:[],summary:'No qualified CSO images are published for this platform; static audits remain available.'};
|
||||
if(!Number.isSafeInteger(perImageBudgetMs)||perImageBudgetMs<1||perImageBudgetMs>MAX_CATALOG_IMAGE_BUDGET_SECONDS*1000)throw new CsoError('INVALID_ARGUMENT','Catalog per-image budget is invalid');
|
||||
const deadlineReason='The bounded aggregate CSO image preload deadline was reached';
|
||||
if(Date.now()>=deadline){const unavailable=entries.map(entry=>({...entry,status:'unavailable' as const,reason:deadlineReason}));return{schemaVersion:1,status:'partial',downloads:true,platform,requested:entries.length,inspected:0,alreadyPresent:0,downloaded:0,deadlineReached:true,unavailable,summary:`Qualified CSO image preload partial: 0/${entries.length} available; ${deadlineReason.toLowerCase()}. Rerun setup to continue.`};}
|
||||
let session:CatalogImageSession;
|
||||
try{session=await open(deadline);}catch(error){
|
||||
const reason=controlledReason(error,'Local Docker is unavailable for qualified image provisioning'),unavailable=entries.map(entry=>({...entry,status:'unavailable' as const,reason}));
|
||||
const deadlineReached=error instanceof CsoError&&error.code==='DEADLINE';
|
||||
return{schemaVersion:1,status:deadlineReached?'partial':'not_available',downloads:true,platform,requested:entries.length,inspected:0,alreadyPresent:0,downloaded:0,deadlineReached,unavailable,summary:deadlineReached?`Qualified CSO image preload partial: 0/${entries.length} available; ${reason}. Rerun setup to continue.`:`Qualified CSO images were not preloaded: ${reason}. Rerun setup after the prerequisite is available.`};
|
||||
export async function provisionCatalogImages(
|
||||
entries: QualifiedCatalogImage[],
|
||||
platform: RuntimePlatform,
|
||||
open: CatalogImageSessionFactory,
|
||||
deadline = Date.now() + catalogImageProvisioningPolicy(entries.length).aggregateMs,
|
||||
perImageBudgetMs = DEFAULT_CATALOG_IMAGE_BUDGET_MS,
|
||||
): Promise<CatalogImageProvisionResult> {
|
||||
if (!entries.length)
|
||||
return {
|
||||
schemaVersion: 1,
|
||||
status: 'complete',
|
||||
downloads: true,
|
||||
platform,
|
||||
requested: 0,
|
||||
inspected: 0,
|
||||
alreadyPresent: 0,
|
||||
downloaded: 0,
|
||||
deadlineReached: false,
|
||||
unavailable: [],
|
||||
summary: 'No qualified CSO images are published for this platform; static audits remain available.',
|
||||
};
|
||||
if (
|
||||
!Number.isSafeInteger(perImageBudgetMs) ||
|
||||
perImageBudgetMs < 1 ||
|
||||
perImageBudgetMs > MAX_CATALOG_IMAGE_BUDGET_SECONDS * 1000
|
||||
)
|
||||
throw new CsoError('INVALID_ARGUMENT', 'Catalog per-image budget is invalid');
|
||||
const deadlineReason = 'The bounded aggregate CSO image preload deadline was reached';
|
||||
if (Date.now() >= deadline) {
|
||||
const unavailable = entries.map((entry) => ({
|
||||
...entry,
|
||||
status: 'unavailable' as const,
|
||||
reason: deadlineReason,
|
||||
}));
|
||||
return {
|
||||
schemaVersion: 1,
|
||||
status: 'partial',
|
||||
downloads: true,
|
||||
platform,
|
||||
requested: entries.length,
|
||||
inspected: 0,
|
||||
alreadyPresent: 0,
|
||||
downloaded: 0,
|
||||
deadlineReached: true,
|
||||
unavailable,
|
||||
summary: `Qualified CSO image preload partial: 0/${entries.length} available; ${deadlineReason.toLowerCase()}. Rerun setup to continue.`,
|
||||
};
|
||||
}
|
||||
let inspected=0,alreadyPresent=0,downloaded=0,pullBlocked='',deadlineReached=false,perImageTimeouts=0;const unavailable:CatalogImageAvailability[]=[];
|
||||
try{
|
||||
for(let index=0;index<entries.length;index++){
|
||||
const entry=entries[index];
|
||||
if(Date.now()>=deadline){deadlineReached=true;for(const remaining of entries.slice(index))unavailable.push({...remaining,status:'unavailable',reason:deadlineReason});break;}
|
||||
const imageDeadline=Math.min(deadline,Date.now()+perImageBudgetMs),perImageReason=`The ${Math.ceil(perImageBudgetMs/1000)}-second per-image CSO preload deadline was reached`;
|
||||
let present=false;
|
||||
try{
|
||||
present=await session.present(entry,imageDeadline);if(Date.now()>=imageDeadline)throw new CsoError('DEADLINE',imageDeadline===deadline?'Exact image inspection reached the aggregate image-provisioning deadline':perImageReason);inspected++;
|
||||
if(present){alreadyPresent++;continue;}
|
||||
}catch(error){
|
||||
if(error instanceof CsoError&&error.code==='DEADLINE'){
|
||||
if(Date.now()>=deadline){deadlineReached=true;unavailable.push({...entry,status:'unavailable',reason:error.message});for(const remaining of entries.slice(index+1))unavailable.push({...remaining,status:'unavailable',reason:deadlineReason});break;}
|
||||
perImageTimeouts++;unavailable.push({...entry,status:'unavailable',reason:perImageReason});continue;
|
||||
let session: CatalogImageSession;
|
||||
try {
|
||||
session = await open(deadline);
|
||||
} catch (error) {
|
||||
const reason = controlledReason(error, 'Local Docker is unavailable for qualified image provisioning'),
|
||||
unavailable = entries.map((entry) => ({ ...entry, status: 'unavailable' as const, reason }));
|
||||
const deadlineReached = error instanceof CsoError && error.code === 'DEADLINE';
|
||||
return {
|
||||
schemaVersion: 1,
|
||||
status: deadlineReached ? 'partial' : 'not_available',
|
||||
downloads: true,
|
||||
platform,
|
||||
requested: entries.length,
|
||||
inspected: 0,
|
||||
alreadyPresent: 0,
|
||||
downloaded: 0,
|
||||
deadlineReached,
|
||||
unavailable,
|
||||
summary: deadlineReached
|
||||
? `Qualified CSO image preload partial: 0/${entries.length} available; ${reason}. Rerun setup to continue.`
|
||||
: `Qualified CSO images were not preloaded: ${reason}. Rerun setup after the prerequisite is available.`,
|
||||
};
|
||||
}
|
||||
let inspected = 0,
|
||||
alreadyPresent = 0,
|
||||
downloaded = 0,
|
||||
pullBlocked = '',
|
||||
deadlineReached = false,
|
||||
perImageTimeouts = 0;
|
||||
const unavailable: CatalogImageAvailability[] = [];
|
||||
try {
|
||||
for (let index = 0; index < entries.length; index++) {
|
||||
const entry = entries[index];
|
||||
if (Date.now() >= deadline) {
|
||||
deadlineReached = true;
|
||||
for (const remaining of entries.slice(index))
|
||||
unavailable.push({ ...remaining, status: 'unavailable', reason: deadlineReason });
|
||||
break;
|
||||
}
|
||||
const imageDeadline = Math.min(deadline, Date.now() + perImageBudgetMs),
|
||||
perImageReason = `The ${Math.ceil(perImageBudgetMs / 1000)}-second per-image CSO preload deadline was reached`;
|
||||
let present = false;
|
||||
try {
|
||||
present = await session.present(entry, imageDeadline);
|
||||
if (Date.now() >= imageDeadline)
|
||||
throw new CsoError(
|
||||
'DEADLINE',
|
||||
imageDeadline === deadline
|
||||
? 'Exact image inspection reached the aggregate image-provisioning deadline'
|
||||
: perImageReason,
|
||||
);
|
||||
inspected++;
|
||||
if (present) {
|
||||
alreadyPresent++;
|
||||
continue;
|
||||
}
|
||||
unavailable.push({...entry,status:'unavailable',reason:controlledReason(error,'Exact qualified image could not be inspected safely')});continue;
|
||||
} catch (error) {
|
||||
if (error instanceof CsoError && error.code === 'DEADLINE') {
|
||||
if (Date.now() >= deadline) {
|
||||
deadlineReached = true;
|
||||
unavailable.push({ ...entry, status: 'unavailable', reason: error.message });
|
||||
for (const remaining of entries.slice(index + 1))
|
||||
unavailable.push({ ...remaining, status: 'unavailable', reason: deadlineReason });
|
||||
break;
|
||||
}
|
||||
perImageTimeouts++;
|
||||
unavailable.push({ ...entry, status: 'unavailable', reason: perImageReason });
|
||||
continue;
|
||||
}
|
||||
unavailable.push({
|
||||
...entry,
|
||||
status: 'unavailable',
|
||||
reason: controlledReason(error, 'Exact qualified image could not be inspected safely'),
|
||||
});
|
||||
continue;
|
||||
}
|
||||
// A registry failure blocks further network attempts, but read-only local
|
||||
// inspection continues so the setup summary never calls a cached digest
|
||||
// unavailable merely because it sorts after the failed pull.
|
||||
if(pullBlocked){unavailable.push({...entry,status:'unavailable',reason:`Network provisioning stopped after an anonymous registry prerequisite failed: ${pullBlocked}`});continue;}
|
||||
if(Date.now()>=deadline){deadlineReached=true;unavailable.push({...entry,status:'unavailable',reason:deadlineReason});for(const remaining of entries.slice(index+1))unavailable.push({...remaining,status:'unavailable',reason:deadlineReason});break;}
|
||||
try{await session.pull(entry,imageDeadline);if(Date.now()>=imageDeadline)throw new CsoError('DEADLINE',imageDeadline===deadline?'Qualified image pull reached the aggregate preload deadline':perImageReason);downloaded++;}
|
||||
catch(error){
|
||||
if(error instanceof CsoError&&error.code==='DEADLINE'){
|
||||
if(Date.now()>=deadline){deadlineReached=true;unavailable.push({...entry,status:'unavailable',reason:error.message});for(const remaining of entries.slice(index+1))unavailable.push({...remaining,status:'unavailable',reason:deadlineReason});break;}
|
||||
perImageTimeouts++;unavailable.push({...entry,status:'unavailable',reason:perImageReason});continue;
|
||||
if (pullBlocked) {
|
||||
unavailable.push({
|
||||
...entry,
|
||||
status: 'unavailable',
|
||||
reason: `Network provisioning stopped after an anonymous registry prerequisite failed: ${pullBlocked}`,
|
||||
});
|
||||
continue;
|
||||
}
|
||||
if (Date.now() >= deadline) {
|
||||
deadlineReached = true;
|
||||
unavailable.push({ ...entry, status: 'unavailable', reason: deadlineReason });
|
||||
for (const remaining of entries.slice(index + 1))
|
||||
unavailable.push({ ...remaining, status: 'unavailable', reason: deadlineReason });
|
||||
break;
|
||||
}
|
||||
try {
|
||||
await session.pull(entry, imageDeadline);
|
||||
if (Date.now() >= imageDeadline)
|
||||
throw new CsoError(
|
||||
'DEADLINE',
|
||||
imageDeadline === deadline
|
||||
? 'Qualified image pull reached the aggregate preload deadline'
|
||||
: perImageReason,
|
||||
);
|
||||
downloaded++;
|
||||
} catch (error) {
|
||||
if (error instanceof CsoError && error.code === 'DEADLINE') {
|
||||
if (Date.now() >= deadline) {
|
||||
deadlineReached = true;
|
||||
unavailable.push({ ...entry, status: 'unavailable', reason: error.message });
|
||||
for (const remaining of entries.slice(index + 1))
|
||||
unavailable.push({ ...remaining, status: 'unavailable', reason: deadlineReason });
|
||||
break;
|
||||
}
|
||||
perImageTimeouts++;
|
||||
unavailable.push({ ...entry, status: 'unavailable', reason: perImageReason });
|
||||
continue;
|
||||
}
|
||||
pullBlocked=controlledReason(error,'Qualified image provisioning failed');unavailable.push({...entry,status:'unavailable',reason:pullBlocked});
|
||||
pullBlocked = controlledReason(error, 'Qualified image provisioning failed');
|
||||
unavailable.push({ ...entry, status: 'unavailable', reason: pullBlocked });
|
||||
}
|
||||
}
|
||||
}finally{session.close();}
|
||||
const status=deadlineReached?'partial':unavailable.length?(alreadyPresent||downloaded?'partial':'not_available'):'complete';
|
||||
const summary=deadlineReached
|
||||
?`Qualified CSO image preload partial: ${alreadyPresent+downloaded}/${entries.length} available; inspected ${inspected}/${entries.length}; the bounded aggregate deadline was reached. Rerun setup to continue.`
|
||||
:perImageTimeouts
|
||||
?`Qualified CSO image preload ${status}: ${alreadyPresent+downloaded}/${entries.length} available; inspected ${inspected}/${entries.length}; ${perImageTimeouts} exceeded the ${Math.ceil(perImageBudgetMs/1000)}-second per-image deadline. Increase GSTACK_CSO_IMAGE_PULL_TIMEOUT_SECONDS within 5..300 or rerun setup to continue.`
|
||||
:unavailable.length
|
||||
?`Qualified CSO image preload ${status}: ${alreadyPresent+downloaded}/${entries.length} available; inspected ${inspected}/${entries.length}; ${unavailable.length} require local Docker and anonymous public registry access. Rerun setup after the prerequisite is available.`
|
||||
:`Qualified CSO images ready: ${entries.length} available (${downloaded} downloaded, ${alreadyPresent} already local).`;
|
||||
return{schemaVersion:1,status,downloads:true,platform,requested:entries.length,inspected,alreadyPresent,downloaded,deadlineReached,unavailable,summary};
|
||||
} finally {
|
||||
session.close();
|
||||
}
|
||||
const status = deadlineReached
|
||||
? 'partial'
|
||||
: unavailable.length
|
||||
? alreadyPresent || downloaded
|
||||
? 'partial'
|
||||
: 'not_available'
|
||||
: 'complete';
|
||||
const summary = deadlineReached
|
||||
? `Qualified CSO image preload partial: ${alreadyPresent + downloaded}/${entries.length} available; inspected ${inspected}/${entries.length}; the bounded aggregate deadline was reached. Rerun setup to continue.`
|
||||
: perImageTimeouts
|
||||
? `Qualified CSO image preload ${status}: ${alreadyPresent + downloaded}/${entries.length} available; inspected ${inspected}/${entries.length}; ${perImageTimeouts} exceeded the ${Math.ceil(perImageBudgetMs / 1000)}-second per-image deadline. Increase GSTACK_CSO_IMAGE_PULL_TIMEOUT_SECONDS within 5..300 or rerun setup to continue.`
|
||||
: unavailable.length
|
||||
? `Qualified CSO image preload ${status}: ${alreadyPresent + downloaded}/${entries.length} available; inspected ${inspected}/${entries.length}; ${unavailable.length} require local Docker and anonymous public registry access. Rerun setup after the prerequisite is available.`
|
||||
: `Qualified CSO images ready: ${entries.length} available (${downloaded} downloaded, ${alreadyPresent} already local).`;
|
||||
return {
|
||||
schemaVersion: 1,
|
||||
status,
|
||||
downloads: true,
|
||||
platform,
|
||||
requested: entries.length,
|
||||
inspected,
|
||||
alreadyPresent,
|
||||
downloaded,
|
||||
deadlineReached,
|
||||
unavailable,
|
||||
summary,
|
||||
};
|
||||
}
|
||||
|
||||
export async function openLocalCatalogImageSession(env:Record<string,string|undefined>=process.env,deadline=Date.now()+CATALOG_IMAGE_INSPECTION_BUDGET_MS):Promise<CatalogImageSession>{
|
||||
let home='';
|
||||
try{
|
||||
home=secureDirectory(fs.mkdtempSync(join(fs.realpathSync(os.tmpdir()),'gstack-cso-images-')));
|
||||
export async function openLocalCatalogImageSession(
|
||||
env: Record<string, string | undefined> = process.env,
|
||||
deadline = Date.now() + CATALOG_IMAGE_INSPECTION_BUDGET_MS,
|
||||
): Promise<CatalogImageSession> {
|
||||
let home = '';
|
||||
try {
|
||||
home = secureDirectory(fs.mkdtempSync(join(fs.realpathSync(os.tmpdir()), 'gstack-cso-images-')));
|
||||
// Endpoint discovery and the daemon probe must not borrow the download
|
||||
// allowance. A slow or hostile local Docker endpoint gets the same bounded
|
||||
// admission window in doctor and setup; successful pulls keep the caller's
|
||||
// larger aggregate deadline below.
|
||||
const admissionDeadline=Math.min(deadline,Date.now()+CATALOG_IMAGE_ADMISSION_BUDGET_MS);
|
||||
const endpoint=await dockerEndpoint(home,env,admissionDeadline),config=secureDirectory(join(home,'docker-config'));
|
||||
const admissionDeadline = Math.min(deadline, Date.now() + CATALOG_IMAGE_ADMISSION_BUDGET_MS);
|
||||
const endpoint = await dockerEndpoint(home, env, admissionDeadline),
|
||||
config = secureDirectory(join(home, 'docker-config'));
|
||||
// dockerEnvironment pins both HOME and DOCKER_CONFIG here. An explicit
|
||||
// empty auth map prevents inherited credential stores/helpers from being
|
||||
// consulted during installation-time public pulls.
|
||||
fs.writeFileSync(join(config,'config.json'),'{"auths":{}}\n',{encoding:'utf8',mode:0o600,flag:'wx'});
|
||||
const probe=await dockerProbe(endpoint,home,admissionDeadline),docker={endpoint:endpoint.uri,...probe};
|
||||
let closed=false;
|
||||
return{
|
||||
fs.writeFileSync(join(config, 'config.json'), '{"auths":{}}\n', {
|
||||
encoding: 'utf8',
|
||||
mode: 0o600,
|
||||
flag: 'wx',
|
||||
});
|
||||
const probe = await dockerProbe(endpoint, home, admissionDeadline),
|
||||
docker = { endpoint: endpoint.uri, ...probe };
|
||||
let closed = false;
|
||||
return {
|
||||
docker,
|
||||
present:(entry,operationDeadline=deadline)=>{if(closed)throw new CsoError('ISOLATION_FAILED','Catalog image session is closed');return dockerExactImagePresent(endpoint,home,entry.image,entry.platform,Math.min(deadline,operationDeadline));},
|
||||
pull:(entry,operationDeadline=deadline)=>{if(closed)throw new CsoError('ISOLATION_FAILED','Catalog image session is closed');return dockerPullExactCatalogImage(endpoint,home,entry.image,entry.platform,Math.min(deadline,operationDeadline));},
|
||||
close:()=>{if(closed)return;closed=true;fs.rmSync(home,{recursive:true,force:true});},
|
||||
present: (entry, operationDeadline = deadline) => {
|
||||
if (closed) throw new CsoError('ISOLATION_FAILED', 'Catalog image session is closed');
|
||||
return dockerExactImagePresent(
|
||||
endpoint,
|
||||
home,
|
||||
entry.image,
|
||||
entry.platform,
|
||||
Math.min(deadline, operationDeadline),
|
||||
);
|
||||
},
|
||||
pull: (entry, operationDeadline = deadline) => {
|
||||
if (closed) throw new CsoError('ISOLATION_FAILED', 'Catalog image session is closed');
|
||||
return dockerPullExactCatalogImage(
|
||||
endpoint,
|
||||
home,
|
||||
entry.image,
|
||||
entry.platform,
|
||||
Math.min(deadline, operationDeadline),
|
||||
);
|
||||
},
|
||||
close: () => {
|
||||
if (closed) return;
|
||||
closed = true;
|
||||
fs.rmSync(home, { recursive: true, force: true });
|
||||
},
|
||||
};
|
||||
}catch(error){if(home)fs.rmSync(home,{recursive:true,force:true});throw error;}
|
||||
} catch (error) {
|
||||
if (home) fs.rmSync(home, { recursive: true, force: true });
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
+716
-196
File diff suppressed because it is too large.
Load diff
+1264
-397
File diff suppressed because it is too large.
Load diff
+937
-297
File diff suppressed because it is too large.
Load diff
+875
-164
File diff suppressed because it is too large.
Load diff
+530
-149
@@ -1,218 +1,599 @@
|
||||
import { spawn } from 'node:child_process';
|
||||
import { accessSync, closeSync, constants, existsSync, fstatSync, lstatSync, openSync, readSync, realpathSync, statSync } from 'node:fs';
|
||||
import {
|
||||
accessSync,
|
||||
closeSync,
|
||||
constants,
|
||||
existsSync,
|
||||
fstatSync,
|
||||
lstatSync,
|
||||
type Stats,
|
||||
openSync,
|
||||
readSync,
|
||||
realpathSync,
|
||||
statSync,
|
||||
} from 'node:fs';
|
||||
import { basename, dirname, join, isAbsolute, delimiter, resolve } from 'node:path';
|
||||
import { redactFindingSpans } from '../redact-engine';
|
||||
import { CsoError, MAX_OUTPUT } from './contracts';
|
||||
|
||||
const SOURCE_RUNTIME=/^bun(?:\.exe)?$/i.test(basename(process.execPath));
|
||||
const WINDOWS_GIT=process.platform==='win32'?(process.env.GSTACK_CSO_TRUSTED_GIT||(SOURCE_RUNTIME?Bun.which('git')??'':'')):'';
|
||||
const WINDOWS_SYSTEM=process.platform==='win32'?join(process.env.SystemRoot||'C:\\Windows','System32'):'';
|
||||
export const TRUSTED_DIRECTORIES = process.platform === 'win32'
|
||||
? [...new Set([WINDOWS_GIT?dirname(WINDOWS_GIT):'',WINDOWS_SYSTEM].filter(Boolean))]
|
||||
: ['/usr/local/bin','/usr/bin','/bin','/opt/homebrew/bin','/usr/local/sbin','/usr/sbin','/sbin'];
|
||||
const SOURCE_RUNTIME = /^bun(?:\.exe)?$/i.test(basename(process.execPath));
|
||||
const WINDOWS_GIT =
|
||||
process.platform === 'win32'
|
||||
? process.env.GSTACK_CSO_TRUSTED_GIT || (SOURCE_RUNTIME ? (Bun.which('git') ?? '') : '')
|
||||
: '';
|
||||
const WINDOWS_SYSTEM =
|
||||
process.platform === 'win32' ? join(process.env.SystemRoot || 'C:\\Windows', 'System32') : '';
|
||||
export const TRUSTED_DIRECTORIES =
|
||||
process.platform === 'win32'
|
||||
? [...new Set([WINDOWS_GIT ? dirname(WINDOWS_GIT) : '', WINDOWS_SYSTEM].filter(Boolean))]
|
||||
: ['/usr/local/bin', '/usr/bin', '/bin', '/opt/homebrew/bin', '/usr/local/sbin', '/usr/sbin', '/sbin'];
|
||||
export const TRUSTED_PATH = TRUSTED_DIRECTORIES.join(delimiter);
|
||||
export function executable(name: string): string {
|
||||
// Never consult the audited repository's PATH or executable overrides.
|
||||
if (!/^[a-zA-Z0-9._-]+$/.test(name)) throw new CsoError('INVALID_ARGUMENT','Invalid executable name');
|
||||
if(process.platform==='win32'&&name.toLowerCase()==='git'){
|
||||
try{if(!WINDOWS_GIT||!isAbsolute(WINDOWS_GIT)||basename(WINDOWS_GIT).toLowerCase()!=='git.exe')throw new Error();const stat=statSync(WINDOWS_GIT);if(!stat.isFile())throw new Error();return realpathSync(WINDOWS_GIT);}catch{throw new CsoError('TOOL_UNAVAILABLE','git.exe is not the trusted executable bound during gstack setup');}
|
||||
if (!/^[a-zA-Z0-9._-]+$/.test(name)) throw new CsoError('INVALID_ARGUMENT', 'Invalid executable name');
|
||||
if (process.platform === 'win32' && name.toLowerCase() === 'git') {
|
||||
try {
|
||||
if (!WINDOWS_GIT || !isAbsolute(WINDOWS_GIT) || basename(WINDOWS_GIT).toLowerCase() !== 'git.exe')
|
||||
throw new Error();
|
||||
const stat = statSync(WINDOWS_GIT);
|
||||
if (!stat.isFile()) throw new Error();
|
||||
return realpathSync(WINDOWS_GIT);
|
||||
} catch {
|
||||
throw new CsoError(
|
||||
'TOOL_UNAVAILABLE',
|
||||
'git.exe is not the trusted executable bound during gstack setup',
|
||||
);
|
||||
}
|
||||
}
|
||||
for (const directory of TRUSTED_DIRECTORIES) {
|
||||
const candidates=process.platform==='win32'?[join(directory,`${name}.exe`),join(directory,`${name}.cmd`),join(directory,name)]:[join(directory,name)];
|
||||
for(const p of candidates){
|
||||
try { const stat=statSync(p);accessSync(p,constants.X_OK);if(stat.isFile()&&(process.platform==='win32'||(stat.mode&0o111)))return realpathSync(p); } catch {}
|
||||
const candidates =
|
||||
process.platform === 'win32'
|
||||
? [join(directory, `${name}.exe`), join(directory, `${name}.cmd`), join(directory, name)]
|
||||
: [join(directory, name)];
|
||||
for (const p of candidates) {
|
||||
try {
|
||||
const stat = statSync(p);
|
||||
accessSync(p, constants.X_OK);
|
||||
if (stat.isFile() && (process.platform === 'win32' || stat.mode & 0o111)) return realpathSync(p);
|
||||
} catch {}
|
||||
}
|
||||
}
|
||||
throw new CsoError('TOOL_UNAVAILABLE', `${name} is not installed in a trusted system executable directory`);
|
||||
}
|
||||
export function childEnvironment(home: string): Record<string,string> {
|
||||
return { PATH: TRUSTED_PATH, HOME: home, LANG: 'C.UTF-8', LC_ALL: 'C.UTF-8', TZ: 'UTC',
|
||||
GIT_CONFIG_NOSYSTEM: '1', GIT_CONFIG_GLOBAL: process.platform==='win32'?'NUL':'/dev/null', GIT_TERMINAL_PROMPT: '0',
|
||||
GIT_OPTIONAL_LOCKS: '0', GIT_ATTR_NOSYSTEM: '1' };
|
||||
export function childEnvironment(home: string): Record<string, string> {
|
||||
return {
|
||||
PATH: TRUSTED_PATH,
|
||||
HOME: home,
|
||||
LANG: 'C.UTF-8',
|
||||
LC_ALL: 'C.UTF-8',
|
||||
TZ: 'UTC',
|
||||
GIT_CONFIG_NOSYSTEM: '1',
|
||||
GIT_CONFIG_GLOBAL: process.platform === 'win32' ? 'NUL' : '/dev/null',
|
||||
GIT_TERMINAL_PROMPT: '0',
|
||||
GIT_OPTIONAL_LOCKS: '0',
|
||||
GIT_ATTR_NOSYSTEM: '1',
|
||||
};
|
||||
}
|
||||
export function redact(value: string): string {
|
||||
// Scan the complete bounded stream, including across write/chunk boundaries.
|
||||
const output = redactFindingSpans(value, { maxBytes: MAX_OUTPUT });
|
||||
if (output === null) throw new CsoError('REDACTION_FAILED','Payload withheld because redaction could not safely locate every secret');
|
||||
if (output === null)
|
||||
throw new CsoError(
|
||||
'REDACTION_FAILED',
|
||||
'Payload withheld because redaction could not safely locate every secret',
|
||||
);
|
||||
return output;
|
||||
}
|
||||
const HASH_KEYS=new Set(['planSha256','planHash','originalHash','executionHash','snapshotHash','sourceHash','beforeSha256','afterSha256','patchHash','reviewedPatchHash','harnessHash','fixturesHash','policyHash','auditPolicyHash','originalSourceHash','transformationsHash','archivesHash','inputHash','beforeSourceHash','afterSourceHash','beforeDependencies','afterDependencies','beforeConfiguration','afterConfiguration','requestHash','startPlanHash','testPlanHash','preparationHash','preparedManifestHash','preparedDependencyHash','sourceProjectionHash','executionEnvironmentHash','databaseHash','receiptHash','dependencyClosureHash','closureHash','acquisitionReceiptHash','registryResponseSha256','sha256','versionOutputSha256','isolationPolicyHash','contentSha256','sbomDigest','provenanceDigest','dependencyHash','configurationHash','assertionHash','commandsHash','minimumPassingTestsHash','commandHash','outputHash','observationHash','witnessHash','keyId']);
|
||||
function safeMetadata(value:string,key:string):boolean{
|
||||
if(HASH_KEYS.has(key)&&/^[a-f0-9]{64}$/.test(value))return true;
|
||||
if(['id','fingerprint','findingId','verificationId','reproductionAttemptId','artifactId','reviewArtifactId','bundleId','pathId'].includes(key)&&/^[a-f0-9]{32}$/.test(value))return true;
|
||||
if(key==='path'&&/^@cso-path\/\/[a-f0-9]{32}$/.test(value))return true;
|
||||
if(key==='repoId'&&/^[a-f0-9]{24}$/.test(value))return true;
|
||||
if(key==='runId'&&/^\d{13}-[a-f0-9]{16}$/.test(value))return true;
|
||||
if(key==='replayId'&&/^\d{13}-[a-f0-9]{16}$/.test(value))return true;
|
||||
if(['baseCommit','headCommit'].includes(key)&&/^[a-f0-9]{40,64}$/.test(value))return true;
|
||||
if(['createdAt','expiresAt','deadline','at','databaseUpdatedAt','qualifiedAt'].includes(key)&&/^\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d(?:\.\d{3})?Z$/.test(value))return true;
|
||||
if(key==='nonce'&&/^[a-f0-9]{64}$/.test(value))return true;
|
||||
if(key==='publicKey'&&/^[a-f0-9]{88}$/.test(value))return true;
|
||||
if(key==='signature'&&/^[a-f0-9]{128}$/.test(value))return true;
|
||||
if(key==='image'&&/^[a-z0-9./:_-]+@sha256:[a-f0-9]{64}$/.test(value))return true;
|
||||
if(key==='integrity'&&/^(?:sha256|sha512)-[A-Za-z0-9+/]+={0,2}$/.test(value))return true;
|
||||
const HASH_KEYS = new Set([
|
||||
'planSha256',
|
||||
'planHash',
|
||||
'originalHash',
|
||||
'executionHash',
|
||||
'snapshotHash',
|
||||
'sourceHash',
|
||||
'beforeSha256',
|
||||
'afterSha256',
|
||||
'patchHash',
|
||||
'reviewedPatchHash',
|
||||
'harnessHash',
|
||||
'fixturesHash',
|
||||
'policyHash',
|
||||
'auditPolicyHash',
|
||||
'originalSourceHash',
|
||||
'transformationsHash',
|
||||
'archivesHash',
|
||||
'inputHash',
|
||||
'beforeSourceHash',
|
||||
'afterSourceHash',
|
||||
'beforeDependencies',
|
||||
'afterDependencies',
|
||||
'beforeConfiguration',
|
||||
'afterConfiguration',
|
||||
'requestHash',
|
||||
'startPlanHash',
|
||||
'testPlanHash',
|
||||
'preparationHash',
|
||||
'preparedManifestHash',
|
||||
'preparedDependencyHash',
|
||||
'sourceProjectionHash',
|
||||
'executionEnvironmentHash',
|
||||
'databaseHash',
|
||||
'receiptHash',
|
||||
'dependencyClosureHash',
|
||||
'closureHash',
|
||||
'acquisitionReceiptHash',
|
||||
'registryResponseSha256',
|
||||
'sha256',
|
||||
'versionOutputSha256',
|
||||
'isolationPolicyHash',
|
||||
'contentSha256',
|
||||
'sbomDigest',
|
||||
'provenanceDigest',
|
||||
'dependencyHash',
|
||||
'configurationHash',
|
||||
'assertionHash',
|
||||
'commandsHash',
|
||||
'minimumPassingTestsHash',
|
||||
'commandHash',
|
||||
'outputHash',
|
||||
'observationHash',
|
||||
'witnessHash',
|
||||
'keyId',
|
||||
]);
|
||||
function safeMetadata(value: string, key: string): boolean {
|
||||
if (HASH_KEYS.has(key) && /^[a-f0-9]{64}$/.test(value)) return true;
|
||||
if (
|
||||
[
|
||||
'id',
|
||||
'fingerprint',
|
||||
'findingId',
|
||||
'verificationId',
|
||||
'reproductionAttemptId',
|
||||
'artifactId',
|
||||
'reviewArtifactId',
|
||||
'bundleId',
|
||||
'pathId',
|
||||
].includes(key) &&
|
||||
/^[a-f0-9]{32}$/.test(value)
|
||||
)
|
||||
return true;
|
||||
if (key === 'path' && /^@cso-path\/\/[a-f0-9]{32}$/.test(value)) return true;
|
||||
if (key === 'repoId' && /^[a-f0-9]{24}$/.test(value)) return true;
|
||||
if (key === 'runId' && /^\d{13}-[a-f0-9]{16}$/.test(value)) return true;
|
||||
if (key === 'replayId' && /^\d{13}-[a-f0-9]{16}$/.test(value)) return true;
|
||||
if (['baseCommit', 'headCommit'].includes(key) && /^[a-f0-9]{40,64}$/.test(value)) return true;
|
||||
if (
|
||||
['createdAt', 'expiresAt', 'deadline', 'at', 'databaseUpdatedAt', 'qualifiedAt'].includes(key) &&
|
||||
/^\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d(?:\.\d{3})?Z$/.test(value)
|
||||
)
|
||||
return true;
|
||||
if (key === 'nonce' && /^[a-f0-9]{64}$/.test(value)) return true;
|
||||
if (key === 'publicKey' && /^[a-f0-9]{88}$/.test(value)) return true;
|
||||
if (key === 'signature' && /^[a-f0-9]{128}$/.test(value)) return true;
|
||||
if (key === 'image' && /^[a-z0-9./:_-]+@sha256:[a-f0-9]{64}$/.test(value)) return true;
|
||||
if (key === 'integrity' && /^(?:sha256|sha512)-[A-Za-z0-9+/]+={0,2}$/.test(value)) return true;
|
||||
return false;
|
||||
}
|
||||
function sanitizeJson(value:unknown,key:string,seen:WeakSet<object>,trustedMetadata:boolean):unknown{
|
||||
if(typeof value==='string'){
|
||||
if(trustedMetadata&&safeMetadata(value,key))return value;
|
||||
function sanitizeJson(value: unknown, key: string, seen: WeakSet<object>, trustedMetadata: boolean): unknown {
|
||||
if (typeof value === 'string') {
|
||||
if (trustedMetadata && safeMetadata(value, key)) return value;
|
||||
return redact(value);
|
||||
}
|
||||
if(value===null||typeof value!=='object')return value;
|
||||
if(seen.has(value as object))throw new CsoError('INVALID_SCHEMA','Cyclic JSON cannot be persisted');seen.add(value as object);
|
||||
if(Array.isArray(value)){const out=value.map(v=>sanitizeJson(v,key,seen,trustedMetadata));seen.delete(value);return out;}
|
||||
const out:Record<string,unknown>=Object.create(null);for(const [k,v] of Object.entries(value as Record<string,unknown>)){
|
||||
if(['__proto__','prototype','constructor'].includes(k))throw new CsoError('INVALID_SCHEMA','Unsafe JSON property');out[k]=sanitizeJson(v,k,seen,trustedMetadata);
|
||||
}seen.delete(value as object);return out;
|
||||
if (value === null || typeof value !== 'object') return value;
|
||||
if (seen.has(value as object)) throw new CsoError('INVALID_SCHEMA', 'Cyclic JSON cannot be persisted');
|
||||
seen.add(value as object);
|
||||
if (Array.isArray(value)) {
|
||||
const out = value.map((v) => sanitizeJson(v, key, seen, trustedMetadata));
|
||||
seen.delete(value);
|
||||
return out;
|
||||
}
|
||||
const out: Record<string, unknown> = Object.create(null);
|
||||
for (const [k, v] of Object.entries(value as Record<string, unknown>)) {
|
||||
if (['__proto__', 'prototype', 'constructor'].includes(k))
|
||||
throw new CsoError('INVALID_SCHEMA', 'Unsafe JSON property');
|
||||
out[k] = sanitizeJson(v, k, seen, trustedMetadata);
|
||||
}
|
||||
seen.delete(value as object);
|
||||
return out;
|
||||
}
|
||||
/** Redact untrusted JSON content. Key names never make an untrusted value exempt. */
|
||||
export function sanitizeForJson(value:unknown):unknown{return sanitizeJson(value,'',new WeakSet<object>(),false);}
|
||||
export function sanitizeForJson(value: unknown): unknown {
|
||||
return sanitizeJson(value, '', new WeakSet<object>(), false);
|
||||
}
|
||||
/** Preserve only validated helper identifiers/hashes while redacting all content-bearing fields. */
|
||||
export function sanitizeHelperForJson(value:unknown):unknown{return sanitizeJson(value,'',new WeakSet<object>(),true);}
|
||||
export interface ProcessResult { code: number; stdout: string; stderr: string; timedOut: boolean; truncated: boolean; capturedBytes:number }
|
||||
interface GitConfigIdentity { path:string; exists:boolean; dev?:number; ino?:number; mode?:number; size?:number; mtimeMs?:number; ctimeMs?:number; content?:string }
|
||||
const GIT_CONFIG_LIMIT=1024*1024;
|
||||
interface BoundedMetadataFile { dev:number;ino:number;mode:number;nlink:number;size:number;mtimeMs:number;ctimeMs:number;content:string }
|
||||
function sameMetadataFile(left:BoundedMetadataFile|ReturnType<typeof lstatSync>,right:BoundedMetadataFile|ReturnType<typeof lstatSync>):boolean{
|
||||
return left.dev===right.dev&&left.ino===right.ino&&left.mode===right.mode&&left.nlink===right.nlink&&left.size===right.size&&left.mtimeMs===right.mtimeMs&&left.ctimeMs===right.ctimeMs;
|
||||
export function sanitizeHelperForJson(value: unknown): unknown {
|
||||
return sanitizeJson(value, '', new WeakSet<object>(), true);
|
||||
}
|
||||
function boundedMetadataFile(path:string,maxBytes:number,label:string,optional=false):BoundedMetadataFile|undefined{
|
||||
let before:ReturnType<typeof lstatSync>;
|
||||
try{before=lstatSync(path);}catch(error:any){if(optional&&error?.code==='ENOENT')return;throw new CsoError(error?.code==='ENOENT'?'SNAPSHOT_RACE':'UNSAFE_PATH',`${label} is not a bounded regular file`);}
|
||||
if(before.isSymbolicLink()||!before.isFile()||before.nlink!==1||before.size>maxBytes)throw new CsoError('UNSAFE_PATH',`${label} is not a bounded regular file`);
|
||||
let fd:number|undefined;
|
||||
try{
|
||||
fd=openSync(path,constants.O_RDONLY|(constants.O_NOFOLLOW??0)|(constants.O_NONBLOCK??0));
|
||||
const opened=fstatSync(fd);
|
||||
if(!opened.isFile()||opened.nlink!==1||opened.size>maxBytes||!sameMetadataFile(before,opened))throw new CsoError('SNAPSHOT_RACE',`${label} changed while it was opened`);
|
||||
const buffer=Buffer.alloc(Math.min(maxBytes+1,opened.size+1));let bytes=0,count=0;
|
||||
while(bytes<buffer.length&&(count=readSync(fd,buffer,bytes,buffer.length-bytes,null))>0)bytes+=count;
|
||||
const final=fstatSync(fd),after=lstatSync(path);
|
||||
if(bytes!==opened.size||!final.isFile()||!after.isFile()||after.isSymbolicLink()||!sameMetadataFile(opened,final)||!sameMetadataFile(opened,after))
|
||||
throw new CsoError('SNAPSHOT_RACE',`${label} changed while it was read`);
|
||||
return{dev:opened.dev,ino:opened.ino,mode:opened.mode,nlink:opened.nlink,size:opened.size,mtimeMs:opened.mtimeMs,ctimeMs:opened.ctimeMs,content:buffer.subarray(0,bytes).toString('utf8')};
|
||||
}catch(error:any){
|
||||
if(error instanceof CsoError)throw error;
|
||||
if(['ENOENT','ELOOP','ENXIO'].includes(error?.code))throw new CsoError('SNAPSHOT_RACE',`${label} changed while it was opened`);
|
||||
throw new CsoError('UNSAFE_PATH',`${label} could not be read safely`);
|
||||
}finally{if(fd!==undefined)try{closeSync(fd);}catch{}}
|
||||
export interface ProcessResult {
|
||||
code: number;
|
||||
stdout: string;
|
||||
stderr: string;
|
||||
timedOut: boolean;
|
||||
truncated: boolean;
|
||||
capturedBytes: number;
|
||||
}
|
||||
function boundedConfig(path:string):GitConfigIdentity{
|
||||
const file=boundedMetadataFile(path,GIT_CONFIG_LIMIT,'Repository Git configuration',true);
|
||||
if(!file)return{path,exists:false};
|
||||
const {content}=file;
|
||||
interface GitConfigIdentity {
|
||||
path: string;
|
||||
exists: boolean;
|
||||
dev?: number;
|
||||
ino?: number;
|
||||
mode?: number;
|
||||
size?: number;
|
||||
mtimeMs?: number;
|
||||
ctimeMs?: number;
|
||||
content?: string;
|
||||
}
|
||||
const GIT_CONFIG_LIMIT = 1024 * 1024;
|
||||
interface BoundedMetadataFile {
|
||||
dev: number;
|
||||
ino: number;
|
||||
mode: number;
|
||||
nlink: number;
|
||||
size: number;
|
||||
mtimeMs: number;
|
||||
ctimeMs: number;
|
||||
content: string;
|
||||
}
|
||||
function sameMetadataFile(left: BoundedMetadataFile | Stats, right: BoundedMetadataFile | Stats): boolean {
|
||||
return (
|
||||
left.dev === right.dev &&
|
||||
left.ino === right.ino &&
|
||||
left.mode === right.mode &&
|
||||
left.nlink === right.nlink &&
|
||||
left.size === right.size &&
|
||||
left.mtimeMs === right.mtimeMs &&
|
||||
left.ctimeMs === right.ctimeMs
|
||||
);
|
||||
}
|
||||
function boundedMetadataFile(
|
||||
path: string,
|
||||
maxBytes: number,
|
||||
label: string,
|
||||
optional = false,
|
||||
): BoundedMetadataFile | undefined {
|
||||
let before: Stats;
|
||||
try {
|
||||
before = lstatSync(path);
|
||||
} catch (error: any) {
|
||||
if (optional && error?.code === 'ENOENT') return;
|
||||
throw new CsoError(
|
||||
error?.code === 'ENOENT' ? 'SNAPSHOT_RACE' : 'UNSAFE_PATH',
|
||||
`${label} is not a bounded regular file`,
|
||||
);
|
||||
}
|
||||
if (before.isSymbolicLink() || !before.isFile() || before.nlink !== 1 || before.size > maxBytes)
|
||||
throw new CsoError('UNSAFE_PATH', `${label} is not a bounded regular file`);
|
||||
let fd: number | undefined;
|
||||
try {
|
||||
fd = openSync(path, constants.O_RDONLY | (constants.O_NOFOLLOW ?? 0) | (constants.O_NONBLOCK ?? 0));
|
||||
const opened = fstatSync(fd);
|
||||
if (!opened.isFile() || opened.nlink !== 1 || opened.size > maxBytes || !sameMetadataFile(before, opened))
|
||||
throw new CsoError('SNAPSHOT_RACE', `${label} changed while it was opened`);
|
||||
const buffer = Buffer.alloc(Math.min(maxBytes + 1, opened.size + 1));
|
||||
let bytes = 0,
|
||||
count = 0;
|
||||
while (bytes < buffer.length && (count = readSync(fd, buffer, bytes, buffer.length - bytes, null)) > 0)
|
||||
bytes += count;
|
||||
const final = fstatSync(fd),
|
||||
after = lstatSync(path);
|
||||
if (
|
||||
bytes !== opened.size ||
|
||||
!final.isFile() ||
|
||||
!after.isFile() ||
|
||||
after.isSymbolicLink() ||
|
||||
!sameMetadataFile(opened, final) ||
|
||||
!sameMetadataFile(opened, after)
|
||||
)
|
||||
throw new CsoError('SNAPSHOT_RACE', `${label} changed while it was read`);
|
||||
return {
|
||||
dev: opened.dev,
|
||||
ino: opened.ino,
|
||||
mode: opened.mode,
|
||||
nlink: opened.nlink,
|
||||
size: opened.size,
|
||||
mtimeMs: opened.mtimeMs,
|
||||
ctimeMs: opened.ctimeMs,
|
||||
content: buffer.subarray(0, bytes).toString('utf8'),
|
||||
};
|
||||
} catch (error: any) {
|
||||
if (error instanceof CsoError) throw error;
|
||||
if (['ENOENT', 'ELOOP', 'ENXIO'].includes(error?.code))
|
||||
throw new CsoError('SNAPSHOT_RACE', `${label} changed while it was opened`);
|
||||
throw new CsoError('UNSAFE_PATH', `${label} could not be read safely`);
|
||||
} finally {
|
||||
if (fd !== undefined)
|
||||
try {
|
||||
closeSync(fd);
|
||||
} catch {}
|
||||
}
|
||||
}
|
||||
function boundedConfig(path: string): GitConfigIdentity {
|
||||
const file = boundedMetadataFile(path, GIT_CONFIG_LIMIT, 'Repository Git configuration', true);
|
||||
if (!file) return { path, exists: false };
|
||||
const { content } = file;
|
||||
// There is no process-wide "--no-includes" switch for ordinary Git
|
||||
// commands. Reject include directives before spawning Git so repository
|
||||
// configuration cannot pull policy or executable settings from elsewhere.
|
||||
if(/^\s*\[\s*include(?:if)?(?=[\s."\]])/im.test(content))throw new CsoError('UNSAFE_PATH','Repository Git config includes are not allowed during a security snapshot');
|
||||
return{path,exists:true,dev:file.dev,ino:file.ino,mode:file.mode,size:file.size,mtimeMs:file.mtimeMs,ctimeMs:file.ctimeMs,content};
|
||||
if (/^\s*\[\s*include(?:if)?(?=[\s."\]])/im.test(content))
|
||||
throw new CsoError(
|
||||
'UNSAFE_PATH',
|
||||
'Repository Git config includes are not allowed during a security snapshot',
|
||||
);
|
||||
return {
|
||||
path,
|
||||
exists: true,
|
||||
dev: file.dev,
|
||||
ino: file.ino,
|
||||
mode: file.mode,
|
||||
size: file.size,
|
||||
mtimeMs: file.mtimeMs,
|
||||
ctimeMs: file.ctimeMs,
|
||||
content,
|
||||
};
|
||||
}
|
||||
function gitDirectories(repo:string):{gitDir:string;commonDir:string}{
|
||||
const marker=join(repo,'.git'),stat=lstatSync(marker);let gitDir:string;
|
||||
if(stat.isDirectory()&&!stat.isSymbolicLink())gitDir=realpathSync(marker);
|
||||
else if(stat.isFile()&&!stat.isSymbolicLink()&&stat.nlink===1&&stat.size<=8192){
|
||||
const value=boundedMetadataFile(marker,8192,'Repository .git pointer')!.content,match=value.match(/^gitdir:\s*(.+?)\s*$/);
|
||||
if(!match||value.includes('\0')||value.split(/\r?\n/).filter(Boolean).length!==1)throw new CsoError('UNSAFE_PATH','Repository .git pointer is invalid');
|
||||
gitDir=realpathSync(resolve(dirname(marker),match[1]));
|
||||
}else throw new CsoError('UNSAFE_PATH','Repository .git metadata is not a regular directory or worktree pointer');
|
||||
const commonMarker=join(gitDir,'commondir'),commonFile=boundedMetadataFile(commonMarker,8192,'Repository common Git directory pointer',true);let commonDir=gitDir;
|
||||
if(commonFile){
|
||||
const value=commonFile.content.trim();
|
||||
if(!value||value.includes('\0')||value.includes('\n')||value.includes('\r'))throw new CsoError('UNSAFE_PATH','Repository common Git directory pointer is invalid');
|
||||
commonDir=realpathSync(resolve(gitDir,value));
|
||||
function gitDirectories(repo: string): { gitDir: string; commonDir: string } {
|
||||
const marker = join(repo, '.git'),
|
||||
stat = lstatSync(marker);
|
||||
let gitDir: string;
|
||||
if (stat.isDirectory() && !stat.isSymbolicLink()) gitDir = realpathSync(marker);
|
||||
else if (stat.isFile() && !stat.isSymbolicLink() && stat.nlink === 1 && stat.size <= 8192) {
|
||||
const value = boundedMetadataFile(marker, 8192, 'Repository .git pointer')!.content,
|
||||
match = value.match(/^gitdir:\s*(.+?)\s*$/);
|
||||
if (!match || value.includes('\0') || value.split(/\r?\n/).filter(Boolean).length !== 1)
|
||||
throw new CsoError('UNSAFE_PATH', 'Repository .git pointer is invalid');
|
||||
gitDir = realpathSync(resolve(dirname(marker), match[1]));
|
||||
} else
|
||||
throw new CsoError(
|
||||
'UNSAFE_PATH',
|
||||
'Repository .git metadata is not a regular directory or worktree pointer',
|
||||
);
|
||||
const commonMarker = join(gitDir, 'commondir'),
|
||||
commonFile = boundedMetadataFile(commonMarker, 8192, 'Repository common Git directory pointer', true);
|
||||
let commonDir = gitDir;
|
||||
if (commonFile) {
|
||||
const value = commonFile.content.trim();
|
||||
if (!value || value.includes('\0') || value.includes('\n') || value.includes('\r'))
|
||||
throw new CsoError('UNSAFE_PATH', 'Repository common Git directory pointer is invalid');
|
||||
commonDir = realpathSync(resolve(gitDir, value));
|
||||
}
|
||||
return{gitDir,commonDir};
|
||||
return { gitDir, commonDir };
|
||||
}
|
||||
function gitConfigIdentities(repo:string):GitConfigIdentity[]{
|
||||
const {gitDir,commonDir}=gitDirectories(repo);
|
||||
function gitConfigIdentities(repo: string): GitConfigIdentity[] {
|
||||
const { gitDir, commonDir } = gitDirectories(repo);
|
||||
// extensions.worktreeConfig makes config.worktree active in both linked and
|
||||
// main worktrees. Bind even its absence so it cannot appear after inspection
|
||||
// and feed Git an unchecked include or executable setting.
|
||||
return [join(commonDir,'config'),join(gitDir,'config.worktree')].map(boundedConfig);
|
||||
return [join(commonDir, 'config'), join(gitDir, 'config.worktree')].map(boundedConfig);
|
||||
}
|
||||
function assertGitConfigIdentities(expected:GitConfigIdentity[]):void{
|
||||
for(const item of expected){
|
||||
const current=boundedConfig(item.path);
|
||||
if(current.exists!==item.exists||current.dev!==item.dev||current.ino!==item.ino||current.mode!==item.mode||current.size!==item.size||current.mtimeMs!==item.mtimeMs||current.ctimeMs!==item.ctimeMs||current.content!==item.content)
|
||||
throw new CsoError('SNAPSHOT_RACE','Repository Git configuration changed during a metadata operation');
|
||||
function assertGitConfigIdentities(expected: GitConfigIdentity[]): void {
|
||||
for (const item of expected) {
|
||||
const current = boundedConfig(item.path);
|
||||
if (
|
||||
current.exists !== item.exists ||
|
||||
current.dev !== item.dev ||
|
||||
current.ino !== item.ino ||
|
||||
current.mode !== item.mode ||
|
||||
current.size !== item.size ||
|
||||
current.mtimeMs !== item.mtimeMs ||
|
||||
current.ctimeMs !== item.ctimeMs ||
|
||||
current.content !== item.content
|
||||
)
|
||||
throw new CsoError('SNAPSHOT_RACE', 'Repository Git configuration changed during a metadata operation');
|
||||
}
|
||||
}
|
||||
function hardenGit(file:string,args:string[]):{args:string[];configs?:GitConfigIdentity[]}{
|
||||
if(!/^(?:git|git\.exe)$/i.test(basename(file)))return{args};
|
||||
let trusted:string;try{trusted=executable('git');}catch{return{args};}
|
||||
if(realpathSync(file)!==trusted)return{args};
|
||||
const positions=args.flatMap((value,index)=>value==='-C'?[index]:[]);
|
||||
if(positions.length!==1||positions[0]+1>=args.length)throw new CsoError('INVALID_ARGUMENT','CSO Git operations require exactly one audited working directory');
|
||||
const position=positions[0],requested=args[position+1];
|
||||
if(!isAbsolute(requested))throw new CsoError('INVALID_ARGUMENT','CSO Git operations require an absolute audited working directory');
|
||||
const repo=realpathSync(requested),stat=statSync(repo);
|
||||
if(!stat.isDirectory())throw new CsoError('MISSING_INPUT','Audited Git working directory is not a directory');
|
||||
const configs=gitConfigIdentities(repo),nullPath=process.platform==='win32'?'NUL':'/dev/null',
|
||||
function hardenGit(file: string, args: string[]): { args: string[]; configs?: GitConfigIdentity[] } {
|
||||
if (!/^(?:git|git\.exe)$/i.test(basename(file))) return { args };
|
||||
let trusted: string;
|
||||
try {
|
||||
trusted = executable('git');
|
||||
} catch {
|
||||
return { args };
|
||||
}
|
||||
if (realpathSync(file) !== trusted) return { args };
|
||||
const positions = args.flatMap((value, index) => (value === '-C' ? [index] : []));
|
||||
if (positions.length !== 1 || positions[0] + 1 >= args.length)
|
||||
throw new CsoError(
|
||||
'INVALID_ARGUMENT',
|
||||
'CSO Git operations require exactly one audited working directory',
|
||||
);
|
||||
const position = positions[0],
|
||||
requested = args[position + 1];
|
||||
if (!isAbsolute(requested))
|
||||
throw new CsoError(
|
||||
'INVALID_ARGUMENT',
|
||||
'CSO Git operations require an absolute audited working directory',
|
||||
);
|
||||
const repo = realpathSync(requested),
|
||||
stat = statSync(repo);
|
||||
if (!stat.isDirectory())
|
||||
throw new CsoError('MISSING_INPUT', 'Audited Git working directory is not a directory');
|
||||
const configs = gitConfigIdentities(repo),
|
||||
nullPath = process.platform === 'win32' ? 'NUL' : '/dev/null',
|
||||
// Git for Windows accepts NUL for ordinary file-valued settings, but its
|
||||
// config include machinery treats NUL as a failing include. Its MSYS path
|
||||
// layer maps /dev/null correctly for this one directive.
|
||||
includeNullPath=process.platform==='win32'?'/dev/null':nullPath;
|
||||
const prefix=args.slice(0,position),command=args.slice(position+2);
|
||||
return{configs,args:[...prefix,
|
||||
'--no-replace-objects',
|
||||
'-c','core.fsmonitor=false','-c',`core.hooksPath=${nullPath}`,'-c',`core.attributesFile=${nullPath}`,
|
||||
'-c',`core.excludesFile=${nullPath}`,'-c','core.ignoreCase=false','-c','core.precomposeUnicode=false',
|
||||
'-c','core.untrackedCache=false','-c',`include.path=${includeNullPath}`,'-c','core.pager=cat',
|
||||
'-C',repo,`--work-tree=${repo}`,...command]};
|
||||
includeNullPath = process.platform === 'win32' ? '/dev/null' : nullPath;
|
||||
const prefix = args.slice(0, position),
|
||||
command = args.slice(position + 2);
|
||||
return {
|
||||
configs,
|
||||
args: [
|
||||
...prefix,
|
||||
'--no-replace-objects',
|
||||
'-c',
|
||||
'core.fsmonitor=false',
|
||||
'-c',
|
||||
`core.hooksPath=${nullPath}`,
|
||||
'-c',
|
||||
`core.attributesFile=${nullPath}`,
|
||||
'-c',
|
||||
`core.excludesFile=${nullPath}`,
|
||||
'-c',
|
||||
'core.ignoreCase=false',
|
||||
'-c',
|
||||
'core.precomposeUnicode=false',
|
||||
'-c',
|
||||
'core.untrackedCache=false',
|
||||
'-c',
|
||||
`include.path=${includeNullPath}`,
|
||||
'-c',
|
||||
'core.pager=cat',
|
||||
'-C',
|
||||
repo,
|
||||
`--work-tree=${repo}`,
|
||||
...command,
|
||||
],
|
||||
};
|
||||
}
|
||||
export async function runProcess(file: string, args: string[], opts: {
|
||||
cwd: string; env: Record<string,string>; timeoutMs?: number; maxBytes?: number; input?: string;
|
||||
raw?: boolean; // Only for inert Git framing or private helper/Docker control JSON that is validated before use. Never print or persist raw results.
|
||||
}): Promise<ProcessResult> {
|
||||
if (!isAbsolute(file) || !isAbsolute(opts.cwd) || !existsSync(opts.cwd)) throw new CsoError('INVALID_ARGUMENT','Children require absolute executables and an existing trusted working directory');
|
||||
if (!args.every(a => typeof a === 'string' && !a.includes('\0'))) throw new CsoError('INVALID_ARGUMENT','Invalid child argument');
|
||||
const hardened=hardenGit(file,args);args=hardened.args;
|
||||
export async function runProcess(
|
||||
file: string,
|
||||
args: string[],
|
||||
opts: {
|
||||
cwd: string;
|
||||
env: Record<string, string>;
|
||||
timeoutMs?: number;
|
||||
maxBytes?: number;
|
||||
input?: string;
|
||||
raw?: boolean; // Only for inert Git framing or private helper/Docker control JSON that is validated before use. Never print or persist raw results.
|
||||
},
|
||||
): Promise<ProcessResult> {
|
||||
if (!isAbsolute(file) || !isAbsolute(opts.cwd) || !existsSync(opts.cwd))
|
||||
throw new CsoError(
|
||||
'INVALID_ARGUMENT',
|
||||
'Children require absolute executables and an existing trusted working directory',
|
||||
);
|
||||
if (!args.every((a) => typeof a === 'string' && !a.includes('\0')))
|
||||
throw new CsoError('INVALID_ARGUMENT', 'Invalid child argument');
|
||||
const hardened = hardenGit(file, args);
|
||||
args = hardened.args;
|
||||
const cap = Math.min(opts.maxBytes ?? MAX_OUTPUT, MAX_OUTPUT);
|
||||
return new Promise((resolve,reject) => {
|
||||
const child = spawn(file,args,{cwd:opts.cwd,env:opts.env,stdio:['pipe','pipe','pipe'],detached:process.platform !== 'win32'});
|
||||
const out: Buffer[] = [], err: Buffer[] = [], ordered:Buffer[]=[]; let bytes = 0, timedOut = false, truncated = false;
|
||||
const kill = () => { try { if (process.platform !== 'win32' && child.pid) process.kill(-child.pid,'SIGKILL'); else child.kill('SIGKILL'); } catch {} };
|
||||
const timer = setTimeout(() => { timedOut = true; kill(); }, Math.max(1,Math.min(opts.timeoutMs ?? 30_000,300_000)));
|
||||
return new Promise((resolve, reject) => {
|
||||
const child = spawn(file, args, {
|
||||
cwd: opts.cwd,
|
||||
env: opts.env,
|
||||
stdio: ['pipe', 'pipe', 'pipe'],
|
||||
detached: process.platform !== 'win32',
|
||||
});
|
||||
const out: Buffer[] = [],
|
||||
err: Buffer[] = [],
|
||||
ordered: Buffer[] = [];
|
||||
let bytes = 0,
|
||||
timedOut = false,
|
||||
truncated = false;
|
||||
const kill = () => {
|
||||
try {
|
||||
if (process.platform !== 'win32' && child.pid) process.kill(-child.pid, 'SIGKILL');
|
||||
else child.kill('SIGKILL');
|
||||
} catch {}
|
||||
};
|
||||
const timer = setTimeout(
|
||||
() => {
|
||||
timedOut = true;
|
||||
kill();
|
||||
},
|
||||
Math.max(1, Math.min(opts.timeoutMs ?? 30_000, 300_000)),
|
||||
);
|
||||
const capture = (target: Buffer[]) => (chunk: Buffer) => {
|
||||
bytes += chunk.length;
|
||||
if (bytes > cap) { truncated = true; kill(); return; }
|
||||
target.push(chunk);ordered.push(chunk);
|
||||
if (bytes > cap) {
|
||||
truncated = true;
|
||||
kill();
|
||||
return;
|
||||
}
|
||||
target.push(chunk);
|
||||
ordered.push(chunk);
|
||||
};
|
||||
child.stdout.on('data',capture(out)); child.stderr.on('data',capture(err));
|
||||
child.on('error',() => { clearTimeout(timer); reject(new CsoError('TOOL_UNAVAILABLE','Trusted child process could not start')); });
|
||||
child.on('close',code => {
|
||||
child.stdout.on('data', capture(out));
|
||||
child.stderr.on('data', capture(err));
|
||||
child.on('error', () => {
|
||||
clearTimeout(timer);
|
||||
reject(new CsoError('TOOL_UNAVAILABLE', 'Trusted child process could not start'));
|
||||
});
|
||||
child.on('close', (code) => {
|
||||
clearTimeout(timer);
|
||||
try {
|
||||
if(hardened.configs)assertGitConfigIdentities(hardened.configs);
|
||||
if (hardened.configs) assertGitConfigIdentities(hardened.configs);
|
||||
// Never expose a truncated tail: it might be the beginning of a secret.
|
||||
const stdout = truncated ? '[output withheld: size limit]' : Buffer.concat(out).toString('utf8');
|
||||
const stderr = truncated ? '' : Buffer.concat(err).toString('utf8');
|
||||
if(opts.raw){resolve({code:code ?? -1,stdout,stderr,timedOut,truncated,capturedBytes:bytes});return;}
|
||||
if (opts.raw) {
|
||||
resolve({ code: code ?? -1, stdout, stderr, timedOut, truncated, capturedBytes: bytes });
|
||||
return;
|
||||
}
|
||||
// A token may be split across stdout/stderr. Stream ordering is not
|
||||
// recoverable here, so scan both concatenation orders and withhold both
|
||||
// channels when either reveals a cross-stream sensitive span.
|
||||
const forward=stdout+stderr,reverse=stderr+stdout,chronological=Buffer.concat(ordered).toString('utf8');
|
||||
if([stdout,stderr,forward,reverse,chronological].some(value=>redact(value)!==value)){
|
||||
resolve({code:code ?? -1,stdout:'[sensitive process output redacted]',stderr:'',timedOut,truncated,capturedBytes:bytes});return;
|
||||
const forward = stdout + stderr,
|
||||
reverse = stderr + stdout,
|
||||
chronological = Buffer.concat(ordered).toString('utf8');
|
||||
if ([stdout, stderr, forward, reverse, chronological].some((value) => redact(value) !== value)) {
|
||||
resolve({
|
||||
code: code ?? -1,
|
||||
stdout: '[sensitive process output redacted]',
|
||||
stderr: '',
|
||||
timedOut,
|
||||
truncated,
|
||||
capturedBytes: bytes,
|
||||
});
|
||||
return;
|
||||
}
|
||||
resolve({code:code ?? -1,stdout,stderr,timedOut,truncated,capturedBytes:bytes});
|
||||
} catch (e) { reject(e); }
|
||||
resolve({ code: code ?? -1, stdout, stderr, timedOut, truncated, capturedBytes: bytes });
|
||||
} catch (e) {
|
||||
reject(e);
|
||||
}
|
||||
});
|
||||
child.stdin.on('error',() => {}); child.stdin.end(opts.input);
|
||||
child.stdin.on('error', () => {});
|
||||
child.stdin.end(opts.input);
|
||||
});
|
||||
}
|
||||
export async function git(repo: string, args: string[], home: string): Promise<string> {
|
||||
const result = await runProcess(executable('git'),['--no-optional-locks','-C',repo,...args],
|
||||
{cwd:home,env:childEnvironment(home),raw:true,timeoutMs:15_000});
|
||||
const result = await runProcess(executable('git'), ['--no-optional-locks', '-C', repo, ...args], {
|
||||
cwd: home,
|
||||
env: childEnvironment(home),
|
||||
raw: true,
|
||||
timeoutMs: 15_000,
|
||||
});
|
||||
if (result.code || result.timedOut || result.truncated) {
|
||||
// Git stderr and argv can contain repository paths, refs, and configured
|
||||
// content. Name only the fixed helper-owned operation and bounded process
|
||||
// outcome so native failures are actionable without exposing either.
|
||||
const knownOperations=new Set(['rev-parse','symbolic-ref','ls-files','ls-tree','log','merge-base']),operation=args.find(value=>knownOperations.has(value))??'metadata',
|
||||
phase=operation==='rev-parse'&&args.includes('--show-object-format')?'object-format':operation==='rev-parse'&&args.includes('--is-inside-work-tree')?'worktree-probe':operation,
|
||||
reason=/not a git repository|outside repository/i.test(result.stderr)?'repository unavailable':/dubious ownership/i.test(result.stderr)?'repository ownership rejected':/(?:bad|invalid|unable to read).*config|config (?:error|file)/i.test(result.stderr)?'configuration rejected':/unknown option|unknown switch|unrecognized option|usage:/i.test(result.stderr)?'unsupported invocation':/(?:cannot|could not|unable to) (?:chdir|change directory)|no such file or directory/i.test(result.stderr)?'path unavailable':'request rejected',
|
||||
outcome=result.timedOut?'timed out':result.truncated?'exceeded the output limit':`exited ${result.code}`;
|
||||
throw new CsoError('MISSING_INPUT',`Could not read bounded Git metadata: ${phase} ${outcome} (${reason}); source may not be a Git repository`);
|
||||
const knownOperations = new Set([
|
||||
'rev-parse',
|
||||
'symbolic-ref',
|
||||
'ls-files',
|
||||
'ls-tree',
|
||||
'log',
|
||||
'merge-base',
|
||||
]),
|
||||
operation = args.find((value) => knownOperations.has(value)) ?? 'metadata',
|
||||
phase =
|
||||
operation === 'rev-parse' && args.includes('--show-object-format')
|
||||
? 'object-format'
|
||||
: operation === 'rev-parse' && args.includes('--is-inside-work-tree')
|
||||
? 'worktree-probe'
|
||||
: operation,
|
||||
reason = /not a git repository|outside repository/i.test(result.stderr)
|
||||
? 'repository unavailable'
|
||||
: /dubious ownership/i.test(result.stderr)
|
||||
? 'repository ownership rejected'
|
||||
: /(?:bad|invalid|unable to read).*config|config (?:error|file)/i.test(result.stderr)
|
||||
? 'configuration rejected'
|
||||
: /unknown option|unknown switch|unrecognized option|usage:/i.test(result.stderr)
|
||||
? 'unsupported invocation'
|
||||
: /(?:cannot|could not|unable to) (?:chdir|change directory)|no such file or directory/i.test(
|
||||
result.stderr,
|
||||
)
|
||||
? 'path unavailable'
|
||||
: 'request rejected',
|
||||
outcome = result.timedOut
|
||||
? 'timed out'
|
||||
: result.truncated
|
||||
? 'exceeded the output limit'
|
||||
: `exited ${result.code}`;
|
||||
throw new CsoError(
|
||||
'MISSING_INPUT',
|
||||
`Could not read bounded Git metadata: ${phase} ${outcome} (${reason}); source may not be a Git repository`,
|
||||
);
|
||||
}
|
||||
return result.stdout;
|
||||
}
|
||||
+218
-61
@@ -13,10 +13,23 @@ interface RuntimeQualificationProvenance {
|
||||
provenanceDigest: string;
|
||||
verifiedProvenance: true;
|
||||
}
|
||||
export type RuntimeQualification = RuntimeQualificationProvenance & (
|
||||
| { kind: 'application'; containmentPassed: true; coldStartPassed: true; positiveNegativeAssertionsPassed: true; heldOutRepairPassed: true }
|
||||
| { kind: 'postgresql'; containmentPassed: true; coldStartPassed: true; multiDatabasePassed: true; readinessPassed: true }
|
||||
);
|
||||
export type RuntimeQualification = RuntimeQualificationProvenance &
|
||||
(
|
||||
| {
|
||||
kind: 'application';
|
||||
containmentPassed: true;
|
||||
coldStartPassed: true;
|
||||
positiveNegativeAssertionsPassed: true;
|
||||
heldOutRepairPassed: true;
|
||||
}
|
||||
| {
|
||||
kind: 'postgresql';
|
||||
containmentPassed: true;
|
||||
coldStartPassed: true;
|
||||
multiDatabasePassed: true;
|
||||
readinessPassed: true;
|
||||
}
|
||||
);
|
||||
export interface QualifiedRuntime {
|
||||
id: string;
|
||||
stack: CsoStack | 'postgresql';
|
||||
@@ -76,46 +89,96 @@ function versionsKey(versions: Record<string, string>): string {
|
||||
return JSON.stringify(Object.entries(versions).sort(([a], [b]) => a.localeCompare(b)));
|
||||
}
|
||||
|
||||
function validateRuntimeIdentity(value: { id: string; stack: string; platform: string; versions: Record<string, string> }): void {
|
||||
function validateRuntimeIdentity(value: {
|
||||
id: string;
|
||||
stack: string;
|
||||
platform: string;
|
||||
versions: Record<string, string>;
|
||||
}): void {
|
||||
if (typeof value.id !== 'string' || !ID.test(value.id)) throw new Error('INVALID_RUNTIME_ID');
|
||||
if (!STACKS.includes(value.stack as typeof STACKS[number]) || !PLATFORMS.includes(value.platform as RuntimePlatform)) throw new Error('UNSUPPORTED_RUNTIME_PLATFORM');
|
||||
if (!value.versions || typeof value.versions !== 'object' || Array.isArray(value.versions) || !Object.keys(value.versions).length ||
|
||||
Object.values(value.versions).some(version => typeof version !== 'string' || !/^[0-9][a-zA-Z0-9.+_-]*$/.test(version))) throw new Error('UNPINNED_RUNTIME_VERSION');
|
||||
if (Object.keys(value.versions).sort().join(',') !== [...REQUIRED[value.stack]].sort().join(',')) throw new Error('MISSING_RUNTIME_TOOL_VERSION');
|
||||
if (['node', 'bun', 'python', 'rails'].includes(value.stack) && value.versions['cso-preparation'] !== '1.0.0') throw new Error('INCOMPATIBLE_PREPARATION_HELPER');
|
||||
if (
|
||||
!STACKS.includes(value.stack as (typeof STACKS)[number]) ||
|
||||
!PLATFORMS.includes(value.platform as RuntimePlatform)
|
||||
)
|
||||
throw new Error('UNSUPPORTED_RUNTIME_PLATFORM');
|
||||
if (
|
||||
!value.versions ||
|
||||
typeof value.versions !== 'object' ||
|
||||
Array.isArray(value.versions) ||
|
||||
!Object.keys(value.versions).length ||
|
||||
Object.values(value.versions).some(
|
||||
(version) => typeof version !== 'string' || !/^[0-9][a-zA-Z0-9.+_-]*$/.test(version),
|
||||
)
|
||||
)
|
||||
throw new Error('UNPINNED_RUNTIME_VERSION');
|
||||
if (Object.keys(value.versions).sort().join(',') !== [...REQUIRED[value.stack]].sort().join(','))
|
||||
throw new Error('MISSING_RUNTIME_TOOL_VERSION');
|
||||
if (
|
||||
['node', 'bun', 'python', 'rails'].includes(value.stack) &&
|
||||
value.versions['cso-preparation'] !== '1.0.0'
|
||||
)
|
||||
throw new Error('INCOMPATIBLE_PREPARATION_HELPER');
|
||||
}
|
||||
|
||||
export function validateRuntimeCatalog(value: unknown): asserts value is RuntimeCatalog {
|
||||
const catalog = value as RuntimeCatalog;
|
||||
if (!catalog || catalog.schemaVersion !== 1 || catalog.helperAbi !== CSO_HELPER_ABI ||
|
||||
typeof catalog.revision !== 'string' || !BUILD_REVISION.test(catalog.revision) || !Array.isArray(catalog.runtimes)) throw new Error('INCOMPATIBLE_RUNTIME_CATALOG');
|
||||
if (catalog.previousRevision !== null && (typeof catalog.previousRevision !== 'string' || !BUILD_REVISION.test(catalog.previousRevision))) throw new Error('INVALID_RUNTIME_CATALOG');
|
||||
if (
|
||||
!catalog ||
|
||||
catalog.schemaVersion !== 1 ||
|
||||
catalog.helperAbi !== CSO_HELPER_ABI ||
|
||||
typeof catalog.revision !== 'string' ||
|
||||
!BUILD_REVISION.test(catalog.revision) ||
|
||||
!Array.isArray(catalog.runtimes)
|
||||
)
|
||||
throw new Error('INCOMPATIBLE_RUNTIME_CATALOG');
|
||||
if (
|
||||
catalog.previousRevision !== null &&
|
||||
(typeof catalog.previousRevision !== 'string' || !BUILD_REVISION.test(catalog.previousRevision))
|
||||
)
|
||||
throw new Error('INVALID_RUNTIME_CATALOG');
|
||||
|
||||
if (!Array.isArray(catalog.profiles) || catalog.profiles.length !== STACKS.length * PLATFORMS.length ||
|
||||
typeof catalog.buildRevision !== 'string' || !BUILD_REVISION.test(catalog.buildRevision)) throw new Error('INVALID_REVIEWED_RUNTIME_PROFILES');
|
||||
const profiles = new Map<string, ReviewedRuntimeProfile>(), profileIdentities = new Set<string>();
|
||||
if (
|
||||
!Array.isArray(catalog.profiles) ||
|
||||
catalog.profiles.length !== STACKS.length * PLATFORMS.length ||
|
||||
typeof catalog.buildRevision !== 'string' ||
|
||||
!BUILD_REVISION.test(catalog.buildRevision)
|
||||
)
|
||||
throw new Error('INVALID_REVIEWED_RUNTIME_PROFILES');
|
||||
const profiles = new Map<string, ReviewedRuntimeProfile>(),
|
||||
profileIdentities = new Set<string>();
|
||||
for (const profile of catalog.profiles) {
|
||||
validateRuntimeIdentity(profile);
|
||||
const identity = `${profile.stack}:${profile.platform}`;
|
||||
if (profiles.has(profile.id) || profileIdentities.has(identity) || profile.state !== 'build_reviewed' ||
|
||||
!Number.isFinite(Date.parse(profile.reviewedAt))) throw new Error('INVALID_REVIEWED_RUNTIME_PROFILE');
|
||||
profiles.set(profile.id, profile); profileIdentities.add(identity);
|
||||
}
|
||||
for (const stack of STACKS) for (const platform of PLATFORMS) {
|
||||
if (!profileIdentities.has(`${stack}:${platform}`)) throw new Error('INCOMPLETE_REVIEWED_RUNTIME_MATRIX');
|
||||
if (
|
||||
profiles.has(profile.id) ||
|
||||
profileIdentities.has(identity) ||
|
||||
profile.state !== 'build_reviewed' ||
|
||||
!Number.isFinite(Date.parse(profile.reviewedAt))
|
||||
)
|
||||
throw new Error('INVALID_REVIEWED_RUNTIME_PROFILE');
|
||||
profiles.set(profile.id, profile);
|
||||
profileIdentities.add(identity);
|
||||
}
|
||||
for (const stack of STACKS)
|
||||
for (const platform of PLATFORMS) {
|
||||
if (!profileIdentities.has(`${stack}:${platform}`))
|
||||
throw new Error('INCOMPLETE_REVIEWED_RUNTIME_MATRIX');
|
||||
}
|
||||
if (catalog.promotion !== undefined) {
|
||||
if (!/^[a-f0-9]{40}$/.test(catalog.promotion.sourceCommit) ||
|
||||
if (
|
||||
!/^[a-f0-9]{40}$/.test(catalog.promotion.sourceCommit) ||
|
||||
!QUALIFICATION_WORKFLOW.test(catalog.promotion.workflow) ||
|
||||
!DIGEST.test(catalog.promotion.evidenceDigest) ||
|
||||
!DIGEST.test(catalog.promotion.qualificationEvidenceDigest) ||
|
||||
Object.keys(catalog.promotion).sort().join(',') !==
|
||||
['evidenceDigest', 'qualificationEvidenceDigest', 'sourceCommit', 'workflow'].sort().join(',')) {
|
||||
['evidenceDigest', 'qualificationEvidenceDigest', 'sourceCommit', 'workflow'].sort().join(',')
|
||||
) {
|
||||
throw new Error('INVALID_RUNTIME_PROMOTION');
|
||||
}
|
||||
}
|
||||
|
||||
if (catalog.runtimes.length !== 0 && catalog.runtimes.length !== STACKS.length * PLATFORMS.length) throw new Error('INCOMPLETE_QUALIFIED_RUNTIME_MATRIX');
|
||||
if (catalog.runtimes.length !== 0 && catalog.runtimes.length !== STACKS.length * PLATFORMS.length)
|
||||
throw new Error('INCOMPLETE_QUALIFIED_RUNTIME_MATRIX');
|
||||
const ids = new Set<string>();
|
||||
const runtimeIdentities = new Set<string>();
|
||||
for (const runtime of catalog.runtimes) {
|
||||
@@ -124,77 +187,171 @@ export function validateRuntimeCatalog(value: unknown): asserts value is Runtime
|
||||
validateRuntimeIdentity(runtime);
|
||||
const identity = `${runtime.stack}:${runtime.platform}`;
|
||||
if (ids.has(runtime.id) || runtimeIdentities.has(identity)) throw new Error('INVALID_RUNTIME_ID');
|
||||
ids.add(runtime.id); runtimeIdentities.add(identity);
|
||||
ids.add(runtime.id);
|
||||
runtimeIdentities.add(identity);
|
||||
const arch = runtime.platform === 'linux/amd64' ? 'amd64' : 'arm64';
|
||||
const expectedImage = new RegExp(`^ghcr\\.io/garrytan/gstack/cso-staging/${runtime.stack}-${arch}@sha256:[a-f0-9]{64}$`);
|
||||
if (runtime.state !== 'qualified' || !IMAGE.test(runtime.image) || !expectedImage.test(runtime.image) || runtime.entrypoint !== '/opt/cso/entrypoint' ||
|
||||
runtime.helperAbi !== CSO_HELPER_ABI || runtime.policyVersion !== 'cso-isolation-v1') throw new Error('UNQUALIFIED_RUNTIME');
|
||||
const expectedImage = new RegExp(
|
||||
`^ghcr\\.io/garrytan/gstack/cso-staging/${runtime.stack}-${arch}@sha256:[a-f0-9]{64}$`,
|
||||
);
|
||||
if (
|
||||
runtime.state !== 'qualified' ||
|
||||
!IMAGE.test(runtime.image) ||
|
||||
!expectedImage.test(runtime.image) ||
|
||||
runtime.entrypoint !== '/opt/cso/entrypoint' ||
|
||||
runtime.helperAbi !== CSO_HELPER_ABI ||
|
||||
runtime.policyVersion !== 'cso-isolation-v1'
|
||||
)
|
||||
throw new Error('UNQUALIFIED_RUNTIME');
|
||||
const reviewed = profiles.get(runtime.id);
|
||||
if (!reviewed || reviewed.stack !== runtime.stack || reviewed.platform !== runtime.platform ||
|
||||
versionsKey(reviewed.versions) !== versionsKey(runtime.versions)) throw new Error('RUNTIME_BUILD_PROFILE_MISMATCH');
|
||||
if (!qualification || !/^[a-f0-9]{40}$/.test(qualification.sourceCommit) ||
|
||||
if (
|
||||
!reviewed ||
|
||||
reviewed.stack !== runtime.stack ||
|
||||
reviewed.platform !== runtime.platform ||
|
||||
versionsKey(reviewed.versions) !== versionsKey(runtime.versions)
|
||||
)
|
||||
throw new Error('RUNTIME_BUILD_PROFILE_MISMATCH');
|
||||
if (
|
||||
!qualification ||
|
||||
!/^[a-f0-9]{40}$/.test(qualification.sourceCommit) ||
|
||||
!QUALIFICATION_WORKFLOW.test(qualification.workflow) ||
|
||||
!DIGEST.test(qualification.sbomDigest) || !DIGEST.test(qualification.provenanceDigest) || qualification.verifiedProvenance !== true ||
|
||||
!Number.isFinite(Date.parse(runtime.qualifiedAt))) throw new Error('MISSING_RUNTIME_QUALIFICATION');
|
||||
!DIGEST.test(qualification.sbomDigest) ||
|
||||
!DIGEST.test(qualification.provenanceDigest) ||
|
||||
qualification.verifiedProvenance !== true ||
|
||||
!Number.isFinite(Date.parse(runtime.qualifiedAt))
|
||||
)
|
||||
throw new Error('MISSING_RUNTIME_QUALIFICATION');
|
||||
const keys = Object.keys(qualification).sort();
|
||||
const common = ['kind', 'sourceCommit', 'workflow', 'sbomDigest', 'provenanceDigest', 'verifiedProvenance'];
|
||||
const common = [
|
||||
'kind',
|
||||
'sourceCommit',
|
||||
'workflow',
|
||||
'sbomDigest',
|
||||
'provenanceDigest',
|
||||
'verifiedProvenance',
|
||||
];
|
||||
if (['node', 'bun', 'python', 'rails'].includes(runtime.stack)) {
|
||||
if (qualification.kind !== 'application' || qualification.containmentPassed !== true || qualification.coldStartPassed !== true ||
|
||||
qualification.positiveNegativeAssertionsPassed !== true || qualification.heldOutRepairPassed !== true ||
|
||||
keys.join(',') !== [...common, 'containmentPassed', 'coldStartPassed', 'positiveNegativeAssertionsPassed', 'heldOutRepairPassed'].sort().join(',')) throw new Error('MISSING_APPLICATION_QUALIFICATION');
|
||||
if (
|
||||
qualification.kind !== 'application' ||
|
||||
qualification.containmentPassed !== true ||
|
||||
qualification.coldStartPassed !== true ||
|
||||
qualification.positiveNegativeAssertionsPassed !== true ||
|
||||
qualification.heldOutRepairPassed !== true ||
|
||||
keys.join(',') !==
|
||||
[
|
||||
...common,
|
||||
'containmentPassed',
|
||||
'coldStartPassed',
|
||||
'positiveNegativeAssertionsPassed',
|
||||
'heldOutRepairPassed',
|
||||
]
|
||||
.sort()
|
||||
.join(',')
|
||||
)
|
||||
throw new Error('MISSING_APPLICATION_QUALIFICATION');
|
||||
} else {
|
||||
if (qualification.kind !== 'postgresql' || qualification.containmentPassed !== true || qualification.coldStartPassed !== true ||
|
||||
qualification.multiDatabasePassed !== true || qualification.readinessPassed !== true ||
|
||||
keys.join(',') !== [...common, 'containmentPassed', 'coldStartPassed', 'multiDatabasePassed', 'readinessPassed'].sort().join(',')) throw new Error('MISSING_POSTGRESQL_QUALIFICATION');
|
||||
if (
|
||||
qualification.kind !== 'postgresql' ||
|
||||
qualification.containmentPassed !== true ||
|
||||
qualification.coldStartPassed !== true ||
|
||||
qualification.multiDatabasePassed !== true ||
|
||||
qualification.readinessPassed !== true ||
|
||||
keys.join(',') !==
|
||||
[...common, 'containmentPassed', 'coldStartPassed', 'multiDatabasePassed', 'readinessPassed']
|
||||
.sort()
|
||||
.join(',')
|
||||
)
|
||||
throw new Error('MISSING_POSTGRESQL_QUALIFICATION');
|
||||
}
|
||||
}
|
||||
if (catalog.runtimes.length > 0) {
|
||||
for (const identity of profileIdentities) if (!runtimeIdentities.has(identity)) throw new Error('INCOMPLETE_QUALIFIED_RUNTIME_MATRIX');
|
||||
for (const identity of profileIdentities)
|
||||
if (!runtimeIdentities.has(identity)) throw new Error('INCOMPLETE_QUALIFIED_RUNTIME_MATRIX');
|
||||
if (!catalog.promotion) throw new Error('MISSING_RUNTIME_PROMOTION');
|
||||
if (catalog.runtimes.some(runtime => runtime.qualification.sourceCommit !== catalog.promotion!.sourceCommit ||
|
||||
runtime.qualification.workflow !== catalog.promotion!.workflow)) throw new Error('RUNTIME_PROMOTION_MISMATCH');
|
||||
if (
|
||||
catalog.runtimes.some(
|
||||
(runtime) =>
|
||||
runtime.qualification.sourceCommit !== catalog.promotion!.sourceCommit ||
|
||||
runtime.qualification.workflow !== catalog.promotion!.workflow,
|
||||
)
|
||||
)
|
||||
throw new Error('RUNTIME_PROMOTION_MISMATCH');
|
||||
if (catalog.promotion.evidenceDigest !== `sha256:${sha256(canonical(catalog.runtimes))}`) {
|
||||
throw new Error('RUNTIME_PROMOTION_EVIDENCE_MISMATCH');
|
||||
}
|
||||
} else if (catalog.promotion) throw new Error('INVALID_RUNTIME_PROMOTION');
|
||||
}
|
||||
|
||||
export const RUNTIME_CATALOG = committedCatalog as RuntimeCatalog;
|
||||
validateRuntimeCatalog(RUNTIME_CATALOG);
|
||||
const committed: unknown = committedCatalog;
|
||||
validateRuntimeCatalog(committed);
|
||||
export const RUNTIME_CATALOG: RuntimeCatalog = committed;
|
||||
|
||||
export function assertRuntimeCompatible(plan: PreparationPlan, runtime: QualifiedRuntime): void {
|
||||
if (plan.schemaVersion !== 1 || plan.status !== 'ready' || runtime.stack !== plan.stack) throw new CsoError('INCOMPATIBLE_INPUT', `Prepared ${plan.stack} source cannot run in ${runtime.stack} runtime ${runtime.id}`);
|
||||
if (plan.schemaVersion !== 1 || plan.status !== 'ready' || runtime.stack !== plan.stack)
|
||||
throw new CsoError(
|
||||
'INCOMPATIBLE_INPUT',
|
||||
`Prepared ${plan.stack} source cannot run in ${runtime.stack} runtime ${runtime.id}`,
|
||||
);
|
||||
for (const [declared, rawRange] of Object.entries(plan.runtimeRequirements)) {
|
||||
if (!rawRange) continue;
|
||||
let tool = declared, range = rawRange;
|
||||
let tool = declared,
|
||||
range = rawRange;
|
||||
if (declared === 'packageManager') {
|
||||
const match = rawRange.match(/^([a-z][a-z0-9_-]*)@(.+)$/i);
|
||||
if (!match) throw new CsoError('PREREQUISITE', 'Package manager declaration must bind a named version range');
|
||||
tool = match[1]; range = match[2];
|
||||
if (!match)
|
||||
throw new CsoError('PREREQUISITE', 'Package manager declaration must bind a named version range');
|
||||
tool = match[1];
|
||||
range = match[2];
|
||||
}
|
||||
const version = runtime.versions[tool];
|
||||
if (!version) throw new CsoError('PREREQUISITE', `Qualified runtime ${runtime.id} does not declare a real ${tool} release`);
|
||||
if (!version)
|
||||
throw new CsoError(
|
||||
'PREREQUISITE',
|
||||
`Qualified runtime ${runtime.id} does not declare a real ${tool} release`,
|
||||
);
|
||||
let satisfies = false;
|
||||
try { satisfies = Bun.semver.satisfies(version.replace(/^v/, ''), range); } catch {}
|
||||
if (!satisfies) throw new CsoError('PREREQUISITE', `Qualified ${tool} ${version} does not satisfy source requirement ${range}`);
|
||||
try {
|
||||
satisfies = Bun.semver.satisfies(version.replace(/^v/, ''), range);
|
||||
} catch {}
|
||||
if (!satisfies)
|
||||
throw new CsoError(
|
||||
'PREREQUISITE',
|
||||
`Qualified ${tool} ${version} does not satisfy source requirement ${range}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export function selectRuntime(profile: string, platform: RuntimePlatform, catalog: RuntimeCatalog = RUNTIME_CATALOG): QualifiedRuntime {
|
||||
export function selectRuntime(
|
||||
profile: string,
|
||||
platform: RuntimePlatform,
|
||||
catalog: RuntimeCatalog = RUNTIME_CATALOG,
|
||||
): QualifiedRuntime {
|
||||
validateRuntimeCatalog(catalog);
|
||||
const matches = catalog.runtimes.filter(runtime => runtime.platform === platform && (runtime.id === profile || runtime.stack === profile));
|
||||
const matches = catalog.runtimes.filter(
|
||||
(runtime) => runtime.platform === platform && (runtime.id === profile || runtime.stack === profile),
|
||||
);
|
||||
if (matches.length === 0) {
|
||||
const reviewed = catalog.profiles?.filter(item => item.platform === platform && (item.id === profile || item.stack === profile)) ?? [];
|
||||
const detail = reviewed.length === 1 ? ` Reviewed build profile ${reviewed[0].id} is awaiting a qualified image promotion.` : '';
|
||||
throw new Error(`MISSING_QUALIFIED_RUNTIME: ${profile} on ${platform}; build, qualify, and review a digest catalog before target execution.${detail}`);
|
||||
const reviewed =
|
||||
catalog.profiles?.filter(
|
||||
(item) => item.platform === platform && (item.id === profile || item.stack === profile),
|
||||
) ?? [];
|
||||
const detail =
|
||||
reviewed.length === 1
|
||||
? ` Reviewed build profile ${reviewed[0].id} is awaiting a qualified image promotion.`
|
||||
: '';
|
||||
throw new Error(
|
||||
`MISSING_QUALIFIED_RUNTIME: ${profile} on ${platform}; build, qualify, and review a digest catalog before target execution.${detail}`,
|
||||
);
|
||||
}
|
||||
if (matches.length !== 1) throw new Error(`AMBIGUOUS_RUNTIME: select an exact qualified runtime id for ${profile}.`);
|
||||
if (matches.length !== 1)
|
||||
throw new Error(`AMBIGUOUS_RUNTIME: select an exact qualified runtime id for ${profile}.`);
|
||||
return matches[0];
|
||||
}
|
||||
|
||||
/** Rollback only pairs the previous catalog with a compatible helper; reports have their own schema. */
|
||||
export function rollbackCatalog(current: RuntimeCatalog, previous: RuntimeCatalog): RuntimeCatalog {
|
||||
validateRuntimeCatalog(current); validateRuntimeCatalog(previous);
|
||||
if (current.previousRevision !== previous.revision || current.helperAbi !== previous.helperAbi) throw new Error('INCOMPATIBLE_RUNTIME_ROLLBACK');
|
||||
validateRuntimeCatalog(current);
|
||||
validateRuntimeCatalog(previous);
|
||||
if (current.previousRevision !== previous.revision || current.helperAbi !== previous.helperAbi)
|
||||
throw new Error('INCOMPATIBLE_RUNTIME_ROLLBACK');
|
||||
return previous;
|
||||
}
|
||||
+162
-39
@@ -54,8 +54,15 @@ const IMAGE = /^(?:[a-z0-9.-]+(?::[0-9]+)?\/)?[a-z0-9][a-z0-9._/-]*@sha256:[a-f0
|
||||
const ID = /^[a-z0-9][a-z0-9._-]{0,100}$/;
|
||||
const QUALIFICATION_WORKFLOW = /^https:\/\/github\.com\/garrytan\/gstack\/actions\/runs\/[0-9]+$/;
|
||||
const PLATFORMS: RuntimePlatform[] = ['linux/amd64', 'linux/arm64'];
|
||||
const path = (s: unknown, prefix: string): s is string => typeof s === 'string' && s.startsWith(prefix) && !/[\x00-\x20\\,]/.test(s) && !s.split('/').some(x => x === '..' || x === '.') && !s.includes('//');
|
||||
function invalid(message: string): never { throw new CsoError('INCOMPATIBLE_INPUT', message); }
|
||||
const path = (s: unknown, prefix: string): s is string =>
|
||||
typeof s === 'string' &&
|
||||
s.startsWith(prefix) &&
|
||||
!/[\x00-\x20\\,]/.test(s) &&
|
||||
!s.split('/').some((x) => x === '..' || x === '.') &&
|
||||
!s.includes('//');
|
||||
function invalid(message: string): never {
|
||||
throw new CsoError('INCOMPATIBLE_INPUT', message);
|
||||
}
|
||||
function sameStrings(left: string[], right: string[]): boolean {
|
||||
return canonical([...left].sort()) === canonical([...right].sort());
|
||||
}
|
||||
@@ -68,63 +75,179 @@ export function scannerVersionHash(stdout: string, stderr = ''): string {
|
||||
* version to be one complete version token. A substring such as `1.2.3` in
|
||||
* `11.2.3`, `1.2.30`, or `1.2.3-dev` is not qualification evidence.
|
||||
*/
|
||||
export function assertScannerVersionOutput(scanner:ScannerId,version:string,stdout:string,stderr=''):void{
|
||||
if(!/^[0-9][A-Za-z0-9.+_-]{0,100}$/.test(version))invalid('Scanner version evidence has an invalid expected version');
|
||||
const output=`${stdout}\n${stderr}`;
|
||||
if(Buffer.byteLength(stdout)+Buffer.byteLength(stderr)>8192)invalid('Scanner version evidence exceeds the bounded output limit');
|
||||
const escaped=version.replace(/[.*+?^${}()|[\]\\]/g,'\\$&');
|
||||
const labels:Record<ScannerId,string>={gitleaks:'gitleaks',osv:'(?:osv|osv-scanner)',semgrep:'semgrep',zizmor:'zizmor',trivy:'trivy',schemathesis:'schemathesis'};
|
||||
const primary=output.split(/\r?\n/).map(line=>line.trim()).find(Boolean)??'';
|
||||
const exact=new RegExp(`^(?:v?${escaped}|${labels[scanner]},?\\s+(?:version\\s*:?\\s*)?v?${escaped}|version\\s*:\\s*v?${escaped})$`,'i');
|
||||
if(!exact.test(primary))invalid('Scanner primary version output does not match the exact catalog version');
|
||||
export function assertScannerVersionOutput(
|
||||
scanner: ScannerId,
|
||||
version: string,
|
||||
stdout: string,
|
||||
stderr = '',
|
||||
): void {
|
||||
if (!/^[0-9][A-Za-z0-9.+_-]{0,100}$/.test(version))
|
||||
invalid('Scanner version evidence has an invalid expected version');
|
||||
const output = `${stdout}\n${stderr}`;
|
||||
if (Buffer.byteLength(stdout) + Buffer.byteLength(stderr) > 8192)
|
||||
invalid('Scanner version evidence exceeds the bounded output limit');
|
||||
const escaped = version.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
||||
const labels: Record<ScannerId, string> = {
|
||||
gitleaks: 'gitleaks',
|
||||
osv: '(?:osv|osv-scanner)',
|
||||
semgrep: 'semgrep',
|
||||
zizmor: 'zizmor',
|
||||
trivy: 'trivy',
|
||||
schemathesis: 'schemathesis',
|
||||
};
|
||||
const primary =
|
||||
output
|
||||
.split(/\r?\n/)
|
||||
.map((line) => line.trim())
|
||||
.find(Boolean) ?? '';
|
||||
const exact = new RegExp(
|
||||
`^(?:v?${escaped}|${labels[scanner]},?\\s+(?:version\\s*:?\\s*)?v?${escaped}|version\\s*:\\s*v?${escaped})$`,
|
||||
'i',
|
||||
);
|
||||
if (!exact.test(primary))
|
||||
invalid('Scanner primary version output does not match the exact catalog version');
|
||||
}
|
||||
export function validateQualifiedScanner(s: QualifiedScanner): void {
|
||||
if (!SCANNER_IDS.includes(s.scanner) || !['linux/amd64', 'linux/arm64'].includes(s.platform)) invalid('Unsupported scanner or platform');
|
||||
const arch = s.platform === 'linux/amd64' ? 'amd64' : 'arm64';
|
||||
const expectedImage = new RegExp(`^ghcr\\.io/garrytan/gstack/cso-scanners/${s.scanner}-${arch}@sha256:[a-f0-9]{64}$`);
|
||||
if (s.state !== 'qualified' || !IMAGE.test(s.image) || !expectedImage.test(s.image) || s.entrypoint !== '/opt/cso/entrypoint' || s.helperAbi !== ABI || s.isolationPolicyHash !== ISOLATION_POLICY_HASH) invalid('Scanner profile is not qualified for this helper isolation policy');
|
||||
if (s.executable !== '/opt/cso/bin/scanner' || !/^[0-9][A-Za-z0-9.+_-]{0,100}$/.test(s.version) || !HASH.test(s.versionOutputSha256)) invalid('Scanner executable and version must be pinned');
|
||||
if (!Array.isArray(s.capabilities) || !s.capabilities.length || s.capabilities.length > 100 || s.capabilities.some(x => typeof x !== 'string' || !x || x.length > 100)) invalid('Scanner capabilities must be reviewed');
|
||||
const required = scannerPlans({ snapshotRoot: '/source', offline: true, selected: [s.scanner] })[0].requiredFeatures;
|
||||
if (!sameStrings(s.capabilities, required)) invalid('Scanner capabilities do not match the helper adapter contract');
|
||||
const rules = s.assets?.semgrepRules, db = s.assets?.advisoryDatabase;
|
||||
if (rules && (s.scanner !== 'semgrep' || !path(rules.path, '/policy/catalog/') || !HASH.test(rules.sha256))) invalid('Invalid immutable Semgrep rules');
|
||||
if (db && (!['osv', 'trivy'].includes(s.scanner) || !path(db.path, '/opt/cso/scanner-data/') || !HASH.test(db.contentSha256) || !Number.isFinite(Date.parse(db.updatedAt)) || !Array.isArray(db.ecosystems) || !db.ecosystems.length || db.ecosystems.some(x => typeof x !== 'string' || !x || x.length > 100))) invalid('Invalid immutable scanner database');
|
||||
if (s.scanner === 'semgrep' && !rules) invalid('Qualified Semgrep profiles require an immutable rules bundle');
|
||||
if (['osv', 'trivy'].includes(s.scanner) && !db) invalid(`Qualified ${s.scanner} profiles require an immutable offline database`);
|
||||
const q = s.qualification;
|
||||
if (!Number.isFinite(Date.parse(s.qualifiedAt)) || !q || !/^[a-f0-9]{40}$/.test(q.sourceCommit) || !QUALIFICATION_WORKFLOW.test(q.workflow) || !DIGEST.test(q.sbomDigest) || !DIGEST.test(q.provenanceDigest) || q.verifiedProvenance !== true || q.containmentPassed !== true || q.adapterContractPassed !== true || q.offlineAssetsPassed !== true) invalid('Missing trusted scanner qualification');
|
||||
if (!SCANNER_IDS.includes(s.scanner) || !['linux/amd64', 'linux/arm64'].includes(s.platform))
|
||||
invalid('Unsupported scanner or platform');
|
||||
const arch = s.platform === 'linux/amd64' ? 'amd64' : 'arm64';
|
||||
const expectedImage = new RegExp(
|
||||
`^ghcr\\.io/garrytan/gstack/cso-scanners/${s.scanner}-${arch}@sha256:[a-f0-9]{64}$`,
|
||||
);
|
||||
if (
|
||||
s.state !== 'qualified' ||
|
||||
!IMAGE.test(s.image) ||
|
||||
!expectedImage.test(s.image) ||
|
||||
s.entrypoint !== '/opt/cso/entrypoint' ||
|
||||
s.helperAbi !== ABI ||
|
||||
s.isolationPolicyHash !== ISOLATION_POLICY_HASH
|
||||
)
|
||||
invalid('Scanner profile is not qualified for this helper isolation policy');
|
||||
if (
|
||||
s.executable !== '/opt/cso/bin/scanner' ||
|
||||
!/^[0-9][A-Za-z0-9.+_-]{0,100}$/.test(s.version) ||
|
||||
!HASH.test(s.versionOutputSha256)
|
||||
)
|
||||
invalid('Scanner executable and version must be pinned');
|
||||
if (
|
||||
!Array.isArray(s.capabilities) ||
|
||||
!s.capabilities.length ||
|
||||
s.capabilities.length > 100 ||
|
||||
s.capabilities.some((x) => typeof x !== 'string' || !x || x.length > 100)
|
||||
)
|
||||
invalid('Scanner capabilities must be reviewed');
|
||||
const required = scannerPlans({ snapshotRoot: '/source', offline: true, selected: [s.scanner] })[0]
|
||||
.requiredFeatures;
|
||||
if (!sameStrings(s.capabilities, required))
|
||||
invalid('Scanner capabilities do not match the helper adapter contract');
|
||||
const rules = s.assets?.semgrepRules,
|
||||
db = s.assets?.advisoryDatabase;
|
||||
if (rules && (s.scanner !== 'semgrep' || !path(rules.path, '/policy/catalog/') || !HASH.test(rules.sha256)))
|
||||
invalid('Invalid immutable Semgrep rules');
|
||||
if (
|
||||
db &&
|
||||
(!['osv', 'trivy'].includes(s.scanner) ||
|
||||
!path(db.path, '/opt/cso/scanner-data/') ||
|
||||
!HASH.test(db.contentSha256) ||
|
||||
!Number.isFinite(Date.parse(db.updatedAt)) ||
|
||||
!Array.isArray(db.ecosystems) ||
|
||||
!db.ecosystems.length ||
|
||||
db.ecosystems.some((x) => typeof x !== 'string' || !x || x.length > 100))
|
||||
)
|
||||
invalid('Invalid immutable scanner database');
|
||||
if (s.scanner === 'semgrep' && !rules)
|
||||
invalid('Qualified Semgrep profiles require an immutable rules bundle');
|
||||
if (['osv', 'trivy'].includes(s.scanner) && !db)
|
||||
invalid(`Qualified ${s.scanner} profiles require an immutable offline database`);
|
||||
const q = s.qualification;
|
||||
if (
|
||||
!Number.isFinite(Date.parse(s.qualifiedAt)) ||
|
||||
!q ||
|
||||
!/^[a-f0-9]{40}$/.test(q.sourceCommit) ||
|
||||
!QUALIFICATION_WORKFLOW.test(q.workflow) ||
|
||||
!DIGEST.test(q.sbomDigest) ||
|
||||
!DIGEST.test(q.provenanceDigest) ||
|
||||
q.verifiedProvenance !== true ||
|
||||
q.containmentPassed !== true ||
|
||||
q.adapterContractPassed !== true ||
|
||||
q.offlineAssetsPassed !== true
|
||||
)
|
||||
invalid('Missing trusted scanner qualification');
|
||||
}
|
||||
export function validateScannerCatalog(value: unknown): asserts value is ScannerCatalog {
|
||||
const c = value as ScannerCatalog;
|
||||
if (!c || c.schemaVersion !== 1 || c.helperAbi !== ABI || typeof c.revision !== 'string' || !ID.test(c.revision) || !Array.isArray(c.scanners) || ![0, SCANNER_IDS.length * PLATFORMS.length].includes(c.scanners.length)) invalid('Incompatible scanner catalog');
|
||||
if (c.previousRevision !== undefined && c.previousRevision !== null && (typeof c.previousRevision !== 'string' || !ID.test(c.previousRevision) || c.previousRevision === c.revision)) invalid('Invalid previous scanner catalog revision');
|
||||
if (c.promotion !== undefined && (!/^[a-f0-9]{40}$/.test(c.promotion.sourceCommit) || !QUALIFICATION_WORKFLOW.test(c.promotion.workflow) || !DIGEST.test(c.promotion.evidenceDigest))) invalid('Invalid scanner catalog promotion');
|
||||
if (
|
||||
!c ||
|
||||
c.schemaVersion !== 1 ||
|
||||
c.helperAbi !== ABI ||
|
||||
typeof c.revision !== 'string' ||
|
||||
!ID.test(c.revision) ||
|
||||
!Array.isArray(c.scanners) ||
|
||||
![0, SCANNER_IDS.length * PLATFORMS.length].includes(c.scanners.length)
|
||||
)
|
||||
invalid('Incompatible scanner catalog');
|
||||
if (
|
||||
c.previousRevision !== undefined &&
|
||||
c.previousRevision !== null &&
|
||||
(typeof c.previousRevision !== 'string' ||
|
||||
!ID.test(c.previousRevision) ||
|
||||
c.previousRevision === c.revision)
|
||||
)
|
||||
invalid('Invalid previous scanner catalog revision');
|
||||
if (
|
||||
c.promotion !== undefined &&
|
||||
(!/^[a-f0-9]{40}$/.test(c.promotion.sourceCommit) ||
|
||||
!QUALIFICATION_WORKFLOW.test(c.promotion.workflow) ||
|
||||
!DIGEST.test(c.promotion.evidenceDigest))
|
||||
)
|
||||
invalid('Invalid scanner catalog promotion');
|
||||
if (c.scanners.length === 0) {
|
||||
if (c.promotion !== undefined) invalid('Empty scanner catalog cannot have a promotion');
|
||||
return;
|
||||
}
|
||||
if (!c.promotion) invalid('Qualified scanner catalog requires trusted promotion evidence');
|
||||
const ids = new Set<string>(), identities = new Set<string>();
|
||||
const ids = new Set<string>(),
|
||||
identities = new Set<string>();
|
||||
for (const s of c.scanners) {
|
||||
if (!s || typeof s.id !== 'string' || !ID.test(s.id) || ids.has(s.id)) invalid('Invalid or duplicate scanner profile');
|
||||
if (!s || typeof s.id !== 'string' || !ID.test(s.id) || ids.has(s.id))
|
||||
invalid('Invalid or duplicate scanner profile');
|
||||
const identity = `${s.scanner}:${s.platform}`;
|
||||
if (identities.has(identity)) invalid('Invalid or duplicate scanner profile');
|
||||
ids.add(s.id); identities.add(identity);
|
||||
ids.add(s.id);
|
||||
identities.add(identity);
|
||||
validateQualifiedScanner(s);
|
||||
if (s.qualification.sourceCommit !== c.promotion.sourceCommit || s.qualification.workflow !== c.promotion.workflow) invalid('Scanner qualification does not match catalog promotion');
|
||||
if (
|
||||
s.qualification.sourceCommit !== c.promotion.sourceCommit ||
|
||||
s.qualification.workflow !== c.promotion.workflow
|
||||
)
|
||||
invalid('Scanner qualification does not match catalog promotion');
|
||||
}
|
||||
for (const scanner of SCANNER_IDS) for (const platform of PLATFORMS) if (!identities.has(`${scanner}:${platform}`)) invalid('Incomplete qualified scanner matrix');
|
||||
if (c.promotion.evidenceDigest !== `sha256:${sha256(canonical(c.scanners))}`) invalid('Scanner catalog promotion does not bind the qualified matrix');
|
||||
for (const scanner of SCANNER_IDS)
|
||||
for (const platform of PLATFORMS)
|
||||
if (!identities.has(`${scanner}:${platform}`)) invalid('Incomplete qualified scanner matrix');
|
||||
if (c.promotion.evidenceDigest !== `sha256:${sha256(canonical(c.scanners))}`)
|
||||
invalid('Scanner catalog promotion does not bind the qualified matrix');
|
||||
}
|
||||
export const SCANNER_CATALOG = committedCatalog as unknown as ScannerCatalog;
|
||||
// A malformed source-controlled catalog must break the helper build/startup;
|
||||
// it can never degrade into an unreviewed executable fallback.
|
||||
validateScannerCatalog(SCANNER_CATALOG);
|
||||
export function selectScanner(scanner: ScannerId, platform: RuntimePlatform, profile?: string, catalog: ScannerCatalog = SCANNER_CATALOG): QualifiedScanner {
|
||||
export function selectScanner(
|
||||
scanner: ScannerId,
|
||||
platform: RuntimePlatform,
|
||||
profile?: string,
|
||||
catalog: ScannerCatalog = SCANNER_CATALOG,
|
||||
): QualifiedScanner {
|
||||
validateScannerCatalog(catalog);
|
||||
const matches = catalog.scanners.filter(s => s.scanner === scanner && s.platform === platform && (!profile || s.id === profile));
|
||||
if (!matches.length) throw new CsoError('PREREQUISITE', `No qualified ${scanner} image for ${platform}${profile ? ` (${profile})` : ''}; qualify and review an immutable scanner catalog before execution`);
|
||||
if (matches.length !== 1) throw new CsoError('PREREQUISITE', `Select an exact qualified ${scanner} profile for ${platform}`);
|
||||
const matches = catalog.scanners.filter(
|
||||
(s) => s.scanner === scanner && s.platform === platform && (!profile || s.id === profile),
|
||||
);
|
||||
if (!matches.length)
|
||||
throw new CsoError(
|
||||
'PREREQUISITE',
|
||||
`No qualified ${scanner} image for ${platform}${profile ? ` (${profile})` : ''}; qualify and review an immutable scanner catalog before execution`,
|
||||
);
|
||||
if (matches.length !== 1)
|
||||
throw new CsoError('PREREQUISITE', `Select an exact qualified ${scanner} profile for ${platform}`);
|
||||
return matches[0];
|
||||
}
|
||||
+628
-144
@@ -2,17 +2,63 @@
|
||||
import * as fs from 'node:fs';
|
||||
import { randomBytes } from 'node:crypto';
|
||||
import { join } from 'node:path';
|
||||
import { Command, CoverageRecord, CsoError, HttpAssertion, RunPolicy, SnapshotManifest, canonical, object, relativePath, sha256, snapshotPathHandleId, snapshotReference, string, strings, validateCommand, validateVerificationObservation, type ErrorCode } from './contracts';
|
||||
import {
|
||||
Command,
|
||||
CoverageRecord,
|
||||
CsoError,
|
||||
HttpAssertion,
|
||||
RunPolicy,
|
||||
SnapshotManifest,
|
||||
canonical,
|
||||
object,
|
||||
relativePath,
|
||||
sha256,
|
||||
snapshotPathHandleId,
|
||||
snapshotReference,
|
||||
string,
|
||||
strings,
|
||||
validateCommand,
|
||||
validateVerificationObservation,
|
||||
type ErrorCode,
|
||||
} from './contracts';
|
||||
import { DockerEndpoint, DockerGroup, dockerEndpoint } from './docker';
|
||||
import { inspectPreparation, type CsoStack } from './preparation';
|
||||
import { redact } from './process';
|
||||
import { QualifiedRuntime, RUNTIME_CATALOG, RuntimeCatalog, RuntimePlatform, assertRuntimeCompatible, selectRuntime } from './runtime-catalog';
|
||||
import { QualifiedScanner, SCANNER_CATALOG, ScannerCatalog, assertScannerVersionOutput, scannerVersionHash, selectScanner } from './scanner-catalog';
|
||||
import { ScannerExecution, ScannerGap, ScannerId, ScannerOutcome, ScannerPlan, parseScannerOutput, scannerPlans } from './scanners';
|
||||
import {
|
||||
QualifiedRuntime,
|
||||
RUNTIME_CATALOG,
|
||||
RuntimeCatalog,
|
||||
RuntimePlatform,
|
||||
assertRuntimeCompatible,
|
||||
selectRuntime,
|
||||
} from './runtime-catalog';
|
||||
import {
|
||||
QualifiedScanner,
|
||||
SCANNER_CATALOG,
|
||||
ScannerCatalog,
|
||||
assertScannerVersionOutput,
|
||||
scannerVersionHash,
|
||||
selectScanner,
|
||||
} from './scanner-catalog';
|
||||
import {
|
||||
ScannerExecution,
|
||||
ScannerGap,
|
||||
ScannerId,
|
||||
ScannerOutcome,
|
||||
ScannerPlan,
|
||||
parseScannerOutput,
|
||||
scannerPlans,
|
||||
} from './scanners';
|
||||
import { assertSnapshot } from './snapshot';
|
||||
import { hasPendingWatchdogCleanup, secureDirectory } from './state';
|
||||
import { PublicArchiveCache, publicArchiveCacheRoot } from './cache';
|
||||
import { admitPreparationRuntime, admitPreparationSidecar, PreparationExecutor, type PreparationSandboxRunner, type RailsDatabaseSelection } from './preparation-executor';
|
||||
import {
|
||||
admitPreparationRuntime,
|
||||
admitPreparationSidecar,
|
||||
PreparationExecutor,
|
||||
type PreparationSandboxRunner,
|
||||
type RailsDatabaseSelection,
|
||||
} from './preparation-executor';
|
||||
import type { PreparedDatabaseContract } from './preparation-executor';
|
||||
import { DockerPreparationSandboxRunner } from './preparation-docker';
|
||||
import { canonicalStartPlan, type CanonicalStartPlan } from './verification';
|
||||
@@ -79,7 +125,9 @@ export interface ScannerRunner {
|
||||
cleanup(): Promise<void>;
|
||||
}
|
||||
/** The trusted HTTP control probe is always the bounded verifier process. */
|
||||
export function schemathesisControlRole(): 'verifier' { return 'verifier'; }
|
||||
export function schemathesisControlRole(): 'verifier' {
|
||||
return 'verifier';
|
||||
}
|
||||
export interface ScannerRunnerContext {
|
||||
input: ScannerRunInput;
|
||||
plan: ScannerPlan;
|
||||
@@ -107,58 +155,121 @@ export interface ScannerRunDependencies {
|
||||
}
|
||||
|
||||
function exact(v: Record<string, unknown>, allowed: string[], name: string): void {
|
||||
for (const key of Object.keys(v)) if (!allowed.includes(key)) throw new CsoError('INVALID_SCHEMA', `Unexpected ${name} field: ${key}`);
|
||||
for (const key of Object.keys(v))
|
||||
if (!allowed.includes(key)) throw new CsoError('INVALID_SCHEMA', `Unexpected ${name} field: ${key}`);
|
||||
}
|
||||
function boundedInt(v: unknown, min: number, max: number, name: string): number {
|
||||
if (!Number.isSafeInteger(v) || (v as number) < min || (v as number) > max) throw new CsoError('INVALID_SCHEMA', `${name} must be ${min}..${max}`);
|
||||
if (!Number.isSafeInteger(v) || (v as number) < min || (v as number) > max)
|
||||
throw new CsoError('INVALID_SCHEMA', `${name} must be ${min}..${max}`);
|
||||
return v as number;
|
||||
}
|
||||
function control(value: unknown): HttpAssertion {
|
||||
const v = object(value, 'API control'), expected = object(v.expected, 'API control expected');
|
||||
const v = object(value, 'API control'),
|
||||
expected = object(v.expected, 'API control expected');
|
||||
exact(v, ['name', 'path', 'method', 'headers', 'body', 'expected'], 'API control');
|
||||
exact(expected, ['status', 'includes', 'excludes'], 'API control expected');
|
||||
const path = string(v.path, 'API control path', 4096);
|
||||
if (!path.startsWith('/') || path.startsWith('//') || /[\r\n\\]/.test(path)) throw new CsoError('INVALID_SCHEMA', 'API control path must remain on numeric loopback');
|
||||
if (!['GET', 'POST', 'PUT', 'PATCH', 'DELETE'].includes(v.method)) throw new CsoError('INVALID_SCHEMA', 'Invalid API control method');
|
||||
if (!path.startsWith('/') || path.startsWith('//') || /[\r\n\\]/.test(path))
|
||||
throw new CsoError('INVALID_SCHEMA', 'API control path must remain on numeric loopback');
|
||||
if (!['GET', 'POST', 'PUT', 'PATCH', 'DELETE'].includes(v.method))
|
||||
throw new CsoError('INVALID_SCHEMA', 'Invalid API control method');
|
||||
const headers: Record<string, string> = {};
|
||||
for (const [key, value] of Object.entries(v.headers === undefined ? {} : object(v.headers, 'API control headers'))) {
|
||||
if (!/^[A-Za-z0-9-]{1,100}$/.test(key) || typeof value !== 'string' || value.length > 8192 || /[\r\n]/.test(value)) throw new CsoError('INVALID_SCHEMA', 'Invalid API control header');
|
||||
for (const [key, value] of Object.entries(
|
||||
v.headers === undefined ? {} : object(v.headers, 'API control headers'),
|
||||
)) {
|
||||
if (
|
||||
!/^[A-Za-z0-9-]{1,100}$/.test(key) ||
|
||||
typeof value !== 'string' ||
|
||||
value.length > 8192 ||
|
||||
/[\r\n]/.test(value)
|
||||
)
|
||||
throw new CsoError('INVALID_SCHEMA', 'Invalid API control header');
|
||||
headers[key] = value;
|
||||
}
|
||||
return { name: string(v.name, 'API control name', 200), path, method: v.method, headers,
|
||||
return {
|
||||
name: string(v.name, 'API control name', 200),
|
||||
path,
|
||||
method: v.method,
|
||||
headers,
|
||||
...(v.body === undefined ? {} : { body: string(v.body, 'API control body', 65536) }),
|
||||
expected: { status: boundedInt(expected.status, 100, 599, 'API control status'),
|
||||
...(expected.includes === undefined ? {} : { includes: string(expected.includes, 'API control includes', 8192) }),
|
||||
...(expected.excludes === undefined ? {} : { excludes: string(expected.excludes, 'API control excludes', 8192) }) } };
|
||||
expected: {
|
||||
status: boundedInt(expected.status, 100, 599, 'API control status'),
|
||||
...(expected.includes === undefined
|
||||
? {}
|
||||
: { includes: string(expected.includes, 'API control includes', 8192) }),
|
||||
...(expected.excludes === undefined
|
||||
? {}
|
||||
: { excludes: string(expected.excludes, 'API control excludes', 8192) }),
|
||||
},
|
||||
};
|
||||
}
|
||||
/** Accept a bounded OpenAPI document, with internal references and selected path operations only. */
|
||||
export function validateScannerRequest(value: unknown, id: ScannerId): ScannerRequest {
|
||||
const v = object(value, 'scanner request');
|
||||
exact(v, ['profile', 'api'], 'scanner request');
|
||||
const request: ScannerRequest = v.profile === undefined ? {} : { profile: string(v.profile, 'scanner profile', 100) };
|
||||
const request: ScannerRequest =
|
||||
v.profile === undefined ? {} : { profile: string(v.profile, 'scanner profile', 100) };
|
||||
if (v.api === undefined) return request;
|
||||
if (id !== 'schemathesis') throw new CsoError('INVALID_SCHEMA', 'Only Schemathesis accepts application execution inputs');
|
||||
if (id !== 'schemathesis')
|
||||
throw new CsoError('INVALID_SCHEMA', 'Only Schemathesis accepts application execution inputs');
|
||||
const api = object(v.api, 'API scan');
|
||||
exact(api, ['runtimeProfile', 'port', 'start', 'control', 'boundaryFiles', 'schema', 'operationIds', 'seed', 'maxExamples'], 'API scan');
|
||||
const schema = object(api.schema, 'OpenAPI schema'), operations = strings(api.operationIds, 'operation IDs');
|
||||
if (operations.length < 1 || operations.length > 20 || new Set(operations).size !== operations.length || operations.some(x => x.length > 200 || /[\x00-\x1f]/.test(x))) throw new CsoError('INVALID_SCHEMA', 'Declare 1..20 unique bounded operation IDs');
|
||||
if (typeof schema.openapi !== 'string' || !/^3\.[01]\.\d+$/.test(schema.openapi)) throw new CsoError('PREREQUISITE', 'Schemathesis requires a reviewed OpenAPI 3.0/3.1 JSON document');
|
||||
if (Buffer.byteLength(JSON.stringify(schema)) > 262144) throw new CsoError('INVALID_SCHEMA', 'OpenAPI schema exceeds 256 KiB');
|
||||
exact(
|
||||
api,
|
||||
[
|
||||
'runtimeProfile',
|
||||
'port',
|
||||
'start',
|
||||
'control',
|
||||
'boundaryFiles',
|
||||
'schema',
|
||||
'operationIds',
|
||||
'seed',
|
||||
'maxExamples',
|
||||
],
|
||||
'API scan',
|
||||
);
|
||||
const schema = object(api.schema, 'OpenAPI schema'),
|
||||
operations = strings(api.operationIds, 'operation IDs');
|
||||
if (
|
||||
operations.length < 1 ||
|
||||
operations.length > 20 ||
|
||||
new Set(operations).size !== operations.length ||
|
||||
operations.some((x) => x.length > 200 || /[\x00-\x1f]/.test(x))
|
||||
)
|
||||
throw new CsoError('INVALID_SCHEMA', 'Declare 1..20 unique bounded operation IDs');
|
||||
if (typeof schema.openapi !== 'string' || !/^3\.[01]\.\d+$/.test(schema.openapi))
|
||||
throw new CsoError('PREREQUISITE', 'Schemathesis requires a reviewed OpenAPI 3.0/3.1 JSON document');
|
||||
if (Buffer.byteLength(JSON.stringify(schema)) > 262144)
|
||||
throw new CsoError('INVALID_SCHEMA', 'OpenAPI schema exceeds 256 KiB');
|
||||
let nodes = 0;
|
||||
const inspect = (x: unknown, depth: number): void => {
|
||||
if (++nodes > 50_000 || depth > 32) throw new CsoError('INVALID_SCHEMA', 'OpenAPI schema exceeds structural bounds');
|
||||
if (++nodes > 50_000 || depth > 32)
|
||||
throw new CsoError('INVALID_SCHEMA', 'OpenAPI schema exceeds structural bounds');
|
||||
if (!x || typeof x !== 'object') return;
|
||||
for (const [key, value] of Object.entries(x)) {
|
||||
if (['__proto__', 'prototype', 'constructor', 'externalValue', 'callbacks', 'webhooks'].includes(key) || /hooks?/i.test(key)) throw new CsoError('PREREQUISITE', 'OpenAPI external examples, callbacks, webhooks, and hooks are not admitted');
|
||||
if (key === '$ref' && (typeof value !== 'string' || !value.startsWith('#/'))) throw new CsoError('PREREQUISITE', 'OpenAPI references must be internal JSON pointers');
|
||||
if (key === 'servers' && (!Array.isArray(value) || value.length)) throw new CsoError('PREREQUISITE', 'Remove server overrides from the reviewed API harness; its target is the isolated loopback application');
|
||||
if (
|
||||
['__proto__', 'prototype', 'constructor', 'externalValue', 'callbacks', 'webhooks'].includes(key) ||
|
||||
/hooks?/i.test(key)
|
||||
)
|
||||
throw new CsoError(
|
||||
'PREREQUISITE',
|
||||
'OpenAPI external examples, callbacks, webhooks, and hooks are not admitted',
|
||||
);
|
||||
if (key === '$ref' && (typeof value !== 'string' || !value.startsWith('#/')))
|
||||
throw new CsoError('PREREQUISITE', 'OpenAPI references must be internal JSON pointers');
|
||||
if (key === 'servers' && (!Array.isArray(value) || value.length))
|
||||
throw new CsoError(
|
||||
'PREREQUISITE',
|
||||
'Remove server overrides from the reviewed API harness; its target is the isolated loopback application',
|
||||
);
|
||||
inspect(value, depth + 1);
|
||||
}
|
||||
};
|
||||
inspect(schema, 0);
|
||||
const declared: string[] = [];
|
||||
for (const [path, item] of Object.entries(object(schema.paths, 'OpenAPI paths'))) {
|
||||
if (!path.startsWith('/') || path.startsWith('//') || /[\r\n\\?#]/.test(path)) throw new CsoError('INVALID_SCHEMA', 'OpenAPI paths must be relative to the loopback target');
|
||||
if (!path.startsWith('/') || path.startsWith('//') || /[\r\n\\?#]/.test(path))
|
||||
throw new CsoError('INVALID_SCHEMA', 'OpenAPI paths must be relative to the loopback target');
|
||||
const methods = object(item, 'OpenAPI path');
|
||||
for (const method of ['get', 'post', 'put', 'patch', 'delete', 'head', 'options', 'trace']) {
|
||||
if (methods[method] === undefined) continue;
|
||||
@@ -166,148 +277,413 @@ export function validateScannerRequest(value: unknown, id: ScannerId): ScannerRe
|
||||
if (typeof op.operationId === 'string') declared.push(op.operationId);
|
||||
}
|
||||
}
|
||||
if (operations.some(op => declared.filter(x => x === op).length !== 1)) throw new CsoError('INVALID_SCHEMA', 'Every selected operation must identify exactly one declared OpenAPI path operation');
|
||||
if (operations.some((op) => declared.filter((x) => x === op).length !== 1))
|
||||
throw new CsoError(
|
||||
'INVALID_SCHEMA',
|
||||
'Every selected operation must identify exactly one declared OpenAPI path operation',
|
||||
);
|
||||
const boundaries = strings(api.boundaryFiles, 'API boundary files').map(snapshotReference);
|
||||
if (!boundaries.length || new Set(boundaries).size !== boundaries.length) throw new CsoError('INVALID_SCHEMA', 'API scan needs unique security-boundary source paths');
|
||||
request.api = { runtimeProfile: string(api.runtimeProfile, 'API runtime profile', 100), port: boundedInt(api.port, 1024, 65535, 'API port'), start: validateCommand(api.start, 'API start'), control: control(api.control), boundaryFiles: boundaries, schema, operationIds: operations,
|
||||
if (!boundaries.length || new Set(boundaries).size !== boundaries.length)
|
||||
throw new CsoError('INVALID_SCHEMA', 'API scan needs unique security-boundary source paths');
|
||||
request.api = {
|
||||
runtimeProfile: string(api.runtimeProfile, 'API runtime profile', 100),
|
||||
port: boundedInt(api.port, 1024, 65535, 'API port'),
|
||||
start: validateCommand(api.start, 'API start'),
|
||||
control: control(api.control),
|
||||
boundaryFiles: boundaries,
|
||||
schema,
|
||||
operationIds: operations,
|
||||
...(api.seed === undefined ? {} : { seed: boundedInt(api.seed, 1, 2147483647, 'API seed') }),
|
||||
...(api.maxExamples === undefined ? {} : { maxExamples: boundedInt(api.maxExamples, 1, 100, 'API maxExamples') }) };
|
||||
...(api.maxExamples === undefined
|
||||
? {}
|
||||
: { maxExamples: boundedInt(api.maxExamples, 1, 100, 'API maxExamples') }),
|
||||
};
|
||||
const raw = JSON.stringify(request);
|
||||
if (redact(raw) !== raw) throw new CsoError('REDACTION_FAILED', 'Scanner harness contains secret-bearing material; use synthetic inputs');
|
||||
if (redact(raw) !== raw)
|
||||
throw new CsoError(
|
||||
'REDACTION_FAILED',
|
||||
'Scanner harness contains secret-bearing material; use synthetic inputs',
|
||||
);
|
||||
return request;
|
||||
}
|
||||
|
||||
/** Resolve only helper-issued path references before any application command reaches containment. */
|
||||
export function resolveScannerRequestPaths(manifest:SnapshotManifest,request:ScannerRequest):ScannerRequest{
|
||||
if(!request.api)return request;
|
||||
const resolve=(reference:string):string=>{const id=snapshotPathHandleId(reference);if(!id)return relativePath(reference);const entry=manifest.entries.find(item=>item.pathId===id);if(!entry)throw new CsoError('INVALID_SCHEMA',`API path handle is outside the retained snapshot: ${reference}`);return entry.path;};
|
||||
const argument=(value:string):string=>{if(snapshotPathHandleId(value))return resolve(value);if(value.startsWith('./')&&snapshotPathHandleId(value.slice(2)))return `./${resolve(value.slice(2))}`;return value;};
|
||||
return{...request,api:{...request.api,start:{...request.api.start,args:request.api.start.args.map(argument)},boundaryFiles:request.api.boundaryFiles.map(resolve)}};
|
||||
export function resolveScannerRequestPaths(
|
||||
manifest: SnapshotManifest,
|
||||
request: ScannerRequest,
|
||||
): ScannerRequest {
|
||||
if (!request.api) return request;
|
||||
const resolve = (reference: string): string => {
|
||||
const id = snapshotPathHandleId(reference);
|
||||
if (!id) return relativePath(reference);
|
||||
const entry = manifest.entries.find((item) => item.pathId === id);
|
||||
if (!entry)
|
||||
throw new CsoError('INVALID_SCHEMA', `API path handle is outside the retained snapshot: ${reference}`);
|
||||
return entry.path;
|
||||
};
|
||||
const argument = (value: string): string => {
|
||||
if (snapshotPathHandleId(value)) return resolve(value);
|
||||
if (value.startsWith('./') && snapshotPathHandleId(value.slice(2))) return `./${resolve(value.slice(2))}`;
|
||||
return value;
|
||||
};
|
||||
return {
|
||||
...request,
|
||||
api: {
|
||||
...request.api,
|
||||
start: { ...request.api.start, args: request.api.start.args.map(argument) },
|
||||
boundaryFiles: request.api.boundaryFiles.map(resolve),
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export function scannerCoverage(outcome: ScannerOutcome, scope: string): CoverageRecord {
|
||||
return { domain: `scanner:${outcome.tool}`, scope, status: outcome.status === 'complete' ? 'assessed' : outcome.status,
|
||||
method: outcome.tool === 'sarif' ? 'bounded untrusted SARIF import' : 'qualified offline Docker scanner; candidate evidence only',
|
||||
gaps: outcome.gaps.map(g => g.message), exclusions: outcome.exclusions,
|
||||
return {
|
||||
domain: `scanner:${outcome.tool}`,
|
||||
scope,
|
||||
status: outcome.status === 'complete' ? 'assessed' : outcome.status,
|
||||
method:
|
||||
outcome.tool === 'sarif'
|
||||
? 'bounded untrusted SARIF import'
|
||||
: 'qualified offline Docker scanner; candidate evidence only',
|
||||
gaps: outcome.gaps.map((g) => g.message),
|
||||
exclusions: outcome.exclusions,
|
||||
evidence: [`${outcome.candidates.length} scanner candidates; plan ${outcome.planSha256}`],
|
||||
tool: { name: outcome.tool, version: outcome.version ?? 'unavailable', freshness: outcome.databaseUpdatedAt ?? 'not reported', outcome: outcome.status } };
|
||||
tool: {
|
||||
name: outcome.tool,
|
||||
version: outcome.version ?? 'unavailable',
|
||||
freshness: outcome.databaseUpdatedAt ?? 'not reported',
|
||||
outcome: outcome.status,
|
||||
},
|
||||
};
|
||||
}
|
||||
function failure(plan: ScannerPlan, error: unknown, version?: string): ScannerOutcome {
|
||||
const e = error instanceof CsoError ? error : new CsoError('ISOLATION_FAILED', 'Scanner execution failed before bounded evidence was established');
|
||||
const e =
|
||||
error instanceof CsoError
|
||||
? error
|
||||
: new CsoError('ISOLATION_FAILED', 'Scanner execution failed before bounded evidence was established');
|
||||
const codes: Record<ErrorCode, ScannerGap['code']> = {
|
||||
INVALID_ARGUMENT: 'INVALID_OUTPUT', INVALID_SCHEMA: 'INVALID_OUTPUT', MISSING_INPUT: 'MISSING_INPUT', SNAPSHOT_RACE: 'SNAPSHOT_RACE',
|
||||
UNSAFE_PATH: 'UNSAFE_PATH', REDACTION_FAILED: 'REDACTION_FAILED', PERSISTENCE_FAILED: 'PERSISTENCE_FAILED', TOOL_UNAVAILABLE: 'UNAVAILABLE',
|
||||
TOOL_FAILED: 'TOOL_FAILED', ISOLATION_FAILED: 'ISOLATION_FAILED', INSUFFICIENT_CAPACITY: 'INSUFFICIENT_CAPACITY', DEADLINE: 'TIMEOUT',
|
||||
CANCELLED: 'CANCELLED', PREREQUISITE: 'PREREQUISITE', INCOMPATIBLE_INPUT: 'PREREQUISITE', ASSERTION_FAILED: 'TOOL_FAILED',
|
||||
INVALID_ARGUMENT: 'INVALID_OUTPUT',
|
||||
INVALID_SCHEMA: 'INVALID_OUTPUT',
|
||||
MISSING_INPUT: 'MISSING_INPUT',
|
||||
SNAPSHOT_RACE: 'SNAPSHOT_RACE',
|
||||
UNSAFE_PATH: 'UNSAFE_PATH',
|
||||
REDACTION_FAILED: 'REDACTION_FAILED',
|
||||
PERSISTENCE_FAILED: 'PERSISTENCE_FAILED',
|
||||
TOOL_UNAVAILABLE: 'UNAVAILABLE',
|
||||
TOOL_FAILED: 'TOOL_FAILED',
|
||||
ISOLATION_FAILED: 'ISOLATION_FAILED',
|
||||
INSUFFICIENT_CAPACITY: 'INSUFFICIENT_CAPACITY',
|
||||
DEADLINE: 'TIMEOUT',
|
||||
CANCELLED: 'CANCELLED',
|
||||
PREREQUISITE: 'PREREQUISITE',
|
||||
INCOMPATIBLE_INPUT: 'PREREQUISITE',
|
||||
ASSERTION_FAILED: 'TOOL_FAILED',
|
||||
};
|
||||
const code = codes[e.code];
|
||||
return { ...parseScannerOutput(plan, { stdout: '', exitCode: null, version }), status: 'not_assessed', candidates: [], gaps: [{ code, message: e.message }] };
|
||||
return {
|
||||
...parseScannerOutput(plan, { stdout: '', exitCode: null, version }),
|
||||
status: 'not_assessed',
|
||||
candidates: [],
|
||||
gaps: [{ code, message: e.message }],
|
||||
};
|
||||
}
|
||||
|
||||
/** Empty catalogs and missing assets produce coverage gaps without opening Docker. */
|
||||
export async function executeScanner(input: ScannerRunInput, dependencies: ScannerRunDependencies = {}): Promise<ScannerRunRecord> {
|
||||
const identityRequest = validateScannerRequest(input.request ?? {}, input.id), catalog = dependencies.catalog ?? SCANNER_CATALOG;
|
||||
export async function executeScanner(
|
||||
input: ScannerRunInput,
|
||||
dependencies: ScannerRunDependencies = {},
|
||||
): Promise<ScannerRunRecord> {
|
||||
const identityRequest = validateScannerRequest(input.request ?? {}, input.id),
|
||||
catalog = dependencies.catalog ?? SCANNER_CATALOG;
|
||||
const timeout = Math.min(300, Math.floor((input.executionDeadline - Date.now()) / 1000));
|
||||
let profile: QualifiedScanner | undefined, runtime: QualifiedRuntime | undefined, observedVersion: string | undefined, versionHash: string | null = null;
|
||||
let application: ScannerApplicationPreparation | undefined,request=identityRequest;
|
||||
let plan = scannerPlans({ snapshotRoot: '/source', offline: input.policy.offline, selected: [input.id], deadlineSeconds: Math.max(1, timeout) })[0];
|
||||
let profile: QualifiedScanner | undefined,
|
||||
runtime: QualifiedRuntime | undefined,
|
||||
observedVersion: string | undefined,
|
||||
versionHash: string | null = null;
|
||||
let application: ScannerApplicationPreparation | undefined,
|
||||
request = identityRequest;
|
||||
let plan = scannerPlans({
|
||||
snapshotRoot: '/source',
|
||||
offline: input.policy.offline,
|
||||
selected: [input.id],
|
||||
deadlineSeconds: Math.max(1, timeout),
|
||||
})[0];
|
||||
let outcome: ScannerOutcome, runner: ScannerRunner | undefined;
|
||||
try {
|
||||
if (timeout < 1) throw new CsoError('DEADLINE', 'No scanner time remains before the reporting reserve');
|
||||
assertSnapshot(input.runDir, input.manifest);
|
||||
request=resolveScannerRequestPaths(input.manifest,identityRequest);
|
||||
if (input.id === 'schemathesis' && input.policy.mode !== 'comprehensive') throw new CsoError('PREREQUISITE', 'Schemathesis requires comprehensive mode; daily audits do not execute applications');
|
||||
request = resolveScannerRequestPaths(input.manifest, identityRequest);
|
||||
if (input.id === 'schemathesis' && input.policy.mode !== 'comprehensive')
|
||||
throw new CsoError(
|
||||
'PREREQUISITE',
|
||||
'Schemathesis requires comprehensive mode; daily audits do not execute applications',
|
||||
);
|
||||
profile = selectScanner(input.id, input.platform, request.profile, catalog);
|
||||
const api = request.api;
|
||||
plan = scannerPlans({ snapshotRoot: '/source', offline: input.policy.offline, selected: [input.id], deadlineSeconds: timeout,
|
||||
tools: { [input.id]: { available: true, version: profile.version, capabilities: profile.capabilities } },
|
||||
semgrepRules: profile.assets?.semgrepRules?.path, advisoryCache: profile.assets?.advisoryDatabase?.path,
|
||||
...(api ? { schemaPath: '/policy/openapi.json', baseUrl: `http://127.0.0.1:${api.port}/`, operationIds: api.operationIds, seed: api.seed, maxExamples: api.maxExamples } : {}) })[0];
|
||||
plan = scannerPlans({
|
||||
snapshotRoot: '/source',
|
||||
offline: input.policy.offline,
|
||||
selected: [input.id],
|
||||
deadlineSeconds: timeout,
|
||||
tools: {
|
||||
[input.id]: { available: true, version: profile.version, capabilities: profile.capabilities },
|
||||
},
|
||||
semgrepRules: profile.assets?.semgrepRules?.path,
|
||||
advisoryCache: profile.assets?.advisoryDatabase?.path,
|
||||
...(api
|
||||
? {
|
||||
schemaPath: '/policy/openapi.json',
|
||||
baseUrl: `http://127.0.0.1:${api.port}/`,
|
||||
operationIds: api.operationIds,
|
||||
seed: api.seed,
|
||||
maxExamples: api.maxExamples,
|
||||
}
|
||||
: {}),
|
||||
})[0];
|
||||
if (plan.prerequisites.length) throw new CsoError('PREREQUISITE', plan.prerequisites.join('; '));
|
||||
if (input.id === 'schemathesis') {
|
||||
if (!api) throw new CsoError('PREREQUISITE', 'Schemathesis requires a reviewed API harness and legitimate control');
|
||||
if (!api)
|
||||
throw new CsoError(
|
||||
'PREREQUISITE',
|
||||
'Schemathesis requires a reviewed API harness and legitimate control',
|
||||
);
|
||||
for (const file of api.boundaryFiles) {
|
||||
const entry = input.manifest.entries.find(e => e.path === file);
|
||||
if (!entry || !entry.executionHash || entry.transformation) throw new CsoError('INCOMPATIBLE_INPUT', `API security boundary is missing or transformed: ${file}`);
|
||||
const entry = input.manifest.entries.find((e) => e.path === file);
|
||||
if (!entry || !entry.executionHash || entry.transformation)
|
||||
throw new CsoError(
|
||||
'INCOMPATIBLE_INPUT',
|
||||
`API security boundary is missing or transformed: ${file}`,
|
||||
);
|
||||
}
|
||||
try { runtime = selectRuntime(api.runtimeProfile, input.platform, dependencies.runtimes ?? RUNTIME_CATALOG); }
|
||||
catch { throw new CsoError('PREREQUISITE', `Qualified application runtime is unavailable: ${api.runtimeProfile}`); }
|
||||
if (!['node', 'bun', 'python', 'rails'].includes(runtime.stack)) throw new CsoError('INCOMPATIBLE_INPUT', 'Schemathesis requires a qualified application runtime');
|
||||
const stack = runtime.stack as CsoStack, sourceRoot = join(input.runDir, 'snapshot');
|
||||
try {
|
||||
runtime = selectRuntime(api.runtimeProfile, input.platform, dependencies.runtimes ?? RUNTIME_CATALOG);
|
||||
} catch {
|
||||
throw new CsoError(
|
||||
'PREREQUISITE',
|
||||
`Qualified application runtime is unavailable: ${api.runtimeProfile}`,
|
||||
);
|
||||
}
|
||||
if (!['node', 'bun', 'python', 'rails'].includes(runtime.stack))
|
||||
throw new CsoError('INCOMPATIBLE_INPUT', 'Schemathesis requires a qualified application runtime');
|
||||
const stack = runtime.stack as CsoStack,
|
||||
sourceRoot = join(input.runDir, 'snapshot');
|
||||
const preparation = inspectPreparation(sourceRoot, stack);
|
||||
assertRuntimeCompatible(preparation, runtime);
|
||||
const startPlan = canonicalStartPlan(sourceRoot, stack, api.port);
|
||||
if (canonical(api.start) !== canonical(startPlan.command)) throw new CsoError('INVALID_SCHEMA', `API start must use the helper-derived ${startPlan.kind} command`);
|
||||
for (const file of startPlan.entrypointFiles) if (!api.boundaryFiles.includes(file))
|
||||
throw new CsoError('INVALID_SCHEMA', `API boundary files must include canonical startup input: ${file}`);
|
||||
if (canonical(api.start) !== canonical(startPlan.command))
|
||||
throw new CsoError(
|
||||
'INVALID_SCHEMA',
|
||||
`API start must use the helper-derived ${startPlan.kind} command`,
|
||||
);
|
||||
for (const file of startPlan.entrypointFiles)
|
||||
if (!api.boundaryFiles.includes(file))
|
||||
throw new CsoError(
|
||||
'INVALID_SCHEMA',
|
||||
`API boundary files must include canonical startup input: ${file}`,
|
||||
);
|
||||
application = await (dependencies.applicationPreparer ?? prepareDockerScannerApplication)({
|
||||
input: { ...input, request }, runtime, stack, startPlan,
|
||||
deadline: Math.min(input.executionDeadline, Date.now() + timeout * 1000), catalog: dependencies.runtimes ?? RUNTIME_CATALOG,
|
||||
input: { ...input, request },
|
||||
runtime,
|
||||
stack,
|
||||
startPlan,
|
||||
deadline: Math.min(input.executionDeadline, Date.now() + timeout * 1000),
|
||||
catalog: dependencies.runtimes ?? RUNTIME_CATALOG,
|
||||
});
|
||||
const preparedStart = canonicalStartPlan(application.sourceRoot, stack, api.port);
|
||||
if (preparedStart.signature !== startPlan.signature || canonical(preparedStart.command) !== canonical(startPlan.command))
|
||||
throw new CsoError('ISOLATION_FAILED', 'Offline API preparation changed the canonical application startup inputs');
|
||||
if (
|
||||
preparedStart.signature !== startPlan.signature ||
|
||||
canonical(preparedStart.command) !== canonical(startPlan.command)
|
||||
)
|
||||
throw new CsoError(
|
||||
'ISOLATION_FAILED',
|
||||
'Offline API preparation changed the canonical application startup inputs',
|
||||
);
|
||||
}
|
||||
runner = await (dependencies.runnerFactory ?? createDockerScannerRunner)({ input: { ...input, request }, plan, profile, runtime, application, deadline: Math.min(input.executionDeadline, Date.now() + timeout * 1000) });
|
||||
runner = await (dependencies.runnerFactory ?? createDockerScannerRunner)({
|
||||
input: { ...input, request },
|
||||
plan,
|
||||
profile,
|
||||
runtime,
|
||||
application,
|
||||
deadline: Math.min(input.executionDeadline, Date.now() + timeout * 1000),
|
||||
});
|
||||
const version = await runner.version();
|
||||
if (version.exitCode !== 0 || version.timedOut || version.truncated || version.unavailable || Buffer.byteLength(version.stdout) + Buffer.byteLength(version.stderr ?? '') > 8192) throw new CsoError('TOOL_UNAVAILABLE', 'Scanner version probe did not complete within the qualified sandbox');
|
||||
assertScannerVersionOutput(profile.scanner,profile.version,version.stdout,version.stderr);
|
||||
if (
|
||||
version.exitCode !== 0 ||
|
||||
version.timedOut ||
|
||||
version.truncated ||
|
||||
version.unavailable ||
|
||||
Buffer.byteLength(version.stdout) + Buffer.byteLength(version.stderr ?? '') > 8192
|
||||
)
|
||||
throw new CsoError(
|
||||
'TOOL_UNAVAILABLE',
|
||||
'Scanner version probe did not complete within the qualified sandbox',
|
||||
);
|
||||
assertScannerVersionOutput(profile.scanner, profile.version, version.stdout, version.stderr);
|
||||
versionHash = scannerVersionHash(version.stdout, version.stderr);
|
||||
if (versionHash !== profile.versionOutputSha256) throw new CsoError('INCOMPATIBLE_INPUT', 'Scanner version output does not match its reviewed image profile');
|
||||
if (versionHash !== profile.versionOutputSha256)
|
||||
throw new CsoError(
|
||||
'INCOMPATIBLE_INPUT',
|
||||
'Scanner version output does not match its reviewed image profile',
|
||||
);
|
||||
observedVersion = profile.version;
|
||||
const execution = await runner.scan();
|
||||
assertSnapshot(input.runDir, input.manifest);
|
||||
outcome = parseScannerOutput(plan, { ...execution, version: profile.version, databaseUpdatedAt: profile.assets?.advisoryDatabase?.updatedAt });
|
||||
} catch (error) { outcome = failure(plan, error, observedVersion); }
|
||||
finally {
|
||||
outcome = parseScannerOutput(plan, {
|
||||
...execution,
|
||||
version: profile.version,
|
||||
databaseUpdatedAt: profile.assets?.advisoryDatabase?.updatedAt,
|
||||
});
|
||||
} catch (error) {
|
||||
outcome = failure(plan, error, observedVersion);
|
||||
} finally {
|
||||
let cleanupError: unknown;
|
||||
if (runner) try { await runner.cleanup(); } catch (error) { cleanupError = error; }
|
||||
if (application) try { await application.cleanup(); } catch (error) { cleanupError ??= error; }
|
||||
if (runner)
|
||||
try {
|
||||
await runner.cleanup();
|
||||
} catch (error) {
|
||||
cleanupError = error;
|
||||
}
|
||||
if (application)
|
||||
try {
|
||||
await application.cleanup();
|
||||
} catch (error) {
|
||||
cleanupError ??= error;
|
||||
}
|
||||
if (cleanupError) outcome = failure(plan, cleanupError, observedVersion);
|
||||
}
|
||||
return { outcome: outcome!, coverage: scannerCoverage(outcome!, input.policy.scope), provenance: {
|
||||
scannerCatalog: catalog.revision, profile: profile?.id ?? null, image: profile?.image ?? null, platform: input.platform,
|
||||
isolationPolicyHash: profile?.isolationPolicyHash ?? null, sourceHash: input.manifest.executionHash, requestHash: sha256(canonical(identityRequest)), versionOutputSha256: versionHash,
|
||||
assets: profile?.assets ?? null, network: plan.network === 'loopback' ? 'isolated-loopback' : 'none', preparation: application?.proof ?? null } };
|
||||
return {
|
||||
outcome: outcome!,
|
||||
coverage: scannerCoverage(outcome!, input.policy.scope),
|
||||
provenance: {
|
||||
scannerCatalog: catalog.revision,
|
||||
profile: profile?.id ?? null,
|
||||
image: profile?.image ?? null,
|
||||
platform: input.platform,
|
||||
isolationPolicyHash: profile?.isolationPolicyHash ?? null,
|
||||
sourceHash: input.manifest.executionHash,
|
||||
requestHash: sha256(canonical(identityRequest)),
|
||||
versionOutputSha256: versionHash,
|
||||
assets: profile?.assets ?? null,
|
||||
network: plan.network === 'loopback' ? 'isolated-loopback' : 'none',
|
||||
preparation: application?.proof ?? null,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export async function prepareDockerScannerApplication(context: Parameters<ScannerApplicationPreparer>[0],dependencies:{endpoint?:DockerEndpoint;runnerFactory?:(options:ConstructorParameters<typeof DockerPreparationSandboxRunner>[0])=>PreparationSandboxRunner;cacheRoot?:string}={}): Promise<ScannerApplicationPreparation> {
|
||||
export async function prepareDockerScannerApplication(
|
||||
context: Parameters<ScannerApplicationPreparer>[0],
|
||||
dependencies: {
|
||||
endpoint?: DockerEndpoint;
|
||||
runnerFactory?: (
|
||||
options: ConstructorParameters<typeof DockerPreparationSandboxRunner>[0],
|
||||
) => PreparationSandboxRunner;
|
||||
cacheRoot?: string;
|
||||
} = {},
|
||||
): Promise<ScannerApplicationPreparation> {
|
||||
const { input, runtime, stack, deadline, catalog } = context;
|
||||
const root = secureDirectory(join(input.runDir, 'supervision', `scanner-preparation-${randomBytes(12).toString('hex')}`));
|
||||
let executor: PreparationExecutor | undefined, prepared: Awaited<ReturnType<PreparationExecutor['prepareOffline']>> | undefined;
|
||||
const root = secureDirectory(
|
||||
join(input.runDir, 'supervision', `scanner-preparation-${randomBytes(12).toString('hex')}`),
|
||||
);
|
||||
let executor: PreparationExecutor | undefined,
|
||||
prepared: Awaited<ReturnType<PreparationExecutor['prepareOffline']>> | undefined;
|
||||
try {
|
||||
const plan = inspectPreparation(join(input.runDir, 'snapshot'), stack);
|
||||
const admission = admitPreparationRuntime({ plan, platform: input.platform, profile: runtime.id, catalog });
|
||||
const endpoint = dependencies.endpoint??await dockerEndpoint(root),runnerOptions={ endpoint, watchdogPath: input.watchdogPath,
|
||||
runRoot: root, controlRoot: secureDirectory(join(root, 'execution')), admission },runner=dependencies.runnerFactory?dependencies.runnerFactory(runnerOptions):new DockerPreparationSandboxRunner(runnerOptions);
|
||||
executor = new PreparationExecutor({ cache: new PublicArchiveCache({ root: dependencies.cacheRoot??publicArchiveCacheRoot(), stagingRoot: secureDirectory(join(root, 'staging')) }),
|
||||
runner, materializationRoot: secureDirectory(join(root, 'materializations')) });
|
||||
const closure = await executor.acquire({ plan, admission, snapshot: join(input.runDir, 'snapshot'), deadline, offline: input.policy.offline });
|
||||
let database:RailsDatabaseSelection|undefined;
|
||||
if(stack==='rails'){
|
||||
if(!plan.database?.selected)throw new CsoError('PREREQUISITE','Rails API preparation could not select one locked database adapter');
|
||||
database=plan.database.selected==='postgresql'
|
||||
?{adapter:'postgresql',sidecar:admitPreparationSidecar({platform:input.platform,catalog})}:{adapter:'sqlite'};
|
||||
const admission = admitPreparationRuntime({
|
||||
plan,
|
||||
platform: input.platform,
|
||||
profile: runtime.id,
|
||||
catalog,
|
||||
});
|
||||
const endpoint = dependencies.endpoint ?? (await dockerEndpoint(root)),
|
||||
runnerOptions = {
|
||||
endpoint,
|
||||
watchdogPath: input.watchdogPath,
|
||||
runRoot: root,
|
||||
controlRoot: secureDirectory(join(root, 'execution')),
|
||||
admission,
|
||||
},
|
||||
runner = dependencies.runnerFactory
|
||||
? dependencies.runnerFactory(runnerOptions)
|
||||
: new DockerPreparationSandboxRunner(runnerOptions);
|
||||
executor = new PreparationExecutor({
|
||||
cache: new PublicArchiveCache({
|
||||
root: dependencies.cacheRoot ?? publicArchiveCacheRoot(),
|
||||
stagingRoot: secureDirectory(join(root, 'staging')),
|
||||
}),
|
||||
runner,
|
||||
materializationRoot: secureDirectory(join(root, 'materializations')),
|
||||
});
|
||||
const closure = await executor.acquire({
|
||||
plan,
|
||||
admission,
|
||||
snapshot: join(input.runDir, 'snapshot'),
|
||||
deadline,
|
||||
offline: input.policy.offline,
|
||||
});
|
||||
let database: RailsDatabaseSelection | undefined;
|
||||
if (stack === 'rails') {
|
||||
if (!plan.database?.selected)
|
||||
throw new CsoError(
|
||||
'PREREQUISITE',
|
||||
'Rails API preparation could not select one locked database adapter',
|
||||
);
|
||||
database =
|
||||
plan.database.selected === 'postgresql'
|
||||
? { adapter: 'postgresql', sidecar: admitPreparationSidecar({ platform: input.platform, catalog }) }
|
||||
: { adapter: 'sqlite' };
|
||||
}
|
||||
prepared = await executor.prepareOffline({ plan, admission, snapshot: join(input.runDir, 'snapshot'), closure, deadline, database });
|
||||
const proof = { dependencyClosureHash: prepared.dependencyClosureHash, preparedManifestHash: prepared.preparedManifestHash,
|
||||
sourceProjectionHash: prepared.sourceProjectionHash, receiptHash: prepared.receiptHash,
|
||||
executionEnvironmentHash: sha256(canonical(prepared.executionEnvironment)), databaseHash: prepared.databaseHash };
|
||||
prepared = await executor.prepareOffline({
|
||||
plan,
|
||||
admission,
|
||||
snapshot: join(input.runDir, 'snapshot'),
|
||||
closure,
|
||||
deadline,
|
||||
database,
|
||||
});
|
||||
const proof = {
|
||||
dependencyClosureHash: prepared.dependencyClosureHash,
|
||||
preparedManifestHash: prepared.preparedManifestHash,
|
||||
sourceProjectionHash: prepared.sourceProjectionHash,
|
||||
receiptHash: prepared.receiptHash,
|
||||
executionEnvironmentHash: sha256(canonical(prepared.executionEnvironment)),
|
||||
databaseHash: prepared.databaseHash,
|
||||
};
|
||||
let cleaned = false;
|
||||
return { sourceRoot: prepared.preparedRoot, environment: prepared.executionEnvironment, database: prepared.database, proof, cleanup: async () => {
|
||||
if (cleaned) return; cleaned = true;
|
||||
await executor!.dispose(prepared!);
|
||||
fs.rmSync(root, { recursive: true, force: false });
|
||||
} };
|
||||
return {
|
||||
sourceRoot: prepared.preparedRoot,
|
||||
environment: prepared.executionEnvironment,
|
||||
database: prepared.database,
|
||||
proof,
|
||||
cleanup: async () => {
|
||||
if (cleaned) return;
|
||||
cleaned = true;
|
||||
await executor!.dispose(prepared!);
|
||||
fs.rmSync(root, { recursive: true, force: false });
|
||||
},
|
||||
};
|
||||
} catch (error) {
|
||||
let cleanupError:unknown;
|
||||
if (prepared && executor) try { await executor.dispose(prepared); } catch (failed) { cleanupError=failed; }
|
||||
let cleanupError: unknown;
|
||||
if (prepared && executor)
|
||||
try {
|
||||
await executor.dispose(prepared);
|
||||
} catch (failed) {
|
||||
cleanupError = failed;
|
||||
}
|
||||
// A failed Docker/retained-copy cleanup deliberately hands ownership to a
|
||||
// detached watchdog. Its journals and label-sweep scratch files live below
|
||||
// this root, so only remove the tree after every watchdog acknowledged.
|
||||
let pending=true;try{pending=hasPendingWatchdogCleanup(input.runDir);}catch(failed){cleanupError??=failed;}
|
||||
if(!cleanupError&&!pending)try { fs.rmSync(root, { recursive: true, force: false }); } catch {}
|
||||
if(cleanupError)throw cleanupError;
|
||||
let pending = true;
|
||||
try {
|
||||
pending = hasPendingWatchdogCleanup(input.runDir);
|
||||
} catch (failed) {
|
||||
cleanupError ??= failed;
|
||||
}
|
||||
if (!cleanupError && !pending)
|
||||
try {
|
||||
fs.rmSync(root, { recursive: true, force: false });
|
||||
} catch {}
|
||||
if (cleanupError) throw cleanupError;
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
@@ -320,8 +696,10 @@ export async function createDockerScannerRunner(context: ScannerRunnerContext):
|
||||
const policyDir = secureDirectory(join(controlDir, 'policy'));
|
||||
const files: Array<{ host: string; container: string }> = [];
|
||||
const writePolicy = (container: string, content: string): void => {
|
||||
if (redact(content) !== content) throw new CsoError('REDACTION_FAILED', 'Scanner policy contains secret-bearing material');
|
||||
const host = join(policyDir, String(files.length)); fs.writeFileSync(host, content, { mode: 0o600, flag: 'wx' });
|
||||
if (redact(content) !== content)
|
||||
throw new CsoError('REDACTION_FAILED', 'Scanner policy contains secret-bearing material');
|
||||
const host = join(policyDir, String(files.length));
|
||||
fs.writeFileSync(host, content, { mode: 0o600, flag: 'wx' });
|
||||
files.push({ host, container });
|
||||
};
|
||||
let group: DockerGroup | undefined;
|
||||
@@ -329,12 +707,27 @@ export async function createDockerScannerRunner(context: ScannerRunnerContext):
|
||||
for (const file of plan.trustedFiles) writePolicy(file.path, file.content);
|
||||
if (input.request?.api) writePolicy('/policy/openapi.json', JSON.stringify(input.request.api.schema));
|
||||
const endpoint: DockerEndpoint = await dockerEndpoint(controlDir);
|
||||
group = await DockerGroup.create(endpoint, attempt, controlDir, deadline, profile.image, input.watchdogPath);
|
||||
const createScanner=()=>group!.createContainer({ role: runtime ? 'verifier' : 'app', image: profile.image, source: join(input.runDir, 'snapshot'), command: ['/bin/sleep', '2147483647'], env: plan.env, readonlyFiles: files });
|
||||
group = await DockerGroup.create(
|
||||
endpoint,
|
||||
attempt,
|
||||
controlDir,
|
||||
deadline,
|
||||
profile.image,
|
||||
input.watchdogPath,
|
||||
);
|
||||
const createScanner = () =>
|
||||
group!.createContainer({
|
||||
role: runtime ? 'verifier' : 'app',
|
||||
image: profile.image,
|
||||
source: join(input.runDir, 'snapshot'),
|
||||
command: ['/bin/sleep', '2147483647'],
|
||||
env: plan.env,
|
||||
readonlyFiles: files,
|
||||
});
|
||||
let scanner = await createScanner();
|
||||
await group.start(scanner);
|
||||
const capture = async (command: string[]): Promise<ScannerExecution> => {
|
||||
if(!scanner)throw new CsoError('ISOLATION_FAILED','Scanner container is unavailable');
|
||||
if (!scanner) throw new CsoError('ISOLATION_FAILED', 'Scanner container is unavailable');
|
||||
const result = await group!.execCapture(scanner, command, { workdir: '/work', env: plan.env });
|
||||
return { stdout: result.stdout, stderr: result.stderr, exitCode: result.code };
|
||||
};
|
||||
@@ -343,41 +736,132 @@ export async function createDockerScannerRunner(context: ScannerRunnerContext):
|
||||
scan: async () => {
|
||||
const api = input.request?.api;
|
||||
if (api && runtime) {
|
||||
if (!application) throw new CsoError('ISOLATION_FAILED', 'Schemathesis application was not materialized through offline preparation');
|
||||
const env={ ...application.environment, PORT: String(api.port), HOST: '127.0.0.1', NODE_ENV: 'test', RAILS_ENV: 'test', RACK_ENV: 'test', PYTHONUNBUFFERED: '1', CI: '1', SECRET_KEY_BASE: 'cso-synthetic-test-key' };
|
||||
const rails=runtime.stack==='rails';
|
||||
if(rails){await group!.removeContainer(scanner);scanner='';}
|
||||
if(application.database?.adapter==='postgresql'){
|
||||
const databaseFile=join(policyDir,'postgresql.databases'),names=application.database.connections.map(name=>`cso_${name}`);
|
||||
if(!names.length||names.some(name=>!/^cso_[A-Za-z_][A-Za-z0-9_]{0,47}$/.test(name)))throw new CsoError('INCOMPATIBLE_INPUT','Prepared PostgreSQL connection names are invalid');
|
||||
fs.writeFileSync(databaseFile,names.join('\n')+'\n',{mode:0o444,flag:'wx'});
|
||||
const postgres=await group!.createContainer({role:'postgres',image:application.database.sidecar.image,command:['/opt/cso/run-postgresql','/policy/postgresql.databases'],postgresDatabasePolicy:databaseFile});await group!.start(postgres);
|
||||
let ready=false;for(let attempt=0;attempt<100&&!ready;attempt++){const checked=await group!.execCapture(postgres,['/opt/cso/postgresql-ready','/policy/postgresql.databases']);ready=checked.code===0;if(!ready)await new Promise(resolveWait=>setTimeout(resolveWait,50));}
|
||||
if(!ready)throw new CsoError('TOOL_FAILED','Disposable PostgreSQL did not become ready for Rails API scanning');
|
||||
if (!application)
|
||||
throw new CsoError(
|
||||
'ISOLATION_FAILED',
|
||||
'Schemathesis application was not materialized through offline preparation',
|
||||
);
|
||||
const env = {
|
||||
...application.environment,
|
||||
PORT: String(api.port),
|
||||
HOST: '127.0.0.1',
|
||||
NODE_ENV: 'test',
|
||||
RAILS_ENV: 'test',
|
||||
RACK_ENV: 'test',
|
||||
PYTHONUNBUFFERED: '1',
|
||||
CI: '1',
|
||||
SECRET_KEY_BASE: 'cso-synthetic-test-key',
|
||||
};
|
||||
const rails = runtime.stack === 'rails';
|
||||
if (rails) {
|
||||
await group!.removeContainer(scanner);
|
||||
scanner = '';
|
||||
}
|
||||
const app = await group!.createContainer({ role: 'app', image: runtime.image, source: application.sourceRoot, env,
|
||||
command:rails?['/opt/cso/run-app','/bin/sleep','2147483647']:['/opt/cso/run-app', api.start.executable, ...api.start.args] });
|
||||
if (application.database?.adapter === 'postgresql') {
|
||||
const databaseFile = join(policyDir, 'postgresql.databases'),
|
||||
names = application.database.connections.map((name) => `cso_${name}`);
|
||||
if (!names.length || names.some((name) => !/^cso_[A-Za-z_][A-Za-z0-9_]{0,47}$/.test(name)))
|
||||
throw new CsoError('INCOMPATIBLE_INPUT', 'Prepared PostgreSQL connection names are invalid');
|
||||
fs.writeFileSync(databaseFile, names.join('\n') + '\n', { mode: 0o444, flag: 'wx' });
|
||||
const postgres = await group!.createContainer({
|
||||
role: 'postgres',
|
||||
image: application.database.sidecar.image,
|
||||
command: ['/opt/cso/run-postgresql', '/policy/postgresql.databases'],
|
||||
postgresDatabasePolicy: databaseFile,
|
||||
});
|
||||
await group!.start(postgres);
|
||||
let ready = false;
|
||||
for (let attempt = 0; attempt < 100 && !ready; attempt++) {
|
||||
const checked = await group!.execCapture(postgres, [
|
||||
'/opt/cso/postgresql-ready',
|
||||
'/policy/postgresql.databases',
|
||||
]);
|
||||
ready = checked.code === 0;
|
||||
if (!ready) await new Promise((resolveWait) => setTimeout(resolveWait, 50));
|
||||
}
|
||||
if (!ready)
|
||||
throw new CsoError(
|
||||
'TOOL_FAILED',
|
||||
'Disposable PostgreSQL did not become ready for Rails API scanning',
|
||||
);
|
||||
}
|
||||
const app = await group!.createContainer({
|
||||
role: 'app',
|
||||
image: runtime.image,
|
||||
source: application.sourceRoot,
|
||||
env,
|
||||
command: rails
|
||||
? ['/opt/cso/run-app', '/bin/sleep', '2147483647']
|
||||
: ['/opt/cso/run-app', api.start.executable, ...api.start.args],
|
||||
});
|
||||
await group!.start(app);
|
||||
if(rails){const clean=['/usr/bin/env','-i',...Object.entries(env).sort(([a],[b])=>a.localeCompare(b)).map(([key,value])=>`${key}=${value}`),'/usr/local/bin/bundle','exec','rails','db:prepare'];const prepared=await group!.execCapture(app,clean,{workdir:'/work'});if(prepared.code!==0)throw new CsoError('TOOL_FAILED','Rails API database preparation failed');await group!.execDetached(app,[api.start.executable,...api.start.args]);}
|
||||
const security = { ...api.control, vulnerable: { status: api.control.expected.status === 599 ? 598 : 599 } };
|
||||
if (rails) {
|
||||
const clean = [
|
||||
'/usr/bin/env',
|
||||
'-i',
|
||||
...Object.entries(env)
|
||||
.sort(([a], [b]) => a.localeCompare(b))
|
||||
.map(([key, value]) => `${key}=${value}`),
|
||||
'/usr/local/bin/bundle',
|
||||
'exec',
|
||||
'rails',
|
||||
'db:prepare',
|
||||
];
|
||||
const prepared = await group!.execCapture(app, clean, { workdir: '/work' });
|
||||
if (prepared.code !== 0)
|
||||
throw new CsoError('TOOL_FAILED', 'Rails API database preparation failed');
|
||||
await group!.execDetached(app, [api.start.executable, ...api.start.args]);
|
||||
}
|
||||
const security = {
|
||||
...api.control,
|
||||
vulnerable: { status: api.control.expected.status === 599 ? 598 : 599 },
|
||||
};
|
||||
const controlFile = join(policyDir, 'control.json');
|
||||
fs.writeFileSync(controlFile, JSON.stringify({ phase: 'after', port: api.port, legitimate: [api.control], security }), { mode: 0o600, flag: 'wx' });
|
||||
const probe = await group!.createContainer({ role: schemathesisControlRole(), image: runtime.image, command: ['/opt/cso/verifier', '/policy/control.json'], readonlyFiles: [{ host: controlFile, container: '/policy/control.json' }] });
|
||||
const observed = await group!.startAttach(probe); await group!.removeContainer(probe);
|
||||
fs.writeFileSync(
|
||||
controlFile,
|
||||
JSON.stringify({ phase: 'after', port: api.port, legitimate: [api.control], security }),
|
||||
{ mode: 0o600, flag: 'wx' },
|
||||
);
|
||||
const probe = await group!.createContainer({
|
||||
role: schemathesisControlRole(),
|
||||
image: runtime.image,
|
||||
command: ['/opt/cso/verifier', '/policy/control.json'],
|
||||
readonlyFiles: [{ host: controlFile, container: '/policy/control.json' }],
|
||||
});
|
||||
const observed = await group!.startAttach(probe);
|
||||
await group!.removeContainer(probe);
|
||||
let valid = false;
|
||||
try { const v = validateVerificationObservation(JSON.parse(observed.output)); valid = observed.code === 0 && v.booted && v.legitimate && v.security === 'pass'; } catch {}
|
||||
if (!valid) throw new CsoError('PREREQUISITE', 'API application boot or legitimate control failed; no Schemathesis requests were sent');
|
||||
if(rails){scanner=await createScanner();await group!.start(scanner);}
|
||||
try {
|
||||
const v = validateVerificationObservation(JSON.parse(observed.output));
|
||||
valid = observed.code === 0 && v.booted && v.legitimate && v.security === 'pass';
|
||||
} catch {}
|
||||
if (!valid)
|
||||
throw new CsoError(
|
||||
'PREREQUISITE',
|
||||
'API application boot or legitimate control failed; no Schemathesis requests were sent',
|
||||
);
|
||||
if (rails) {
|
||||
scanner = await createScanner();
|
||||
await group!.start(scanner);
|
||||
}
|
||||
}
|
||||
const execution = await capture([profile.executable, ...plan.args]);
|
||||
if (plan.outputPath) {
|
||||
const report = await capture(['/bin/cat', plan.outputPath]);
|
||||
if (report.exitCode !== 0) throw new CsoError('PREREQUISITE', 'Scanner did not produce its required bounded report file');
|
||||
return { ...execution, stdout: report.stdout, stderr: [execution.stderr, report.stderr].filter(Boolean).join('\n') };
|
||||
if (report.exitCode !== 0)
|
||||
throw new CsoError('PREREQUISITE', 'Scanner did not produce its required bounded report file');
|
||||
return {
|
||||
...execution,
|
||||
stdout: report.stdout,
|
||||
stderr: [execution.stderr, report.stderr].filter(Boolean).join('\n'),
|
||||
};
|
||||
}
|
||||
return execution;
|
||||
},
|
||||
cleanup: async () => { await group!.cleanup(); fs.rmSync(policyDir, { recursive: true, force: true }); },
|
||||
cleanup: async () => {
|
||||
await group!.cleanup();
|
||||
fs.rmSync(policyDir, { recursive: true, force: true });
|
||||
},
|
||||
};
|
||||
} catch (error) {
|
||||
if (group) await group.cleanup();
|
||||
|
||||
+581
-126
@@ -8,8 +8,9 @@ import { posix } from 'node:path';
|
||||
import { redactFindingSpans } from '../redact-engine';
|
||||
|
||||
export const SCANNER_IDS = ['gitleaks', 'osv', 'semgrep', 'zizmor', 'trivy', 'schemathesis'] as const;
|
||||
export type ScannerId = typeof SCANNER_IDS[number];
|
||||
export type ScannerFormat = 'gitleaks-json' | 'osv-json' | 'semgrep-json' | 'sarif' | 'trivy-json' | 'schemathesis-json';
|
||||
export type ScannerId = (typeof SCANNER_IDS)[number];
|
||||
export type ScannerFormat =
|
||||
'gitleaks-json' | 'osv-json' | 'semgrep-json' | 'sarif' | 'trivy-json' | 'schemathesis-json';
|
||||
export const MAX_SCANNER_OUTPUT_BYTES = 1_048_576;
|
||||
const MAX_CANDIDATES = 5_000;
|
||||
|
||||
@@ -63,7 +64,13 @@ export interface ScannerCandidate {
|
||||
reportedSeverity: 'critical' | 'high' | 'medium' | 'low' | 'info' | 'unknown';
|
||||
location?: { path: string; line?: number; column?: number };
|
||||
advisoryIds: string[];
|
||||
dependency?: { name: string; version?: string; ecosystem?: string; reachability: 'unknown'; exposure: 'unknown' };
|
||||
dependency?: {
|
||||
name: string;
|
||||
version?: string;
|
||||
ecosystem?: string;
|
||||
reachability: 'unknown';
|
||||
exposure: 'unknown';
|
||||
};
|
||||
operation?: string;
|
||||
suppressed: boolean;
|
||||
evidence: 'scanner-candidate';
|
||||
@@ -71,10 +78,25 @@ export interface ScannerCandidate {
|
||||
}
|
||||
|
||||
export interface ScannerGap {
|
||||
code: 'UNAVAILABLE' | 'PREREQUISITE' | 'TIMEOUT' | 'OUTPUT_LIMIT' | 'INVALID_OUTPUT' | 'TOOL_FAILED' |
|
||||
'REDACTION_FAILED' | 'ISOLATION_FAILED' | 'PERSISTENCE_FAILED' | 'SNAPSHOT_RACE' | 'CANCELLED' |
|
||||
'INSUFFICIENT_CAPACITY' | 'UNSAFE_PATH' | 'MISSING_INPUT' | 'INCOMPATIBLE_INPUT' |
|
||||
'UNSAFE_LOCATION' | 'SKIPPED_INPUT' | 'UNKNOWN_FRESHNESS';
|
||||
code:
|
||||
| 'UNAVAILABLE'
|
||||
| 'PREREQUISITE'
|
||||
| 'TIMEOUT'
|
||||
| 'OUTPUT_LIMIT'
|
||||
| 'INVALID_OUTPUT'
|
||||
| 'TOOL_FAILED'
|
||||
| 'REDACTION_FAILED'
|
||||
| 'ISOLATION_FAILED'
|
||||
| 'PERSISTENCE_FAILED'
|
||||
| 'SNAPSHOT_RACE'
|
||||
| 'CANCELLED'
|
||||
| 'INSUFFICIENT_CAPACITY'
|
||||
| 'UNSAFE_PATH'
|
||||
| 'MISSING_INPUT'
|
||||
| 'INCOMPATIBLE_INPUT'
|
||||
| 'UNSAFE_LOCATION'
|
||||
| 'SKIPPED_INPUT'
|
||||
| 'UNKNOWN_FRESHNESS';
|
||||
message: string;
|
||||
}
|
||||
|
||||
@@ -109,34 +131,64 @@ export interface ScannerExecution {
|
||||
|
||||
const SOURCES: Record<ScannerId, string[]> = {
|
||||
gitleaks: ['https://github.com/gitleaks/gitleaks/blob/master/README.md'],
|
||||
osv: ['https://google.github.io/osv-scanner/usage/scan-source/', 'https://google.github.io/osv-scanner/usage/offline-mode/'],
|
||||
osv: [
|
||||
'https://google.github.io/osv-scanner/usage/scan-source/',
|
||||
'https://google.github.io/osv-scanner/usage/offline-mode/',
|
||||
],
|
||||
semgrep: ['https://docs.semgrep.dev/cli-reference'],
|
||||
zizmor: ['https://docs.zizmor.sh/usage/', 'https://docs.zizmor.sh/quickstart/'],
|
||||
trivy: ['https://trivy.dev/docs/dev/docs/advanced/telemetry/', 'https://trivy.dev/docs/latest/guide/advanced/air-gap/'],
|
||||
schemathesis: ['https://schemathesis.readthedocs.io/en/stable/reference/cli/', 'https://github.com/schemathesis/schemathesis/blob/master/src/schemathesis/cli/json_report.py'],
|
||||
trivy: [
|
||||
'https://trivy.dev/docs/dev/docs/advanced/telemetry/',
|
||||
'https://trivy.dev/docs/latest/guide/advanced/air-gap/',
|
||||
],
|
||||
schemathesis: [
|
||||
'https://schemathesis.readthedocs.io/en/stable/reference/cli/',
|
||||
'https://github.com/schemathesis/schemathesis/blob/master/src/schemathesis/cli/json_report.py',
|
||||
],
|
||||
};
|
||||
|
||||
function absolutePath(value: string, name: string): string {
|
||||
if (value === '/' || !value.startsWith('/') || value.startsWith('//') || /[\x00-\x1f\\]/.test(value) || value.split('/').includes('..')) {
|
||||
if (
|
||||
value === '/' ||
|
||||
!value.startsWith('/') ||
|
||||
value.startsWith('//') ||
|
||||
/[\x00-\x1f\\]/.test(value) ||
|
||||
value.split('/').includes('..')
|
||||
) {
|
||||
throw new Error(`${name} must be an absolute sandbox path without traversal`);
|
||||
}
|
||||
return posix.normalize(value);
|
||||
}
|
||||
|
||||
function positiveInteger(value: number, max: number, name: string): number {
|
||||
if (!Number.isSafeInteger(value) || value < 1 || value > max) throw new Error(`${name} must be between 1 and ${max}`);
|
||||
if (!Number.isSafeInteger(value) || value < 1 || value > max)
|
||||
throw new Error(`${name} must be between 1 and ${max}`);
|
||||
return value;
|
||||
}
|
||||
|
||||
/** Numeric loopback only: no DNS, URL credentials, redirected targets, or remote schemas. */
|
||||
export function validateScannerBaseUrl(raw: string): string {
|
||||
let url: URL;
|
||||
try { url = new URL(raw); } catch { throw new Error('Schemathesis requires a numeric loopback HTTP URL'); }
|
||||
if (!['http:', 'https:'].includes(url.protocol) || !['127.0.0.1', '[::1]'].includes(url.hostname) || url.username || url.password || url.hash || url.search) {
|
||||
throw new Error('Schemathesis requires a numeric loopback HTTP URL without credentials, query, or fragment');
|
||||
try {
|
||||
url = new URL(raw);
|
||||
} catch {
|
||||
throw new Error('Schemathesis requires a numeric loopback HTTP URL');
|
||||
}
|
||||
if (
|
||||
!['http:', 'https:'].includes(url.protocol) ||
|
||||
!['127.0.0.1', '[::1]'].includes(url.hostname) ||
|
||||
url.username ||
|
||||
url.password ||
|
||||
url.hash ||
|
||||
url.search
|
||||
) {
|
||||
throw new Error(
|
||||
'Schemathesis requires a numeric loopback HTTP URL without credentials, query, or fragment',
|
||||
);
|
||||
}
|
||||
// URL canonicalization accepts integer, hex, and shorthand IPv4. Reject these spellings.
|
||||
if (!/^https?:\/\/(127\.0\.0\.1|\[::1\])(?::\d+)?(?:\/|$)/.test(raw)) throw new Error('Schemathesis requires canonical numeric loopback');
|
||||
if (!/^https?:\/\/(127\.0\.0\.1|\[::1\])(?::\d+)?(?:\/|$)/.test(raw))
|
||||
throw new Error('Schemathesis requires canonical numeric loopback');
|
||||
return url.href;
|
||||
}
|
||||
|
||||
@@ -148,99 +200,288 @@ export function validateScannerBaseUrl(raw: string): string {
|
||||
export function scannerPlans(opts: ScannerOptions): ScannerPlan[] {
|
||||
const root = absolutePath(opts.snapshotRoot, 'snapshotRoot');
|
||||
const policy = absolutePath(opts.policyRoot ?? '/policy', 'policyRoot');
|
||||
if (policy === root || policy.startsWith(`${root}/`) || root.startsWith(`${policy}/`)) throw new Error('policyRoot must be separate from source');
|
||||
if (policy === root || policy.startsWith(`${root}/`) || root.startsWith(`${policy}/`))
|
||||
throw new Error('policyRoot must be separate from source');
|
||||
const cache = opts.advisoryCache ? absolutePath(opts.advisoryCache, 'advisoryCache') : undefined;
|
||||
if (cache && (cache === root || cache.startsWith(`${root}/`))) throw new Error('advisoryCache must be separate from source');
|
||||
if (cache && (cache === root || cache.startsWith(`${root}/`)))
|
||||
throw new Error('advisoryCache must be separate from source');
|
||||
const timeout = positiveInteger(opts.deadlineSeconds ?? 120, 300, 'deadlineSeconds');
|
||||
const selected = opts.selected ?? [...SCANNER_IDS];
|
||||
if (new Set(selected).size !== selected.length || selected.some(id => !SCANNER_IDS.includes(id))) throw new Error('Invalid or duplicate scanner selection');
|
||||
return selected.map(id => {
|
||||
if (new Set(selected).size !== selected.length || selected.some((id) => !SCANNER_IDS.includes(id)))
|
||||
throw new Error('Invalid or duplicate scanner selection');
|
||||
return selected.map((id) => {
|
||||
const plan: ScannerPlan = {
|
||||
id, executableName: id === 'osv' ? 'osv-scanner' : id, args: [], versionArgs: ['--version'], requiredFeatures: [],
|
||||
format: 'sarif', execution: 'sandbox', network: 'none', cwd: '/work', sourceRoot: root,
|
||||
env: { HOME: '/work/home', TMPDIR: '/tmp', LANG: 'C.UTF-8', NO_COLOR: '1' }, trustedFiles: [], prerequisites: [],
|
||||
timeoutSeconds: timeout, maxOutputBytes: MAX_SCANNER_OUTPUT_BYTES,
|
||||
coverage: { domain: id, scope: [root], exclusions: ['Snapshot transformations apply; inspect the snapshot manifest.'] },
|
||||
provenanceSources: SOURCES[id], documentationInspectedAt: '2026-09-09',
|
||||
id,
|
||||
executableName: id === 'osv' ? 'osv-scanner' : id,
|
||||
args: [],
|
||||
versionArgs: ['--version'],
|
||||
requiredFeatures: [],
|
||||
format: 'sarif',
|
||||
execution: 'sandbox',
|
||||
network: 'none',
|
||||
cwd: '/work',
|
||||
sourceRoot: root,
|
||||
env: { HOME: '/work/home', TMPDIR: '/tmp', LANG: 'C.UTF-8', NO_COLOR: '1' },
|
||||
trustedFiles: [],
|
||||
prerequisites: [],
|
||||
timeoutSeconds: timeout,
|
||||
maxOutputBytes: MAX_SCANNER_OUTPUT_BYTES,
|
||||
coverage: {
|
||||
domain: id,
|
||||
scope: [root],
|
||||
exclusions: ['Snapshot transformations apply; inspect the snapshot manifest.'],
|
||||
},
|
||||
provenanceSources: SOURCES[id],
|
||||
documentationInspectedAt: '2026-09-09',
|
||||
};
|
||||
if (opts.tools?.[id]?.available === false) plan.prerequisites.push(`Install a reviewed ${plan.executableName} executable in the scanner image.`);
|
||||
if (opts.tools?.[id]?.available === false)
|
||||
plan.prerequisites.push(`Install a reviewed ${plan.executableName} executable in the scanner image.`);
|
||||
switch (id) {
|
||||
case 'gitleaks': {
|
||||
const target = opts.gitHistory ? absolutePath(opts.gitHistory, 'gitHistory') : root;
|
||||
plan.format = 'gitleaks-json';
|
||||
plan.coverage.domain = 'secrets';
|
||||
plan.coverage.scope = [target];
|
||||
plan.trustedFiles.push({ path: `${policy}/gitleaks.toml`, content: '[extend]\nuseDefault = true\n' }, { path: `${policy}/gitleaksignore`, content: '' });
|
||||
plan.args = [opts.gitHistory ? 'git' : 'dir', '--redact=100', '--no-banner', '--no-color', '--ignore-gitleaks-allow', '--gitleaks-ignore-path', `${policy}/gitleaksignore`, '--config', `${policy}/gitleaks.toml`, '--report-format=json', '--report-path=-', '--exit-code=10', '--timeout', String(timeout), target];
|
||||
if (opts.gitHistory) plan.prerequisites.push('History input must be a sanitized Git object store with trusted config and no hooks, filters, alternates, or external helpers.');
|
||||
plan.trustedFiles.push(
|
||||
{ path: `${policy}/gitleaks.toml`, content: '[extend]\nuseDefault = true\n' },
|
||||
{ path: `${policy}/gitleaksignore`, content: '' },
|
||||
);
|
||||
plan.args = [
|
||||
opts.gitHistory ? 'git' : 'dir',
|
||||
'--redact=100',
|
||||
'--no-banner',
|
||||
'--no-color',
|
||||
'--ignore-gitleaks-allow',
|
||||
'--gitleaks-ignore-path',
|
||||
`${policy}/gitleaksignore`,
|
||||
'--config',
|
||||
`${policy}/gitleaks.toml`,
|
||||
'--report-format=json',
|
||||
'--report-path=-',
|
||||
'--exit-code=10',
|
||||
'--timeout',
|
||||
String(timeout),
|
||||
target,
|
||||
];
|
||||
if (opts.gitHistory)
|
||||
plan.prerequisites.push(
|
||||
'History input must be a sanitized Git object store with trusted config and no hooks, filters, alternates, or external helpers.',
|
||||
);
|
||||
else plan.coverage.exclusions.push('Historical revisions are not scanned by this directory pass.');
|
||||
plan.requiredFeatures = ['dir', '--redact', '--ignore-gitleaks-allow'];
|
||||
break;
|
||||
}
|
||||
case 'osv':
|
||||
plan.format = 'osv-json'; plan.coverage.domain = 'dependencies';
|
||||
plan.format = 'osv-json';
|
||||
plan.coverage.domain = 'dependencies';
|
||||
plan.trustedFiles.push({ path: `${policy}/osv-scanner.toml`, content: '' });
|
||||
plan.args = ['scan', 'source', '--format=json', '--offline', '--no-call-analysis=all', '--config', `${policy}/osv-scanner.toml`, '--recursive', root];
|
||||
plan.args = [
|
||||
'scan',
|
||||
'source',
|
||||
'--format=json',
|
||||
'--offline',
|
||||
'--no-call-analysis=all',
|
||||
'--config',
|
||||
`${policy}/osv-scanner.toml`,
|
||||
'--recursive',
|
||||
root,
|
||||
];
|
||||
plan.requiredFeatures = ['scan source', '--offline', '--no-call-analysis'];
|
||||
if (cache) plan.env.OSV_SCANNER_LOCAL_DB_CACHE_DIRECTORY = cache;
|
||||
else plan.prerequisites.push('Provide verified offline OSV databases for every assessed ecosystem.');
|
||||
plan.coverage.exclusions.push('Call analysis is disabled; dependency reachability remains unknown until independently investigated.');
|
||||
plan.coverage.exclusions.push(
|
||||
'Call analysis is disabled; dependency reachability remains unknown until independently investigated.',
|
||||
);
|
||||
break;
|
||||
case 'semgrep': {
|
||||
plan.format = 'semgrep-json'; plan.coverage.domain = 'code';
|
||||
const rules = opts.semgrepRules ? absolutePath(opts.semgrepRules, 'semgrepRules') : `${policy}/semgrep.yml`;
|
||||
if (!rules.startsWith(`${policy}/`)) throw new Error('Semgrep rules must be below the trusted policyRoot');
|
||||
if (!opts.semgrepRules) plan.prerequisites.push('Provide a reviewed, pinned local Semgrep ruleset; registry aliases and repo rules are not accepted.');
|
||||
plan.args = ['scan', '--json', '--config', rules, '--metrics=off', '--disable-version-check', '--disable-nosem', '--no-git-ignore', '--no-secrets-validation', '--oss-only', '--no-autofix', '--timeout=10', '--timeout-threshold=3', '--jobs=1', root];
|
||||
plan.env.SEMGREP_SEND_METRICS = 'off'; plan.env.SEMGREP_ENABLE_VERSION_CHECK = '0'; plan.env.SEMGREP_APP_TOKEN = '';
|
||||
plan.requiredFeatures = ['scan', '--metrics', '--disable-version-check', '--no-secrets-validation', '--oss-only'];
|
||||
plan.coverage.exclusions.push('Semgrep language support, built-in file selection, and .semgrepignore rules can exclude inputs; independently inspect these exclusions.');
|
||||
plan.format = 'semgrep-json';
|
||||
plan.coverage.domain = 'code';
|
||||
const rules = opts.semgrepRules
|
||||
? absolutePath(opts.semgrepRules, 'semgrepRules')
|
||||
: `${policy}/semgrep.yml`;
|
||||
if (!rules.startsWith(`${policy}/`))
|
||||
throw new Error('Semgrep rules must be below the trusted policyRoot');
|
||||
if (!opts.semgrepRules)
|
||||
plan.prerequisites.push(
|
||||
'Provide a reviewed, pinned local Semgrep ruleset; registry aliases and repo rules are not accepted.',
|
||||
);
|
||||
plan.args = [
|
||||
'scan',
|
||||
'--json',
|
||||
'--config',
|
||||
rules,
|
||||
'--metrics=off',
|
||||
'--disable-version-check',
|
||||
'--disable-nosem',
|
||||
'--no-git-ignore',
|
||||
'--no-secrets-validation',
|
||||
'--oss-only',
|
||||
'--no-autofix',
|
||||
'--timeout=10',
|
||||
'--timeout-threshold=3',
|
||||
'--jobs=1',
|
||||
root,
|
||||
];
|
||||
plan.env.SEMGREP_SEND_METRICS = 'off';
|
||||
plan.env.SEMGREP_ENABLE_VERSION_CHECK = '0';
|
||||
plan.env.SEMGREP_APP_TOKEN = '';
|
||||
plan.requiredFeatures = [
|
||||
'scan',
|
||||
'--metrics',
|
||||
'--disable-version-check',
|
||||
'--no-secrets-validation',
|
||||
'--oss-only',
|
||||
];
|
||||
plan.coverage.exclusions.push(
|
||||
'Semgrep language support, built-in file selection, and .semgrepignore rules can exclude inputs; independently inspect these exclusions.',
|
||||
);
|
||||
break;
|
||||
}
|
||||
case 'zizmor':
|
||||
plan.coverage.domain = 'github-actions';
|
||||
plan.args = ['--offline', '--no-config', '--no-ignores', '--no-exit-codes', '--no-progress', '--color=never', '--format=sarif', root];
|
||||
plan.env.ZIZMOR_OFFLINE = '1'; plan.requiredFeatures = ['--offline', '--no-config', '--no-ignores'];
|
||||
plan.coverage.exclusions.push('Online GitHub audits and remote reusable action inspection require separate assessment.');
|
||||
plan.args = [
|
||||
'--offline',
|
||||
'--no-config',
|
||||
'--no-ignores',
|
||||
'--no-exit-codes',
|
||||
'--no-progress',
|
||||
'--color=never',
|
||||
'--format=sarif',
|
||||
root,
|
||||
];
|
||||
plan.env.ZIZMOR_OFFLINE = '1';
|
||||
plan.requiredFeatures = ['--offline', '--no-config', '--no-ignores'];
|
||||
plan.coverage.exclusions.push(
|
||||
'Online GitHub audits and remote reusable action inspection require separate assessment.',
|
||||
);
|
||||
break;
|
||||
case 'trivy':
|
||||
plan.format = 'trivy-json'; plan.coverage.domain = 'dependencies-and-infrastructure';
|
||||
plan.trustedFiles.push({ path: `${policy}/trivy.yaml`, content: '{}\n' }, { path: `${policy}/trivyignore`, content: '' });
|
||||
plan.args = ['fs', '--format=json', '--config', `${policy}/trivy.yaml`, '--ignorefile', `${policy}/trivyignore`, '--scanners=vuln,misconfig,secret', '--cache-backend=memory', '--disable-telemetry', '--offline-scan', '--skip-db-update', '--skip-java-db-update', '--skip-check-update', '--skip-version-check', '--skip-vex-repo-update', '--timeout', `${timeout}s`, ...(cache ? ['--cache-dir', cache] : []), root];
|
||||
plan.format = 'trivy-json';
|
||||
plan.coverage.domain = 'dependencies-and-infrastructure';
|
||||
plan.trustedFiles.push(
|
||||
{ path: `${policy}/trivy.yaml`, content: '{}\n' },
|
||||
{ path: `${policy}/trivyignore`, content: '' },
|
||||
);
|
||||
plan.args = [
|
||||
'fs',
|
||||
'--format=json',
|
||||
'--config',
|
||||
`${policy}/trivy.yaml`,
|
||||
'--ignorefile',
|
||||
`${policy}/trivyignore`,
|
||||
'--scanners=vuln,misconfig,secret',
|
||||
'--cache-backend=memory',
|
||||
'--disable-telemetry',
|
||||
'--offline-scan',
|
||||
'--skip-db-update',
|
||||
'--skip-java-db-update',
|
||||
'--skip-check-update',
|
||||
'--skip-version-check',
|
||||
'--skip-vex-repo-update',
|
||||
'--timeout',
|
||||
`${timeout}s`,
|
||||
...(cache ? ['--cache-dir', cache] : []),
|
||||
root,
|
||||
];
|
||||
plan.env.TRIVY_DISABLE_TELEMETRY = 'true';
|
||||
plan.requiredFeatures = ['--cache-backend', '--disable-telemetry', '--offline-scan', '--skip-db-update', '--skip-java-db-update', '--skip-check-update', '--skip-version-check', '--skip-vex-repo-update'];
|
||||
if (!cache) plan.prerequisites.push('Provide verified offline Trivy vulnerability, Java, and misconfiguration databases as needed.');
|
||||
plan.requiredFeatures = [
|
||||
'--cache-backend',
|
||||
'--disable-telemetry',
|
||||
'--offline-scan',
|
||||
'--skip-db-update',
|
||||
'--skip-java-db-update',
|
||||
'--skip-check-update',
|
||||
'--skip-version-check',
|
||||
'--skip-vex-repo-update',
|
||||
];
|
||||
if (!cache)
|
||||
plan.prerequisites.push(
|
||||
'Provide verified offline Trivy vulnerability, Java, and misconfiguration databases as needed.',
|
||||
);
|
||||
break;
|
||||
case 'schemathesis': {
|
||||
plan.format = 'schemathesis-json'; plan.network = 'loopback'; plan.coverage.domain = 'api-runtime';
|
||||
plan.format = 'schemathesis-json';
|
||||
plan.network = 'loopback';
|
||||
plan.coverage.domain = 'api-runtime';
|
||||
plan.outputPath = '/work/schemathesis.json';
|
||||
// The upstream image enables a Python hook module and coverage plugin by
|
||||
// default. Qualified CSO scans use only the reviewed schema/config.
|
||||
plan.env.SCHEMATHESIS_HOOKS = ''; plan.env.SCHEMATHESIS_COVERAGE = 'false';
|
||||
plan.env.SCHEMATHESIS_HOOKS = '';
|
||||
plan.env.SCHEMATHESIS_COVERAGE = 'false';
|
||||
plan.trustedFiles.push({ path: `${policy}/schemathesis.toml`, content: '' });
|
||||
const schema = opts.schemaPath ? absolutePath(opts.schemaPath, 'schemaPath') : `${policy}/openapi.json`;
|
||||
if (!schema.startsWith(`${policy}/`)) throw new Error('Schemathesis schema must be below trusted policyRoot');
|
||||
if (!opts.schemaPath) plan.prerequisites.push('Provide a reviewed local schema with resolved local references, no remote references, and no hook imports.');
|
||||
const schema = opts.schemaPath
|
||||
? absolutePath(opts.schemaPath, 'schemaPath')
|
||||
: `${policy}/openapi.json`;
|
||||
if (!schema.startsWith(`${policy}/`))
|
||||
throw new Error('Schemathesis schema must be below trusted policyRoot');
|
||||
if (!opts.schemaPath)
|
||||
plan.prerequisites.push(
|
||||
'Provide a reviewed local schema with resolved local references, no remote references, and no hook imports.',
|
||||
);
|
||||
const base = opts.baseUrl ? validateScannerBaseUrl(opts.baseUrl) : 'http://127.0.0.1:3000/';
|
||||
if (!opts.baseUrl) plan.prerequisites.push('Start the application and a legitimate control in the admitted loopback namespace.');
|
||||
if (!opts.baseUrl)
|
||||
plan.prerequisites.push(
|
||||
'Start the application and a legitimate control in the admitted loopback namespace.',
|
||||
);
|
||||
const seed = positiveInteger(opts.seed ?? 1, 2_147_483_647, 'seed');
|
||||
const examples = positiveInteger(opts.maxExamples ?? 20, 100, 'maxExamples');
|
||||
const operations = opts.operationIds ?? [];
|
||||
if (operations.length === 0 || operations.length > 20) plan.prerequisites.push('Declare between 1 and 20 reviewed operation IDs to bound the API assessment.');
|
||||
if (operations.some(op => !op || op.length > 200 || /[\x00-\x1f]/.test(op))) throw new Error('Invalid Schemathesis operation ID');
|
||||
plan.args = ['--config-file', `${policy}/schemathesis.toml`, '--no-color', 'run', schema, '--url', base, '--workers=1', '--phases=fuzzing', '--max-examples', String(examples), '--max-failures=10', '--max-time', String(timeout), '--seed', String(seed), '--request-timeout=5', '--request-retries=0', '--max-redirects=0', '--rate-limit=10/s', '--output-sanitize=true', '--generation-database=none', '--report-json-path', plan.outputPath, ...operations.flatMap(op => ['--include-operation-id', op])];
|
||||
plan.requiredFeatures = ['--report-json-path', '--max-time', '--seed', '--max-redirects', '--include-operation-id'];
|
||||
plan.coverage.scope = operations.map(op => `operation:${op}`);
|
||||
plan.coverage.exclusions.push('Only declared operations and generated examples are exercised; API failures are candidates, not security proofs.');
|
||||
if (operations.length === 0 || operations.length > 20)
|
||||
plan.prerequisites.push(
|
||||
'Declare between 1 and 20 reviewed operation IDs to bound the API assessment.',
|
||||
);
|
||||
if (operations.some((op) => !op || op.length > 200 || /[\x00-\x1f]/.test(op)))
|
||||
throw new Error('Invalid Schemathesis operation ID');
|
||||
plan.args = [
|
||||
'--config-file',
|
||||
`${policy}/schemathesis.toml`,
|
||||
'--no-color',
|
||||
'run',
|
||||
schema,
|
||||
'--url',
|
||||
base,
|
||||
'--workers=1',
|
||||
'--phases=fuzzing',
|
||||
'--max-examples',
|
||||
String(examples),
|
||||
'--max-failures=10',
|
||||
'--max-time',
|
||||
String(timeout),
|
||||
'--seed',
|
||||
String(seed),
|
||||
'--request-timeout=5',
|
||||
'--request-retries=0',
|
||||
'--max-redirects=0',
|
||||
'--rate-limit=10/s',
|
||||
'--output-sanitize=true',
|
||||
'--generation-database=none',
|
||||
'--report-json-path',
|
||||
plan.outputPath,
|
||||
...operations.flatMap((op) => ['--include-operation-id', op]),
|
||||
];
|
||||
plan.requiredFeatures = [
|
||||
'--report-json-path',
|
||||
'--max-time',
|
||||
'--seed',
|
||||
'--max-redirects',
|
||||
'--include-operation-id',
|
||||
];
|
||||
plan.coverage.scope = operations.map((op) => `operation:${op}`);
|
||||
plan.coverage.exclusions.push(
|
||||
'Only declared operations and generated examples are exercised; API failures are candidates, not security proofs.',
|
||||
);
|
||||
break;
|
||||
}
|
||||
}
|
||||
const capabilities = opts.tools?.[id]?.capabilities;
|
||||
if (capabilities) for (const required of plan.requiredFeatures) {
|
||||
if (!capabilities.includes(required)) plan.prerequisites.push(`${plan.executableName} lacks required capability ${required}.`);
|
||||
}
|
||||
if (capabilities)
|
||||
for (const required of plan.requiredFeatures) {
|
||||
if (!capabilities.includes(required))
|
||||
plan.prerequisites.push(`${plan.executableName} lacks required capability ${required}.`);
|
||||
}
|
||||
const version = opts.tools?.[id]?.version;
|
||||
if (id === 'osv' && version && !/\b(?:v)?2\./.test(version)) plan.prerequisites.push('OSV-Scanner major version 2 is required.');
|
||||
if (id === 'osv' && version && !/\b(?:v)?2\./.test(version))
|
||||
plan.prerequisites.push('OSV-Scanner major version 2 is required.');
|
||||
return plan;
|
||||
});
|
||||
}
|
||||
@@ -258,7 +499,9 @@ function str(value: unknown): string {
|
||||
if (typeof value !== 'string' || value.length > 16_384) throw new Error('Expected bounded string');
|
||||
return value;
|
||||
}
|
||||
function optionalString(value: unknown): string | undefined { return value === undefined || value === null ? undefined : str(value); }
|
||||
function optionalString(value: unknown): string | undefined {
|
||||
return value === undefined || value === null ? undefined : str(value);
|
||||
}
|
||||
function integer(value: unknown): number | undefined {
|
||||
if (value === undefined) return undefined;
|
||||
if (!Number.isSafeInteger(value) || (value as number) < 1) throw new Error('Invalid source coordinate');
|
||||
@@ -266,20 +509,32 @@ function integer(value: unknown): number | undefined {
|
||||
}
|
||||
function severity(value: unknown): ScannerCandidate['reportedSeverity'] {
|
||||
const normalized = typeof value === 'string' ? value.toLowerCase() : '';
|
||||
if (['critical', 'high', 'medium', 'low', 'info'].includes(normalized)) return normalized as ScannerCandidate['reportedSeverity'];
|
||||
return ({ error: 'high', warning: 'medium', note: 'info', informational: 'info', unknown: 'unknown' } as const)[normalized] ?? 'unknown';
|
||||
if (['critical', 'high', 'medium', 'low', 'info'].includes(normalized))
|
||||
return normalized as ScannerCandidate['reportedSeverity'];
|
||||
return (
|
||||
({ error: 'high', warning: 'medium', note: 'info', informational: 'info', unknown: 'unknown' } as const)[
|
||||
normalized
|
||||
] ?? 'unknown'
|
||||
);
|
||||
}
|
||||
|
||||
/** No path is opened by this module. Normalization refuses URI/traversal escapes. */
|
||||
export function scannerLocation(raw: string, sourceRoot: string): string {
|
||||
let decoded: string;
|
||||
try { decoded = decodeURIComponent(raw); } catch { throw new Error('Unsafe location'); }
|
||||
if (/[\x00-\x1f\x7f]/.test(decoded) || /%[\da-f]{2}/i.test(decoded) || decoded.includes('\\')) throw new Error('Unsafe location');
|
||||
try {
|
||||
decoded = decodeURIComponent(raw);
|
||||
} catch {
|
||||
throw new Error('Unsafe location');
|
||||
}
|
||||
if (/[\x00-\x1f\x7f]/.test(decoded) || /%[\da-f]{2}/i.test(decoded) || decoded.includes('\\'))
|
||||
throw new Error('Unsafe location');
|
||||
if (decoded.startsWith('file:')) {
|
||||
const url = new URL(decoded);
|
||||
if (url.hostname || url.username || url.password || url.search || url.hash) throw new Error('Unsafe file URI');
|
||||
if (url.hostname || url.username || url.password || url.search || url.hash)
|
||||
throw new Error('Unsafe file URI');
|
||||
decoded = decodeURIComponent(url.pathname);
|
||||
} else if (/^[a-z][a-z\d+.-]*:/i.test(decoded) || decoded.startsWith('//')) throw new Error('Unsafe location');
|
||||
} else if (/^[a-z][a-z\d+.-]*:/i.test(decoded) || decoded.startsWith('//'))
|
||||
throw new Error('Unsafe location');
|
||||
if (decoded.split('/').includes('..')) throw new Error('Unsafe location');
|
||||
const root = absolutePath(sourceRoot, 'sourceRoot');
|
||||
const absolute = decoded.startsWith('/') ? posix.normalize(decoded) : posix.join(root, decoded);
|
||||
@@ -314,32 +569,66 @@ function decodedDocument(raw: string): unknown {
|
||||
return document;
|
||||
}
|
||||
|
||||
function candidate(tool: ScannerCandidate['tool'], fields: Omit<ScannerCandidate, 'id' | 'tool' | 'evidence' | 'trust' | 'suppressed'> & { suppressed?: boolean }): ScannerCandidate {
|
||||
const identity = [tool, fields.ruleId, fields.location?.path ?? fields.operation ?? '', fields.location?.line ?? '', ...fields.advisoryIds.slice().sort()];
|
||||
function candidate(
|
||||
tool: ScannerCandidate['tool'],
|
||||
fields: Omit<ScannerCandidate, 'id' | 'tool' | 'evidence' | 'trust' | 'suppressed'> & {
|
||||
suppressed?: boolean;
|
||||
},
|
||||
): ScannerCandidate {
|
||||
const identity = [
|
||||
tool,
|
||||
fields.ruleId,
|
||||
fields.location?.path ?? fields.operation ?? '',
|
||||
fields.location?.line ?? '',
|
||||
...fields.advisoryIds.slice().sort(),
|
||||
];
|
||||
const id = createHash('sha256').update(JSON.stringify(identity)).digest('hex');
|
||||
return { ...fields, id, tool, suppressed: fields.suppressed ?? false, evidence: 'scanner-candidate', trust: 'untrusted' };
|
||||
return {
|
||||
...fields,
|
||||
id,
|
||||
tool,
|
||||
suppressed: fields.suppressed ?? false,
|
||||
evidence: 'scanner-candidate',
|
||||
trust: 'untrusted',
|
||||
};
|
||||
}
|
||||
|
||||
function location(path: unknown, line: unknown, column: unknown, root: string): ScannerCandidate['location'] {
|
||||
return { path: scannerLocation(str(path), root), line: integer(line), column: integer(column) };
|
||||
}
|
||||
|
||||
function parseSarif(document: unknown, tool: ScannerCandidate['tool'], root: string, add: (value: ScannerCandidate) => void, gap: (code: ScannerGap['code'], message: string) => void): void {
|
||||
function parseSarif(
|
||||
document: unknown,
|
||||
tool: ScannerCandidate['tool'],
|
||||
root: string,
|
||||
add: (value: ScannerCandidate) => void,
|
||||
gap: (code: ScannerGap['code'], message: string) => void,
|
||||
): void {
|
||||
const sarif = obj(document);
|
||||
if (sarif.version !== '2.1.0') throw new Error('SARIF 2.1.0 required');
|
||||
const runs = arr(sarif.runs);
|
||||
if (!runs.length) { gap('SKIPPED_INPUT', 'SARIF contains no assessment runs.'); return; }
|
||||
if (!runs.length) {
|
||||
gap('SKIPPED_INPUT', 'SARIF contains no assessment runs.');
|
||||
return;
|
||||
}
|
||||
for (const input of runs) {
|
||||
const run = obj(input); const driver = obj(obj(run.tool).driver);
|
||||
const run = obj(input);
|
||||
const driver = obj(obj(run.tool).driver);
|
||||
str(driver.name);
|
||||
if (run.externalPropertyFileReferences !== undefined) {
|
||||
const refs = obj(run.externalPropertyFileReferences);
|
||||
if (refs.results !== undefined && arr(refs.results).length) gap('SKIPPED_INPUT', 'External SARIF result files were not fetched or assessed.');
|
||||
if (refs.results !== undefined && arr(refs.results).length)
|
||||
gap('SKIPPED_INPUT', 'External SARIF result files were not fetched or assessed.');
|
||||
}
|
||||
for (const invocation of run.invocations === undefined ? [] : arr(run.invocations)) {
|
||||
const inv = obj(invocation);
|
||||
if (inv.executionSuccessful === false) gap('TOOL_FAILED', 'SARIF records an unsuccessful tool invocation.');
|
||||
if (Array.isArray(inv.toolExecutionNotifications) && inv.toolExecutionNotifications.some(n => obj(n).level === 'error')) gap('TOOL_FAILED', 'SARIF records tool execution errors.');
|
||||
if (inv.executionSuccessful === false)
|
||||
gap('TOOL_FAILED', 'SARIF records an unsuccessful tool invocation.');
|
||||
if (
|
||||
Array.isArray(inv.toolExecutionNotifications) &&
|
||||
inv.toolExecutionNotifications.some((n) => obj(n).level === 'error')
|
||||
)
|
||||
gap('TOOL_FAILED', 'SARIF records tool execution errors.');
|
||||
}
|
||||
const rules = driver.rules === undefined ? [] : arr(driver.rules);
|
||||
const results = arr(run.results);
|
||||
@@ -349,7 +638,10 @@ function parseSarif(document: unknown, tool: ScannerCandidate['tool'], root: str
|
||||
// SARIF also represents passing checks and informational inventory.
|
||||
if (['pass', 'notApplicable', 'informational'].includes(String(result.kind))) continue;
|
||||
const ruleIndex = result.ruleIndex;
|
||||
const rule = Number.isSafeInteger(ruleIndex) && (ruleIndex as number) >= 0 && rules[ruleIndex as number] ? obj(rules[ruleIndex as number]) : undefined;
|
||||
const rule =
|
||||
Number.isSafeInteger(ruleIndex) && (ruleIndex as number) >= 0 && rules[ruleIndex as number]
|
||||
? obj(rules[ruleIndex as number])
|
||||
: undefined;
|
||||
const ruleId = str(result.ruleId ?? rule?.id);
|
||||
const message = obj(result.message);
|
||||
let loc: ScannerCandidate['location'];
|
||||
@@ -358,7 +650,8 @@ function parseSarif(document: unknown, tool: ScannerCandidate['tool'], root: str
|
||||
let artifact = obj(physical.artifactLocation);
|
||||
if (artifact.uri === undefined && Number.isSafeInteger(artifact.index)) {
|
||||
const index = artifact.index as number;
|
||||
if (index < 0 || !Array.isArray(run.artifacts) || !run.artifacts[index]) throw new Error('Invalid artifact index');
|
||||
if (index < 0 || !Array.isArray(run.artifacts) || !run.artifacts[index])
|
||||
throw new Error('Invalid artifact index');
|
||||
artifact = obj(obj(run.artifacts[index]).location);
|
||||
}
|
||||
let uri = str(artifact.uri);
|
||||
@@ -373,21 +666,58 @@ function parseSarif(document: unknown, tool: ScannerCandidate['tool'], root: str
|
||||
loc = location(uri, region.startLine, region.startColumn, root);
|
||||
}
|
||||
const properties = result.properties === undefined ? {} : obj(result.properties);
|
||||
const aliases = properties.tags === undefined ? [] : arr(properties.tags).filter(v => typeof v === 'string' && /^(CVE-|GHSA-|OSV-)/.test(v));
|
||||
add(candidate(tool, { ruleId, message: str(message.text ?? message.markdown ?? message.id), location: loc, reportedSeverity: severity(result.level ?? (rule?.defaultConfiguration as Obj | undefined)?.level), advisoryIds: aliases as string[], suppressed: Array.isArray(result.suppressions) && result.suppressions.length > 0 }));
|
||||
} catch (error) { gap(error instanceof Error && /[Ll]ocation|URI|source root/.test(error.message) ? 'UNSAFE_LOCATION' : 'INVALID_OUTPUT', 'A SARIF result could not be safely normalized.'); }
|
||||
const aliases =
|
||||
properties.tags === undefined
|
||||
? []
|
||||
: arr(properties.tags).filter((v) => typeof v === 'string' && /^(CVE-|GHSA-|OSV-)/.test(v));
|
||||
add(
|
||||
candidate(tool, {
|
||||
ruleId,
|
||||
message: str(message.text ?? message.markdown ?? message.id),
|
||||
location: loc,
|
||||
reportedSeverity: severity(
|
||||
result.level ?? (rule?.defaultConfiguration as Obj | undefined)?.level,
|
||||
),
|
||||
advisoryIds: aliases as string[],
|
||||
suppressed: Array.isArray(result.suppressions) && result.suppressions.length > 0,
|
||||
}),
|
||||
);
|
||||
} catch (error) {
|
||||
gap(
|
||||
error instanceof Error && /[Ll]ocation|URI|source root/.test(error.message)
|
||||
? 'UNSAFE_LOCATION'
|
||||
: 'INVALID_OUTPUT',
|
||||
'A SARIF result could not be safely normalized.',
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function parseResults(plan: ScannerPlan, document: unknown, add: (value: ScannerCandidate) => void, gap: (code: ScannerGap['code'], message: string) => void): void {
|
||||
function parseResults(
|
||||
plan: ScannerPlan,
|
||||
document: unknown,
|
||||
add: (value: ScannerCandidate) => void,
|
||||
gap: (code: ScannerGap['code'], message: string) => void,
|
||||
): void {
|
||||
const root = plan.sourceRoot;
|
||||
if (plan.format === 'sarif') { parseSarif(document, plan.id, root, add, gap); return; }
|
||||
if (plan.format === 'sarif') {
|
||||
parseSarif(document, plan.id, root, add, gap);
|
||||
return;
|
||||
}
|
||||
if (plan.format === 'gitleaks-json') {
|
||||
for (const value of arr(document)) {
|
||||
const row = obj(value);
|
||||
// Never retain Match, Secret, Line, commit message, author, or scanner fingerprint.
|
||||
add(candidate(plan.id, { ruleId: str(row.RuleID), message: str(row.Description), reportedSeverity: 'unknown', location: location(row.File, row.StartLine, row.StartColumn, root), advisoryIds: [] }));
|
||||
add(
|
||||
candidate(plan.id, {
|
||||
ruleId: str(row.RuleID),
|
||||
message: str(row.Description),
|
||||
reportedSeverity: 'unknown',
|
||||
location: location(row.File, row.StartLine, row.StartColumn, root),
|
||||
advisoryIds: [],
|
||||
}),
|
||||
);
|
||||
}
|
||||
return;
|
||||
}
|
||||
@@ -395,38 +725,93 @@ function parseResults(plan: ScannerPlan, document: unknown, add: (value: Scanner
|
||||
switch (plan.format) {
|
||||
case 'semgrep-json':
|
||||
for (const value of arr(doc.results)) {
|
||||
const row = obj(value), extra = obj(row.extra), start = obj(row.start);
|
||||
add(candidate(plan.id, { ruleId: str(row.check_id), message: str(extra.message), reportedSeverity: severity(extra.severity), location: location(row.path, start.line, start.col, root), advisoryIds: [], suppressed: extra.is_ignored === true }));
|
||||
const row = obj(value),
|
||||
extra = obj(row.extra),
|
||||
start = obj(row.start);
|
||||
add(
|
||||
candidate(plan.id, {
|
||||
ruleId: str(row.check_id),
|
||||
message: str(extra.message),
|
||||
reportedSeverity: severity(extra.severity),
|
||||
location: location(row.path, start.line, start.col, root),
|
||||
advisoryIds: [],
|
||||
suppressed: extra.is_ignored === true,
|
||||
}),
|
||||
);
|
||||
}
|
||||
if (arr(doc.errors).length) gap('TOOL_FAILED', 'Semgrep reported parser, rule, or execution errors; inspect affected coverage.');
|
||||
if (arr(doc.errors).length)
|
||||
gap('TOOL_FAILED', 'Semgrep reported parser, rule, or execution errors; inspect affected coverage.');
|
||||
if (!arr(obj(doc.paths).scanned).length) gap('SKIPPED_INPUT', 'Semgrep did not scan any source files.');
|
||||
if (Array.isArray(obj(doc.paths).skipped) && (obj(doc.paths).skipped as unknown[]).length) gap('SKIPPED_INPUT', 'Semgrep skipped source files.');
|
||||
if (Array.isArray(obj(doc.paths).skipped) && (obj(doc.paths).skipped as unknown[]).length)
|
||||
gap('SKIPPED_INPUT', 'Semgrep skipped source files.');
|
||||
return;
|
||||
case 'osv-json':
|
||||
for (const value of arr(doc.results)) {
|
||||
const result = obj(value), source = obj(result.source);
|
||||
const result = obj(value),
|
||||
source = obj(result.source);
|
||||
for (const entry of arr(result.packages)) {
|
||||
const pkg = obj(entry), detail = obj(pkg.package);
|
||||
const pkg = obj(entry),
|
||||
detail = obj(pkg.package);
|
||||
for (const input of arr(pkg.vulnerabilities)) {
|
||||
const vuln = obj(input), id = str(vuln.id);
|
||||
const vuln = obj(input),
|
||||
id = str(vuln.id);
|
||||
const aliases = vuln.aliases === undefined ? [] : arr(vuln.aliases).map(str);
|
||||
add(candidate(plan.id, { ruleId: id, message: optionalString(vuln.summary) ?? id, reportedSeverity: 'unknown', location: location(source.path, undefined, undefined, root), advisoryIds: [...new Set([id, ...aliases])], dependency: { name: str(detail.name), version: optionalString(detail.version), ecosystem: optionalString(detail.ecosystem), reachability: 'unknown', exposure: 'unknown' } }));
|
||||
add(
|
||||
candidate(plan.id, {
|
||||
ruleId: id,
|
||||
message: optionalString(vuln.summary) ?? id,
|
||||
reportedSeverity: 'unknown',
|
||||
location: location(source.path, undefined, undefined, root),
|
||||
advisoryIds: [...new Set([id, ...aliases])],
|
||||
dependency: {
|
||||
name: str(detail.name),
|
||||
version: optionalString(detail.version),
|
||||
ecosystem: optionalString(detail.ecosystem),
|
||||
reachability: 'unknown',
|
||||
exposure: 'unknown',
|
||||
},
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
return;
|
||||
case 'trivy-json':
|
||||
if (doc.SchemaVersion !== 2) throw new Error('Trivy schema version 2 required');
|
||||
if (doc.Results === undefined && (typeof doc.ArtifactName !== 'string' || doc.ArtifactType !== 'filesystem')) throw new Error('Missing Trivy assessment metadata');
|
||||
if (
|
||||
doc.Results === undefined &&
|
||||
(typeof doc.ArtifactName !== 'string' || doc.ArtifactType !== 'filesystem')
|
||||
)
|
||||
throw new Error('Missing Trivy assessment metadata');
|
||||
for (const value of arr(doc.Results ?? [])) {
|
||||
const result = obj(value);
|
||||
for (const key of ['Vulnerabilities', 'Misconfigurations', 'Secrets'] as const) {
|
||||
for (const input of result[key] === undefined ? [] : arr(result[key])) {
|
||||
const row = obj(input), id = str(row.VulnerabilityID ?? row.ID ?? row.RuleID);
|
||||
const row = obj(input),
|
||||
id = str(row.VulnerabilityID ?? row.ID ?? row.RuleID);
|
||||
const cause = row.CauseMetadata === undefined ? {} : obj(row.CauseMetadata);
|
||||
// Some filesystem package scanners add " (type)" after their target.
|
||||
const target = str(result.Target).replace(/ \([a-zA-Z0-9_. -]+\)$/, '');
|
||||
add(candidate(plan.id, { ruleId: id, message: optionalString(row.Title) ?? optionalString(row.Description) ?? id, reportedSeverity: severity(row.Severity), location: location(target, cause.StartLine ?? row.StartLine, undefined, root), advisoryIds: row.VulnerabilityID ? [id] : [], ...(key === 'Vulnerabilities' ? { dependency: { name: str(row.PkgName), version: optionalString(row.InstalledVersion), ecosystem: optionalString(result.Type), reachability: 'unknown' as const, exposure: 'unknown' as const } } : {}) }));
|
||||
add(
|
||||
candidate(plan.id, {
|
||||
ruleId: id,
|
||||
message: optionalString(row.Title) ?? optionalString(row.Description) ?? id,
|
||||
reportedSeverity: severity(row.Severity),
|
||||
location: location(target, cause.StartLine ?? row.StartLine, undefined, root),
|
||||
advisoryIds: row.VulnerabilityID ? [id] : [],
|
||||
...(key === 'Vulnerabilities'
|
||||
? {
|
||||
dependency: {
|
||||
name: str(row.PkgName),
|
||||
version: optionalString(row.InstalledVersion),
|
||||
ecosystem: optionalString(result.Type),
|
||||
reachability: 'unknown' as const,
|
||||
exposure: 'unknown' as const,
|
||||
},
|
||||
}
|
||||
: {}),
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -434,13 +819,31 @@ function parseResults(plan: ScannerPlan, document: unknown, add: (value: Scanner
|
||||
case 'schemathesis-json': {
|
||||
str(doc.schemathesis_version);
|
||||
const operations = doc.operations === null ? null : obj(doc.operations);
|
||||
if (doc.complete !== true || doc.stop_reason !== 'completed') gap('SKIPPED_INPUT', 'Schemathesis did not finish its declared operation assessment.');
|
||||
if (!operations || typeof operations.tested !== 'number' || operations.tested === 0) gap('SKIPPED_INPUT', 'Schemathesis exercised no operations.');
|
||||
if (operations && (Number(operations.errored) > 0 || Number(operations.skipped) > 0 || Number(operations.tested) < Number(operations.selected))) gap('SKIPPED_INPUT', 'Schemathesis skipped or failed to exercise selected operations.');
|
||||
if (arr(doc.errors).length) gap('TOOL_FAILED', 'Schemathesis reported setup or test-generation errors.');
|
||||
if (doc.complete !== true || doc.stop_reason !== 'completed')
|
||||
gap('SKIPPED_INPUT', 'Schemathesis did not finish its declared operation assessment.');
|
||||
if (!operations || typeof operations.tested !== 'number' || operations.tested === 0)
|
||||
gap('SKIPPED_INPUT', 'Schemathesis exercised no operations.');
|
||||
if (
|
||||
operations &&
|
||||
(Number(operations.errored) > 0 ||
|
||||
Number(operations.skipped) > 0 ||
|
||||
Number(operations.tested) < Number(operations.selected))
|
||||
)
|
||||
gap('SKIPPED_INPUT', 'Schemathesis skipped or failed to exercise selected operations.');
|
||||
if (arr(doc.errors).length)
|
||||
gap('TOOL_FAILED', 'Schemathesis reported setup or test-generation errors.');
|
||||
for (const value of arr(doc.failures)) {
|
||||
const row = obj(value);
|
||||
for (const op of arr(row.operations)) add(candidate(plan.id, { ruleId: str(row.type), message: str(row.title), reportedSeverity: severity(row.severity), advisoryIds: [], operation: str(op) }));
|
||||
for (const op of arr(row.operations))
|
||||
add(
|
||||
candidate(plan.id, {
|
||||
ruleId: str(row.type),
|
||||
message: str(row.title),
|
||||
reportedSeverity: severity(row.severity),
|
||||
advisoryIds: [],
|
||||
operation: str(op),
|
||||
}),
|
||||
);
|
||||
}
|
||||
return;
|
||||
}
|
||||
@@ -450,16 +853,38 @@ function parseResults(plan: ScannerPlan, document: unknown, add: (value: Scanner
|
||||
/** Failed or malformed tools never become an empty-clean assessment. */
|
||||
export function parseScannerOutput(plan: ScannerPlan, execution: ScannerExecution): ScannerOutcome {
|
||||
const outcome: ScannerOutcome = {
|
||||
tool: plan.id, version: null, status: 'not_assessed', candidates: [], gaps: [], scope: plan.coverage.scope.slice(), exclusions: plan.coverage.exclusions.slice(),
|
||||
databaseUpdatedAt: null, exitCode: execution.exitCode, evidence: 'scanner-candidate', provenanceSources: plan.provenanceSources.slice(),
|
||||
planSha256: createHash('sha256').update(JSON.stringify(plan)).digest('hex'), documentationInspectedAt: plan.documentationInspectedAt,
|
||||
tool: plan.id,
|
||||
version: null,
|
||||
status: 'not_assessed',
|
||||
candidates: [],
|
||||
gaps: [],
|
||||
scope: plan.coverage.scope.slice(),
|
||||
exclusions: plan.coverage.exclusions.slice(),
|
||||
databaseUpdatedAt: null,
|
||||
exitCode: execution.exitCode,
|
||||
evidence: 'scanner-candidate',
|
||||
provenanceSources: plan.provenanceSources.slice(),
|
||||
planSha256: createHash('sha256').update(JSON.stringify(plan)).digest('hex'),
|
||||
documentationInspectedAt: plan.documentationInspectedAt,
|
||||
};
|
||||
const gap = (code: ScannerGap['code'], message: string) => { if (!outcome.gaps.some(g => g.code === code && g.message === message)) outcome.gaps.push({ code, message }); };
|
||||
if (execution.unavailable) { gap('UNAVAILABLE', `${plan.id} was unavailable; this scanner assessment did not run.`); return outcome; }
|
||||
if (plan.prerequisites.length) { for (const value of plan.prerequisites) gap('PREREQUISITE', value); return outcome; }
|
||||
const gap = (code: ScannerGap['code'], message: string) => {
|
||||
if (!outcome.gaps.some((g) => g.code === code && g.message === message))
|
||||
outcome.gaps.push({ code, message });
|
||||
};
|
||||
if (execution.unavailable) {
|
||||
gap('UNAVAILABLE', `${plan.id} was unavailable; this scanner assessment did not run.`);
|
||||
return outcome;
|
||||
}
|
||||
if (plan.prerequisites.length) {
|
||||
for (const value of plan.prerequisites) gap('PREREQUISITE', value);
|
||||
return outcome;
|
||||
}
|
||||
if (execution.timedOut) gap('TIMEOUT', 'Scanner exceeded its execution deadline.');
|
||||
const outputBytes = Buffer.byteLength(execution.stdout) + Buffer.byteLength(execution.stderr ?? '');
|
||||
if (execution.truncated || outputBytes > Math.min(plan.maxOutputBytes, MAX_SCANNER_OUTPUT_BYTES)) { gap('OUTPUT_LIMIT', 'Scanner output exceeded the capture limit; payload withheld.'); return outcome; }
|
||||
if (execution.truncated || outputBytes > Math.min(plan.maxOutputBytes, MAX_SCANNER_OUTPUT_BYTES)) {
|
||||
gap('OUTPUT_LIMIT', 'Scanner output exceeded the capture limit; payload withheld.');
|
||||
return outcome;
|
||||
}
|
||||
try {
|
||||
if (execution.version) {
|
||||
const safe = redactFindingSpans(execution.version);
|
||||
@@ -467,31 +892,61 @@ export function parseScannerOutput(plan: ScannerPlan, execution: ScannerExecutio
|
||||
outcome.version = safe.slice(0, 200).replace(/[\x00-\x1f\x7f]/g, '');
|
||||
}
|
||||
if (redactFindingSpans(execution.stderr ?? '') === null) throw new RedactionFailure();
|
||||
if (/\b(?:error|fatal|panic|failed to|unable to|no offline version)\b/i.test(execution.stderr ?? '')) gap('TOOL_FAILED', 'Scanner diagnostic output reported a failure; the JSON result does not establish complete coverage.');
|
||||
if (/\b(?:error|fatal|panic|failed to|unable to|no offline version)\b/i.test(execution.stderr ?? ''))
|
||||
gap(
|
||||
'TOOL_FAILED',
|
||||
'Scanner diagnostic output reported a failure; the JSON result does not establish complete coverage.',
|
||||
);
|
||||
const doc = decodedDocument(execution.stdout);
|
||||
const seen = new Set<string>();
|
||||
parseResults(plan, doc, item => {
|
||||
if (outcome.candidates.length >= MAX_CANDIDATES) throw new Error('Candidate limit exceeded');
|
||||
if (!seen.has(item.id)) { seen.add(item.id); outcome.candidates.push(item); }
|
||||
}, gap);
|
||||
parseResults(
|
||||
plan,
|
||||
doc,
|
||||
(item) => {
|
||||
if (outcome.candidates.length >= MAX_CANDIDATES) throw new Error('Candidate limit exceeded');
|
||||
if (!seen.has(item.id)) {
|
||||
seen.add(item.id);
|
||||
outcome.candidates.push(item);
|
||||
}
|
||||
},
|
||||
gap,
|
||||
);
|
||||
outcome.status = 'complete';
|
||||
} catch (error) {
|
||||
if (error instanceof RedactionFailure) { outcome.candidates = []; gap('REDACTION_FAILED', 'Scanner payload could not be safely redacted and was withheld.'); }
|
||||
else gap('INVALID_OUTPUT', 'Scanner report is malformed, unsupported, or exceeds structural limits.');
|
||||
if (error instanceof RedactionFailure) {
|
||||
outcome.candidates = [];
|
||||
gap('REDACTION_FAILED', 'Scanner payload could not be safely redacted and was withheld.');
|
||||
} else gap('INVALID_OUTPUT', 'Scanner report is malformed, unsupported, or exceeds structural limits.');
|
||||
}
|
||||
const successCodes = plan.id === 'gitleaks' ? [0, 10] : ['osv', 'schemathesis'].includes(plan.id) ? [0, 1] : [0];
|
||||
if (execution.exitCode === null || !successCodes.includes(execution.exitCode)) gap('TOOL_FAILED', 'Scanner did not exit with a recognized assessment status.');
|
||||
if ((plan.id === 'gitleaks' && execution.exitCode === 10 || plan.id === 'osv' && execution.exitCode === 1) && !outcome.candidates.length) gap('INVALID_OUTPUT', 'Scanner finding exit status disagrees with its empty report.');
|
||||
const successCodes =
|
||||
plan.id === 'gitleaks' ? [0, 10] : ['osv', 'schemathesis'].includes(plan.id) ? [0, 1] : [0];
|
||||
if (execution.exitCode === null || !successCodes.includes(execution.exitCode))
|
||||
gap('TOOL_FAILED', 'Scanner did not exit with a recognized assessment status.');
|
||||
if (
|
||||
((plan.id === 'gitleaks' && execution.exitCode === 10) ||
|
||||
(plan.id === 'osv' && execution.exitCode === 1)) &&
|
||||
!outcome.candidates.length
|
||||
)
|
||||
gap('INVALID_OUTPUT', 'Scanner finding exit status disagrees with its empty report.');
|
||||
if (['osv', 'trivy'].includes(plan.id)) {
|
||||
if (execution.databaseUpdatedAt && /^\d{4}-\d\d-\d\dT/.test(execution.databaseUpdatedAt) && Number.isFinite(Date.parse(execution.databaseUpdatedAt))) outcome.databaseUpdatedAt = execution.databaseUpdatedAt;
|
||||
if (
|
||||
execution.databaseUpdatedAt &&
|
||||
/^\d{4}-\d\d-\d\dT/.test(execution.databaseUpdatedAt) &&
|
||||
Number.isFinite(Date.parse(execution.databaseUpdatedAt))
|
||||
)
|
||||
outcome.databaseUpdatedAt = execution.databaseUpdatedAt;
|
||||
else gap('UNKNOWN_FRESHNESS', 'The advisory database freshness is unknown.');
|
||||
}
|
||||
if (outcome.gaps.length) outcome.status = outcome.status === 'complete' || outcome.candidates.length ? 'partial' : 'not_assessed';
|
||||
if (outcome.gaps.length)
|
||||
outcome.status = outcome.status === 'complete' || outcome.candidates.length ? 'partial' : 'not_assessed';
|
||||
return outcome;
|
||||
}
|
||||
|
||||
/** Import CodeQL or other SARIF as read-only candidates; never trust its verdict. */
|
||||
export function importSarif(raw: string, opts: { sourceRoot: string; version?: string; scope?: string[] }): ScannerOutcome {
|
||||
export function importSarif(
|
||||
raw: string,
|
||||
opts: { sourceRoot: string; version?: string; scope?: string[] },
|
||||
): ScannerOutcome {
|
||||
const root = absolutePath(opts.sourceRoot, 'sourceRoot');
|
||||
const plan = scannerPlans({ snapshotRoot: root, offline: true, selected: ['zizmor'] })[0];
|
||||
plan.coverage.scope = opts.scope ?? [root];
|
||||
@@ -499,6 +954,6 @@ export function importSarif(raw: string, opts: { sourceRoot: string; version?: s
|
||||
plan.coverage.exclusions = ['Imported scanner scope and suppressions require independent validation.'];
|
||||
const outcome = parseScannerOutput(plan, { stdout: raw, exitCode: 0, version: opts.version });
|
||||
outcome.tool = 'sarif';
|
||||
outcome.candidates = outcome.candidates.map(item => candidate('sarif', item));
|
||||
outcome.candidates = outcome.candidates.map((item) => candidate('sarif', item));
|
||||
return outcome;
|
||||
}
|
||||
+902
-192
File diff suppressed because it is too large.
Load diff
+2332
-653
File diff suppressed because it is too large.
Load diff
+2118
-302
File diff suppressed because it is too large.
Load diff
+153
-24
@@ -3,30 +3,159 @@ import * as fs from 'node:fs';
|
||||
import { connect } from 'node:net';
|
||||
import { HttpAssertion, VerificationObservation, object } from './contracts';
|
||||
|
||||
interface Config { phase:'before'|'after'; port:number; legitimate:HttpAssertion[]; security:HttpAssertion }
|
||||
function matches(status:number,body:string,oracle:HttpAssertion['expected']):boolean{return status===oracle.status&&(oracle.includes===undefined||body.includes(oracle.includes))&&(oracle.excludes===undefined||!body.includes(oracle.excludes));}
|
||||
export async function boundedResponseBody(response:Response,limit=65536):Promise<string>{
|
||||
if(!Number.isSafeInteger(limit)||limit<1)throw new Error('invalid response limit');
|
||||
const declared=response.headers.get('content-length');
|
||||
if(declared!==null&&(/^\d+$/.test(declared)?Number(declared)>limit:true)){await response.body?.cancel();throw new Error('response too large');}
|
||||
if(!response.body)return'';
|
||||
const reader=response.body.getReader(),chunks:Uint8Array[]=[];let total=0;
|
||||
try{
|
||||
for(;;){const next=await reader.read();if(next.done)break;if(!next.value)continue;total+=next.value.byteLength;if(total>limit){await reader.cancel();throw new Error('response too large');}chunks.push(next.value);}
|
||||
}finally{reader.releaseLock();}
|
||||
const bytes=new Uint8Array(total);let offset=0;for(const chunk of chunks){bytes.set(chunk,offset);offset+=chunk.byteLength;}return new TextDecoder().decode(bytes);
|
||||
interface Config {
|
||||
phase: 'before' | 'after';
|
||||
port: number;
|
||||
legitimate: HttpAssertion[];
|
||||
security: HttpAssertion;
|
||||
}
|
||||
async function request(a:HttpAssertion,port:number):Promise<{status:number;body:string}>{
|
||||
const controller=new AbortController(),timer=setTimeout(()=>controller.abort(),5000);
|
||||
try{const response=await fetch(`http://127.0.0.1:${port}${a.path}`,{method:a.method,headers:a.headers,body:['GET'].includes(a.method)?undefined:a.body,redirect:'manual',signal:controller.signal});return{status:response.status,body:await boundedResponseBody(response)};}finally{clearTimeout(timer);}
|
||||
function matches(status: number, body: string, oracle: HttpAssertion['expected']): boolean {
|
||||
return (
|
||||
status === oracle.status &&
|
||||
(oracle.includes === undefined || body.includes(oracle.includes)) &&
|
||||
(oracle.excludes === undefined || !body.includes(oracle.excludes))
|
||||
);
|
||||
}
|
||||
async function ready(port:number):Promise<boolean>{return await new Promise(resolve=>{const socket=connect({host:'127.0.0.1',port}),done=(value:boolean)=>{socket.removeAllListeners();socket.destroy();resolve(value);},timer=setTimeout(()=>done(false),500);socket.once('connect',()=>{clearTimeout(timer);done(true);});socket.once('error',()=>{clearTimeout(timer);done(false);});});}
|
||||
async function main(){
|
||||
const file=process.argv[2];if(!file||!file.startsWith('/policy/'))throw new Error('trusted policy path required');const raw=fs.readFileSync(file,'utf8');if(Buffer.byteLength(raw)>1024*1024)throw new Error('policy too large');const v=object(JSON.parse(raw),'verifier policy') as any;
|
||||
if(!['before','after'].includes(v.phase)||!Number.isInteger(v.port)||v.port<1024||v.port>65535||!Array.isArray(v.legitimate)||!v.security)throw new Error('invalid verifier policy');const config=v as Config;
|
||||
let booted=false;for(let attempt=0;attempt<60;attempt++){if(await ready(config.port)){booted=true;break;}await Bun.sleep(250);}
|
||||
let legitimate=false,security:VerificationObservation['security']='inconclusive',summary='application did not answer a legitimate control';
|
||||
if(booted){try{legitimate=(await Promise.all(config.legitimate.map(async a=>{const r=await request(a,config.port);return matches(r.status,r.body,a.expected);}))).every(Boolean);const r=await request(config.security,config.port),fixed=matches(r.status,r.body,config.security.expected),vulnerable=matches(r.status,r.body,config.security.vulnerable!);security=config.phase==='before'?(vulnerable&&!fixed?'intended_failure':fixed&&!vulnerable?'pass':'inconclusive'):(fixed&&!vulnerable?'pass':'inconclusive');summary=`boot=true legitimate=${legitimate} security=${security}`;}catch{summary='bounded verifier request failed';}}
|
||||
process.stdout.write(JSON.stringify({booted,legitimate,security,existingTests:false,output:summary,inputHash:''})+'\n');
|
||||
export async function boundedResponseBody(response: Response, limit = 65536): Promise<string> {
|
||||
if (!Number.isSafeInteger(limit) || limit < 1) throw new Error('invalid response limit');
|
||||
const declared = response.headers.get('content-length');
|
||||
if (declared !== null && (/^\d+$/.test(declared) ? Number(declared) > limit : true)) {
|
||||
await response.body?.cancel();
|
||||
throw new Error('response too large');
|
||||
}
|
||||
if (!response.body) return '';
|
||||
const reader = response.body.getReader(),
|
||||
chunks: Uint8Array[] = [];
|
||||
let total = 0;
|
||||
try {
|
||||
for (;;) {
|
||||
const next = await reader.read();
|
||||
if (next.done) break;
|
||||
if (!next.value) continue;
|
||||
total += next.value.byteLength;
|
||||
if (total > limit) {
|
||||
await reader.cancel();
|
||||
throw new Error('response too large');
|
||||
}
|
||||
chunks.push(next.value);
|
||||
}
|
||||
} finally {
|
||||
reader.releaseLock();
|
||||
}
|
||||
const bytes = new Uint8Array(total);
|
||||
let offset = 0;
|
||||
for (const chunk of chunks) {
|
||||
bytes.set(chunk, offset);
|
||||
offset += chunk.byteLength;
|
||||
}
|
||||
return new TextDecoder().decode(bytes);
|
||||
}
|
||||
if(import.meta.main)main().catch(()=>{process.stdout.write(JSON.stringify({booted:false,legitimate:false,security:'inconclusive',existingTests:false,output:'verifier setup failed',inputHash:''})+'\n');process.exitCode=1;});
|
||||
async function request(a: HttpAssertion, port: number): Promise<{ status: number; body: string }> {
|
||||
const controller = new AbortController(),
|
||||
timer = setTimeout(() => controller.abort(), 5000);
|
||||
try {
|
||||
const response = await fetch(`http://127.0.0.1:${port}${a.path}`, {
|
||||
method: a.method,
|
||||
headers: a.headers,
|
||||
body: ['GET'].includes(a.method) ? undefined : a.body,
|
||||
redirect: 'manual',
|
||||
signal: controller.signal,
|
||||
});
|
||||
return { status: response.status, body: await boundedResponseBody(response) };
|
||||
} finally {
|
||||
clearTimeout(timer);
|
||||
}
|
||||
}
|
||||
async function ready(port: number): Promise<boolean> {
|
||||
return await new Promise((resolve) => {
|
||||
const socket = connect({ host: '127.0.0.1', port }),
|
||||
done = (value: boolean) => {
|
||||
socket.removeAllListeners();
|
||||
socket.destroy();
|
||||
resolve(value);
|
||||
},
|
||||
timer = setTimeout(() => done(false), 500);
|
||||
socket.once('connect', () => {
|
||||
clearTimeout(timer);
|
||||
done(true);
|
||||
});
|
||||
socket.once('error', () => {
|
||||
clearTimeout(timer);
|
||||
done(false);
|
||||
});
|
||||
});
|
||||
}
|
||||
async function main() {
|
||||
const file = process.argv[2];
|
||||
if (!file || !file.startsWith('/policy/')) throw new Error('trusted policy path required');
|
||||
const raw = fs.readFileSync(file, 'utf8');
|
||||
if (Buffer.byteLength(raw) > 1024 * 1024) throw new Error('policy too large');
|
||||
const v = object(JSON.parse(raw), 'verifier policy') as any;
|
||||
if (
|
||||
!['before', 'after'].includes(v.phase) ||
|
||||
!Number.isInteger(v.port) ||
|
||||
v.port < 1024 ||
|
||||
v.port > 65535 ||
|
||||
!Array.isArray(v.legitimate) ||
|
||||
!v.security
|
||||
)
|
||||
throw new Error('invalid verifier policy');
|
||||
const config = v as Config;
|
||||
let booted = false;
|
||||
for (let attempt = 0; attempt < 60; attempt++) {
|
||||
if (await ready(config.port)) {
|
||||
booted = true;
|
||||
break;
|
||||
}
|
||||
await Bun.sleep(250);
|
||||
}
|
||||
let legitimate = false,
|
||||
security: VerificationObservation['security'] = 'inconclusive',
|
||||
summary = 'application did not answer a legitimate control';
|
||||
if (booted) {
|
||||
try {
|
||||
legitimate = (
|
||||
await Promise.all(
|
||||
config.legitimate.map(async (a) => {
|
||||
const r = await request(a, config.port);
|
||||
return matches(r.status, r.body, a.expected);
|
||||
}),
|
||||
)
|
||||
).every(Boolean);
|
||||
const r = await request(config.security, config.port),
|
||||
fixed = matches(r.status, r.body, config.security.expected),
|
||||
vulnerable = matches(r.status, r.body, config.security.vulnerable!);
|
||||
security =
|
||||
config.phase === 'before'
|
||||
? vulnerable && !fixed
|
||||
? 'intended_failure'
|
||||
: fixed && !vulnerable
|
||||
? 'pass'
|
||||
: 'inconclusive'
|
||||
: fixed && !vulnerable
|
||||
? 'pass'
|
||||
: 'inconclusive';
|
||||
summary = `boot=true legitimate=${legitimate} security=${security}`;
|
||||
} catch {
|
||||
summary = 'bounded verifier request failed';
|
||||
}
|
||||
}
|
||||
process.stdout.write(
|
||||
JSON.stringify({ booted, legitimate, security, existingTests: false, output: summary, inputHash: '' }) +
|
||||
'\n',
|
||||
);
|
||||
}
|
||||
if (import.meta.main)
|
||||
main().catch(() => {
|
||||
process.stdout.write(
|
||||
JSON.stringify({
|
||||
booted: false,
|
||||
legitimate: false,
|
||||
security: 'inconclusive',
|
||||
existingTests: false,
|
||||
output: 'verifier setup failed',
|
||||
inputHash: '',
|
||||
}) + '\n',
|
||||
);
|
||||
process.exitCode = 1;
|
||||
});
|
||||
+714
-105
@@ -1,136 +1,745 @@
|
||||
import { generateKeyPairSync, createPrivateKey, createPublicKey, randomBytes, sign, verify } from 'node:crypto';
|
||||
import { lstatSync, realpathSync } from 'node:fs';
|
||||
import { basename, dirname } from 'node:path';
|
||||
import {
|
||||
AssertionWitnessBinding, AssertionWitnessReceipt, Command, CsoError, MAX_OUTPUT,
|
||||
VerificationObservation, canonical, object, oneOf, sha256, string, validateCommand,
|
||||
generateKeyPairSync,
|
||||
createPrivateKey,
|
||||
createPublicKey,
|
||||
randomBytes,
|
||||
sign,
|
||||
verify,
|
||||
} from 'node:crypto';
|
||||
import { existsSync, lstatSync, realpathSync } from 'node:fs';
|
||||
import { basename, posix, win32 } from 'node:path';
|
||||
import {
|
||||
AssertionWitnessBinding,
|
||||
AssertionWitnessReceipt,
|
||||
Command,
|
||||
CsoError,
|
||||
MAX_OUTPUT,
|
||||
VerificationObservation,
|
||||
canonical,
|
||||
object,
|
||||
oneOf,
|
||||
sha256,
|
||||
string,
|
||||
validateCommand,
|
||||
validateVerificationObservation,
|
||||
} from './contracts';
|
||||
import { runProcess } from './process';
|
||||
|
||||
export interface WitnessTestExecution { command:Command; code:number; output:string; minimumPassingTests:number }
|
||||
export interface WitnessedVerificationResult { observation:VerificationObservation; witness:AssertionWitnessReceipt }
|
||||
export interface WitnessTestExecution {
|
||||
command: Command;
|
||||
code: number;
|
||||
output: string;
|
||||
minimumPassingTests: number;
|
||||
}
|
||||
export interface WitnessedVerificationResult {
|
||||
observation: VerificationObservation;
|
||||
witness: AssertionWitnessReceipt;
|
||||
}
|
||||
export interface AssertionWitnessHandle {
|
||||
readonly binding:AssertionWitnessBinding;
|
||||
attest(observation:VerificationObservation,executions:WitnessTestExecution[]):Promise<AssertionWitnessReceipt>;
|
||||
validate(receipt:unknown,observation:VerificationObservation,now?:number):AssertionWitnessReceipt;
|
||||
readonly binding: AssertionWitnessBinding;
|
||||
attest(
|
||||
observation: VerificationObservation,
|
||||
executions: WitnessTestExecution[],
|
||||
): Promise<AssertionWitnessReceipt>;
|
||||
validate(receipt: unknown, observation: VerificationObservation, now?: number): AssertionWitnessReceipt;
|
||||
}
|
||||
|
||||
const HASH=/^[a-f0-9]{64}$/;
|
||||
const PUBLIC_KEY=/^[a-f0-9]{88}$/;
|
||||
const SIGNATURE=/^[a-f0-9]{128}$/;
|
||||
const PROTOCOL='gstack-cso-assertion-witness-v1' as const;
|
||||
const MAX_RECEIPT_AGE=300_000;
|
||||
const exact=(value:Record<string,any>,allowed:readonly string[],name:string)=>{for(const key of Object.keys(value))if(!allowed.includes(key))throw new CsoError('INVALID_SCHEMA',`Unexpected ${name} field: ${key}`);};
|
||||
const hash=(value:unknown,name:string):string=>{if(typeof value!=='string'||!HASH.test(value))throw new CsoError('INVALID_SCHEMA',`${name} must be a sha256 hash`);return value;};
|
||||
const timestamp=(value:unknown,name:string):string=>{const result=string(value,name,64),ms=Date.parse(result);if(!Number.isFinite(ms)||new Date(ms).toISOString()!==result)throw new CsoError('INVALID_SCHEMA',`${name} must be a canonical UTC timestamp`);return result;};
|
||||
const HASH = /^[a-f0-9]{64}$/;
|
||||
const PUBLIC_KEY = /^[a-f0-9]{88}$/;
|
||||
const SIGNATURE = /^[a-f0-9]{128}$/;
|
||||
const PROTOCOL = 'gstack-cso-assertion-witness-v1' as const;
|
||||
const MAX_RECEIPT_AGE = 300_000;
|
||||
const exact = (value: Record<string, any>, allowed: readonly string[], name: string) => {
|
||||
for (const key of Object.keys(value))
|
||||
if (!allowed.includes(key)) throw new CsoError('INVALID_SCHEMA', `Unexpected ${name} field: ${key}`);
|
||||
};
|
||||
const hash = (value: unknown, name: string): string => {
|
||||
if (typeof value !== 'string' || !HASH.test(value))
|
||||
throw new CsoError('INVALID_SCHEMA', `${name} must be a sha256 hash`);
|
||||
return value;
|
||||
};
|
||||
const timestamp = (value: unknown, name: string): string => {
|
||||
const result = string(value, name, 64),
|
||||
ms = Date.parse(result);
|
||||
if (!Number.isFinite(ms) || new Date(ms).toISOString() !== result)
|
||||
throw new CsoError('INVALID_SCHEMA', `${name} must be a canonical UTC timestamp`);
|
||||
return result;
|
||||
};
|
||||
|
||||
export function validateAssertionWitnessBinding(value:unknown):AssertionWitnessBinding{
|
||||
const v=object(value,'assertion witness binding'),runtime=object(v.runtime,'assertion witness runtime'),runner=object(v.runner,'assertion witness runner');
|
||||
exact(v,['schemaVersion','protocol','nonce','phase','issuedAt','expiresAt','runId','findingId','policyHash','auditPolicyHash','runtime','runner','sourceHash','dependencyHash','configurationHash','requestHash','patchHash','harnessHash','assertionHash','fixturesHash'],'assertion witness binding');
|
||||
exact(runtime,['image','verifierImage','platform','profile'],'assertion witness runtime');exact(runner,['testToolchain','startPlanHash','testPlanHash','commandsHash','minimumPassingTestsHash'],'assertion witness runner');
|
||||
if(v.schemaVersion!==1||v.protocol!==PROTOCOL)throw new CsoError('INVALID_SCHEMA','Unsupported assertion witness protocol');
|
||||
const issuedAt=timestamp(v.issuedAt,'assertion witness issuedAt'),expiresAt=timestamp(v.expiresAt,'assertion witness expiresAt'),duration=Date.parse(expiresAt)-Date.parse(issuedAt);
|
||||
if(duration<=0||duration>MAX_RECEIPT_AGE)throw new CsoError('INVALID_SCHEMA','Assertion witness lifetime exceeds the bounded attempt policy');
|
||||
if(typeof v.nonce!=='string'||!HASH.test(v.nonce))throw new CsoError('INVALID_SCHEMA','Assertion witness nonce must be 32 random bytes');
|
||||
const findingId=string(v.findingId,'assertion witness findingId',64);if(!/^[a-f0-9]{32}$/.test(findingId))throw new CsoError('INVALID_SCHEMA','Assertion witness findingId is invalid');
|
||||
return{schemaVersion:1,protocol:PROTOCOL,nonce:v.nonce,phase:oneOf(v.phase,['before','after'],'assertion witness phase'),issuedAt,expiresAt,runId:string(v.runId,'assertion witness runId',200),findingId,policyHash:hash(v.policyHash,'assertion witness policyHash'),auditPolicyHash:hash(v.auditPolicyHash,'assertion witness auditPolicyHash'),runtime:{image:string(runtime.image,'assertion witness runtime image',500),verifierImage:string(runtime.verifierImage,'assertion witness verifier image',500),platform:string(runtime.platform,'assertion witness runtime platform',100),profile:string(runtime.profile,'assertion witness runtime profile',100)},runner:{testToolchain:oneOf(runner.testToolchain,['runtime','project'],'assertion witness test toolchain'),startPlanHash:hash(runner.startPlanHash,'assertion witness start plan'),testPlanHash:hash(runner.testPlanHash,'assertion witness test plan'),commandsHash:hash(runner.commandsHash,'assertion witness commands'),minimumPassingTestsHash:hash(runner.minimumPassingTestsHash,'assertion witness execution floors')},sourceHash:hash(v.sourceHash,'assertion witness sourceHash'),dependencyHash:hash(v.dependencyHash,'assertion witness dependencyHash'),configurationHash:hash(v.configurationHash,'assertion witness configurationHash'),requestHash:hash(v.requestHash,'assertion witness requestHash'),patchHash:hash(v.patchHash,'assertion witness patchHash'),harnessHash:hash(v.harnessHash,'assertion witness harnessHash'),assertionHash:hash(v.assertionHash,'assertion witness assertionHash'),fixturesHash:hash(v.fixturesHash,'assertion witness fixturesHash')};
|
||||
export function validateAssertionWitnessBinding(value: unknown): AssertionWitnessBinding {
|
||||
const v = object(value, 'assertion witness binding'),
|
||||
runtime = object(v.runtime, 'assertion witness runtime'),
|
||||
runner = object(v.runner, 'assertion witness runner');
|
||||
exact(
|
||||
v,
|
||||
[
|
||||
'schemaVersion',
|
||||
'protocol',
|
||||
'nonce',
|
||||
'phase',
|
||||
'issuedAt',
|
||||
'expiresAt',
|
||||
'runId',
|
||||
'findingId',
|
||||
'policyHash',
|
||||
'auditPolicyHash',
|
||||
'runtime',
|
||||
'runner',
|
||||
'sourceHash',
|
||||
'dependencyHash',
|
||||
'configurationHash',
|
||||
'requestHash',
|
||||
'patchHash',
|
||||
'harnessHash',
|
||||
'assertionHash',
|
||||
'fixturesHash',
|
||||
],
|
||||
'assertion witness binding',
|
||||
);
|
||||
exact(runtime, ['image', 'verifierImage', 'platform', 'profile'], 'assertion witness runtime');
|
||||
exact(
|
||||
runner,
|
||||
['testToolchain', 'startPlanHash', 'testPlanHash', 'commandsHash', 'minimumPassingTestsHash'],
|
||||
'assertion witness runner',
|
||||
);
|
||||
if (v.schemaVersion !== 1 || v.protocol !== PROTOCOL)
|
||||
throw new CsoError('INVALID_SCHEMA', 'Unsupported assertion witness protocol');
|
||||
const issuedAt = timestamp(v.issuedAt, 'assertion witness issuedAt'),
|
||||
expiresAt = timestamp(v.expiresAt, 'assertion witness expiresAt'),
|
||||
duration = Date.parse(expiresAt) - Date.parse(issuedAt);
|
||||
if (duration <= 0 || duration > MAX_RECEIPT_AGE)
|
||||
throw new CsoError('INVALID_SCHEMA', 'Assertion witness lifetime exceeds the bounded attempt policy');
|
||||
if (typeof v.nonce !== 'string' || !HASH.test(v.nonce))
|
||||
throw new CsoError('INVALID_SCHEMA', 'Assertion witness nonce must be 32 random bytes');
|
||||
const findingId = string(v.findingId, 'assertion witness findingId', 64);
|
||||
if (!/^[a-f0-9]{32}$/.test(findingId))
|
||||
throw new CsoError('INVALID_SCHEMA', 'Assertion witness findingId is invalid');
|
||||
return {
|
||||
schemaVersion: 1,
|
||||
protocol: PROTOCOL,
|
||||
nonce: v.nonce,
|
||||
phase: oneOf(v.phase, ['before', 'after'], 'assertion witness phase'),
|
||||
issuedAt,
|
||||
expiresAt,
|
||||
runId: string(v.runId, 'assertion witness runId', 200),
|
||||
findingId,
|
||||
policyHash: hash(v.policyHash, 'assertion witness policyHash'),
|
||||
auditPolicyHash: hash(v.auditPolicyHash, 'assertion witness auditPolicyHash'),
|
||||
runtime: {
|
||||
image: string(runtime.image, 'assertion witness runtime image', 500),
|
||||
verifierImage: string(runtime.verifierImage, 'assertion witness verifier image', 500),
|
||||
platform: string(runtime.platform, 'assertion witness runtime platform', 100),
|
||||
profile: string(runtime.profile, 'assertion witness runtime profile', 100),
|
||||
},
|
||||
runner: {
|
||||
testToolchain: oneOf(runner.testToolchain, ['runtime', 'project'], 'assertion witness test toolchain'),
|
||||
startPlanHash: hash(runner.startPlanHash, 'assertion witness start plan'),
|
||||
testPlanHash: hash(runner.testPlanHash, 'assertion witness test plan'),
|
||||
commandsHash: hash(runner.commandsHash, 'assertion witness commands'),
|
||||
minimumPassingTestsHash: hash(runner.minimumPassingTestsHash, 'assertion witness execution floors'),
|
||||
},
|
||||
sourceHash: hash(v.sourceHash, 'assertion witness sourceHash'),
|
||||
dependencyHash: hash(v.dependencyHash, 'assertion witness dependencyHash'),
|
||||
configurationHash: hash(v.configurationHash, 'assertion witness configurationHash'),
|
||||
requestHash: hash(v.requestHash, 'assertion witness requestHash'),
|
||||
patchHash: hash(v.patchHash, 'assertion witness patchHash'),
|
||||
harnessHash: hash(v.harnessHash, 'assertion witness harnessHash'),
|
||||
assertionHash: hash(v.assertionHash, 'assertion witness assertionHash'),
|
||||
fixturesHash: hash(v.fixturesHash, 'assertion witness fixturesHash'),
|
||||
};
|
||||
}
|
||||
|
||||
function receiptUnsigned(receipt:AssertionWitnessReceipt):Omit<AssertionWitnessReceipt,'signature'>{const {signature:_,...unsigned}=receipt;return unsigned;}
|
||||
function observationForReceipt(observation:VerificationObservation,binding:AssertionWitnessBinding,diagnosticTestsPassed:boolean):VerificationObservation{
|
||||
const checked=validateVerificationObservation(observation);
|
||||
return{...checked,existingTests:diagnosticTestsPassed,inputHash:binding.harnessHash};
|
||||
function receiptUnsigned(receipt: AssertionWitnessReceipt): Omit<AssertionWitnessReceipt, 'signature'> {
|
||||
const { signature: _, ...unsigned } = receipt;
|
||||
return unsigned;
|
||||
}
|
||||
export function witnessObservationHash(observation:VerificationObservation):string{return sha256(canonical(validateVerificationObservation(observation)));}
|
||||
|
||||
export function validateStoredAssertionWitnessReceipt(value:unknown):AssertionWitnessReceipt{
|
||||
const v=object(value,'assertion witness receipt'),binding=validateAssertionWitnessBinding(v.binding);
|
||||
exact(v,['schemaVersion','binding','keyId','publicKey','observationHash','externalAssertionsPassed','diagnosticTestsPassed','executions','signature'],'assertion witness receipt');
|
||||
if(v.schemaVersion!==1||typeof v.keyId!=='string'||!HASH.test(v.keyId)||typeof v.publicKey!=='string'||!PUBLIC_KEY.test(v.publicKey)||typeof v.signature!=='string'||!SIGNATURE.test(v.signature))throw new CsoError('INVALID_SCHEMA','Assertion witness cryptographic metadata is invalid');
|
||||
if(sha256(Buffer.from(v.publicKey,'hex'))!==v.keyId)throw new CsoError('INCOMPATIBLE_INPUT','Assertion witness key identity does not match its public key');
|
||||
if(typeof v.observationHash!=='string'||!HASH.test(v.observationHash)||typeof v.externalAssertionsPassed!=='boolean'||typeof v.diagnosticTestsPassed!=='boolean'||!Array.isArray(v.executions)||!v.executions.length||v.executions.length>100)throw new CsoError('INVALID_SCHEMA','Assertion witness outcomes are malformed');
|
||||
const executions=v.executions.map((raw:any,index:number)=>{const item=object(raw,`assertion witness execution ${index}`);exact(item,['commandHash','exitCode','outputHash','minimumPassingTests','executedTests','passingTests','reportedPassed'],`assertion witness execution ${index}`);if(!Number.isSafeInteger(item.exitCode)||item.exitCode<-1||item.exitCode>255||!Number.isSafeInteger(item.minimumPassingTests)||item.minimumPassingTests<1||!Number.isSafeInteger(item.executedTests)||item.executedTests<0||!Number.isSafeInteger(item.passingTests)||item.passingTests<0||item.passingTests>item.executedTests||typeof item.reportedPassed!=='boolean'||(item.reportedPassed&&item.passingTests<item.minimumPassingTests))throw new CsoError('INVALID_SCHEMA','Assertion witness execution outcome is malformed');return{commandHash:hash(item.commandHash,'assertion witness commandHash'),exitCode:item.exitCode,outputHash:hash(item.outputHash,'assertion witness outputHash'),minimumPassingTests:item.minimumPassingTests,executedTests:item.executedTests,passingTests:item.passingTests,reportedPassed:item.reportedPassed};});
|
||||
if(v.diagnosticTestsPassed!==executions.every(item=>item.reportedPassed))throw new CsoError('INCOMPATIBLE_INPUT','Assertion witness diagnostic summary does not match its executions');
|
||||
const receipt:AssertionWitnessReceipt={schemaVersion:1,binding,keyId:v.keyId,publicKey:v.publicKey,observationHash:v.observationHash,externalAssertionsPassed:v.externalAssertionsPassed,diagnosticTestsPassed:v.diagnosticTestsPassed,executions,signature:v.signature};
|
||||
let valid=false;try{valid=verify(null,Buffer.from(canonical(receiptUnsigned(receipt))),createPublicKey({key:Buffer.from(receipt.publicKey,'hex'),format:'der',type:'spki'}),Buffer.from(receipt.signature,'hex'));}catch{}
|
||||
if(!valid)throw new CsoError('INCOMPATIBLE_INPUT','Assertion witness signature is invalid');return receipt;
|
||||
function observationForReceipt(
|
||||
observation: VerificationObservation,
|
||||
binding: AssertionWitnessBinding,
|
||||
diagnosticTestsPassed: boolean,
|
||||
): VerificationObservation {
|
||||
const checked = validateVerificationObservation(observation);
|
||||
return { ...checked, existingTests: diagnosticTestsPassed, inputHash: binding.harnessHash };
|
||||
}
|
||||
export function witnessObservationHash(observation: VerificationObservation): string {
|
||||
return sha256(canonical(validateVerificationObservation(observation)));
|
||||
}
|
||||
|
||||
export function validateAssertionWitnessReceipt(value:unknown,expected:AssertionWitnessBinding,expectedPublicKey:string,observation:VerificationObservation,now=Date.now()):AssertionWitnessReceipt{
|
||||
const receipt=validateStoredAssertionWitnessReceipt(value),binding=validateAssertionWitnessBinding(expected);
|
||||
if(canonical(receipt.binding)!==canonical(binding)||receipt.publicKey!==expectedPublicKey)throw new CsoError('INCOMPATIBLE_INPUT','Assertion witness receipt does not bind this verification challenge');
|
||||
if(now<Date.parse(binding.issuedAt)||now>Date.parse(binding.expiresAt))throw new CsoError('INCOMPATIBLE_INPUT','Assertion witness receipt is stale');
|
||||
const normalized=observationForReceipt(observation,binding,receipt.diagnosticTestsPassed),external=normalized.booted&&normalized.legitimate&&normalized.security!=='inconclusive';
|
||||
if(receipt.observationHash!==witnessObservationHash(normalized)||receipt.externalAssertionsPassed!==external)throw new CsoError('INCOMPATIBLE_INPUT','Assertion witness receipt does not bind the external verifier observation');
|
||||
export function validateStoredAssertionWitnessReceipt(value: unknown): AssertionWitnessReceipt {
|
||||
const v = object(value, 'assertion witness receipt'),
|
||||
binding = validateAssertionWitnessBinding(v.binding);
|
||||
exact(
|
||||
v,
|
||||
[
|
||||
'schemaVersion',
|
||||
'binding',
|
||||
'keyId',
|
||||
'publicKey',
|
||||
'observationHash',
|
||||
'externalAssertionsPassed',
|
||||
'diagnosticTestsPassed',
|
||||
'executions',
|
||||
'signature',
|
||||
],
|
||||
'assertion witness receipt',
|
||||
);
|
||||
if (
|
||||
v.schemaVersion !== 1 ||
|
||||
typeof v.keyId !== 'string' ||
|
||||
!HASH.test(v.keyId) ||
|
||||
typeof v.publicKey !== 'string' ||
|
||||
!PUBLIC_KEY.test(v.publicKey) ||
|
||||
typeof v.signature !== 'string' ||
|
||||
!SIGNATURE.test(v.signature)
|
||||
)
|
||||
throw new CsoError('INVALID_SCHEMA', 'Assertion witness cryptographic metadata is invalid');
|
||||
if (sha256(Buffer.from(v.publicKey, 'hex')) !== v.keyId)
|
||||
throw new CsoError('INCOMPATIBLE_INPUT', 'Assertion witness key identity does not match its public key');
|
||||
if (
|
||||
typeof v.observationHash !== 'string' ||
|
||||
!HASH.test(v.observationHash) ||
|
||||
typeof v.externalAssertionsPassed !== 'boolean' ||
|
||||
typeof v.diagnosticTestsPassed !== 'boolean' ||
|
||||
!Array.isArray(v.executions) ||
|
||||
!v.executions.length ||
|
||||
v.executions.length > 100
|
||||
)
|
||||
throw new CsoError('INVALID_SCHEMA', 'Assertion witness outcomes are malformed');
|
||||
const executions = v.executions.map((raw: any, index: number) => {
|
||||
const item = object(raw, `assertion witness execution ${index}`);
|
||||
exact(
|
||||
item,
|
||||
[
|
||||
'commandHash',
|
||||
'exitCode',
|
||||
'outputHash',
|
||||
'minimumPassingTests',
|
||||
'executedTests',
|
||||
'passingTests',
|
||||
'reportedPassed',
|
||||
],
|
||||
`assertion witness execution ${index}`,
|
||||
);
|
||||
if (
|
||||
!Number.isSafeInteger(item.exitCode) ||
|
||||
item.exitCode < -1 ||
|
||||
item.exitCode > 255 ||
|
||||
!Number.isSafeInteger(item.minimumPassingTests) ||
|
||||
item.minimumPassingTests < 1 ||
|
||||
!Number.isSafeInteger(item.executedTests) ||
|
||||
item.executedTests < 0 ||
|
||||
!Number.isSafeInteger(item.passingTests) ||
|
||||
item.passingTests < 0 ||
|
||||
item.passingTests > item.executedTests ||
|
||||
typeof item.reportedPassed !== 'boolean' ||
|
||||
(item.reportedPassed && item.passingTests < item.minimumPassingTests)
|
||||
)
|
||||
throw new CsoError('INVALID_SCHEMA', 'Assertion witness execution outcome is malformed');
|
||||
return {
|
||||
commandHash: hash(item.commandHash, 'assertion witness commandHash'),
|
||||
exitCode: item.exitCode,
|
||||
outputHash: hash(item.outputHash, 'assertion witness outputHash'),
|
||||
minimumPassingTests: item.minimumPassingTests,
|
||||
executedTests: item.executedTests,
|
||||
passingTests: item.passingTests,
|
||||
reportedPassed: item.reportedPassed,
|
||||
};
|
||||
});
|
||||
if (v.diagnosticTestsPassed !== executions.every((item) => item.reportedPassed))
|
||||
throw new CsoError(
|
||||
'INCOMPATIBLE_INPUT',
|
||||
'Assertion witness diagnostic summary does not match its executions',
|
||||
);
|
||||
const receipt: AssertionWitnessReceipt = {
|
||||
schemaVersion: 1,
|
||||
binding,
|
||||
keyId: v.keyId,
|
||||
publicKey: v.publicKey,
|
||||
observationHash: v.observationHash,
|
||||
externalAssertionsPassed: v.externalAssertionsPassed,
|
||||
diagnosticTestsPassed: v.diagnosticTestsPassed,
|
||||
executions,
|
||||
signature: v.signature,
|
||||
};
|
||||
let valid = false;
|
||||
try {
|
||||
valid = verify(
|
||||
null,
|
||||
Buffer.from(canonical(receiptUnsigned(receipt))),
|
||||
createPublicKey({ key: Buffer.from(receipt.publicKey, 'hex'), format: 'der', type: 'spki' }),
|
||||
Buffer.from(receipt.signature, 'hex'),
|
||||
);
|
||||
} catch {}
|
||||
if (!valid) throw new CsoError('INCOMPATIBLE_INPUT', 'Assertion witness signature is invalid');
|
||||
return receipt;
|
||||
}
|
||||
|
||||
export function assertionWitnessSemanticValue(receipt:AssertionWitnessReceipt):unknown{
|
||||
const checked=validateStoredAssertionWitnessReceipt(receipt),{nonce:_,issuedAt:__,expiresAt:___,...stable}=checked.binding;
|
||||
return{binding:stable,observationHash:checked.observationHash,externalAssertionsPassed:checked.externalAssertionsPassed,diagnosticTestsPassed:checked.diagnosticTestsPassed,executions:checked.executions};
|
||||
}
|
||||
export function assertionWitnessPairHash(pair:{before:AssertionWitnessReceipt;after:AssertionWitnessReceipt}):string{
|
||||
return sha256(canonical({before:assertionWitnessSemanticValue(pair.before),after:assertionWitnessSemanticValue(pair.after)}));
|
||||
export function validateAssertionWitnessReceipt(
|
||||
value: unknown,
|
||||
expected: AssertionWitnessBinding,
|
||||
expectedPublicKey: string,
|
||||
observation: VerificationObservation,
|
||||
now = Date.now(),
|
||||
): AssertionWitnessReceipt {
|
||||
const receipt = validateStoredAssertionWitnessReceipt(value),
|
||||
binding = validateAssertionWitnessBinding(expected);
|
||||
if (canonical(receipt.binding) !== canonical(binding) || receipt.publicKey !== expectedPublicKey)
|
||||
throw new CsoError(
|
||||
'INCOMPATIBLE_INPUT',
|
||||
'Assertion witness receipt does not bind this verification challenge',
|
||||
);
|
||||
if (now < Date.parse(binding.issuedAt) || now > Date.parse(binding.expiresAt))
|
||||
throw new CsoError('INCOMPATIBLE_INPUT', 'Assertion witness receipt is stale');
|
||||
const normalized = observationForReceipt(observation, binding, receipt.diagnosticTestsPassed),
|
||||
external = normalized.booted && normalized.legitimate && normalized.security !== 'inconclusive';
|
||||
if (
|
||||
receipt.observationHash !== witnessObservationHash(normalized) ||
|
||||
receipt.externalAssertionsPassed !== external
|
||||
)
|
||||
throw new CsoError(
|
||||
'INCOMPATIBLE_INPUT',
|
||||
'Assertion witness receipt does not bind the external verifier observation',
|
||||
);
|
||||
return receipt;
|
||||
}
|
||||
|
||||
function witnessReplayValue(receipt:AssertionWitnessReceipt):unknown{
|
||||
const checked=validateStoredAssertionWitnessReceipt(receipt),{nonce:_,issuedAt:__,expiresAt:___,...binding}=checked.binding;
|
||||
return{binding,externalAssertionsPassed:checked.externalAssertionsPassed,diagnosticTestsPassed:checked.diagnosticTestsPassed,
|
||||
executions:checked.executions.map(({outputHash:_,...execution})=>execution)};
|
||||
export function assertionWitnessSemanticValue(receipt: AssertionWitnessReceipt): unknown {
|
||||
const checked = validateStoredAssertionWitnessReceipt(receipt),
|
||||
{ nonce: _, issuedAt: __, expiresAt: ___, ...stable } = checked.binding;
|
||||
return {
|
||||
binding: stable,
|
||||
observationHash: checked.observationHash,
|
||||
externalAssertionsPassed: checked.externalAssertionsPassed,
|
||||
diagnosticTestsPassed: checked.diagnosticTestsPassed,
|
||||
executions: checked.executions,
|
||||
};
|
||||
}
|
||||
export function assertionWitnessReplayHash(pair:{before:AssertionWitnessReceipt;after:AssertionWitnessReceipt}):string{
|
||||
return sha256(canonical({before:witnessReplayValue(pair.before),after:witnessReplayValue(pair.after)}));
|
||||
export function assertionWitnessPairHash(pair: {
|
||||
before: AssertionWitnessReceipt;
|
||||
after: AssertionWitnessReceipt;
|
||||
}): string {
|
||||
return sha256(
|
||||
canonical({
|
||||
before: assertionWitnessSemanticValue(pair.before),
|
||||
after: assertionWitnessSemanticValue(pair.after),
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
interface TestExecutionSummary {executedTests:number;passingTests:number;reportedPassed:boolean}
|
||||
function testExecutionSummary(command:Command,code:number,output:string,minimumPassingTests=1):TestExecutionSummary{
|
||||
const failed={executedTests:0,passingTests:0,reportedPassed:false};
|
||||
if(!Number.isInteger(minimumPassingTests)||minimumPassingTests<1||code!==0||!output||output.includes('[sensitive process output redacted]'))return failed;
|
||||
const clean=output.replace(/\x1b\[[0-?]*[ -/]*[@-~]/g,''),args=command.args,name=basename(command.executable),json=()=>{const end=clean.lastIndexOf('}');if(end<0)return undefined;for(let start=clean.lastIndexOf('{',end);start>=0;start=clean.lastIndexOf('{',start-1)){try{const value=JSON.parse(clean.slice(start,end+1));if(value&&typeof value==='object')return value;}catch{}}};
|
||||
let executedTests=0,passingTests=0,valid=false;
|
||||
if(name==='node'&&args.includes('--test')&&args.includes('--test-reporter=tap')){const paths=args.filter(arg=>arg.startsWith('./')).map(arg=>arg.slice(2)),registered=[...clean.matchAll(/^# Subtest:\s+(.+?)\s*$/gm)].map(match=>match[1]),isPathWrapper=(label:string)=>paths.some(path=>label===path||label.endsWith(`/${path}`));executedTests=Number(clean.match(/^# tests\s+(\d+)\s*$/m)?.[1]);passingTests=Number(clean.match(/^# pass\s+(\d+)\s*$/m)?.[1]);valid=registered.some(label=>!isPathWrapper(label))&&!registered.some(isPathWrapper)&&executedTests>=passingTests&&/^# fail\s+0\s*$/m.test(clean)&&/^# cancelled\s+0\s*$/m.test(clean);}
|
||||
else if(name==='bun'&&args.includes('test')){passingTests=Number(clean.match(/^\s*(\d+)\s+pass(?:es)?\s*$/mi)?.[1]);executedTests=Number(clean.match(/\bRan\s+(\d+)\s+tests?\b/i)?.[1]);valid=executedTests>=passingTests&&/^\s*0\s+fail(?:ures?)?\s*$/mi.test(clean);}
|
||||
else if(name==='jest'&&args.includes('--json')){const value=json();passingTests=Number(value?.numPassedTests);executedTests=Number(value?.numTotalTests);valid=value?.success===true&&value?.numFailedTests===0&&value?.numRuntimeErrorTestSuites===0&&executedTests>=passingTests;}
|
||||
else if(name==='vitest'&&args.includes('--reporter=verbose')){const match=clean.match(/^\s*Tests\s+.*?(\d+)\s+passed.*?\((\d+)\)\s*$/mi);passingTests=Number(match?.[1]);executedTests=Number(match?.[2]);valid=executedTests>=passingTests&&!/\b\d+\s+failed\b/i.test(match?.[0]??'');}
|
||||
else if(name==='mocha'&&args.includes('json')){const stats=json()?.stats;passingTests=Number(stats?.passes);executedTests=Number(stats?.tests);valid=stats?.failures===0&&Number.isSafeInteger(stats?.pending)&&executedTests===passingTests+stats.pending;}
|
||||
else if(name==='ava'&&args.includes('--tap')){executedTests=Number(clean.match(/^# tests\s+(\d+)\s*$/m)?.[1]);passingTests=Number(clean.match(/^# pass\s+(\d+)\s*$/m)?.[1]);valid=executedTests>=passingTests&&/^# fail\s+0\s*$/m.test(clean);}
|
||||
else if(name==='python'&&args.some(arg=>arg.includes('import pytest;')&&arg.includes('pytest.main'))){passingTests=Number(clean.match(/(?:^|\s)(\d+)\s+passed\b/i)?.[1]);const skipped=Number(clean.match(/(?:^|\s)(\d+)\s+skipped\b/i)?.[1]??0);executedTests=passingTests+skipped;valid=true;}
|
||||
else if(name==='python'&&args.some(arg=>arg.includes('import os,sys,unittest;')&&arg.includes('unittest.main'))){executedTests=Number(clean.match(/\bRan\s+(\d+)\s+tests?\b/i)?.[1]);const skipped=Number(clean.match(/\bskipped=(\d+)\b/i)?.[1]??0);passingTests=executedTests-skipped;valid=Number.isSafeInteger(skipped);}
|
||||
else if(name==='bundle'&&args[0]==='exec'&&args[1]==='rspec'&&args.includes('json')){const summary=json()?.summary,pending=Number(summary?.pending_count??0);executedTests=Number(summary?.example_count);passingTests=executedTests-pending;valid=Number.isSafeInteger(pending)&&summary?.failure_count===0&&(summary?.errors_outside_of_examples_count??0)===0;}
|
||||
else if(name==='bundle'&&args[0]==='exec'&&args[1]==='rails'&&args[2]==='test'&&args.includes('--no-color')){const match=clean.match(/\b(\d+)\s+runs?\s*,\s*(\d+)\s+assertions?\s*,\s*0\s+failures?\s*,\s*0\s+errors?\s*,\s*(\d+)\s+skips?\b/i),skipped=Number(match?.[3]);executedTests=Number(match?.[1]);passingTests=executedTests-skipped;valid=Number.isSafeInteger(skipped);}
|
||||
const countsValid=Number.isSafeInteger(executedTests)&&executedTests>=0&&Number.isSafeInteger(passingTests)&&passingTests>=0&&executedTests>=passingTests;
|
||||
return countsValid?{executedTests,passingTests,reportedPassed:valid&&passingTests>=minimumPassingTests}:failed;
|
||||
function witnessReplayValue(receipt: AssertionWitnessReceipt): unknown {
|
||||
const checked = validateStoredAssertionWitnessReceipt(receipt),
|
||||
{ nonce: _, issuedAt: __, expiresAt: ___, ...binding } = checked.binding;
|
||||
return {
|
||||
binding,
|
||||
externalAssertionsPassed: checked.externalAssertionsPassed,
|
||||
diagnosticTestsPassed: checked.diagnosticTestsPassed,
|
||||
executions: checked.executions.map(({ outputHash: _, ...execution }) => execution),
|
||||
};
|
||||
}
|
||||
export function testExecutionPassed(command:Command,code:number,output:string,minimumPassingTests=1):boolean{
|
||||
return testExecutionSummary(command,code,output,minimumPassingTests).reportedPassed;
|
||||
export function assertionWitnessReplayHash(pair: {
|
||||
before: AssertionWitnessReceipt;
|
||||
after: AssertionWitnessReceipt;
|
||||
}): string {
|
||||
return sha256(
|
||||
canonical({ before: witnessReplayValue(pair.before), after: witnessReplayValue(pair.after) }),
|
||||
);
|
||||
}
|
||||
|
||||
interface ChildRequest {privateKey:string;publicKey:string;binding:AssertionWitnessBinding;observation:VerificationObservation;executions:WitnessTestExecution[]}
|
||||
async function readChildInput():Promise<string>{const chunks:Buffer[]=[];let bytes=0;for await(const value of process.stdin){const chunk=Buffer.from(value);bytes+=chunk.length;if(bytes>2*MAX_OUTPUT)throw new CsoError('INVALID_SCHEMA','Assertion witness request exceeds the bounded input limit');chunks.push(chunk);}return Buffer.concat(chunks).toString('utf8');}
|
||||
function createReceipt(input:unknown):AssertionWitnessReceipt{
|
||||
const v=object(input,'assertion witness child request');exact(v,['privateKey','publicKey','binding','observation','executions'],'assertion witness child request');const binding=validateAssertionWitnessBinding(v.binding);
|
||||
if(Date.now()<Date.parse(binding.issuedAt)||Date.now()>Date.parse(binding.expiresAt))throw new CsoError('INCOMPATIBLE_INPUT','Assertion witness challenge is stale');
|
||||
if(typeof v.privateKey!=='string'||v.privateKey.length>4096||typeof v.publicKey!=='string'||!PUBLIC_KEY.test(v.publicKey))throw new CsoError('INVALID_SCHEMA','Assertion witness signing input is invalid');
|
||||
let privateKey;try{privateKey=createPrivateKey(v.privateKey);const derived=createPublicKey(privateKey).export({format:'der',type:'spki'}).toString('hex');if(derived!==v.publicKey)throw new Error();}catch{throw new CsoError('INCOMPATIBLE_INPUT','Assertion witness signing authority does not match the challenge');}
|
||||
const rawObservation=validateVerificationObservation(v.observation);if(!Array.isArray(v.executions)||!v.executions.length||v.executions.length>100)throw new CsoError('INVALID_SCHEMA','Assertion witness needs one or more canonical test executions');
|
||||
let outputBytes=0;const rawExecutions:WitnessTestExecution[]=v.executions.map((raw:any,index:number)=>{const item=object(raw,`witness execution ${index}`);exact(item,['command','code','output','minimumPassingTests'],`witness execution ${index}`);const command=validateCommand(item.command,`witness execution ${index}.command`);if(!Number.isSafeInteger(item.code)||item.code<-1||item.code>255||typeof item.output!=='string'||item.output.includes('\0')||!Number.isSafeInteger(item.minimumPassingTests)||item.minimumPassingTests<1)throw new CsoError('INVALID_SCHEMA','Assertion witness test execution is malformed');outputBytes+=Buffer.byteLength(item.output);if(outputBytes>MAX_OUTPUT)throw new CsoError('INVALID_SCHEMA','Assertion witness test output exceeds the group capture limit');return{command,code:item.code,output:item.output,minimumPassingTests:item.minimumPassingTests};});
|
||||
if(sha256(canonical(rawExecutions.map(item=>item.command)))!==binding.runner.commandsHash||sha256(canonical(rawExecutions.map(item=>item.minimumPassingTests)))!==binding.runner.minimumPassingTestsHash)throw new CsoError('INCOMPATIBLE_INPUT','Assertion witness executions do not match the helper-derived runner');
|
||||
const executions=rawExecutions.map(item=>{const summary=testExecutionSummary(item.command,item.code,item.output,item.minimumPassingTests);return{commandHash:sha256(canonical(item.command)),exitCode:item.code,outputHash:sha256(item.output),minimumPassingTests:item.minimumPassingTests,...summary};}),diagnosticTestsPassed=executions.every(item=>item.reportedPassed),observation=observationForReceipt(rawObservation,binding,diagnosticTestsPassed),externalAssertionsPassed=observation.booted&&observation.legitimate&&observation.security!=='inconclusive';
|
||||
const unsigned:Omit<AssertionWitnessReceipt,'signature'>={schemaVersion:1,binding,keyId:sha256(Buffer.from(v.publicKey,'hex')),publicKey:v.publicKey,observationHash:witnessObservationHash(observation),externalAssertionsPassed,diagnosticTestsPassed,executions};
|
||||
return{...unsigned,signature:sign(null,Buffer.from(canonical(unsigned)),privateKey).toString('hex')};
|
||||
interface TestExecutionSummary {
|
||||
executedTests: number;
|
||||
passingTests: number;
|
||||
reportedPassed: boolean;
|
||||
}
|
||||
function testExecutionSummary(
|
||||
command: Command,
|
||||
code: number,
|
||||
output: string,
|
||||
minimumPassingTests = 1,
|
||||
): TestExecutionSummary {
|
||||
const failed = { executedTests: 0, passingTests: 0, reportedPassed: false };
|
||||
if (
|
||||
!Number.isInteger(minimumPassingTests) ||
|
||||
minimumPassingTests < 1 ||
|
||||
code !== 0 ||
|
||||
!output ||
|
||||
output.includes('[sensitive process output redacted]')
|
||||
)
|
||||
return failed;
|
||||
const clean = output.replace(/\x1b\[[0-?]*[ -/]*[@-~]/g, ''),
|
||||
args = command.args,
|
||||
name = basename(command.executable),
|
||||
json = () => {
|
||||
const end = clean.lastIndexOf('}');
|
||||
if (end < 0) return undefined;
|
||||
for (let start = clean.lastIndexOf('{', end); start >= 0; start = clean.lastIndexOf('{', start - 1)) {
|
||||
try {
|
||||
const value = JSON.parse(clean.slice(start, end + 1));
|
||||
if (value && typeof value === 'object') return value;
|
||||
} catch {}
|
||||
}
|
||||
};
|
||||
let executedTests = 0,
|
||||
passingTests = 0,
|
||||
valid = false;
|
||||
if (name === 'node' && args.includes('--test') && args.includes('--test-reporter=tap')) {
|
||||
const paths = args.filter((arg) => arg.startsWith('./')).map((arg) => arg.slice(2)),
|
||||
registered = [...clean.matchAll(/^# Subtest:\s+(.+?)\s*$/gm)].map((match) => match[1]),
|
||||
isPathWrapper = (label: string) => paths.some((path) => label === path || label.endsWith(`/${path}`));
|
||||
executedTests = Number(clean.match(/^# tests\s+(\d+)\s*$/m)?.[1]);
|
||||
passingTests = Number(clean.match(/^# pass\s+(\d+)\s*$/m)?.[1]);
|
||||
valid =
|
||||
registered.some((label) => !isPathWrapper(label)) &&
|
||||
!registered.some(isPathWrapper) &&
|
||||
executedTests >= passingTests &&
|
||||
/^# fail\s+0\s*$/m.test(clean) &&
|
||||
/^# cancelled\s+0\s*$/m.test(clean);
|
||||
} else if (name === 'bun' && args.includes('test')) {
|
||||
passingTests = Number(clean.match(/^\s*(\d+)\s+pass(?:es)?\s*$/im)?.[1]);
|
||||
executedTests = Number(clean.match(/\bRan\s+(\d+)\s+tests?\b/i)?.[1]);
|
||||
valid = executedTests >= passingTests && /^\s*0\s+fail(?:ures?)?\s*$/im.test(clean);
|
||||
} else if (name === 'jest' && args.includes('--json')) {
|
||||
const value = json();
|
||||
passingTests = Number(value?.numPassedTests);
|
||||
executedTests = Number(value?.numTotalTests);
|
||||
valid =
|
||||
value?.success === true &&
|
||||
value?.numFailedTests === 0 &&
|
||||
value?.numRuntimeErrorTestSuites === 0 &&
|
||||
executedTests >= passingTests;
|
||||
} else if (name === 'vitest' && args.includes('--reporter=verbose')) {
|
||||
const match = clean.match(/^\s*Tests\s+.*?(\d+)\s+passed.*?\((\d+)\)\s*$/im);
|
||||
passingTests = Number(match?.[1]);
|
||||
executedTests = Number(match?.[2]);
|
||||
valid = executedTests >= passingTests && !/\b\d+\s+failed\b/i.test(match?.[0] ?? '');
|
||||
} else if (name === 'mocha' && args.includes('json')) {
|
||||
const stats = json()?.stats;
|
||||
passingTests = Number(stats?.passes);
|
||||
executedTests = Number(stats?.tests);
|
||||
valid =
|
||||
stats?.failures === 0 &&
|
||||
Number.isSafeInteger(stats?.pending) &&
|
||||
executedTests === passingTests + stats.pending;
|
||||
} else if (name === 'ava' && args.includes('--tap')) {
|
||||
executedTests = Number(clean.match(/^# tests\s+(\d+)\s*$/m)?.[1]);
|
||||
passingTests = Number(clean.match(/^# pass\s+(\d+)\s*$/m)?.[1]);
|
||||
valid = executedTests >= passingTests && /^# fail\s+0\s*$/m.test(clean);
|
||||
} else if (
|
||||
name === 'python' &&
|
||||
args.some((arg) => arg.includes('import pytest;') && arg.includes('pytest.main'))
|
||||
) {
|
||||
passingTests = Number(clean.match(/(?:^|\s)(\d+)\s+passed\b/i)?.[1]);
|
||||
const skipped = Number(clean.match(/(?:^|\s)(\d+)\s+skipped\b/i)?.[1] ?? 0);
|
||||
executedTests = passingTests + skipped;
|
||||
valid = true;
|
||||
} else if (
|
||||
name === 'python' &&
|
||||
args.some((arg) => arg.includes('import os,sys,unittest;') && arg.includes('unittest.main'))
|
||||
) {
|
||||
executedTests = Number(clean.match(/\bRan\s+(\d+)\s+tests?\b/i)?.[1]);
|
||||
const skipped = Number(clean.match(/\bskipped=(\d+)\b/i)?.[1] ?? 0);
|
||||
passingTests = executedTests - skipped;
|
||||
valid = Number.isSafeInteger(skipped);
|
||||
} else if (name === 'bundle' && args[0] === 'exec' && args[1] === 'rspec' && args.includes('json')) {
|
||||
const summary = json()?.summary,
|
||||
pending = Number(summary?.pending_count ?? 0);
|
||||
executedTests = Number(summary?.example_count);
|
||||
passingTests = executedTests - pending;
|
||||
valid =
|
||||
Number.isSafeInteger(pending) &&
|
||||
summary?.failure_count === 0 &&
|
||||
(summary?.errors_outside_of_examples_count ?? 0) === 0;
|
||||
} else if (
|
||||
name === 'bundle' &&
|
||||
args[0] === 'exec' &&
|
||||
args[1] === 'rails' &&
|
||||
args[2] === 'test' &&
|
||||
args.includes('--no-color')
|
||||
) {
|
||||
const match = clean.match(
|
||||
/\b(\d+)\s+runs?\s*,\s*(\d+)\s+assertions?\s*,\s*0\s+failures?\s*,\s*0\s+errors?\s*,\s*(\d+)\s+skips?\b/i,
|
||||
),
|
||||
skipped = Number(match?.[3]);
|
||||
executedTests = Number(match?.[1]);
|
||||
passingTests = executedTests - skipped;
|
||||
valid = Number.isSafeInteger(skipped);
|
||||
}
|
||||
const countsValid =
|
||||
Number.isSafeInteger(executedTests) &&
|
||||
executedTests >= 0 &&
|
||||
Number.isSafeInteger(passingTests) &&
|
||||
passingTests >= 0 &&
|
||||
executedTests >= passingTests;
|
||||
return countsValid
|
||||
? { executedTests, passingTests, reportedPassed: valid && passingTests >= minimumPassingTests }
|
||||
: failed;
|
||||
}
|
||||
export function testExecutionPassed(
|
||||
command: Command,
|
||||
code: number,
|
||||
output: string,
|
||||
minimumPassingTests = 1,
|
||||
): boolean {
|
||||
return testExecutionSummary(command, code, output, minimumPassingTests).reportedPassed;
|
||||
}
|
||||
|
||||
export async function runAssertionWitnessChild():Promise<void>{const receipt=createReceipt(JSON.parse(await readChildInput()));process.stdout.write(JSON.stringify(receipt)+'\n');}
|
||||
interface ChildRequest {
|
||||
privateKey: string;
|
||||
publicKey: string;
|
||||
binding: AssertionWitnessBinding;
|
||||
observation: VerificationObservation;
|
||||
executions: WitnessTestExecution[];
|
||||
}
|
||||
async function readChildInput(): Promise<string> {
|
||||
const chunks: Buffer[] = [];
|
||||
let bytes = 0;
|
||||
for await (const value of process.stdin) {
|
||||
const chunk = Buffer.from(value);
|
||||
bytes += chunk.length;
|
||||
if (bytes > 2 * MAX_OUTPUT)
|
||||
throw new CsoError('INVALID_SCHEMA', 'Assertion witness request exceeds the bounded input limit');
|
||||
chunks.push(chunk);
|
||||
}
|
||||
return Buffer.concat(chunks).toString('utf8');
|
||||
}
|
||||
function createReceipt(input: unknown): AssertionWitnessReceipt {
|
||||
const v = object(input, 'assertion witness child request');
|
||||
exact(
|
||||
v,
|
||||
['privateKey', 'publicKey', 'binding', 'observation', 'executions'],
|
||||
'assertion witness child request',
|
||||
);
|
||||
const binding = validateAssertionWitnessBinding(v.binding);
|
||||
if (Date.now() < Date.parse(binding.issuedAt) || Date.now() > Date.parse(binding.expiresAt))
|
||||
throw new CsoError('INCOMPATIBLE_INPUT', 'Assertion witness challenge is stale');
|
||||
if (
|
||||
typeof v.privateKey !== 'string' ||
|
||||
v.privateKey.length > 4096 ||
|
||||
typeof v.publicKey !== 'string' ||
|
||||
!PUBLIC_KEY.test(v.publicKey)
|
||||
)
|
||||
throw new CsoError('INVALID_SCHEMA', 'Assertion witness signing input is invalid');
|
||||
let privateKey;
|
||||
try {
|
||||
privateKey = createPrivateKey(v.privateKey);
|
||||
const derived = createPublicKey(privateKey).export({ format: 'der', type: 'spki' }).toString('hex');
|
||||
if (derived !== v.publicKey) throw new Error();
|
||||
} catch {
|
||||
throw new CsoError(
|
||||
'INCOMPATIBLE_INPUT',
|
||||
'Assertion witness signing authority does not match the challenge',
|
||||
);
|
||||
}
|
||||
const rawObservation = validateVerificationObservation(v.observation);
|
||||
if (!Array.isArray(v.executions) || !v.executions.length || v.executions.length > 100)
|
||||
throw new CsoError('INVALID_SCHEMA', 'Assertion witness needs one or more canonical test executions');
|
||||
let outputBytes = 0;
|
||||
const rawExecutions: WitnessTestExecution[] = v.executions.map((raw: any, index: number) => {
|
||||
const item = object(raw, `witness execution ${index}`);
|
||||
exact(item, ['command', 'code', 'output', 'minimumPassingTests'], `witness execution ${index}`);
|
||||
const command = validateCommand(item.command, `witness execution ${index}.command`);
|
||||
if (
|
||||
!Number.isSafeInteger(item.code) ||
|
||||
item.code < -1 ||
|
||||
item.code > 255 ||
|
||||
typeof item.output !== 'string' ||
|
||||
item.output.includes('\0') ||
|
||||
!Number.isSafeInteger(item.minimumPassingTests) ||
|
||||
item.minimumPassingTests < 1
|
||||
)
|
||||
throw new CsoError('INVALID_SCHEMA', 'Assertion witness test execution is malformed');
|
||||
outputBytes += Buffer.byteLength(item.output);
|
||||
if (outputBytes > MAX_OUTPUT)
|
||||
throw new CsoError('INVALID_SCHEMA', 'Assertion witness test output exceeds the group capture limit');
|
||||
return { command, code: item.code, output: item.output, minimumPassingTests: item.minimumPassingTests };
|
||||
});
|
||||
if (
|
||||
sha256(canonical(rawExecutions.map((item) => item.command))) !== binding.runner.commandsHash ||
|
||||
sha256(canonical(rawExecutions.map((item) => item.minimumPassingTests))) !==
|
||||
binding.runner.minimumPassingTestsHash
|
||||
)
|
||||
throw new CsoError(
|
||||
'INCOMPATIBLE_INPUT',
|
||||
'Assertion witness executions do not match the helper-derived runner',
|
||||
);
|
||||
const executions = rawExecutions.map((item) => {
|
||||
const summary = testExecutionSummary(item.command, item.code, item.output, item.minimumPassingTests);
|
||||
return {
|
||||
commandHash: sha256(canonical(item.command)),
|
||||
exitCode: item.code,
|
||||
outputHash: sha256(item.output),
|
||||
minimumPassingTests: item.minimumPassingTests,
|
||||
...summary,
|
||||
};
|
||||
}),
|
||||
diagnosticTestsPassed = executions.every((item) => item.reportedPassed),
|
||||
observation = observationForReceipt(rawObservation, binding, diagnosticTestsPassed),
|
||||
externalAssertionsPassed =
|
||||
observation.booted && observation.legitimate && observation.security !== 'inconclusive';
|
||||
const unsigned: Omit<AssertionWitnessReceipt, 'signature'> = {
|
||||
schemaVersion: 1,
|
||||
binding,
|
||||
keyId: sha256(Buffer.from(v.publicKey, 'hex')),
|
||||
publicKey: v.publicKey,
|
||||
observationHash: witnessObservationHash(observation),
|
||||
externalAssertionsPassed,
|
||||
diagnosticTestsPassed,
|
||||
executions,
|
||||
};
|
||||
return { ...unsigned, signature: sign(null, Buffer.from(canonical(unsigned)), privateKey).toString('hex') };
|
||||
}
|
||||
|
||||
export class AssertionWitnessSession{
|
||||
private privateKey:string;readonly publicKey:string;readonly keyId:string;private nonces=new Set<string>();
|
||||
constructor(private workDirectory:string,private deadline:number){const stat=lstatSync(workDirectory),real=realpathSync(workDirectory),resolved=lstatSync(real);if(!stat.isDirectory()||stat.isSymbolicLink()||!resolved.isDirectory()||resolved.isSymbolicLink()||stat.dev!==resolved.dev||stat.ino!==resolved.ino||(process.getuid&&resolved.uid!==process.getuid())||(resolved.mode&0o022)!==0)throw new CsoError('UNSAFE_PATH','Assertion witness working directory must be private and owned');this.workDirectory=real;const pair=generateKeyPairSync('ed25519');this.privateKey=pair.privateKey.export({format:'pem',type:'pkcs8'}).toString();this.publicKey=pair.publicKey.export({format:'der',type:'spki'}).toString('hex');this.keyId=sha256(Buffer.from(this.publicKey,'hex'));}
|
||||
handle(stable:Omit<AssertionWitnessBinding,'schemaVersion'|'protocol'|'nonce'|'issuedAt'|'expiresAt'>):AssertionWitnessHandle{
|
||||
const now=Date.now(),expires=Math.min(this.deadline,now+MAX_RECEIPT_AGE);if(expires<=now)throw new CsoError('DEADLINE','No time remains for an authenticated assertion witness');let nonce='';do{nonce=randomBytes(32).toString('hex');}while(this.nonces.has(nonce));this.nonces.add(nonce);
|
||||
const binding=validateAssertionWitnessBinding({schemaVersion:1,protocol:PROTOCOL,nonce,issuedAt:new Date(now).toISOString(),expiresAt:new Date(expires).toISOString(),...stable});let consumed=false;
|
||||
return{binding,attest:async(observation,executions)=>{if(consumed)throw new CsoError('INCOMPATIBLE_INPUT','Assertion witness challenge was already consumed');consumed=true;const input=JSON.stringify({privateKey:this.privateKey,publicKey:this.publicKey,binding,observation,executions} satisfies ChildRequest);if(Buffer.byteLength(input)>2*MAX_OUTPUT)throw new CsoError('REDACTION_FAILED','Assertion witness input exceeds the bounded helper channel');const bun=/^bun(?:\.exe)?$/i.test(basename(process.execPath)),file=bun?process.execPath:join(dirname(process.execPath),process.platform==='win32'?'gstack-cso-launcher.exe':'gstack-cso-launcher'),args=bun?[import.meta.path,'--child']:['__cso-assertion-witness'],env=process.platform==='win32'?{PATH:dirname(process.execPath),SYSTEMROOT:process.env.SYSTEMROOT??'C:\\Windows',WINDIR:process.env.WINDIR??'C:\\Windows'}:{PATH:'/usr/bin:/bin',LANG:'C.UTF-8',LC_ALL:'C.UTF-8',TZ:'UTC'},result=await runProcess(file,args,{cwd:this.workDirectory,env,timeoutMs:Math.max(1,expires-Date.now()),maxBytes:128*1024,input,raw:true});if(result.timedOut)throw new CsoError('DEADLINE','Assertion witness exceeded the verification deadline');if(result.truncated||result.code!==0)throw new CsoError('TOOL_FAILED','Authenticated assertion witness did not return a bounded receipt');let receipt:unknown;try{receipt=JSON.parse(result.stdout);}catch{throw new CsoError('TOOL_FAILED','Authenticated assertion witness returned invalid output');}return validateAssertionWitnessReceipt(receipt,binding,this.publicKey,observation);},validate:(receipt,observation,current=Date.now())=>validateAssertionWitnessReceipt(receipt,binding,this.publicKey,observation,current)};
|
||||
export async function runAssertionWitnessChild(): Promise<void> {
|
||||
const receipt = createReceipt(JSON.parse(await readChildInput()));
|
||||
process.stdout.write(JSON.stringify(receipt) + '\n');
|
||||
}
|
||||
|
||||
export function assertionWitnessChildCommand(input: {
|
||||
execPath: string;
|
||||
platform: NodeJS.Platform;
|
||||
modulePath: string;
|
||||
systemRoot?: string;
|
||||
windir?: string;
|
||||
}): { file: string; args: string[]; env: Record<string, string> } {
|
||||
const paths = input.platform === 'win32' ? win32 : posix,
|
||||
directory = paths.dirname(input.execPath);
|
||||
if (/^bun(?:\.exe)?$/i.test(paths.basename(input.execPath)))
|
||||
return {
|
||||
file: input.execPath,
|
||||
args: [input.modulePath, '--child'],
|
||||
env: witnessChildEnv(input, directory),
|
||||
};
|
||||
return {
|
||||
file: paths.join(
|
||||
directory,
|
||||
input.platform === 'win32' ? 'gstack-cso-launcher.exe' : 'gstack-cso-launcher',
|
||||
),
|
||||
args: ['__cso-assertion-witness'],
|
||||
env: witnessChildEnv(input, directory),
|
||||
};
|
||||
}
|
||||
|
||||
function witnessChildEnv(
|
||||
input: { platform: NodeJS.Platform; systemRoot?: string; windir?: string },
|
||||
directory: string,
|
||||
): Record<string, string> {
|
||||
return input.platform === 'win32'
|
||||
? {
|
||||
PATH: directory,
|
||||
SYSTEMROOT: input.systemRoot ?? 'C:\\Windows',
|
||||
WINDIR: input.windir ?? 'C:\\Windows',
|
||||
}
|
||||
: { PATH: '/usr/bin:/bin', LANG: 'C.UTF-8', LC_ALL: 'C.UTF-8', TZ: 'UTC' };
|
||||
}
|
||||
|
||||
export class AssertionWitnessSession {
|
||||
private privateKey: string;
|
||||
readonly publicKey: string;
|
||||
readonly keyId: string;
|
||||
private nonces = new Set<string>();
|
||||
constructor(
|
||||
private workDirectory: string,
|
||||
private deadline: number,
|
||||
private execPath: string = process.execPath,
|
||||
) {
|
||||
const stat = lstatSync(workDirectory),
|
||||
real = realpathSync(workDirectory),
|
||||
resolved = lstatSync(real);
|
||||
if (
|
||||
!stat.isDirectory() ||
|
||||
stat.isSymbolicLink() ||
|
||||
!resolved.isDirectory() ||
|
||||
resolved.isSymbolicLink() ||
|
||||
stat.dev !== resolved.dev ||
|
||||
stat.ino !== resolved.ino ||
|
||||
(process.getuid && resolved.uid !== process.getuid()) ||
|
||||
(resolved.mode & 0o022) !== 0
|
||||
)
|
||||
throw new CsoError('UNSAFE_PATH', 'Assertion witness working directory must be private and owned');
|
||||
this.workDirectory = real;
|
||||
const pair = generateKeyPairSync('ed25519');
|
||||
this.privateKey = pair.privateKey.export({ format: 'pem', type: 'pkcs8' }).toString();
|
||||
this.publicKey = pair.publicKey.export({ format: 'der', type: 'spki' }).toString('hex');
|
||||
this.keyId = sha256(Buffer.from(this.publicKey, 'hex'));
|
||||
}
|
||||
handle(
|
||||
stable: Omit<AssertionWitnessBinding, 'schemaVersion' | 'protocol' | 'nonce' | 'issuedAt' | 'expiresAt'>,
|
||||
): AssertionWitnessHandle {
|
||||
const now = Date.now(),
|
||||
expires = Math.min(this.deadline, now + MAX_RECEIPT_AGE);
|
||||
if (expires <= now)
|
||||
throw new CsoError('DEADLINE', 'No time remains for an authenticated assertion witness');
|
||||
let nonce = '';
|
||||
do {
|
||||
nonce = randomBytes(32).toString('hex');
|
||||
} while (this.nonces.has(nonce));
|
||||
this.nonces.add(nonce);
|
||||
const binding = validateAssertionWitnessBinding({
|
||||
schemaVersion: 1,
|
||||
protocol: PROTOCOL,
|
||||
nonce,
|
||||
issuedAt: new Date(now).toISOString(),
|
||||
expiresAt: new Date(expires).toISOString(),
|
||||
...stable,
|
||||
});
|
||||
let consumed = false;
|
||||
return {
|
||||
binding,
|
||||
attest: async (observation, executions) => {
|
||||
if (consumed)
|
||||
throw new CsoError('INCOMPATIBLE_INPUT', 'Assertion witness challenge was already consumed');
|
||||
consumed = true;
|
||||
const input = JSON.stringify({
|
||||
privateKey: this.privateKey,
|
||||
publicKey: this.publicKey,
|
||||
binding,
|
||||
observation,
|
||||
executions,
|
||||
} satisfies ChildRequest);
|
||||
if (Buffer.byteLength(input) > 2 * MAX_OUTPUT)
|
||||
throw new CsoError(
|
||||
'REDACTION_FAILED',
|
||||
'Assertion witness input exceeds the bounded helper channel',
|
||||
);
|
||||
const { file, args, env } = assertionWitnessChildCommand({
|
||||
execPath: this.execPath,
|
||||
platform: process.platform,
|
||||
modulePath: import.meta.path,
|
||||
systemRoot: process.env.SYSTEMROOT,
|
||||
windir: process.env.WINDIR,
|
||||
});
|
||||
if (!existsSync(file))
|
||||
throw new CsoError('PREREQUISITE', `Assertion witness launcher is missing: ${file}`);
|
||||
const result = await runProcess(file, args, {
|
||||
cwd: this.workDirectory,
|
||||
env,
|
||||
timeoutMs: Math.max(1, expires - Date.now()),
|
||||
maxBytes: 128 * 1024,
|
||||
input,
|
||||
raw: true,
|
||||
});
|
||||
if (result.timedOut)
|
||||
throw new CsoError('DEADLINE', 'Assertion witness exceeded the verification deadline');
|
||||
if (result.truncated || result.code !== 0)
|
||||
throw new CsoError(
|
||||
'TOOL_FAILED',
|
||||
'Authenticated assertion witness did not return a bounded receipt',
|
||||
);
|
||||
let receipt: unknown;
|
||||
try {
|
||||
receipt = JSON.parse(result.stdout);
|
||||
} catch {
|
||||
throw new CsoError('TOOL_FAILED', 'Authenticated assertion witness returned invalid output');
|
||||
}
|
||||
return validateAssertionWitnessReceipt(receipt, binding, this.publicKey, observation);
|
||||
},
|
||||
validate: (receipt, observation, current = Date.now()) =>
|
||||
validateAssertionWitnessReceipt(receipt, binding, this.publicKey, observation, current),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if(import.meta.main&&process.argv.at(-1)==='--child')runAssertionWitnessChild().catch(()=>{process.stderr.write('assertion witness failed\n');process.exitCode=1;});
|
||||
if (import.meta.main && process.argv.at(-1) === '--child')
|
||||
runAssertionWitnessChild().catch(() => {
|
||||
process.stderr.write('assertion witness failed\n');
|
||||
process.exitCode = 1;
|
||||
});
|
||||
+1
-1
@@ -179,7 +179,7 @@ export class DesignMdEditRefused extends Error {
|
||||
}
|
||||
|
||||
/** Does a section heading name the requested section? By canonical name when the request has one, else by exact (case-insensitive) heading. */
|
||||
function headingMatches(heading: string, wanted: string, canonical: CanonicalSection | null): boolean {
|
||||
function headingMatches(heading: string, wanted: string, canonical: CanonicalSection | null | undefined): boolean {
|
||||
return canonical ? canonicalFor(heading) === canonical : heading.trim().toLowerCase() === wanted.trim().toLowerCase();
|
||||
}
|
||||
|
||||
|
||||
+6
-1
@@ -7,6 +7,7 @@ import { initializeWindowsReviewJob } from './claude-code-windows-job';
|
||||
|
||||
const MAX_MS = 2_147_483_647;
|
||||
class QaDeadlineError extends Error {}
|
||||
const QA_DEADLINE_USAGE = 'gstack-qa-deadline start FILE SECONDS [EARLIER_UTC] | status FILE | run FILE -- COMMAND ARGS...';
|
||||
type QaCommandResult = { exitCode: number; signal: NodeJS.Signals | null; completed: boolean };
|
||||
type Emit = (stream: 'stdout' | 'stderr', receipt: Record<string, unknown>, completion?: QaCommandResult) => void;
|
||||
|
||||
@@ -269,6 +270,10 @@ export async function qaDeadlineMain(args: string[], receiptWorker = false): Pro
|
||||
return withQaReceiptOutput(receiptWorker, 'qa-deadline-receipt', receipt => '\nQA_DEADLINE ' + JSON.stringify({ guard: 'qa-deadline', ...receipt }) + '\n', async emit => {
|
||||
try {
|
||||
const [action, file, ...rest] = args;
|
||||
if (action === '--help' && args.length === 1) {
|
||||
emit('stdout', { event: 'help', usage: QA_DEADLINE_USAGE });
|
||||
return 0;
|
||||
}
|
||||
if (action === 'start' && file && (rest.length === 1 || rest.length === 2)) {
|
||||
const status = qaDeadlineStatus(startQaDeadline(file, rest[0], rest[1]));
|
||||
emit('stdout', { event: 'start', ...status });
|
||||
@@ -283,7 +288,7 @@ export async function qaDeadlineMain(args: string[], receiptWorker = false): Pro
|
||||
if (process.platform === 'win32' && !receiptWorker) return await runWindowsWorker(args, emit);
|
||||
return await runQaDeadlineCommand(file, rest[1], rest.slice(2), emit);
|
||||
}
|
||||
throw new QaDeadlineError('Usage: gstack-qa-deadline start FILE SECONDS [EARLIER_UTC] | status FILE | run FILE -- COMMAND ARGS...');
|
||||
throw new QaDeadlineError(`Usage: ${QA_DEADLINE_USAGE}`);
|
||||
} catch (error) {
|
||||
emit('stderr', { event: 'error', message: error instanceof QaDeadlineError ? error.message : 'Deadline guard failed' });
|
||||
return 2;
|
||||
|
||||
+141
-15
@@ -1,14 +1,19 @@
|
||||
import * as fs from 'node:fs';
|
||||
import * as path from 'node:path';
|
||||
import { createHash } from 'node:crypto';
|
||||
import { spawnSync } from 'node:child_process';
|
||||
import { atomicWriteSync } from './fs-atomic';
|
||||
import { runQaDeadlineCommand, runQaWindowsWorker, startQaDeadline, withQaReceiptOutput } from './qa-deadline';
|
||||
import { qaDeadlineStatus, readQaDeadline, runQaDeadlineCommand, runQaWindowsWorker, startQaDeadline, withQaReceiptOutput } from './qa-deadline';
|
||||
import { scan } from './redact-engine';
|
||||
|
||||
const object = (value: unknown): value is Record<string, any> => value !== null && typeof value === 'object' && !Array.isArray(value);
|
||||
const hash = (value: string | Buffer) => createHash('sha256').update(value).digest('hex');
|
||||
const exact = (value: unknown, keys: string[]) => object(value) && Object.keys(value).sort().join(',') === keys.sort().join(',');
|
||||
class QaEvidenceError extends Error {}
|
||||
const currentRevision = () => {
|
||||
const result = spawnSync('git', ['rev-parse', 'HEAD'], { encoding: 'utf8', timeout: 5000, env: { ...process.env, GIT_OPTIONAL_LOCKS: '0' } });
|
||||
return result.status === 0 && /^[0-9a-f]{40,64}$/.test(result.stdout.trim()) ? result.stdout.trim() : undefined;
|
||||
};
|
||||
|
||||
function id(value: string): string {
|
||||
if (!/^\d{3}$/.test(value)) throw new QaEvidenceError('Capture and checkpoint IDs must be three digits');
|
||||
@@ -108,12 +113,61 @@ export function readQaCapture(reportRoot: string, captureId: string, expectedHas
|
||||
return { receipt, sha256, stdout: out, stderr: err, observed, observationText };
|
||||
}
|
||||
|
||||
async function capture(root: string, captureId: string, publicOutput: boolean, option: string, budget: string, command: string, args: string[]) {
|
||||
const anchoredOn = (command: unknown, captureId: string) => typeof command === 'string' && new RegExp(`\\scapture\\s+\\S+\\s+${captureId}(?:\\s|$)`).test(command);
|
||||
const nativeCommand = (command: string) => command.slice(command.indexOf(' -- ') + 4).trim();
|
||||
const MERGED_NOTE = ['observationCapture', 'observationArgv', 'observed', 'hypothesis', 'nextCapture', 'nextArgv'];
|
||||
const links = (note: Record<string, any>, previous: string, captureId: string) => note.observationCapture === previous && note.nextCapture === captureId
|
||||
|| anchoredOn(note.observationCommand, previous) && anchoredOn(note.nextCommand, captureId);
|
||||
const learned = (note: Record<string, any>) => exact(note, MERGED_NOTE)
|
||||
? JSON.stringify(note.observationArgv) !== JSON.stringify(note.nextArgv)
|
||||
: typeof note.observationCommand === 'string' && typeof note.nextCommand === 'string' && nativeCommand(note.observationCommand) !== nativeCommand(note.nextCommand);
|
||||
const validHypothesis = (value: unknown) => typeof value === 'string' && value.trim().length > 20 && /[a-z]{3}/i.test(value);
|
||||
|
||||
function checkpointNotes(root: string): Record<string, any>[] {
|
||||
return fs.readdirSync(root).filter(name => /^exploration-\d{3}\.json$/.test(name)).sort()
|
||||
.map(name => ({ name, ...JSON.parse(decode(read(root, name))) }));
|
||||
}
|
||||
|
||||
function completeReceipts(root: string): Record<string, any>[] {
|
||||
if (!fs.existsSync(path.join(root, '.qa-evidence'))) return [];
|
||||
return fs.readdirSync(owned(root, '.qa-evidence')).filter(name => /^\d{3}$/.test(name) && fs.existsSync(path.join(root, '.qa-evidence', name, 'receipt.json')))
|
||||
.map(name => JSON.parse(decode(read(root, `.qa-evidence/${name}/receipt.json`))))
|
||||
.filter(receipt => receipt.status === 'complete')
|
||||
.sort((a, b) => Date.parse(a.completedAt) - Date.parse(b.completedAt));
|
||||
}
|
||||
const completeCaptures = (root: string): string[] => completeReceipts(root).map(receipt => receipt.id);
|
||||
const latestCompleteCapture = (root: string): string | undefined => completeCaptures(root).at(-1);
|
||||
|
||||
/** Required native probes the caller declared (GSTACK_QA_REQUIRED_PROBES, a JSON array of child commands) that no complete capture has run yet. Informational only. */
|
||||
function requiredRemaining(root: string): { requiredRemaining?: string[] } {
|
||||
let required: unknown;
|
||||
try { required = JSON.parse(process.env.GSTACK_QA_REQUIRED_PROBES ?? 'null'); } catch { return {}; }
|
||||
if (!Array.isArray(required) || !required.every(item => typeof item === 'string')) return {};
|
||||
const run = new Set(completeReceipts(root).map(receipt => Array.isArray(receipt.argv) ? receipt.argv.join(' ') : ''));
|
||||
return { requiredRemaining: required.filter(command => !run.has(command)) };
|
||||
}
|
||||
|
||||
async function capture(root: string, captureId: string, publicOutput: boolean, option: string, budget: string, command: string, args: string[], after?: { capture: string; hypothesis: string }) {
|
||||
id(captureId);
|
||||
if (!command || !['--deadline', '--timeout-ms'].includes(option)) throw new QaEvidenceError('Capture requires a deadline or finite command timeout');
|
||||
const previous = latestCompleteCapture(root);
|
||||
if (after && after.capture !== previous) throw new QaEvidenceError(previous ? `--after must name capture ${previous}, the latest complete capture` : 'The first capture takes no --after');
|
||||
if (after && !validHypothesis(after.hypothesis)) throw new QaEvidenceError('Invalid --hypothesis: need one causal sentence over 20 characters');
|
||||
if (!after && previous && !checkpointNotes(root).some(note => links(note, previous, captureId))) {
|
||||
throw new QaEvidenceError(`Checkpoint required before capture ${captureId}: rerun with the causal note for capture ${previous}: capture ROOT ${captureId} ${publicOutput ? '--public ' : ''}${option} ${budget} --after ${previous} --hypothesis 'what capture ${previous} taught you to test next' -- COMMAND ARGS. To stop exploring instead, run no further probe.`);
|
||||
}
|
||||
if (option === '--timeout-ms' && (!/^[1-9]\d*$/.test(budget) || !Number.isSafeInteger(Number(budget)) || Number(budget) > 2_147_483_647)) throw new QaEvidenceError('Invalid command timeout');
|
||||
let note: Record<string, unknown> | undefined;
|
||||
if (after) {
|
||||
const observation = readQaCapture(root, after.capture);
|
||||
note = { observationCapture: after.capture, observationArgv: observation.receipt.argv, observed: observation.observed,
|
||||
hypothesis: after.hypothesis, nextCapture: captureId, nextArgv: [command, ...args] };
|
||||
if (scan(JSON.stringify(note)).findings.some(finding => finding.tier === 'HIGH')) throw new QaEvidenceError('Sensitive intent cannot be published');
|
||||
if (fs.existsSync(owned(root, `exploration-${captureId}.json`))) throw new QaEvidenceError(`Checkpoint ${captureId} already exists; use a fresh capture ID`);
|
||||
}
|
||||
privateDirectory(root, '.qa-evidence');
|
||||
const directory = privateDirectory(root, `.qa-evidence/${captureId}`, true);
|
||||
const checkpointSha256 = note && publish(root, `exploration-${captureId}.json`, note);
|
||||
const deadline = option === '--deadline' ? owned(root, path.resolve(budget)) : path.join(directory, 'deadline.json');
|
||||
if (option === '--timeout-ms') startQaDeadline(deadline, (Number(budget) / 1000).toFixed(3));
|
||||
const startedAt = new Date().toISOString();
|
||||
@@ -174,11 +228,18 @@ async function capture(root: string, captureId: string, publicOutput: boolean, o
|
||||
fs.writeFileSync(owned(root, `.qa-evidence/${captureId}/observation.json`), bytes, { flag: 'wx', mode: 0o600 });
|
||||
observation = { sha256: hash(bytes), bytes: Buffer.byteLength(bytes) };
|
||||
}
|
||||
const completedAt = new Date().toISOString();
|
||||
let remainingMs: number | undefined;
|
||||
if (option === '--deadline') try { remainingMs = qaDeadlineStatus(readQaDeadline(deadline)).remainingMs; } catch {}
|
||||
const receipt = { version: 1, id: captureId, cwd: process.cwd(), argv: [command, ...args], deadline, timing, startedAt,
|
||||
completedAt: new Date().toISOString(), exitCode, signal: result.signal, status, observation, publicOutput,
|
||||
completedAt, exitCode, signal: result.signal, status, observation, publicOutput,
|
||||
...streams };
|
||||
const sha256 = publish(root, `.qa-evidence/${captureId}/receipt.json`, receipt);
|
||||
return { action: 'capture', id: captureId, status, sha256, exitCode, signal: result.signal, publicOutput };
|
||||
return { action: 'capture', id: captureId, status, sha256, exitCode, signal: result.signal, publicOutput,
|
||||
startedAt, completedAt, durationMs: Date.parse(completedAt) - Date.parse(startedAt), ...(remainingMs === undefined ? {} : { remainingMs }),
|
||||
...(checkpointSha256 ? { checkpoint: captureId, checkpointSha256, link: `[checkpoint ${captureId}](exploration-${captureId}.json)` } : {}),
|
||||
...(status === 'complete' ? { next: `Another probe requires a checkpoint anchored on capture ${captureId}: add --after ${captureId} --hypothesis 'TEXT' before --. To stop exploring, run none.` } : {}),
|
||||
...requiredRemaining(root) };
|
||||
}
|
||||
|
||||
function checkpoint(root: string, checkpointId: string, source: string | Record<string, string>) {
|
||||
@@ -187,8 +248,8 @@ function checkpoint(root: string, checkpointId: string, source: string | Record<
|
||||
const intent = JSON.parse(decode(bytes));
|
||||
if (!exact(intent, ['capture', 'observationCommand', 'hypothesis', 'nextCommand'])
|
||||
|| typeof intent.capture !== 'string' || typeof intent.observationCommand !== 'string' || !intent.observationCommand.trim()
|
||||
|| typeof intent.hypothesis !== 'string' || intent.hypothesis.trim().length <= 20 || !/[a-z]{3}/i.test(intent.hypothesis)
|
||||
|| typeof intent.nextCommand !== 'string' || !intent.nextCommand.trim()) throw new QaEvidenceError('Invalid causal intent');
|
||||
|| !validHypothesis(intent.hypothesis)
|
||||
|| typeof intent.nextCommand !== 'string' || !intent.nextCommand.trim()) throw new QaEvidenceError('Invalid causal intent: need exactly capture, observationCommand, hypothesis (one sentence over 20 characters) and nextCommand');
|
||||
if (scan(decode(bytes)).findings.some(finding => finding.tier === 'HIGH')) throw new QaEvidenceError('Sensitive intent cannot be published');
|
||||
const captured = readQaCapture(root, intent.capture);
|
||||
const value = { observationCommand: intent.observationCommand, observed: captured.observed, hypothesis: intent.hypothesis, nextCommand: intent.nextCommand };
|
||||
@@ -197,44 +258,109 @@ function checkpoint(root: string, checkpointId: string, source: string | Record<
|
||||
link: `[checkpoint ${checkpointId}](exploration-${checkpointId}.json)`, exitCode: 0 };
|
||||
}
|
||||
|
||||
/** Labels the verdict reads; an unrecognized label is rejected before publication so it can be corrected. */
|
||||
const QA_CLASSIFICATIONS = ['pass', 'superseded', 'product-defect', 'fail', 'setup-blocked', 'blocked', 'inconclusive'];
|
||||
|
||||
function materialize(root: string, source: string) {
|
||||
const bytes = read(root, source);
|
||||
if (scan(decode(bytes)).findings.some(finding => finding.tier === 'HIGH')) throw new QaEvidenceError('Sensitive annotations cannot be published');
|
||||
const annotations = JSON.parse(decode(bytes));
|
||||
const supplied = JSON.parse(decode(bytes));
|
||||
if (!object(supplied)) throw new QaEvidenceError('Invalid report annotations: need a JSON object');
|
||||
const notes = checkpointNotes(root);
|
||||
const measured: Record<string, string | undefined> = { revision: currentRevision(), runtime: `bun ${Bun.version}`, cwd: process.cwd() };
|
||||
for (const [key, value] of Object.entries(measured)) {
|
||||
if (value !== undefined && supplied[key] !== undefined && supplied[key] !== value) {
|
||||
throw new QaEvidenceError(`Invalid report annotations: ${key} must be ${JSON.stringify(value)}; omit it and Q fills it`);
|
||||
}
|
||||
}
|
||||
if (!measured.revision && supplied.revision === undefined) throw new QaEvidenceError('Invalid report annotations: revision is required when git rev-parse HEAD is unavailable');
|
||||
const annotations: Record<string, any> = {
|
||||
revision: measured.revision ?? supplied.revision,
|
||||
runtime: measured.runtime,
|
||||
cwd: measured.cwd,
|
||||
limits: typeof supplied.limits === 'string' ? [supplied.limits] : supplied.limits,
|
||||
evidence: supplied.evidence,
|
||||
learning: supplied.learning ?? notes.filter(({ name, ...note }) => learned(note)).map(note => note.name.slice(12, 15)),
|
||||
...Object.fromEntries(Object.entries(supplied).filter(([key]) => !['revision', 'runtime', 'cwd', 'limits', 'evidence', 'learning'].includes(key))),
|
||||
};
|
||||
if (!exact(annotations, ['revision', 'runtime', 'cwd', 'limits', 'evidence', 'learning'])
|
||||
|| !['revision', 'runtime', 'cwd'].every(key => typeof annotations[key] === 'string' && annotations[key].trim())
|
||||
|| !Array.isArray(annotations.limits) || !annotations.limits.length || !annotations.limits.every((limit: unknown) => typeof limit === 'string' && limit.trim())
|
||||
|| !Array.isArray(annotations.evidence) || !Array.isArray(annotations.learning)) throw new QaEvidenceError('Invalid report annotations');
|
||||
|| !Array.isArray(annotations.evidence) || !Array.isArray(annotations.learning)) throw new QaEvidenceError('Invalid report annotations: need limits (non-empty string array) and evidence (row array), no other keys; revision, runtime and cwd (non-empty strings) and learning (checkpoint ID array) are filled in when omitted');
|
||||
const captures = new Set<string>();
|
||||
const argv: string[] = [];
|
||||
const evidence = annotations.evidence.map((row: any) => {
|
||||
if (!exact(row, ['capture', 'command', 'contract', 'expected', 'classification'])
|
||||
|| !Object.values(row).every(value => typeof value === 'string' && value.trim()) || captures.has(row.capture)) throw new QaEvidenceError('Invalid evidence annotation');
|
||||
|| !Object.values(row).every(value => typeof value === 'string' && value.trim()) || captures.has(row.capture)) throw new QaEvidenceError('Invalid evidence annotation: each row needs exactly capture, command, contract, expected and classification as non-empty strings, with a unique capture');
|
||||
if (!QA_CLASSIFICATIONS.includes(row.classification)) throw new QaEvidenceError(`Invalid evidence annotation: capture ${row.capture} classification must be one of ${QA_CLASSIFICATIONS.join(', ')}; put the reason in limits or Markdown, not the label`);
|
||||
captures.add(row.capture);
|
||||
const captured = readQaCapture(root, row.capture);
|
||||
argv.push(JSON.stringify(captured.receipt.argv));
|
||||
return { command: row.command, contract: row.contract, expected: row.expected, classification: row.classification, observed: captured.observed };
|
||||
});
|
||||
const snapshotOf = (observed: unknown) => object(observed) && typeof observed.snapshot === 'string' ? observed.snapshot : undefined;
|
||||
const latestCapture = latestCompleteCapture(root);
|
||||
const currentSnapshot = latestCapture ? snapshotOf(readQaCapture(root, latestCapture).observed) : undefined;
|
||||
const superseded = currentSnapshot === undefined ? [] : annotations.evidence.filter((row: any, index: number) => {
|
||||
const snapshot = snapshotOf(evidence[index].observed);
|
||||
return snapshot !== undefined && snapshot !== currentSnapshot && row.classification !== 'superseded';
|
||||
}).map((row: any) => row.capture);
|
||||
if (superseded.length) throw new QaEvidenceError(`Superseded evidence: capture ${superseded.join(', ')} observed an older input snapshot than the latest capture ${latestCapture}; rerun the affected probe on current inputs, or classify the row "superseded" and keep its contract open`);
|
||||
const missing = completeCaptures(root).filter(capture => !captures.has(capture)
|
||||
&& !annotations.limits.some((limit: string) => new RegExp(`\\b${capture}\\b`).test(limit)));
|
||||
if (missing.length) throw new QaEvidenceError(`Invalid report annotations: add an evidence row for capture ${missing.join(', ')} (every complete capture needs one, or name it in limits with why it is withheld)`);
|
||||
const learning = annotations.learning.map((name: unknown) => {
|
||||
if (typeof name !== 'string') throw new QaEvidenceError('Invalid checkpoint reference');
|
||||
const note = JSON.parse(decode(read(root, `exploration-${id(name)}.json`)));
|
||||
if (!exact(note, ['observationCommand', 'observed', 'hypothesis', 'nextCommand'])) throw new QaEvidenceError('Invalid referenced checkpoint');
|
||||
return { observationCommand: note.observationCommand, hypothesis: note.hypothesis, nextCommand: note.nextCommand };
|
||||
if (!exact(note, ['observationCommand', 'observed', 'hypothesis', 'nextCommand']) && !exact(note, MERGED_NOTE)) throw new QaEvidenceError('Invalid referenced checkpoint');
|
||||
if (!learned(note)) {
|
||||
throw new QaEvidenceError(`Invalid learning: checkpoint ${name} replays the same probe; name checkpoints whose next probe differs, or omit learning and Q selects them`);
|
||||
}
|
||||
const { observed, ...row } = note;
|
||||
return row;
|
||||
});
|
||||
const sha256 = publish(root, 'evidence.json', { ...annotations, evidence, learning });
|
||||
return { action: 'materialize', status: 'complete', sha256, annotationsSha256: hash(bytes), exitCode: 0 };
|
||||
const classes = annotations.evidence.map((row: any) => String(row.classification).toLowerCase());
|
||||
const open = [
|
||||
...annotations.evidence.filter((row: any, index: number) => String(row.classification).toLowerCase() === 'superseded'
|
||||
&& !annotations.evidence.some((other: any, rerun: number) => String(other.classification).toLowerCase() !== 'superseded' && argv[rerun] === argv[index]
|
||||
&& (currentSnapshot === undefined || snapshotOf(evidence[rerun].observed) === currentSnapshot))).map((row: any) => `capture ${row.capture} superseded`),
|
||||
...completeCaptures(root).filter(capture => !captures.has(capture)).map(capture => `capture ${capture} withheld`),
|
||||
...(requiredRemaining(root).requiredRemaining ?? []).map(command => `required probe not run: ${command}`),
|
||||
...(annotations.evidence.length ? [] : ['no evidence rows']),
|
||||
];
|
||||
const verdict = {
|
||||
status: classes.some((value: string) => /fail|defect/.test(value)) ? 'fail'
|
||||
: classes.some((value: string) => /block/.test(value)) ? 'blocked'
|
||||
: open.length || classes.some((value: string) => !['pass', 'superseded'].includes(value)) ? 'inconclusive' : 'pass',
|
||||
open,
|
||||
};
|
||||
if (fs.existsSync(owned(root, 'evidence.json'))) throw new QaEvidenceError('evidence.json is already published for this report root; materialize runs once, so report its printed verdict');
|
||||
const sha256 = publish(root, 'evidence.json', { ...annotations, evidence, learning, verdict });
|
||||
return { action: 'materialize', status: 'complete', sha256, annotationsSha256: hash(bytes), exitCode: 0, verdict,
|
||||
reportLinks: notes.map(note => `[checkpoint ${note.name.slice(12, 15)}](${note.name})`),
|
||||
next: `Include every reportLinks entry in the Markdown report, and report the overall status as ${verdict.status}${verdict.open.length ? ` (open: ${verdict.open.join('; ')})` : ''}; rerun what is open first if a pass is required.` };
|
||||
}
|
||||
|
||||
const QA_EVIDENCE_USAGE = 'capture ROOT ID [--public] --deadline FILE|--timeout-ms MS [--after PREVIOUS_CAPTURE --hypothesis TEXT] -- COMMAND ARGS (--after publishes checkpoint ID linking PREVIOUS_CAPTURE to this probe; required after the first complete capture unless a checkpoint was published) | checkpoint ROOT ID CAPTURE OBSERVATION_COMMAND HYPOTHESIS NEXT_COMMAND | checkpoint ROOT ID INTENT_FILE | materialize ROOT ANNOTATIONS (annotations: {evidence: [{capture, command, contract, expected, classification: pass|superseded|product-defect|fail|setup-blocked|blocked|inconclusive}], limits: [..]}; revision, runtime, cwd and learning are filled in)';
|
||||
|
||||
export async function qaEvidenceMain(args: string[]): Promise<number> {
|
||||
return withQaReceiptOutput(false, 'qa-evidence-receipt', value => value.event === 'observation'
|
||||
? JSON.stringify(value.observed) + '\n' : value.event === 'diagnostic' ? String(value.stderr)
|
||||
: '\nQA_EVIDENCE ' + JSON.stringify({ producer: 'gstack-qa-evidence', version: 1, ...value }) + '\n', async emit => {
|
||||
try {
|
||||
const [action, reportRoot, ...rest] = args;
|
||||
if (action === '--help' && args.length === 1) {
|
||||
emit('stdout', { action: 'help', status: 'complete', usage: QA_EVIDENCE_USAGE, exitCode: 0 });
|
||||
return 0;
|
||||
}
|
||||
const root = qaEvidenceRoot(reportRoot);
|
||||
let receipt: Record<string, any>;
|
||||
const publicOutput = action === 'capture' && rest[1] === '--public';
|
||||
if (publicOutput) rest.splice(1, 1);
|
||||
const after = action === 'capture' && rest[3] === '--after' && rest[5] === '--hypothesis' ? { capture: rest[4], hypothesis: rest[6] } : undefined;
|
||||
if (after) rest.splice(3, 4);
|
||||
if (action === 'capture' && rest.length >= 5 && rest[3] === '--') {
|
||||
receipt = await capture(root, rest[0], publicOutput, rest[1], rest[2], rest[4], rest.slice(5));
|
||||
receipt = await capture(root, rest[0], publicOutput, rest[1], rest[2], rest[4], rest.slice(5), after);
|
||||
if (publicOutput && receipt.status === 'complete') {
|
||||
const captured = readQaCapture(root, rest[0], receipt.sha256);
|
||||
emit('stdout', { event: 'observation', observed: captured.observed });
|
||||
@@ -243,7 +369,7 @@ export async function qaEvidenceMain(args: string[]): Promise<number> {
|
||||
} else if (action === 'checkpoint' && rest.length === 2) receipt = checkpoint(root, rest[0], rest[1]);
|
||||
else if (action === 'checkpoint' && rest.length === 5) receipt = checkpoint(root, rest[0], { capture: rest[1], observationCommand: rest[2], hypothesis: rest[3], nextCommand: rest[4] });
|
||||
else if (action === 'materialize' && rest.length === 1) receipt = materialize(root, rest[0]);
|
||||
else throw new QaEvidenceError('Usage: capture ROOT ID [--public] --deadline FILE|--timeout-ms MS -- COMMAND ARGS | checkpoint ROOT ID CAPTURE OBSERVATION_COMMAND HYPOTHESIS NEXT_COMMAND | checkpoint ROOT ID INTENT_FILE | materialize ROOT ANNOTATIONS');
|
||||
else throw new QaEvidenceError(`Usage: ${QA_EVIDENCE_USAGE}`);
|
||||
emit('stdout', receipt);
|
||||
return receipt.status === 'complete' ? receipt.exitCode : receipt.status === 'incomplete' ? receipt.exitCode || 2 : 2;
|
||||
} catch (error) {
|
||||
|
||||
Reference in new issue
Block a user