summaryrefslogtreecommitdiff
path: root/plugins/claude-security/scripts/lib/secret.py
blob: 50815956f25d1c1ccb4b2e76c804be5b42c52f97 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
"""A hard-coded credential finding's snippet and link to the change, withheld from the products.

The line such a finding quotes is the credential itself, and the JSONL and
SARIF files exist to leave the machine (a code scanning upload, a CI
artifact), so in neither file does that finding quote it: its file, line and
symbol still locate the code, and the SARIF result's message says why no line
is quoted. A change scan's link to the change (via_change) describes that same
line in the researcher's words, so it goes with the snippet: here for the
credential finding, in sarif.placed for a finding near the credential's line
or whose link opens there.

At Change Scan Effort high a site, which is a rule at a line of a file, can
have two records: one among the findings of the change, and one among those
older than it. render_report.one_per_site gives each the credential CWE ids of
the other, so that neither quotes the line. A record under another rule on
that line still quotes it.

Only the emitted copy changes: the finding is still placed on the snippet as
the researcher quoted it, and neither its id nor that of a finding placed near
it hashes the credential's line.
"""

from __future__ import annotations

from typing import TYPE_CHECKING

from . import cwe

if TYPE_CHECKING:
    from .finding import Finding, Record

# CWE's Simplified Mapping entry Use of Hard-coded Credentials; CWE-259, 321 and 671 roll up to it.
CREDENTIALS = 798


def is_credential_cwe(number: int) -> bool:
    """Whether CWE `number` rolls up to Use of Hard-coded Credentials."""
    return cwe.catalog.category_of.get(number) == CREDENTIALS


def is_credential_id(cwe_id: str) -> bool:
    """Whether a canonical `CWE-<number>` id rolls up to Use of Hard-coded Credentials;
    ValueError for one whose number cannot be read."""
    return is_credential_cwe(cwe.id_number(cwe_id))


def is_credential(finding: Finding) -> bool:
    """Whether any CWE the finding carries rolls up to Use of Hard-coded Credentials."""
    return any(map(is_credential_id, (finding["cwe_id"], *finding["other_cwe_ids"])))


def withheld(finding: Record) -> Record:
    """`finding` as the products carry it: a hard-coded credential's snippet and link withheld."""
    if not is_credential(finding):
        return finding
    return {**finding, "snippet": "", "via_change": None}