ctxtop 4.1.0 → 4.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.antigravity-plugin/plugin.json +1 -1
- package/.claude-plugin/plugin.json +21 -18
- package/CHECKSUMS.sha256 +51 -44
- package/LICENSE +27 -27
- package/README.md +90 -56
- package/SECURITY.md +7 -1
- package/assets/hero-demo.svg +1 -1
- package/bin/ctxtop.mjs +267 -54
- package/config/modelCatalog.json +847 -273
- package/hooks/handlers/hookIpcClient.mjs +8 -4
- package/hooks/handlers/post-model-switch.mjs +39 -2
- package/hooks/handlers/post-tool-use-core.mjs +394 -0
- package/hooks/handlers/post-tool-use.mjs +54 -321
- package/hooks/handlers/pre-model-switch.mjs +115 -5
- package/hooks/handlers/pre-tool-use-core.mjs +680 -0
- package/hooks/handlers/pre-tool-use.mjs +61 -571
- package/hooks/handlers/stop-gate.mjs +20 -8
- package/hooks/handlers/user-prompt-submit.mjs +111 -2
- package/hooks/post_tool_use.mjs +17 -4
- package/hooks/pre_tool_use.mjs +84 -54
- package/hooks.json +56 -56
- package/package.json +4 -3
- package/packages/antigravity/package.json +10 -3
- package/packages/antigravity/src/adapter.mjs +109 -0
- package/packages/antigravity/src/index.mjs +7 -1
- package/packages/claude-code/package.json +10 -3
- package/packages/claude-code/src/adapter.mjs +194 -0
- package/packages/claude-code/src/index.mjs +8 -2
- package/packages/claude-code/src/register.js +726 -0
- package/packages/core/package.json +6 -2
- package/packages/core/src/catalog/detectorCatalog.mjs +558 -0
- package/packages/core/src/catalog/index.mjs +1 -0
- package/packages/core/src/catalog/safeguardGuide.mjs +412 -0
- package/packages/core/src/coach/cacheGravityRouter.mjs +1547 -0
- package/packages/core/src/coach/effortShiftDetector.mjs +316 -0
- package/packages/core/src/coach/microSkills.mjs +1 -0
- package/packages/core/src/coach/promptHygieneGovernor.mjs +1 -0
- package/packages/core/src/coach/repoCalibration.mjs +254 -0
- package/packages/core/src/coach/resumeToolsetAuditor.mjs +321 -0
- package/packages/core/src/coach/savedQuotaLedger.mjs +527 -0
- package/packages/core/src/coach/smartCompactAdvisor.mjs +339 -0
- package/packages/core/src/coach/stateBatonHydrator.mjs +810 -0
- package/packages/core/src/coach/ttlWatchdog.mjs +464 -0
- package/packages/core/src/config/anthropicModelCatalog.json +607 -0
- package/packages/core/src/config/catalogLoader.mjs +1271 -0
- package/packages/core/src/config/horizonConfig.mjs +1055 -0
- package/packages/core/src/control/callTree.mjs +338 -0
- package/packages/core/src/control/coachLatches.mjs +126 -0
- package/packages/core/src/control/controlStateMachine.mjs +205 -0
- package/packages/core/src/control/index.mjs +49 -0
- package/packages/core/src/control/swarmWorktree.mjs +109 -0
- package/packages/core/src/control/toolSuspension.mjs +197 -0
- package/packages/core/src/diagnostics/drift_guard.mjs +382 -0
- package/packages/core/src/diagnostics/index.mjs +9 -0
- package/packages/core/src/diagnostics/micro_skills.mjs +471 -0
- package/packages/core/src/diagnostics/prompt_hygiene_governor.mjs +888 -0
- package/packages/core/src/economics/billingBlocks.mjs +251 -0
- package/packages/core/src/economics/cacheGravity.mjs +371 -0
- package/packages/core/src/economics/cliffBreaker.mjs +256 -0
- package/packages/core/src/economics/driftLatency.mjs +433 -0
- package/packages/core/src/economics/dualTtl.mjs +198 -0
- package/packages/core/src/economics/index.mjs +47 -0
- package/packages/core/src/economics/retentionRadar.mjs +348 -0
- package/packages/core/src/economics/shadowAutoModel.mjs +228 -0
- package/packages/core/src/guards/bucketBAlphaBeta.mjs +860 -0
- package/packages/core/src/guards/bucketBGamma.mjs +442 -0
- package/packages/core/src/guards/bucketBHeuristics.mjs +37 -0
- package/packages/core/src/guards/circuitBreakers.mjs +1202 -0
- package/packages/core/src/guards/det01_03_shell_secret.mjs +1184 -0
- package/packages/core/src/guards/det04_12_thrash_rot.mjs +572 -0
- package/packages/core/src/guards/det13_20_mcp_scope.mjs +913 -0
- package/packages/core/src/guards/docIndexCore.mjs +446 -0
- package/packages/core/src/guards/losslessPostSanitizer.mjs +317 -0
- package/packages/core/src/guards/slopsquatGate.mjs +551 -0
- package/packages/core/src/index.js +27 -0
- package/packages/core/src/index.mjs +25 -1
- package/packages/core/src/telemetry/otelExporter.mjs +187 -0
- package/packages/core/src/telemetry/scopeLockRegistry.mjs +522 -0
- package/packages/core/src/telemetry/sprintMetrics.mjs +327 -0
- package/packages/core/src/telemetry/swarmTopology.mjs +152 -0
- package/packages/core/src/telemetry/telemetryBridge.mjs +1232 -0
- package/packages/core/src/ui/brailleSparkline.mjs +142 -0
- package/packages/core/src/ui/costRadarSvg.mjs +276 -0
- package/packages/core/src/ui/horizonGrid.mjs +475 -0
- package/packages/core/src/ui/horizonPanelHtml.mjs +1502 -0
- package/packages/core/src/ui/modViews.mjs +1558 -0
- package/packages/core/src/ui/pdfReceiptGenerator.mjs +329 -0
- package/packages/core/src/ustf/index.mjs +7 -0
- package/packages/core/src/ustf/normalizer.mjs +512 -0
- package/packages/core/src/ustf/schema.mjs +186 -0
- package/packages/core/src/utils/runtime.mjs +962 -0
- package/packages/engine-node/package.json +6 -2
- package/packages/engine-node/src/db/index.mjs +58 -0
- package/packages/engine-node/src/db/jsonl_fallback.mjs +184 -0
- package/packages/engine-node/src/db/probe.mjs +342 -0
- package/packages/engine-node/src/db/sqlite_wrapper.mjs +122 -0
- package/packages/engine-node/src/index.mjs +11 -1
- package/packages/engine-node/src/ipc/client.mjs +447 -0
- package/packages/engine-node/src/ipc/fast_client.mjs +191 -0
- package/packages/engine-node/src/ipc/index.mjs +24 -0
- package/packages/engine-node/src/ipc/protocol.mjs +177 -0
- package/packages/engine-node/src/ipc/server.mjs +445 -0
- package/packages/engine-node/src/multi_host_mcp.mjs +168 -0
- package/packages/engine-node/src/offloader/index.mjs +6 -0
- package/packages/engine-node/src/offloader/tool_offloader.mjs +241 -0
- package/packages/engine-node/src/rsl/index.mjs +3 -0
- package/packages/engine-node/src/sensors/blindspots.mjs +376 -0
- package/packages/engine-node/src/sensors/git_merge_tree.mjs +223 -0
- package/packages/engine-node/src/sensors/index.mjs +7 -0
- package/packages/engine-node/src/vendor/config/default_flightcheck.json +133 -0
- package/packages/engine-node/src/vendor/config/modelCatalog.json +1359 -0
- package/packages/engine-node/src/vendor/config/platformQuirks.json +19 -0
- package/packages/engine-node/src/vendor/core/atomic_io.mjs +124 -0
- package/packages/engine-node/src/vendor/core/constants.mjs +2150 -0
- package/packages/engine-node/src/vendor/core/doc_indexer.mjs +338 -0
- package/packages/engine-node/src/vendor/core/rsl_clash_auditor.mjs +423 -0
- package/packages/engine-node/src/vendor/core/rsl_distiller.mjs +394 -0
- package/packages/engine-node/src/vendor/core/rsl_ledger.mjs +1029 -0
- package/packages/engine-node/src/vendor/core/rsl_triage.mjs +1051 -0
- package/packages/engine-node/src/vendor/core/shield_manager.mjs +1417 -0
- package/packages/engine-node/src/vendor/core-isomorphic/catalog/detectorCatalog.mjs +558 -0
- package/packages/engine-node/src/vendor/core-isomorphic/catalog/index.mjs +1 -0
- package/packages/engine-node/src/vendor/core-isomorphic/catalog/safeguardGuide.mjs +412 -0
- package/packages/engine-node/src/vendor/core-isomorphic/coach/cacheGravityRouter.mjs +1547 -0
- package/packages/engine-node/src/vendor/core-isomorphic/coach/effortShiftDetector.mjs +316 -0
- package/packages/engine-node/src/vendor/core-isomorphic/coach/microSkills.mjs +1 -0
- package/packages/engine-node/src/vendor/core-isomorphic/coach/promptHygieneGovernor.mjs +1 -0
- package/packages/engine-node/src/vendor/core-isomorphic/coach/repoCalibration.mjs +254 -0
- package/packages/engine-node/src/vendor/core-isomorphic/coach/resumeToolsetAuditor.mjs +321 -0
- package/packages/engine-node/src/vendor/core-isomorphic/coach/savedQuotaLedger.mjs +527 -0
- package/packages/engine-node/src/vendor/core-isomorphic/coach/smartCompactAdvisor.mjs +339 -0
- package/packages/engine-node/src/vendor/core-isomorphic/coach/stateBatonHydrator.mjs +810 -0
- package/packages/engine-node/src/vendor/core-isomorphic/coach/ttlWatchdog.mjs +464 -0
- package/packages/engine-node/src/vendor/core-isomorphic/config/anthropicModelCatalog.json +607 -0
- package/packages/engine-node/src/vendor/core-isomorphic/config/catalogLoader.mjs +1271 -0
- package/packages/engine-node/src/vendor/core-isomorphic/config/horizonConfig.mjs +1055 -0
- package/packages/engine-node/src/vendor/core-isomorphic/control/callTree.mjs +338 -0
- package/packages/engine-node/src/vendor/core-isomorphic/control/coachLatches.mjs +126 -0
- package/packages/engine-node/src/vendor/core-isomorphic/control/controlStateMachine.mjs +205 -0
- package/packages/engine-node/src/vendor/core-isomorphic/control/index.mjs +49 -0
- package/packages/engine-node/src/vendor/core-isomorphic/control/swarmWorktree.mjs +109 -0
- package/packages/engine-node/src/vendor/core-isomorphic/control/toolSuspension.mjs +197 -0
- package/packages/engine-node/src/vendor/core-isomorphic/diagnostics/drift_guard.mjs +382 -0
- package/packages/engine-node/src/vendor/core-isomorphic/diagnostics/index.mjs +9 -0
- package/packages/engine-node/src/vendor/core-isomorphic/diagnostics/micro_skills.mjs +471 -0
- package/packages/engine-node/src/vendor/core-isomorphic/diagnostics/prompt_hygiene_governor.mjs +888 -0
- package/packages/engine-node/src/vendor/core-isomorphic/economics/billingBlocks.mjs +251 -0
- package/packages/engine-node/src/vendor/core-isomorphic/economics/cacheGravity.mjs +371 -0
- package/packages/engine-node/src/vendor/core-isomorphic/economics/cliffBreaker.mjs +256 -0
- package/packages/engine-node/src/vendor/core-isomorphic/economics/driftLatency.mjs +433 -0
- package/packages/engine-node/src/vendor/core-isomorphic/economics/dualTtl.mjs +198 -0
- package/packages/engine-node/src/vendor/core-isomorphic/economics/index.mjs +47 -0
- package/packages/engine-node/src/vendor/core-isomorphic/economics/retentionRadar.mjs +348 -0
- package/packages/engine-node/src/vendor/core-isomorphic/economics/shadowAutoModel.mjs +228 -0
- package/packages/engine-node/src/vendor/core-isomorphic/guards/bucketBAlphaBeta.mjs +860 -0
- package/packages/engine-node/src/vendor/core-isomorphic/guards/bucketBGamma.mjs +442 -0
- package/packages/engine-node/src/vendor/core-isomorphic/guards/bucketBHeuristics.mjs +37 -0
- package/packages/engine-node/src/vendor/core-isomorphic/guards/circuitBreakers.mjs +1202 -0
- package/packages/engine-node/src/vendor/core-isomorphic/guards/det01_03_shell_secret.mjs +1184 -0
- package/packages/engine-node/src/vendor/core-isomorphic/guards/det04_12_thrash_rot.mjs +572 -0
- package/packages/engine-node/src/vendor/core-isomorphic/guards/det13_20_mcp_scope.mjs +913 -0
- package/packages/engine-node/src/vendor/core-isomorphic/guards/docIndexCore.mjs +446 -0
- package/packages/engine-node/src/vendor/core-isomorphic/guards/losslessPostSanitizer.mjs +317 -0
- package/packages/engine-node/src/vendor/core-isomorphic/guards/slopsquatGate.mjs +551 -0
- package/packages/engine-node/src/vendor/core-isomorphic/index.js +27 -0
- package/packages/engine-node/src/vendor/core-isomorphic/telemetry/otelExporter.mjs +187 -0
- package/packages/engine-node/src/vendor/core-isomorphic/telemetry/scopeLockRegistry.mjs +522 -0
- package/packages/engine-node/src/vendor/core-isomorphic/telemetry/sprintMetrics.mjs +327 -0
- package/packages/engine-node/src/vendor/core-isomorphic/telemetry/swarmTopology.mjs +152 -0
- package/packages/engine-node/src/vendor/core-isomorphic/telemetry/telemetryBridge.mjs +1232 -0
- package/packages/engine-node/src/vendor/core-isomorphic/ui/brailleSparkline.mjs +142 -0
- package/packages/engine-node/src/vendor/core-isomorphic/ui/costRadarSvg.mjs +276 -0
- package/packages/engine-node/src/vendor/core-isomorphic/ui/horizonGrid.mjs +475 -0
- package/packages/engine-node/src/vendor/core-isomorphic/ui/horizonPanelHtml.mjs +1502 -0
- package/packages/engine-node/src/vendor/core-isomorphic/ui/modViews.mjs +1558 -0
- package/packages/engine-node/src/vendor/core-isomorphic/ui/pdfReceiptGenerator.mjs +329 -0
- package/packages/engine-node/src/vendor/core-isomorphic/ustf/index.mjs +7 -0
- package/packages/engine-node/src/vendor/core-isomorphic/ustf/normalizer.mjs +512 -0
- package/packages/engine-node/src/vendor/core-isomorphic/ustf/schema.mjs +186 -0
- package/packages/engine-node/src/vendor/core-isomorphic/utils/runtime.mjs +962 -0
- package/packages/mcp/package.json +7 -3
- package/packages/mcp/src/server.mjs +115 -14
- package/packages/mcp/src/vendor/config/default_flightcheck.json +133 -0
- package/packages/mcp/src/vendor/config/modelCatalog.json +1359 -0
- package/packages/mcp/src/vendor/config/platformQuirks.json +19 -0
- package/packages/mcp/src/vendor/core/atomic_io.mjs +124 -0
- package/packages/mcp/src/vendor/core/coach/saved_quota_ledger.mjs +615 -0
- package/packages/mcp/src/vendor/core/constants.mjs +2150 -0
- package/packages/mcp/src/vendor/core/doc_indexer.mjs +338 -0
- package/packages/mcp/src/vendor/core/fts5_output_compressor.mjs +596 -0
- package/packages/mcp/src/vendor/core/hook_diagnostics.mjs +158 -0
- package/packages/mcp/src/vendor/core/paths.mjs +311 -0
- package/packages/mcp/src/vendor/core/session_auditor.mjs +1109 -0
- package/packages/mcp/src/vendor/core/sqlite_client.mjs +289 -0
- package/packages/mcp/src/vendor/core/telemetry/scope_lock_registry.mjs +578 -0
- package/packages/mcp/src/vendor/core-isomorphic/catalog/detectorCatalog.mjs +558 -0
- package/packages/mcp/src/vendor/core-isomorphic/catalog/index.mjs +1 -0
- package/packages/mcp/src/vendor/core-isomorphic/catalog/safeguardGuide.mjs +412 -0
- package/packages/mcp/src/vendor/core-isomorphic/coach/cacheGravityRouter.mjs +1547 -0
- package/packages/mcp/src/vendor/core-isomorphic/coach/effortShiftDetector.mjs +316 -0
- package/packages/mcp/src/vendor/core-isomorphic/coach/microSkills.mjs +1 -0
- package/packages/mcp/src/vendor/core-isomorphic/coach/promptHygieneGovernor.mjs +1 -0
- package/packages/mcp/src/vendor/core-isomorphic/coach/repoCalibration.mjs +254 -0
- package/packages/mcp/src/vendor/core-isomorphic/coach/resumeToolsetAuditor.mjs +321 -0
- package/packages/mcp/src/vendor/core-isomorphic/coach/savedQuotaLedger.mjs +527 -0
- package/packages/mcp/src/vendor/core-isomorphic/coach/smartCompactAdvisor.mjs +339 -0
- package/packages/mcp/src/vendor/core-isomorphic/coach/stateBatonHydrator.mjs +810 -0
- package/packages/mcp/src/vendor/core-isomorphic/coach/ttlWatchdog.mjs +464 -0
- package/packages/mcp/src/vendor/core-isomorphic/config/anthropicModelCatalog.json +607 -0
- package/packages/mcp/src/vendor/core-isomorphic/config/catalogLoader.mjs +1271 -0
- package/packages/mcp/src/vendor/core-isomorphic/config/horizonConfig.mjs +1055 -0
- package/packages/mcp/src/vendor/core-isomorphic/control/callTree.mjs +338 -0
- package/packages/mcp/src/vendor/core-isomorphic/control/coachLatches.mjs +126 -0
- package/packages/mcp/src/vendor/core-isomorphic/control/controlStateMachine.mjs +205 -0
- package/packages/mcp/src/vendor/core-isomorphic/control/index.mjs +49 -0
- package/packages/mcp/src/vendor/core-isomorphic/control/swarmWorktree.mjs +109 -0
- package/packages/mcp/src/vendor/core-isomorphic/control/toolSuspension.mjs +197 -0
- package/packages/mcp/src/vendor/core-isomorphic/diagnostics/drift_guard.mjs +382 -0
- package/packages/mcp/src/vendor/core-isomorphic/diagnostics/index.mjs +9 -0
- package/packages/mcp/src/vendor/core-isomorphic/diagnostics/micro_skills.mjs +471 -0
- package/packages/mcp/src/vendor/core-isomorphic/diagnostics/prompt_hygiene_governor.mjs +888 -0
- package/packages/mcp/src/vendor/core-isomorphic/economics/billingBlocks.mjs +251 -0
- package/packages/mcp/src/vendor/core-isomorphic/economics/cacheGravity.mjs +371 -0
- package/packages/mcp/src/vendor/core-isomorphic/economics/cliffBreaker.mjs +256 -0
- package/packages/mcp/src/vendor/core-isomorphic/economics/driftLatency.mjs +433 -0
- package/packages/mcp/src/vendor/core-isomorphic/economics/dualTtl.mjs +198 -0
- package/packages/mcp/src/vendor/core-isomorphic/economics/index.mjs +47 -0
- package/packages/mcp/src/vendor/core-isomorphic/economics/retentionRadar.mjs +348 -0
- package/packages/mcp/src/vendor/core-isomorphic/economics/shadowAutoModel.mjs +228 -0
- package/packages/mcp/src/vendor/core-isomorphic/guards/bucketBAlphaBeta.mjs +860 -0
- package/packages/mcp/src/vendor/core-isomorphic/guards/bucketBGamma.mjs +442 -0
- package/packages/mcp/src/vendor/core-isomorphic/guards/bucketBHeuristics.mjs +37 -0
- package/packages/mcp/src/vendor/core-isomorphic/guards/circuitBreakers.mjs +1202 -0
- package/packages/mcp/src/vendor/core-isomorphic/guards/det01_03_shell_secret.mjs +1184 -0
- package/packages/mcp/src/vendor/core-isomorphic/guards/det04_12_thrash_rot.mjs +572 -0
- package/packages/mcp/src/vendor/core-isomorphic/guards/det13_20_mcp_scope.mjs +913 -0
- package/packages/mcp/src/vendor/core-isomorphic/guards/docIndexCore.mjs +446 -0
- package/packages/mcp/src/vendor/core-isomorphic/guards/losslessPostSanitizer.mjs +317 -0
- package/packages/mcp/src/vendor/core-isomorphic/guards/slopsquatGate.mjs +551 -0
- package/packages/mcp/src/vendor/core-isomorphic/index.js +27 -0
- package/packages/mcp/src/vendor/core-isomorphic/telemetry/otelExporter.mjs +187 -0
- package/packages/mcp/src/vendor/core-isomorphic/telemetry/scopeLockRegistry.mjs +522 -0
- package/packages/mcp/src/vendor/core-isomorphic/telemetry/sprintMetrics.mjs +327 -0
- package/packages/mcp/src/vendor/core-isomorphic/telemetry/swarmTopology.mjs +152 -0
- package/packages/mcp/src/vendor/core-isomorphic/telemetry/telemetryBridge.mjs +1232 -0
- package/packages/mcp/src/vendor/core-isomorphic/ui/brailleSparkline.mjs +142 -0
- package/packages/mcp/src/vendor/core-isomorphic/ui/costRadarSvg.mjs +276 -0
- package/packages/mcp/src/vendor/core-isomorphic/ui/horizonGrid.mjs +475 -0
- package/packages/mcp/src/vendor/core-isomorphic/ui/horizonPanelHtml.mjs +1502 -0
- package/packages/mcp/src/vendor/core-isomorphic/ui/modViews.mjs +1558 -0
- package/packages/mcp/src/vendor/core-isomorphic/ui/pdfReceiptGenerator.mjs +329 -0
- package/packages/mcp/src/vendor/core-isomorphic/ustf/index.mjs +7 -0
- package/packages/mcp/src/vendor/core-isomorphic/ustf/normalizer.mjs +512 -0
- package/packages/mcp/src/vendor/core-isomorphic/ustf/schema.mjs +186 -0
- package/packages/mcp/src/vendor/core-isomorphic/utils/runtime.mjs +962 -0
- package/packages/mcp/src/vendor/engine-node/db/probe.mjs +342 -0
- package/packages/mcp/src/vendor/pro/pro_license.mjs +547 -0
- package/packages/pro/package.json +23 -16
- package/packages/pro/src/index.mjs +9 -3
- package/packages/pro/src/pro_license.mjs +547 -0
- package/packages/pro/src/team_finops_pack.mjs +241 -241
- package/packages/pro/workers/polar_webhook_worker.mjs +219 -215
- package/plugin.json +1 -1
- package/sidecars/horizon-daemon/server.mjs +482 -44
- package/src/cli/doctor.mjs +11 -2
- package/src/cli/horizon_hud.mjs +31 -15
- package/src/cli/init.mjs +46 -13
- package/src/config/modelCatalog.json +886 -231
- package/src/core/classifier.mjs +25 -11
- package/src/core/coach/circuit_breakers.mjs +84 -6
- package/src/core/coach/task_quota_router.mjs +8 -0
- package/src/core/code_fold_lite.mjs +169 -0
- package/src/core/config.mjs +44 -0
- package/src/core/constants.mjs +175 -12
- package/src/core/detectors_det01_to_16.mjs +44 -3
- package/src/core/fts5_output_compressor.mjs +272 -35
- package/src/core/horizon_config.mjs +42 -0
- package/src/core/parser.mjs +86 -11
- package/src/core/pro_license.mjs +174 -12
- package/src/core/pruner.mjs +7 -146
- package/src/core/quota.mjs +14 -2
- package/src/core/rsl_distiller.mjs +31 -9
- package/src/core/rsl_ledger.mjs +66 -6
- package/src/core/rsl_triage.mjs +159 -17
- package/src/core/session_auditor.mjs +476 -25
- package/src/core/shadow_auto_ledger.mjs +205 -0
- package/src/core/sqlite_client.mjs +10 -4
- package/src/core/subagents.mjs +18 -2
- package/src/core/task_quota_router.mjs +61 -4
- package/src/core-isomorphic/catalog/safeguardGuide.mjs +412 -0
- package/src/core-isomorphic/coach/effortShiftDetector.mjs +316 -0
- package/src/core-isomorphic/coach/smartCompactAdvisor.mjs +54 -20
- package/src/core-isomorphic/config/anthropicModelCatalog.json +1 -0
- package/src/core-isomorphic/config/catalogLoader.mjs +6 -3
- package/src/core-isomorphic/config/horizonConfig.mjs +1 -1
- package/src/core-isomorphic/economics/cacheGravity.mjs +3 -1
- package/src/core-isomorphic/economics/shadowAutoModel.mjs +228 -0
- package/src/core-isomorphic/guards/bucketBGamma.mjs +1 -1
- package/src/core-isomorphic/telemetry/telemetryBridge.mjs +77 -1
- package/src/core-isomorphic/ui/horizonPanelHtml.mjs +395 -130
- package/src/core-isomorphic/ui/modViews.mjs +62 -8
- package/src/diagnostics/claudeHookCapabilities.mjs +32 -2
- package/src/diagnostics/doctorEngine.mjs +28 -2
- package/src/engine-node/ipc/fast_client.mjs +191 -0
- package/src/engine-node/ipc/index.mjs +2 -0
- package/src/engine-node/multi_host_mcp.mjs +168 -0
- package/src/hooks/antigravity_rsl_hook.mjs +24 -2
- package/src/hooks/claude_rsl_hook.mjs +22 -2
- package/src/hooks/daemon_hook_bridge.mjs +256 -0
- package/src/hooks/fast_hook_ipc.mjs +295 -0
- package/src/hooks/post_invocation.mjs +37 -597
- package/src/hooks/post_invocation_core.mjs +623 -0
- package/src/hooks/pre_invocation.mjs +103 -1668
- package/src/hooks/pre_invocation_core.mjs +1830 -0
- package/src/utils/sessionCalibration.mjs +52 -5
- package/src/webview/advanced_view.html +34 -18
- package/src/webview/horizon_panel.html +3233 -675
- package/src/webview/simple_view.html +1 -1
|
@@ -0,0 +1,1184 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file src/core-isomorphic/guards/det01_03_shell_secret.mjs
|
|
3
|
+
* Shell & Git Safety, Secret Boundary, and Protected File Ask-Gate (`DET-01`..`DET-03`).
|
|
4
|
+
* Pure-V8 isomorphic ESM module (zero `node:*` imports).
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { path } from '../utils/runtime.mjs';
|
|
8
|
+
|
|
9
|
+
export const SHELL_TOOL_REGEX = /^(Bash|PowerShell|pwsh|cmd|sh)$/i;
|
|
10
|
+
export const EDIT_WRITE_TOOL_REGEX = /^(Edit|Write|MultiEdit|NotebookEdit)$/i;
|
|
11
|
+
export const SEARCH_READ_TOOL_REGEX = /^(Read|Grep|Glob|read_file|grep_search)$/i;
|
|
12
|
+
|
|
13
|
+
const GIT_GLOBAL_OPTS_WITH_ARG = new Set([
|
|
14
|
+
'-c',
|
|
15
|
+
'-C',
|
|
16
|
+
'--git-dir',
|
|
17
|
+
'--work-tree',
|
|
18
|
+
'--namespace',
|
|
19
|
+
'--super-prefix',
|
|
20
|
+
'--config-env'
|
|
21
|
+
]);
|
|
22
|
+
|
|
23
|
+
const GIT_COMMIT_OPTS_WITH_ARG = new Set([
|
|
24
|
+
'-m',
|
|
25
|
+
'--message',
|
|
26
|
+
'-F',
|
|
27
|
+
'--file',
|
|
28
|
+
'-c',
|
|
29
|
+
'--reedit-message',
|
|
30
|
+
'-C',
|
|
31
|
+
'--reuse-message',
|
|
32
|
+
'--author',
|
|
33
|
+
'--date',
|
|
34
|
+
'--cleanup',
|
|
35
|
+
'-t',
|
|
36
|
+
'--template'
|
|
37
|
+
]);
|
|
38
|
+
|
|
39
|
+
const GIT_CONFIG_OPTS_WITH_ARG = new Set([
|
|
40
|
+
'-f',
|
|
41
|
+
'--file',
|
|
42
|
+
'--blob',
|
|
43
|
+
'--type',
|
|
44
|
+
'-t',
|
|
45
|
+
'--default',
|
|
46
|
+
'--comment',
|
|
47
|
+
'--fixed-value'
|
|
48
|
+
]);
|
|
49
|
+
|
|
50
|
+
const VALID_GIT_COMMIT_UNTRACKED_MODES = new Set(['', 'no', 'normal', 'all']);
|
|
51
|
+
|
|
52
|
+
const SHORT_COMMIT_ARG_CHARS = new Set(['m', 'F', 'c', 'C', 't']);
|
|
53
|
+
|
|
54
|
+
const SAFE_ENV_ALLOWLIST_REGEX =
|
|
55
|
+
/(?:^|[\\/])\.env\.(?:example|sample|template|defaults|schema)$/i;
|
|
56
|
+
|
|
57
|
+
const SECRET_FILE_PATTERNS = [
|
|
58
|
+
/(?:^|[\\/])\.env(?:\.[a-zA-Z0-9._-]+)?$/i,
|
|
59
|
+
/\.(?:pem|key|p12|pfx)$/i,
|
|
60
|
+
/(?:^|[\\/])id_(?:rsa|ed25519|ecdsa|dsa)(?:\.pub)?$/i,
|
|
61
|
+
/(?:^|[\\/])\.(?:ssh|aws)[\\/]/i,
|
|
62
|
+
/(?:^|[\\/])\.(?:npmrc|netrc|pypirc)$/i
|
|
63
|
+
];
|
|
64
|
+
|
|
65
|
+
const PROTECTED_EDIT_PATTERNS = [
|
|
66
|
+
/(?:^|[\\/])migrations[\\/]/i,
|
|
67
|
+
/(?:^|[\\/])prisma[\\/]migrations[\\/]/i,
|
|
68
|
+
/(?:^|[\\/])(?:package-lock\.json|pnpm-lock\.yaml|yarn\.lock|Cargo\.lock|poetry\.lock|go\.sum)$/i,
|
|
69
|
+
/(?:^|[\\/])\.github[\\/]workflows[\\/]/i
|
|
70
|
+
];
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Detects whether a command or tool invocation uses PowerShell lexical conventions
|
|
74
|
+
* (leading `&` call operator, `$env:`, or PowerShell backtick flag escapes `` `-- `` / `` `-x ``).
|
|
75
|
+
*
|
|
76
|
+
* @param {string} [toolName='Bash']
|
|
77
|
+
* @param {string} [command='']
|
|
78
|
+
* @returns {boolean}
|
|
79
|
+
*/
|
|
80
|
+
function detectPowerShellSyntax(toolName = 'Bash', command = '') {
|
|
81
|
+
if (/^(?:PowerShell|pwsh)$/i.test(String(toolName || ''))) {
|
|
82
|
+
return true;
|
|
83
|
+
}
|
|
84
|
+
const str = String(command || '');
|
|
85
|
+
return /(?:^|[;|&\r\n]\s*)&\s*['"A-Za-z0-9_.\\/]|(?:\$env:[A-Za-z_])|(?:`--|`-[A-Za-z])/i.test(
|
|
86
|
+
str
|
|
87
|
+
);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Tokenizes a shell command line into tokens while preserving quoted strings as single tokens.
|
|
92
|
+
* Supports both POSIX backslash escapes and PowerShell backtick escapes / `&` call operator (`ISS-043`).
|
|
93
|
+
*
|
|
94
|
+
* @param {string} commandSegment
|
|
95
|
+
* @param {{ isPowerShell?: boolean }} [options={}]
|
|
96
|
+
* @returns {Array<string>}
|
|
97
|
+
*/
|
|
98
|
+
export function tokenizeShellSegment(commandSegment = '', options = {}) {
|
|
99
|
+
const tokens = [];
|
|
100
|
+
const str = String(commandSegment || '').trim();
|
|
101
|
+
const isPowerShell = Boolean(options?.isPowerShell);
|
|
102
|
+
let current = '';
|
|
103
|
+
let quoteChar = null;
|
|
104
|
+
|
|
105
|
+
for (let i = 0; i < str.length; i++) {
|
|
106
|
+
const ch = str[i];
|
|
107
|
+
if (quoteChar === "'") {
|
|
108
|
+
if (ch === "'") {
|
|
109
|
+
if (isPowerShell && str[i + 1] === "'") {
|
|
110
|
+
current += "'";
|
|
111
|
+
i += 1;
|
|
112
|
+
} else {
|
|
113
|
+
quoteChar = null;
|
|
114
|
+
}
|
|
115
|
+
} else {
|
|
116
|
+
current += ch;
|
|
117
|
+
}
|
|
118
|
+
} else if (quoteChar === '"') {
|
|
119
|
+
if (isPowerShell && ch === '`' && i + 1 < str.length) {
|
|
120
|
+
const next = str[i + 1];
|
|
121
|
+
current += next === 'n' ? '\n' : next === 'r' ? '\r' : next === 't' ? '\t' : next;
|
|
122
|
+
i += 1;
|
|
123
|
+
} else if (!isPowerShell && ch === '\\' && i + 1 < str.length && ['"', '\\', '$', '`'].includes(str[i + 1])) {
|
|
124
|
+
current += str[i + 1];
|
|
125
|
+
i += 1;
|
|
126
|
+
} else if (isPowerShell && ch === '\\' && str[i + 1] === '"') {
|
|
127
|
+
current += '"';
|
|
128
|
+
i += 1;
|
|
129
|
+
} else if (ch === '"') {
|
|
130
|
+
quoteChar = null;
|
|
131
|
+
} else {
|
|
132
|
+
current += ch;
|
|
133
|
+
}
|
|
134
|
+
} else if (ch === '"' || ch === "'") {
|
|
135
|
+
quoteChar = ch;
|
|
136
|
+
} else if (isPowerShell && ch === '`' && i + 1 < str.length) {
|
|
137
|
+
const next = str[i + 1];
|
|
138
|
+
current += next === 'n' ? '\n' : next === 'r' ? '\r' : next === 't' ? '\t' : next;
|
|
139
|
+
i += 1;
|
|
140
|
+
} else if (!isPowerShell && ch === '\\' && i + 1 < str.length) {
|
|
141
|
+
current += str[i + 1];
|
|
142
|
+
i += 1;
|
|
143
|
+
} else if (current.length === 0 && ch === '&' && str[i + 1] !== '&' && str[i + 1] !== '>') {
|
|
144
|
+
tokens.push('&');
|
|
145
|
+
} else if (/\s/.test(ch)) {
|
|
146
|
+
if (current.length > 0) {
|
|
147
|
+
tokens.push(current);
|
|
148
|
+
current = '';
|
|
149
|
+
}
|
|
150
|
+
} else {
|
|
151
|
+
current += ch;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
if (current.length > 0) {
|
|
155
|
+
tokens.push(current);
|
|
156
|
+
}
|
|
157
|
+
return tokens;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Extracts inner `$(...)` command substitutions (and POSIX `` `...` `` backtick substitutions
|
|
162
|
+
* when `!isPowerShell`) outside single-quoted literals for recursive `DET-01` inspection (`ISS-043`).
|
|
163
|
+
*
|
|
164
|
+
* @param {string} commandLine
|
|
165
|
+
* @param {{ isPowerShell?: boolean }} [options={}]
|
|
166
|
+
* @returns {Array<string>}
|
|
167
|
+
*/
|
|
168
|
+
function extractCommandSubstitutions(commandLine = '', options = {}) {
|
|
169
|
+
const extracted = [];
|
|
170
|
+
const str = String(commandLine || '');
|
|
171
|
+
const isPowerShell = Boolean(options?.isPowerShell);
|
|
172
|
+
let quoteChar = null;
|
|
173
|
+
|
|
174
|
+
for (let i = 0; i < str.length; i++) {
|
|
175
|
+
const ch = str[i];
|
|
176
|
+
if (quoteChar === "'") {
|
|
177
|
+
if (ch === "'") quoteChar = null;
|
|
178
|
+
continue;
|
|
179
|
+
}
|
|
180
|
+
if (quoteChar === '"') {
|
|
181
|
+
if ((ch === '\\' || (isPowerShell && ch === '`')) && i + 1 < str.length) {
|
|
182
|
+
i += 1;
|
|
183
|
+
continue;
|
|
184
|
+
}
|
|
185
|
+
if (ch === '"') {
|
|
186
|
+
quoteChar = null;
|
|
187
|
+
continue;
|
|
188
|
+
}
|
|
189
|
+
} else if (ch === "'" || ch === '"') {
|
|
190
|
+
quoteChar = ch;
|
|
191
|
+
continue;
|
|
192
|
+
} else if ((ch === '\\' || (isPowerShell && ch === '`')) && i + 1 < str.length) {
|
|
193
|
+
i += 1;
|
|
194
|
+
continue;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
// Balanced $(...) extraction
|
|
198
|
+
if (ch === '$' && str[i + 1] === '(') {
|
|
199
|
+
let depth = 1;
|
|
200
|
+
let j = i + 2;
|
|
201
|
+
let innerQuote = null;
|
|
202
|
+
while (j < str.length && depth > 0) {
|
|
203
|
+
const c = str[j];
|
|
204
|
+
if (innerQuote === "'") {
|
|
205
|
+
if (c === "'") innerQuote = null;
|
|
206
|
+
} else if (innerQuote === '"') {
|
|
207
|
+
if (c === '\\' && j + 1 < str.length) {
|
|
208
|
+
j += 1;
|
|
209
|
+
} else if (c === '"') {
|
|
210
|
+
innerQuote = null;
|
|
211
|
+
}
|
|
212
|
+
} else if (c === "'" || c === '"') {
|
|
213
|
+
innerQuote = c;
|
|
214
|
+
} else if (c === '(') {
|
|
215
|
+
depth += 1;
|
|
216
|
+
} else if (c === ')') {
|
|
217
|
+
depth -= 1;
|
|
218
|
+
if (depth === 0) break;
|
|
219
|
+
}
|
|
220
|
+
j += 1;
|
|
221
|
+
}
|
|
222
|
+
if (depth === 0) {
|
|
223
|
+
const inner = str.slice(i + 2, j).trim();
|
|
224
|
+
if (inner) extracted.push(inner);
|
|
225
|
+
}
|
|
226
|
+
continue;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
// POSIX `...` backtick command substitution (only when not PowerShell escape syntax)
|
|
230
|
+
if (!isPowerShell && ch === '`') {
|
|
231
|
+
let j = i + 1;
|
|
232
|
+
while (j < str.length) {
|
|
233
|
+
if (str[j] === '\\' && j + 1 < str.length) {
|
|
234
|
+
j += 2;
|
|
235
|
+
continue;
|
|
236
|
+
}
|
|
237
|
+
if (str[j] === '`') break;
|
|
238
|
+
j += 1;
|
|
239
|
+
}
|
|
240
|
+
if (j < str.length && str[j] === '`') {
|
|
241
|
+
const inner = str.slice(i + 1, j).trim();
|
|
242
|
+
if (inner && !inner.startsWith('-')) {
|
|
243
|
+
extracted.push(inner);
|
|
244
|
+
}
|
|
245
|
+
i = j;
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
return extracted;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* Splits a compound Bash, cmd, or PowerShell command into individual command segments
|
|
255
|
+
* on `;`, `\r?\n`, `&&`, `||`, single pipe `|` (`|&`), and non-redirection `&` separators (`ISS-043`).
|
|
256
|
+
*
|
|
257
|
+
* @param {string} commandLine
|
|
258
|
+
* @param {{ isPowerShell?: boolean }} [options={}]
|
|
259
|
+
* @returns {Array<string>}
|
|
260
|
+
*/
|
|
261
|
+
function splitCommandSegments(commandLine = '', options = {}) {
|
|
262
|
+
const segments = [];
|
|
263
|
+
const str = String(commandLine || '');
|
|
264
|
+
const isPowerShell = Boolean(options?.isPowerShell);
|
|
265
|
+
let current = '';
|
|
266
|
+
let quoteChar = null;
|
|
267
|
+
let subshellDepth = 0;
|
|
268
|
+
|
|
269
|
+
for (let i = 0; i < str.length; i++) {
|
|
270
|
+
const ch = str[i];
|
|
271
|
+
if (quoteChar === "'") {
|
|
272
|
+
current += ch;
|
|
273
|
+
if (ch === "'") {
|
|
274
|
+
if (isPowerShell && str[i + 1] === "'") {
|
|
275
|
+
current += "'";
|
|
276
|
+
i += 1;
|
|
277
|
+
} else {
|
|
278
|
+
quoteChar = null;
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
continue;
|
|
282
|
+
}
|
|
283
|
+
if (quoteChar === '"') {
|
|
284
|
+
current += ch;
|
|
285
|
+
if ((ch === '\\' || (isPowerShell && ch === '`')) && i + 1 < str.length) {
|
|
286
|
+
current += str[i + 1];
|
|
287
|
+
i += 1;
|
|
288
|
+
} else if (ch === '"') {
|
|
289
|
+
quoteChar = null;
|
|
290
|
+
}
|
|
291
|
+
continue;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
if (ch === '"' || ch === "'") {
|
|
295
|
+
quoteChar = ch;
|
|
296
|
+
current += ch;
|
|
297
|
+
continue;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
if ((ch === '\\' || (isPowerShell && ch === '`')) && i + 1 < str.length) {
|
|
301
|
+
current += ch + str[i + 1];
|
|
302
|
+
i += 1;
|
|
303
|
+
continue;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
if (ch === '$' && str[i + 1] === '(') {
|
|
307
|
+
subshellDepth += 1;
|
|
308
|
+
current += '$(';
|
|
309
|
+
i += 1;
|
|
310
|
+
continue;
|
|
311
|
+
}
|
|
312
|
+
if (subshellDepth > 0) {
|
|
313
|
+
if (ch === '(') subshellDepth += 1;
|
|
314
|
+
else if (ch === ')') subshellDepth -= 1;
|
|
315
|
+
current += ch;
|
|
316
|
+
continue;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
if (ch === ';' || ch === '\n' || ch === '\r') {
|
|
320
|
+
if (current.trim()) segments.push(current.trim());
|
|
321
|
+
current = '';
|
|
322
|
+
continue;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
if ((ch === '&' && str[i + 1] === '&') || (ch === '|' && str[i + 1] === '|')) {
|
|
326
|
+
if (current.trim()) segments.push(current.trim());
|
|
327
|
+
current = '';
|
|
328
|
+
i += 1;
|
|
329
|
+
continue;
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
if (ch === '|') {
|
|
333
|
+
if (current.trim()) segments.push(current.trim());
|
|
334
|
+
current = '';
|
|
335
|
+
if (str[i + 1] === '&') i += 1;
|
|
336
|
+
continue;
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
// Split mid-line `&` command separator (cmd.exe / POSIX background) while preserving leading `&` (PowerShell call op) and `2>&1` / `&>`
|
|
340
|
+
const prevChar = i > 0 ? str[i - 1] : '';
|
|
341
|
+
const nextChar = i + 1 < str.length ? str[i + 1] : '';
|
|
342
|
+
if (
|
|
343
|
+
ch === '&' &&
|
|
344
|
+
current.trim().length > 0 &&
|
|
345
|
+
prevChar !== '>' &&
|
|
346
|
+
prevChar !== '<' &&
|
|
347
|
+
nextChar !== '>' &&
|
|
348
|
+
nextChar !== '&'
|
|
349
|
+
) {
|
|
350
|
+
segments.push(current.trim());
|
|
351
|
+
current = '';
|
|
352
|
+
continue;
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
current += ch;
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
if (current.trim()) {
|
|
359
|
+
segments.push(current.trim());
|
|
360
|
+
}
|
|
361
|
+
return segments;
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
/**
|
|
365
|
+
* Checks whether a tokenized `(key, value)` environment variable assignment disables Git hooks
|
|
366
|
+
* or injects `core.hooksPath` (`ISS-043`).
|
|
367
|
+
*
|
|
368
|
+
* @param {string} rawKey
|
|
369
|
+
* @param {string} rawVal
|
|
370
|
+
* @returns {{ subType: string, reason: string } | null}
|
|
371
|
+
*/
|
|
372
|
+
function checkForbiddenGitEnvAssignment(rawKey, rawVal) {
|
|
373
|
+
const key = String(rawKey || '').trim().toUpperCase();
|
|
374
|
+
const val = String(rawVal ?? '')
|
|
375
|
+
.trim()
|
|
376
|
+
.replace(/^['"]|['"]$/g, '');
|
|
377
|
+
|
|
378
|
+
if (
|
|
379
|
+
(key === 'HUSKY' && val === '0') ||
|
|
380
|
+
(key === 'HUSKY_SKIP_HOOKS' && (val === '1' || val.toLowerCase() === 'true')) ||
|
|
381
|
+
(key === 'SKIP_GIT_HOOKS' && (val === '1' || val.toLowerCase() === 'true'))
|
|
382
|
+
) {
|
|
383
|
+
return {
|
|
384
|
+
subType: 'GIT_HOOK_ENV_BYPASS',
|
|
385
|
+
reason: `[DET-01 Git Hook Bypass Blocked] Disabling git hooks via ${key}=${val} is forbidden.`
|
|
386
|
+
};
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
if (
|
|
390
|
+
(key === 'GIT_CONFIG_COUNT' && /^[1-9]\d*$/.test(val)) ||
|
|
391
|
+
(/^GIT_CONFIG_KEY_\d+$/.test(key) && /^core\.hooksPath$/i.test(val)) ||
|
|
392
|
+
(key === 'GIT_CONFIG_PARAMETERS' && /core\.hooksPath/i.test(val))
|
|
393
|
+
) {
|
|
394
|
+
return {
|
|
395
|
+
subType: 'GIT_HOOKS_PATH_OVERRIDE',
|
|
396
|
+
reason: `[DET-01 Git Hook Bypass Blocked] Overriding git config via environment variable ${key}=${val} is forbidden.`
|
|
397
|
+
};
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
return null;
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
/**
|
|
404
|
+
* Splits a `KEY=VALUE` token on its first `=` character.
|
|
405
|
+
* @param {string} token
|
|
406
|
+
* @returns {{ key: string, value: string } | null}
|
|
407
|
+
*/
|
|
408
|
+
function splitEnvKeyValToken(token = '') {
|
|
409
|
+
const m = String(token || '').match(/^([A-Za-z_][A-Za-z0-9_]*)=(.*)$/s);
|
|
410
|
+
if (!m) return null;
|
|
411
|
+
return { key: m[1], value: m[2] };
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
/**
|
|
415
|
+
* Checks whether the user's prompt explicitly requested modifying, deleting, or skipping tests.
|
|
416
|
+
* @param {string} [promptText='']
|
|
417
|
+
* @returns {boolean}
|
|
418
|
+
*/
|
|
419
|
+
export function hasExplicitTestModifyIntent(promptText = '') {
|
|
420
|
+
const s = String(promptText || '');
|
|
421
|
+
if (!s) return false;
|
|
422
|
+
return (
|
|
423
|
+
/\b(?:edit|update|modify|delete|remove|drop|skip|disable|rewrite|adjust|refactor)\s+(?:(?:the|all|any|some|these|those|existing|failing|obsolete|legacy|old|broken|flaky|unit|integration|e2e)\s+)*(?:tests?|specs?|assertions?)\b/i.test(
|
|
424
|
+
s
|
|
425
|
+
) ||
|
|
426
|
+
(/\.(?:test|spec)\.[a-z0-9]+\b/i.test(s) &&
|
|
427
|
+
/\b(?:delete|remove|drop|skip|disable|update|rewrite)\b/i.test(s))
|
|
428
|
+
);
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
/**
|
|
432
|
+
* Checks whether a shell argument token points to a repository test file (excluding temp/cache paths).
|
|
433
|
+
* @param {string} [token='']
|
|
434
|
+
* @returns {boolean}
|
|
435
|
+
*/
|
|
436
|
+
function isProtectedTestFileToken(token = '') {
|
|
437
|
+
const norm = String(token || '').trim().replace(/^['"]|['"]$/g, '').replace(/\\/g, '/');
|
|
438
|
+
if (!norm) return false;
|
|
439
|
+
if (
|
|
440
|
+
/^(?:\/tmp\/|\/var\/folders\/)/i.test(norm) ||
|
|
441
|
+
/(?:^|\/)(?:tmp|temp|\.claude\/cache|\.agents\/tmp)\//i.test(norm) ||
|
|
442
|
+
/appdata\/local\/temp\//i.test(norm)
|
|
443
|
+
) {
|
|
444
|
+
return false;
|
|
445
|
+
}
|
|
446
|
+
return (
|
|
447
|
+
/\.(?:test|spec)\.(?:[cm]?[jt]sx?|py|go|rs)$/i.test(norm) ||
|
|
448
|
+
/(?:^|\/)(?:test|tests|__tests__)\/.*\.(?:[cm]?[jt]sx?|py|go|rs)$/i.test(norm) ||
|
|
449
|
+
/_test\.(?:py|go)$/i.test(norm)
|
|
450
|
+
);
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
/**
|
|
454
|
+
* DET-01: Destructive Shell & Git Hook-Bypass Blocker (`Bash|PowerShell`).
|
|
455
|
+
* Strictly skips option arguments (such as `-m "-next..."`, `-m "Fix HUSKY=0"`, `-F`, `-c`, `--author`),
|
|
456
|
+
* never blocks `git log -n 5`, `git clean -n`, `git commit -mnotes`, or `git config --get core.hooksPath`,
|
|
457
|
+
* and blocks `-u --no-verify`, pipe/subshell/newline chains, and PowerShell `&` / backtick bypasses (`ISS-043`).
|
|
458
|
+
*
|
|
459
|
+
* @param {object} [params={}]
|
|
460
|
+
* @param {string} [params.toolName='Bash']
|
|
461
|
+
* @param {string} [params.command='']
|
|
462
|
+
* @param {number} [_depth=0]
|
|
463
|
+
*/
|
|
464
|
+
export function evaluateDet01GitAndShellSafety(params = {}, _depth = 0) {
|
|
465
|
+
if (_depth > 4) {
|
|
466
|
+
return { detectorId: 'DET-01', triggered: false, decision: 'allow', reason: null };
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
const toolName = String(params.toolName || 'Bash');
|
|
470
|
+
if (!SHELL_TOOL_REGEX.test(toolName)) {
|
|
471
|
+
return { detectorId: 'DET-01', triggered: false, decision: 'allow', reason: null };
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
const command = String(params.command || params.cmd || '').trim();
|
|
475
|
+
if (!command) {
|
|
476
|
+
return { detectorId: 'DET-01', triggered: false, decision: 'allow', reason: null };
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
const isPowerShell = detectPowerShellSyntax(toolName, command);
|
|
480
|
+
|
|
481
|
+
// 1. Recursively inspect any inner $(...) or `...` command substitutions
|
|
482
|
+
const substitutions = extractCommandSubstitutions(command, { isPowerShell });
|
|
483
|
+
for (const subCmd of substitutions) {
|
|
484
|
+
const subVerdict = evaluateDet01GitAndShellSafety(
|
|
485
|
+
{ toolName: isPowerShell ? 'PowerShell' : toolName, command: subCmd },
|
|
486
|
+
_depth + 1
|
|
487
|
+
);
|
|
488
|
+
if (subVerdict.triggered) return subVerdict;
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
// 2. Inspect individual command segments after splitting on ;, &&, ||, |, \r?\n
|
|
492
|
+
const segments = splitCommandSegments(command, { isPowerShell });
|
|
493
|
+
for (const segment of segments) {
|
|
494
|
+
const rawTokens = tokenizeShellSegment(segment, { isPowerShell });
|
|
495
|
+
if (rawTokens.length === 0) continue;
|
|
496
|
+
|
|
497
|
+
let idx = 0;
|
|
498
|
+
|
|
499
|
+
// Strip leading PowerShell `&` call operator (`& git ...`, `& "git.exe" ...`)
|
|
500
|
+
while (idx < rawTokens.length && rawTokens[idx] === '&') {
|
|
501
|
+
idx += 1;
|
|
502
|
+
}
|
|
503
|
+
if (idx >= rawTokens.length) continue;
|
|
504
|
+
|
|
505
|
+
// 2a. PowerShell `$env:KEY = 'VAL'` / `$env:KEY='VAL'` assignment check
|
|
506
|
+
const psEnvMatch = rawTokens[idx].match(/^\$env:([A-Za-z_][A-Za-z0-9_]*)(?:=(.*))?$/is);
|
|
507
|
+
if (psEnvMatch) {
|
|
508
|
+
const envKey = psEnvMatch[1];
|
|
509
|
+
let envVal = psEnvMatch[2];
|
|
510
|
+
if (envVal === undefined) {
|
|
511
|
+
if (rawTokens[idx + 1] === '=') {
|
|
512
|
+
envVal = rawTokens[idx + 2] ?? '';
|
|
513
|
+
} else if (rawTokens[idx + 1]?.startsWith('=')) {
|
|
514
|
+
envVal = rawTokens[idx + 1].slice(1) || (rawTokens[idx + 2] ?? '');
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
if (envVal !== undefined) {
|
|
518
|
+
const envHit = checkForbiddenGitEnvAssignment(envKey, envVal);
|
|
519
|
+
if (envHit) {
|
|
520
|
+
return {
|
|
521
|
+
detectorId: 'DET-01',
|
|
522
|
+
triggered: true,
|
|
523
|
+
decision: 'deny',
|
|
524
|
+
subType: envHit.subType,
|
|
525
|
+
reason: envHit.reason
|
|
526
|
+
};
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
continue;
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
// 2b. PowerShell `Set-Item Env:HUSKY 0` check
|
|
533
|
+
if (/^(?:Set-Item|si)$/i.test(rawTokens[idx]) && rawTokens[idx + 1]) {
|
|
534
|
+
const envPathMatch = rawTokens[idx + 1].match(/^env:[\\/]?([A-Za-z_][A-Za-z0-9_]*)$/i);
|
|
535
|
+
if (envPathMatch) {
|
|
536
|
+
const envHit = checkForbiddenGitEnvAssignment(envPathMatch[1], rawTokens[idx + 2] ?? '');
|
|
537
|
+
if (envHit) {
|
|
538
|
+
return {
|
|
539
|
+
detectorId: 'DET-01',
|
|
540
|
+
triggered: true,
|
|
541
|
+
decision: 'deny',
|
|
542
|
+
subType: envHit.subType,
|
|
543
|
+
reason: envHit.reason
|
|
544
|
+
};
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
// 2c. `export KEY=VAL` or `set KEY=VAL` statements
|
|
550
|
+
const firstWordLower = rawTokens[idx].toLowerCase();
|
|
551
|
+
if (firstWordLower === 'export' || firstWordLower === 'set') {
|
|
552
|
+
for (let eIdx = idx + 1; eIdx < rawTokens.length; eIdx++) {
|
|
553
|
+
const kv = splitEnvKeyValToken(rawTokens[eIdx]);
|
|
554
|
+
if (kv) {
|
|
555
|
+
const envHit = checkForbiddenGitEnvAssignment(kv.key, kv.value);
|
|
556
|
+
if (envHit) {
|
|
557
|
+
return {
|
|
558
|
+
detectorId: 'DET-01',
|
|
559
|
+
triggered: true,
|
|
560
|
+
decision: 'deny',
|
|
561
|
+
subType: envHit.subType,
|
|
562
|
+
reason: envHit.reason
|
|
563
|
+
};
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
continue;
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
// 2d. Leading `KEY=VAL` assignments (`HUSKY=0 git commit ...`)
|
|
571
|
+
while (idx < rawTokens.length) {
|
|
572
|
+
const kv = splitEnvKeyValToken(rawTokens[idx]);
|
|
573
|
+
if (!kv) break;
|
|
574
|
+
const envHit = checkForbiddenGitEnvAssignment(kv.key, kv.value);
|
|
575
|
+
if (envHit) {
|
|
576
|
+
return {
|
|
577
|
+
detectorId: 'DET-01',
|
|
578
|
+
triggered: true,
|
|
579
|
+
decision: 'deny',
|
|
580
|
+
subType: envHit.subType,
|
|
581
|
+
reason: envHit.reason
|
|
582
|
+
};
|
|
583
|
+
}
|
|
584
|
+
idx += 1;
|
|
585
|
+
}
|
|
586
|
+
while (idx < rawTokens.length && rawTokens[idx] === '&') {
|
|
587
|
+
idx += 1;
|
|
588
|
+
}
|
|
589
|
+
if (idx >= rawTokens.length) continue;
|
|
590
|
+
|
|
591
|
+
// 2e. `env` wrapper (`env HUSKY=0 git commit ...`)
|
|
592
|
+
let bin = rawTokens[idx].replace(/^.*[\\/]/, '').toLowerCase();
|
|
593
|
+
if (bin === 'env' || bin === 'env.exe') {
|
|
594
|
+
idx += 1;
|
|
595
|
+
while (idx < rawTokens.length) {
|
|
596
|
+
const t = rawTokens[idx];
|
|
597
|
+
if (t === '-u' || t === '--unset' || t === '-C' || t === '--chdir' || t === '-S') {
|
|
598
|
+
idx += 2;
|
|
599
|
+
continue;
|
|
600
|
+
}
|
|
601
|
+
if (t.startsWith('-')) {
|
|
602
|
+
idx += 1;
|
|
603
|
+
continue;
|
|
604
|
+
}
|
|
605
|
+
const kv = splitEnvKeyValToken(t);
|
|
606
|
+
if (kv) {
|
|
607
|
+
const envHit = checkForbiddenGitEnvAssignment(kv.key, kv.value);
|
|
608
|
+
if (envHit) {
|
|
609
|
+
return {
|
|
610
|
+
detectorId: 'DET-01',
|
|
611
|
+
triggered: true,
|
|
612
|
+
decision: 'deny',
|
|
613
|
+
subType: envHit.subType,
|
|
614
|
+
reason: envHit.reason
|
|
615
|
+
};
|
|
616
|
+
}
|
|
617
|
+
idx += 1;
|
|
618
|
+
continue;
|
|
619
|
+
}
|
|
620
|
+
break;
|
|
621
|
+
}
|
|
622
|
+
if (idx >= rawTokens.length) continue;
|
|
623
|
+
bin = rawTokens[idx].replace(/^.*[\\/]/, '').toLowerCase();
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
// 2f. Nested shell wrappers (`bash -c "..."`, `sh -c "..."`, `cmd /c "..."`, `pwsh -Command "..."`)
|
|
627
|
+
const userExplicitTestIntent =
|
|
628
|
+
Boolean(params.userExplicitTestIntent) ||
|
|
629
|
+
hasExplicitTestModifyIntent(params.lastUserInput || params.prompt || '');
|
|
630
|
+
if (['bash', 'bash.exe', 'sh', 'sh.exe', 'zsh', 'dash'].includes(bin)) {
|
|
631
|
+
const cPos = rawTokens.findIndex((t, ti) => ti > idx && (t === '-c' || /^-[a-zA-Z]*c$/.test(t)));
|
|
632
|
+
if (cPos !== -1 && rawTokens[cPos + 1]) {
|
|
633
|
+
const nested = evaluateDet01GitAndShellSafety(
|
|
634
|
+
{
|
|
635
|
+
toolName: 'Bash',
|
|
636
|
+
command: rawTokens[cPos + 1],
|
|
637
|
+
userExplicitTestIntent,
|
|
638
|
+
lastUserInput: params.lastUserInput || params.prompt || ''
|
|
639
|
+
},
|
|
640
|
+
_depth + 1
|
|
641
|
+
);
|
|
642
|
+
if (nested.triggered) return nested;
|
|
643
|
+
}
|
|
644
|
+
continue;
|
|
645
|
+
}
|
|
646
|
+
if (bin === 'cmd' || bin === 'cmd.exe') {
|
|
647
|
+
const cPos = rawTokens.findIndex((t, ti) => ti > idx && /^\/[ck]$/i.test(t));
|
|
648
|
+
if (cPos !== -1 && cPos + 1 < rawTokens.length) {
|
|
649
|
+
const nestedCmd = rawTokens.slice(cPos + 1).join(' ');
|
|
650
|
+
const nested = evaluateDet01GitAndShellSafety(
|
|
651
|
+
{
|
|
652
|
+
toolName: 'cmd',
|
|
653
|
+
command: nestedCmd,
|
|
654
|
+
userExplicitTestIntent,
|
|
655
|
+
lastUserInput: params.lastUserInput || params.prompt || ''
|
|
656
|
+
},
|
|
657
|
+
_depth + 1
|
|
658
|
+
);
|
|
659
|
+
if (nested.triggered) return nested;
|
|
660
|
+
}
|
|
661
|
+
continue;
|
|
662
|
+
}
|
|
663
|
+
if (['powershell', 'powershell.exe', 'pwsh', 'pwsh.exe'].includes(bin)) {
|
|
664
|
+
const cPos = rawTokens.findIndex((t, ti) => ti > idx && /^-(?:c|command)$/i.test(t));
|
|
665
|
+
if (cPos !== -1 && cPos + 1 < rawTokens.length) {
|
|
666
|
+
const nestedCmd = rawTokens.slice(cPos + 1).join(' ');
|
|
667
|
+
const nested = evaluateDet01GitAndShellSafety(
|
|
668
|
+
{
|
|
669
|
+
toolName: 'PowerShell',
|
|
670
|
+
command: nestedCmd,
|
|
671
|
+
userExplicitTestIntent,
|
|
672
|
+
lastUserInput: params.lastUserInput || params.prompt || ''
|
|
673
|
+
},
|
|
674
|
+
_depth + 1
|
|
675
|
+
);
|
|
676
|
+
if (nested.triggered) return nested;
|
|
677
|
+
}
|
|
678
|
+
continue;
|
|
679
|
+
}
|
|
680
|
+
|
|
681
|
+
// 2g. Destructive filesystem wipe commands & Test File Deletion Lock (#3)
|
|
682
|
+
if (bin === 'rm' || bin === 'rm.exe' || bin === 'unlink' || bin === 'unlink.exe') {
|
|
683
|
+
const rmArgs = rawTokens.slice(idx + 1);
|
|
684
|
+
const hasRecursive = rmArgs.some(
|
|
685
|
+
(a) => a === '--recursive' || (/^-[a-zA-Z]+$/.test(a) && /[rR]/.test(a))
|
|
686
|
+
);
|
|
687
|
+
const hasForce = rmArgs.some(
|
|
688
|
+
(a) => a === '--force' || (/^-[a-zA-Z]+$/.test(a) && /f/.test(a))
|
|
689
|
+
);
|
|
690
|
+
const targetsRootOrHome = rmArgs.some((a) =>
|
|
691
|
+
/^(?:\/+|\/\*|\~|\$HOME|\$\{HOME\}|[A-Za-z]:[\\/]?)$/i.test(a)
|
|
692
|
+
);
|
|
693
|
+
if (hasRecursive && hasForce && targetsRootOrHome) {
|
|
694
|
+
return {
|
|
695
|
+
detectorId: 'DET-01',
|
|
696
|
+
triggered: true,
|
|
697
|
+
decision: 'deny',
|
|
698
|
+
subType: 'DESTRUCTIVE_FS_WIPE',
|
|
699
|
+
reason: `[DET-01 Destructive Shell Blocked] Recursive root/home deletion is forbidden.`
|
|
700
|
+
};
|
|
701
|
+
}
|
|
702
|
+
if (!userExplicitTestIntent) {
|
|
703
|
+
const deletedTestTarget = rmArgs.find(
|
|
704
|
+
(a) => !a.startsWith('-') && isProtectedTestFileToken(a)
|
|
705
|
+
);
|
|
706
|
+
if (deletedTestTarget) {
|
|
707
|
+
return {
|
|
708
|
+
detectorId: 'DET-01',
|
|
709
|
+
triggered: true,
|
|
710
|
+
decision: 'deny',
|
|
711
|
+
subType: 'TEST_FILE_DELETION',
|
|
712
|
+
reason: `[DET-01 Test Integrity Lock (#3)] Deleting test file "${deletedTestTarget}" via shell without explicit user instruction is forbidden.`
|
|
713
|
+
};
|
|
714
|
+
}
|
|
715
|
+
}
|
|
716
|
+
continue;
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
if (/^(?:remove-item|ri|del|erase|rmdir)$/i.test(bin)) {
|
|
720
|
+
const psArgs = rawTokens.slice(idx + 1);
|
|
721
|
+
const hasRecurse = psArgs.some((a) => /^-r(?:ecurse)?$/i.test(a));
|
|
722
|
+
const hasForce = psArgs.some((a) => /^-f(?:orce)?$/i.test(a));
|
|
723
|
+
const targetsRootOrHome = psArgs.some((a) =>
|
|
724
|
+
/^(?:[A-Za-z]:[\\/]?|\/+|\~|\$HOME|\$env:USERPROFILE)$/i.test(a)
|
|
725
|
+
);
|
|
726
|
+
if (hasRecurse && hasForce && targetsRootOrHome) {
|
|
727
|
+
return {
|
|
728
|
+
detectorId: 'DET-01',
|
|
729
|
+
triggered: true,
|
|
730
|
+
decision: 'deny',
|
|
731
|
+
subType: 'DESTRUCTIVE_FS_WIPE',
|
|
732
|
+
reason: `[DET-01 Destructive Shell Blocked] Recursive root/home deletion is forbidden.`
|
|
733
|
+
};
|
|
734
|
+
}
|
|
735
|
+
if (!userExplicitTestIntent) {
|
|
736
|
+
const deletedTestTarget = psArgs.find(
|
|
737
|
+
(a) => !a.startsWith('-') && !a.startsWith('/') && isProtectedTestFileToken(a)
|
|
738
|
+
);
|
|
739
|
+
if (deletedTestTarget) {
|
|
740
|
+
return {
|
|
741
|
+
detectorId: 'DET-01',
|
|
742
|
+
triggered: true,
|
|
743
|
+
decision: 'deny',
|
|
744
|
+
subType: 'TEST_FILE_DELETION',
|
|
745
|
+
reason: `[DET-01 Test Integrity Lock (#3)] Deleting test file "${deletedTestTarget}" via shell without explicit user instruction is forbidden.`
|
|
746
|
+
};
|
|
747
|
+
}
|
|
748
|
+
}
|
|
749
|
+
continue;
|
|
750
|
+
}
|
|
751
|
+
|
|
752
|
+
if (bin !== 'git' && bin !== 'git.exe') continue;
|
|
753
|
+
idx += 1;
|
|
754
|
+
|
|
755
|
+
// Parse global git options before subcommand
|
|
756
|
+
let subcommand = null;
|
|
757
|
+
while (idx < rawTokens.length) {
|
|
758
|
+
const tok = rawTokens[idx];
|
|
759
|
+
if (!tok.startsWith('-')) {
|
|
760
|
+
subcommand = tok.toLowerCase();
|
|
761
|
+
idx += 1;
|
|
762
|
+
break;
|
|
763
|
+
}
|
|
764
|
+
if (tok === '-c' && idx + 1 < rawTokens.length) {
|
|
765
|
+
const cfg = rawTokens[idx + 1];
|
|
766
|
+
if (/^core\.hooksPath(?:\s*=|$)/i.test(cfg)) {
|
|
767
|
+
return {
|
|
768
|
+
detectorId: 'DET-01',
|
|
769
|
+
triggered: true,
|
|
770
|
+
decision: 'deny',
|
|
771
|
+
subType: 'GIT_HOOKS_PATH_OVERRIDE',
|
|
772
|
+
reason: `[DET-01 Git Hook Bypass Blocked] Overriding core.hooksPath via "git -c ${cfg}" is forbidden.`
|
|
773
|
+
};
|
|
774
|
+
}
|
|
775
|
+
idx += 2;
|
|
776
|
+
continue;
|
|
777
|
+
}
|
|
778
|
+
if (
|
|
779
|
+
/^-c\s*core\.hooksPath(?:\s*=|$)/i.test(tok) ||
|
|
780
|
+
/^--config-env=core\.hooksPath(?:=|$)/i.test(tok)
|
|
781
|
+
) {
|
|
782
|
+
return {
|
|
783
|
+
detectorId: 'DET-01',
|
|
784
|
+
triggered: true,
|
|
785
|
+
decision: 'deny',
|
|
786
|
+
subType: 'GIT_HOOKS_PATH_OVERRIDE',
|
|
787
|
+
reason: `[DET-01 Git Hook Bypass Blocked] Overriding core.hooksPath is forbidden.`
|
|
788
|
+
};
|
|
789
|
+
}
|
|
790
|
+
if (tok === '--config-env' && idx + 1 < rawTokens.length) {
|
|
791
|
+
if (/^core\.hooksPath(?:=|$)/i.test(rawTokens[idx + 1])) {
|
|
792
|
+
return {
|
|
793
|
+
detectorId: 'DET-01',
|
|
794
|
+
triggered: true,
|
|
795
|
+
decision: 'deny',
|
|
796
|
+
subType: 'GIT_HOOKS_PATH_OVERRIDE',
|
|
797
|
+
reason: `[DET-01 Git Hook Bypass Blocked] Overriding core.hooksPath via --config-env is forbidden.`
|
|
798
|
+
};
|
|
799
|
+
}
|
|
800
|
+
idx += 2;
|
|
801
|
+
continue;
|
|
802
|
+
}
|
|
803
|
+
if (GIT_GLOBAL_OPTS_WITH_ARG.has(tok)) {
|
|
804
|
+
idx += 2;
|
|
805
|
+
} else {
|
|
806
|
+
idx += 1;
|
|
807
|
+
}
|
|
808
|
+
}
|
|
809
|
+
|
|
810
|
+
if (!subcommand) continue;
|
|
811
|
+
|
|
812
|
+
const subArgs = rawTokens.slice(idx);
|
|
813
|
+
|
|
814
|
+
// Check `git config ... core.hooksPath <val>` (allow read-only `--get`, `--list`, `get`, or single-arg query)
|
|
815
|
+
if (subcommand === 'config') {
|
|
816
|
+
let isReadOnlyQuery = false;
|
|
817
|
+
const positional = [];
|
|
818
|
+
for (let i = 0; i < subArgs.length; i++) {
|
|
819
|
+
const arg = subArgs[i];
|
|
820
|
+
if (arg === '--') {
|
|
821
|
+
positional.push(...subArgs.slice(i + 1));
|
|
822
|
+
break;
|
|
823
|
+
}
|
|
824
|
+
if (
|
|
825
|
+
arg === '--get' ||
|
|
826
|
+
arg === '--get-all' ||
|
|
827
|
+
arg === '--get-regexp' ||
|
|
828
|
+
arg === '--get-urlmatch' ||
|
|
829
|
+
arg === '--list' ||
|
|
830
|
+
arg === '-l'
|
|
831
|
+
) {
|
|
832
|
+
isReadOnlyQuery = true;
|
|
833
|
+
break;
|
|
834
|
+
}
|
|
835
|
+
if (GIT_CONFIG_OPTS_WITH_ARG.has(arg)) {
|
|
836
|
+
i += 1;
|
|
837
|
+
continue;
|
|
838
|
+
}
|
|
839
|
+
if (arg.startsWith('-')) {
|
|
840
|
+
continue;
|
|
841
|
+
}
|
|
842
|
+
positional.push(arg);
|
|
843
|
+
}
|
|
844
|
+
|
|
845
|
+
if (!isReadOnlyQuery && positional.length >= 2) {
|
|
846
|
+
const firstPosLower = positional[0].toLowerCase();
|
|
847
|
+
if (firstPosLower !== 'get' && firstPosLower !== 'list') {
|
|
848
|
+
const cfgKey = firstPosLower === 'set' ? positional[1] || '' : positional[0];
|
|
849
|
+
const hasCfgVal = firstPosLower === 'set' ? positional.length >= 3 : positional.length >= 2;
|
|
850
|
+
if (hasCfgVal && /^core\.hooksPath$/i.test(cfgKey)) {
|
|
851
|
+
return {
|
|
852
|
+
detectorId: 'DET-01',
|
|
853
|
+
triggered: true,
|
|
854
|
+
decision: 'deny',
|
|
855
|
+
subType: 'GIT_HOOKS_PATH_OVERRIDE',
|
|
856
|
+
reason: `[DET-01 Git Hook Bypass Blocked] Mutating "git config core.hooksPath" is forbidden.`
|
|
857
|
+
};
|
|
858
|
+
}
|
|
859
|
+
}
|
|
860
|
+
}
|
|
861
|
+
continue;
|
|
862
|
+
}
|
|
863
|
+
|
|
864
|
+
// Check `git reset --hard`
|
|
865
|
+
if (subcommand === 'reset' && subArgs.includes('--hard')) {
|
|
866
|
+
return {
|
|
867
|
+
detectorId: 'DET-01',
|
|
868
|
+
triggered: true,
|
|
869
|
+
decision: 'deny',
|
|
870
|
+
subType: 'GIT_RESET_HARD',
|
|
871
|
+
reason: `[DET-01 Destructive Git Blocked] "git reset --hard" destroys uncommitted work.`
|
|
872
|
+
};
|
|
873
|
+
}
|
|
874
|
+
|
|
875
|
+
// Check `git clean` with any short-flag cluster containing `f` (e.g. `-f`, `-uf`, `-xfd`) while allowing `-n` / `--dry-run`
|
|
876
|
+
if (subcommand === 'clean') {
|
|
877
|
+
let hasDryRun = false;
|
|
878
|
+
let hasForce = false;
|
|
879
|
+
let hasExplicitLongForce = false;
|
|
880
|
+
for (let i = 0; i < subArgs.length; i++) {
|
|
881
|
+
const arg = subArgs[i];
|
|
882
|
+
if (arg === '--') break;
|
|
883
|
+
if (arg === '-e' || arg === '--exclude') {
|
|
884
|
+
i += 1;
|
|
885
|
+
continue;
|
|
886
|
+
}
|
|
887
|
+
if (arg === '-n' || arg === '--dry-run' || (/^-[a-zA-Z]+$/.test(arg) && arg.includes('n'))) {
|
|
888
|
+
hasDryRun = true;
|
|
889
|
+
}
|
|
890
|
+
if (arg === '--force') {
|
|
891
|
+
hasForce = true;
|
|
892
|
+
hasExplicitLongForce = true;
|
|
893
|
+
} else if (/^-[a-zA-Z]*f[a-zA-Z]*$/.test(arg)) {
|
|
894
|
+
hasForce = true;
|
|
895
|
+
}
|
|
896
|
+
}
|
|
897
|
+
if (hasForce && (!hasDryRun || hasExplicitLongForce)) {
|
|
898
|
+
return {
|
|
899
|
+
detectorId: 'DET-01',
|
|
900
|
+
triggered: true,
|
|
901
|
+
decision: 'deny',
|
|
902
|
+
subType: 'GIT_CLEAN_FORCE',
|
|
903
|
+
reason: `[DET-01 Destructive Git Blocked] "git clean -f" permanently deletes untracked files (use "git clean -n" for dry-run).`
|
|
904
|
+
};
|
|
905
|
+
}
|
|
906
|
+
}
|
|
907
|
+
|
|
908
|
+
// Check `git commit` with strict option-argument skipping (`-u` optional attached mode, `-mnotes` allowed, `-mfoo --no-verify` denied)
|
|
909
|
+
if (subcommand === 'commit') {
|
|
910
|
+
for (let i = 0; i < subArgs.length; i++) {
|
|
911
|
+
const arg = subArgs[i];
|
|
912
|
+
if (arg === '--') break; // End of options
|
|
913
|
+
|
|
914
|
+
if (arg === '--no-verify') {
|
|
915
|
+
return {
|
|
916
|
+
detectorId: 'DET-01',
|
|
917
|
+
triggered: true,
|
|
918
|
+
decision: 'deny',
|
|
919
|
+
subType: 'GIT_COMMIT_NO_VERIFY',
|
|
920
|
+
reason: `[DET-01 Git Hook Bypass Blocked] "git commit --no-verify" is forbidden.`
|
|
921
|
+
};
|
|
922
|
+
}
|
|
923
|
+
|
|
924
|
+
// Exact option that consumes the next token as its value (e.g. `-m "msg"`, `-F file`, `--author "name"`)
|
|
925
|
+
if (GIT_COMMIT_OPTS_WITH_ARG.has(arg)) {
|
|
926
|
+
i += 1; // Skip argument value token!
|
|
927
|
+
continue;
|
|
928
|
+
}
|
|
929
|
+
|
|
930
|
+
// Long option (e.g. `--message=...`, `--untracked-files`, `--untracked-files=no`) -> self-contained
|
|
931
|
+
if (arg.startsWith('--')) {
|
|
932
|
+
continue;
|
|
933
|
+
}
|
|
934
|
+
|
|
935
|
+
// Short flag or short flag cluster (`-n`, `-am`, `-amn`, `-m"msg"`, `-mnotes`, `-uno`, `-u`)
|
|
936
|
+
if (arg.startsWith('-') && arg.length > 1) {
|
|
937
|
+
const cluster = arg.slice(1);
|
|
938
|
+
// Pure short-flag cluster not starting with a value-taking flag (e.g. `-n`, `-an`, `-amn`, `-nm`)
|
|
939
|
+
if (
|
|
940
|
+
!SHORT_COMMIT_ARG_CHARS.has(cluster[0]) &&
|
|
941
|
+
cluster[0] !== 'u' &&
|
|
942
|
+
/^[amnvqseS]+$/.test(cluster)
|
|
943
|
+
) {
|
|
944
|
+
if (cluster.includes('n')) {
|
|
945
|
+
return {
|
|
946
|
+
detectorId: 'DET-01',
|
|
947
|
+
triggered: true,
|
|
948
|
+
decision: 'deny',
|
|
949
|
+
subType: 'GIT_COMMIT_SHORT_NO_VERIFY',
|
|
950
|
+
reason: `[DET-01 Git Hook Bypass Blocked] Short flag "-n" (--no-verify) in "git commit ${arg}" is forbidden.`
|
|
951
|
+
};
|
|
952
|
+
}
|
|
953
|
+
if (cluster.includes('m')) {
|
|
954
|
+
i += 1; // Next token is the commit message argument
|
|
955
|
+
}
|
|
956
|
+
continue;
|
|
957
|
+
}
|
|
958
|
+
|
|
959
|
+
let consumedRestOfToken = false;
|
|
960
|
+
for (let cIdx = 0; cIdx < cluster.length; cIdx++) {
|
|
961
|
+
const ch = cluster[cIdx];
|
|
962
|
+
if (ch === 'n') {
|
|
963
|
+
return {
|
|
964
|
+
detectorId: 'DET-01',
|
|
965
|
+
triggered: true,
|
|
966
|
+
decision: 'deny',
|
|
967
|
+
subType: 'GIT_COMMIT_SHORT_NO_VERIFY',
|
|
968
|
+
reason: `[DET-01 Git Hook Bypass Blocked] Short flag "-n" (--no-verify) in "git commit ${arg}" is forbidden.`
|
|
969
|
+
};
|
|
970
|
+
}
|
|
971
|
+
if (ch === 'u') {
|
|
972
|
+
// `-u[<mode>]` takes an optional ATTACHED mode (`no`, `normal`, `all`) and NEVER consumes the next token!
|
|
973
|
+
const restMode = cluster.slice(cIdx + 1);
|
|
974
|
+
if (VALID_GIT_COMMIT_UNTRACKED_MODES.has(restMode)) {
|
|
975
|
+
consumedRestOfToken = true;
|
|
976
|
+
break;
|
|
977
|
+
}
|
|
978
|
+
continue;
|
|
979
|
+
}
|
|
980
|
+
if (SHORT_COMMIT_ARG_CHARS.has(ch)) {
|
|
981
|
+
// If `m`/`F`/`c`/`C`/`t` is the last char in the cluster (e.g. `-am`), the NEXT token is its value
|
|
982
|
+
if (cIdx === cluster.length - 1) {
|
|
983
|
+
i += 1;
|
|
984
|
+
}
|
|
985
|
+
// Otherwise (e.g. `-mnotes`, `-mfoo`), the rest of THIS token is its attached value (do NOT skip next token!)
|
|
986
|
+
consumedRestOfToken = true;
|
|
987
|
+
break;
|
|
988
|
+
}
|
|
989
|
+
}
|
|
990
|
+
if (consumedRestOfToken) continue;
|
|
991
|
+
}
|
|
992
|
+
}
|
|
993
|
+
}
|
|
994
|
+
|
|
995
|
+
// Check `git push`, `git merge`, `git rebase`, `git cherry-pick` for `--no-verify` or destructive force push
|
|
996
|
+
if (['push', 'merge', 'rebase', 'cherry-pick'].includes(subcommand)) {
|
|
997
|
+
for (let i = 0; i < subArgs.length; i++) {
|
|
998
|
+
const arg = subArgs[i];
|
|
999
|
+
if (arg === '--') break;
|
|
1000
|
+
if (arg === '--no-verify') {
|
|
1001
|
+
return {
|
|
1002
|
+
detectorId: 'DET-01',
|
|
1003
|
+
triggered: true,
|
|
1004
|
+
decision: 'deny',
|
|
1005
|
+
subType: 'GIT_NO_VERIFY',
|
|
1006
|
+
reason: `[DET-01 Git Hook Bypass Blocked] "git ${subcommand} --no-verify" is forbidden.`
|
|
1007
|
+
};
|
|
1008
|
+
}
|
|
1009
|
+
if (
|
|
1010
|
+
subcommand === 'push' &&
|
|
1011
|
+
(arg === '--force' || arg === '-f') &&
|
|
1012
|
+
!subArgs.some((a) => a.startsWith('--force-with-lease'))
|
|
1013
|
+
) {
|
|
1014
|
+
return {
|
|
1015
|
+
detectorId: 'DET-01',
|
|
1016
|
+
triggered: true,
|
|
1017
|
+
decision: 'deny',
|
|
1018
|
+
subType: 'GIT_PUSH_FORCE',
|
|
1019
|
+
reason: `[DET-01 Destructive Git Blocked] Unconditional "git push --force" is forbidden (use --force-with-lease if required).`
|
|
1020
|
+
};
|
|
1021
|
+
}
|
|
1022
|
+
}
|
|
1023
|
+
}
|
|
1024
|
+
|
|
1025
|
+
// Item A9 (#18): Un-stashed Working-Tree Discard Guard (`git checkout -- <path>`, `git checkout .`, `git checkout -f`, `git restore <path>`)
|
|
1026
|
+
if (subcommand === 'checkout') {
|
|
1027
|
+
const isBranchCreation = subArgs.some((a) => a === '-b' || a === '-B' || a === '--orphan');
|
|
1028
|
+
if (!isBranchCreation) {
|
|
1029
|
+
const hasForceCheckout = subArgs.some((a) => a === '-f' || a === '--force');
|
|
1030
|
+
const hasDoubleDashDiscard = subArgs.includes('--');
|
|
1031
|
+
const hasDotDiscard = subArgs.includes('.');
|
|
1032
|
+
if (hasForceCheckout || hasDoubleDashDiscard || hasDotDiscard) {
|
|
1033
|
+
return {
|
|
1034
|
+
detectorId: 'DET-01',
|
|
1035
|
+
triggered: true,
|
|
1036
|
+
decision: 'deny',
|
|
1037
|
+
subType: 'GIT_UNSTASHED_DISCARD',
|
|
1038
|
+
reason: `[DET-01 Worktree Destructive Guard (#18)] Un-stashed working-tree discard ("git checkout") destroys uncommitted edits. Run "git stash push -u" first.`
|
|
1039
|
+
};
|
|
1040
|
+
}
|
|
1041
|
+
}
|
|
1042
|
+
}
|
|
1043
|
+
|
|
1044
|
+
if (subcommand === 'restore') {
|
|
1045
|
+
const hasStagedOnly =
|
|
1046
|
+
subArgs.some((a) => a === '--staged' || a === '-S') &&
|
|
1047
|
+
!subArgs.some((a) => a === '--worktree' || a === '-W');
|
|
1048
|
+
if (!hasStagedOnly && subArgs.some((a) => a !== '--help' && a !== '-h')) {
|
|
1049
|
+
return {
|
|
1050
|
+
detectorId: 'DET-01',
|
|
1051
|
+
triggered: true,
|
|
1052
|
+
decision: 'deny',
|
|
1053
|
+
subType: 'GIT_UNSTASHED_DISCARD',
|
|
1054
|
+
reason: `[DET-01 Worktree Destructive Guard (#18)] Un-stashed "git restore" overwrites working-tree edits. Run "git stash push -u" first (or pass --staged to unstage).`
|
|
1055
|
+
};
|
|
1056
|
+
}
|
|
1057
|
+
}
|
|
1058
|
+
|
|
1059
|
+
// Item A2 (#3): EvilGenie `.git` Object / Reflog Answer-Mining Guard (`git fsck`, `git cat-file`, `git reflog`, `git log --all -p`)
|
|
1060
|
+
const promptForGit = String(params.lastUserInput || params.prompt || '');
|
|
1061
|
+
const userRequestedGitForensics = /\b(?:reflog|fsck|cat-file|recover\s+lost|dangling)\b/i.test(
|
|
1062
|
+
promptForGit
|
|
1063
|
+
);
|
|
1064
|
+
if (!userRequestedGitForensics) {
|
|
1065
|
+
if (subcommand === 'fsck' || subcommand === 'cat-file' || subcommand === 'reflog') {
|
|
1066
|
+
return {
|
|
1067
|
+
detectorId: 'DET-01',
|
|
1068
|
+
triggered: true,
|
|
1069
|
+
decision: 'deny',
|
|
1070
|
+
subType: 'GIT_HISTORY_MINING',
|
|
1071
|
+
reason: `[DET-01 Test Integrity Lock (#3)] Raw git object/reflog inspection ("git ${subcommand}") is blocked to prevent benchmark/test history mining.`
|
|
1072
|
+
};
|
|
1073
|
+
}
|
|
1074
|
+
if (
|
|
1075
|
+
subcommand === 'log' &&
|
|
1076
|
+
subArgs.includes('--all') &&
|
|
1077
|
+
subArgs.some((a) => a === '-p' || a === '--patch')
|
|
1078
|
+
) {
|
|
1079
|
+
return {
|
|
1080
|
+
detectorId: 'DET-01',
|
|
1081
|
+
triggered: true,
|
|
1082
|
+
decision: 'deny',
|
|
1083
|
+
subType: 'GIT_HISTORY_MINING',
|
|
1084
|
+
reason: `[DET-01 Test Integrity Lock (#3)] Full-history patch mining ("git log --all -p") is blocked.`
|
|
1085
|
+
};
|
|
1086
|
+
}
|
|
1087
|
+
}
|
|
1088
|
+
}
|
|
1089
|
+
|
|
1090
|
+
return { detectorId: 'DET-01', triggered: false, decision: 'allow', reason: null };
|
|
1091
|
+
}
|
|
1092
|
+
|
|
1093
|
+
/**
|
|
1094
|
+
* DET-02: Secret & Credential Boundary.
|
|
1095
|
+
* Blocks access to `.env*`, `*.pem`, `~/.ssh/*`, `~/.aws/*` while explicitly allowlisting
|
|
1096
|
+
* `.env.example`, `.env.sample`, `.env.template`, `.env.defaults`, `.env.schema`.
|
|
1097
|
+
*
|
|
1098
|
+
* @param {object} [params={}]
|
|
1099
|
+
* @param {string} [params.toolName='Read']
|
|
1100
|
+
* @param {string} [params.filePath='']
|
|
1101
|
+
* @param {string} [params.command='']
|
|
1102
|
+
*/
|
|
1103
|
+
export function evaluateDet02SecretBoundary(params = {}) {
|
|
1104
|
+
const candidates = [];
|
|
1105
|
+
if (params.filePath) candidates.push(String(params.filePath));
|
|
1106
|
+
if (params.path) candidates.push(String(params.path));
|
|
1107
|
+
|
|
1108
|
+
// If shell command, extract any `.env*` or `.pem`/`.ssh`/`.aws` tokens referenced
|
|
1109
|
+
if (params.command && SHELL_TOOL_REGEX.test(String(params.toolName || 'Bash'))) {
|
|
1110
|
+
const tokens = tokenizeShellSegment(String(params.command));
|
|
1111
|
+
for (const tok of tokens) {
|
|
1112
|
+
if (
|
|
1113
|
+
tok.includes('.env') ||
|
|
1114
|
+
tok.includes('.pem') ||
|
|
1115
|
+
tok.includes('.ssh') ||
|
|
1116
|
+
tok.includes('.aws') ||
|
|
1117
|
+
tok.includes('id_rsa') ||
|
|
1118
|
+
tok.includes('id_ed25519')
|
|
1119
|
+
) {
|
|
1120
|
+
candidates.push(tok);
|
|
1121
|
+
}
|
|
1122
|
+
}
|
|
1123
|
+
}
|
|
1124
|
+
|
|
1125
|
+
for (const rawTarget of candidates) {
|
|
1126
|
+
const norm = String(rawTarget || '').trim().replace(/\\/g, '/');
|
|
1127
|
+
if (!norm) continue;
|
|
1128
|
+
|
|
1129
|
+
// Explicit allowlist for safe template files
|
|
1130
|
+
if (SAFE_ENV_ALLOWLIST_REGEX.test(norm)) {
|
|
1131
|
+
continue;
|
|
1132
|
+
}
|
|
1133
|
+
|
|
1134
|
+
for (const pattern of SECRET_FILE_PATTERNS) {
|
|
1135
|
+
if (pattern.test(norm)) {
|
|
1136
|
+
return {
|
|
1137
|
+
detectorId: 'DET-02',
|
|
1138
|
+
triggered: true,
|
|
1139
|
+
decision: 'deny',
|
|
1140
|
+
targetPath: norm,
|
|
1141
|
+
reason: `[DET-02 Secret & Credential Boundary] Access to sensitive credential path "${norm}" is blocked. (.env.example / .env.sample are permitted.)`
|
|
1142
|
+
};
|
|
1143
|
+
}
|
|
1144
|
+
}
|
|
1145
|
+
}
|
|
1146
|
+
|
|
1147
|
+
return { detectorId: 'DET-02', triggered: false, decision: 'allow', reason: null };
|
|
1148
|
+
}
|
|
1149
|
+
|
|
1150
|
+
/**
|
|
1151
|
+
* DET-03: Protected Migration, Lockfile & Workflow Ask Gate.
|
|
1152
|
+
* Triggers `"ask"` on manual `Edit`/`Write` to `migrations/*`, `package-lock.json`, `Cargo.lock`,
|
|
1153
|
+
* `.github/workflows/*`. Does NOT block automated `npm install` via `Bash|PowerShell`.
|
|
1154
|
+
*
|
|
1155
|
+
* @param {object} [params={}]
|
|
1156
|
+
* @param {string} [params.toolName='Edit']
|
|
1157
|
+
* @param {string} [params.filePath='']
|
|
1158
|
+
*/
|
|
1159
|
+
export function evaluateDet03ProtectedFilesAskGate(params = {}) {
|
|
1160
|
+
const toolName = String(params.toolName || '');
|
|
1161
|
+
// Only gate manual file Edit/Write tools; never block npm install / cargo via Bash/PowerShell
|
|
1162
|
+
if (!EDIT_WRITE_TOOL_REGEX.test(toolName)) {
|
|
1163
|
+
return { detectorId: 'DET-03', triggered: false, decision: 'allow', reason: null };
|
|
1164
|
+
}
|
|
1165
|
+
|
|
1166
|
+
const filePath = String(params.filePath || params.path || '').trim().replace(/\\/g, '/');
|
|
1167
|
+
if (!filePath) {
|
|
1168
|
+
return { detectorId: 'DET-03', triggered: false, decision: 'allow', reason: null };
|
|
1169
|
+
}
|
|
1170
|
+
|
|
1171
|
+
for (const pattern of PROTECTED_EDIT_PATTERNS) {
|
|
1172
|
+
if (pattern.test(filePath)) {
|
|
1173
|
+
return {
|
|
1174
|
+
detectorId: 'DET-03',
|
|
1175
|
+
triggered: true,
|
|
1176
|
+
decision: 'ask',
|
|
1177
|
+
targetPath: filePath,
|
|
1178
|
+
reason: `[DET-03 Protected File Ask Gate] Direct manual ${toolName} to "${filePath}" requires confirmation (use package manager CLI for lockfiles).`
|
|
1179
|
+
};
|
|
1180
|
+
}
|
|
1181
|
+
}
|
|
1182
|
+
|
|
1183
|
+
return { detectorId: 'DET-03', triggered: false, decision: 'allow', reason: null };
|
|
1184
|
+
}
|