@openai/codex-security 0.1.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.
Files changed (178) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +306 -0
  3. package/_bundled_plugin/.app.json +20 -0
  4. package/_bundled_plugin/.codex-plugin/plugin.json +41 -0
  5. package/_bundled_plugin/.mcp.json +20 -0
  6. package/_bundled_plugin/assets/logo.png +0 -0
  7. package/_bundled_plugin/examples/completed-scan/coverage.json +22 -0
  8. package/_bundled_plugin/examples/completed-scan/findings.json +51 -0
  9. package/_bundled_plugin/examples/completed-scan/scan-manifest.json +43 -0
  10. package/_bundled_plugin/mcp/mcp-app.html.br +0 -0
  11. package/_bundled_plugin/mcp/server.mjs +24 -0
  12. package/_bundled_plugin/mcp/server.mjs.br.part-000 +0 -0
  13. package/_bundled_plugin/mcp/server.mjs.br.part-001 +0 -0
  14. package/_bundled_plugin/preflight/capability-profiles.toml +179 -0
  15. package/_bundled_plugin/references/config-preflight.md +115 -0
  16. package/_bundled_plugin/references/final-report.md +205 -0
  17. package/_bundled_plugin/references/finding-detail-fields.md +162 -0
  18. package/_bundled_plugin/references/sarif-adapter.md +26 -0
  19. package/_bundled_plugin/references/scan-artifacts.md +116 -0
  20. package/_bundled_plugin/references/scan-contract.md +164 -0
  21. package/_bundled_plugin/references/security-guidance.md +15 -0
  22. package/_bundled_plugin/references/shared-hard-rules.md +15 -0
  23. package/_bundled_plugin/references/static-finding-assessment.md +75 -0
  24. package/_bundled_plugin/schemas/coverage.schema.json +228 -0
  25. package/_bundled_plugin/schemas/findings.schema.json +359 -0
  26. package/_bundled_plugin/schemas/scan-manifest.schema.json +332 -0
  27. package/_bundled_plugin/scripts/config_preflight.py +901 -0
  28. package/_bundled_plugin/scripts/deep_scan_config.py +108 -0
  29. package/_bundled_plugin/scripts/deep_scan_workbench.py +1547 -0
  30. package/_bundled_plugin/scripts/filesystem_identity.py +30 -0
  31. package/_bundled_plugin/scripts/finalize_scan_contract.py +2208 -0
  32. package/_bundled_plugin/scripts/finding_preview.py +270 -0
  33. package/_bundled_plugin/scripts/generate_rank_input.py +1013 -0
  34. package/_bundled_plugin/scripts/normalize_candidates.py +320 -0
  35. package/_bundled_plugin/scripts/rank_preview.py +968 -0
  36. package/_bundled_plugin/scripts/report_projection.py +844 -0
  37. package/_bundled_plugin/scripts/resolve_security_md.py +158 -0
  38. package/_bundled_plugin/scripts/snapshot_sqlite.py +26 -0
  39. package/_bundled_plugin/scripts/validate_report_format.py +138 -0
  40. package/_bundled_plugin/scripts/validate_scan_contract.py +108 -0
  41. package/_bundled_plugin/scripts/validate_tracking_source.py +92 -0
  42. package/_bundled_plugin/scripts/windows_scan_local_files.py +612 -0
  43. package/_bundled_plugin/scripts/workbench/__init__.py +1 -0
  44. package/_bundled_plugin/scripts/workbench/handoff.py +238 -0
  45. package/_bundled_plugin/scripts/workbench_cli.py +325 -0
  46. package/_bundled_plugin/scripts/workbench_constants.py +81 -0
  47. package/_bundled_plugin/scripts/workbench_db.py +3728 -0
  48. package/_bundled_plugin/scripts/workbench_native_indexes.py +242 -0
  49. package/_bundled_plugin/scripts/workbench_progress.py +226 -0
  50. package/_bundled_plugin/scripts/workbench_remediation.py +125 -0
  51. package/_bundled_plugin/scripts/workbench_scan_history.py +829 -0
  52. package/_bundled_plugin/scripts/workbench_scan_start.py +149 -0
  53. package/_bundled_plugin/scripts/workbench_schema.py +714 -0
  54. package/_bundled_plugin/scripts/workbench_source_excerpt.py +99 -0
  55. package/_bundled_plugin/scripts/workbench_target.py +469 -0
  56. package/_bundled_plugin/scripts/workbench_target_state.py +63 -0
  57. package/_bundled_plugin/scripts/workbench_validation.py +294 -0
  58. package/_bundled_plugin/skills/attack-path-analysis/SKILL.md +117 -0
  59. package/_bundled_plugin/skills/attack-path-analysis/agents/openai.yaml +4 -0
  60. package/_bundled_plugin/skills/attack-path-analysis/references/attack-path-facts.md +76 -0
  61. package/_bundled_plugin/skills/attack-path-analysis/references/severity-policy.md +153 -0
  62. package/_bundled_plugin/skills/deep-security-scan/SKILL.md +166 -0
  63. package/_bundled_plugin/skills/deep-security-scan/agents/openai.yaml +4 -0
  64. package/_bundled_plugin/skills/define-security-policy/SKILL.md +93 -0
  65. package/_bundled_plugin/skills/define-security-policy/agents/openai.yaml +4 -0
  66. package/_bundled_plugin/skills/finding-discovery/SKILL.md +155 -0
  67. package/_bundled_plugin/skills/finding-discovery/agents/openai.yaml +4 -0
  68. package/_bundled_plugin/skills/fix-finding/SKILL.md +112 -0
  69. package/_bundled_plugin/skills/fix-finding/agents/openai.yaml +4 -0
  70. package/_bundled_plugin/skills/propose-security-hardening/SKILL.md +240 -0
  71. package/_bundled_plugin/skills/propose-security-hardening/agents/openai.yaml +4 -0
  72. package/_bundled_plugin/skills/propose-security-hardening/references/proposal-format.md +424 -0
  73. package/_bundled_plugin/skills/security-diff-scan/SKILL.md +167 -0
  74. package/_bundled_plugin/skills/security-diff-scan/agents/openai.yaml +4 -0
  75. package/_bundled_plugin/skills/security-scan/SKILL.md +42 -0
  76. package/_bundled_plugin/skills/security-scan/agents/openai.yaml +4 -0
  77. package/_bundled_plugin/skills/security-scan/references/repository-wide-scan.md +48 -0
  78. package/_bundled_plugin/skills/security-scan/references/scan-artifacts-and-ledger.md +85 -0
  79. package/_bundled_plugin/skills/threat-model/SKILL.md +59 -0
  80. package/_bundled_plugin/skills/threat-model/agents/openai.yaml +4 -0
  81. package/_bundled_plugin/skills/threat-model/references/threat-model-guidance.md +48 -0
  82. package/_bundled_plugin/skills/track-findings/SKILL.md +222 -0
  83. package/_bundled_plugin/skills/track-findings/agents/openai.yaml +6 -0
  84. package/_bundled_plugin/skills/track-findings/references/github-security-advisories.md +54 -0
  85. package/_bundled_plugin/skills/track-findings/references/jira.md +77 -0
  86. package/_bundled_plugin/skills/triage-finding/SKILL.md +342 -0
  87. package/_bundled_plugin/skills/triage-finding/agents/openai.yaml +4 -0
  88. package/_bundled_plugin/skills/triage-finding/references/github-rest-intake.md +168 -0
  89. package/_bundled_plugin/skills/triage-finding/references/ticket-intake.md +62 -0
  90. package/_bundled_plugin/skills/triage-finding/references/triage-result-contract.md +108 -0
  91. package/_bundled_plugin/skills/validation/SKILL.md +110 -0
  92. package/_bundled_plugin/skills/validation/agents/openai.yaml +4 -0
  93. package/_bundled_plugin/skills/validation/references/validation-guidance.md +122 -0
  94. package/_bundled_plugin/skills/vulnerability-writeup/SKILL.md +263 -0
  95. package/_bundled_plugin/skills/vulnerability-writeup/agents/openai.yaml +4 -0
  96. package/_bundled_plugin/skills/vulnerability-writeup/references/report-format.md +104 -0
  97. package/bin/codex-security.mjs +26 -0
  98. package/dist/api.d.ts +112 -0
  99. package/dist/api.d.ts.map +1 -0
  100. package/dist/api.js +1051 -0
  101. package/dist/api.js.map +1 -0
  102. package/dist/auth.d.ts +29 -0
  103. package/dist/auth.d.ts.map +1 -0
  104. package/dist/auth.js +246 -0
  105. package/dist/auth.js.map +1 -0
  106. package/dist/bulk-scan-discovery.d.ts +35 -0
  107. package/dist/bulk-scan-discovery.d.ts.map +1 -0
  108. package/dist/bulk-scan-discovery.js +217 -0
  109. package/dist/bulk-scan-discovery.js.map +1 -0
  110. package/dist/cli.d.ts +67 -0
  111. package/dist/cli.d.ts.map +1 -0
  112. package/dist/cli.js +2128 -0
  113. package/dist/cli.js.map +1 -0
  114. package/dist/config.d.ts +19 -0
  115. package/dist/config.d.ts.map +1 -0
  116. package/dist/config.js +199 -0
  117. package/dist/config.js.map +1 -0
  118. package/dist/contract.d.ts +21 -0
  119. package/dist/contract.d.ts.map +1 -0
  120. package/dist/contract.js +848 -0
  121. package/dist/contract.js.map +1 -0
  122. package/dist/cost.d.ts +30 -0
  123. package/dist/cost.d.ts.map +1 -0
  124. package/dist/cost.js +280 -0
  125. package/dist/cost.js.map +1 -0
  126. package/dist/errors.d.ts +38 -0
  127. package/dist/errors.d.ts.map +1 -0
  128. package/dist/errors.js +52 -0
  129. package/dist/errors.js.map +1 -0
  130. package/dist/index.d.ts +22 -0
  131. package/dist/index.d.ts.map +1 -0
  132. package/dist/index.js +11 -0
  133. package/dist/index.js.map +1 -0
  134. package/dist/knowledge-base.d.ts +7 -0
  135. package/dist/knowledge-base.d.ts.map +1 -0
  136. package/dist/knowledge-base.js +181 -0
  137. package/dist/knowledge-base.js.map +1 -0
  138. package/dist/models.d.ts +231 -0
  139. package/dist/models.d.ts.map +1 -0
  140. package/dist/models.js +3 -0
  141. package/dist/models.js.map +1 -0
  142. package/dist/multiscan.d.ts +30 -0
  143. package/dist/multiscan.d.ts.map +1 -0
  144. package/dist/multiscan.js +397 -0
  145. package/dist/multiscan.js.map +1 -0
  146. package/dist/result.d.ts +39 -0
  147. package/dist/result.d.ts.map +1 -0
  148. package/dist/result.js +71 -0
  149. package/dist/result.js.map +1 -0
  150. package/dist/runtime.d.ts +69 -0
  151. package/dist/runtime.d.ts.map +1 -0
  152. package/dist/runtime.js +997 -0
  153. package/dist/runtime.js.map +1 -0
  154. package/dist/scan-comparison.d.ts +42 -0
  155. package/dist/scan-comparison.d.ts.map +1 -0
  156. package/dist/scan-comparison.js +153 -0
  157. package/dist/scan-comparison.js.map +1 -0
  158. package/dist/scan-history-renderer.d.ts +13 -0
  159. package/dist/scan-history-renderer.d.ts.map +1 -0
  160. package/dist/scan-history-renderer.js +297 -0
  161. package/dist/scan-history-renderer.js.map +1 -0
  162. package/dist/targets.d.ts +39 -0
  163. package/dist/targets.d.ts.map +1 -0
  164. package/dist/targets.js +295 -0
  165. package/dist/targets.js.map +1 -0
  166. package/dist/trusted-executable.d.ts +6 -0
  167. package/dist/trusted-executable.d.ts.map +1 -0
  168. package/dist/trusted-executable.js +77 -0
  169. package/dist/trusted-executable.js.map +1 -0
  170. package/dist/version.d.ts +6 -0
  171. package/dist/version.d.ts.map +1 -0
  172. package/dist/version.js +42 -0
  173. package/dist/version.js.map +1 -0
  174. package/dist/worker-progress.d.ts +13 -0
  175. package/dist/worker-progress.d.ts.map +1 -0
  176. package/dist/worker-progress.js +113 -0
  177. package/dist/worker-progress.js.map +1 -0
  178. package/package.json +74 -0
