@openai/codex-security 0.7.0 → 0.8.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 (153) hide show
  1. package/README.md +10 -4
  2. package/_bundled_plugin/examples/completed-scan/findings.json +34 -34
  3. package/_bundled_plugin/examples/completed-scan/report.md +22 -31
  4. package/_bundled_plugin/examples/completed-scan/scan-manifest.json +27 -29
  5. package/_bundled_plugin/examples/{completed-scan → legacy-completed-scan}/coverage.json +1 -0
  6. package/_bundled_plugin/examples/legacy-completed-scan/findings.json +57 -0
  7. package/_bundled_plugin/examples/legacy-completed-scan/report.md +98 -0
  8. package/_bundled_plugin/examples/legacy-completed-scan/scan-manifest.json +43 -0
  9. package/_bundled_plugin/mcp/helpers.mjs.br.part-000 +0 -0
  10. package/_bundled_plugin/mcp/helpers.mjs.br.part-001 +0 -0
  11. package/_bundled_plugin/mcp/native/win32-arm64/windows.node +0 -0
  12. package/_bundled_plugin/mcp/native/win32-x64/windows.node +0 -0
  13. package/_bundled_plugin/mcp/server.mjs.br.part-000 +0 -0
  14. package/_bundled_plugin/mcp/server.mjs.br.part-001 +0 -0
  15. package/_bundled_plugin/mcp/server.mjs.br.part-002 +0 -0
  16. package/_bundled_plugin/references/core-scan.md +2 -2
  17. package/_bundled_plugin/references/final-report.md +16 -6
  18. package/_bundled_plugin/references/scan-artifacts.md +5 -5
  19. package/_bundled_plugin/references/scan-contract.md +10 -12
  20. package/_bundled_plugin/references/threat-model.md +1 -1
  21. package/_bundled_plugin/schemas/scan-manifest.schema.json +36 -11
  22. package/_bundled_plugin/schemas/tools/scan-draft.schema.json +5 -5
  23. package/_bundled_plugin/scripts/deep_scan_workbench.py +1 -1
  24. package/_bundled_plugin/scripts/finalize_scan_contract.py +60 -19
  25. package/_bundled_plugin/scripts/report_projection.py +5 -2
  26. package/_bundled_plugin/scripts/review_state.py +68 -0
  27. package/_bundled_plugin/scripts/validate_scan_contract.py +1 -2
  28. package/_bundled_plugin/scripts/workbench/retained_coverage.py +49 -37
  29. package/_bundled_plugin/scripts/workbench/retained_records.py +7 -7
  30. package/_bundled_plugin/scripts/workbench/saved_coverage.py +15 -15
  31. package/_bundled_plugin/scripts/workbench/saved_sources.py +24 -6
  32. package/_bundled_plugin/scripts/workbench_constants.py +0 -1
  33. package/_bundled_plugin/scripts/workbench_coverage_receipts.py +4 -4
  34. package/_bundled_plugin/scripts/workbench_db.py +5 -150
  35. package/_bundled_plugin/scripts/workbench_saved_results.py +169 -4
  36. package/_bundled_plugin/scripts/workbench_scan_history.py +12 -7
  37. package/_bundled_plugin/skills/publish-findings/SKILL.md +5 -1
  38. package/_bundled_plugin/skills/security-diff-scan/SKILL.md +1 -1
  39. package/_bundled_plugin/skills/security-scan/SKILL.md +6 -6
  40. package/_bundled_plugin/skills/track-findings/SKILL.md +1 -1
  41. package/dist/api.d.ts.map +1 -1
  42. package/dist/api.js +19 -17
  43. package/dist/api.js.map +1 -1
  44. package/dist/cli.d.ts.map +1 -1
  45. package/dist/cli.js +28 -17
  46. package/dist/cli.js.map +1 -1
  47. package/dist/cloud-publish.d.ts.map +1 -1
  48. package/dist/cloud-publish.js +2 -23
  49. package/dist/cloud-publish.js.map +1 -1
  50. package/dist/contract.d.ts +3 -2
  51. package/dist/contract.d.ts.map +1 -1
  52. package/dist/contract.js +28 -14
  53. package/dist/contract.js.map +1 -1
  54. package/dist/custom-publish.js +2 -2
  55. package/dist/custom-publish.js.map +1 -1
  56. package/dist/custom-validation-prompt.js +5 -5
  57. package/dist/custom-validation-prompt.js.map +1 -1
  58. package/dist/custom-validation.d.ts.map +1 -1
  59. package/dist/custom-validation.js +17 -15
  60. package/dist/custom-validation.js.map +1 -1
  61. package/dist/deduplication/codex-review.d.ts.map +1 -1
  62. package/dist/deduplication/codex-review.js +2 -2
  63. package/dist/deduplication/codex-review.js.map +1 -1
  64. package/dist/deduplication/deduplication-prompts.d.ts +1 -0
  65. package/dist/deduplication/deduplication-prompts.d.ts.map +1 -1
  66. package/dist/deduplication/deduplication-prompts.js +3 -0
  67. package/dist/deduplication/deduplication-prompts.js.map +1 -1
  68. package/dist/deduplication/records-protocol.d.ts.map +1 -1
  69. package/dist/deduplication/records-protocol.js +21 -1
  70. package/dist/deduplication/records-protocol.js.map +1 -1
  71. package/dist/deduplication/scan.d.ts.map +1 -1
  72. package/dist/deduplication/scan.js +2 -2
  73. package/dist/deduplication/scan.js.map +1 -1
  74. package/dist/external-findings-local.d.ts +1 -0
  75. package/dist/external-findings-local.d.ts.map +1 -1
  76. package/dist/external-findings-local.js +6 -2
  77. package/dist/external-findings-local.js.map +1 -1
  78. package/dist/external-findings-publish.d.ts +1 -0
  79. package/dist/external-findings-publish.d.ts.map +1 -1
  80. package/dist/external-findings-publish.js +53 -25
  81. package/dist/external-findings-publish.js.map +1 -1
  82. package/dist/external-findings-read.d.ts +1 -0
  83. package/dist/external-findings-read.d.ts.map +1 -1
  84. package/dist/external-findings-read.js +10 -0
  85. package/dist/external-findings-read.js.map +1 -1
  86. package/dist/external-findings.d.ts +2 -0
  87. package/dist/external-findings.d.ts.map +1 -1
  88. package/dist/external-findings.js.map +1 -1
  89. package/dist/external-import-contract.d.ts +4 -0
  90. package/dist/external-import-contract.d.ts.map +1 -1
  91. package/dist/external-import-contract.js +34 -10
  92. package/dist/external-import-contract.js.map +1 -1
  93. package/dist/finding-workflow.d.ts +2 -0
  94. package/dist/finding-workflow.d.ts.map +1 -1
  95. package/dist/finding-workflow.js +3 -0
  96. package/dist/finding-workflow.js.map +1 -1
  97. package/dist/host-protocol.d.ts +1 -1
  98. package/dist/host-protocol.d.ts.map +1 -1
  99. package/dist/host-protocol.js +2 -2
  100. package/dist/host-protocol.js.map +1 -1
  101. package/dist/hosted-scan.d.ts.map +1 -1
  102. package/dist/hosted-scan.js +0 -1
  103. package/dist/hosted-scan.js.map +1 -1
  104. package/dist/import-scan.js +16 -16
  105. package/dist/import-scan.js.map +1 -1
  106. package/dist/index.d.ts +1 -0
  107. package/dist/index.d.ts.map +1 -1
  108. package/dist/index.js +1 -0
  109. package/dist/index.js.map +1 -1
  110. package/dist/knowledge-base.js +3 -0
  111. package/dist/knowledge-base.js.map +1 -1
  112. package/dist/mock-scan.d.ts +1 -2
  113. package/dist/mock-scan.d.ts.map +1 -1
  114. package/dist/mock-scan.js +12 -17
  115. package/dist/mock-scan.js.map +1 -1
  116. package/dist/models.d.ts +9 -1
  117. package/dist/models.d.ts.map +1 -1
  118. package/dist/models.js.map +1 -1
  119. package/dist/multiscan.d.ts.map +1 -1
  120. package/dist/multiscan.js +1 -6
  121. package/dist/multiscan.js.map +1 -1
  122. package/dist/native-json-findings.d.ts +30 -0
  123. package/dist/native-json-findings.d.ts.map +1 -0
  124. package/dist/native-json-findings.js +114 -0
  125. package/dist/native-json-findings.js.map +1 -0
  126. package/dist/opengrep-findings.d.ts +10 -0
  127. package/dist/opengrep-findings.d.ts.map +1 -0
  128. package/dist/opengrep-findings.js +198 -0
  129. package/dist/opengrep-findings.js.map +1 -0
  130. package/dist/patch-tui.d.ts.map +1 -1
  131. package/dist/patch-tui.js +2 -0
  132. package/dist/patch-tui.js.map +1 -1
  133. package/dist/publication.js +1 -2
  134. package/dist/publication.js.map +1 -1
  135. package/dist/record.d.ts +1 -0
  136. package/dist/record.d.ts.map +1 -1
  137. package/dist/record.js +10 -0
  138. package/dist/record.js.map +1 -1
  139. package/dist/result.d.ts +5 -3
  140. package/dist/result.d.ts.map +1 -1
  141. package/dist/result.js +9 -6
  142. package/dist/result.js.map +1 -1
  143. package/dist/scan-history-renderer.js +1 -1
  144. package/dist/scan-history-renderer.js.map +1 -1
  145. package/dist/security-policy.d.ts.map +1 -1
  146. package/dist/security-policy.js +16 -2
  147. package/dist/security-policy.js.map +1 -1
  148. package/dist/semgrep-findings.d.ts.map +1 -1
  149. package/dist/semgrep-findings.js +15 -82
  150. package/dist/semgrep-findings.js.map +1 -1
  151. package/docs/cli.md +16 -3
  152. package/docs/dedupe-records.md +15 -1
  153. package/package.json +3 -3
