Skip to content

Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.

fix(security): stop raising high findings on fixtures, scripts and bundles (#3685)

Swati Ahujad14886f48f13
diff --git a/docs/layers/SECURITY.md b/docs/layers/SECURITY.md
index e1b9c7576..5e0969966 100644
--- a/docs/layers/SECURITY.md
+++ b/docs/layers/SECURITY.md
@@ -33,7 +33,7 @@ repowise security check origin/main...HEAD # CI gate on what a change adds
```
From an agent, `get_risk(targets=["src/api/"])` includes a `security_signals`
-block. In the dashboard, findings are on the Security tab of the Code Health
+block, without the informational `security_sensitive_symbol` rows. In the dashboard, findings are on the Security tab of the Code Health
page, and over REST at `GET /api/repos/{repo_id}/security`.
## Reading the results
@@ -44,7 +44,9 @@ page, and over REST at `GET /api/repos/{repo_id}/security`.
severities. Severity is fixed per kind, with one exception: a finding in test
material, or under a directory named `test`, `tests`, `__tests__`, `__test__`,
`fixtures`, `__fixtures__`, `spec`, `specs`, `mock`, `mocks`, `__mocks__`,
-`example` or `examples`, is recorded at `low`. Nothing is scored or ranked.
+`example` or `examples`, is recorded at `low`. A non-secret finding in a
+script, tool, benchmark, migration or docs example is `low` too; a secret there
+keeps its severity. Nothing is scored or ranked.
| Kind | Severity | Matches |
|------|----------|---------|
@@ -94,7 +96,8 @@ and the in-change commit scan report only these by default.
- **Vendor key shapes match anywhere**, comments included: a real key in a
comment is still a leak.
- **Prose files** (`.md`, `.mdx`, `.rst`, `.txt`, `.adoc`) are scanned for the
- secret kinds only.
+ secret kinds only. So are generated and vendored files (a minified or webpack
+ bundle included) and `.d.ts` declarations: nobody here wrote their calls.
- **JavaScript kinds stay in JavaScript.** `unsafe_inner_html`,
`template_literal_sql`, `new_function_call`, `reject_unauthorized_false` and
`public_env_secret` do not fire in Python files, where such a line can only be
@@ -200,9 +203,11 @@ stores leaves it out, so it does not reach the dashboard, `get_risk` or REST.
Exact rules are in the
[CLI reference](../reference/CLI_REFERENCE.md#repowise-security-check-revspec).
-The scanner also honours detect-secrets comments: `# pragma: allowlist secret`
+The scanner also honours other scanners' comments: `# pragma: allowlist secret`
on the finding's line, or `# pragma: allowlist nextline secret` on the line
-above it (`//` and `/*` comments work too). Unlike the Repowise gate-only
+above it (detect-secrets), and `gitleaks:allow`, `trufflehog:ignore`, `nosec`
+(Bandit, gosec) and `noqa: S105`/`S106`/`S107` (Ruff) on the finding's line
+(`//` and `/*` comments work too). Unlike the Repowise gate-only
marker, allowlisted secret findings are excluded from the stored scan and
the gate; they do not appear in the gate's suppressed count. Non-secret
findings on the same line still report. A marker inside a string does not
diff --git a/packages/core/src/repowise/core/analysis/security_gate.py b/packages/core/src/repowise/core/analysis/security_gate.py
index 638d5c993..b5a8a1e74 100644
--- a/packages/core/src/repowise/core/analysis/security_gate.py
+++ b/packages/core/src/repowise/core/analysis/security_gate.py
@@ -43,9 +43,8 @@ from .change_risk.features import GIT_TIMEOUT_SECONDS, _git, revspec_head, split
from .changed_lines import DIFF_ARGS, parse_unified_diff
from .security_scan import (
SECRET_KINDS,
- _detect_secrets_allowlisted,
+ _allowlisted_lines,
marker_silences,
- mask_comments_and_strings,
masked_snippet,
scan_source,
source_lines,
@@ -431,16 +430,12 @@ def _scoped(
if (entry := _builtin_entry(text, f, lines, secrets_only)) is not None
]
custom, long_lines = _custom_entries(text, lines)
- uncommented = (
- source_lines(mask_comments_and_strings(source, strings=False))
- if "pragma: allowlist" in source
- else text.lines
- )
+ allowed = _allowlisted_lines(path, source)
return [
_row(text, entry)
for entry in [*found, *custom]
if (entry[1] not in SECRET_KINDS and not entry[1].startswith("custom:"))
- or not _detect_secrets_allowlisted(text.lines, uncommented, entry[0])
+ or entry[0] not in allowed
], long_lines
diff --git a/packages/core/src/repowise/core/analysis/security_scan.py b/packages/core/src/repowise/core/analysis/security_scan.py
index ea71ad762..702218c4a 100644
--- a/packages/core/src/repowise/core/analysis/security_scan.py
+++ b/packages/core/src/repowise/core/analysis/security_scan.py
@@ -29,6 +29,7 @@ from typing import Any
from sqlalchemy import text
from sqlalchemy.ext.asyncio import AsyncSession
+from repowise.core.code_origin import code_origin
from repowise.core.support_paths import DOC_EXTENSIONS
from repowise.core.test_paths import is_test_related_path
@@ -41,30 +42,40 @@ logger = logging.getLogger(__name__)
# the same way ``HEALTH_ANALYZER_VERSION`` forces a full health re-score
# (#3072). Without this, a scanner fix only ever reaches a file a user
# happens to edit.
-SECURITY_SCANNER_VERSION = 2
-
-# detect-secrets allows a marker on the finding's line or the line above.
-_ALLOWLIST_SAME_RE = re.compile(r"(?:#|//|/\*)\s*pragma:\s*allowlist\s+secret\b")
+SECURITY_SCANNER_VERSION = 3
+
+# The inline markers other secret scanners honour, in a comment on the
+# finding's line: detect-secrets, gitleaks, TruffleHog, Bandit and gosec
+# ``nosec``, and Ruff's hard-coded-password codes. detect-secrets also has a
+# marker for the line below.
+_ALLOWLIST_SAME_RE = re.compile(
+ r"(?:#|//|/\*)[ \t]*(?:pragma:[ \t]*allowlist[ \t]+secret|gitleaks:allow|trufflehog:ignore"
+ r"|nosec|noqa:[ \t\w,]*\bS10[5-7])\b"
+)
_ALLOWLIST_NEXT_RE = re.compile(
- r"(?:#|//|/\*)\s*pragma:\s*allowlist\s+nextline\s+secret\b"
+ r"(?:#|//|/\*)[ \t]*pragma:[ \t]*allowlist[ \t]+nextline[ \t]+secret\b"
)
+_ALLOWLIST_TOKENS = ("allowlist", "gitleaks:allow", "trufflehog:ignore", "nosec", "noqa:")
-def _detect_secrets_allowlisted(
- lines: list[str], uncommented: list[str], lineno: int
-) -> bool:
- """Whether a pragma in a comment (not a string) allowlists this line."""
+def _allowlisted_lines(file_path: str, source: str) -> set[int]:
+ """Line numbers an allow marker in a comment (not a string) covers.
- def marked(index: int, pattern: re.Pattern[str]) -> bool:
- return any(
- uncommented[index][m.start() : m.end()] != m.group()
- for m in pattern.finditer(lines[index])
- )
-
- return 0 < lineno <= len(lines) and (
- marked(lineno - 1, _ALLOWLIST_SAME_RE)
- or (lineno > 1 and marked(lineno - 2, _ALLOWLIST_NEXT_RE))
+ Prose has no comments to set apart, so a marker anywhere on its line counts.
+ """
+ if not any(token in source for token in _ALLOWLIST_TOKENS):
+ return set()
+ uncommented = (
+ None
+ if file_path.lower().endswith(_PROSE_EXTENSIONS)
+ else mask_comments_and_strings(source, strings=False, **mask_dialect(file_path))
)
+ allowed: set[int] = set()
+ for pattern, below in ((_ALLOWLIST_SAME_RE, 0), (_ALLOWLIST_NEXT_RE, 1)):
+ for m in pattern.finditer(source):
+ if uncommented is None or uncommented[m.start() : m.end()] != m.group():
+ allowed.add(source.count("\n", 0, m.start()) + 1 + below)
+ return allowed
IGNORE_TOKEN = "repowise-security-ignore"
@@ -151,6 +162,28 @@ def _is_low_severity_path(file_path: str) -> bool:
return any(p in _LOW_SEVERITY_PATH_TOKENS for p in parts) or is_test_related_path(posix_path)
+# Code that runs at build or release time, or only in docs: a sink there is not
+# a shipped one.
+_NON_SHIPPING_ORIGINS = frozenset({"tooling", "docs_example"})
+# A bundler's or generator's output, or a copied library: nobody here wrote its
+# calls, so only a secret it carries is news.
+_MACHINE_ORIGINS = frozenset({"generated", "vendored"})
+_DECLARATION_SUFFIXES = (".d.ts", ".d.mts", ".d.cts")
+
+
+def _holds_no_written_code(file_path: str, origin: str) -> bool:
+ """Whether only secret kinds apply: machine output, or a TS declaration file, which never runs."""
+ return origin in _MACHINE_ORIGINS or file_path.lower().endswith(_DECLARATION_SUFFIXES)
+
+
+def _is_low_for_path(kind: str, is_low_sev_file: bool, origin: str) -> bool:
+ """Test and fixture material never carries a shipped sink, whatever the kind.
+
+ Nor do scripts, tools or docs examples, though a secret there still leaks.
+ """
+ return is_low_sev_file or (origin in _NON_SHIPPING_ORIGINS and kind not in SECRET_KINDS)
+
+
# ---------------------------------------------------------------------------
# Pattern registry: (compiled_pattern, kind_label, severity)
# ---------------------------------------------------------------------------
@@ -260,7 +293,9 @@ _PATTERNS: list[tuple[re.Pattern, str, str]] = [
"github_token",
"high",
),
- (re.compile(r"(?i:(xox[baprs]-[0-9a-zA-Z-]{10,72}))"), "slack_token", "high"),
+ # Slack writes the prefix in lowercase, at a token's start: ``xOxP-`` inside
+ # a document id is not one.
+ (re.compile(r"(?<![A-Za-z0-9])(xox[baprs]-[0-9a-zA-Z-]{10,72})"), "slack_token", "high"),
# Bounded by lookarounds, not ``\b``: a key can end in ``-``, and a base64 run
# (a lockfile ``sha512-`` integrity hash) must not yield one from its middle.
(
@@ -357,8 +392,16 @@ _PATTERNS: list[tuple[re.Pattern, str, str]] = [
# constant (a stylesheet string assigned to a module-level name), which
# this test cannot distinguish from a genuinely dynamic value without
# dataflow, so the pattern is a places-to-read signal rather than a
- # confirmed sink.
- (re.compile(r"__html\s*:\s*(?=[A-Za-z_$(])"), "unsafe_inner_html", "med"),
+ # confirmed sink. A type keyword there (``__html: string``) is a TS
+ # annotation, not a value.
+ (
+ re.compile(
+ r"__html\s*:\s*(?=[A-Za-z_$(])"
+ r"(?!(?:string|number|boolean|any|unknown|never|undefined|null|TrustedHTML)\b)"
+ ),
+ "unsafe_inner_html",
+ "med",
+ ),
# Analogue of ``fstring_sql`` for a JS/TS template literal. The bare verb
# `SELECT` or `UPDATE` is an ordinary English word and fires on prose
# (`` `Order update failed: ${status}` ``) and even on class names
@@ -920,13 +963,51 @@ _STRING_STOP = {
marker: re.compile(re.escape(marker) + r"|\\") for marker in ("'", '"', "'''", '"""')
}
_NOT_NEWLINE = re.compile(r"[^\n]")
+_BLOCK_STOP = re.compile(r"/\*|\*/")
+_RAW_BACKTICK_STOP = re.compile("`")
+# Languages whose block comments nest, and Go, whose backtick string is raw: a
+# backslash there is a character, not an escape.
+_NESTED_COMMENT_SUFFIXES = (".kt", ".kts", ".scala", ".sc", ".swift", ".rs", ".dart")
+_RAW_BACKTICK_SUFFIXES = (".go",)
+
+
+def mask_dialect(file_path: str) -> dict[str, bool]:
+ """The :func:`mask_comments_and_strings` options *file_path*'s language needs."""
+ lowered = file_path.lower()
+ return {
+ "nested_comments": lowered.endswith(_NESTED_COMMENT_SUFFIXES),
+ "raw_backticks": lowered.endswith(_RAW_BACKTICK_SUFFIXES),
+ }
+
+
+def _block_comment_end(source: str, start: int, nested: bool) -> int:
+ """Offset just past the ``*/`` closing the block comment opened before *start*."""
+ if not nested:
+ stop = source.find("*/", start)
+ return len(source) if stop == -1 else stop + 2
+ stop, depth = start, 1
+ while depth and (m := _BLOCK_STOP.search(source, stop)):
+ depth += 1 if m.group() == "/*" else -1
+ stop = m.end()
+ return len(source) if depth else stop
+
+def _template_stop(raw_backticks: bool) -> re.Pattern[str]:
+ return _RAW_BACKTICK_STOP if raw_backticks else _TEMPLATE_STOP
-def mask_comments_and_strings(source: str, *, strings: bool = True) -> str:
+
+def mask_comments_and_strings(
+ source: str,
+ *,
+ strings: bool = True,
+ nested_comments: bool = False,
+ raw_backticks: bool = False,
+) -> str:
"""Blank common comments (and strings, unless *strings* is false), keeping offsets.
Strings are still tracked when they are kept, so a ``#`` or ``//`` inside
- one does not open a comment. Newlines are never blanked.
+ one does not open a comment. Newlines are never blanked. *nested_comments*
+ and *raw_backticks* come from :func:`mask_dialect`.
"""
out: list[str] = []
copied = 0
@@ -952,15 +1033,16 @@ def mask_comments_and_strings(source: str, *, strings: bool = True) -> str:
state = "code"
continue
if state == "block_comment":
- stop = source.find("*/", i)
- stop = size if stop == -1 else stop + 2
+ stop = _block_comment_end(source, i, nested_comments)
blank(i, stop)
i = stop
state = "code"
continue
if state in {"string", "template"}:
pattern = (
- _TEMPLATE_STOP if state == "template" else _STRING_STOP[quote * (3 if triple else 1)]
+ _STRING_STOP[quote * (3 if triple else 1)]
+ if state == "string"
+ else _template_stop(raw_backticks)
)
match = pattern.search(source, i)
# A backslash escapes the character after it, whatever it is.
@@ -1119,6 +1201,8 @@ def scan_source(file_path: str, source: str, symbols: Iterable[Any] = ()) -> lis
findings: list[dict] = []
lines = source_lines(source)
is_prose = file_path.lower().endswith(_PROSE_EXTENSIONS)
+ origin = code_origin(file_path, source)
+ secrets_only = is_prose or _holds_no_written_code(file_path, origin)
# Masking is the costly step and most files match no pattern at all, so
# each form is built once, on first use. Prose has no comments or strings
@@ -1127,7 +1211,13 @@ def scan_source(file_path: str, source: str, symbols: Iterable[Any] = ()) -> lis
def mask(strings: bool = True) -> tuple[str, list[str]]:
if strings not in masks:
- text = source if is_prose else mask_comments_and_strings(source, strings=strings)
+ text = (
+ source
+ if is_prose
+ else mask_comments_and_strings(
+ source, strings=strings, **mask_dialect(file_path)
+ )
+ )
masks[strings] = (text, lines if is_prose else source_lines(text))
return masks[strings]
@@ -1135,7 +1225,7 @@ def scan_source(file_path: str, source: str, symbols: Iterable[Any] = ()) -> lis
masked_lines = mask(strings)[1]
return masked_lines[lineno - 1] if lineno <= len(masked_lines) else line
- if not is_prose:
+ if not secrets_only:
findings.extend(_call_findings(file_path, source, lambda: mask()[0]))
# Line-by-line pattern scan. A line's edges and the newline around it read
@@ -1153,7 +1243,7 @@ def scan_source(file_path: str, source: str, symbols: Iterable[Any] = ()) -> lis
for pattern, kind, severity in _PATTERNS:
if kind in _CALL_KINDS:
continue
- if is_prose and kind not in SECRET_KINDS:
+ if secrets_only and kind not in SECRET_KINDS:
continue
if is_python and kind in _JS_ONLY_KINDS:
continue
@@ -1218,7 +1308,7 @@ def scan_source(file_path: str, source: str, symbols: Iterable[Any] = ()) -> lis
# on the opening line, unless the per-line pass already did.
pem_body_lines: set[int] = set()
for pattern, kind, severity in _SPANNING_PATTERNS:
- if is_prose and kind not in SECRET_KINDS:
+ if secrets_only and kind not in SECRET_KINDS:
continue
if kind in _MASKED_KINDS:
# The masked pattern opens with this literal, which masking keeps
@@ -1277,9 +1367,8 @@ def scan_source(file_path: str, source: str, symbols: Iterable[Any] = ()) -> lis
if finding["line"] in pem_body_lines:
finding["snippet"] = _redaction(lines[finding["line"] - 1].strip())
- # Test and fixture material never carries a shipped sink, whatever the kind.
- if is_low_sev_file:
- for finding in findings:
+ for finding in findings:
+ if _is_low_for_path(finding["kind"], is_low_sev_file, origin):
finding["severity"] = "low"
# Symbol-name scan (informational / low)
@@ -1295,16 +1384,8 @@ def scan_source(file_path: str, source: str, symbols: Iterable[Any] = ()) -> lis
}
)
- uncommented = (
- source_lines(mask_comments_and_strings(source, strings=False))
- if "pragma: allowlist" in source
- else lines
- )
- return [
- f for f in findings
- if f["kind"] not in SECRET_KINDS
- or not _detect_secrets_allowlisted(lines, uncommented, f["line"])
- ]
+ allowed = _allowlisted_lines(file_path, source)
+ return [f for f in findings if f["kind"] not in SECRET_KINDS or f["line"] not in allowed]
def scan_source_map(
diff --git a/packages/core/src/repowise/core/ingestion/always_run.py b/packages/core/src/repowise/core/ingestion/always_run.py
index 58248e296..47a7146e0 100644
--- a/packages/core/src/repowise/core/ingestion/always_run.py
+++ b/packages/core/src/repowise/core/ingestion/always_run.py
@@ -245,7 +245,7 @@ def _beside_code(path: str) -> bool:
def always_run_reason(path: str, text: str, own: OwnCode) -> str | None:
"""Why the test at *path* must run with every subset, or ``None``."""
if path.endswith(_JVM):
- return _loads_spring_context(text)
+ return _loads_spring_context(path, text)
if path.endswith(_PY):
lang = _PY_LANG
elif path.endswith(_JS):
@@ -259,15 +259,15 @@ def always_run_reason(path: str, text: str, own: OwnCode) -> str | None:
return None
-def _loads_spring_context(text: str) -> str | None:
+def _loads_spring_context(path: str, text: str) -> str | None:
# Ceiling: per file, so ``@SpringBootTest`` inherited from a base class or
# carried by a custom meta-annotation is not seen; resolving annotations
# through the class graph is the upgrade path.
if "SpringBootTest" not in text:
return None
- from ..analysis.security_scan import mask_comments_and_strings
+ from ..analysis.security_scan import mask_comments_and_strings, mask_dialect
- if _SPRING_BOOT_TEST.search(mask_comments_and_strings(text)):
+ if _SPRING_BOOT_TEST.search(mask_comments_and_strings(text, **mask_dialect(path))):
return "it loads the whole Spring application context (@SpringBootTest)"
return None
diff --git a/packages/server/src/repowise/server/mcp_server/tool_risk/assessment.py b/packages/server/src/repowise/server/mcp_server/tool_risk/assessment.py
index 24929cb65..ff8791bdd 100644
--- a/packages/server/src/repowise/server/mcp_server/tool_risk/assessment.py
+++ b/packages/server/src/repowise/server/mcp_server/tool_risk/assessment.py
@@ -13,6 +13,7 @@ from sqlalchemy import select, text
from sqlalchemy.ext.asyncio import AsyncSession
from repowise.core.analysis.pr_blast import untested_files
+from repowise.core.analysis.security_scan import SYMBOL_NAME_KINDS
from repowise.core.co_change import confidence_ratio, parse_partners
from repowise.core.git_refs import find_path_removal
from repowise.core.persistence.models import (
@@ -244,7 +245,11 @@ async def _check_test_gap(session: AsyncSession, repo_id: str, target: str) -> b
async def _get_security_signals(session: AsyncSession, repo_id: str, target: str) -> list[dict]:
- """Fetch stored security findings for *target* from security_findings table."""
+ """Fetch stored security findings for *target* from security_findings table.
+
+ Symbol-name rows stay stored for the dashboard; next to real findings they
+ only read as alarms, so an agent never sees them.
+ """
try:
rows = await session.execute(
text(
@@ -255,7 +260,11 @@ async def _get_security_signals(session: AsyncSession, repo_id: str, target: str
),
{"repo_id": repo_id, "fp": target},
)
- return [{"kind": r[0], "severity": r[1], "snippet": r[2]} for r in rows.all()]
+ return [
+ {"kind": r[0], "severity": r[1], "snippet": r[2]}
+ for r in rows.all()
+ if r[0] not in SYMBOL_NAME_KINDS
+ ]
except Exception:
return []
diff --git a/tests/unit/analysis/test_security_gate_extras.py b/tests/unit/analysis/test_security_gate_extras.py
index 3d15152eb..65bf753a3 100644
--- a/tests/unit/analysis/test_security_gate_extras.py
+++ b/tests/unit/analysis/test_security_gate_extras.py
@@ -115,6 +115,18 @@ def test_detect_secrets_pragmas_do_not_enter_gate_or_suppressed_report(repo):
assert scan.suppressed == []
+def test_other_scanners_allow_markers_keep_a_secret_out_of_the_gate(repo):
+ _write(
+ repo,
+ "cfg.go",
+ f'var a = "{KEY}" // gitleaks:allow\nvar b = "{KEY}" // #nosec G101\nvar c = "{KEY}"\n',
+ )
+ _commit(repo, "add")
+ scan = _scan(repo)
+ assert [(f["kind"], f["line_number"]) for f in scan.findings] == [("aws_access_key", 3)]
+ assert scan.suppressed == []
+
+
def test_a_kind_scoped_marker_silences_only_the_kinds_it_names(repo):
_write(
repo,
diff --git a/tests/unit/analysis/test_security_scan.py b/tests/unit/analysis/test_security_scan.py
index ce6281993..7a25f38e9 100644
--- a/tests/unit/analysis/test_security_scan.py
+++ b/tests/unit/analysis/test_security_scan.py
@@ -1479,3 +1479,113 @@ class TestSecretPrecision:
def test_vendor_key_in_a_comment_still_fires(self) -> None:
kinds = {f["kind"] for f in scan_source("src/app.py", "# AKIAQZXNRTVYWMPKLBHG\n")} # pragma: allowlist secret
assert kinds == {"aws_access_key"}
+
+
+class TestNoAlarmOnCodeThatDoesNotShip:
+ """Type annotations, release scripts, bundles and marked fixtures raise nothing high."""
+
+ KEY: ClassVar[str] = "AKIA" + "QZXNRTVYWMPKLBHG"
+
+ @staticmethod
+ def _found(path: str, source: str) -> list[tuple[str, str]]:
+ return [(f["kind"], f["severity"]) for f in scan_source(path, source)]
+
+ @pytest.mark.parametrize("annotation", ["string", "TrustedHTML", "string | TrustedHTML"])
+ def test_inner_html_type_annotation_is_not_a_sink(self, annotation: str) -> None:
+ source = f"interface Props {{\n dangerouslySetInnerHTML: {{ __html: {annotation} }};\n}}\n"
+ assert self._found("src/props.ts", source) == []
+
+ def test_declaration_file_holds_no_sink(self) -> None:
+ assert self._found("types/react.d.ts", "type T = { __html: SafeHtml };\n") == []
+
+ def test_inner_html_from_a_value_is_still_medium(self) -> None:
+ source = "<div dangerouslySetInnerHTML={{__html: foo}} />\n"
+ assert self._found("src/view.tsx", source) == [("unsafe_inner_html", "med")]
+
+ def test_inner_html_from_a_name_starting_with_a_type_word_is_still_medium(self) -> None:
+ source = "<div dangerouslySetInnerHTML={{__html: stringValue}} />\n"
+ assert self._found("src/view.tsx", source) == [("unsafe_inner_html", "med")]
+
+ def test_a_release_script_sink_is_low(self) -> None:
+ source = 'import { execSync } from "child_process";\nexecSync(cmd);\n'
+ assert self._found("scripts/release.ts", source) == [("exec_call", "low")]
+ assert self._found("src/run.ts", source) == [("exec_call", "high")]
+
+ def test_a_secret_in_a_script_stays_high(self) -> None:
+ source = f'import os\nos.system(cmd)\nAWS = "{self.KEY}"\n'
+ assert self._found("scripts/deploy.py", source) == [
+ ("os_system", "low"),
+ ("aws_access_key", "high"),
+ ]
+
+ def test_a_minified_bundle_has_no_call_sink_but_keeps_its_secrets(self) -> None:
+ bundle = "var a=1;" * 400 + 'var f=new Function("x",y);'
+ assert self._found("static/app.3f2a.js", bundle) == []
+ assert self._found("static/app.3f2a.js", bundle + f'var k="{self.KEY}";') == [
+ ("aws_access_key", "high")
+ ]
+
+ def test_new_function_in_written_source_is_still_high(self) -> None:
+ source = 'const f = new Function("x", y);\n'
+ assert self._found("src/app.js", source) == [("new_function_call", "high")]
+
+ @pytest.mark.parametrize(
+ ("path", "marker"),
+ [
+ ("cfg.go", "// gitleaks:allow"),
+ ("README.md", "#gitleaks:allow"),
+ ("cfg.go", "// #nosec G101"),
+ ("cfg.py", "# nosec"),
+ ("cfg.py", "# trufflehog:ignore"),
+ ("cfg.py", "# noqa: E501, S105"),
+ ],
+ )
+ def test_other_scanners_allow_markers_silence_a_secret(self, path: str, marker: str) -> None:
+ source = f'key = "{self.KEY}" {marker}\n'
+ assert self._found(path, source) == []
+
+ @pytest.mark.parametrize(
+ "source",
+ [
+ 'key = "{key}" # noqa: E501\n',
+ 'key = "{key} // gitleaks:allow"\n',
+ 'key = "{key}"\n',
+ ],
+ )
+ def test_a_secret_without_a_marker_comment_still_fires(self, source: str) -> None:
+ source = source.format(key=self.KEY)
+ assert self._found("src/settings.py", source) == [("aws_access_key", "high")]
+
+ def test_an_allow_marker_does_not_hide_a_call(self) -> None:
+ source = "eval(payload) # nosec\n"
+ assert self._found("src/app.py", source) == [("eval_call", "high")]
+
+ def test_a_secret_in_testdata_is_low(self) -> None:
+ source = f'var k = "{self.KEY}"\n'
+ assert self._found("pkg/testdata/creds.go", source) == [("aws_access_key", "low")]
+
+ def test_slack_prefix_inside_a_document_id_is_not_a_token(self) -> None:
+ url = "https://docs.google.com/document/d/1W7KCxOxP-1Fy5EyF2lbJGE2WuKmu5v0suYqoHas1jRM"
+ assert self._found("src/links.py", f'URL = "{url}"\n') == []
+
+ def test_kotlin_nested_block_comment_is_comment_to_its_end(self) -> None:
+ source = '/* outer /* inner */ password = "Tr0ub4dor&3x" */\nval x = 1\n'
+ assert self._found("src/App.kt", source) == []
+
+ def test_go_raw_string_ends_at_a_backslash(self) -> None:
+ source = r"s := `C:\dir\`" + '\nt := `password = "Tr0ub4dor&3x"`\n'
+ assert self._found("cmd/paths.go", source) == []
+
+ @pytest.mark.parametrize(
+ ("options", "masked"),
+ [
+ ({}, "a " + " " * 12 + " d */ e"),
+ ({"nested_comments": True}, "a " + " " * 17 + " e"),
+ ],
+ )
+ def test_masking_nests_block_comments_only_when_asked(
+ self, options: dict[str, bool], masked: str
+ ) -> None:
+ from repowise.core.analysis.security_scan import mask_comments_and_strings
+
+ assert mask_comments_and_strings("a /* b /* c */ d */ e", **options) == masked
diff --git a/tests/unit/ingestion/test_always_run.py b/tests/unit/ingestion/test_always_run.py
index 5ec492ddf..5b8f24f0c 100644
--- a/tests/unit/ingestion/test_always_run.py
+++ b/tests/unit/ingestion/test_always_run.py
@@ -291,6 +291,11 @@ def test_other_jvm_tests_have_no_reason(text) -> None:
assert _reason("src/test/java/org/x/SomeTests.java", text) is None
+def test_a_kotlin_nested_comment_hides_the_annotation() -> None:
+ text = "/* outer /* inner */\n@SpringBootTest\n*/\nclass ApiTest {}\n"
+ assert _reason("src/test/kotlin/org/x/ApiTest.kt", text) is None
+
+
def test_jvm_tests_are_judged() -> None:
assert is_judged_test("src/test/java/org/x/OwnerTests.java")
assert is_judged_test("src/test/kotlin/org/x/ApiTest.kt")
diff --git a/tests/unit/server/mcp/test_risk.py b/tests/unit/server/mcp/test_risk.py
index a8a573df2..c9e69e12d 100644
--- a/tests/unit/server/mcp/test_risk.py
+++ b/tests/unit/server/mcp/test_risk.py
@@ -1100,7 +1100,13 @@ async def test_security_signals_are_ranked_high_first(setup_mcp, session):
from repowise.server.mcp_server.tool_risk.assessment import _get_security_signals
for line, (kind, severity) in enumerate(
- [("weak_hash", "low"), ("tls_verify_false", "med"), ("pickle_loads", "high")], start=1
+ [
+ ("weak_hash", "low"),
+ ("tls_verify_false", "med"),
+ ("pickle_loads", "high"),
+ ("security_sensitive_symbol", "low"),
+ ],
+ start=1,
):
session.add(
SecurityFinding(
@@ -1117,7 +1123,12 @@ async def test_security_signals_are_ranked_high_first(setup_mcp, session):
await session.flush()
signals = await _get_security_signals(session, "repo1", "src/auth/service.py")
- assert [s["severity"] for s in signals] == ["high", "med", "low"]
+ # A symbol-name row is stored for the dashboard but never served to an agent.
+ assert [(s["kind"], s["severity"]) for s in signals] == [
+ ("pickle_loads", "high"),
+ ("tls_verify_false", "med"),
+ ("weak_hash", "low"),
+ ]
@pytest.mark.asyncio