@@ -0,0 +1,99 @@
1
+ """Read bounded finding source excerpts from sealed Git revisions."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import argparse
6
+ import sqlite3
7
+ import sys
8
+ from pathlib import Path, PurePosixPath
9
+ from typing import Any
10
+
11
+ # Some plugin hosts launch Python with safe-path isolation enabled.
12
+ sys.path.insert(0, str(Path(__file__).resolve().parent))
13
+ from workbench_target import clean_worktree_content_digest, git_bytes, git_output
14
+
15
+ CONTEXT_LINES = 3
16
+ MAX_BYTES = 16_000
17
+ MAX_FILE_BYTES = 1_048_576
18
+ MAX_LINES = 60
19
+
20
+
21
+ def finding_source_excerpt(
22
+ scan: sqlite3.Row,
23
+ target: Path | None,
24
+ locations: list[dict[str, Any]],
25
+ ) -> str | None:
26
+ if target is None or not locations:
27
+ return None
28
+ location = next(
29
+ (
30
+ candidate
31
+ for candidate in locations
32
+ if "root_control" in str(candidate.get("role") or "").lower()
33
+ ),
34
+ locations[0],
35
+ )
36
+ path = location.get("path")
37
+ start_line = location.get("startLine")
38
+ end_line = location.get("endLine")
39
+ if not isinstance(path, str) or not isinstance(start_line, int):
40
+ return None
41
+ source = scanned_source_text(scan, target, path)
42
+ if not source or "\0" in source:
43
+ return None
44
+ lines = source.splitlines()
45
+ if start_line < 1 or start_line > len(lines):
46
+ return None
47
+ last_affected_line = end_line if isinstance(end_line, int) else start_line
48
+ excerpt_start = max(1, start_line - CONTEXT_LINES)
49
+ excerpt_end = min(
50
+ len(lines),
51
+ max(start_line, last_affected_line) + CONTEXT_LINES,
52
+ excerpt_start + MAX_LINES - 1,
53
+ )
54
+ width = len(str(excerpt_end))
55
+ excerpt = "\n".join(
56
+ f"{line_number:>{width}} {lines[line_number - 1]}"
57
+ for line_number in range(excerpt_start, excerpt_end + 1)
58
+ )
59
+ encoded = excerpt.encode("utf-8")[:MAX_BYTES]
60
+ return encoded.decode("utf-8", errors="ignore")
61
+
62
+
63
+ def scanned_source_text(scan: sqlite3.Row, target: Path, path: str) -> str | None:
64
+ if safe_source_path(target, path) is None:
65
+ return None
66
+ revision = scan["target_revision"]
67
+ if revision == "unversioned":
68
+ return None
69
+ snapshot_digest = scan["target_snapshot_digest"]
70
+ if snapshot_digest is not None and snapshot_digest != clean_worktree_content_digest():
71
+ return None
72
+ object_name = f"{revision}:{path}"
73
+ size = git_output(target, "cat-file", "-s", object_name)
74
+ if size is None or not size.isdigit() or int(size) > MAX_FILE_BYTES:
75
+ return None
76
+ content = git_bytes(target, "cat-file", "blob", object_name)
77
+ return content.decode("utf-8", errors="replace") if content is not None else None
78
+
79
+
80
+ def safe_source_path(target: Path, relative_path: str) -> Path | None:
81
+ if "\\" in relative_path:
82
+ return None
83
+ parsed = PurePosixPath(relative_path)
84
+ if parsed.is_absolute() or ".." in parsed.parts:
85
+ return None
86
+ try:
87
+ path = (target / parsed.as_posix()).resolve()
88
+ path.relative_to(target)
89
+ except (OSError, RuntimeError, ValueError):
90
+ return None
91
+ return path
92
+
93
+
94
+ def main() -> None:
95
+ argparse.ArgumentParser(description=__doc__).parse_args()
96
+
97
+
98
+ if __name__ == "__main__":
99
+ main()
@@ -0,0 +1,469 @@
1
+ """Target inspection and content-integrity helpers for the security workbench."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import argparse
6
+ import hashlib
7
+ import os
8
+ import shutil
9
+ import stat
10
+ import subprocess
11
+ import sys
12
+ from pathlib import Path
13
+ from typing import Any
14
+
15
+ # Some plugin hosts launch Python with safe-path isolation enabled.
16
+ sys.path.insert(0, str(Path(__file__).resolve().parent))
17
+ from workbench_constants import GIT_REPOSITORY_ENVIRONMENT
18
+
19
+
20
+ def git_output(
21
+ target: Path,
22
+ *args: str,
23
+ git_dir: Path | None = None,
24
+ work_tree: Path | None = None,
25
+ ) -> str | None:
26
+ completed = git_command(target, *args, text=True, git_dir=git_dir, work_tree=work_tree)
27
+ output = completed.stdout.strip()
28
+ return output if completed.returncode == 0 and output else None
29
+
30
+
31
+ def git_bytes(
32
+ target: Path,
33
+ *args: str,
34
+ git_dir: Path | None = None,
35
+ work_tree: Path | None = None,
36
+ ) -> bytes | None:
37
+ completed = git_command(target, *args, text=False, git_dir=git_dir, work_tree=work_tree)
38
+ return completed.stdout if completed.returncode == 0 else None
39
+
40
+
41
+ def git_command(
42
+ target: Path,
43
+ *args: str,
44
+ text: bool,
45
+ git_dir: Path | None = None,
46
+ work_tree: Path | None = None,
47
+ ) -> subprocess.CompletedProcess[str] | subprocess.CompletedProcess[bytes]:
48
+ if (git_dir is None) != (work_tree is None):
49
+ raise ValueError("git_dir and work_tree must be provided together")
50
+ environment = os.environ.copy()
51
+ for name in GIT_REPOSITORY_ENVIRONMENT:
52
+ environment.pop(name, None)
53
+ environment["GIT_LITERAL_PATHSPECS"] = "1"
54
+ # Repository-local config is untrusted; fsmonitor may name an executable hook.
55
+ command = ["git", "-c", "core.fsmonitor=false", "-C", str(target)]
56
+ if git_dir is not None and work_tree is not None:
57
+ command.extend(["--git-dir", str(git_dir), "--work-tree", str(work_tree)])
58
+ full_command = [*command, *args]
59
+ try:
60
+ return subprocess.run(
61
+ full_command,
62
+ check=False,
63
+ capture_output=True,
64
+ env=environment,
65
+ text=text,
66
+ )
67
+ except FileNotFoundError:
68
+ # Git is optional for Codebase scans. Treat an unavailable executable like
69
+ # any other failed Git probe so the target falls back to a directory snapshot.
70
+ empty_output = "" if text else b""
71
+ return subprocess.CompletedProcess(full_command, 127, empty_output, empty_output)
72
+
73
+
74
+ def update_digest_field(digest: Any, label: bytes, value: bytes) -> None:
75
+ digest.update(len(label).to_bytes(4, "big"))
76
+ digest.update(label)
77
+ digest.update(len(value).to_bytes(8, "big"))
78
+ digest.update(value)
79
+
80
+
81
+ def worktree_content_digest(target: Path) -> str:
82
+ require_clean_submodule_worktrees(target)
83
+ repository, pathspec = git_worktree_context(target)
84
+ return worktree_content_digest_for_context(repository, pathspec)
85
+
86
+
87
+ def worktree_content_digest_for_context(
88
+ repository: Path,
89
+ pathspec: str,
90
+ *,
91
+ git_dir: Path | None = None,
92
+ work_tree: Path | None = None,
93
+ ) -> str:
94
+ tracked = git_bytes(
95
+ repository,
96
+ "diff",
97
+ "--binary",
98
+ "--full-index",
99
+ "--no-ext-diff",
100
+ "--no-textconv",
101
+ "--ignore-submodules=none",
102
+ "HEAD",
103
+ "--",
104
+ pathspec,
105
+ git_dir=git_dir,
106
+ work_tree=work_tree,
107
+ )
108
+ untracked = git_bytes(
109
+ repository,
110
+ "ls-files",
111
+ "--others",
112
+ "--exclude-standard",
113
+ "-z",
114
+ "--",
115
+ pathspec,
116
+ git_dir=git_dir,
117
+ work_tree=work_tree,
118
+ )
119
+ if tracked is None or untracked is None:
120
+ raise SystemExit("Could not snapshot the selected working-tree changes.")
121
+ digest = hashlib.sha256()
122
+ update_digest_field(digest, b"format", b"codex-security-snapshot/v1")
123
+ update_digest_field(digest, b"tracked-diff", tracked)
124
+ for raw_path in sorted(path for path in untracked.split(b"\0") if path):
125
+ relative_path = os.fsdecode(raw_path)
126
+ path = (work_tree or repository) / relative_path
127
+ try:
128
+ metadata = path.lstat()
129
+ except OSError as exc:
130
+ raise SystemExit(f"Could not read untracked file: {relative_path}") from exc
131
+ update_digest_field(digest, b"untracked-path", raw_path)
132
+ update_digest_field(
133
+ digest,
134
+ b"untracked-mode",
135
+ str(stat.S_IMODE(metadata.st_mode)).encode(),
136
+ )
137
+ if stat.S_ISLNK(metadata.st_mode):
138
+ update_digest_field(digest, b"untracked-kind", b"symlink")
139
+ update_digest_field(
140
+ digest,
141
+ b"untracked-content",
142
+ os.fsencode(os.readlink(path)),
143
+ )
144
+ elif stat.S_ISREG(metadata.st_mode):
145
+ content_digest = hashlib.sha256()
146
+ content_size = 0
147
+ try:
148
+ with path.open("rb") as handle:
149
+ for chunk in iter(lambda: handle.read(1024 * 1024), b""):
150
+ content_digest.update(chunk)
151
+ content_size += len(chunk)
152
+ except OSError as exc:
153
+ raise SystemExit(f"Could not read untracked file: {relative_path}") from exc
154
+ update_digest_field(digest, b"untracked-kind", b"file")
155
+ update_digest_field(
156
+ digest,
157
+ b"untracked-size",
158
+ str(content_size).encode(),
159
+ )
160
+ update_digest_field(
161
+ digest,
162
+ b"untracked-content-sha256",
163
+ content_digest.digest(),
164
+ )
165
+ else:
166
+ raise SystemExit(f"Unsupported untracked file type: {relative_path}")
167
+ return f"codex-security-snapshot/v1:sha256:{digest.hexdigest()}"
168
+
169
+
170
+ def git_worktree_context(target: Path) -> tuple[Path, str]:
171
+ root = git_output(target, "rev-parse", "--show-toplevel")
172
+ if root is None:
173
+ raise SystemExit("Could not inspect the selected Git working tree.")
174
+ repository = Path(root).resolve()
175
+ try:
176
+ relative = target.resolve().relative_to(repository)
177
+ except ValueError as exc:
178
+ raise SystemExit("Scan target must stay inside its Git working tree.") from exc
179
+ return repository, relative.as_posix() or "."
180
+
181
+
182
+ def git_submodule_entries(target: Path) -> tuple[tuple[Path, str], ...]:
183
+ repository, pathspec = git_worktree_context(target)
184
+ staged = git_bytes(repository, "ls-files", "--stage", "-z", "--", pathspec)
185
+ if staged is None:
186
+ raise SystemExit("Could not inspect Git submodules in the selected working tree.")
187
+ entries = []
188
+ for record in (item for item in staged.split(b"\0") if item):
189
+ try:
190
+ metadata, raw_path = record.split(b"\t", 1)
191
+ mode, object_id, _ = metadata.split(b" ", 2)
192
+ except ValueError as exc:
193
+ raise SystemExit(
194
+ "Could not inspect Git submodules in the selected working tree."
195
+ ) from exc
196
+ if mode != b"160000":
197
+ continue
198
+ entries.append((repository / os.fsdecode(raw_path), object_id.decode("ascii")))
199
+ return tuple(entries)
200
+
201
+
202
+ def git_submodule_paths(target: Path) -> tuple[Path, ...]:
203
+ return tuple(path for path, _ in git_submodule_entries(target))
204
+
205
+
206
+ def require_clean_submodule_worktrees(target: Path) -> None:
207
+ for submodule, expected_revision in git_submodule_entries(target):
208
+ relative_path = str(submodule.relative_to(target))
209
+ if not submodule.exists():
210
+ continue
211
+ try:
212
+ (submodule / ".git").lstat()
213
+ except FileNotFoundError:
214
+ continue
215
+ root = git_output(submodule, "rev-parse", "--show-toplevel")
216
+ try:
217
+ is_initialized = root is not None and Path(root).resolve() == submodule.resolve()
218
+ except OSError:
219
+ is_initialized = False
220
+ if not is_initialized:
221
+ raise SystemExit(
222
+ f"Could not inspect initialized Git submodule contents: {relative_path}"
223
+ )
224
+ if git_output(submodule, "rev-parse", "HEAD") != expected_revision:
225
+ raise SystemExit(
226
+ "Initialized Git submodules must be checked out at the revision recorded "
227
+ f"by the parent repository: {relative_path}"
228
+ )
229
+ status = git_bytes(
230
+ submodule,
231
+ "status",
232
+ "--porcelain=v1",
233
+ "-z",
234
+ "--untracked-files=all",
235
+ "--ignore-submodules=none",
236
+ )
237
+ if status is None:
238
+ raise SystemExit(f"Could not inspect Git submodule contents: {relative_path}")
239
+ if status:
240
+ raise SystemExit(
241
+ f"Dirty Git submodules are not supported for remediation integrity checks: {relative_path}"
242
+ )
243
+ require_clean_submodule_worktrees(submodule)
244
+
245
+
246
+ def clean_worktree_content_digest() -> str:
247
+ digest = hashlib.sha256()
248
+ update_digest_field(digest, b"format", b"codex-security-snapshot/v1")
249
+ update_digest_field(digest, b"tracked-diff", b"")
250
+ return f"codex-security-snapshot/v1:sha256:{digest.hexdigest()}"
251
+
252
+
253
+ def git_directory_snapshot_paths(target: Path) -> list[Path] | None:
254
+ repository_root = git_output(target, "rev-parse", "--show-toplevel")
255
+ if repository_root is None:
256
+ return None
257
+ repository, pathspec = git_worktree_context(target)
258
+ listed = git_bytes(
259
+ repository,
260
+ "ls-files",
261
+ "--cached",
262
+ "--others",
263
+ "--exclude-standard",
264
+ "-z",
265
+ "--",
266
+ pathspec,
267
+ )
268
+ if listed is None:
269
+ raise SystemExit("Could not inspect files in the selected Git working tree.")
270
+ paths: list[Path] = []
271
+ for raw_path in (raw_path for raw_path in listed.split(b"\0") if raw_path):
272
+ path = repository / os.fsdecode(raw_path)
273
+ try:
274
+ metadata = path.lstat()
275
+ except FileNotFoundError:
276
+ # The index can retain a path that was staged and then deleted.
277
+ continue
278
+ paths.append(path)
279
+ if not stat.S_ISDIR(metadata.st_mode):
280
+ continue
281
+ nested_repository_root = git_output(path, "rev-parse", "--show-toplevel")
282
+ if (
283
+ nested_repository_root is not None
284
+ and Path(nested_repository_root).resolve() == path.resolve()
285
+ ):
286
+ nested_paths = git_directory_snapshot_paths(path)
287
+ if nested_paths is not None:
288
+ paths.extend(nested_paths)
289
+ continue
290
+ paths.extend(
291
+ nested_path
292
+ for nested_path in path.rglob("*")
293
+ if ".git" not in nested_path.relative_to(path).parts
294
+ )
295
+ return sorted(set(paths))
296
+
297
+
298
+ def directory_content_digest(target: Path, *, excluded: tuple[Path, ...] = ()) -> str:
299
+ excluded_relative = []
300
+ for path in excluded:
301
+ try:
302
+ excluded_relative.append(path.relative_to(target))
303
+ except ValueError:
304
+ continue
305
+ paths = git_directory_snapshot_paths(target)
306
+ if paths is None:
307
+ paths = sorted(target.rglob("*"))
308
+ digest = hashlib.sha256()
309
+ update_digest_field(digest, b"format", b"codex-security-directory/v1")
310
+ for path in paths:
311
+ relative_path = path.relative_to(target)
312
+ if any(
313
+ relative_path == excluded_path or excluded_path in relative_path.parents
314
+ for excluded_path in excluded_relative
315
+ ):
316
+ continue
317
+ try:
318
+ metadata = path.lstat()
319
+ except OSError as exc:
320
+ raise SystemExit(f"Could not read local file: {relative_path}") from exc
321
+ raw_path = os.fsencode(relative_path.as_posix())
322
+ update_digest_field(digest, b"path", raw_path)
323
+ update_digest_field(digest, b"mode", str(stat.S_IMODE(metadata.st_mode)).encode())
324
+ if stat.S_ISLNK(metadata.st_mode):
325
+ update_digest_field(digest, b"kind", b"symlink")
326
+ update_digest_field(digest, b"content", os.fsencode(os.readlink(path)))
327
+ elif stat.S_ISDIR(metadata.st_mode):
328
+ update_digest_field(digest, b"kind", b"directory")
329
+ elif stat.S_ISREG(metadata.st_mode):
330
+ content_digest = hashlib.sha256()
331
+ content_size = 0
332
+ try:
333
+ with path.open("rb") as handle:
334
+ for chunk in iter(lambda: handle.read(1024 * 1024), b""):
335
+ content_digest.update(chunk)
336
+ content_size += len(chunk)
337
+ except OSError as exc:
338
+ raise SystemExit(f"Could not read local file: {relative_path}") from exc
339
+ update_digest_field(digest, b"kind", b"file")
340
+ update_digest_field(digest, b"size", str(content_size).encode())
341
+ update_digest_field(digest, b"content-sha256", content_digest.digest())
342
+ else:
343
+ raise SystemExit(f"Unsupported local file type: {relative_path}")
344
+ return f"codex-security-snapshot/v1:sha256:{digest.hexdigest()}"
345
+
346
+
347
+ def directory_snapshot_regular_file_count(target: Path) -> int:
348
+ paths = git_directory_snapshot_paths(target)
349
+ if paths is None:
350
+ paths = sorted(target.rglob("*"))
351
+ count = 0
352
+ for path in paths:
353
+ try:
354
+ metadata = path.lstat()
355
+ except OSError as exc:
356
+ raise SystemExit(f"Could not inspect local file: {path.relative_to(target)}") from exc
357
+ if stat.S_ISREG(metadata.st_mode):
358
+ count += 1
359
+ return count
360
+
361
+
362
+ def copy_directory_excluding(source: Path, destination: Path, excluded: tuple[Path, ...]) -> None:
363
+ excluded_relative = []
364
+ for path in excluded:
365
+ try:
366
+ excluded_relative.append(path.relative_to(source))
367
+ except ValueError:
368
+ continue
369
+
370
+ def ignored(directory: str, names: list[str]) -> list[str]:
371
+ relative = Path(directory).relative_to(source)
372
+ return [
373
+ path.name
374
+ for path in excluded_relative
375
+ if path.parent == relative and path.name in names
376
+ ]
377
+
378
+ shutil.copytree(source, destination, symlinks=True, ignore=ignored)
379
+
380
+
381
+ def copy_git_worktree_files(source: Path, destination: Path, excluded: tuple[Path, ...]) -> Path:
382
+ repository, pathspec = git_worktree_context(source)
383
+ listed = git_bytes(
384
+ repository,
385
+ "ls-files",
386
+ "--cached",
387
+ "--others",
388
+ "--exclude-standard",
389
+ "-z",
390
+ "--",
391
+ pathspec,
392
+ )
393
+ if listed is None:
394
+ raise SystemExit("Could not inspect files in the selected Git working tree.")
395
+ excluded_relative = []
396
+ for path in excluded:
397
+ try:
398
+ excluded_relative.append(path.relative_to(repository))
399
+ except ValueError:
400
+ continue
401
+ destination.mkdir()
402
+ for raw_path in sorted(path for path in listed.split(b"\0") if path):
403
+ relative = Path(os.fsdecode(raw_path))
404
+ if any(
405
+ relative == excluded_path or excluded_path in relative.parents
406
+ for excluded_path in excluded_relative
407
+ ):
408
+ continue
409
+ source_path = repository / relative
410
+ try:
411
+ metadata = source_path.lstat()
412
+ except FileNotFoundError:
413
+ continue
414
+ destination_path = destination / relative
415
+ destination_path.parent.mkdir(parents=True, exist_ok=True)
416
+ if stat.S_ISLNK(metadata.st_mode):
417
+ destination_path.symlink_to(os.readlink(source_path))
418
+ elif stat.S_ISREG(metadata.st_mode):
419
+ shutil.copy2(source_path, destination_path, follow_symlinks=False)
420
+ elif not stat.S_ISDIR(metadata.st_mode):
421
+ raise SystemExit(f"Unsupported Git working-tree file type: {relative}")
422
+ copied_target = destination if pathspec == "." else destination / pathspec
423
+ copied_target.mkdir(parents=True, exist_ok=True)
424
+ return copied_target
425
+
426
+
427
+ def git_revision(target: Path) -> str:
428
+ return git_output(target, "rev-parse", "HEAD") or "unversioned"
429
+
430
+
431
+ def git_target_metadata(target: Path) -> dict[str, Any]:
432
+ is_git = git_output(target, "rev-parse", "--git-dir") is not None
433
+ is_worktree = git_output(target, "rev-parse", "--is-inside-work-tree") == "true"
434
+ revision = git_output(target, "rev-parse", "--verify", "HEAD")
435
+ repository_root = git_output(target, "rev-parse", "--show-toplevel") if is_worktree else None
436
+ supported = (
437
+ is_git
438
+ and is_worktree
439
+ and revision is not None
440
+ and repository_root is not None
441
+ and Path(repository_root).resolve() == target
442
+ )
443
+ metadata: dict[str, Any] = {
444
+ "hasHead": revision is not None,
445
+ "isGit": is_git,
446
+ "isWorktree": is_worktree,
447
+ "reviewChangesSupported": supported,
448
+ }
449
+ if not is_git:
450
+ return metadata
451
+ branch = git_output(target, "symbolic-ref", "--quiet", "--short", "HEAD")
452
+ metadata.update({"branch": branch, "detachedHead": revision is not None and branch is None})
453
+ if revision is not None:
454
+ metadata.update(
455
+ {
456
+ "commitSubject": git_output(target, "show", "-s", "--format=%s", "HEAD"),
457
+ "revision": revision,
458
+ "shortRevision": revision[:7],
459
+ }
460
+ )
461
+ return metadata
462
+
463
+
464
+ def main() -> None:
465
+ argparse.ArgumentParser(description=__doc__).parse_args()
466
+
467
+
468
+ if __name__ == "__main__":
469
+ main()
@@ -0,0 +1,63 @@
1
+ """Persist stable Codex Security target identities."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import argparse
6
+ import hashlib
7
+ import sqlite3
8
+ from datetime import datetime, timezone
9
+ from pathlib import Path
10
+
11
+
12
+ def stable_target_id(target: Path) -> str:
13
+ digest = hashlib.sha256(f"local-workspace\0{target}".encode()).hexdigest()
14
+ return f"target_sha256_{digest}"
15
+
16
+
17
+ def backfill_security_targets(connection: sqlite3.Connection) -> None:
18
+ rows = connection.execute(
19
+ """
20
+ SELECT target_path FROM workspaces WHERE target_path IS NOT NULL
21
+ UNION
22
+ SELECT target_path FROM scans
23
+ """
24
+ ).fetchall()
25
+ for row in rows:
26
+ target_path = row["target_path"]
27
+ target_id = ensure_security_target(connection, target_path)
28
+ connection.execute(
29
+ "UPDATE workspaces SET target_id = ? WHERE target_path = ? AND target_id IS NULL",
30
+ (target_id, target_path),
31
+ )
32
+ connection.execute(
33
+ "UPDATE scans SET target_id = ? WHERE target_path = ? AND target_id IS NULL",
34
+ (target_id, target_path),
35
+ )
36
+
37
+
38
+ def ensure_security_target(connection: sqlite3.Connection, target_path: str) -> str:
39
+ existing = connection.execute(
40
+ "SELECT id FROM security_targets WHERE current_path = ?",
41
+ (target_path,),
42
+ ).fetchone()
43
+ if existing is not None:
44
+ return str(existing["id"])
45
+ target_id = stable_target_id(Path(target_path))
46
+ timestamp = datetime.now(timezone.utc).isoformat().replace("+00:00", "Z")
47
+ connection.execute(
48
+ """
49
+ INSERT OR IGNORE INTO security_targets (
50
+ id, current_path, display_name, created_at, updated_at
51
+ ) VALUES (?, ?, ?, ?, ?)
52
+ """,
53
+ (target_id, target_path, Path(target_path).name, timestamp, timestamp),
54
+ )
55
+ return target_id
56
+
57
+
58
+ def main() -> None:
59
+ argparse.ArgumentParser(description=__doc__).parse_args()
60
+
61
+
62
+ if __name__ == "__main__":
63
+ main()