secret.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. """A hard-coded credential finding's snippet and link to the change, withheld from the products.
  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. A change scan's link to the change (via_change) describes that same
  7. line in the researcher's words, so it goes with the snippet: here for the
  8. credential finding, in sarif.placed for a finding near the credential's line
  9. or whose link opens there. Only the emitted copy changes: the finding is still
  10. placed on the snippet as the researcher quoted it, and neither its id nor that
  11. of a finding placed near it hashes the credential's line.
  12. """
  13. from __future__ import annotations
  14. from typing import TYPE_CHECKING
  15. from . import cwe
  16. if TYPE_CHECKING:
  17. from .finding import Finding, Record
  18. # CWE's Simplified Mapping entry Use of Hard-coded Credentials; CWE-259, 321 and 671 roll up to it.
  19. CREDENTIALS = 798
  20. def is_credential_cwe(number: int) -> bool:
  21. """Whether CWE `number` rolls up to Use of Hard-coded Credentials."""
  22. return cwe.catalog.category_of.get(number) == CREDENTIALS
  23. def is_credential(finding: Finding) -> bool:
  24. """Whether any CWE the finding carries rolls up to Use of Hard-coded Credentials."""
  25. ids = (finding["cwe_id"], *finding["other_cwe_ids"])
  26. return any(is_credential_cwe(cwe.id_number(cwe_id)) for cwe_id in ids)
  27. def withheld(finding: Record) -> Record:
  28. """`finding` as the products carry it: a hard-coded credential's snippet and link withheld."""
  29. if not is_credential(finding):
  30. return finding
  31. return {**finding, "snippet": "", "via_change": None}