@@ -0,0 +1,114 @@
1
+ import { validateExternalEvidence, } from "./external-import-contract.js";
2
+ import { isRecord } from "./record.js";
3
+ import { record, relativePath, requiredText, strings, text, } from "./scanner-import.js";
4
+ export function sourceRepositoryUrl(value, origin) {
5
+ const supplied = requiredText(value, `${origin} sourceRepositoryUrl (--source-repository-url)`);
6
+ const url = new URL(supplied);
7
+ if (!["http:", "https:"].includes(url.protocol) ||
8
+ url.username ||
9
+ url.password)
10
+ throw new Error("Use an HTTP(S) source repository URL without credentials.");
11
+ return supplied;
12
+ }
13
+ export function ungatedText(value) {
14
+ const supplied = text(value);
15
+ // Semgrep CE emits this placeholder for gated fields.
16
+ return supplied === "requires login" ? null : supplied;
17
+ }
18
+ export function messageText(value, origin) {
19
+ if (typeof value !== "string")
20
+ throw new Error(`Expected a ${origin} message string.`);
21
+ return text(value);
22
+ }
23
+ export function weaknessIds(value) {
24
+ const labels = typeof value === "string" ? [value] : strings(value);
25
+ return [
26
+ ...new Set(labels.flatMap((label) => {
27
+ const match = /^(CWE-\d+)(?::|$)/u.exec(label);
28
+ return match ? [match[1]] : [];
29
+ })),
30
+ ];
31
+ }
32
+ export function position(value, label) {
33
+ if (typeof value !== "number" || !Number.isSafeInteger(value) || value < 1)
34
+ throw new Error(`${label} must be a positive integer.`);
35
+ return value;
36
+ }
37
+ /** Parse the native code-result fields shared by Semgrep and OpenGrep. */
38
+ export function parseNativeJsonResult(value, origin) {
39
+ const result = record(value, `${origin} result`);
40
+ const extra = record(result["extra"], `${origin} extra`);
41
+ const rawMetadata = extra["metadata"];
42
+ const metadata = origin === "OpenGrep" && !isRecord(rawMetadata)
43
+ ? {}
44
+ : record(rawMetadata ?? {}, `${origin} metadata`);
45
+ const product = metadata["product"];
46
+ const validation = extra["validation_state"];
47
+ if ((origin === "OpenGrep"
48
+ ? product === "secrets"
49
+ : product != null && product !== "sast") ||
50
+ extra["sca_info"] != null ||
51
+ extra["historical_info"] != null ||
52
+ (validation != null && validation !== "NO_VALIDATOR"))
53
+ throw new Error(`This adapter supports ${origin} SAST; Supply Chain and Secrets reports need separate qualification.`);
54
+ const rule = requiredText(result["check_id"], `${origin} check_id`);
55
+ const rawPath = requiredText(result["path"], `${origin} path`);
56
+ const start = record(result["start"], `${origin} start`);
57
+ const end = record(result["end"], `${origin} end`);
58
+ const line = position(start["line"], "Start line");
59
+ const column = position(start["col"], "Start column");
60
+ const endLine = position(end["line"], "End line");
61
+ const endColumn = position(end["col"], "End column");
62
+ if (endLine < line || (endLine === line && endColumn < column))
63
+ throw new Error(`${origin} source range ends before it starts.`);
64
+ return {
65
+ result,
66
+ extra,
67
+ metadata,
68
+ rule,
69
+ rawPath,
70
+ path: relativePath(rawPath),
71
+ line,
72
+ column,
73
+ endLine,
74
+ endColumn,
75
+ };
76
+ }
77
+ export function projectNativeJsonResult(parsed, options) {
78
+ const { result, extra, metadata, rule, path, line, endLine } = parsed;
79
+ const lines = extra["lines"];
80
+ return validateExternalEvidence({
81
+ title: rule,
82
+ description: messageText(extra["message"], options.origin),
83
+ severity: options.severity(extra["severity"]),
84
+ locations: path ? [{ path, line }] : [],
85
+ details: {
86
+ kind: "sast",
87
+ repository: { url: options.repositoryUrl },
88
+ scanner_origin: options.origin,
89
+ rule: { id: rule },
90
+ weakness_ids: weaknessIds(metadata["cwe"]),
91
+ code: {
92
+ end_line: path ? endLine : null,
93
+ snippet: options.origin === "OpenGrep" && typeof lines === "string"
94
+ ? lines
95
+ : ungatedText(lines),
96
+ },
97
+ source_status: typeof extra["is_ignored"] === "boolean"
98
+ ? extra["is_ignored"]
99
+ ? "ignored"
100
+ : "open"
101
+ : null,
102
+ },
103
+ source_data: structuredClone({
104
+ report: options.reportContext,
105
+ ...(options.scanErrorCount === undefined
106
+ ? {}
107
+ : { scan_error_count: options.scanErrorCount }),
108
+ ...(options.observations
109
+ ? { results: options.observations }
110
+ : { result }),
111
+ }),
112
+ }, options.target);
113
+ }
114
+ //# sourceMappingURL=native-json-findings.js.map
@@ -0,0 +1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AC,YAAY,CAAC,KAAK,SAAS;gBACtC,CAAC,CAAC,KAAK,CAAC,YAAY,CAAC;oBACnB,CAAC,CAAC,SAAS;oBACX,CAAC,CAAC,MAAM;gBACV,CAAC,CAAC,IAAI;SACX;QACD,WAAW,EAAE,eAAe,CAAC;YAC3B,MAAM,EAAE,OAAO,CAAC,aAAa;YAC7B,GAAG,CAAC,OAAO,CAAC,cAAc,KAAK,SAAS;gBACtC,CAAC,CAAC,EAAE;gBACJ,CAAC,CAAC,EAAE,gBAAgB,EAAE,OAAO,CAAC,cAAc,EAAE,CAAC;YACjD,GAAG,CAAC,OAAO,CAAC,YAAY;gBACtB,CAAC,CAAC,EAAE,OAAO,EAAE,OAAO,CAAC,YAAY,EAAE;gBACnC,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC;SAChB,CAAC;KACH,EACD,OAAO,CAAC,MAAM,CACf,CAAC;AACJ,CAAC","sourcesContent":["import {\n type ExternalImportTarget,\n validateExternalEvidence,\n} from \"./external-import-contract.js\";\nimport type { ExternalFindingEvidence } from \"./external-import-models.js\";\nimport { isRecord } from \"./record.js\";\nimport {\n record,\n relativePath,\n requiredText,\n strings,\n text,\n} from \"./scanner-import.js\";\n\nexport function sourceRepositoryUrl(value: unknown, origin: string): string {\n const supplied = requiredText(\n value,\n `${origin} sourceRepositoryUrl (--source-repository-url)`,\n );\n const url = new URL(supplied);\n if (\n ![\"http:\", \"https:\"].includes(url.protocol) ||\n url.username ||\n url.password\n )\n throw new Error(\n \"Use an HTTP(S) source repository URL without credentials.\",\n );\n return supplied;\n}\n\nexport function ungatedText(value: unknown): string | null {\n const supplied = text(value);\n // Semgrep CE emits this placeholder for gated fields.\n return supplied === \"requires login\" ? null : supplied;\n}\n\nexport function messageText(value: unknown, origin: string): string | null {\n if (typeof value !== \"string\")\n throw new Error(`Expected a ${origin} message string.`);\n return text(value);\n}\n\nexport function weaknessIds(value: unknown): string[] {\n const labels = typeof value === \"string\" ? [value] : strings(value);\n return [\n ...new Set(\n labels.flatMap((label) => {\n const match = /^(CWE-\\d+)(?::|$)/u.exec(label);\n return match ? [match[1]!] : [];\n }),\n ),\n ];\n}\n\nexport function position(value: unknown, label: string): number {\n if (typeof value !== \"number\" || !Number.isSafeInteger(value) || value < 1)\n throw new Error(`${label} must be a positive integer.`);\n return value;\n}\n\n/** Parse the native code-result fields shared by Semgrep and OpenGrep. */\nexport function parseNativeJsonResult(\n value: unknown,\n origin: \"Semgrep\" | \"OpenGrep\",\n) {\n const result = record(value, `${origin} result`);\n const extra = record(result[\"extra\"], `${origin} extra`);\n const rawMetadata = extra[\"metadata\"];\n const metadata =\n origin === \"OpenGrep\" && !isRecord(rawMetadata)\n ? {}\n : record(rawMetadata ?? {}, `${origin} metadata`);\n const product = metadata[\"product\"];\n const validation = extra[\"validation_state\"];\n if (\n (origin === \"OpenGrep\"\n ? product === \"secrets\"\n : product != null && product !== \"sast\") ||\n extra[\"sca_info\"] != null ||\n extra[\"historical_info\"] != null ||\n (validation != null && validation !== \"NO_VALIDATOR\")\n )\n throw new Error(\n `This adapter supports ${origin} SAST; Supply Chain and Secrets reports need separate qualification.`,\n );\n const rule = requiredText(result[\"check_id\"], `${origin} check_id`);\n const rawPath = requiredText(result[\"path\"], `${origin} path`);\n const start = record(result[\"start\"], `${origin} start`);\n const end = record(result[\"end\"], `${origin} end`);\n const line = position(start[\"line\"], \"Start line\");\n const column = position(start[\"col\"], \"Start column\");\n const endLine = position(end[\"line\"], \"End line\");\n const endColumn = position(end[\"col\"], \"End column\");\n if (endLine < line || (endLine === line && endColumn < column))\n throw new Error(`${origin} source range ends before it starts.`);\n return {\n result,\n extra,\n metadata,\n rule,\n rawPath,\n path: relativePath(rawPath),\n line,\n column,\n endLine,\n endColumn,\n };\n}\n\nexport function projectNativeJsonResult(\n parsed: ReturnType<typeof parseNativeJsonResult>,\n options: {\n origin: string;\n repositoryUrl: string;\n severity: (value: unknown) => string;\n reportContext: Record<string, unknown>;\n target: ExternalImportTarget;\n scanErrorCount?: number;\n observations?: Record<string, unknown>[];\n },\n): ExternalFindingEvidence {\n const { result, extra, metadata, rule, path, line, endLine } = parsed;\n const lines = extra[\"lines\"];\n return validateExternalEvidence(\n {\n title: rule,\n description: messageText(extra[\"message\"], options.origin),\n severity: options.severity(extra[\"severity\"]),\n locations: path ? [{ path, line }] : [],\n details: {\n kind: \"sast\",\n repository: { url: options.repositoryUrl },\n scanner_origin: options.origin,\n rule: { id: rule },\n weakness_ids: weaknessIds(metadata[\"cwe\"]),\n code: {\n end_line: path ? endLine : null,\n snippet:\n options.origin === \"OpenGrep\" && typeof lines === \"string\"\n ? lines\n : ungatedText(lines),\n },\n source_status:\n typeof extra[\"is_ignored\"] === \"boolean\"\n ? extra[\"is_ignored\"]\n ? \"ignored\"\n : \"open\"\n : null,\n },\n source_data: structuredClone({\n report: options.reportContext,\n ...(options.scanErrorCount === undefined\n ? {}\n : { scan_error_count: options.scanErrorCount }),\n ...(options.observations\n ? { results: options.observations }\n : { result }),\n }),\n },\n options.target,\n );\n}\n"]}
@@ -0,0 +1,10 @@
1
+ import { type ExternalImportTarget } from "./external-import-contract.js";
2
+ import type { ExternalFindings } from "./external-findings.js";
3
+ export interface OpenGrepImportOptions {
4
+ /** Source repository declared by the caller, independent of the destination. */
5
+ sourceRepositoryUrl: string;
6
+ }
7
+ /** Import native scanner evidence without assigning a Codex assessment. */
8
+ export declare function normalizeOpenGrepFindings(input: unknown, options: OpenGrepImportOptions, target?: ExternalImportTarget): ExternalFindings;
9
+ export declare function readOpenGrepFindings(path: string, options: OpenGrepImportOptions, target?: ExternalImportTarget): Promise<ExternalFindings>;
10
+ //# sourceMappingURL=opengrep-findings.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"opengrep-findings.d.ts","sourceRoot":"","sources":["../src/opengrep-findings.ts"],"names":[],"mappings":"AACA,OAAO,EAGL,KAAK,oBAAoB,EAC1B,MAAM,+BAA+B,CAAC;AAGvC,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAgB/D,MAAM,WAAW,qBAAqB;IACpC,gFAAgF;IAChF,mBAAmB,EAAE,MAAM,CAAC;CAC7B;AAED,2EAA2E;AAC3E,wBAAgB,yBAAyB,CACvC,KAAK,EAAE,OAAO,EACd,OAAO,EAAE,qBAAqB,EAC9B,MAAM,GAAE,oBAA8B,GACrC,gBAAgB,CA0MlB;AAED,wBAAsB,oBAAoB,CACxC,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,qBAAqB,EAC9B,MAAM,GAAE,oBAA8B,GACrC,OAAO,CAAC,gBAAgB,CAAC,CAM3B"}
@@ -0,0 +1,198 @@
1
+ import { posix } from "node:path";
2
+ import { validateExternalEvidenceJson, validateExternalReport, } from "./external-import-contract.js";
3
+ import { canonicalJson } from "./record.js";
4
+ import { errorMessage } from "./errors.js";
5
+ import { messageText, parseNativeJsonResult, projectNativeJsonResult, sourceRepositoryUrl, } from "./native-json-findings.js";
6
+ import { array, readScannerJson, record, relativePath, requiredText, stableId, } from "./scanner-import.js";
7
+ /** Import native scanner evidence without assigning a Codex assessment. */
8
+ export function normalizeOpenGrepFindings(input, options, target = "cloud") {
9
+ const document = record(input, "OpenGrep report");
10
+ const repositoryUrl = sourceRepositoryUrl(options?.sourceRepositoryUrl, "OpenGrep");
11
+ const version = requiredText(document["version"], "OpenGrep version");
12
+ const { results, ...report } = document;
13
+ const values = array(results, "OpenGrep results");
14
+ const errors = array(report["errors"], "OpenGrep errors");
15
+ validateExternalReport(report);
16
+ const projection = {
17
+ origin: "OpenGrep",
18
+ repositoryUrl,
19
+ severity: () => "unknown",
20
+ reportContext: { version },
21
+ target,
22
+ scanErrorCount: errors.length,
23
+ };
24
+ const output = {
25
+ read: values.length,
26
+ findings: [],
27
+ excluded: [],
28
+ report: structuredClone(report),
29
+ };
30
+ function exclude(position, id, error) {
31
+ output.excluded.push({
32
+ position,
33
+ source_finding_id: id,
34
+ reason: errorMessage(error),
35
+ });
36
+ }
37
+ function projectGroup(observations) {
38
+ const results = observations.map(({ parsed }) => parsed.result);
39
+ if (results.length === 1)
40
+ return projectNativeJsonResult(observations[0].parsed, {
41
+ ...projection,
42
+ observations: results,
43
+ });
44
+ const sourceData = {
45
+ report: { version },
46
+ scan_error_count: errors.length,
47
+ results,
48
+ };
49
+ // Replacing [] with the same raw results adds the same wire bytes to every candidate.
50
+ const rawBytes = validateExternalEvidenceJson({ source_data: sourceData }, target) -
51
+ validateExternalEvidenceJson({ source_data: { ...sourceData, results: [] } }, target);
52
+ let projectionError;
53
+ for (const { parsed } of observations) {
54
+ try {
55
+ const evidence = projectNativeJsonResult(parsed, {
56
+ ...projection,
57
+ observations: [],
58
+ });
59
+ validateExternalEvidenceJson(evidence, target, rawBytes);
60
+ evidence.source_data["results"] = results;
61
+ return evidence;
62
+ }
63
+ catch (error) {
64
+ projectionError ??= error;
65
+ }
66
+ }
67
+ throw projectionError;
68
+ }
69
+ function retainPublishableTraces(observations, id) {
70
+ const emptyWrapperBytes = validateExternalEvidenceJson({ source_data: { results: [] } }, target);
71
+ function sizeWithTrace(observation, evidenceBytes) {
72
+ return validateExternalEvidenceJson({ source_data: { results: [observation.parsed.result] } }, target, evidenceBytes - emptyWrapperBytes);
73
+ }
74
+ const singletonErrors = new Map();
75
+ let smallestProjectionBytes = Infinity;
76
+ let coreRawBytes = 0;
77
+ for (const observation of observations) {
78
+ try {
79
+ const evidence = projectNativeJsonResult(observation.parsed, {
80
+ ...projection,
81
+ observations: [],
82
+ });
83
+ const projectedBytes = validateExternalEvidenceJson(evidence, target);
84
+ const singletonBytes = sizeWithTrace(observation, projectedBytes);
85
+ coreRawBytes += singletonBytes - projectedBytes;
86
+ smallestProjectionBytes = Math.min(smallestProjectionBytes, projectedBytes);
87
+ }
88
+ catch (error) {
89
+ singletonErrors.set(observation, errorMessage(error));
90
+ }
91
+ }
92
+ if (!singletonErrors.size)
93
+ return observations;
94
+ // Reserve every independently publishable trace, including the commas in
95
+ // their results array, before trying raw-only traces in canonical order.
96
+ const coreCount = observations.length - singletonErrors.size;
97
+ let groupBytes = smallestProjectionBytes + coreRawBytes + coreCount - 1;
98
+ return observations.filter((observation) => {
99
+ let reason = singletonErrors.get(observation);
100
+ if (reason === undefined)
101
+ return true;
102
+ if (coreCount) {
103
+ try {
104
+ groupBytes = sizeWithTrace(observation, groupBytes + 1);
105
+ return true;
106
+ }
107
+ catch (error) {
108
+ const groupReason = errorMessage(error);
109
+ if (groupReason !== reason)
110
+ reason = `${groupReason} This trace also fails standalone validation. ${reason}`;
111
+ }
112
+ }
113
+ exclude(observation.position, id, reason);
114
+ return false;
115
+ });
116
+ }
117
+ const groups = new Map();
118
+ for (const [index, value] of values.entries()) {
119
+ let id = null;
120
+ try {
121
+ const parsed = parseNativeJsonResult(value, "OpenGrep");
122
+ if (parsed.path !== null) {
123
+ // Validate the emitted spelling across platforms, keeping literal POSIX backslashes.
124
+ const path = posix.normalize(parsed.rawPath);
125
+ parsed.path =
126
+ relativePath(posix.normalize(path.replaceAll("\\", "/"))) === null
127
+ ? null
128
+ : path;
129
+ }
130
+ // Native fingerprint counters change when another analysis pass adds matches.
131
+ // A source move creates a new occurrence; this is not cross-revision matching.
132
+ id = stableId("opengrep-location", [
133
+ parsed.rule,
134
+ parsed.path ?? parsed.rawPath,
135
+ parsed.line,
136
+ parsed.column,
137
+ parsed.endLine,
138
+ parsed.endColumn,
139
+ ]);
140
+ messageText(parsed.extra["message"], "OpenGrep");
141
+ const group = groups.get(id) ?? [];
142
+ group.push({ position: index + 1, parsed });
143
+ groups.set(id, group);
144
+ }
145
+ catch (error) {
146
+ exclude(index + 1, id, error);
147
+ }
148
+ }
149
+ for (const [id, observations] of [...groups].sort(([left], [right]) => left < right ? -1 : left > right ? 1 : 0)) {
150
+ const ordered = observations.length === 1
151
+ ? observations
152
+ : observations
153
+ .flatMap((observation) => {
154
+ try {
155
+ const result = structuredClone(observation.parsed.result);
156
+ validateExternalEvidenceJson({ source_data: { results: [result] } }, target);
157
+ observation.parsed.result = result;
158
+ return [{ observation, key: canonicalJson(result) }];
159
+ }
160
+ catch (error) {
161
+ exclude(observation.position, id, error);
162
+ return [];
163
+ }
164
+ })
165
+ .sort((left, right) => left.key < right.key ? -1 : left.key > right.key ? 1 : 0)
166
+ .map(({ observation }) => observation);
167
+ if (!ordered.length)
168
+ continue;
169
+ let retained = ordered;
170
+ try {
171
+ let evidence;
172
+ try {
173
+ evidence = projectGroup(retained);
174
+ }
175
+ catch (error) {
176
+ if (retained.length === 1)
177
+ throw error;
178
+ retained = retainPublishableTraces(retained, id);
179
+ if (!retained.length)
180
+ continue;
181
+ if (retained.length === ordered.length)
182
+ throw error;
183
+ evidence = projectGroup(retained);
184
+ }
185
+ output.findings.push({ source_finding_id: id, evidence });
186
+ }
187
+ catch (error) {
188
+ for (const observation of retained)
189
+ exclude(observation.position, id, error);
190
+ }
191
+ }
192
+ output.excluded.sort((left, right) => left.position - right.position);
193
+ return output;
194
+ }
195
+ export async function readOpenGrepFindings(path, options, target = "cloud") {
196
+ return normalizeOpenGrepFindings(await readScannerJson(path), options, target);
197
+ }
198
+ //# sourceMappingURL=opengrep-findings.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"opengrep-findings.js","sourceRoot":"","sources":["../src/opengrep-findings.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,WAAW,CAAC;AAClC,OAAO,EACL,4BAA4B,EAC5B,sBAAsB,GAEvB,MAAM,+BAA+B,CAAC;AACvC,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC5C,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAE3C,OAAO,EACL,WAAW,EACX,qBAAqB,EACrB,uBAAuB,EACvB,mBAAmB,GACpB,MAAM,2BAA2B,CAAC;AACnC,OAAO,EACL,KAAK,EACL,eAAe,EACf,MAAM,EACN,YAAY,EACZ,YAAY,EACZ,QAAQ,GACT,MAAM,qBAAqB,CAAC;AAO7B,2EAA2E;AAC3E,MAAM,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S;gBAC/B,IAAI,QAAQ,CAAC,MAAM,KAAK,OAAO,CAAC,MAAM;oBAAE,MAAM,KAAK,CAAC;gBACpD,QAAQ,GAAG,YAAY,CAAC,QAAQ,CAAC,CAAC;YACpC,CAAC;YACD,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC,EAAE,iBAAiB,EAAE,EAAE,EAAE,QAAQ,EAAE,CAAC,CAAC;QAC5D,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,KAAK,MAAM,WAAW,IAAI,QAAQ;gBAChC,OAAO,CAAC,WAAW,CAAC,QAAQ,EAAE,EAAE,EAAE,KAAK,CAAC,CAAC;QAC7C,CAAC;IACH,CAAC;IACD,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE,CAAC,IAAI,CAAC,QAAQ,GAAG,KAAK,CAAC,QAAQ,CAAC,CAAC;IACtE,OAAO,MAAM,CAAC;AAChB,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,oBAAoB,CACxC,IAAY,EACZ,OAA8B,EAC9B,MAAM,GAAyB,OAAO;IAEtC,OAAO,yBAAyB,CAC9B,MAAM,eAAe,CAAC,IAAI,CAAC,EAC3B,OAAO,EACP,MAAM,CACP,CAAC;AACJ,CAAC","sourcesContent":["import { posix } from \"node:path\";\nimport {\n validateExternalEvidenceJson,\n validateExternalReport,\n type ExternalImportTarget,\n} from \"./external-import-contract.js\";\nimport { canonicalJson } from \"./record.js\";\nimport { errorMessage } from \"./errors.js\";\nimport type { ExternalFindings } from \"./external-findings.js\";\nimport {\n messageText,\n parseNativeJsonResult,\n projectNativeJsonResult,\n sourceRepositoryUrl,\n} from \"./native-json-findings.js\";\nimport {\n array,\n readScannerJson,\n record,\n relativePath,\n requiredText,\n stableId,\n} from \"./scanner-import.js\";\n\nexport interface OpenGrepImportOptions {\n /** Source repository declared by the caller, independent of the destination. */\n sourceRepositoryUrl: string;\n}\n\n/** Import native scanner evidence without assigning a Codex assessment. */\nexport function normalizeOpenGrepFindings(\n input: unknown,\n options: OpenGrepImportOptions,\n target: ExternalImportTarget = \"cloud\",\n): ExternalFindings {\n const document = record(input, \"OpenGrep report\");\n const repositoryUrl = sourceRepositoryUrl(\n options?.sourceRepositoryUrl,\n \"OpenGrep\",\n );\n const version = requiredText(document[\"version\"], \"OpenGrep version\");\n const { results, ...report } = document;\n const values = array(results, \"OpenGrep results\");\n const errors = array(report[\"errors\"], \"OpenGrep errors\");\n validateExternalReport(report);\n const projection = {\n origin: \"OpenGrep\",\n repositoryUrl,\n severity: () => \"unknown\",\n reportContext: { version },\n target,\n scanErrorCount: errors.length,\n };\n const output: ExternalFindings = {\n read: values.length,\n findings: [],\n excluded: [],\n report: structuredClone(report),\n };\n type Observation = {\n position: number;\n parsed: ReturnType<typeof parseNativeJsonResult>;\n };\n function exclude(position: number, id: string | null, error: unknown) {\n output.excluded.push({\n position,\n source_finding_id: id,\n reason: errorMessage(error),\n });\n }\n function projectGroup(observations: Observation[]) {\n const results = observations.map(({ parsed }) => parsed.result);\n if (results.length === 1)\n return projectNativeJsonResult(observations[0]!.parsed, {\n ...projection,\n observations: results,\n });\n const sourceData = {\n report: { version },\n scan_error_count: errors.length,\n results,\n };\n // Replacing [] with the same raw results adds the same wire bytes to every candidate.\n const rawBytes =\n validateExternalEvidenceJson({ source_data: sourceData }, target) -\n validateExternalEvidenceJson(\n { source_data: { ...sourceData, results: [] } },\n target,\n );\n let projectionError: unknown;\n for (const { parsed } of observations) {\n try {\n const evidence = projectNativeJsonResult(parsed, {\n ...projection,\n observations: [],\n });\n validateExternalEvidenceJson(evidence, target, rawBytes);\n evidence.source_data![\"results\"] = results;\n return evidence;\n } catch (error) {\n projectionError ??= error;\n }\n }\n throw projectionError;\n }\n function retainPublishableTraces(observations: Observation[], id: string) {\n const emptyWrapperBytes = validateExternalEvidenceJson(\n { source_data: { results: [] } },\n target,\n );\n function sizeWithTrace(observation: Observation, evidenceBytes: number) {\n return validateExternalEvidenceJson(\n { source_data: { results: [observation.parsed.result] } },\n target,\n evidenceBytes - emptyWrapperBytes,\n );\n }\n const singletonErrors = new Map<Observation, string>();\n let smallestProjectionBytes = Infinity;\n let coreRawBytes = 0;\n for (const observation of observations) {\n try {\n const evidence = projectNativeJsonResult(observation.parsed, {\n ...projection,\n observations: [],\n });\n const projectedBytes = validateExternalEvidenceJson(evidence, target);\n const singletonBytes = sizeWithTrace(observation, projectedBytes);\n coreRawBytes += singletonBytes - projectedBytes;\n smallestProjectionBytes = Math.min(\n smallestProjectionBytes,\n projectedBytes,\n );\n } catch (error) {\n singletonErrors.set(observation, errorMessage(error));\n }\n }\n if (!singletonErrors.size) return observations;\n // Reserve every independently publishable trace, including the commas in\n // their results array, before trying raw-only traces in canonical order.\n const coreCount = observations.length - singletonErrors.size;\n let groupBytes = smallestProjectionBytes + coreRawBytes + coreCount - 1;\n return observations.filter((observation) => {\n let reason = singletonErrors.get(observation);\n if (reason === undefined) return true;\n if (coreCount) {\n try {\n groupBytes = sizeWithTrace(observation, groupBytes + 1);\n return true;\n } catch (error) {\n const groupReason = errorMessage(error);\n if (groupReason !== reason)\n reason = `${groupReason} This trace also fails standalone validation. ${reason}`;\n }\n }\n exclude(observation.position, id, reason);\n return false;\n });\n }\n const groups = new Map<string, Observation[]>();\n for (const [index, value] of values.entries()) {\n let id: string | null = null;\n try {\n const parsed = parseNativeJsonResult(value, \"OpenGrep\");\n if (parsed.path !== null) {\n // Validate the emitted spelling across platforms, keeping literal POSIX backslashes.\n const path = posix.normalize(parsed.rawPath);\n parsed.path =\n relativePath(posix.normalize(path.replaceAll(\"\\\\\", \"/\"))) === null\n ? null\n : path;\n }\n // Native fingerprint counters change when another analysis pass adds matches.\n // A source move creates a new occurrence; this is not cross-revision matching.\n id = stableId(\"opengrep-location\", [\n parsed.rule,\n parsed.path ?? parsed.rawPath,\n parsed.line,\n parsed.column,\n parsed.endLine,\n parsed.endColumn,\n ]);\n messageText(parsed.extra[\"message\"], \"OpenGrep\");\n const group = groups.get(id) ?? [];\n group.push({ position: index + 1, parsed });\n groups.set(id, group);\n } catch (error) {\n exclude(index + 1, id, error);\n }\n }\n for (const [id, observations] of [...groups].sort(([left], [right]) =>\n left < right ? -1 : left > right ? 1 : 0,\n )) {\n const ordered =\n observations.length === 1\n ? observations\n : observations\n .flatMap((observation) => {\n try {\n const result = structuredClone(observation.parsed.result);\n validateExternalEvidenceJson(\n { source_data: { results: [result] } },\n target,\n );\n observation.parsed.result = result;\n return [{ observation, key: canonicalJson(result) }];\n } catch (error) {\n exclude(observation.position, id, error);\n return [];\n }\n })\n .sort((left, right) =>\n left.key < right.key ? -1 : left.key > right.key ? 1 : 0,\n )\n .map(({ observation }) => observation);\n if (!ordered.length) continue;\n let retained = ordered;\n try {\n let evidence;\n try {\n evidence = projectGroup(retained);\n } catch (error) {\n if (retained.length === 1) throw error;\n retained = retainPublishableTraces(retained, id);\n if (!retained.length) continue;\n if (retained.length === ordered.length) throw error;\n evidence = projectGroup(retained);\n }\n output.findings.push({ source_finding_id: id, evidence });\n } catch (error) {\n for (const observation of retained)\n exclude(observation.position, id, error);\n }\n }\n output.excluded.sort((left, right) => left.position - right.position);\n return output;\n}\n\nexport async function readOpenGrepFindings(\n path: string,\n options: OpenGrepImportOptions,\n target: ExternalImportTarget = \"cloud\",\n): Promise<ExternalFindings> {\n return normalizeOpenGrepFindings(\n await readScannerJson(path),\n options,\n target,\n );\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"patch-tui.d.ts","sourceRoot":"","sources":["../src/patch-tui.tsx"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,OAAO,EAAuB,MAAM,aAAa,CAAC;AAEhE,QAAA,MAAM,eAAe;aACnB,QAAQ,EAAE,WAAW;aACrB,IAAI,EAAE,KAAK;aACX,MAAM,EAAE,QAAQ;aAChB,GAAG,EAAE,YAAY;CACT,CAAC;AAEX,KAAK,aAAa,GAAG,MAAM,OAAO,eAAe,CAAC;AAgClD,MAAM,WAAW,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AAa,EAAE,MAAM,EAAE,CAAC;IACxB,YAAY,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACtC,iBAAiB,CAAC,EAAE,OAAO,CAAC;CAC7B;AAED,UAAU,aAAa;IACrB,UAAU,EAAE,MAAM,CAAC;IACnB,QAAQ,EAAE,SAAS,OAAO,EAAE,CAAC;IAC7B,UAAU,CAAC,SAAS,EAAE,cAAc,GAAG,IAAI,GAAG,IAAI,CAAC;IACnD,KAAK,CAAC,EAAE,OAAO,CAAC;CACjB;AAgTD,wBAAgB,QAAQ,CAAC,EACvB,UAAU,EACV,QAAQ,EACR,UAAU,EACV,KAA6C,GAC9C,EAAE,aAAa,GAAG,KAAK,CAAC,GAAG,CAAC,OAAO,CA4TnC;AAED,wBAAsB,WAAW,CAC/B,UAAU,EAAE,MAAM,EAClB,QAAQ,EAAE,SAAS,OAAO,EAAE,EAC5B,EACE,KAAqB,EACrB,MAAuB,EACvB,KAAK,GACN,GAAE;IACD,KAAK,CAAC,EAAE,MAAM,CAAC,UAAU,CAAC;IAC1B,MAAM,CAAC,EAAE,MAAM,CAAC,WAAW,CAAC;IAC5B,KAAK,CAAC,EAAE,OAAO,CAAC;CACZ,GACL,OAAO,CAAC,cAAc,GAAG,IAAI,CAAC,CAyBhC"}
package/dist/patch-tui.js CHANGED
@@ -127,6 +127,8 @@ function locationSource(repository, location) {
127
127
  return [];
128
128
  }
129
129
  const source = readFileSync(path, "utf8").split(/\r?\n/u);
130
+ if (source.at(-1) === "")
131
+ source.pop();
130
132
  const first = Math.max(0, location.startLine - 4);
131
133
  const last = Math.min(source.length, (location.endLine ?? location.startLine) + 3);
132
134
  return numberedCode(source.slice(first, last), first + 1, location);