secret.py 1.2 KB

1234567891011121314151617181920212223242526272829303132
  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 its fingerprint hashes no text of
  8. the file (see sarif.fingerprint).
  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
  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(finding: Finding) -> bool:
  18. """Whether the finding's CWE rolls up to Use of Hard-coded Credentials."""
  19. return cwe.catalog.category_of.get(cwe.id_number(finding["cwe_id"])) == CREDENTIALS
  20. def withheld(finding: Finding) -> Finding:
  21. """`finding` as the products carry it: a hard-coded credential's snippet is empty."""
  22. return {**finding, "snippet": ""} if is_credential(finding) else finding