@openai/codex-security 0.9.1 → 0.10.0
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 +37 -788
- package/_bundled_plugin/.codex-plugin/plugin.json +2 -0
- package/_bundled_plugin/.mcp.json +2 -0
- package/_bundled_plugin/mcp/helpers.mjs.br +0 -0
- 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/server.mjs.br +0 -0
- package/_bundled_plugin/references/artifact-storage.md +1 -1
- package/_bundled_plugin/references/baseline-auditor.md +18 -0
- package/_bundled_plugin/references/core-scan.md +5 -42
- package/_bundled_plugin/references/final-report.md +1 -1
- package/_bundled_plugin/references/focused-investigator.md +13 -0
- package/_bundled_plugin/references/scan-prologue.md +2 -2
- package/_bundled_plugin/references/scan-runtime.md +4 -2
- package/_bundled_plugin/references/security-guidance.md +14 -1
- package/_bundled_plugin/references/validate-scan-findings.md +3 -0
- package/_bundled_plugin/scripts/deep_scan_workbench.py +29 -12
- package/_bundled_plugin/scripts/dispatch_inventory_tool +7 -0
- package/_bundled_plugin/scripts/finalize_scan_contract.py +29 -3
- package/_bundled_plugin/scripts/launch_codex_security_mcp +54 -16
- package/_bundled_plugin/scripts/posix_scan_local_files.py +26 -1
- package/_bundled_plugin/scripts/snapshot_sqlite.py +8 -2
- package/_bundled_plugin/scripts/windows_scan_local_files.py +216 -25
- package/_bundled_plugin/scripts/workbench/handoff.py +55 -25
- package/_bundled_plugin/scripts/workbench/storage.py +25 -5
- package/_bundled_plugin/scripts/workbench_cli.py +24 -2
- package/_bundled_plugin/scripts/workbench_constants.py +2 -0
- package/_bundled_plugin/scripts/workbench_db.py +244 -217
- package/_bundled_plugin/scripts/workbench_feedback.py +49 -4
- package/_bundled_plugin/scripts/workbench_native_indexes.py +226 -29
- package/_bundled_plugin/scripts/workbench_progress.py +159 -3
- package/_bundled_plugin/scripts/workbench_publication.py +2 -3
- package/_bundled_plugin/scripts/workbench_remediation.py +142 -2
- package/_bundled_plugin/scripts/workbench_saved_results.py +48 -8
- package/_bundled_plugin/scripts/workbench_scan_history.py +281 -119
- package/_bundled_plugin/scripts/workbench_scan_start.py +143 -9
- package/_bundled_plugin/scripts/workbench_scan_usage.py +36 -10
- package/_bundled_plugin/scripts/workbench_schema.py +220 -14
- package/_bundled_plugin/scripts/workbench_setup.py +1 -1
- package/_bundled_plugin/scripts/workbench_target.py +471 -84
- package/_bundled_plugin/scripts/workbench_target_state.py +1108 -22
- package/_bundled_plugin/shared/workbench-history-repairs.json +60 -14
- package/_bundled_plugin/shared/workbench-migrations.json +66 -0
- package/_bundled_plugin/skills/fix-finding/SKILL.md +1 -0
- package/_bundled_plugin/skills/publish-findings/SKILL.md +24 -160
- package/_bundled_plugin/skills/publish-findings/agents/openai.yaml +2 -2
- package/_bundled_plugin/skills/publish-findings/references/checkmarx-import.md +2 -2
- package/_bundled_plugin/skills/publish-findings/scripts/collect_checkmarx.py +3 -58
- package/_bundled_plugin/skills/publish-findings/scripts/collect_continuum.py +224 -0
- package/_bundled_plugin/skills/publish-findings/scripts/collector_paths.py +63 -0
- package/_bundled_plugin/skills/security-scan/SKILL.md +9 -46
- package/_bundled_plugin/skills/security-scan/agents/openai.yaml +2 -2
- package/_bundled_plugin/skills/security-scan/references/desktop-scan.md +3 -21
- package/_bundled_plugin/skills/security-scan/references/managed-scan.md +11 -0
- package/_bundled_plugin/skills/security-scan/references/scan-results.md +15 -0
- package/_bundled_plugin/skills/security-scan/references/scoped-paths.md +23 -0
- package/_bundled_plugin/skills/security-scan/references/sdk-files.md +24 -0
- package/bin/codex-security.mjs +2 -2
- package/dist/api.d.ts +6 -1
- package/dist/api.d.ts.map +1 -1
- package/dist/api.js +437 -154
- package/dist/api.js.map +1 -1
- package/dist/armorcode-findings.d.ts +9 -0
- package/dist/armorcode-findings.d.ts.map +1 -0
- package/dist/armorcode-findings.js +145 -0
- package/dist/armorcode-findings.js.map +1 -0
- package/dist/artifact-export.d.ts +2 -0
- package/dist/artifact-export.d.ts.map +1 -1
- package/dist/artifact-export.js +29 -13
- package/dist/artifact-export.js.map +1 -1
- package/dist/auth.d.ts +6 -2
- package/dist/auth.d.ts.map +1 -1
- package/dist/auth.js +33 -12
- package/dist/auth.js.map +1 -1
- package/dist/batch-cost.d.ts +18 -0
- package/dist/batch-cost.d.ts.map +1 -0
- package/dist/batch-cost.js +41 -0
- package/dist/batch-cost.js.map +1 -0
- package/dist/blackduck-findings.d.ts +1 -1
- package/dist/blackduck-findings.d.ts.map +1 -1
- package/dist/blackduck-findings.js +5 -18
- package/dist/blackduck-findings.js.map +1 -1
- package/dist/bundled-plugin.d.ts +4 -0
- package/dist/bundled-plugin.d.ts.map +1 -0
- package/dist/bundled-plugin.js +23 -0
- package/dist/bundled-plugin.js.map +1 -0
- package/dist/classify-scan-severity.d.ts +1 -1
- package/dist/classify-scan-severity.d.ts.map +1 -1
- package/dist/classify-scan-severity.js +11 -3
- package/dist/classify-scan-severity.js.map +1 -1
- package/dist/classify-severity.d.ts.map +1 -1
- package/dist/classify-severity.js +9 -6
- package/dist/classify-severity.js.map +1 -1
- package/dist/cli-argument.d.ts +4 -0
- package/dist/cli-argument.d.ts.map +1 -0
- package/dist/cli-argument.js +17 -0
- package/dist/cli-argument.js.map +1 -0
- package/dist/cli-help.d.ts +1 -1
- package/dist/cli-help.d.ts.map +1 -1
- package/dist/cli-help.js +71 -29
- package/dist/cli-help.js.map +1 -1
- package/dist/cli-hook.d.ts +3 -0
- package/dist/cli-hook.d.ts.map +1 -0
- package/dist/cli-hook.js +87 -0
- package/dist/cli-hook.js.map +1 -0
- package/dist/cli-integrations.d.ts +13 -0
- package/dist/cli-integrations.d.ts.map +1 -0
- package/dist/cli-integrations.js +333 -0
- package/dist/cli-integrations.js.map +1 -0
- package/dist/cli-mcp.d.ts +15 -0
- package/dist/cli-mcp.d.ts.map +1 -0
- package/dist/cli-mcp.js +178 -0
- package/dist/cli-mcp.js.map +1 -0
- package/dist/cli-token-view.d.ts +10 -0
- package/dist/cli-token-view.d.ts.map +1 -0
- package/dist/cli-token-view.js +41 -0
- package/dist/cli-token-view.js.map +1 -0
- package/dist/cli.d.ts +5 -3
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +1763 -894
- package/dist/cli.js.map +1 -1
- package/dist/cloud-publish.d.ts.map +1 -1
- package/dist/cloud-publish.js +33 -31
- package/dist/cloud-publish.js.map +1 -1
- package/dist/codex-sdk-environment.d.ts +6 -0
- package/dist/codex-sdk-environment.d.ts.map +1 -1
- package/dist/codex-sdk-environment.js +19 -0
- package/dist/codex-sdk-environment.js.map +1 -1
- package/dist/command-words.d.ts +3 -0
- package/dist/command-words.d.ts.map +1 -0
- package/dist/command-words.js +46 -0
- package/dist/command-words.js.map +1 -0
- package/dist/component-scan.d.ts +3 -1
- package/dist/component-scan.d.ts.map +1 -1
- package/dist/component-scan.js +40 -7
- package/dist/component-scan.js.map +1 -1
- package/dist/config.d.ts +1 -1
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +30 -1
- package/dist/config.js.map +1 -1
- package/dist/continuum-findings.d.ts +1 -1
- package/dist/continuum-findings.d.ts.map +1 -1
- package/dist/continuum-findings.js +93 -31
- package/dist/continuum-findings.js.map +1 -1
- package/dist/cost.d.ts.map +1 -1
- package/dist/cost.js +1 -45
- 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 +29 -19
- package/dist/custom-validation-prompt.js.map +1 -1
- package/dist/deduplication/local.d.ts +3 -1
- package/dist/deduplication/local.d.ts.map +1 -1
- package/dist/deduplication/local.js +8 -1
- package/dist/deduplication/local.js.map +1 -1
- package/dist/deduplication/refusal.d.ts.map +1 -1
- package/dist/deduplication/refusal.js +2 -1
- package/dist/deduplication/refusal.js.map +1 -1
- package/dist/deduplication/scan.d.ts +2 -0
- package/dist/deduplication/scan.d.ts.map +1 -1
- package/dist/deduplication/scan.js +24 -7
- package/dist/deduplication/scan.js.map +1 -1
- package/dist/deep-scan.d.ts.map +1 -1
- package/dist/deep-scan.js +16 -15
- package/dist/deep-scan.js.map +1 -1
- package/dist/endor-findings.d.ts +2 -2
- package/dist/endor-findings.d.ts.map +1 -1
- package/dist/endor-findings.js +6 -23
- package/dist/endor-findings.js.map +1 -1
- package/dist/errors.d.ts +3 -1
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +7 -3
- package/dist/errors.js.map +1 -1
- package/dist/executable-path.d.ts +3 -0
- package/dist/executable-path.d.ts.map +1 -1
- package/dist/executable-path.js +26 -0
- package/dist/executable-path.js.map +1 -1
- package/dist/external-findings-import.d.ts.map +1 -1
- package/dist/external-findings-import.js +12 -1
- package/dist/external-findings-import.js.map +1 -1
- package/dist/external-findings-local.d.ts +66 -4
- package/dist/external-findings-local.d.ts.map +1 -1
- package/dist/external-findings-local.js +234 -53
- package/dist/external-findings-local.js.map +1 -1
- package/dist/external-findings-publish.d.ts +6 -1
- package/dist/external-findings-publish.d.ts.map +1 -1
- package/dist/external-findings-publish.js +326 -1633
- package/dist/external-findings-publish.js.map +1 -1
- package/dist/external-findings-read.d.ts +1 -0
- package/dist/external-findings-read.d.ts.map +1 -1
- package/dist/external-findings-read.js +31 -51
- package/dist/external-findings-read.js.map +1 -1
- package/dist/external-findings.d.ts +1 -1
- package/dist/external-findings.d.ts.map +1 -1
- package/dist/external-findings.js +12 -1
- package/dist/external-findings.js.map +1 -1
- package/dist/external-import-contract.d.ts +3 -11
- package/dist/external-import-contract.d.ts.map +1 -1
- package/dist/external-import-contract.js +29 -176
- 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-prompt.d.ts.map +1 -1
- package/dist/finding-prompt.js +2 -2
- package/dist/finding-prompt.js.map +1 -1
- package/dist/github-dependabot-findings.d.ts.map +1 -1
- package/dist/github-dependabot-findings.js +3 -4
- package/dist/github-dependabot-findings.js.map +1 -1
- package/dist/github-findings.d.ts.map +1 -1
- package/dist/github-findings.js +19 -287
- package/dist/github-findings.js.map +1 -1
- package/dist/hosted-remediation.d.ts +1 -0
- package/dist/hosted-remediation.d.ts.map +1 -1
- package/dist/hosted-remediation.js +23 -3
- package/dist/hosted-remediation.js.map +1 -1
- package/dist/index.d.ts +3 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/dist/inspector-findings.d.ts.map +1 -1
- package/dist/inspector-findings.js +19 -35
- package/dist/inspector-findings.js.map +1 -1
- package/dist/knowledge-base.d.ts +1 -1
- package/dist/knowledge-base.d.ts.map +1 -1
- package/dist/knowledge-base.js +34 -27
- package/dist/knowledge-base.js.map +1 -1
- package/dist/linear.d.ts +11 -0
- package/dist/linear.d.ts.map +1 -1
- package/dist/linear.js.map +1 -1
- package/dist/local-git-metadata.d.ts +5 -2
- package/dist/local-git-metadata.d.ts.map +1 -1
- package/dist/local-git-metadata.js +8 -4
- package/dist/local-git-metadata.js.map +1 -1
- package/dist/multiscan.d.ts +4 -0
- package/dist/multiscan.d.ts.map +1 -1
- package/dist/multiscan.js +79 -37
- package/dist/multiscan.js.map +1 -1
- package/dist/opengrep-findings.d.ts +4 -2
- package/dist/opengrep-findings.d.ts.map +1 -1
- package/dist/opengrep-findings.js +39 -22
- package/dist/opengrep-findings.js.map +1 -1
- package/dist/private-finding-import.d.ts +8 -13
- package/dist/private-finding-import.d.ts.map +1 -1
- package/dist/private-finding-import.js +69 -129
- package/dist/private-finding-import.js.map +1 -1
- package/dist/project-config.d.ts.map +1 -1
- package/dist/project-config.js +53 -13
- package/dist/project-config.js.map +1 -1
- package/dist/prompt-files.d.ts +7 -2
- package/dist/prompt-files.d.ts.map +1 -1
- package/dist/prompt-files.js +42 -20
- package/dist/prompt-files.js.map +1 -1
- package/dist/provider-profile.d.ts.map +1 -1
- package/dist/provider-profile.js +4 -9
- package/dist/provider-profile.js.map +1 -1
- package/dist/publication.d.ts +1 -1
- package/dist/publication.d.ts.map +1 -1
- package/dist/publication.js.map +1 -1
- package/dist/record.d.ts +1 -5
- package/dist/record.d.ts.map +1 -1
- package/dist/record.js +3 -51
- package/dist/record.js.map +1 -1
- package/dist/remediation.d.ts.map +1 -1
- package/dist/remediation.js +3 -1
- package/dist/remediation.js.map +1 -1
- package/dist/runtime.d.ts +35 -7
- package/dist/runtime.d.ts.map +1 -1
- package/dist/runtime.js +778 -153
- package/dist/runtime.js.map +1 -1
- package/dist/saved-scan-bootstrap.d.ts +6 -1
- package/dist/saved-scan-bootstrap.d.ts.map +1 -1
- package/dist/saved-scan-bootstrap.js +267 -115
- package/dist/saved-scan-bootstrap.js.map +1 -1
- package/dist/scan-archive-path.d.ts +2 -0
- package/dist/scan-archive-path.d.ts.map +1 -0
- package/dist/scan-archive-path.js +5 -0
- package/dist/scan-archive-path.js.map +1 -0
- package/dist/scan-comparison.d.ts.map +1 -1
- package/dist/scan-comparison.js +6 -2
- 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 +18 -2
- package/dist/scan-dashboard.js.map +1 -1
- package/dist/scan-executor.d.ts.map +1 -1
- package/dist/scan-executor.js +4 -2
- package/dist/scan-executor.js.map +1 -1
- package/dist/scan-history-renderer.d.ts +1 -0
- package/dist/scan-history-renderer.d.ts.map +1 -1
- package/dist/scan-history-renderer.js +25 -9
- package/dist/scan-history-renderer.js.map +1 -1
- package/dist/scan-logs.d.ts.map +1 -1
- package/dist/scan-logs.js +28 -4
- package/dist/scan-logs.js.map +1 -1
- package/dist/scanner-import.d.ts +5 -11
- package/dist/scanner-import.d.ts.map +1 -1
- package/dist/scanner-import.js +16 -89
- package/dist/scanner-import.js.map +1 -1
- package/dist/security-policy.d.ts.map +1 -1
- package/dist/security-policy.js +26 -6
- package/dist/security-policy.js.map +1 -1
- package/dist/semgrep-findings.d.ts.map +1 -1
- package/dist/semgrep-findings.js +14 -48
- package/dist/semgrep-findings.js.map +1 -1
- package/dist/semgrep-platform-findings.d.ts +20 -70
- package/dist/semgrep-platform-findings.d.ts.map +1 -1
- package/dist/semgrep-platform-findings.js +116 -333
- package/dist/semgrep-platform-findings.js.map +1 -1
- package/dist/snyk-findings.d.ts +2 -0
- package/dist/snyk-findings.d.ts.map +1 -1
- package/dist/snyk-findings.js +5 -8
- package/dist/snyk-findings.js.map +1 -1
- package/dist/snyk-rest-findings.d.ts +3 -20
- package/dist/snyk-rest-findings.d.ts.map +1 -1
- package/dist/snyk-rest-findings.js +45 -217
- package/dist/snyk-rest-findings.js.map +1 -1
- package/dist/sonar-findings.d.ts +1 -1
- package/dist/sonar-findings.d.ts.map +1 -1
- package/dist/sonar-findings.js +2 -4
- package/dist/sonar-findings.js.map +1 -1
- package/dist/sonar-import.d.ts +3 -0
- package/dist/sonar-import.d.ts.map +1 -1
- package/dist/sonar-import.js +28 -30
- package/dist/sonar-import.js.map +1 -1
- package/dist/stored-finding-projection.d.ts +6 -0
- package/dist/stored-finding-projection.d.ts.map +1 -0
- package/dist/stored-finding-projection.js +104 -0
- package/dist/stored-finding-projection.js.map +1 -0
- package/dist/stored-findings-publication.d.ts +23 -0
- package/dist/stored-findings-publication.d.ts.map +1 -0
- package/dist/stored-findings-publication.js +169 -0
- package/dist/stored-findings-publication.js.map +1 -0
- package/dist/targets.d.ts +6 -1
- package/dist/targets.d.ts.map +1 -1
- package/dist/targets.js +76 -23
- package/dist/targets.js.map +1 -1
- package/dist/tenable-findings.d.ts +1 -4
- package/dist/tenable-findings.d.ts.map +1 -1
- package/dist/tenable-findings.js.map +1 -1
- package/dist/trivy-findings.d.ts.map +1 -1
- package/dist/trivy-findings.js +0 -11
- package/dist/trivy-findings.js.map +1 -1
- package/dist/trusted-executable.d.ts +6 -0
- package/dist/trusted-executable.d.ts.map +1 -1
- package/dist/trusted-executable.js +140 -37
- package/dist/trusted-executable.js.map +1 -1
- package/dist/veracode-findings.d.ts +1 -3
- package/dist/veracode-findings.d.ts.map +1 -1
- package/dist/veracode-findings.js +13 -38
- package/dist/veracode-findings.js.map +1 -1
- package/dist/version.d.ts +3 -1
- package/dist/version.d.ts.map +1 -1
- package/dist/version.js +184 -32
- package/dist/version.js.map +1 -1
- package/dist/wiz-findings.d.ts +2 -2
- package/dist/wiz-findings.d.ts.map +1 -1
- package/dist/wiz-findings.js +14 -92
- package/dist/wiz-findings.js.map +1 -1
- package/docs/cli.md +373 -106
- package/docs/findings-service.md +22 -9
- package/docs/hosted-remediation.md +13 -4
- package/docs/sdk.md +903 -0
- package/package.json +10 -6
- package/schemas/external-findings.schema.json +2 -1
- package/dist/hosted-scanner-v1.d.ts +0 -6
- package/dist/hosted-scanner-v1.d.ts.map +0 -1
- package/dist/hosted-scanner-v1.js +0 -60
- package/dist/hosted-scanner-v1.js.map +0 -1
- package/dist/reader-selection.d.ts +0 -15
- package/dist/reader-selection.d.ts.map +0 -1
- package/dist/reader-selection.js +0 -89
- package/dist/reader-selection.js.map +0 -1
- package/dist/semgrep-platform-v1.d.ts +0 -6
- package/dist/semgrep-platform-v1.d.ts.map +0 -1
- package/dist/semgrep-platform-v1.js +0 -83
- package/dist/semgrep-platform-v1.js.map +0 -1
- package/dist/snyk-rest-v1.d.ts +0 -28
- package/dist/snyk-rest-v1.d.ts.map +0 -1
- package/dist/snyk-rest-v1.js +0 -132
- package/dist/snyk-rest-v1.js.map +0 -1
|
@@ -3,10 +3,34 @@
|
|
|
3
3
|
from __future__ import annotations
|
|
4
4
|
|
|
5
5
|
import argparse
|
|
6
|
+
import ctypes
|
|
6
7
|
import hashlib
|
|
8
|
+
import os
|
|
9
|
+
import platform
|
|
10
|
+
import re
|
|
7
11
|
import sqlite3
|
|
12
|
+
import stat
|
|
13
|
+
import sys
|
|
14
|
+
from dataclasses import dataclass, replace
|
|
8
15
|
from datetime import datetime, timezone
|
|
16
|
+
from decimal import Decimal
|
|
17
|
+
from fractions import Fraction
|
|
9
18
|
from pathlib import Path
|
|
19
|
+
from urllib.parse import urlsplit
|
|
20
|
+
|
|
21
|
+
# Some plugin hosts launch Python with safe-path isolation enabled.
|
|
22
|
+
sys.path.insert(0, str(Path(__file__).resolve().parent))
|
|
23
|
+
from filesystem_identity import (
|
|
24
|
+
serialize_filesystem_identity,
|
|
25
|
+
stored_filesystem_identity_matches,
|
|
26
|
+
)
|
|
27
|
+
from workbench_target import (
|
|
28
|
+
_protected_repository_root,
|
|
29
|
+
git_bytes,
|
|
30
|
+
git_output,
|
|
31
|
+
git_worktree_context,
|
|
32
|
+
pin_scoped_git,
|
|
33
|
+
)
|
|
10
34
|
|
|
11
35
|
|
|
12
36
|
def stable_target_id(target: Path) -> str:
|
|
@@ -15,47 +39,1109 @@ def stable_target_id(target: Path) -> str:
|
|
|
15
39
|
return f"target_sha256_{digest}"
|
|
16
40
|
|
|
17
41
|
|
|
42
|
+
def repository_relative_path(target: Path) -> str | None:
|
|
43
|
+
"""Return the target's normalized location within its own Git worktree."""
|
|
44
|
+
worktree = _repository_worktree(target)
|
|
45
|
+
return worktree[1] if worktree is not None else None
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def _repository_worktree(target: Path) -> tuple[Path, str] | None:
|
|
49
|
+
try:
|
|
50
|
+
return git_worktree_context(target)
|
|
51
|
+
except SystemExit:
|
|
52
|
+
return None
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def _path_from_git_bytes(
|
|
56
|
+
value: bytes, relative_to: Path, *, strip_line_feed: bool = True
|
|
57
|
+
) -> Path | None:
|
|
58
|
+
if strip_line_feed and value.endswith(b"\n"):
|
|
59
|
+
value = value[:-2] if sys.platform == "win32" and value.endswith(b"\r\n") else value[:-1]
|
|
60
|
+
if not value or b"\0" in value:
|
|
61
|
+
return None
|
|
62
|
+
path = Path(os.fsdecode(value))
|
|
63
|
+
return Path(os.path.realpath(path if path.is_absolute() else relative_to / path))
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def _git_path(target: Path, *args: str) -> Path | None:
|
|
67
|
+
value = git_bytes(target, *args)
|
|
68
|
+
return _path_from_git_bytes(value, target) if value is not None else None
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def _same_existing_path(first: Path | None, second: Path | None) -> bool:
|
|
72
|
+
if first is None or second is None:
|
|
73
|
+
return False
|
|
74
|
+
try:
|
|
75
|
+
return first.samefile(second)
|
|
76
|
+
except OSError:
|
|
77
|
+
return False
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def _registered_worktree(target: Path, root: Path, common: Path) -> bool:
|
|
81
|
+
registered = git_bytes(target, "worktree", "list", "--porcelain", "-z")
|
|
82
|
+
if registered is not None:
|
|
83
|
+
return any(
|
|
84
|
+
os.path.realpath(os.fsdecode(record[len(b"worktree ") :])) == str(root)
|
|
85
|
+
for record in registered.split(b"\0")
|
|
86
|
+
if record.startswith(b"worktree ")
|
|
87
|
+
)
|
|
88
|
+
# Git before 2.36 emits unquoted newline-delimited paths. Repository-side
|
|
89
|
+
# ownership records are unambiguous even when a checkout path has newlines.
|
|
90
|
+
gitdir = _git_path(target, "rev-parse", "--absolute-git-dir")
|
|
91
|
+
dotgit = root / ".git"
|
|
92
|
+
try:
|
|
93
|
+
mode = dotgit.lstat().st_mode
|
|
94
|
+
if stat.S_ISDIR(mode):
|
|
95
|
+
return _same_existing_path(gitdir, common) and _same_existing_path(gitdir, dotgit)
|
|
96
|
+
if not stat.S_ISREG(mode) or gitdir is None or not gitdir.is_dir():
|
|
97
|
+
return False
|
|
98
|
+
forward = dotgit.read_bytes()
|
|
99
|
+
if not forward.startswith(b"gitdir: ") or not _same_existing_path(
|
|
100
|
+
_path_from_git_bytes(forward[len(b"gitdir: ") :], root), gitdir
|
|
101
|
+
):
|
|
102
|
+
return False
|
|
103
|
+
if _same_existing_path(gitdir, common):
|
|
104
|
+
configured = git_bytes(
|
|
105
|
+
target,
|
|
106
|
+
"config",
|
|
107
|
+
"--null",
|
|
108
|
+
"--show-scope",
|
|
109
|
+
"--no-includes",
|
|
110
|
+
"--get",
|
|
111
|
+
"core.worktree",
|
|
112
|
+
)
|
|
113
|
+
fields = configured.split(b"\0") if configured is not None else []
|
|
114
|
+
if (
|
|
115
|
+
len(fields) != 3
|
|
116
|
+
or fields[0] not in (b"local", b"worktree")
|
|
117
|
+
or not fields[1]
|
|
118
|
+
or fields[2]
|
|
119
|
+
):
|
|
120
|
+
return False
|
|
121
|
+
configured_root = _path_from_git_bytes(fields[1], gitdir, strip_line_feed=False)
|
|
122
|
+
return _same_existing_path(configured_root, root)
|
|
123
|
+
return _same_existing_path(gitdir.parent, common / "worktrees") and _same_existing_path(
|
|
124
|
+
_path_from_git_bytes((gitdir / "gitdir").read_bytes(), gitdir), dotgit
|
|
125
|
+
)
|
|
126
|
+
except (OSError, ValueError):
|
|
127
|
+
return False
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
class _LinuxStatxTimestamp(ctypes.Structure):
|
|
131
|
+
_fields_ = (
|
|
132
|
+
("seconds", ctypes.c_int64),
|
|
133
|
+
("nanoseconds", ctypes.c_uint32),
|
|
134
|
+
("_reserved", ctypes.c_int32),
|
|
135
|
+
)
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
class _LinuxStatx(ctypes.Structure):
|
|
139
|
+
# Linux's statx UAPI keeps birth time at offset 0x50 in a 0x100-byte buffer.
|
|
140
|
+
_fields_ = (
|
|
141
|
+
("mask", ctypes.c_uint32),
|
|
142
|
+
("_before_birth_time", ctypes.c_ubyte * 76),
|
|
143
|
+
("birth_time", _LinuxStatxTimestamp),
|
|
144
|
+
("_after_birth_time", ctypes.c_ubyte * 160),
|
|
145
|
+
)
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
def _linux_repository_birth_time_ns(path: str) -> int | None:
|
|
149
|
+
if ctypes.sizeof(_LinuxStatx) != 256 or _LinuxStatx.birth_time.offset != 80:
|
|
150
|
+
return None
|
|
151
|
+
metadata = _LinuxStatx()
|
|
152
|
+
birth_time_mask = 0x800
|
|
153
|
+
arguments = (-100, os.fsencode(path), 0, birth_time_mask, ctypes.byref(metadata))
|
|
154
|
+
try:
|
|
155
|
+
libc = ctypes.CDLL(None, use_errno=True)
|
|
156
|
+
try:
|
|
157
|
+
statx = libc.statx
|
|
158
|
+
except AttributeError:
|
|
159
|
+
# Older musl has the syscall but no statx wrapper. Only use known LP64 ABIs.
|
|
160
|
+
if ctypes.sizeof(ctypes.c_void_p) != 8 or ctypes.sizeof(ctypes.c_long) != 8:
|
|
161
|
+
return None
|
|
162
|
+
number = {"x86_64": 332, "aarch64": 291}.get(platform.machine())
|
|
163
|
+
if number is None:
|
|
164
|
+
return None
|
|
165
|
+
statx = libc.syscall
|
|
166
|
+
statx.argtypes = (
|
|
167
|
+
ctypes.c_long,
|
|
168
|
+
ctypes.c_long,
|
|
169
|
+
ctypes.c_char_p,
|
|
170
|
+
ctypes.c_long,
|
|
171
|
+
ctypes.c_ulong,
|
|
172
|
+
ctypes.POINTER(_LinuxStatx),
|
|
173
|
+
)
|
|
174
|
+
statx.restype = ctypes.c_long
|
|
175
|
+
status = statx(number, *arguments)
|
|
176
|
+
else:
|
|
177
|
+
statx.argtypes = (
|
|
178
|
+
ctypes.c_int,
|
|
179
|
+
ctypes.c_char_p,
|
|
180
|
+
ctypes.c_int,
|
|
181
|
+
ctypes.c_uint,
|
|
182
|
+
ctypes.POINTER(_LinuxStatx),
|
|
183
|
+
)
|
|
184
|
+
statx.restype = ctypes.c_int
|
|
185
|
+
status = statx(*arguments)
|
|
186
|
+
except (AttributeError, OSError):
|
|
187
|
+
return None
|
|
188
|
+
birth_time = metadata.birth_time
|
|
189
|
+
if (
|
|
190
|
+
status != 0
|
|
191
|
+
or not metadata.mask & birth_time_mask
|
|
192
|
+
or not 0 <= birth_time.nanoseconds < 1_000_000_000
|
|
193
|
+
):
|
|
194
|
+
return None
|
|
195
|
+
birth_time_ns = birth_time.seconds * 1_000_000_000 + birth_time.nanoseconds
|
|
196
|
+
return birth_time_ns if birth_time_ns > 0 else None
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
def _repository_birth_time_ns(path: str, metadata: os.stat_result) -> int | None:
|
|
200
|
+
birth_time_ns = getattr(metadata, "st_birthtime_ns", None)
|
|
201
|
+
if birth_time_ns is not None:
|
|
202
|
+
return birth_time_ns if birth_time_ns > 0 else None
|
|
203
|
+
if os.name == "nt":
|
|
204
|
+
return metadata.st_ctime_ns if metadata.st_ctime_ns > 0 else None
|
|
205
|
+
birth_time = getattr(metadata, "st_birthtime", None)
|
|
206
|
+
if birth_time is not None:
|
|
207
|
+
return int(birth_time * 1_000_000_000) if birth_time > 0 else None
|
|
208
|
+
if not sys.platform.startswith("linux"):
|
|
209
|
+
return None
|
|
210
|
+
return _linux_repository_birth_time_ns(path)
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
@dataclass(frozen=True)
|
|
214
|
+
class GitRepositoryIdentity:
|
|
215
|
+
value: str
|
|
216
|
+
relative_path: str
|
|
217
|
+
common_directory: str
|
|
218
|
+
device: int | str
|
|
219
|
+
inode: int | str
|
|
220
|
+
birth_time_ns: int
|
|
221
|
+
object_birth_time_ns: int
|
|
222
|
+
previous_value: str | None = None
|
|
223
|
+
|
|
224
|
+
@property
|
|
225
|
+
def legacy_value(self) -> str:
|
|
226
|
+
return _identity_digest(
|
|
227
|
+
f"git-common-dir\0{self.common_directory}\0{self.device}\0{self.inode}\0"
|
|
228
|
+
f"{self.birth_time_ns}\0{self.relative_path}"
|
|
229
|
+
)
|
|
230
|
+
|
|
231
|
+
|
|
232
|
+
def _identity_digest(material: str, prefix: str = "repository_sha256_") -> str:
|
|
233
|
+
return f"{prefix}{hashlib.sha256(material.encode(errors='surrogateescape')).hexdigest()}"
|
|
234
|
+
|
|
235
|
+
|
|
236
|
+
def _repository_worktree_location(target: Path) -> tuple[Path, str] | None:
|
|
237
|
+
common_directory = _git_path(target, "rev-parse", "--path-format=absolute", "--git-common-dir")
|
|
238
|
+
worktree = _repository_worktree(target)
|
|
239
|
+
if common_directory is None or worktree is None:
|
|
240
|
+
return None
|
|
241
|
+
worktree_root, relative = worktree
|
|
242
|
+
if not _registered_worktree(target, worktree_root, common_directory):
|
|
243
|
+
return None
|
|
244
|
+
return common_directory, relative
|
|
245
|
+
|
|
246
|
+
|
|
247
|
+
def same_repository_worktree(before: Path, after: Path) -> bool:
|
|
248
|
+
before_location = _repository_worktree_location(before)
|
|
249
|
+
after_location = _repository_worktree_location(after)
|
|
250
|
+
return (
|
|
251
|
+
before_location is not None
|
|
252
|
+
and after_location is not None
|
|
253
|
+
and before_location[1] == after_location[1]
|
|
254
|
+
and _same_existing_path(before_location[0], after_location[0])
|
|
255
|
+
)
|
|
256
|
+
|
|
257
|
+
|
|
258
|
+
def _repository_identity_details(target: Path | str) -> GitRepositoryIdentity | None:
|
|
259
|
+
target = Path(target)
|
|
260
|
+
location = _repository_worktree_location(target)
|
|
261
|
+
if location is None:
|
|
262
|
+
return None
|
|
263
|
+
common_directory, relative = location
|
|
264
|
+
object_directory = _git_path(
|
|
265
|
+
target, "rev-parse", "--path-format=absolute", "--git-path", "objects"
|
|
266
|
+
)
|
|
267
|
+
if object_directory is None:
|
|
268
|
+
return None
|
|
269
|
+
canonical_directory = str(common_directory)
|
|
270
|
+
try:
|
|
271
|
+
metadata = common_directory.stat()
|
|
272
|
+
object_metadata = object_directory.stat()
|
|
273
|
+
except OSError:
|
|
274
|
+
return None
|
|
275
|
+
if not stat.S_ISDIR(metadata.st_mode) or not stat.S_ISDIR(object_metadata.st_mode):
|
|
276
|
+
return None
|
|
277
|
+
birth_time_ns = _repository_birth_time_ns(canonical_directory, metadata)
|
|
278
|
+
object_birth_time_ns = _repository_birth_time_ns(str(object_directory), object_metadata)
|
|
279
|
+
if birth_time_ns is None or object_birth_time_ns is None:
|
|
280
|
+
return None
|
|
281
|
+
device = serialize_filesystem_identity(metadata.st_dev)
|
|
282
|
+
inode = serialize_filesystem_identity(metadata.st_ino)
|
|
283
|
+
material = (
|
|
284
|
+
f"git-generation-v2\0{canonical_directory}\0{device}\0{inode}\0"
|
|
285
|
+
f"{birth_time_ns}\0{object_directory}\0"
|
|
286
|
+
f"{serialize_filesystem_identity(object_metadata.st_dev)}\0"
|
|
287
|
+
f"{serialize_filesystem_identity(object_metadata.st_ino)}\0"
|
|
288
|
+
f"{object_birth_time_ns}\0{relative}"
|
|
289
|
+
)
|
|
290
|
+
previous_value = None
|
|
291
|
+
value = _identity_digest(material)
|
|
292
|
+
if sys.platform == "linux":
|
|
293
|
+
try:
|
|
294
|
+
common_filesystem = os.statvfs(common_directory).f_fsid
|
|
295
|
+
object_filesystem = os.statvfs(object_directory).f_fsid
|
|
296
|
+
except OSError:
|
|
297
|
+
return None
|
|
298
|
+
previous_value = value
|
|
299
|
+
value = _identity_digest(
|
|
300
|
+
f"git-generation-v3\0{canonical_directory}\0{common_filesystem}\0{inode}\0"
|
|
301
|
+
f"{birth_time_ns}\0{object_directory}\0{object_filesystem}\0"
|
|
302
|
+
f"{serialize_filesystem_identity(object_metadata.st_ino)}\0"
|
|
303
|
+
f"{object_birth_time_ns}\0{relative}",
|
|
304
|
+
"repository_v3_sha256_",
|
|
305
|
+
)
|
|
306
|
+
return GitRepositoryIdentity(
|
|
307
|
+
value,
|
|
308
|
+
relative,
|
|
309
|
+
canonical_directory,
|
|
310
|
+
device,
|
|
311
|
+
inode,
|
|
312
|
+
birth_time_ns,
|
|
313
|
+
object_birth_time_ns,
|
|
314
|
+
previous_value,
|
|
315
|
+
)
|
|
316
|
+
|
|
317
|
+
|
|
318
|
+
def repository_identity(target: Path | str) -> str | None:
|
|
319
|
+
"""Identify matching Git worktree targets without storing remote credentials."""
|
|
320
|
+
identity = _repository_identity_details(target)
|
|
321
|
+
return identity.value if identity is not None else None
|
|
322
|
+
|
|
323
|
+
|
|
324
|
+
def repository_origin(target: Path) -> tuple[str, str] | None:
|
|
325
|
+
remote = git_output(target, "remote", "get-url", "origin")
|
|
326
|
+
if remote is None:
|
|
327
|
+
return None
|
|
328
|
+
if "://" in remote:
|
|
329
|
+
try:
|
|
330
|
+
parsed = urlsplit(remote)
|
|
331
|
+
port = parsed.port
|
|
332
|
+
except ValueError:
|
|
333
|
+
return None
|
|
334
|
+
if parsed.scheme not in {"https", "ssh"} or parsed.hostname is None:
|
|
335
|
+
return None
|
|
336
|
+
if parsed.query or parsed.fragment:
|
|
337
|
+
return None
|
|
338
|
+
host = parsed.hostname
|
|
339
|
+
if port is not None and port != {"https": 443, "ssh": 22}[parsed.scheme]:
|
|
340
|
+
host = f"{host}:{port}"
|
|
341
|
+
path = parsed.path
|
|
342
|
+
else:
|
|
343
|
+
authority, separator, path = remote.partition(":")
|
|
344
|
+
if not separator or "?" in path or "#" in path:
|
|
345
|
+
return None
|
|
346
|
+
host = authority.rsplit("@", 1)[-1]
|
|
347
|
+
path = path.strip("/").removesuffix(".git")
|
|
348
|
+
return (host.lower(), path) if host and path else None
|
|
349
|
+
|
|
350
|
+
|
|
351
|
+
def supports_repository_identity(connection: sqlite3.Connection) -> bool:
|
|
352
|
+
return any(
|
|
353
|
+
row["name"] == "repository_identity"
|
|
354
|
+
for row in connection.execute("PRAGMA table_info(security_targets)")
|
|
355
|
+
)
|
|
356
|
+
|
|
357
|
+
|
|
358
|
+
@dataclass(frozen=True)
|
|
359
|
+
class RepositoryTargetState:
|
|
360
|
+
target_id: str
|
|
361
|
+
target_path: str
|
|
362
|
+
stored_identity: str | None
|
|
363
|
+
resolved_path: str | None = None
|
|
364
|
+
metadata: os.stat_result | None = None
|
|
365
|
+
repository: GitRepositoryIdentity | None = None
|
|
366
|
+
ownership_matches: bool = False
|
|
367
|
+
strict_owner_matches: bool = False
|
|
368
|
+
generation_predates_history: bool = False
|
|
369
|
+
has_historical_scans: bool = False
|
|
370
|
+
missing: bool = False
|
|
371
|
+
previous_generation: str | None = None
|
|
372
|
+
|
|
373
|
+
@property
|
|
374
|
+
def live_identity(self) -> str | None:
|
|
375
|
+
return (
|
|
376
|
+
self.previous_generation or self.repository.value
|
|
377
|
+
if self.repository is not None
|
|
378
|
+
else None
|
|
379
|
+
)
|
|
380
|
+
|
|
381
|
+
@property
|
|
382
|
+
def verified_identity(self) -> str | None:
|
|
383
|
+
if not self.ownership_matches or self.repository is None:
|
|
384
|
+
return None
|
|
385
|
+
if self.stored_identity is None:
|
|
386
|
+
# Checkout ownership and repository age cannot identify an older Git generation.
|
|
387
|
+
return (
|
|
388
|
+
self.live_identity
|
|
389
|
+
if self.strict_owner_matches
|
|
390
|
+
and self.generation_predates_history
|
|
391
|
+
and not self.has_historical_scans
|
|
392
|
+
else None
|
|
393
|
+
)
|
|
394
|
+
return self.live_identity if self.live_identity == self.stored_identity else None
|
|
395
|
+
|
|
396
|
+
def require_owner(self) -> None:
|
|
397
|
+
if not self.ownership_matches:
|
|
398
|
+
raise SystemExit(
|
|
399
|
+
f"The repository checkout at {self.target_path} no longer matches its recorded "
|
|
400
|
+
"security scan history; refusing to reuse its target."
|
|
401
|
+
)
|
|
402
|
+
|
|
403
|
+
|
|
404
|
+
def _inspect_repository_target(
|
|
405
|
+
connection: sqlite3.Connection,
|
|
406
|
+
target_id: str,
|
|
407
|
+
target_path: str,
|
|
408
|
+
stored_identity: str | None,
|
|
409
|
+
*,
|
|
410
|
+
scan_columns: set[str] | None = None,
|
|
411
|
+
) -> RepositoryTargetState:
|
|
412
|
+
target = Path(target_path)
|
|
413
|
+
try:
|
|
414
|
+
resolved_path = str(target.resolve())
|
|
415
|
+
except (OSError, RuntimeError):
|
|
416
|
+
return RepositoryTargetState(target_id, target_path, stored_identity)
|
|
417
|
+
try:
|
|
418
|
+
metadata = target.stat()
|
|
419
|
+
except (FileNotFoundError, NotADirectoryError):
|
|
420
|
+
return RepositoryTargetState(
|
|
421
|
+
target_id,
|
|
422
|
+
target_path,
|
|
423
|
+
stored_identity,
|
|
424
|
+
resolved_path=resolved_path,
|
|
425
|
+
ownership_matches=True,
|
|
426
|
+
missing=True,
|
|
427
|
+
)
|
|
428
|
+
except OSError:
|
|
429
|
+
return RepositoryTargetState(
|
|
430
|
+
target_id, target_path, stored_identity, resolved_path=resolved_path
|
|
431
|
+
)
|
|
432
|
+
repository = _repository_identity_details(target)
|
|
433
|
+
if scan_columns is None:
|
|
434
|
+
scan_columns = {row["name"] for row in connection.execute("PRAGMA table_info(scans)")}
|
|
435
|
+
historical_scan = False
|
|
436
|
+
recorded_owner = False
|
|
437
|
+
malformed_owner = False
|
|
438
|
+
mismatch = False
|
|
439
|
+
strict_owner_matches = False
|
|
440
|
+
generation_predates_history = False
|
|
441
|
+
ownership_matches = True
|
|
442
|
+
if {"target_id", "target_path"} <= scan_columns:
|
|
443
|
+
recorded_generations = (
|
|
444
|
+
{
|
|
445
|
+
row[0]
|
|
446
|
+
for row in connection.execute(
|
|
447
|
+
"SELECT DISTINCT repository_generation FROM scans "
|
|
448
|
+
"WHERE (target_id = ? OR target_path = ?) AND repository_generation IS NOT NULL",
|
|
449
|
+
(target_id, target_path),
|
|
450
|
+
)
|
|
451
|
+
}
|
|
452
|
+
if "repository_generation" in scan_columns
|
|
453
|
+
else set()
|
|
454
|
+
)
|
|
455
|
+
generation_conflict = bool(recorded_generations) and (
|
|
456
|
+
repository is None
|
|
457
|
+
or recorded_generations not in ({repository.value}, {repository.previous_value})
|
|
458
|
+
)
|
|
459
|
+
if not {"target_device", "target_inode"} <= scan_columns:
|
|
460
|
+
historical_scan = (
|
|
461
|
+
connection.execute(
|
|
462
|
+
"SELECT 1 FROM scans WHERE target_id = ? OR target_path = ? LIMIT 1",
|
|
463
|
+
(target_id, target_path),
|
|
464
|
+
).fetchone()
|
|
465
|
+
is not None
|
|
466
|
+
)
|
|
467
|
+
strict_owner_matches = not historical_scan
|
|
468
|
+
generation_predates_history = repository is not None and not historical_scan
|
|
469
|
+
else:
|
|
470
|
+
strict_owner_matches = True
|
|
471
|
+
timestamps = (
|
|
472
|
+
", started_at, created_at" if {"started_at", "created_at"} <= scan_columns else ""
|
|
473
|
+
)
|
|
474
|
+
revisions = ", target_revision" if "target_revision" in scan_columns else ""
|
|
475
|
+
scans = connection.execute(
|
|
476
|
+
f"""
|
|
477
|
+
SELECT target_device, target_inode{timestamps}{revisions} FROM scans
|
|
478
|
+
WHERE target_id = ? OR target_path = ?
|
|
479
|
+
""",
|
|
480
|
+
(target_id, target_path),
|
|
481
|
+
).fetchall()
|
|
482
|
+
git_scans = [
|
|
483
|
+
scan
|
|
484
|
+
for scan in scans
|
|
485
|
+
if "target_revision" not in scan.keys() or scan["target_revision"] != "unversioned"
|
|
486
|
+
]
|
|
487
|
+
generation_predates_history = repository is not None and _repository_predates_history(
|
|
488
|
+
repository, git_scans
|
|
489
|
+
)
|
|
490
|
+
for scan in scans:
|
|
491
|
+
historical_scan = True
|
|
492
|
+
device, inode = scan["target_device"], scan["target_inode"]
|
|
493
|
+
if device is None and inode is None:
|
|
494
|
+
strict_owner_matches = False
|
|
495
|
+
continue
|
|
496
|
+
if device is None or inode is None:
|
|
497
|
+
malformed_owner = True
|
|
498
|
+
strict_owner_matches = False
|
|
499
|
+
continue
|
|
500
|
+
recorded_owner = True
|
|
501
|
+
if not stored_filesystem_identity_matches(device, metadata.st_dev):
|
|
502
|
+
strict_owner_matches = False
|
|
503
|
+
mismatch = mismatch or sys.platform != "linux"
|
|
504
|
+
if not stored_filesystem_identity_matches(inode, metadata.st_ino):
|
|
505
|
+
mismatch = True
|
|
506
|
+
strict_owner_matches = False
|
|
507
|
+
stored_matches = repository is not None and (
|
|
508
|
+
stored_identity
|
|
509
|
+
in {
|
|
510
|
+
repository.value,
|
|
511
|
+
repository.legacy_value,
|
|
512
|
+
repository.previous_value or repository.value,
|
|
513
|
+
}
|
|
514
|
+
or (
|
|
515
|
+
repository.value.startswith("repository_v3_sha256_")
|
|
516
|
+
and recorded_generations == {repository.value}
|
|
517
|
+
and isinstance(stored_identity, str)
|
|
518
|
+
and re.fullmatch(r"repository_sha256_[0-9a-f]{64}", stored_identity) is not None
|
|
519
|
+
)
|
|
520
|
+
)
|
|
521
|
+
verified_repository = repository is not None and (
|
|
522
|
+
stored_matches
|
|
523
|
+
or recorded_generations in ({repository.value}, {repository.previous_value})
|
|
524
|
+
)
|
|
525
|
+
ownership_matches = not (
|
|
526
|
+
malformed_owner
|
|
527
|
+
or mismatch
|
|
528
|
+
and (
|
|
529
|
+
not verified_repository or repository is None or repository.relative_path == "."
|
|
530
|
+
)
|
|
531
|
+
or stored_identity is not None
|
|
532
|
+
and (not stored_matches or historical_scan and not recorded_owner)
|
|
533
|
+
or stored_identity is None
|
|
534
|
+
and repository is not None
|
|
535
|
+
and historical_scan
|
|
536
|
+
and not verified_repository
|
|
537
|
+
and not generation_predates_history
|
|
538
|
+
)
|
|
539
|
+
ownership_matches = ownership_matches and not generation_conflict
|
|
540
|
+
previous_generation = None
|
|
541
|
+
if (
|
|
542
|
+
ownership_matches
|
|
543
|
+
and repository is not None
|
|
544
|
+
and repository.previous_value is not None
|
|
545
|
+
and supports_repository_identity(connection)
|
|
546
|
+
and "repository_generation" in scan_columns
|
|
547
|
+
and connection.execute(
|
|
548
|
+
"SELECT 1 FROM security_targets WHERE repository_identity = ? "
|
|
549
|
+
"UNION ALL SELECT 1 FROM scans WHERE repository_generation = ? LIMIT 1",
|
|
550
|
+
(repository.previous_value, repository.previous_value),
|
|
551
|
+
).fetchone()
|
|
552
|
+
is not None
|
|
553
|
+
):
|
|
554
|
+
previous_generation = repository.previous_value
|
|
555
|
+
return RepositoryTargetState(
|
|
556
|
+
target_id,
|
|
557
|
+
target_path,
|
|
558
|
+
stored_identity,
|
|
559
|
+
resolved_path,
|
|
560
|
+
metadata,
|
|
561
|
+
repository,
|
|
562
|
+
ownership_matches,
|
|
563
|
+
strict_owner_matches,
|
|
564
|
+
generation_predates_history,
|
|
565
|
+
historical_scan,
|
|
566
|
+
previous_generation=previous_generation,
|
|
567
|
+
)
|
|
568
|
+
|
|
569
|
+
|
|
570
|
+
def verified_repository_identity(
|
|
571
|
+
connection: sqlite3.Connection,
|
|
572
|
+
target_id: str,
|
|
573
|
+
target_path: str,
|
|
574
|
+
*,
|
|
575
|
+
stored_identity: str | None = None,
|
|
576
|
+
) -> str | None:
|
|
577
|
+
try:
|
|
578
|
+
return _inspect_repository_target(
|
|
579
|
+
connection, target_id, target_path, stored_identity
|
|
580
|
+
).verified_identity
|
|
581
|
+
except SystemExit:
|
|
582
|
+
# Historical identity enrichment is optional; active registration stays strict.
|
|
583
|
+
return None
|
|
584
|
+
|
|
585
|
+
|
|
586
|
+
def scan_repository_generation(scan: sqlite3.Row | dict) -> str | None:
|
|
587
|
+
return scan["repository_generation"] if "repository_generation" in scan.keys() else None
|
|
588
|
+
|
|
589
|
+
|
|
590
|
+
def scan_repository_group(scan: sqlite3.Row | dict) -> tuple[str, str]:
|
|
591
|
+
generation = scan_repository_generation(scan)
|
|
592
|
+
return ("repository", generation) if generation is not None else ("target", scan["target_id"])
|
|
593
|
+
|
|
594
|
+
|
|
595
|
+
@dataclass(frozen=True)
|
|
596
|
+
class RepositoryScanScope:
|
|
597
|
+
"""A verified generation and, separately, its exact target's legacy scans."""
|
|
598
|
+
|
|
599
|
+
generation: str | None = None
|
|
600
|
+
target_id: str | None = None
|
|
601
|
+
exact_target: bool = False
|
|
602
|
+
available: bool = True
|
|
603
|
+
|
|
604
|
+
def sql(
|
|
605
|
+
self, alias: str = "scans", *, supports_generation: bool = True
|
|
606
|
+
) -> tuple[str, tuple[str, ...]]:
|
|
607
|
+
if not self.available:
|
|
608
|
+
return "0", ()
|
|
609
|
+
if self.exact_target:
|
|
610
|
+
return (f"{alias}.target_id = ?", (self.target_id,)) if self.target_id else ("0", ())
|
|
611
|
+
clauses, values = [], []
|
|
612
|
+
if self.generation is not None and supports_generation:
|
|
613
|
+
clauses.append(f"{alias}.repository_generation = ?")
|
|
614
|
+
values.append(self.generation)
|
|
615
|
+
if self.target_id:
|
|
616
|
+
legacy = f"{alias}.repository_generation IS NULL AND " if supports_generation else ""
|
|
617
|
+
clauses.append(f"({legacy}{alias}.target_id = ?)")
|
|
618
|
+
values.append(self.target_id)
|
|
619
|
+
return (f"({' OR '.join(clauses)})", tuple(values)) if clauses else ("0", ())
|
|
620
|
+
|
|
621
|
+
def contains(self, scan: sqlite3.Row | dict) -> bool:
|
|
622
|
+
if not self.available:
|
|
623
|
+
return False
|
|
624
|
+
if self.exact_target:
|
|
625
|
+
return bool(self.target_id) and scan["target_id"] == self.target_id
|
|
626
|
+
generation = scan_repository_generation(scan)
|
|
627
|
+
if generation is not None:
|
|
628
|
+
return generation == self.generation
|
|
629
|
+
return bool(self.target_id) and scan["target_id"] == self.target_id
|
|
630
|
+
|
|
631
|
+
|
|
632
|
+
class RepositoryIdentityCache:
|
|
633
|
+
"""One request's saved identities and verified live aliases."""
|
|
634
|
+
|
|
635
|
+
def __init__(self, connection: sqlite3.Connection) -> None:
|
|
636
|
+
self.connection = connection
|
|
637
|
+
self.supports_identity = supports_repository_identity(connection)
|
|
638
|
+
self.scan_columns = {row["name"] for row in connection.execute("PRAGMA table_info(scans)")}
|
|
639
|
+
self.supports_generation = "repository_generation" in self.scan_columns
|
|
640
|
+
identity_column = "repository_identity" if self.supports_identity else "NULL"
|
|
641
|
+
self.targets = {
|
|
642
|
+
row["target_id"]: row
|
|
643
|
+
for row in connection.execute(
|
|
644
|
+
"SELECT id AS target_id, current_path AS target_path, "
|
|
645
|
+
f"{identity_column} AS repository_identity FROM security_targets"
|
|
646
|
+
)
|
|
647
|
+
}
|
|
648
|
+
self.targets_by_path = {row["target_path"]: row for row in self.targets.values()}
|
|
649
|
+
self._states: dict[tuple[str, str, str | None], RepositoryTargetState] = {}
|
|
650
|
+
self._origins: dict[str, tuple[str, str] | None] = {}
|
|
651
|
+
|
|
652
|
+
def for_row(self, row: sqlite3.Row | dict) -> RepositoryTargetState:
|
|
653
|
+
target = self.targets.get(row["target_id"])
|
|
654
|
+
stored = (
|
|
655
|
+
row["repository_identity"]
|
|
656
|
+
if "repository_identity" in row.keys()
|
|
657
|
+
else target["repository_identity"]
|
|
658
|
+
if target is not None
|
|
659
|
+
else None
|
|
660
|
+
)
|
|
661
|
+
key = (
|
|
662
|
+
row["target_id"] or "",
|
|
663
|
+
row["target_path"],
|
|
664
|
+
stored,
|
|
665
|
+
)
|
|
666
|
+
if key not in self._states:
|
|
667
|
+
self._states[key] = _inspect_repository_target(
|
|
668
|
+
self.connection, *key, scan_columns=self.scan_columns
|
|
669
|
+
)
|
|
670
|
+
return self._states[key]
|
|
671
|
+
|
|
672
|
+
def for_path(self, target_path: str) -> RepositoryTargetState:
|
|
673
|
+
path = Path(target_path)
|
|
674
|
+
if os.name != "nt" and ".." in path.parts:
|
|
675
|
+
last_parent = len(path.parts) - 1 - path.parts[::-1].index("..")
|
|
676
|
+
try:
|
|
677
|
+
# A missing final checkout can retain history. Every component
|
|
678
|
+
# traversed before a parent segment must still exist.
|
|
679
|
+
Path(*path.parts[: last_parent + 1]).stat()
|
|
680
|
+
except (OSError, RuntimeError):
|
|
681
|
+
return RepositoryTargetState("", target_path, None)
|
|
682
|
+
row = self.targets_by_path.get(target_path)
|
|
683
|
+
if row is None:
|
|
684
|
+
try:
|
|
685
|
+
canonical = str(Path(target_path).resolve())
|
|
686
|
+
except (OSError, RuntimeError):
|
|
687
|
+
canonical = None
|
|
688
|
+
row = self.targets_by_path.get(canonical)
|
|
689
|
+
if row is None:
|
|
690
|
+
row = next(
|
|
691
|
+
(
|
|
692
|
+
target
|
|
693
|
+
for target in self.targets.values()
|
|
694
|
+
if _same_existing_path(Path(target["target_path"]), Path(target_path))
|
|
695
|
+
),
|
|
696
|
+
None,
|
|
697
|
+
)
|
|
698
|
+
return self.for_row(
|
|
699
|
+
row
|
|
700
|
+
if row is not None
|
|
701
|
+
else {
|
|
702
|
+
"target_id": "",
|
|
703
|
+
"target_path": target_path,
|
|
704
|
+
"repository_identity": None,
|
|
705
|
+
}
|
|
706
|
+
)
|
|
707
|
+
|
|
708
|
+
def scope(self, target_id: str) -> RepositoryScanScope:
|
|
709
|
+
target = self.targets.get(target_id)
|
|
710
|
+
return (
|
|
711
|
+
self._scope_for_state(self.for_row(target))
|
|
712
|
+
if target is not None
|
|
713
|
+
else RepositoryScanScope(target_id=target_id)
|
|
714
|
+
)
|
|
715
|
+
|
|
716
|
+
def history_path(self, target_path: str) -> str:
|
|
717
|
+
"""Use the worktree root when a subdirectory has no saved history of its own."""
|
|
718
|
+
requested = self.for_path(target_path)
|
|
719
|
+
if (
|
|
720
|
+
not requested.ownership_matches
|
|
721
|
+
or requested.target_id
|
|
722
|
+
or requested.repository is None
|
|
723
|
+
or requested.repository.relative_path == "."
|
|
724
|
+
):
|
|
725
|
+
return target_path
|
|
726
|
+
if (
|
|
727
|
+
self.supports_generation
|
|
728
|
+
and self.connection.execute(
|
|
729
|
+
"SELECT 1 FROM scans WHERE repository_generation = ? LIMIT 1",
|
|
730
|
+
(requested.live_identity,),
|
|
731
|
+
).fetchone()
|
|
732
|
+
):
|
|
733
|
+
return target_path
|
|
734
|
+
worktree = _repository_worktree(Path(target_path))
|
|
735
|
+
return str(worktree[0]) if worktree is not None else target_path
|
|
736
|
+
|
|
737
|
+
def scope_for_path(self, target_path: str) -> RepositoryScanScope:
|
|
738
|
+
return self._scope_for_state(self.for_path(target_path))
|
|
739
|
+
|
|
740
|
+
def scope_for_scan(self, scan: sqlite3.Row | dict) -> RepositoryScanScope:
|
|
741
|
+
requested = self.for_row(scan)
|
|
742
|
+
generation = scan_repository_generation(scan)
|
|
743
|
+
if not requested.ownership_matches or (
|
|
744
|
+
generation is not None and generation != requested.live_identity
|
|
745
|
+
):
|
|
746
|
+
return RepositoryScanScope(available=False)
|
|
747
|
+
return RepositoryScanScope(generation, requested.target_id)
|
|
748
|
+
|
|
749
|
+
def _scope_for_state(self, requested: RepositoryTargetState) -> RepositoryScanScope:
|
|
750
|
+
if not requested.ownership_matches:
|
|
751
|
+
return RepositoryScanScope(available=False)
|
|
752
|
+
return RepositoryScanScope(
|
|
753
|
+
requested.live_identity if not requested.missing else None,
|
|
754
|
+
requested.target_id or None,
|
|
755
|
+
exact_target=requested.missing,
|
|
756
|
+
)
|
|
757
|
+
|
|
758
|
+
def origin(self, state: RepositoryTargetState) -> tuple[str, str] | None:
|
|
759
|
+
if state.target_path not in self._origins:
|
|
760
|
+
self._origins[state.target_path] = repository_origin(Path(state.target_path))
|
|
761
|
+
return self._origins[state.target_path]
|
|
762
|
+
|
|
763
|
+
|
|
764
|
+
def require_scan_checkout_owner(connection: sqlite3.Connection, scan: sqlite3.Row | dict) -> None:
|
|
765
|
+
identities = RepositoryIdentityCache(connection)
|
|
766
|
+
owner = identities.for_row(scan)
|
|
767
|
+
owner.require_owner()
|
|
768
|
+
if owner.missing or not identities.scope_for_scan(scan).contains(scan):
|
|
769
|
+
raise SystemExit("The saved scan no longer matches the selected repository checkout.")
|
|
770
|
+
|
|
771
|
+
|
|
772
|
+
def upgrade_linux_repository_generations(connection: sqlite3.Connection) -> None:
|
|
773
|
+
"""Convert only old digests authenticated before their device evidence changes."""
|
|
774
|
+
if sys.platform != "linux":
|
|
775
|
+
return
|
|
776
|
+
targets = connection.execute(
|
|
777
|
+
"SELECT id, current_path, repository_identity FROM security_targets "
|
|
778
|
+
"WHERE repository_identity GLOB 'repository_sha256_*' OR EXISTS ("
|
|
779
|
+
"SELECT 1 FROM scans WHERE (scans.target_id = security_targets.id "
|
|
780
|
+
"OR scans.target_path = security_targets.current_path) "
|
|
781
|
+
"AND repository_generation GLOB 'repository_sha256_*')"
|
|
782
|
+
).fetchall()
|
|
783
|
+
for target in targets:
|
|
784
|
+
try:
|
|
785
|
+
state = _inspect_repository_target(
|
|
786
|
+
connection, target["id"], target["current_path"], target["repository_identity"]
|
|
787
|
+
)
|
|
788
|
+
except SystemExit:
|
|
789
|
+
continue
|
|
790
|
+
_upgrade_repository_generation(connection, state)
|
|
791
|
+
|
|
792
|
+
|
|
793
|
+
def _upgrade_repository_generation(
|
|
794
|
+
connection: sqlite3.Connection, state: RepositoryTargetState
|
|
795
|
+
) -> RepositoryTargetState:
|
|
796
|
+
identity = state.repository
|
|
797
|
+
if (
|
|
798
|
+
identity is None
|
|
799
|
+
or identity.previous_value is None
|
|
800
|
+
or state.stored_identity not in (None, identity.value, identity.previous_value)
|
|
801
|
+
or not state.ownership_matches
|
|
802
|
+
or not state.strict_owner_matches
|
|
803
|
+
):
|
|
804
|
+
return state
|
|
805
|
+
connection.execute(
|
|
806
|
+
"UPDATE security_targets SET repository_identity = ? WHERE repository_identity = ?",
|
|
807
|
+
(identity.value, identity.previous_value),
|
|
808
|
+
)
|
|
809
|
+
connection.execute(
|
|
810
|
+
"UPDATE scans SET repository_generation = ? WHERE repository_generation = ?",
|
|
811
|
+
(identity.value, identity.previous_value),
|
|
812
|
+
)
|
|
813
|
+
return replace(
|
|
814
|
+
state,
|
|
815
|
+
stored_identity=(
|
|
816
|
+
identity.value
|
|
817
|
+
if state.stored_identity == identity.previous_value
|
|
818
|
+
else state.stored_identity
|
|
819
|
+
),
|
|
820
|
+
previous_generation=None,
|
|
821
|
+
)
|
|
822
|
+
|
|
823
|
+
|
|
824
|
+
def _pre_release_repository_identities(identity: GitRepositoryIdentity) -> set[str]:
|
|
825
|
+
directory = os.path.normcase(identity.common_directory)
|
|
826
|
+
relative = os.path.normcase(os.fspath(Path(identity.relative_path))).replace(os.sep, "/")
|
|
827
|
+
prefix = f"git-common-dir\0{directory}\0{identity.device}\0{identity.inode}\0"
|
|
828
|
+
identities = {_identity_digest(f"{prefix}{relative}")}
|
|
829
|
+
try:
|
|
830
|
+
generation = (Path(identity.common_directory) / "description").lstat()
|
|
831
|
+
except OSError:
|
|
832
|
+
return identities
|
|
833
|
+
if stat.S_ISREG(generation.st_mode):
|
|
834
|
+
identities.add(
|
|
835
|
+
_identity_digest(
|
|
836
|
+
f"{prefix}git-description\0"
|
|
837
|
+
f"{serialize_filesystem_identity(generation.st_dev)}\0"
|
|
838
|
+
f"{serialize_filesystem_identity(generation.st_ino)}\0"
|
|
839
|
+
f"{generation.st_ctime_ns}\0{relative}"
|
|
840
|
+
)
|
|
841
|
+
)
|
|
842
|
+
return identities
|
|
843
|
+
|
|
844
|
+
|
|
845
|
+
def _timestamp_ns(value: str) -> int | Fraction | None:
|
|
846
|
+
try:
|
|
847
|
+
# Python 3.10 requires three or six fractional digits; stored timestamps
|
|
848
|
+
# can come from newer runtimes that accept any RFC 3339 precision.
|
|
849
|
+
fraction = re.search(r"\.(\d+)(?:[Zz]|[+-]\d{2}:\d{2})$", value)
|
|
850
|
+
normalized = value
|
|
851
|
+
if (
|
|
852
|
+
fraction is not None
|
|
853
|
+
and fraction[1].isascii()
|
|
854
|
+
and re.search(r"[0-9]{2}:[0-9]{2}:[0-9]{2}$", value[: fraction.start()]) is not None
|
|
855
|
+
):
|
|
856
|
+
normalized = (
|
|
857
|
+
value[: fraction.start(1)]
|
|
858
|
+
+ fraction[1][:6].ljust(6, "0")
|
|
859
|
+
+ value[fraction.end(1) :]
|
|
860
|
+
)
|
|
861
|
+
if normalized.endswith(("Z", "z")):
|
|
862
|
+
normalized = normalized[:-1] + "+00:00"
|
|
863
|
+
timestamp = datetime.fromisoformat(normalized)
|
|
864
|
+
except (TypeError, ValueError):
|
|
865
|
+
return None
|
|
866
|
+
if timestamp.tzinfo is None:
|
|
867
|
+
return None
|
|
868
|
+
delta = timestamp - datetime(1970, 1, 1, tzinfo=timezone.utc)
|
|
869
|
+
nanoseconds = (delta.days * 86400 + delta.seconds) * 1_000_000_000 + delta.microseconds * 1000
|
|
870
|
+
# datetime truncates RFC 3339 fractions after six digits; sealed timestamps retain them.
|
|
871
|
+
if fraction is not None and len(fraction[1]) > 6:
|
|
872
|
+
return nanoseconds + Fraction(Decimal("0." + fraction[1][6:])) * 1000
|
|
873
|
+
return nanoseconds
|
|
874
|
+
|
|
875
|
+
|
|
876
|
+
def _repository_predates_history(
|
|
877
|
+
identity: GitRepositoryIdentity,
|
|
878
|
+
scans: list[sqlite3.Row],
|
|
879
|
+
*,
|
|
880
|
+
empty_timestamp: str | None = None,
|
|
881
|
+
) -> bool:
|
|
882
|
+
if not scans and empty_timestamp is None:
|
|
883
|
+
return True
|
|
884
|
+
if any(not {"started_at", "created_at"} <= set(scan.keys()) for scan in scans):
|
|
885
|
+
return False
|
|
886
|
+
timestamps = (
|
|
887
|
+
[_timestamp_ns(scan[column]) for scan in scans for column in ("started_at", "created_at")]
|
|
888
|
+
if scans
|
|
889
|
+
else [_timestamp_ns(empty_timestamp)]
|
|
890
|
+
)
|
|
891
|
+
repository_birth = max(identity.birth_time_ns, identity.object_birth_time_ns)
|
|
892
|
+
return all(value is not None for value in timestamps) and repository_birth <= min(timestamps)
|
|
893
|
+
|
|
894
|
+
|
|
895
|
+
def normalize_pre_release_repository_identities(connection: sqlite3.Connection) -> None:
|
|
896
|
+
"""Retain only individually established pre-release identity bindings."""
|
|
897
|
+
if not supports_repository_identity(connection):
|
|
898
|
+
return
|
|
899
|
+
identities = RepositoryIdentityCache(connection)
|
|
900
|
+
targets = connection.execute(
|
|
901
|
+
"SELECT id AS target_id, current_path AS target_path, created_at, repository_identity "
|
|
902
|
+
"FROM security_targets WHERE repository_identity IS NOT NULL"
|
|
903
|
+
).fetchall()
|
|
904
|
+
states = {target["target_id"]: identities.for_row(target) for target in targets}
|
|
905
|
+
anchors = {
|
|
906
|
+
state.stored_identity: state.repository
|
|
907
|
+
for state in states.values()
|
|
908
|
+
if state.repository is not None
|
|
909
|
+
and state.ownership_matches
|
|
910
|
+
and state.stored_identity
|
|
911
|
+
in {
|
|
912
|
+
state.repository.value,
|
|
913
|
+
state.repository.legacy_value,
|
|
914
|
+
state.repository.previous_value or state.repository.value,
|
|
915
|
+
}
|
|
916
|
+
}
|
|
917
|
+
for target in targets:
|
|
918
|
+
stored = target["repository_identity"]
|
|
919
|
+
state = states[target["target_id"]]
|
|
920
|
+
scans = connection.execute(
|
|
921
|
+
"SELECT started_at, created_at FROM scans WHERE target_id = ? OR target_path = ?",
|
|
922
|
+
(target["target_id"], target["target_path"]),
|
|
923
|
+
).fetchall()
|
|
924
|
+
anchor = anchors.get(stored)
|
|
925
|
+
identity = state.repository
|
|
926
|
+
replacement = None
|
|
927
|
+
if anchor is not None:
|
|
928
|
+
if _repository_predates_history(anchor, scans, empty_timestamp=target["created_at"]):
|
|
929
|
+
replacement = stored
|
|
930
|
+
elif (
|
|
931
|
+
identity is not None
|
|
932
|
+
and state.strict_owner_matches
|
|
933
|
+
and stored in _pre_release_repository_identities(identity)
|
|
934
|
+
and _repository_predates_history(identity, scans, empty_timestamp=target["created_at"])
|
|
935
|
+
):
|
|
936
|
+
replacement = identity.legacy_value
|
|
937
|
+
if replacement != stored:
|
|
938
|
+
connection.execute(
|
|
939
|
+
"UPDATE security_targets SET repository_identity = ? "
|
|
940
|
+
"WHERE id = ? AND repository_identity = ?",
|
|
941
|
+
(replacement, target["target_id"], stored),
|
|
942
|
+
)
|
|
943
|
+
|
|
944
|
+
|
|
945
|
+
def _bind_unscanned_repository_identity(
|
|
946
|
+
connection: sqlite3.Connection,
|
|
947
|
+
target_id: str,
|
|
948
|
+
target_path: str,
|
|
949
|
+
identity: str | None,
|
|
950
|
+
*,
|
|
951
|
+
previous_identity: str | None = None,
|
|
952
|
+
) -> bool:
|
|
953
|
+
return (
|
|
954
|
+
connection.execute(
|
|
955
|
+
"""
|
|
956
|
+
UPDATE security_targets
|
|
957
|
+
SET repository_identity = ?
|
|
958
|
+
WHERE id = ? AND current_path = ? AND repository_identity IS ?
|
|
959
|
+
AND NOT EXISTS (
|
|
960
|
+
SELECT 1 FROM scans
|
|
961
|
+
WHERE scans.target_id = security_targets.id
|
|
962
|
+
OR scans.target_path = security_targets.current_path
|
|
963
|
+
)
|
|
964
|
+
""",
|
|
965
|
+
(identity, target_id, target_path, previous_identity),
|
|
966
|
+
).rowcount
|
|
967
|
+
== 1
|
|
968
|
+
)
|
|
969
|
+
|
|
970
|
+
|
|
971
|
+
def backfill_repository_identities(connection: sqlite3.Connection) -> None:
|
|
972
|
+
if not supports_repository_identity(connection):
|
|
973
|
+
return
|
|
974
|
+
targets = connection.execute(
|
|
975
|
+
"""
|
|
976
|
+
SELECT targets.id, targets.current_path
|
|
977
|
+
FROM security_targets AS targets
|
|
978
|
+
WHERE targets.repository_identity IS NULL
|
|
979
|
+
AND NOT EXISTS (
|
|
980
|
+
SELECT 1 FROM scans
|
|
981
|
+
WHERE scans.target_id = targets.id OR scans.target_path = targets.current_path
|
|
982
|
+
)
|
|
983
|
+
"""
|
|
984
|
+
).fetchall()
|
|
985
|
+
for target in targets:
|
|
986
|
+
identity = verified_repository_identity(
|
|
987
|
+
connection, str(target["id"]), target["current_path"]
|
|
988
|
+
)
|
|
989
|
+
if identity is not None:
|
|
990
|
+
_bind_unscanned_repository_identity(
|
|
991
|
+
connection, str(target["id"]), target["current_path"], identity
|
|
992
|
+
)
|
|
993
|
+
|
|
994
|
+
|
|
18
995
|
def backfill_security_targets(connection: sqlite3.Connection) -> None:
|
|
19
996
|
"""Fill missing target IDs on scans and workspaces in the caller’s transaction."""
|
|
997
|
+
workspace_columns = {row["name"] for row in connection.execute("PRAGMA table_info(workspaces)")}
|
|
998
|
+
validated_workspace = (
|
|
999
|
+
" AND target_path_unvalidated = 0" if "target_path_unvalidated" in workspace_columns else ""
|
|
1000
|
+
)
|
|
20
1001
|
rows = connection.execute(
|
|
21
|
-
"""
|
|
22
|
-
SELECT target_path FROM workspaces WHERE target_path IS NOT NULL
|
|
1002
|
+
f"""
|
|
1003
|
+
SELECT target_path FROM workspaces WHERE target_path IS NOT NULL{validated_workspace}
|
|
23
1004
|
UNION
|
|
24
1005
|
SELECT target_path FROM scans
|
|
25
1006
|
"""
|
|
26
1007
|
).fetchall()
|
|
27
1008
|
for row in rows:
|
|
28
1009
|
target_path = row["target_path"]
|
|
29
|
-
target_id = ensure_security_target(connection, target_path)
|
|
1010
|
+
target_id = ensure_security_target(connection, target_path, verify_ownership=False)
|
|
30
1011
|
connection.execute(
|
|
31
|
-
"UPDATE workspaces SET target_id = ? WHERE target_path = ? AND target_id IS NULL"
|
|
1012
|
+
"UPDATE workspaces SET target_id = ? WHERE target_path = ? AND target_id IS NULL"
|
|
1013
|
+
+ validated_workspace,
|
|
32
1014
|
(target_id, target_path),
|
|
33
1015
|
)
|
|
34
1016
|
connection.execute(
|
|
35
1017
|
"UPDATE scans SET target_id = ? WHERE target_path = ? AND target_id IS NULL",
|
|
36
1018
|
(target_id, target_path),
|
|
37
1019
|
)
|
|
1020
|
+
backfill_repository_identities(connection)
|
|
38
1021
|
|
|
39
1022
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
1023
|
+
@dataclass(frozen=True)
|
|
1024
|
+
class RegisteredRepositoryTarget:
|
|
1025
|
+
target_id: str
|
|
1026
|
+
repository_generation: str | None
|
|
1027
|
+
|
|
1028
|
+
|
|
1029
|
+
def _record_checkout_root(
|
|
1030
|
+
connection: sqlite3.Connection, row: sqlite3.Row, state: RepositoryTargetState, inserted: bool
|
|
1031
|
+
) -> None:
|
|
1032
|
+
if "git_checkout_root" not in row.keys():
|
|
1033
|
+
return
|
|
1034
|
+
previous = row["git_checkout_root"]
|
|
1035
|
+
# Migrations and old rows cannot attest to the former boundary of a checkout.
|
|
1036
|
+
if previous is None and (not inserted or state.has_historical_scans):
|
|
1037
|
+
return
|
|
1038
|
+
target = Path(state.target_path)
|
|
1039
|
+
if (
|
|
1040
|
+
state.metadata is None
|
|
1041
|
+
or not stat.S_ISDIR(state.metadata.st_mode)
|
|
1042
|
+
or state.resolved_path != state.target_path
|
|
1043
|
+
):
|
|
1044
|
+
return
|
|
1045
|
+
try:
|
|
1046
|
+
root = _protected_repository_root(target)
|
|
1047
|
+
except (OSError, RuntimeError):
|
|
1048
|
+
return
|
|
1049
|
+
if previous is None or root in Path(previous).parents:
|
|
1050
|
+
connection.execute(
|
|
1051
|
+
"UPDATE security_targets SET git_checkout_root = ? "
|
|
1052
|
+
"WHERE id = ? AND current_path = ? AND git_checkout_root IS ?",
|
|
1053
|
+
(str(root), state.target_id, state.target_path, previous),
|
|
1054
|
+
)
|
|
1055
|
+
|
|
1056
|
+
|
|
1057
|
+
def register_security_target(
|
|
1058
|
+
connection: sqlite3.Connection, target_path: str, *, verify_ownership: bool = True
|
|
1059
|
+
) -> RegisteredRepositoryTarget:
|
|
1060
|
+
target_query = "SELECT * FROM security_targets WHERE current_path = ?"
|
|
1061
|
+
existing = connection.execute(target_query, (target_path,)).fetchone()
|
|
1062
|
+
inserted = False
|
|
1063
|
+
if existing is None:
|
|
1064
|
+
if verify_ownership:
|
|
1065
|
+
# Pin ambient Git before the new row temporarily lacks a checkout boundary.
|
|
1066
|
+
pin_scoped_git(Path(target_path))
|
|
1067
|
+
target_id = stable_target_id(Path(target_path))
|
|
1068
|
+
timestamp = datetime.now(timezone.utc).isoformat().replace("+00:00", "Z")
|
|
1069
|
+
# Leave repository_identity NULL until the guarded binding below succeeds.
|
|
1070
|
+
insert = connection.execute(
|
|
1071
|
+
"""
|
|
1072
|
+
INSERT OR IGNORE INTO security_targets (
|
|
1073
|
+
id, current_path, display_name, created_at, updated_at
|
|
1074
|
+
) VALUES (?, ?, ?, ?, ?)
|
|
1075
|
+
""",
|
|
1076
|
+
(target_id, target_path, Path(target_path).name, timestamp, timestamp),
|
|
1077
|
+
)
|
|
1078
|
+
inserted = insert.rowcount == 1
|
|
1079
|
+
existing = connection.execute(target_query, (target_path,)).fetchone()
|
|
1080
|
+
if existing is None:
|
|
1081
|
+
existing = connection.execute(
|
|
1082
|
+
"SELECT * FROM security_targets WHERE id = ?", (target_id,)
|
|
1083
|
+
).fetchone()
|
|
1084
|
+
if existing is None or Path(existing["current_path"]) != Path(target_path):
|
|
1085
|
+
raise SystemExit("The repository target changed while it was being registered.")
|
|
1086
|
+
target_path = existing["current_path"]
|
|
1087
|
+
target_id = str(existing["id"])
|
|
1088
|
+
if "repository_identity" not in existing.keys():
|
|
1089
|
+
return RegisteredRepositoryTarget(target_id, None)
|
|
1090
|
+
try:
|
|
1091
|
+
state = _inspect_repository_target(
|
|
1092
|
+
connection, target_id, target_path, existing["repository_identity"]
|
|
1093
|
+
)
|
|
1094
|
+
except SystemExit:
|
|
1095
|
+
if verify_ownership:
|
|
1096
|
+
raise
|
|
1097
|
+
return RegisteredRepositoryTarget(target_id, None)
|
|
1098
|
+
if not state.has_historical_scans and state.live_identity != state.stored_identity:
|
|
1099
|
+
_bind_unscanned_repository_identity(
|
|
1100
|
+
connection,
|
|
1101
|
+
target_id,
|
|
1102
|
+
target_path,
|
|
1103
|
+
state.live_identity,
|
|
1104
|
+
previous_identity=existing["repository_identity"],
|
|
1105
|
+
)
|
|
1106
|
+
existing = connection.execute(target_query, (target_path,)).fetchone()
|
|
1107
|
+
if existing is None:
|
|
1108
|
+
raise SystemExit("The repository target changed while it was being registered.")
|
|
1109
|
+
target_id = str(existing["id"])
|
|
1110
|
+
state = _inspect_repository_target(
|
|
1111
|
+
connection, target_id, target_path, existing["repository_identity"]
|
|
1112
|
+
)
|
|
1113
|
+
state = _upgrade_repository_generation(connection, state)
|
|
1114
|
+
if verify_ownership:
|
|
1115
|
+
state.require_owner()
|
|
1116
|
+
if existing["repository_identity"] is None:
|
|
1117
|
+
identity = state.verified_identity
|
|
1118
|
+
if identity is not None and not _bind_unscanned_repository_identity(
|
|
1119
|
+
connection, target_id, target_path, identity
|
|
1120
|
+
):
|
|
1121
|
+
# A writer may have registered history or bound this target after inspection.
|
|
1122
|
+
existing = connection.execute(target_query, (target_path,)).fetchone()
|
|
1123
|
+
if existing is None:
|
|
1124
|
+
raise SystemExit("The repository target changed while it was being registered.")
|
|
1125
|
+
target_id = str(existing["id"])
|
|
1126
|
+
state = _inspect_repository_target(
|
|
1127
|
+
connection, target_id, target_path, existing["repository_identity"]
|
|
1128
|
+
)
|
|
1129
|
+
if verify_ownership:
|
|
1130
|
+
state.require_owner()
|
|
1131
|
+
if verify_ownership:
|
|
1132
|
+
_record_checkout_root(connection, existing, state, inserted)
|
|
1133
|
+
return RegisteredRepositoryTarget(
|
|
1134
|
+
target_id, state.live_identity if state.ownership_matches and not state.missing else None
|
|
57
1135
|
)
|
|
58
|
-
|
|
1136
|
+
|
|
1137
|
+
|
|
1138
|
+
def ensure_security_target(
|
|
1139
|
+
connection: sqlite3.Connection, target_path: str, *, verify_ownership: bool = True
|
|
1140
|
+
) -> str:
|
|
1141
|
+
"""Return the stable ID for the registered target and its verified ownership."""
|
|
1142
|
+
return register_security_target(
|
|
1143
|
+
connection, target_path, verify_ownership=verify_ownership
|
|
1144
|
+
).target_id
|
|
59
1145
|
|
|
60
1146
|
|
|
61
1147
|
if __name__ == "__main__":
|