@openai/codex-security 0.8.1 → 0.8.3
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/README.md +45 -57
- package/_bundled_plugin/.app.json +4 -0
- package/_bundled_plugin/.mcp.json +1 -0
- package/_bundled_plugin/mcp/helpers.mjs.br.part-000 +0 -0
- package/_bundled_plugin/mcp/helpers.mjs.br.part-001 +0 -0
- package/_bundled_plugin/mcp/native/THIRD_PARTY_NOTICES.txt +4 -4
- package/_bundled_plugin/mcp/native/darwin-arm64/unix.node +0 -0
- package/_bundled_plugin/mcp/native/darwin-x64/unix.node +0 -0
- package/_bundled_plugin/mcp/native/linux-arm64-gnu/unix.node +0 -0
- package/_bundled_plugin/mcp/native/linux-arm64-musl/unix.node +0 -0
- package/_bundled_plugin/mcp/native/linux-x64-gnu/unix.node +0 -0
- package/_bundled_plugin/mcp/native/linux-x64-musl/unix.node +0 -0
- package/_bundled_plugin/mcp/native/win32-arm64/windows.node +0 -0
- package/_bundled_plugin/mcp/native/win32-x64/windows.node +0 -0
- package/_bundled_plugin/mcp/permission-profile-preflight.mjs +46 -12
- package/_bundled_plugin/mcp/server.mjs.br.part-000 +0 -0
- package/_bundled_plugin/mcp/server.mjs.br.part-001 +0 -0
- package/_bundled_plugin/mcp/server.mjs.br.part-002 +0 -0
- package/_bundled_plugin/references/artifact-storage.md +1 -1
- package/_bundled_plugin/references/core-scan.md +14 -5
- package/_bundled_plugin/references/default-severity-rubric.md +28 -0
- package/_bundled_plugin/references/finding-detail-fields.md +1 -0
- package/_bundled_plugin/references/threat-model.md +1 -0
- package/_bundled_plugin/scripts/deep_scan_workbench.py +23 -112
- package/_bundled_plugin/scripts/finalize_scan_contract.py +167 -153
- package/_bundled_plugin/scripts/finding_preview.py +5 -29
- package/_bundled_plugin/scripts/posix_scan_local_files.py +187 -0
- package/_bundled_plugin/scripts/report_projection.py +87 -136
- package/_bundled_plugin/scripts/validate_scan_contract.py +2 -6
- package/_bundled_plugin/scripts/windows_scan_local_files.py +212 -55
- package/_bundled_plugin/scripts/workbench/retained_coverage.py +8 -24
- package/_bundled_plugin/scripts/workbench/saved_coverage.py +143 -231
- package/_bundled_plugin/scripts/workbench_cli.py +102 -13
- package/_bundled_plugin/scripts/workbench_constants.py +9 -0
- package/_bundled_plugin/scripts/workbench_coverage_receipts.py +0 -17
- package/_bundled_plugin/scripts/workbench_db.py +187 -363
- package/_bundled_plugin/scripts/workbench_finding_workflows.py +86 -4
- package/_bundled_plugin/scripts/workbench_native_indexes.py +17 -21
- package/_bundled_plugin/scripts/workbench_remediation.py +495 -11
- package/_bundled_plugin/scripts/workbench_saved_results.py +0 -66
- package/_bundled_plugin/scripts/workbench_scan_history.py +22 -25
- package/_bundled_plugin/scripts/workbench_scan_start.py +1 -1
- package/_bundled_plugin/scripts/workbench_scan_usage.py +25 -52
- package/_bundled_plugin/scripts/workbench_schema.py +1 -1
- package/_bundled_plugin/scripts/workbench_setup.py +239 -0
- package/_bundled_plugin/scripts/workbench_source_scopes.py +15 -23
- package/_bundled_plugin/scripts/workbench_target.py +120 -35
- package/_bundled_plugin/scripts/workbench_validation.py +1 -9
- package/_bundled_plugin/shared/workbench-history-repairs.json +7 -0
- package/_bundled_plugin/shared/workbench-migrations.json +19 -0
- package/_bundled_plugin/skills/attack-path-analysis/SKILL.md +1 -1
- package/_bundled_plugin/skills/attack-path-analysis/references/severity-policy.md +4 -68
- package/_bundled_plugin/skills/check-skills/SKILL.md +43 -0
- package/_bundled_plugin/skills/check-skills/agents/openai.yaml +4 -0
- package/_bundled_plugin/skills/cloud-findings/SKILL.md +2 -0
- package/_bundled_plugin/skills/fix-finding/SKILL.md +18 -1
- package/_bundled_plugin/skills/publish-findings/SKILL.md +68 -35
- package/_bundled_plugin/skills/publish-findings/references/wiz-import.md +2 -21
- package/_bundled_plugin/skills/resolve-dependency-alert/SKILL.md +18 -0
- package/_bundled_plugin/skills/resolve-dependency-alert/agents/openai.yaml +4 -0
- package/_bundled_plugin/skills/threat-model-visualize/SKILL.md +83 -0
- package/_bundled_plugin/skills/threat-model-visualize/agents/openai.yaml +4 -0
- package/_bundled_plugin/skills/threat-model-visualize/assets/context-reference-overview.svg +366 -0
- package/_bundled_plugin/skills/threat-model-visualize/assets/context-reference-path.svg +558 -0
- package/_bundled_plugin/skills/threat-model-visualize/assets/control-effects.js +85 -0
- package/_bundled_plugin/skills/threat-model-visualize/assets/explorer.css +17 -0
- package/_bundled_plugin/skills/threat-model-visualize/assets/explorer.html +38 -0
- package/_bundled_plugin/skills/threat-model-visualize/assets/explorer.js +372 -0
- package/_bundled_plugin/skills/threat-model-visualize/assets/route-paths.js +71 -0
- package/_bundled_plugin/skills/threat-model-visualize/assets/source-roles.js +47 -0
- package/_bundled_plugin/skills/threat-model-visualize/assets/svg-layout.js +149 -0
- package/_bundled_plugin/skills/threat-model-visualize/references/context-explorer.md +21 -0
- package/_bundled_plugin/skills/threat-model-visualize/references/view-model.md +49 -0
- package/_bundled_plugin/skills/threat-model-visualize/scripts/build_explorer.py +569 -0
- package/_bundled_plugin/skills/track-findings/SKILL.md +5 -3
- package/_bundled_plugin/skills/triage-finding/references/github-rest-intake.md +22 -68
- package/_bundled_plugin/skills/triage-finding/references/ticket-intake.md +6 -4
- package/dist/api.d.ts +6 -1
- package/dist/api.d.ts.map +1 -1
- package/dist/api.js +414 -163
- package/dist/api.js.map +1 -1
- package/dist/auth.js +7 -2
- package/dist/auth.js.map +1 -1
- package/dist/blackduck-findings.d.ts +8 -0
- package/dist/blackduck-findings.d.ts.map +1 -0
- package/dist/blackduck-findings.js +305 -0
- package/dist/blackduck-findings.js.map +1 -0
- package/dist/bulk-scan-discovery.d.ts.map +1 -1
- package/dist/bulk-scan-discovery.js +51 -36
- package/dist/bulk-scan-discovery.js.map +1 -1
- package/dist/classify-severity-records.d.ts +5 -0
- package/dist/classify-severity-records.d.ts.map +1 -1
- package/dist/classify-severity-records.js +6 -8
- package/dist/classify-severity-records.js.map +1 -1
- package/dist/classify-severity.d.ts +4 -1
- package/dist/classify-severity.d.ts.map +1 -1
- package/dist/classify-severity.js +28 -9
- package/dist/classify-severity.js.map +1 -1
- package/dist/cli.d.ts +3 -0
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +635 -266
- package/dist/cli.js.map +1 -1
- package/dist/codex-prompt.d.ts +2 -0
- package/dist/codex-prompt.d.ts.map +1 -1
- package/dist/codex-prompt.js +6 -0
- package/dist/codex-prompt.js.map +1 -1
- package/dist/component-scan.d.ts.map +1 -1
- package/dist/component-scan.js +6 -5
- package/dist/component-scan.js.map +1 -1
- package/dist/config.d.ts +2 -1
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +162 -40
- package/dist/config.js.map +1 -1
- package/dist/continuum-findings.d.ts +7 -0
- package/dist/continuum-findings.d.ts.map +1 -0
- package/dist/continuum-findings.js +157 -0
- package/dist/continuum-findings.js.map +1 -0
- package/dist/contract-values.d.ts +28 -0
- package/dist/contract-values.d.ts.map +1 -0
- package/dist/contract-values.js +75 -0
- package/dist/contract-values.js.map +1 -0
- package/dist/contract.d.ts +2 -20
- package/dist/contract.d.ts.map +1 -1
- package/dist/contract.js +58 -669
- package/dist/contract.js.map +1 -1
- package/dist/cost-model.d.ts.map +1 -1
- package/dist/cost-model.js +1 -4
- package/dist/cost-model.js.map +1 -1
- package/dist/cost.d.ts +19 -1
- package/dist/cost.d.ts.map +1 -1
- package/dist/cost.js +2120 -39
- package/dist/cost.js.map +1 -1
- package/dist/custom-validation-prompt.d.ts +1 -1
- package/dist/custom-validation-prompt.d.ts.map +1 -1
- package/dist/custom-validation-prompt.js +5 -0
- package/dist/custom-validation-prompt.js.map +1 -1
- package/dist/custom-validation.d.ts +2 -0
- package/dist/custom-validation.d.ts.map +1 -1
- package/dist/custom-validation.js +6 -2
- package/dist/custom-validation.js.map +1 -1
- package/dist/deduplication/deduplication-prompts.d.ts +4 -4
- package/dist/deduplication/deduplication-prompts.d.ts.map +1 -1
- package/dist/deduplication/deduplication-prompts.js +2 -2
- package/dist/deduplication/deduplication-prompts.js.map +1 -1
- package/dist/deduplication/deduplication-reviewer.d.ts +14 -11
- package/dist/deduplication/deduplication-reviewer.d.ts.map +1 -1
- package/dist/deduplication/deduplication-reviewer.js +12 -5
- package/dist/deduplication/deduplication-reviewer.js.map +1 -1
- package/dist/deduplication/deduplication.js.map +1 -1
- package/dist/deduplication/finding-schema.d.ts +19 -1
- package/dist/deduplication/finding-schema.d.ts.map +1 -1
- package/dist/deduplication/finding-schema.js +25 -0
- package/dist/deduplication/finding-schema.js.map +1 -1
- package/dist/deduplication/local.d.ts.map +1 -1
- package/dist/deduplication/local.js +3 -8
- package/dist/deduplication/local.js.map +1 -1
- package/dist/deduplication/records.d.ts +11 -3
- package/dist/deduplication/records.d.ts.map +1 -1
- package/dist/deduplication/records.js +48 -16
- package/dist/deduplication/records.js.map +1 -1
- package/dist/deep-scan.d.ts +11 -0
- package/dist/deep-scan.d.ts.map +1 -1
- package/dist/deep-scan.js +235 -4
- package/dist/deep-scan.js.map +1 -1
- package/dist/default-severity-rubric.d.ts +1 -2
- package/dist/default-severity-rubric.d.ts.map +1 -1
- package/dist/default-severity-rubric.js +2 -66
- package/dist/default-severity-rubric.js.map +1 -1
- package/dist/endor-findings.d.ts.map +1 -1
- package/dist/endor-findings.js +44 -89
- package/dist/endor-findings.js.map +1 -1
- package/dist/errors.js +1 -1
- package/dist/errors.js.map +1 -1
- package/dist/external-findings-import.d.ts.map +1 -1
- package/dist/external-findings-import.js +11 -15
- package/dist/external-findings-import.js.map +1 -1
- package/dist/external-findings-local.d.ts +12 -1
- package/dist/external-findings-local.d.ts.map +1 -1
- package/dist/external-findings-local.js +411 -184
- package/dist/external-findings-local.js.map +1 -1
- package/dist/external-findings-publish.d.ts +15 -1
- package/dist/external-findings-publish.d.ts.map +1 -1
- package/dist/external-findings-publish.js +1238 -226
- package/dist/external-findings-publish.js.map +1 -1
- package/dist/external-findings-read.d.ts +10 -0
- package/dist/external-findings-read.d.ts.map +1 -1
- package/dist/external-findings-read.js +71 -51
- package/dist/external-findings-read.js.map +1 -1
- package/dist/external-findings.d.ts +13 -0
- package/dist/external-findings.d.ts.map +1 -1
- package/dist/external-findings.js +124 -1
- package/dist/external-findings.js.map +1 -1
- package/dist/external-import-contract.d.ts +29 -1
- package/dist/external-import-contract.d.ts.map +1 -1
- package/dist/external-import-contract.js +314 -44
- package/dist/external-import-contract.js.map +1 -1
- package/dist/external-import-models.d.ts +1 -1
- package/dist/external-import-models.d.ts.map +1 -1
- package/dist/external-import-models.js.map +1 -1
- package/dist/finding-normalization.d.ts +1 -1
- package/dist/finding-normalization.d.ts.map +1 -1
- package/dist/finding-normalization.js +46 -11
- package/dist/finding-normalization.js.map +1 -1
- package/dist/finding-retrieval.d.ts +3 -3
- package/dist/finding-retrieval.d.ts.map +1 -1
- package/dist/finding-retrieval.js.map +1 -1
- package/dist/finding-workflow.d.ts +6 -1
- package/dist/finding-workflow.d.ts.map +1 -1
- package/dist/finding-workflow.js +2 -2
- package/dist/finding-workflow.js.map +1 -1
- package/dist/git-remote.d.ts +4 -0
- package/dist/git-remote.d.ts.map +1 -1
- package/dist/git-remote.js +20 -0
- package/dist/git-remote.js.map +1 -1
- package/dist/github-advisories.d.ts +3 -12
- package/dist/github-advisories.d.ts.map +1 -1
- package/dist/github-advisories.js +9 -29
- package/dist/github-advisories.js.map +1 -1
- package/dist/github-dependabot-findings.d.ts.map +1 -1
- package/dist/github-dependabot-findings.js +7 -16
- package/dist/github-dependabot-findings.js.map +1 -1
- package/dist/github-findings.d.ts.map +1 -1
- package/dist/github-findings.js +344 -56
- package/dist/github-findings.js.map +1 -1
- package/dist/github.d.ts +17 -7
- package/dist/github.d.ts.map +1 -1
- package/dist/github.js +43 -30
- package/dist/github.js.map +1 -1
- package/dist/host-protocol.d.ts +7 -2
- package/dist/host-protocol.d.ts.map +1 -1
- package/dist/host-protocol.js +27 -3
- package/dist/host-protocol.js.map +1 -1
- package/dist/host-records-protocol.d.ts +10 -11
- package/dist/host-records-protocol.d.ts.map +1 -1
- package/dist/host-records-protocol.js +69 -9
- package/dist/host-records-protocol.js.map +1 -1
- package/dist/hosted-scan.d.ts +15 -0
- package/dist/hosted-scan.d.ts.map +1 -1
- package/dist/hosted-scan.js +55 -33
- package/dist/hosted-scan.js.map +1 -1
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +7 -0
- package/dist/index.js.map +1 -1
- package/dist/inspector-findings.d.ts +9 -0
- package/dist/inspector-findings.d.ts.map +1 -0
- package/dist/inspector-findings.js +512 -0
- package/dist/inspector-findings.js.map +1 -0
- package/dist/knowledge-base.js +18 -5
- package/dist/knowledge-base.js.map +1 -1
- package/dist/local-git-metadata.d.ts +5 -0
- package/dist/local-git-metadata.d.ts.map +1 -0
- package/dist/local-git-metadata.js +131 -0
- package/dist/local-git-metadata.js.map +1 -0
- package/dist/multiscan.d.ts.map +1 -1
- package/dist/multiscan.js +22 -12
- package/dist/multiscan.js.map +1 -1
- package/dist/opengrep-findings.d.ts.map +1 -1
- package/dist/opengrep-findings.js +34 -20
- package/dist/opengrep-findings.js.map +1 -1
- package/dist/patch-tui.d.ts +1 -1
- package/dist/patch-tui.d.ts.map +1 -1
- package/dist/patch-tui.js +94 -36
- package/dist/patch-tui.js.map +1 -1
- package/dist/private-finding-import.d.ts +246 -0
- package/dist/private-finding-import.d.ts.map +1 -0
- package/dist/private-finding-import.js +387 -0
- package/dist/private-finding-import.js.map +1 -0
- package/dist/project-config.d.ts.map +1 -1
- package/dist/project-config.js +2 -2
- package/dist/project-config.js.map +1 -1
- package/dist/provider-profile.d.ts +10 -2
- package/dist/provider-profile.d.ts.map +1 -1
- package/dist/provider-profile.js +72 -10
- package/dist/provider-profile.js.map +1 -1
- package/dist/publication.js +7 -1
- package/dist/publication.js.map +1 -1
- package/dist/reader-selection.d.ts +13 -0
- package/dist/reader-selection.d.ts.map +1 -0
- package/dist/reader-selection.js +81 -0
- package/dist/reader-selection.js.map +1 -0
- package/dist/record.d.ts +6 -1
- package/dist/record.d.ts.map +1 -1
- package/dist/record.js +53 -2
- package/dist/record.js.map +1 -1
- package/dist/remediation.d.ts +5 -0
- package/dist/remediation.d.ts.map +1 -1
- package/dist/remediation.js +30 -0
- package/dist/remediation.js.map +1 -1
- package/dist/result.d.ts +2 -0
- package/dist/result.d.ts.map +1 -1
- package/dist/result.js +3 -1
- package/dist/result.js.map +1 -1
- package/dist/runtime.d.ts +5 -3
- package/dist/runtime.d.ts.map +1 -1
- package/dist/runtime.js +110 -57
- package/dist/runtime.js.map +1 -1
- package/dist/scan-activity.js +40 -17
- package/dist/scan-activity.js.map +1 -1
- package/dist/scan-comparison.d.ts +1 -1
- package/dist/scan-comparison.d.ts.map +1 -1
- package/dist/scan-comparison.js +11 -13
- package/dist/scan-comparison.js.map +1 -1
- package/dist/scan-dashboard.d.ts +1 -0
- package/dist/scan-dashboard.d.ts.map +1 -1
- package/dist/scan-dashboard.js +37 -48
- package/dist/scan-dashboard.js.map +1 -1
- package/dist/scan-executor.d.ts +2 -0
- package/dist/scan-executor.d.ts.map +1 -1
- package/dist/scan-executor.js.map +1 -1
- package/dist/scan-history-renderer.d.ts.map +1 -1
- package/dist/scan-history-renderer.js +22 -9
- package/dist/scan-history-renderer.js.map +1 -1
- package/dist/scan-logs.d.ts.map +1 -1
- package/dist/scan-logs.js +5 -5
- package/dist/scan-logs.js.map +1 -1
- package/dist/scanner-import.d.ts +48 -3
- package/dist/scanner-import.d.ts.map +1 -1
- package/dist/scanner-import.js +214 -39
- package/dist/scanner-import.js.map +1 -1
- package/dist/scanner-repository-url.d.ts +14 -0
- package/dist/scanner-repository-url.d.ts.map +1 -0
- package/dist/scanner-repository-url.js +67 -0
- package/dist/scanner-repository-url.js.map +1 -0
- package/dist/semgrep-findings.d.ts.map +1 -1
- package/dist/semgrep-findings.js +50 -88
- package/dist/semgrep-findings.js.map +1 -1
- package/dist/semgrep-platform-findings.d.ts +12 -0
- package/dist/semgrep-platform-findings.d.ts.map +1 -0
- package/dist/semgrep-platform-findings.js +198 -0
- package/dist/semgrep-platform-findings.js.map +1 -0
- package/dist/severity-rubric.d.ts +20 -0
- package/dist/severity-rubric.d.ts.map +1 -0
- package/dist/severity-rubric.js +73 -0
- package/dist/severity-rubric.js.map +1 -0
- package/dist/severity-store.js +15 -5
- package/dist/severity-store.js.map +1 -1
- package/dist/snyk-findings.d.ts.map +1 -1
- package/dist/snyk-findings.js +97 -106
- package/dist/snyk-findings.js.map +1 -1
- package/dist/sonar-collector.d.ts +16 -0
- package/dist/sonar-collector.d.ts.map +1 -0
- package/dist/sonar-collector.js +383 -0
- package/dist/sonar-collector.js.map +1 -0
- package/dist/sonar-findings.d.ts +49 -0
- package/dist/sonar-findings.d.ts.map +1 -0
- package/dist/sonar-findings.js +288 -0
- package/dist/sonar-findings.js.map +1 -0
- package/dist/sonar-import.d.ts +111 -0
- package/dist/sonar-import.d.ts.map +1 -0
- package/dist/sonar-import.js +279 -0
- package/dist/sonar-import.js.map +1 -0
- package/dist/suggest-owners.d.ts.map +1 -1
- package/dist/suggest-owners.js +2 -1
- package/dist/suggest-owners.js.map +1 -1
- package/dist/targets.d.ts +3 -0
- package/dist/targets.d.ts.map +1 -1
- package/dist/targets.js +18 -12
- package/dist/targets.js.map +1 -1
- package/dist/terminal-columns.d.ts +4 -0
- package/dist/terminal-columns.d.ts.map +1 -0
- package/dist/terminal-columns.js +19 -0
- package/dist/terminal-columns.js.map +1 -0
- package/dist/validation-records.d.ts +0 -3
- package/dist/validation-records.d.ts.map +1 -1
- package/dist/validation-records.js +0 -16
- package/dist/validation-records.js.map +1 -1
- package/dist/veracode-findings.d.ts +7 -0
- package/dist/veracode-findings.d.ts.map +1 -0
- package/dist/veracode-findings.js +371 -0
- package/dist/veracode-findings.js.map +1 -0
- package/dist/windows-path.d.ts.map +1 -1
- package/dist/windows-path.js +1 -0
- package/dist/windows-path.js.map +1 -1
- package/dist/wiz-csv.d.ts +2 -2
- package/dist/wiz-csv.d.ts.map +1 -1
- package/dist/wiz-csv.js +1 -3
- package/dist/wiz-csv.js.map +1 -1
- package/dist/wiz-findings.d.ts +10 -2
- package/dist/wiz-findings.d.ts.map +1 -1
- package/dist/wiz-findings.js +88 -49
- package/dist/wiz-findings.js.map +1 -1
- package/docs/classify-severity-records.md +1 -1
- package/docs/cli.md +320 -481
- package/docs/dedupe-records.md +27 -4
- package/docs/findings-service.md +21 -8
- package/docs/hosted-scans.md +16 -1
- package/package.json +9 -10
- package/schemas/external-findings.schema.json +12 -1
- package/schemas/hosted-scan-v2.fixture.json +16 -1
- package/_bundled_plugin/scripts/validate_tracking_source.py +0 -82
- package/_bundled_plugin/scripts/workbench_local_dedupe.py +0 -136
- package/dist/deduplication/records-protocol.d.ts +0 -4
- package/dist/deduplication/records-protocol.d.ts.map +0 -1
- package/dist/deduplication/records-protocol.js +0 -35
- package/dist/deduplication/records-protocol.js.map +0 -1
|
@@ -3,16 +3,36 @@
|
|
|
3
3
|
from __future__ import annotations
|
|
4
4
|
|
|
5
5
|
import argparse
|
|
6
|
+
import codecs
|
|
7
|
+
import hashlib
|
|
8
|
+
import io
|
|
9
|
+
import os
|
|
10
|
+
import re
|
|
6
11
|
import sqlite3
|
|
7
12
|
import sys
|
|
13
|
+
from collections.abc import Callable
|
|
14
|
+
from contextlib import ExitStack, suppress
|
|
8
15
|
from datetime import datetime, timedelta, timezone
|
|
9
16
|
from pathlib import Path
|
|
10
|
-
from typing import Any
|
|
17
|
+
from typing import Any, BinaryIO
|
|
11
18
|
|
|
12
19
|
# Some plugin hosts launch Python with safe-path isolation enabled.
|
|
13
20
|
sys.path.insert(0, str(Path(__file__).resolve().parent))
|
|
21
|
+
from finalize_scan_contract import (
|
|
22
|
+
ContractError,
|
|
23
|
+
MissingScanLocalFileError,
|
|
24
|
+
_open_scan_local_directory,
|
|
25
|
+
_open_verified_scan_directory,
|
|
26
|
+
_read_scan_local_json_bytes,
|
|
27
|
+
_read_sealed_scan,
|
|
28
|
+
scan_local_file_version,
|
|
29
|
+
scan_root_identity,
|
|
30
|
+
stage_scan_local_bytes,
|
|
31
|
+
)
|
|
32
|
+
from posix_scan_local_files import managed_fd
|
|
14
33
|
from workbench_constants import CLAIM_LEASE_SECONDS, DELIVERED_ACTION_LEASE_SECONDS
|
|
15
|
-
from
|
|
34
|
+
from workbench_target import git_command, git_output
|
|
35
|
+
from workbench_validation import optional_text, require_occurrence, require_uuid
|
|
16
36
|
|
|
17
37
|
COMMANDS = {
|
|
18
38
|
"request-finding-remediation",
|
|
@@ -46,6 +66,478 @@ def require_request(
|
|
|
46
66
|
return current
|
|
47
67
|
|
|
48
68
|
|
|
69
|
+
def latest_attempt(connection: sqlite3.Connection, occurrence_id: str) -> sqlite3.Row | None:
|
|
70
|
+
return connection.execute(
|
|
71
|
+
"""
|
|
72
|
+
SELECT * FROM finding_remediation_attempts
|
|
73
|
+
WHERE occurrence_id = ?
|
|
74
|
+
ORDER BY created_at DESC, rowid DESC
|
|
75
|
+
LIMIT 1
|
|
76
|
+
""",
|
|
77
|
+
(occurrence_id,),
|
|
78
|
+
).fetchone()
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
_HUNK_HEADER = re.compile(rb"@@ -[0-9]+(?:,([0-9]+))? \+[0-9]+(?:,([0-9]+))? @@")
|
|
82
|
+
_QUOTED_GIT_PATH = rb'"((?:[^"\\]|\\(?:[abfnrtv"\\]|[0-3][0-7]{2}))*)"'
|
|
83
|
+
_SUBMODULE_SUMMARY_END = re.compile(
|
|
84
|
+
rb" (?:contains (?:modified|untracked) content|"
|
|
85
|
+
rb"[0-9a-f]+\.{2,3}[0-9a-f]+(?: \([a-z ]+\):?|:))$"
|
|
86
|
+
)
|
|
87
|
+
_SUBMODULE_ERROR = (
|
|
88
|
+
"A remediation patch cannot update Git submodules. Regenerate without gitlink changes."
|
|
89
|
+
)
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
def _git_path(field: bytes) -> bytes:
|
|
93
|
+
if quoted := re.match(_QUOTED_GIT_PATH + rb"(?:\t|$)", field):
|
|
94
|
+
return codecs.escape_decode(quoted[1])[0]
|
|
95
|
+
return field.partition(b"\t")[0]
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
def _matches_git_paths(header: bytes, old_field: bytes, new_field: bytes) -> bool:
|
|
99
|
+
old, new = _git_path(old_field), _git_path(new_field)
|
|
100
|
+
# Git strips one component from each path. Unquoted paths can contain
|
|
101
|
+
# spaces, so use the known preimage to locate the separator.
|
|
102
|
+
if quoted := re.match(_QUOTED_GIT_PATH + rb"(?= )", header):
|
|
103
|
+
split = quoted.end()
|
|
104
|
+
elif quoted := re.search(rb" " + _QUOTED_GIT_PATH + rb"$", header):
|
|
105
|
+
split = quoted.start()
|
|
106
|
+
elif old == b"/dev/null" and header.endswith(b" " + new):
|
|
107
|
+
split = len(header) - len(new) - 1
|
|
108
|
+
elif header.count(b" ") == 1:
|
|
109
|
+
split = header.index(b" ")
|
|
110
|
+
else:
|
|
111
|
+
# On a creation Git repeats the destination on the left of the header.
|
|
112
|
+
expected = new if old == b"/dev/null" else old
|
|
113
|
+
prefix, slash, suffix = expected.partition(b"/")
|
|
114
|
+
split = header.find(b"/") + 1 + len(suffix) if slash else len(prefix)
|
|
115
|
+
if split < 0 or header[split : split + 1] != b" ":
|
|
116
|
+
return False
|
|
117
|
+
for path, field in ((header[:split], old), (header[split + 1 :], new)):
|
|
118
|
+
if field != b"/dev/null" and _git_path(path).split(b"/", 1)[-1] != field.split(b"/", 1)[-1]:
|
|
119
|
+
return False
|
|
120
|
+
return True
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
def _patch_file_sections(patch: bytes, incomplete: str) -> tuple[bytes, int]:
|
|
124
|
+
mail = re.match(rb"From (?:[0-9a-f]{40}|[0-9a-f]{64}) Mon Sep 17 00:00:00 2001\r?\n", patch)
|
|
125
|
+
if mail:
|
|
126
|
+
first = re.search(rb"(?m)^diff --git [^\n]+\n", patch)
|
|
127
|
+
if first:
|
|
128
|
+
start = first.start()
|
|
129
|
+
# A commit message can quote a Git header. Look for format-patch's
|
|
130
|
+
# diffstat before the first hunk, where a deleted "--" can also
|
|
131
|
+
# look like the separator. Each run of indented lines is scanned once.
|
|
132
|
+
hunk = re.compile(rb"(?m)^@@ -").search(patch, start)
|
|
133
|
+
diffstat = re.compile(
|
|
134
|
+
rb"(?m)^---\r?\n(?: [^\n]*\n)+\r?\n(?=diff --git |Submodule )"
|
|
135
|
+
).search(patch, 0, hunk.start() if hunk else len(patch))
|
|
136
|
+
if diffstat:
|
|
137
|
+
start = diffstat.end()
|
|
138
|
+
else:
|
|
139
|
+
# With --no-stat Git can retain a quoted header as the preimage of
|
|
140
|
+
# the first change. Ignore it only for the same in-place file edit.
|
|
141
|
+
file = re.compile(
|
|
142
|
+
rb"(?m)^diff --git [^\n]+\n(?=index |--- |@@ |old mode|new mode|new file|deleted file"
|
|
143
|
+
rb"|(?:dis)?similarity index|(?:rename|copy) |GIT binary patch|Binary files |Files |Only in )"
|
|
144
|
+
).search(patch, start)
|
|
145
|
+
if (
|
|
146
|
+
file
|
|
147
|
+
and file[0] == first[0]
|
|
148
|
+
and patch.endswith((b"\n\n", b"\n\r\n"), 0, file.start())
|
|
149
|
+
and re.compile(
|
|
150
|
+
rb"(?:index [0-9a-f]+\.\.[0-9a-f]+ 100(?:644|755)|"
|
|
151
|
+
rb"old mode 100(?:644|755)\r?\nnew mode 100(?:644|755)\r?\n"
|
|
152
|
+
rb"index [0-9a-f]+\.\.[0-9a-f]+)\r?\n(?:--- |GIT binary patch)"
|
|
153
|
+
).match(patch, file.end())
|
|
154
|
+
):
|
|
155
|
+
start = file.start()
|
|
156
|
+
elif not patch.endswith((b"\n\n", b"\n\r\n"), 0, start):
|
|
157
|
+
# A leading submodule summary runs directly into the next file header.
|
|
158
|
+
summary = re.compile(rb"(?m)^Submodule ").search(patch, 0, start)
|
|
159
|
+
if summary:
|
|
160
|
+
start = summary.start()
|
|
161
|
+
patch = patch[start:]
|
|
162
|
+
|
|
163
|
+
sections = old_lines = new_lines = 0
|
|
164
|
+
git_paths = b""
|
|
165
|
+
pending_paths = None
|
|
166
|
+
submodule_summary = False
|
|
167
|
+
expect_hunk = False
|
|
168
|
+
stream = io.BytesIO(patch)
|
|
169
|
+
for raw_line in stream:
|
|
170
|
+
line = raw_line.removesuffix(b"\n").removesuffix(b"\r")
|
|
171
|
+
if old_lines or new_lines:
|
|
172
|
+
if line.startswith(b"\\ "): # No newline at end of file
|
|
173
|
+
continue
|
|
174
|
+
prefix = line[:1]
|
|
175
|
+
if prefix in (b"", b" ", b"-"):
|
|
176
|
+
old_lines -= 1
|
|
177
|
+
if prefix in (b"", b" ", b"+"):
|
|
178
|
+
new_lines -= 1
|
|
179
|
+
if prefix not in (b"", b" ", b"+", b"-") or old_lines < 0 or new_lines < 0:
|
|
180
|
+
raise SystemExit(incomplete)
|
|
181
|
+
expect_hunk = True
|
|
182
|
+
continue
|
|
183
|
+
if expect_hunk and line.startswith(b"\\ "): # No newline at end of file
|
|
184
|
+
continue
|
|
185
|
+
hunk = _HUNK_HEADER.match(line)
|
|
186
|
+
if expect_hunk and not hunk and re.match(rb"@@ -(?:[0-9]|$)", line):
|
|
187
|
+
raise SystemExit(incomplete)
|
|
188
|
+
expect_hunk = bool(sections) and line.startswith(b"+++ ")
|
|
189
|
+
if mail and sections and line == b"-- ":
|
|
190
|
+
patch = patch[: stream.tell() - len(raw_line)]
|
|
191
|
+
break
|
|
192
|
+
# Git writes the raw submodule name (possibly including newlines) before the
|
|
193
|
+
# status or object range. Neither log nor inline-diff headers can be replayed.
|
|
194
|
+
if line.startswith(b"Submodule "):
|
|
195
|
+
submodule_summary = True
|
|
196
|
+
if submodule_summary and _SUBMODULE_SUMMARY_END.search(line):
|
|
197
|
+
raise SystemExit(_SUBMODULE_ERROR)
|
|
198
|
+
if pending_paths:
|
|
199
|
+
if line.startswith(b"+++ ") and not _matches_git_paths(*pending_paths, line[4:]):
|
|
200
|
+
sections += 1
|
|
201
|
+
pending_paths = None
|
|
202
|
+
if line.startswith(b"diff --git "):
|
|
203
|
+
sections += 1
|
|
204
|
+
submodule_summary = False
|
|
205
|
+
git_paths = line.removeprefix(b"diff --git ")
|
|
206
|
+
continue
|
|
207
|
+
if line.startswith(b"--- "):
|
|
208
|
+
submodule_summary = False
|
|
209
|
+
# A Git header can introduce the same unified path pair. An
|
|
210
|
+
# unrelated pair starts a separate file even if Git ignores one.
|
|
211
|
+
if git_paths:
|
|
212
|
+
pending_paths = (git_paths, line[4:])
|
|
213
|
+
else:
|
|
214
|
+
sections += 1
|
|
215
|
+
git_paths = b""
|
|
216
|
+
continue
|
|
217
|
+
if git_paths:
|
|
218
|
+
if line.startswith(
|
|
219
|
+
(b"index ", b"old mode ", b"new mode ", b"new file mode ", b"deleted file mode ")
|
|
220
|
+
):
|
|
221
|
+
if re.search(rb" 0*16[0-7]{4}(?:\s|$)", line):
|
|
222
|
+
raise SystemExit(_SUBMODULE_ERROR)
|
|
223
|
+
if not line.startswith(b"index ") and not re.fullmatch(rb".* [0-7]{6}[ \t]*", line):
|
|
224
|
+
raise SystemExit(incomplete)
|
|
225
|
+
continue
|
|
226
|
+
if line.startswith(
|
|
227
|
+
(
|
|
228
|
+
b"similarity index ",
|
|
229
|
+
b"dissimilarity index ",
|
|
230
|
+
b"rename from ",
|
|
231
|
+
b"rename to ",
|
|
232
|
+
b"copy from ",
|
|
233
|
+
b"copy to ",
|
|
234
|
+
)
|
|
235
|
+
):
|
|
236
|
+
continue
|
|
237
|
+
git_paths = b""
|
|
238
|
+
if hunk:
|
|
239
|
+
old_lines = int(hunk[1] or b"1")
|
|
240
|
+
new_lines = int(hunk[2] or b"1")
|
|
241
|
+
elif (
|
|
242
|
+
line.startswith((b"Binary files ", b"Files "))
|
|
243
|
+
and line.endswith(b" differ")
|
|
244
|
+
and b" and " in line
|
|
245
|
+
):
|
|
246
|
+
raise SystemExit(incomplete)
|
|
247
|
+
elif line.startswith(b"Only in ") and b": " in line:
|
|
248
|
+
raise SystemExit(incomplete)
|
|
249
|
+
if old_lines or new_lines:
|
|
250
|
+
raise SystemExit(incomplete)
|
|
251
|
+
return patch, sections
|
|
252
|
+
|
|
253
|
+
|
|
254
|
+
def require_valid_patch(target: Path, patch: bytes) -> None:
|
|
255
|
+
"""Reject incomplete patches before recording the submitted bytes."""
|
|
256
|
+
incomplete = (
|
|
257
|
+
"The remediation patch omits file content. Generate a complete binary diff "
|
|
258
|
+
"with Git's --binary option."
|
|
259
|
+
)
|
|
260
|
+
# Git's numstat skips target-relative paths when run below the repository root.
|
|
261
|
+
root = git_output(target, "rev-parse", "--show-toplevel", offline=True)
|
|
262
|
+
directory = ("-C", root) if root else ()
|
|
263
|
+
|
|
264
|
+
def git(data: bytes, *arguments: str):
|
|
265
|
+
result = git_command(
|
|
266
|
+
target,
|
|
267
|
+
*directory,
|
|
268
|
+
*arguments,
|
|
269
|
+
text=False,
|
|
270
|
+
input_data=data,
|
|
271
|
+
environment_overrides={"LC_ALL": "C", "GIT_NO_REPLACE_OBJECTS": "1"},
|
|
272
|
+
offline=True,
|
|
273
|
+
)
|
|
274
|
+
if result.returncode == 127:
|
|
275
|
+
raise SystemExit(
|
|
276
|
+
"Validating a new remediation patch requires a trusted Git executable."
|
|
277
|
+
)
|
|
278
|
+
# Git may report a corrupt trailing binary block but return zero for an earlier file.
|
|
279
|
+
if result.returncode or re.search(rb"(?m)^error: ", result.stderr):
|
|
280
|
+
reason = result.stderr.decode(errors="replace").strip()
|
|
281
|
+
raise SystemExit(f"Could not parse the remediation patch: {reason}")
|
|
282
|
+
return result.stdout
|
|
283
|
+
|
|
284
|
+
# Parse every header and hunk, then skip checkout reads and repository clean filters.
|
|
285
|
+
apply = ("apply", "--no-index", "--whitespace=nowarn")
|
|
286
|
+
body, sections = _patch_file_sections(patch, incomplete)
|
|
287
|
+
git(patch, *apply, "--check", "--exclude=*", "-")
|
|
288
|
+
# Git silently ignores some incomplete sections when another file is valid.
|
|
289
|
+
# NUL termination gives one record per file, even for unusual pathnames.
|
|
290
|
+
if git(patch, *apply, "--numstat", "-z", "-").count(b"\0") != sections:
|
|
291
|
+
raise SystemExit(incomplete)
|
|
292
|
+
|
|
293
|
+
binary_oid_width = None
|
|
294
|
+
for header in re.finditer(
|
|
295
|
+
rb"(?m)^diff --git [^\n]+\n((?:(?!diff )[a-z][^\n]*(?:\n|\Z))*)",
|
|
296
|
+
body,
|
|
297
|
+
):
|
|
298
|
+
metadata = header.group(1)
|
|
299
|
+
index = re.search(rb"(?m)^index ([^\s.]+)\.\.([^\s.]+)(?: [0-7]+[ \t]*)?\r?$", metadata)
|
|
300
|
+
binary = body.startswith((b"GIT binary patch\n", b"GIT binary patch\r\n"), header.end())
|
|
301
|
+
if not binary:
|
|
302
|
+
if index is None or re.compile(rb"(?:--- [^\n]*\n\+\+\+ [^\n]*\n)?@@ ").match(
|
|
303
|
+
body, header.end()
|
|
304
|
+
):
|
|
305
|
+
continue
|
|
306
|
+
old, new = index.groups()
|
|
307
|
+
if metadata.startswith(b"deleted file mode ") and not new.strip(b"0"):
|
|
308
|
+
content_id = old
|
|
309
|
+
elif metadata.startswith(b"new file mode ") and not old.strip(b"0"):
|
|
310
|
+
content_id = new
|
|
311
|
+
else:
|
|
312
|
+
# Mode changes and renames can omit hunks only if
|
|
313
|
+
# their advertised blob contents are unchanged.
|
|
314
|
+
old, new = old.lower(), new.lower()
|
|
315
|
+
if not old.startswith(new) and not new.startswith(old):
|
|
316
|
+
raise SystemExit(incomplete)
|
|
317
|
+
continue
|
|
318
|
+
# Only an empty file can be added or deleted without a hunk.
|
|
319
|
+
empty_blobs = (
|
|
320
|
+
hashlib.sha1(b"blob 0\0", usedforsecurity=False).hexdigest().encode(),
|
|
321
|
+
hashlib.sha256(b"blob 0\0").hexdigest().encode(),
|
|
322
|
+
)
|
|
323
|
+
if not any(digest.startswith(content_id.lower()) for digest in empty_blobs):
|
|
324
|
+
raise SystemExit(incomplete)
|
|
325
|
+
continue
|
|
326
|
+
if index is None:
|
|
327
|
+
raise SystemExit("A binary patch requires an index line with full object IDs.")
|
|
328
|
+
old, new = index.groups()
|
|
329
|
+
if binary_oid_width is None:
|
|
330
|
+
binary_oid_width = len(git(b"", "hash-object", "--no-filters", "--stdin").strip())
|
|
331
|
+
if (
|
|
332
|
+
len(old) != binary_oid_width
|
|
333
|
+
or len(new) != binary_oid_width
|
|
334
|
+
or not re.fullmatch(rb"[0-9a-fA-F]+", old + new)
|
|
335
|
+
):
|
|
336
|
+
raise SystemExit(
|
|
337
|
+
"A binary patch requires full object IDs in the selected target's Git object format."
|
|
338
|
+
)
|
|
339
|
+
|
|
340
|
+
|
|
341
|
+
def patch_metadata(
|
|
342
|
+
current: sqlite3.Row,
|
|
343
|
+
args: argparse.Namespace,
|
|
344
|
+
patch: bytes | None,
|
|
345
|
+
resolve_path: Callable[[str], str],
|
|
346
|
+
) -> tuple[str | None, str | None]:
|
|
347
|
+
path = args.patch_path
|
|
348
|
+
digest = args.patch_digest
|
|
349
|
+
if patch is not None:
|
|
350
|
+
if args.state != "generated" or path is not None or digest is not None:
|
|
351
|
+
raise SystemExit("Supply patch only for generation, without a patch path or digest.")
|
|
352
|
+
path = current["patch_path"] or (
|
|
353
|
+
f"artifacts/remediation/{current['occurrence_id']}/{current['request_id']}/"
|
|
354
|
+
f"{current['pending_action_claim_token']}.patch"
|
|
355
|
+
)
|
|
356
|
+
digest = f"sha256:{hashlib.sha256(patch).hexdigest()}"
|
|
357
|
+
else:
|
|
358
|
+
if path is not None:
|
|
359
|
+
path = resolve_path(path)
|
|
360
|
+
if digest is not None:
|
|
361
|
+
digest = optional_text(digest, maximum=71)
|
|
362
|
+
if not re.fullmatch(r"sha256:[0-9a-f]{64}", digest or ""):
|
|
363
|
+
raise SystemExit("patch-digest must use sha256:<64 lowercase hex characters>.")
|
|
364
|
+
if path is not None and current["patch_path"] not in (None, path):
|
|
365
|
+
raise SystemExit("A remediation attempt cannot replace its reviewed patch path.")
|
|
366
|
+
if digest is not None and current["patch_digest"] not in (None, digest):
|
|
367
|
+
raise SystemExit("A remediation attempt cannot replace its reviewed patch digest.")
|
|
368
|
+
return path or current["patch_path"], digest or current["patch_digest"]
|
|
369
|
+
|
|
370
|
+
|
|
371
|
+
def same_file_version(
|
|
372
|
+
original: tuple[os.stat_result, int], current: tuple[os.stat_result, int]
|
|
373
|
+
) -> bool:
|
|
374
|
+
before, before_change = original
|
|
375
|
+
after, after_change = current
|
|
376
|
+
return (
|
|
377
|
+
os.path.samestat(before, after)
|
|
378
|
+
and before.st_size == after.st_size
|
|
379
|
+
and before.st_mtime_ns == after.st_mtime_ns
|
|
380
|
+
and before_change == after_change
|
|
381
|
+
)
|
|
382
|
+
|
|
383
|
+
|
|
384
|
+
def file_digest_and_version(source: BinaryIO, label: str) -> tuple[str, tuple[os.stat_result, int]]:
|
|
385
|
+
before = scan_local_file_version(source.fileno())
|
|
386
|
+
digest = hashlib.sha256()
|
|
387
|
+
for chunk in iter(lambda: source.read(1024 * 1024), b""):
|
|
388
|
+
digest.update(chunk)
|
|
389
|
+
after = scan_local_file_version(source.fileno())
|
|
390
|
+
if not same_file_version(before, after):
|
|
391
|
+
raise SystemExit(f"The {label} changed while it was being verified.")
|
|
392
|
+
return f"sha256:{digest.hexdigest()}", after
|
|
393
|
+
|
|
394
|
+
|
|
395
|
+
def matching_patch_metadata(source: BinaryIO, expected_digest: str) -> tuple[os.stat_result, int]:
|
|
396
|
+
digest, version = file_digest_and_version(source, "scan-local patch")
|
|
397
|
+
if digest != expected_digest:
|
|
398
|
+
raise SystemExit("Patch digest does not match the scan-local patch file.")
|
|
399
|
+
return version
|
|
400
|
+
|
|
401
|
+
|
|
402
|
+
def patch_manifest_version(
|
|
403
|
+
db: Any, scan: sqlite3.Row, scan_dir: Path
|
|
404
|
+
) -> tuple[str, tuple[os.stat_result, int]]:
|
|
405
|
+
manifest_path = db.ARTIFACTS["manifest"]
|
|
406
|
+
expected = scan["seal_manifest_digest"]
|
|
407
|
+
if expected is None:
|
|
408
|
+
# Historical manifests can have different JSON formatting. Bind their original bytes.
|
|
409
|
+
saved, raw = _read_scan_local_json_bytes(scan_dir, manifest_path, manifest_path)
|
|
410
|
+
manifest, _, _, _ = _read_sealed_scan(scan_dir, None, "remediation")
|
|
411
|
+
if saved != manifest:
|
|
412
|
+
raise SystemExit("The sealed scan manifest changed during validation.")
|
|
413
|
+
db.verify_manifest_binding(scan, manifest)
|
|
414
|
+
expected = f"sha256:{hashlib.sha256(raw).hexdigest()}"
|
|
415
|
+
with db.open_scan_local_file(scan_dir, manifest_path) as source:
|
|
416
|
+
digest, version = file_digest_and_version(source, "sealed scan manifest")
|
|
417
|
+
if digest != expected:
|
|
418
|
+
raise SystemExit("The sealed scan manifest changed after completion.")
|
|
419
|
+
return digest, version
|
|
420
|
+
|
|
421
|
+
|
|
422
|
+
def save_patch(
|
|
423
|
+
db: Any,
|
|
424
|
+
connection: sqlite3.Connection,
|
|
425
|
+
scan_id: str,
|
|
426
|
+
current: sqlite3.Row,
|
|
427
|
+
path: str,
|
|
428
|
+
digest: str,
|
|
429
|
+
patch: bytes,
|
|
430
|
+
record_update: Callable[[], None],
|
|
431
|
+
) -> None:
|
|
432
|
+
def admit() -> Path:
|
|
433
|
+
record_update()
|
|
434
|
+
latest = db.require_scan(connection, scan_id)
|
|
435
|
+
directory = db.require_canonical_scan_directory(Path(latest["scan_dir"]))
|
|
436
|
+
if scan_root_identity(directory)[1] != identity:
|
|
437
|
+
raise SystemExit("The scan directory changed after seal validation.")
|
|
438
|
+
with db.open_scan_local_file(directory, db.ARTIFACTS["manifest"]) as named:
|
|
439
|
+
current_version = scan_local_file_version(named.fileno())
|
|
440
|
+
if latest["seal_manifest_digest"] not in (None, manifest_digest) or not same_file_version(
|
|
441
|
+
manifest_version, current_version
|
|
442
|
+
):
|
|
443
|
+
raise SystemExit("The sealed scan manifest changed after completion.")
|
|
444
|
+
return directory
|
|
445
|
+
|
|
446
|
+
def prepare_saved_patch(directory: Path) -> tuple[os.stat_result, int]:
|
|
447
|
+
with db.open_scan_local_file(directory, path) as named:
|
|
448
|
+
version = matching_patch_metadata(named, digest)
|
|
449
|
+
if os.name == "posix":
|
|
450
|
+
# Persist the file and every directory entry before SQLite commits its path.
|
|
451
|
+
os.fsync(named.fileno())
|
|
452
|
+
with (
|
|
453
|
+
managed_fd(_open_verified_scan_directory(directory, identity)) as root,
|
|
454
|
+
managed_fd(
|
|
455
|
+
_open_scan_local_directory(
|
|
456
|
+
root, Path(path).parts[:-1], create=False, sync_parents=True
|
|
457
|
+
)
|
|
458
|
+
) as parent,
|
|
459
|
+
):
|
|
460
|
+
os.fsync(parent)
|
|
461
|
+
return version
|
|
462
|
+
|
|
463
|
+
def verify_name(directory: Path, expected: tuple[os.stat_result, int]) -> None:
|
|
464
|
+
with db.open_scan_local_file(directory, path) as named:
|
|
465
|
+
if not same_file_version(expected, scan_local_file_version(named.fileno())):
|
|
466
|
+
raise SystemExit("The scan-local patch changed during generation.")
|
|
467
|
+
if scan_root_identity(directory)[1] != identity:
|
|
468
|
+
raise SystemExit("The scan directory changed while saving the patch.")
|
|
469
|
+
|
|
470
|
+
existing = current["patch_path"] is not None
|
|
471
|
+
retried_directory = None
|
|
472
|
+
identity = None
|
|
473
|
+
while True:
|
|
474
|
+
scan = db.require_scan(connection, scan_id)
|
|
475
|
+
root_changed_during_validation = False
|
|
476
|
+
with ExitStack() as staging:
|
|
477
|
+
try:
|
|
478
|
+
scan_dir = db.require_canonical_scan_directory(Path(scan["scan_dir"]))
|
|
479
|
+
if identity is None:
|
|
480
|
+
scan_dir, root_identity = scan_root_identity(scan_dir)
|
|
481
|
+
manifest_digest, manifest_version = patch_manifest_version(db, scan, scan_dir)
|
|
482
|
+
root_changed_during_validation = (
|
|
483
|
+
scan_root_identity(scan_dir)[1] != root_identity
|
|
484
|
+
)
|
|
485
|
+
if root_changed_during_validation:
|
|
486
|
+
raise ContractError("The scan directory changed during seal validation.")
|
|
487
|
+
identity = root_identity
|
|
488
|
+
publish = None
|
|
489
|
+
if not existing:
|
|
490
|
+
with suppress(FileExistsError):
|
|
491
|
+
publish = staging.enter_context(
|
|
492
|
+
stage_scan_local_bytes(
|
|
493
|
+
scan_dir,
|
|
494
|
+
path,
|
|
495
|
+
patch,
|
|
496
|
+
expected_root_identity=identity,
|
|
497
|
+
owner_read_write=True,
|
|
498
|
+
)
|
|
499
|
+
)
|
|
500
|
+
if publish is None:
|
|
501
|
+
metadata = prepare_saved_patch(scan_dir)
|
|
502
|
+
except (ContractError, OSError, SystemExit) as exc:
|
|
503
|
+
# Archiving moves the directory before committing its new location.
|
|
504
|
+
with connection:
|
|
505
|
+
connection.execute("BEGIN IMMEDIATE")
|
|
506
|
+
directory = db.require_scan(connection, scan_id)["scan_dir"]
|
|
507
|
+
if directory == scan["scan_dir"]:
|
|
508
|
+
if directory == retried_directory:
|
|
509
|
+
raise
|
|
510
|
+
# Rollback can restore a missing file or the verified staging root.
|
|
511
|
+
cause: BaseException | None = exc
|
|
512
|
+
while cause and not isinstance(
|
|
513
|
+
cause, (FileNotFoundError, MissingScanLocalFileError)
|
|
514
|
+
):
|
|
515
|
+
cause = cause.__cause__
|
|
516
|
+
if (
|
|
517
|
+
cause is None
|
|
518
|
+
and not root_changed_during_validation
|
|
519
|
+
and not (
|
|
520
|
+
isinstance(exc, ContractError)
|
|
521
|
+
and identity is not None
|
|
522
|
+
and scan_root_identity(Path(directory))[1] == identity
|
|
523
|
+
)
|
|
524
|
+
):
|
|
525
|
+
raise
|
|
526
|
+
retried_directory = directory
|
|
527
|
+
continue
|
|
528
|
+
directory = admit()
|
|
529
|
+
if publish is not None:
|
|
530
|
+
with suppress(FileExistsError):
|
|
531
|
+
publish(directory)
|
|
532
|
+
# Keep the token's file, including a competing winner, if cancellation
|
|
533
|
+
# or interruption wins before final admission. Verify it outside SQLite.
|
|
534
|
+
connection.rollback()
|
|
535
|
+
existing = True
|
|
536
|
+
continue
|
|
537
|
+
verify_name(directory, metadata)
|
|
538
|
+
return
|
|
539
|
+
|
|
540
|
+
|
|
49
541
|
def remediation_claim_is_active(remediation: sqlite3.Row) -> bool:
|
|
50
542
|
if remediation["pending_action_claim_token"] is None:
|
|
51
543
|
return False
|
|
@@ -173,15 +665,7 @@ def _cancel_generation(
|
|
|
173
665
|
connection.execute(
|
|
174
666
|
"DELETE FROM finding_remediation_attempts WHERE request_id = ?", (request_id,)
|
|
175
667
|
)
|
|
176
|
-
previous = connection
|
|
177
|
-
"""
|
|
178
|
-
SELECT * FROM finding_remediation_attempts
|
|
179
|
-
WHERE occurrence_id = ?
|
|
180
|
-
ORDER BY created_at DESC, rowid DESC
|
|
181
|
-
LIMIT 1
|
|
182
|
-
""",
|
|
183
|
-
(occurrence_id,),
|
|
184
|
-
).fetchone()
|
|
668
|
+
previous = latest_attempt(connection, occurrence_id)
|
|
185
669
|
if previous is None or previous["state"] != "superseded":
|
|
186
670
|
return
|
|
187
671
|
restored_state = "applied" if previous["applied_content_digest"] is not None else "generated"
|
|
@@ -39,55 +39,7 @@ from finalize_scan_contract import (
|
|
|
39
39
|
)
|
|
40
40
|
from finalize_scan_contract import finding_candidate_id as finding_candidate_id
|
|
41
41
|
from review_state import checkpoint_review_state, review_state, review_summary, uses_legacy_review
|
|
42
|
-
from workbench.retained_coverage import (
|
|
43
|
-
_append_previous_finding as _append_previous_finding,
|
|
44
|
-
)
|
|
45
|
-
from workbench.retained_coverage import (
|
|
46
|
-
_coverage_rows as _coverage_rows,
|
|
47
|
-
)
|
|
48
|
-
from workbench.retained_coverage import (
|
|
49
|
-
_ensure_finding_identities as _ensure_finding_identities,
|
|
50
|
-
)
|
|
51
|
-
from workbench.retained_coverage import (
|
|
52
|
-
_ensure_finding_identity as _ensure_finding_identity,
|
|
53
|
-
)
|
|
54
|
-
from workbench.retained_coverage import (
|
|
55
|
-
_finding_content as _finding_content,
|
|
56
|
-
)
|
|
57
|
-
from workbench.retained_coverage import (
|
|
58
|
-
_finding_identity as _finding_identity,
|
|
59
|
-
)
|
|
60
|
-
from workbench.retained_coverage import (
|
|
61
|
-
_finding_key as _finding_key,
|
|
62
|
-
)
|
|
63
|
-
from workbench.retained_coverage import (
|
|
64
|
-
_has_candidate_metadata as _has_candidate_metadata,
|
|
65
|
-
)
|
|
66
|
-
from workbench.retained_coverage import (
|
|
67
|
-
_identity_candidate as _identity_candidate,
|
|
68
|
-
)
|
|
69
|
-
from workbench.retained_coverage import (
|
|
70
|
-
_inherit_coverage_sources as _inherit_coverage_sources,
|
|
71
|
-
)
|
|
72
|
-
from workbench.retained_coverage import (
|
|
73
|
-
_retained_findings as _retained_findings,
|
|
74
|
-
)
|
|
75
|
-
from workbench.retained_coverage import (
|
|
76
|
-
_semantic_identity_key as _semantic_identity_key,
|
|
77
|
-
)
|
|
78
|
-
from workbench.retained_coverage import (
|
|
79
|
-
_worker_candidate_key as _worker_candidate_key,
|
|
80
|
-
)
|
|
81
42
|
from workbench.retained_coverage import merge_saved_results as merge_saved_results
|
|
82
|
-
from workbench.saved_coverage import (
|
|
83
|
-
_deferred_rows as _deferred_rows,
|
|
84
|
-
)
|
|
85
|
-
from workbench.saved_coverage import (
|
|
86
|
-
_merge_tied_parent_observations as _merge_tied_parent_observations,
|
|
87
|
-
)
|
|
88
|
-
from workbench.saved_coverage import (
|
|
89
|
-
_resolved_deferred_rows as _resolved_deferred_rows,
|
|
90
|
-
)
|
|
91
43
|
from workbench.saved_coverage import (
|
|
92
44
|
_saved_coverage_id as _saved_coverage_id,
|
|
93
45
|
)
|
|
@@ -115,28 +67,10 @@ from workbench.saved_sources import (
|
|
|
115
67
|
_saved_result_paths,
|
|
116
68
|
_source_digests,
|
|
117
69
|
)
|
|
118
|
-
from workbench.saved_sources import (
|
|
119
|
-
_capture_parent_observation as _capture_parent_observation,
|
|
120
|
-
)
|
|
121
|
-
from workbench.saved_sources import (
|
|
122
|
-
_checkpoint_head_directory as _checkpoint_head_directory,
|
|
123
|
-
)
|
|
124
70
|
from workbench.saved_sources import (
|
|
125
71
|
_digest as _digest,
|
|
126
72
|
)
|
|
127
|
-
from workbench.saved_sources import (
|
|
128
|
-
_latest_successful_reducer as _latest_successful_reducer,
|
|
129
|
-
)
|
|
130
|
-
from workbench.saved_sources import (
|
|
131
|
-
_parent_publication_ancestors as _parent_publication_ancestors,
|
|
132
|
-
)
|
|
133
|
-
from workbench.saved_sources import (
|
|
134
|
-
_worker_outputs as _worker_outputs,
|
|
135
|
-
)
|
|
136
73
|
from workbench_constants import PHASES
|
|
137
|
-
from workbench_coverage_receipts import (
|
|
138
|
-
_deferred_candidate_id as _deferred_candidate_id,
|
|
139
|
-
)
|
|
140
74
|
from workbench_coverage_receipts import (
|
|
141
75
|
_generic_surface_updates as _generic_surface_updates,
|
|
142
76
|
)
|