diff options
| author | Claude <noreply@anthropic.com> | 2026-11-06 00:54:48 +0000 |
|---|---|---|
| committer | Claude <noreply@anthropic.com> | 2026-11-06 00:54:48 +0000 |
| commit | 7aba3deae3ff17d032c37e07d2929d1426b34278 (patch) | |
| tree | 9c482f9acda1599bd2f9bed95a6a46b75e634e80 /plugins/claude-security/scripts/lib/sarif.py | |
| parent | 5472f5f56e0070feeeb464318b316714fe3e9105 (diff) | |
| parent | b8331dd69ab3e132bbf8cd6e89b5d8c1747e21b1 (diff) | |
Merge remote-tracking branch 'origin/main' into add-plugin/xcodeadd-plugin/xcode
Diffstat (limited to 'plugins/claude-security/scripts/lib/sarif.py')
| -rw-r--r-- | plugins/claude-security/scripts/lib/sarif.py | 51 |
1 files changed, 39 insertions, 12 deletions
diff --git a/plugins/claude-security/scripts/lib/sarif.py b/plugins/claude-security/scripts/lib/sarif.py index de98e4f5..ace752ba 100644 --- a/plugins/claude-security/scripts/lib/sarif.py +++ b/plugins/claude-security/scripts/lib/sarif.py @@ -4,13 +4,13 @@ from __future__ import annotations import hashlib import json -import posixpath import re from dataclasses import dataclass from typing import TYPE_CHECKING, Final, NamedTuple from urllib.parse import quote from . import cwe, secret, source +from .finding import LINK_SITE, repository_file if TYPE_CHECKING: import uuid @@ -173,7 +173,7 @@ def placed( `sources` is the text of each scanned file by the finding's `file`; a finding whose file is absent from it keeps its line and hashes the number. """ - texts = {repository_file(scan, file): text for file, text in sources.items()} + texts = {repository_file(scan.prefix, file=file): text for file, text in sources.items()} secrets = { (path, line) for f in findings @@ -188,23 +188,55 @@ def placed( return [placed_one(f, scan, sources.get(f["file"]), secrets) for f in findings] +class Span(NamedTuple): + """Lines `first` to `last` of the file at repository path `path`.""" + + path: str + first: int + last: int + + def placed_one( finding: Finding, scan: Scan, text: str | None, secrets: Collection[tuple[str, int]] ) -> Record: - """One finding placed in `text`, its file's content (None when unread), and given its id.""" + """One finding placed in `text`, its file's content (None when unread), and given its id. + + The link to the change is dropped for a finding inside a credential's window, + or whose link opens at a line or range that reaches one: it may describe the credential. + """ moved: Finding = finding code = None + path = repository_path(scan, finding) if text is not None: lines = source.normalized_lines(text) row = source.placing_row(lines, finding["line"], finding["snippet"]) if row is not None: moved = {**finding, "line": row + 1} - path = repository_path(scan, finding) - near_secret = any(p == path and abs(row + 1 - n) <= CONTEXT_LINES for p, n in secrets) - code = None if near_secret else code_at(lines, row) + code = None if near(Span(path, row + 1, row + 1), secrets) else code_at(lines, row) + anchor = Span(path, moved["line"], moved["line"]) + link = link_site(scan, moved["via_change"]) + if near(anchor, secrets) or (link is not None and near(link, secrets)): + moved = {**moved, "via_change": None} return {**moved, FINDING_ID: fingerprint(moved, scan, code)} +def near(span: Span, secrets: Collection[tuple[str, int]]) -> bool: + """True when `span` comes within CONTEXT_LINES of a credential line in its file.""" + return any( + p == span.path and span.first - CONTEXT_LINES <= n <= span.last + CONTEXT_LINES + for p, n in secrets + ) + + +def link_site(scan: Scan, via_change: str | None) -> Span | None: + """The span a change link opens with; None when it opens with no file:line.""" + matched = LINK_SITE.match(via_change or "") + if not matched: + return None + first, last = sorted((int(matched[2]), int(matched[3] or matched[2]))) + return Span(repository_file(scan.prefix, file=matched[1]), first, last) + + def secret_lines(home: str, credential: Finding, texts: Mapping[str, str]) -> set[tuple[str, int]]: """Every (repository path, line) of `texts` on which the `credential` finding's quote occurs. @@ -306,12 +338,7 @@ def location(finding: Finding, scan: Scan) -> dict[str, object]: def repository_path(scan: Scan, finding: Finding) -> str: """The finding's file relative to the repository top level, with any leading climb folded.""" - return repository_file(scan, finding["file"]) - - -def repository_file(scan: Scan, file: str) -> str: - """A scan-root-relative `file`, as file_field carries it, made repository-relative.""" - return posixpath.normpath(scan.prefix + file) + return repository_file(scan.prefix, file=finding["file"]) def uri_bytes(text: str) -> bytes: |
