secret.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. """The snippet of a hard-coded credential finding, withheld from what the products carry.
  2. The line such a finding quotes is the credential itself, and the JSONL and
  3. SARIF files exist to leave the machine (a code scanning upload, a CI
  4. artifact), so neither file quotes it: the finding's file, line and symbol
  5. still locate the code, and the SARIF result's message says why no line is
  6. quoted. Only the emitted copy changes: the finding is still placed on the
  7. snippet as the researcher quoted it, and neither its id nor that of a
  8. finding placed near it hashes the credential's line.
  9. """
  10. from __future__ import annotations
  11. from typing import TYPE_CHECKING
  12. from . import cwe
  13. if TYPE_CHECKING:
  14. from .finding import Finding, Record
  15. # CWE's Simplified Mapping entry Use of Hard-coded Credentials; CWE-259, 321 and 671 roll up to it.
  16. CREDENTIALS = 798
  17. def is_credential_cwe(number: int) -> bool:
  18. """Whether CWE `number` rolls up to Use of Hard-coded Credentials."""
  19. return cwe.catalog.category_of.get(number) == CREDENTIALS
  20. def is_credential(finding: Finding) -> bool:
  21. """Whether the finding's CWE rolls up to Use of Hard-coded Credentials."""
  22. return is_credential_cwe(cwe.id_number(finding["cwe_id"]))
  23. def withheld(finding: Record) -> Record:
  24. """`finding` as the products carry it: a hard-coded credential's snippet is empty."""
  25. return {**finding, "snippet": ""} if is_credential(finding) else finding