Skip to content

armgit / AiSOC

Public Imported from GitHub
0

Open-source AI Security Operations Center: alert fusion, LLM-agent triage, MITRE ATT&CK investigation, and a replayable decision ledger for every agent step. Self-hostable, runs with no API keys, MIT licensed. Ships an MCP server for Claude, Cursor and Continue.

fix(gate): close ten blind spots in the sample-data gate (#871)

Beenu Arora16a558936654
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index d7065b999..5fbbab396 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -337,6 +337,15 @@ jobs:
# API call failed, none of it gated on demo mode — and because SWR v2
# disables `revalidateOnMount` when `fallbackData` is set, several never
# fetched at all. This gate fails the build if that shape reappears.
+ #
+ # Self-test first, as always. This one earns it twice over: the gate
+ # reported everything gated over a tree holding nine live bypasses, and
+ # several of its rules overlap by construction — a parenthesised
+ # `?? (flag ? DEMO_X : [])` satisfies both the nullish rule and the
+ # ternary rule — so each case names the rule that must account for it
+ # rather than asserting that something fired.
+ - name: Sample-data gate — self-test
+ run: python scripts/check_mock_data_gated.py --self-test
- name: Verify sample data is gated behind demo mode
run: python scripts/check_mock_data_gated.py
# The check above recognises the *shapes* that carried fabricated data:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index b34e5836f..84939699a 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -189,6 +189,29 @@ number is the one the requirement is sized against.
### Added
+- **`RENDER_FALLBACK_EXEMPT` is now checked in both directions.**
+ `ALLOWED_ILLUSTRATIVE` had a staleness check from the start and this list
+ had none, so an exemption could outlive the code it excused and go on
+ covering whatever was written next under that name.
+- **A corpus floor.** The gate refuses a root holding no `.ts`/`.tsx` rather
+ than certifying it: a renamed directory or a changed suffix produces zero
+ findings over zero files, and found-nothing and scanned-nothing print the
+ same word. It also names what it scanned, rooted at `git rev-parse`.
+- **A real `--self-test`.** Twenty-six cases, each asserting the *rule* that
+ must account for it rather than that something fired — the conditional rules
+ overlap by construction, so a boolean check would pass with half the fixes
+ reverted. Each case builds its own source and its own tree.
+- **`KNOWN_UNGATED`**, a shrink-only ledger of sites the detector reports and
+ the tree has not fixed, each with a reason, so a stricter rule can land
+ without weakening it. Not an exemption: while an entry stands the gate
+ prints the outstanding count instead of the unqualified "all gated"
+ sentence. Recorded by count, so an entry cannot survive its own fix and a
+ second violation cannot hide behind a recorded one. It shipped holding the
+ seven sites this revision first detected and is **empty** — they were fixed
+ in a parallel change, the counts stopped matching, and the entries had to be
+ deleted before the build would pass. The console is now genuinely clean:
+ 21 module-scope fabricated-record literals, 21 of them gated.
+
- **`scripts/generate_corpus_stats.py`** — generates
`apps/web/src/data/corpus-stats.json` + `corpusStats.ts` from the compiled
engine ruleset, the generated detection truth table, and the marketplace
@@ -358,6 +381,47 @@ number is the one the requirement is sized against.
### Fixed
+- **`check_mock_data_gated.py` reported "All sample-data fallbacks are gated
+ behind demo mode." on a tree carrying nine of them.** Each gap was confirmed
+ by running the gate's own patterns against the live lines rather than by
+ reading them, and every fix is falsifiable on its own: reverting any one of
+ them turns exactly one self-test case red.
+
+ The dominant one was the fabrication test. It required an identity **and**
+ at least two numeric keys, so four datasets with zero numbers between them
+ were invisible — including fifteen invented ATT&CK coverage verdicts from
+ which `CoverageAdvisorView.tsx` derives and prints a coverage percentage.
+ The harm was never numeric, and the headline figure never existed as a
+ literal the gate could match. The numeric requirement is gone, replaced by
+ two unequal arms: an **estate identifier** — a hostname, an address, a
+ ticket reference — stands alone, because no filter list or route table in
+ this console contains one; a **proper noun** counts only alongside an
+ assertion of state, because most of this console's configuration is made of
+ proper nouns. That distinction is what separates fifteen coverage verdicts
+ from the fourteen-entry MITRE tactic vocabulary two directories away.
+
+ Eight more: a ternary (`cond ? real : MOCK_X`) is the same bypass as `??`
+ and matched nothing; a demo factory reached by `return` rather than through
+ a setter; an inline object literal handed to a setter in a `catch`, which
+ the docstring had always claimed to catch and never did; `= {` object
+ literals, scanned now like `= [` arrays; `useState(MOCK_X)`; a one-record
+ array, under a `< 3` floor; a single `(` defeating `\s*` in the nullish
+ rule; and `.ts` modules, which the record glob never opened.
+
+ Gating is now decided **per constant** from its use sites, not once per
+ file: one `demoFallback` anywhere used to credit every dataset in the
+ module, and `HuntView.tsx` has exactly that pair. `_is_exempt_name` was a
+ substring test over the whole line, so a line mentioning an editor
+ placeholder was skipped even when it also carried a real bypass.
+
+ Two things the relaxed detector got wrong, both found by enumerating what it
+ *credits* rather than what it flags: a comment explaining why a mock is
+ **not** reached at render was counted as a use of it, so three components
+ were reported for documenting their own fix; and the marketing pages'
+ published disclosure address matched the mailbox arm, so a mailbox now
+ counts as estate data only when it is not sitting beside the link you are
+ meant to click.
+
- **A retracted benchmark figure was still badged "Real measurement".**
`apps/docs/docs/benchmark.md` withdrew the 75.3 % alert-reduction claim —
the harness that produced it groups on four tiers of `(rule_id, host,
diff --git a/scripts/check_mock_data_gated.py b/scripts/check_mock_data_gated.py
index 3a8bcf25e..609eed1c5 100644
--- a/scripts/check_mock_data_gated.py
+++ b/scripts/check_mock_data_gated.py
@@ -12,36 +12,77 @@ was unreachable. And SWR v2 disables `revalidateOnMount` whenever `fallbackData`
is supplied, so a component passing a mock unconditionally may never fetch at
all: the sample data is not a placeholder, it is what the view shows.
-This gate checks the mechanical shapes that caused it:
-
-* `fallbackData:` receiving a bare `MOCK_*` / `DEMO_*` reference rather than one
- wrapped in `demoFallback(...)`.
-* A `catch` block assigning a `MOCK_*` / `DEMO_*` value with no `canUseDemoData`
- check anywhere in the file.
-* A module-scope array of fabricated *records* — named entities carrying
- measurements — rendered by a component with no demo gate.
-
-That third check exists because the first two only recognise mock data that
-announces itself. Both require the `MOCK_` / `DEMO_` naming convention *and* an
-assignment through a state setter or SWR, which models one situation: a view
-that fetches, and substitutes a sample when the fetch fails.
-
-`MSSPDashboardView.tsx` was the worse case and was invisible to all of it. It
-declared `const TENANTS = [...]` — six companies with invented alert counts,
-MTTD/MTTR figures, risk scores, analyst headcounts and ARR — passed it straight
-to `useState`, and **made no API call at all**. No mock-ish name, no setter, no
-`fallbackData`, so nothing matched; and because there was no fetch, there was no
-real path for a fallback to fall back *from*. Every operator on every deployment
-saw the same six rows, permanently. `TeamAnalyticsView.tsx` had the same shape
-with six named analysts and their accuracy scores.
-
-So the third check is name-agnostic. It looks for what makes fabricated domain
-data harmful rather than for what an author happened to call it: records that
-name an entity a customer would recognise (a company, a person, a host, an IP,
-an address) *and* attach numbers to it. UI configuration — filter lists, sort
-options, language menus, marketing copy — carries neither, which is what keeps
-the check quiet: 93 module-scope object arrays in the console at the time of
-writing, 7 matched, and the 4 already behind `demoFallback()` were the mocks.
+The gate has two halves.
+
+*Named sample data* — anything called `MOCK_*` / `DEMO_*` / `SAMPLE_*` /
+`FALLBACK_*`, or built by a `makeDemo…()` / `buildDemo…()` factory — is reported
+wherever it can reach a render without passing a demo check. That covers SWR
+`fallbackData`, state setters, `useState` initial values, `return` from a hook
+or helper, and conditional fallbacks written with `??`, `||` or a ternary.
+
+*Fabricated records* are found without reference to any naming convention, by
+what makes them harmful: a module-scope dataset that names entities a customer
+would read as their own. `MSSPDashboardView.tsx` was the worst case and was
+invisible to every name-based pattern — it declared `const TENANTS = [...]`,
+six companies with invented alert counts, MTTD/MTTR figures and ARR, passed it
+straight to `useState`, and **made no API call at all**. No mock-ish name, no
+setter, no `fallbackData`, and because there was no fetch there was no real
+path for a fallback to fall back *from*. Every operator on every deployment saw
+the same six rows, permanently.
+
+What counts as a fabricated record
+----------------------------------
+The first version of this check required an identity **and** at least two
+numeric keys, on the reasoning that harm comes from naming an entity and
+attaching numbers to it. That was true of the MSSP table that motivated it and
+false in general, and it made the check quiet on four live datasets at once:
+
+ TECHNIQUES 15 records, zero numeric keys
+ DEMO_RESULTS 6 records, zero numeric keys
+ MOCK_ASSETS 10 records, zero numeric keys
+ MOCK_HANDOFF_ITEMS 8 records, zero numeric keys
+
+`CoverageAdvisorView.tsx` is the clearest of the four: fifteen invented ATT&CK
+coverage verdicts with no number anywhere in the literal, from which the
+component *derives* and renders "Coverage 50%". The headline figure never
+exists as a literal the gate could see. Requiring numbers asked for the wrong
+thing — it asked for the symptom rather than the claim.
+
+So the numeric requirement is gone, and what replaces it is two arms:
+
+* an **estate identifier** — a hostname, an address, a ticket reference, a
+ mailbox — is sufficient on its own. Measured over this console, that arm
+ produces no configuration false positives at all: no filter list, option set
+ or route table contains `WORKSTATION-042`, `203.0.113.42` or `ALR-4201`.
+
+* a **proper noun** is far weaker on its own — "Detection Rules", "Microsoft
+ Sentinel" and "Initial Access" are the product's own vocabulary, not a
+ customer's estate — so it counts only alongside an *assertion of state*: a
+ status/severity/verdict key, a timestamp, or two or more measurements. That
+ is what separates fifteen coverage verdicts from the fourteen-entry MITRE
+ tactic list two directories away, which carries `{ id, name, shortName }` and
+ asserts nothing.
+
+Gating is read per constant, not per file. The previous version asked whether
+`demoFallback` appeared anywhere in the text, so one correctly-gated SWR call
+credited every other dataset in the module — and `HuntView.tsx` has exactly
+that pair, one constant behind a demo conditional and another forty lines away
+that is not.
+
+What it will not do
+-------------------
+It will not certify a corpus it did not read. A renamed directory, a changed
+suffix or a wrong `--root` all produce zero findings over zero files, so the
+file count is part of the verdict rather than a statistic printed beside it.
+
+And when it cannot say the tree is clean, it says so instead of saying
+nothing. `KNOWN_UNGATED` records sites the detector reports and the tree has
+not fixed, with a reason each, so a stricter rule can land without either
+weakening it or waiting on somebody else's change; while any entry stands, the
+closing line is a count of what is outstanding rather than the unqualified
+sentence. It is empty today, and it emptied itself: the seven sites this
+revision first detected were fixed, the recorded counts stopped matching, and
+the entries had to go before the build would pass.
It remains deliberately shallow. It cannot prove a view is honest — only that
these regressions have not reappeared. The reviewable property is that adding a
@@ -49,6 +90,9 @@ new ungated mock fails here rather than shipping.
Usage:
python scripts/check_mock_data_gated.py [--root apps/web/src]
+ python scripts/check_mock_data_gated.py --list-records
+ python scripts/check_mock_data_gated.py --list-known
+ python scripts/check_mock_data_gated.py --self-test
"""
from __future__ import annotations
@@ -57,56 +101,227 @@ import argparse
import pathlib
import re
import sys
+from dataclasses import dataclass
# `scripts/` is on sys.path when this file is run as a program, but not when a
# test loads it by path with importlib. gate_toolkit sits beside it either way.
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parent))
-from gate_toolkit import self_test_if_requested
-
-self_test_if_requested(__file__)
+from gate_toolkit import SELF_TEST_FLAG, repo_root, self_test_main # noqa: E402
MOCK_NAME = r"(?:MOCK|DEMO|SAMPLE|FALLBACK)_[A-Z0-9_]+"
-#: A `fallbackData:` whose value mentions sample data. Whether it is gated is
-#: decided separately by looking for `demoFallback(`, rather than with a
-#: lookahead: `fallbackData:\s*(?!demoFallback\()` looks correct and is not,
-#: because `\s*` backtracks to zero width and the lookahead then passes at the
-#: space, letting `.*` find the name inside the wrapper it was meant to accept.
-FALLBACK_WITH_MOCK = re.compile(rf"fallbackData:.*{MOCK_NAME}")
+#: `makeDemoTimeline(`, `buildDemoCase(` — sample data produced rather than
+#: named. `InvestigationTimeline.tsx` used this form and passed the gate while
+#: rendering a fully invented investigation (a named analyst, a routable source
+#: IP, "Session suspended; email dispatched") whenever no run was selected.
+SAMPLE_FACTORY = r"\b(?:make|build|get|create)(?:Mock|Demo|Sample|Fake|Fallback)\w*\("
-#: An assignment of a mock inside a file, e.g. `setRules(MOCK_RULES)`.
-MOCK_ASSIGN = re.compile(rf"\bset[A-Z]\w*\(\s*{MOCK_NAME}")
+#: Either spelling of sample data, for the rules that accept both.
+SAMPLE_REF = rf"(?:{MOCK_NAME}\b|{SAMPLE_FACTORY})"
-#: A *factory* that builds sample data, e.g. `setTimeline(makeDemoTimeline())`.
-#: The two patterns above match a constant by name, which is one of the two
-#: ways to render fabricated state — `InvestigationTimeline.tsx` used the other
-#: and passed this gate while rendering a fully invented investigation
-#: (a named analyst, a routable source IP, "Session suspended; email
-#: dispatched") whenever no run was selected.
-MOCK_FACTORY_ASSIGN = re.compile(r"\bset[A-Z]\w*\(\s*(?:make|build|get|create)(?:Mock|Demo|Sample|Fake|Fallback)\w*\(")
+_MOCK_NAME_RE = re.compile(MOCK_NAME)
-#: Sample data reached through a nullish/OR fallback at the point of *render*,
-#: e.g. `const rows = data?.items ?? MOCK_ROWS;`.
-#:
-#: This is the shape the three patterns above cannot see, because the file can
-#: be correctly gated at the SWR boundary and still bypass it one line later.
-#: `ThreatIntelView.tsx` did exactly that: line 239 passed
+
+# ── Demo gating ─────────────────────────────────────────────────────────────
+
+#: Any of these on a line means *that line* consulted demo mode.
+DEMO_GATE = re.compile(r"demoFallback\s*\(|canUseDemoData|isDemoMode")
+
+#: `if (!canUseDemoData())` — the component withholds itself entirely when demo
+#: mode is off. `EASMView.tsx` and `ShiftsView.tsx` both open this way and then
+#: reference their mocks freely below, which is correct and must stay quiet.
+WITHHOLDING_GUARD = re.compile(r"if\s*\(\s*!\s*(?:canUseDemoData|isDemoMode)\s*\(\s*\)\s*\)")
+
+#: A positive demo check, which gates the block it introduces:
+#: `if (canUseDemoData()) { setRows(MOCK_ROWS); }` — the idiom `demoFallback.ts`
+#: documents. Gating has to follow the block, not just the line, or the gate
+#: would flag the exact shape its own helper tells people to write.
+POSITIVE_GATE = re.compile(r"(?:canUseDemoData|isDemoMode)\s*\(\s*\)")
+
+#: `const useFallback = !!error && canUseDemoData();` — the demo predicate
+#: bound to a name and used a few lines later. Both `DetectionsView.tsx` and
+#: `SettingsView.tsx` gate correctly this way, and a gate that reads only the
+#: literal call would report them and be wrong. One hop, deliberately: the
+#: alias has to be assigned *from* the predicate, so `HuntView.tsx`'s
+#: `const [demoMode, setDemoMode] = useState(false)` — a flag the component
+#: raises itself when the backend fails — is not mistaken for a demo check.
+DEMO_ALIAS = re.compile(r"\b(?:const|let|var)\s+([A-Za-z_$][\w$]*)\s*(?::[^=]*)?=\s*[^;]*?(?:canUseDemoData|isDemoMode)\s*\(")
+
+
+def _indent(line: str) -> int:
+ return len(line) - len(line.lstrip())
+
+
+def demo_guarded_lines(lines: list[str]) -> list[bool]:
+ """Per line: is sample data reached here already behind a demo check?
+
+ Indentation rather than brace depth. Braces appear inside template
+ literals, JSX and embedded query strings all over this console, so a
+ counter drifts; every file here is Prettier-formatted, so indentation does
+ not. The cost is that a one-line `if` body written without braces on the
+ *next* line is read as a block, which over-credits by at most one line.
+ """
+ if _withholds_itself(lines):
+ return [True] * len(lines)
+
+ aliases = {name for line in lines for name in DEMO_ALIAS.findall(line)}
+ alias_ref = re.compile(r"\b(?:" + "|".join(re.escape(a) for a in sorted(aliases)) + r")\b") if aliases else None
+
+ guarded = [False] * len(lines)
+ blocks: list[int] = [] # indentation of each open positive-gate block
+
+ for number, line in enumerate(lines):
+ stripped = line.strip()
+ if not stripped:
+ guarded[number] = bool(blocks)
+ continue
+
+ indent = _indent(line)
+ while blocks and indent <= blocks[-1]:
+ blocks.pop()
+
+ guarded[number] = bool(blocks) or bool(DEMO_GATE.search(line)) or bool(alias_ref and alias_ref.search(line))
+
+ if POSITIVE_GATE.search(line) and not WITHHOLDING_GUARD.search(line) and stripped.endswith(("{", "(", "&&")):
+ blocks.append(indent)
+
+ return guarded
+
+
+def _withholds_itself(lines: list[str]) -> bool:
+ """Whether the module refuses to render at all when demo mode is off.
+
+ `if (!canUseDemoData()) { return <Empty /> }` covers the whole component,
+ not just the lines below it — `EASMView.tsx` derives a summary object at
+ module scope from the mock it guards, and `ShiftsView.tsx` seeds `useState`
+ six lines *above* its guard. Both are correct: nothing renders.
+
+ This is the one file-level credit the gate still extends, and it is
+ narrower than what it replaces. The previous version credited a file for
+ containing the string `demoFallback` anywhere in it, so a correctly-gated
+ SWR call excused every unrelated constant in the module. This requires the
+ component to actually leave.
+ """
+ for number, line in enumerate(lines):
+ if not WITHHOLDING_GUARD.search(line):
+ continue
+ indent = _indent(line)
+ body_end = number + 1
+ while body_end < len(lines) and (not lines[body_end].strip() or _indent(lines[body_end]) > indent):
+ body_end += 1
+ if any("return" in lines[index] for index in range(number, body_end)):
+ return True
+ return False
+
+
+# ── Named sample data reaching a render ─────────────────────────────────────
+
+
+@dataclass(frozen=True)
+class Rule:
+ """One mechanical shape, with the sentence a reader gets when it fires."""
+
+ name: str
+ pattern: re.Pattern[str]
+ detail: str
+
+
+#: `fallbackData:` receiving sample data directly. Whether it is gated is
+#: decided by looking for `demoFallback(` rather than with a lookahead:
+#: `fallbackData:\s*(?!demoFallback\()` looks correct and is not, because `\s*`
+#: backtracks to zero width and the lookahead then passes at the space, letting
+#: `.*` find the name inside the wrapper it was meant to accept.
+FALLBACK_WITH_MOCK = re.compile(rf"fallbackData:.*{MOCK_NAME}")
+
+#: Sample data assigned to component state: `setRules(MOCK_RULES)`,
+#: `setTimeline(makeDemoTimeline())`.
+MOCK_ASSIGN = re.compile(rf"\bset[A-Z]\w*\(\s*{SAMPLE_REF}")
+
+#: Retained under its historical name because the published gate contract and
+#: the existing tests both refer to it; the factory arm now also reaches the
+#: other rules through `SAMPLE_REF`.
+MOCK_FACTORY_ASSIGN = re.compile(rf"\bset[A-Z]\w*\(\s*{SAMPLE_FACTORY}")
+
+#: An *inline object literal* handed to a setter inside a `catch`. The gate's
+#: docstring always promised it caught "a `catch` block assigning a sample
+#: value", and it never did: `MOCK_ASSIGN` needs a bare named identifier, so
+#: `CaseWorkspace.tsx` escaped while inventing an entire investigation in its
+#: error path — a named host, a routable pivot IP, a C2 domain, "confidence
+#: 0.88" — and reported it as `status: 'completed'`.
+CATCH_SETTER = re.compile(r"\bset[A-Z]\w*\(\s*\{")
+
+#: Sample data reached through a conditional at the point of *render*, whether
+#: written with `??`, `||` or a ternary. `ThreatIntelView.tsx` passed
#: `fallbackData: demoFallback({ indicators: MOCK_INDICATORS })` — which the
-#: gate accepted, correctly — and line 242 then read
-#: `data?.indicators ?? MOCK_INDICATORS`. With demo mode off and the API
-#: returning 404, `data` is undefined and the mock renders anyway: five
-#: invented IOCs (a "Tor exit node with ransomware C2", a LockBit payload
-#: hash) under the counters "5 Total IOCs · 4 Malicious · 3 Added Today", on a
-#: deployment where no indicator had ever been ingested. Observed live; this
-#: gate reported "All sample-data fallbacks are gated behind demo mode."
-MOCK_RENDER_FALLBACK = re.compile(rf"(?:\?\?|\|\|)\s*{MOCK_NAME}\b")
+#: gate accepted, correctly — and read `data?.indicators ?? MOCK_INDICATORS`
+#: three lines later. `SLADashboard.tsx` wrote the same bypass as
+#: `isValidMetrics ? rawMetrics : MOCK_SLA_METRICS`, which is identical in
+#: meaning and matched nothing, because the first spelling of this rule was
+#: `(?:\?\?|\|\|)\s*MOCK_NAME`. `\s*` also meant a single `(` defeated it, so
+#: `data ?? (demoMode ? DEMO_SAVED : [])` read clean.
+RENDER_FALLBACK = re.compile(rf"(?:\?\?|\|\|)[\s(!]*(?:[A-Za-z_$][\w$.]*\s*\?\s*)?{SAMPLE_REF}")
+
+#: `cond ? MOCK_A : real` and `cond ? real : MOCK_B`. Bounded so it cannot run
+#: across a whole minified line looking for something to blame.
+TERNARY_FALLBACK = re.compile(rf"\?[\s(!]*{SAMPLE_REF}|\?[^?;]{{0,160}}?:\s*[(!\s]*{SAMPLE_REF}")
+
+#: Sample data as the initial value of component state. `useState(TECHNIQUES)`
+#: renders on first paint with no fetch behind it at all, which is the MSSP
+#: shape again in one line.
+STATE_SEED = re.compile(rf"\buseState(?:<[^>]*>)?\(\s*{SAMPLE_REF}")
+
+#: `return buildDemoCase(caseId)` inside a `useMemo`. The factory rule was
+#: widened once for `setTimeline(makeDemoTimeline())` and the `return` form was
+#: never covered, so `CaseWorkspace.tsx` substituted a whole fabricated case
+#: for any case id the API failed on.
+RETURN_SAMPLE = re.compile(rf"\breturn\s+{SAMPLE_REF}")
+
+RULES: tuple[Rule, ...] = (
+ Rule(
+ "fallbackData",
+ FALLBACK_WITH_MOCK,
+ "fallbackData receives sample data directly. Wrap it in demoFallback(...) so it is withheld outside the hosted demo.",
+ ),
+ Rule(
+ "state-assignment",
+ MOCK_ASSIGN,
+ "sample data is assigned to state with no demo check. Show an error or empty state instead.",
+ ),
+ Rule(
+ "state-seed",
+ STATE_SEED,
+ "sample data seeds component state, so it renders on first paint with no fetch behind it. Start from an empty value.",
+ ),
+ Rule(
+ "returned-sample",
+ RETURN_SAMPLE,
+ "sample data is returned to the caller with no demo check, so the view receives it as though it were real.",
+ ),
+ Rule(
+ "render-fallback",
+ RENDER_FALLBACK,
+ "sample data is reached through a `??`/`||` fallback with no demo check. Gating the SWR fallbackData does not cover "
+ "this — when the request fails the mock renders anyway. Fall back to an empty value and let the view show its error state.",
+ ),
+ Rule(
+ "ternary-fallback",
+ TERNARY_FALLBACK,
+ "sample data is reached through a ternary with no demo check. `cond ? real : MOCK_X` is the same bypass as `?? MOCK_X`. "
+ "Fall back to an empty value and let the view show its error state.",
+ ),
+)
#: Sites where the fallback is UI configuration rather than a tenant's data.
#: Keyed by `(path suffix, constant)` with the reason it is not a finding.
#: Kept deliberately small: this gate's value depends on it staying quiet on
#: the filter lists and option sets that make up most module-scope arrays in
#: this console, and a gate that flags those gets switched off.
+#:
+#: Checked in both directions by `stale_render_exempt_entries()`, for the same
+#: reason `ALLOWED_ILLUSTRATIVE` is: an exemption that outlives the code it
+#: excused is cover for whatever is written next under that name. This half of
+#: the gate had no staleness check for its first three revisions.
RENDER_FALLBACK_EXEMPT: dict[tuple[str, str], str] = {
(
"identity/permissions/EffectivePermissionsView.tsx",
@@ -122,38 +337,59 @@ RENDER_FALLBACK_EXEMPT: dict[tuple[str, str], str] = {
EXEMPT_SUFFIXES = (".test.ts", ".test.tsx", ".stories.tsx", "demoFallback.ts")
#: Editor placeholder text is not data presented as tenant state.
-EXEMPT_NAMES = {
- "SAMPLE_BODIES",
- "SAMPLE_EVENT",
- "SAMPLE_SIGMA",
- "SAMPLE_KQL",
- "SAMPLE_EQL",
- "SAMPLE_SPL",
- "SAMPLE_ESQL",
- "SAMPLE_YAML",
- "SAMPLE_QUERY",
- "SAMPLE_RULE",
- "DEMO_EMAIL",
- "DEMO_PASSWORD",
-}
+#:
+#: Matched against the *constant being reported*, not against the whole line.
+#: A substring test over the line skipped any line that so much as mentioned
+#: one of these — including a line that also carried a real violation, and
+#: including `SAMPLE_QUERY` matching inside `SAMPLE_QUERY_RESULTS`.
+EXEMPT_NAMES = frozenset(
+ {
+ "SAMPLE_BODIES",
+ "SAMPLE_EVENT",
+ "SAMPLE_SIGMA",
+ "SAMPLE_KQL",
+ "SAMPLE_EQL",
+ "SAMPLE_SPL",
+ "SAMPLE_ESQL",
+ "SAMPLE_YAML",
+ "SAMPLE_QUERY",
+ "SAMPLE_RULE",
+ "DEMO_EMAIL",
+ "DEMO_PASSWORD",
+ }
+)
-def _is_exempt_name(line: str) -> bool:
- return any(name in line for name in EXEMPT_NAMES)
+def _exempt_match(matched: str) -> bool:
+ """True when every sample name in this fragment is editor placeholder text."""
+ names = set(_MOCK_NAME_RE.findall(matched))
+ return bool(names) and names <= EXEMPT_NAMES
# ── Fabricated records declared inline ──────────────────────────────────────
-#: Any module-scope `const NAME = [` / `const NAME: T[] = [`. Anchored at column
-#: zero so a filter list built inside a component body is out of scope; what
-#: this check is after is a dataset baked into the module.
-MODULE_ARRAY = re.compile(r"^(?:export\s+)?const\s+([A-Za-z_][A-Za-z0-9_]*)\s*(?::[^=]*)?=\s*\[")
+#: Any module-scope `const NAME = [` / `const NAME = {` / `const NAME: T[] = [`.
+#: Anchored at column zero so a filter list built inside a component body is out
+#: of scope; what this check is after is a dataset baked into the module.
+#:
+#: The object form matters as much as the array form and was missing: a
+#: fabricated alert, an audit page, an attack graph and a chat context are all
+#: written `= {` in this console, and all of them were invisible.
+MODULE_LITERAL = re.compile(r"^(?:export\s+)?const\s+([A-Za-z_][A-Za-z0-9_]*)\s*(?::[^=]*)?=\s*([\[{])")
+
+#: The start of the next module-scope declaration, used to bound a literal's
+#: body. Bracket counting alone is not enough — a literal holding a template
+#: literal of SPL or a JSX snippet never balances — and an unbounded read then
+#: drags hundreds of unrelated lines into the evidence for one constant.
+NEXT_DECLARATION = re.compile(r"^(?:export\b|const\b|let\b|function\b|async\b|interface\b|type\b|class\b|/\*\*)")
#: `key: 42` / `key: 3.5`. The *measurements*.
NUMERIC_KEY = re.compile(r"\b([A-Za-z_][A-Za-z0-9_]*)\s*:\s*-?\d+(?:[._]\d+)*\b")
-#: `key: 'some string'`. The candidate *identities*.
-STRING_VALUE = re.compile(r"\b[A-Za-z_][A-Za-z0-9_]*\s*:\s*'([^']{2,60})'")
+#: `key: 'some string'`. The candidate *identities*. Both quote styles: this
+#: console is single-quoted by Prettier, but a dataset pasted in from a vendor
+#: response arrives double-quoted and was invisible.
+STRING_VALUE = re.compile(r"\b[A-Za-z_][A-Za-z0-9_]*\s*:\s*['\"]([^'\"]{2,60})['\"]")
#: Keys whose numbers describe how something is *drawn*, not what it measures.
#: Without this a cytoscape stylesheet (`width: 40, height: 40`) and the
@@ -164,6 +400,21 @@ LAYOUT_KEYS = frozenset(
padding margin col row min max position""".split()
)
+#: Keys that assert *how something currently stands* rather than how it is
+#: labelled or laid out. This is what lets the proper-noun arm stay quiet: a
+#: navigation list, a tactic vocabulary and a connector marquee all carry
+#: recognisable names and none of them claims a state for what it names.
+ASSERTION_KEYS = frozenset(
+ """status state severity priority risk riskscore score health verdict disposition confidence coverage
+ covered impact likelihood criticality posture exposure compliance sla breached progress trend
+ lastseen lastsync lastfired lastrun firstseen createdat updatedat closedat detectedat timestamp ts time date""".split()
+)
+
+ASSERTION_KEY = re.compile(r"\b([A-Za-z_][A-Za-z0-9_]*)\s*:\s*[^,\n}]", re.MULTILINE)
+
+#: An ISO-8601 instant as a *value*. Configuration never carries one.
+TIMESTAMP_VALUE = re.compile(r"['\"]\d{4}-\d{2}-\d{2}T\d{2}:\d{2}")
+
#: "Acme Financial", "Sarah Chen", "GlobalRetail Corp" — a name a customer would
#: read as one of their own entities. Single capitalised words are excluded:
#: "Starter", "Critical" and "Execution" are labels, not identities.
@@ -172,16 +423,23 @@ PROPER_NOUN = re.compile(r"^[A-Z][A-Za-z.&]*(?:[ -][A-Z][A-Za-z.&]*)+$")
#: WIN-DC01, srv-01.corp.local, 10.0.0.4, [email protected].
ESTATE_IDENTIFIER = re.compile(r"^(?:[A-Z]{2,}-[A-Z0-9-]{2,}|[a-z][\w-]*-\d+\.[a-z][\w.-]+|\d{1,3}(?:\.\d{1,3}){3}|[\w.+-]+@[\w-]+\.\w+)$")
-#: Any of these in the file means the author considered demo mode.
-DEMO_GATE = re.compile(r"demoFallback|canUseDemoData|isDemoMode")
+#: The mailbox half of the above, which is the one arm that is not self-evident.
+#: A hostname or an address in module-scope data is estate data with nothing
+#: more to say about it; a mailbox might instead be the project's own published
+#: contact details, and the marketing pages carry exactly that — a security
+#: disclosure address and a sales address, each sitting beside the link you are
+#: meant to click. So a mailbox counts as an estate identifier unless the
+#: literal is a list of links, which is what `MAILBOX_IS_A_LINK` decides.
+MAILBOX = re.compile(r"^[\w.+-]+@[\w-]+\.\w+$")
+MAILBOX_IS_A_LINK = re.compile(r"\b(?:href|url|link|to)\s*:|mailto:")
#: Reviewed exceptions: fabricated-looking records that are not, and never
#: render as, a tenant's state. Keyed on (path suffix, const name) so an
#: exemption cannot silently widen to a different constant in the same file.
#:
-#: Checked in *both* directions — `--check-allowlist` fails when an entry no
-#: longer matches anything, so a stale exemption is removed rather than
-#: accumulating as cover for whatever is written next under that name.
+#: Checked in *both* directions — `stale_allowlist_entries()` fails when an
+#: entry no longer matches anything, so a stale exemption is removed rather
+#: than accumulating as cover for whatever is written next under that name.
#: Empty on purpose. The one entry this held excused `MitreStrip.tsx`, whose
#: twelve ATT&CK tactic tiles carried unsourced coverage counts behind a caveat
#: in the body copy. The component was imported nowhere and rendered on no
@@ -191,52 +449,207 @@ DEMO_GATE = re.compile(r"demoFallback|canUseDemoData|isDemoMode")
#: would have left the numbers in the tree for the next person to inherit.
ALLOWED_ILLUSTRATIVE: dict[tuple[str, str], str] = {}
+#: Live violations this gate detects and the tree has not fixed yet.
+#:
+#: Not an exemption and not a relaxation. Every site here is *reported by the
+#: detector*; this records that it is known, unfixed, and owned elsewhere, so
+#: the stricter gate can be merged without either weakening it or waiting on a
+#: change to `apps/web` that belongs to another author. The distinction that
+#: matters: removing a rule would make the tree look clean, and this makes the
+#: tree look exactly as dirty as it is.
+#:
+#: Keyed `(path suffix, rule) -> (how many, why)`. The count is what makes an
+#: entry falsifiable in both directions, the same way `EMPTY_TREE_EXCEPTIONS`
+#: records a disposition rather than a bare name: fix one of them and the
+#: count no longer matches, so the entry has to go; add a second violation of
+#: the same rule to the same file and the count no longer matches either, so
+#: it cannot hide behind the first.
+#:
+#: Shrink-only. `--list-known` prints what is left.
+#: **Empty, and it emptied itself.** This held the seven sites the relaxed
+#: detector found: a fabricated case returned from a `useMemo`, an invented
+#: investigation written into a `catch` and reported as `status: 'completed'`,
+#: a hunt substituting three detections on named workstations, an SLA ternary
+#: bypassing the `demoFallback` three lines above it, a hard-coded chat
+#: context, and fifteen ATT&CK coverage verdicts behind a printed percentage.
+#: Every one of them was *reported* for as long as this ledger existed — it
+#: recorded that they were known, it never hid them. They were fixed in a
+#: parallel change, the recorded counts stopped matching, and the entries had
+#: to be deleted before the build would pass again. That is the direction the
+#: count exists for, and it is the reason to keep the mechanism rather than
+#: leave the next person to invent a weaker one.
+KNOWN_UNGATED: dict[tuple[str, str], tuple[int, str]] = {}
+
+
+def _known_ungated_key(finding: Finding) -> tuple[str, str] | None:
+ for suffix, rule in KNOWN_UNGATED:
+ if finding.rule == rule and finding.path.endswith(suffix):
+ return (suffix, rule)
+ return None
+
+
+def stale_known_ungated_entries(findings: list[Finding]) -> list[str]:
+ """Entries whose count no longer matches what the detector reports.
+
+ Both directions from one comparison. A site that has been fixed makes the
+ count too low and the entry has to be deleted; a new violation of the same
+ rule in the same file makes it too high and cannot shelter behind the
+ recorded one.
+ """
+ seen: dict[tuple[str, str], int] = {}
+ for finding in findings:
+ key = _known_ungated_key(finding)
+ if key is not None:
+ seen[key] = seen.get(key, 0) + 1
+ stale = []
+ for key, (expected, _reason) in KNOWN_UNGATED.items():
+ actual = seen.get(key, 0)
+ if actual != expected:
+ verb = "has been fixed — delete this entry" if actual < expected else "has grown; a new violation cannot hide here"
+ stale.append(f"{key[0]} :: {key[1]} — recorded {expected}, detector reports {actual}: {verb}")
+ return stale
+
def _render_fallback_exempt(rel_path: str, matched: str) -> bool:
- """True when this `?? MOCK_*` site is documented UI configuration."""
+ """True when this conditional-fallback site is documented UI configuration."""
return any(rel_path.endswith(suffix) and const in matched for (suffix, const) in RENDER_FALLBACK_EXEMPT)
-def _array_body(lines: list[str], start: int) -> str:
- """Lines from `start` to the bracket that closes the array."""
+def _literal_body(lines: list[str], start: int, opener: str) -> str:
+ """Lines from `start` to the bracket that closes the literal.
+
+ `opener` is passed rather than re-derived: the caller has just matched it,
+ and matching a second time invites the reader to wonder what happens when
+ the second match fails.
+ """
+ closer = "]" if opener == "[" else "}"
depth = 0
body: list[str] = []
for index in range(start, min(start + 500, len(lines))):
+ if index > start and NEXT_DECLARATION.match(lines[index]):
+ break
body.append(lines[index])
- depth += lines[index].count("[") - lines[index].count("]")
+ depth += lines[index].count(opener) - lines[index].count(closer)
if index > start and depth <= 0:
break
return "\n".join(body)
-def _looks_fabricated(blob: str) -> bool:
- """A set of records that names entities and attaches numbers to them."""
- if blob.count("{") < 3:
- return False
+def _asserts_state(blob: str) -> bool:
+ """Whether the records claim how something currently stands."""
measurements = {key for key in NUMERIC_KEY.findall(blob) if key.lower() not in LAYOUT_KEYS}
- if len(measurements) < 2:
+ if len(measurements) >= 2:
+ return True
+ if TIMESTAMP_VALUE.search(blob):
+ return True
+ return any(key.lower() in ASSERTION_KEYS for key in ASSERTION_KEY.findall(blob))
+
+
+def _looks_fabricated(blob: str) -> bool:
+ """A set of records naming entities a customer would read as their own.
+
+ Two arms, deliberately unequal. An estate identifier stands alone — no
+ filter list, option set or route table in this console holds a hostname or
+ an address. A proper noun does not, because most of this console's
+ configuration is made of proper nouns; it counts only where the records
+ also assert a state for what they name.
+ """
+ values = STRING_VALUE.findall(blob)
+ estate = [value for value in values if ESTATE_IDENTIFIER.match(value)]
+ if any(not MAILBOX.match(value) for value in estate):
+ return True
+ if estate and not MAILBOX_IS_A_LINK.search(blob):
+ return True
+ if not any(PROPER_NOUN.match(value) for value in values):
return False
- return any(PROPER_NOUN.match(value) or ESTATE_IDENTIFIER.match(value) for value in STRING_VALUE.findall(blob))
+ return _asserts_state(blob)
-def find_inline_records(root: pathlib.Path) -> list[tuple[str, int, str, bool]]:
- """Every module-scope fabricated-record array: (path, line, name, gated)."""
- found: list[tuple[str, int, str, bool]] = []
- for path in sorted(root.rglob("*.tsx")):
- if path.name.endswith(EXEMPT_SUFFIXES):
- continue
+def _is_comment(line: str) -> bool:
+ return line.strip().startswith(("*", "//", "/*"))
+
+
+def _uses_of(name: str, lines: list[str], declared: range) -> list[int]:
+ """Line numbers where `name` is referenced in *code* outside its declaration.
+
+ Comments are excluded, and that is not a detail. Three components carry a
+ comment explaining why the mock is *not* reached at render — "Not `??
+ MOCK_INDICATORS`; `fallbackData` above already withholds it" — and counting
+ that sentence as a use site made the gate report the very files whose
+ authors had written down the fix. A gate reading its own documentation as
+ the defect it documents is the same mistake one level up.
+ """
+ reference = re.compile(rf"\b{re.escape(name)}\b")
+ return [number for number, line in enumerate(lines) if number not in declared and not _is_comment(line) and reference.search(line)]
+
+
+@dataclass(frozen=True)
+class Record:
+ """One module-scope literal that reads as fabricated tenant data."""
+
+ path: str
+ line: int
+ name: str
+ gated: bool
+ #: The lines that reach it without passing a demo check. Used to drop the
+ #: declaration finding when a line rule has already named the same bypass.
+ ungated_uses: tuple[int, ...] = ()
+
+
+def find_inline_records(root: pathlib.Path) -> list[Record]:
+ """Every module-scope fabricated-record literal and whether it is gated.
+
+ Gating is decided **per constant**, from the lines that reference it. The
+ previous version asked `DEMO_GATE.search(text)` once per file, so a single
+ `demoFallback` anywhere marked every module-scope array in that file as
+ gated — and a correctly-gated constant sitting beside an ungated one read
+ clean. `HuntView.tsx` is the live example: `DEMO_SAVED` is reached through
+ a demo conditional and `DEMO_RESULTS`, forty lines away, is not.
+ """
+ found: list[Record] = []
+ for path in _sources(root):
text = path.read_text(encoding="utf-8", errors="replace")
- gated = bool(DEMO_GATE.search(text))
lines = text.split("\n")
+ guarded = demo_guarded_lines(lines)
for number, line in enumerate(lines):
- match = MODULE_ARRAY.match(line)
- if not match or _is_exempt_name(line):
+ match = MODULE_LITERAL.match(line)
+ if not match or match.group(1) in EXEMPT_NAMES:
continue
- if _looks_fabricated(_array_body(lines, number)):
- found.append((str(path.relative_to(root.parent.parent.parent)), number + 1, match.group(1), gated))
+ body = _literal_body(lines, number, match.group(2))
+ if not _looks_fabricated(body):
+ continue
+ declared = range(number, number + body.count("\n") + 1)
+ uses = _uses_of(match.group(1), lines, declared)
+ # A dataset nothing references is *not* thereby safe, and treating
+ # it as safe would re-open the hole the `.ts` glob fix closes: move
+ # a fabricated array into `lib/`, import it from a component, and
+ # the declaring module has no use site at all. The allow-list's own
+ # history says the same thing — `MitreStrip.tsx` was imported
+ # nowhere and rendered on no route, and the fix was deleting it,
+ # not excusing it until someone mounted the section.
+ gated = bool(uses) and all(guarded[use] for use in uses)
+ found.append(
+ Record(
+ str(path.relative_to(root.parent.parent.parent)),
+ number + 1,
+ match.group(1),
+ gated,
+ tuple(use + 1 for use in uses if not guarded[use]),
+ )
+ )
return found
+def _sources(root: pathlib.Path) -> list[pathlib.Path]:
+ """Every TypeScript module in scope, both `.ts` and `.tsx`.
+
+ The record scan globbed `*.tsx` only, so the same fabricated dataset was a
+ finding in a component and invisible in the `lib/` module beside it —
+ which is where a dataset ends up the first time two components need it.
+ """
+ return sorted(p for p in root.rglob("*.ts*") if p.suffix in (".ts", ".tsx") and not p.name.endswith(EXEMPT_SUFFIXES))
+
+
def _allowlist_key(rel_path: str, name: str) -> tuple[str, str] | None:
for (suffix, const), _reason in ALLOWED_ILLUSTRATIVE.items():
if rel_path.endswith(suffix) and const == name:
@@ -244,62 +657,180 @@ def _allowlist_key(rel_path: str, name: str) -> tuple[str, str] | None:
return None
+@dataclass(frozen=True)
+class Finding:
+ """One site, tagged with the rule that produced it.
+
+ The tag is not decoration. Several of these rules overlap by construction
+ — a parenthesised `?? (demoMode ? DEMO_SAVED : [])` is matched by both the
+ nullish rule and the ternary rule — so a self-test that only asked "did
+ anything fire" would pass with the fix for either one reverted. Naming the
+ rule is what makes each case falsifiable on its own terms.
+ """
+
+ rule: str
+ path: str
+ line: int
+ detail: str
+
+ def __str__(self) -> str:
+ return f"{self.path}:{self.line}: {self.detail}"
+
+
+@dataclass
+class Report:
+ """What the scan found, and what it opened to find it."""
+
+ findings: list[Finding]
+ files: int
+ records: list[Record]
+
+ @property
+ def problems(self) -> list[str]:
+ return [str(finding) for finding in self.findings]
+
+
def scan(root: pathlib.Path) -> list[str]:
- problems: list[str] = []
+ """Every finding, as a reader-facing line. The shape the tests drive."""
+ return inspect(root).problems
- for path in sorted(root.rglob("*.ts*")):
- if path.name.endswith(EXEMPT_SUFFIXES):
- continue
+
+def inspect(root: pathlib.Path) -> Report:
+ findings: list[Finding] = []
+ sources = _sources(root)
+ records = find_inline_records(root)
+ #: Every (file, line) a line rule has already spoken for. A named mock
+ #: reported at the line that bypasses the gate does not also need its
+ #: declaration reported forty lines up: that is one defect described twice,
+ #: and the second description points away from the fix.
+ spoken_for: set[tuple[str, int]] = set()
+
+ for path in sources:
text = path.read_text(encoding="utf-8", errors="replace")
- rel = path.relative_to(root.parent.parent.parent)
+ rel = str(path.relative_to(root.parent.parent.parent))
lines = text.split("\n")
- file_has_guard = "canUseDemoData" in text
+ guarded = demo_guarded_lines(lines)
+ in_catch = _catch_regions(lines)
- for number, line in enumerate(lines, start=1):
- stripped = line.strip()
- if stripped.startswith(("*", "//", "/*")) or _is_exempt_name(line):
+ for number, line in enumerate(lines):
+ if _is_comment(line) or guarded[number]:
continue
- if FALLBACK_WITH_MOCK.search(line) and "demoFallback(" not in line:
- problems.append(
- f"{rel}:{number}: fallbackData receives sample data directly. "
- f"Wrap it in demoFallback(...) so it is withheld outside the hosted demo."
- )
+ fired: set[str] = set()
+ for rule in RULES:
+ # Every match on the line, not the first. `re.search` stops at
+ # the first hit, so a line carrying an exempt placeholder
+ # *before* a real bypass would be dismissed on the strength of
+ # the placeholder.
+ hits = [m.group(0) for m in rule.pattern.finditer(line)]
+ if not any(_actionable(rule, rel, line, hit) for hit in hits):
+ continue
+ fired.add(rule.name)
+ findings.append(Finding(rule.name, rel, number + 1, rule.detail))
+ spoken_for.add((rel, number + 1))
+
+ # One fallback written `?? (cond ? MOCK : [])` satisfies both
+ # conditional rules and is one bypass, not two.
+ if {"ternary-fallback", "render-fallback"} <= fired:
+ findings.pop()
+
+ if in_catch[number] and not fired and CATCH_SETTER.search(line):
+ body = _braced_body(lines, number)
+ if _invents_records(body):
+ findings.append(
+ Finding(
+ "catch-inline-object",
+ rel,
+ number + 1,
+ "a `catch` block assigns an inline object of fabricated records to state. The error path is "
+ "exactly where an operator most needs to be told the backend failed.",
+ )
+ )
+ # The whole object, not just its opening line: the names it
+ # renders sit on the lines below the setter.
+ spoken_for.update((rel, n) for n in range(number + 1, number + body.count("\n") + 2))
+
+ for record in records:
+ if record.gated or _allowlist_key(record.path, record.name) is not None:
+ continue
+ if record.ungated_uses and all((record.path, use) in spoken_for for use in record.ungated_uses):
+ continue
+ findings.append(
+ Finding(
+ "inline-records",
+ record.path,
+ record.line,
+ f"`{record.name}` is a module-scope dataset of named entities, reached by a component with no demo "
+ f"gate. If it is a tenant's state, read it from the API; if it is a sample, gate it with "
+ f"canUseDemoData().",
+ )
+ )
- if MOCK_ASSIGN.search(line) and not file_has_guard:
- problems.append(
- f"{rel}:{number}: sample data assigned to state with no canUseDemoData() "
- f"check in this file. Show an error or empty state instead."
- )
+ return Report(findings=findings, files=len(sources), records=records)
- render_fallback = MOCK_RENDER_FALLBACK.search(line)
- if render_fallback and not file_has_guard and not _render_fallback_exempt(str(rel), render_fallback.group(0)):
- problems.append(
- f"{rel}:{number}: sample data is reached through a `??`/`||` fallback with "
- f"no canUseDemoData() check in this file. Gating the SWR fallbackData does "
- f"not cover this — when the request fails the mock renders anyway. Fall back "
- f"to an empty value and let the view show its error state."
- )
- if MOCK_FACTORY_ASSIGN.search(line) and not file_has_guard:
- problems.append(
- f"{rel}:{number}: a sample-data factory is assigned to state with no "
- f"canUseDemoData() check in this file. Show an error or empty state instead."
- )
+def _actionable(rule: Rule, rel: str, line: str, hit: str) -> bool:
+ """Whether one match of one rule is a finding rather than an accepted shape."""
+ if _exempt_match(hit):
+ return False
+ if rule.name == "fallbackData" and "demoFallback(" in line:
+ return False
+ if rule.name in ("render-fallback", "ternary-fallback") and _render_fallback_exempt(rel, hit):
+ return False
+ return True
+
- # A distinct name from the `rel: Path` above. `_allowlist_key` matches on
- # a string suffix, and handing it a Path would make every allow-list entry
- # miss — noisily, but for a reason nobody would look for here.
- for record_path, number, name, gated in find_inline_records(root):
- if gated or _allowlist_key(record_path, name) is not None:
+def _catch_regions(lines: list[str]) -> list[bool]:
+ """Per line: is this inside a `catch` block?
+
+ Same indentation reasoning as `demo_guarded_lines`. The rule this feeds is
+ deliberately narrow — an inline object literal handed to a setter is only
+ a finding in an error path, because everywhere else it is ordinary state.
+ """
+ inside = [False] * len(lines)
+ opened: int | None = None
+ for number, line in enumerate(lines):
+ if not line.strip():
+ inside[number] = opened is not None
continue
- problems.append(
- f"{record_path}:{number}: `{name}` is a module-scope array of named entities with "
- f"measurements attached, in a component with no demo gate. If it is a tenant's "
- f"state, read it from the API; if it is a sample, gate it with canUseDemoData()."
- )
+ indent = _indent(line)
+ if opened is not None and indent <= opened:
+ opened = None
+ if opened is None and re.search(r"\}?\s*catch\b", line):
+ opened = indent
+ inside[number] = opened is not None
+ return inside
+
+
+def _braced_body(lines: list[str], start: int) -> str:
+ """From `start` to the brace that closes the object opened on it.
+
+ The setter and its opening brace are usually all that fits on the line —
+ `setInvestigationData({` — so a single-line test sees an empty object and
+ finds nothing. Reading the body is the whole point: what is fabricated is
+ on the lines below.
+ """
+ depth = 0
+ body: list[str] = []
+ for index in range(start, min(start + 200, len(lines))):
+ body.append(lines[index])
+ depth += lines[index].count("{") - lines[index].count("}")
+ if index > start and depth <= 0:
+ break
+ return "\n".join(body)
+
- return problems
+def _invents_records(blob: str) -> bool:
+ """Whether an inline object literal carries fabricated domain data.
+
+ `setPage({ size: 20 })` is state. `setInvestigationData({ … 'WIN-FIN-DB01'
+ … 'c2.evil-corp.io' … 'confidence: 0.88' })` is a verdict about an estate
+ nobody has, reported as `status: 'completed'`. Naming sample data inside
+ the object counts too: `setResults({ total: DEMO_RESULTS.length, hits:
+ DEMO_RESULTS })` reaches a render through a brace the setter rule cannot
+ see past.
+ """
+ return _looks_fabricated(blob) or bool(_MOCK_NAME_RE.search(blob))
def stale_allowlist_entries(root: pathlib.Path) -> list[str]:
@@ -309,51 +840,340 @@ def stale_allowlist_entries(root: pathlib.Path) -> list[str]:
exemption that outlives the code it excused is cover for whatever is
written next under that name.
"""
- live = {_allowlist_key(rel, name) for rel, _n, name, _g in find_inline_records(root)}
+ live = {_allowlist_key(record.path, record.name) for record in find_inline_records(root)}
return [f"{suffix} :: {const}" for (suffix, const) in ALLOWED_ILLUSTRATIVE if (suffix, const) not in live]
-def main() -> int:
- parser = argparse.ArgumentParser(description=__doc__)
- parser.add_argument("--root", default="apps/web/src", help="directory to scan")
+def stale_render_exempt_entries(root: pathlib.Path) -> list[str]:
+ """`RENDER_FALLBACK_EXEMPT` entries that no longer match any live line.
+
+ The same direction, for the other allow-list. `ALLOWED_ILLUSTRATIVE` was
+ checked both ways from the start and this one was not, so an exemption here
+ could outlive the code it excused and nothing would say so.
+ """
+ live: set[tuple[str, str]] = set()
+ for path in _sources(root):
+ rel = str(path.relative_to(root.parent.parent.parent))
+ text = path.read_text(encoding="utf-8", errors="replace")
+ for suffix, const in RENDER_FALLBACK_EXEMPT:
+ if rel.endswith(suffix) and re.search(rf"\b{re.escape(const)}\b", text):
+ live.add((suffix, const))
+ return [f"{suffix} :: {const}" for (suffix, const) in RENDER_FALLBACK_EXEMPT if (suffix, const) not in live]
+
+
+# ── Self-test ───────────────────────────────────────────────────────────────
+
+
+@dataclass(frozen=True)
+class Case:
+ """One self-test case: source, and the rule that must account for it.
+
+ `expect` names the rule, so a case cannot pass on the strength of a
+ different rule happening to cover the same line. That matters here because
+ the conditional rules overlap deliberately: reverting the parenthesis fix
+ in `RENDER_FALLBACK` leaves `TERNARY_FALLBACK` matching the same fixture,
+ and a boolean "did anything fire" would have gone on reporting PASS.
+
+ `expect=None` means the case must produce nothing at all.
+ """
+
+ description: str
+ source: str
+ expect: str | None
+ suffix: str = ".tsx"
+
+
+#: Each case owns its source string and is written to its own directory tree,
+#: built fresh from `Case(...)` every run. A self-test elsewhere in this tree
+#: shallow-copied one baseline object across its cases, so the first case
+#: poisoned it and every later case "caught" a code it had inherited rather
+#: than provoked. Strings are immutable and each tree is new, so neither can
+#: happen here.
+def self_test_cases() -> tuple[Case, ...]:
+ return (
+ Case(
+ "fifteen coverage verdicts with no number anywhere",
+ "const TECHNIQUES = [\n"
+ " { id: 'T1071', name: 'Application Layer Protocol', status: 'partial', priority: 'medium' },\n"
+ " { id: 'T1078', name: 'Valid Accounts', status: 'partial', priority: 'high' },\n"
+ "];\nexport function V() { return <b>{TECHNIQUES.length}</b>; }\n",
+ "inline-records",
+ ),
+ Case(
+ "a single record naming a host — the old three-record floor",
+ "const ROWS = [{ host: 'WIN-DC01', user: 'svc_admin' }];\nexport function V() { return <b>{ROWS.length}</b>; }\n",
+ "inline-records",
+ ),
+ Case(
+ "an object literal rather than an array",
+ "const CONTEXT = { caseId: 'CS-2187', alertCount: 7 };\nexport function V() { return <b>{CONTEXT.caseId}</b>; }\n",
+ "inline-records",
+ ),
+ Case(
+ "a fabricated dataset in a .ts module, not a .tsx component",
+ "export const ROWS = [{ host: 'WIN-DC01', risk: 91, score: 12 }];\nexport const used = ROWS.length;\n",
+ "inline-records",
+ suffix=".ts",
+ ),
+ Case(
+ "a ternary reaching a mock, which `??`-only matching missed",
+ "const metrics = isValid ? rawMetrics : MOCK_SLA_METRICS;\n",
+ "ternary-fallback",
+ ),
+ Case(
+ "a parenthesised nullish fallback, which `\\s*` could not cross",
+ "const saved = state.data ?? (flag ? DEMO_SAVED : []);\n",
+ "render-fallback",
+ ),
+ Case(
+ "a demo factory returned rather than assigned to a setter",
+ "function pick() {\n if (useFallback) return buildDemoCase(caseId);\n return undefined;\n}\n",
+ "returned-sample",
+ ),
+ Case(
+ "a mock seeding useState rather than reaching a setter",
+ "const [items] = useState(MOCK_HANDOFF_ITEMS);\n",
+ "state-seed",
+ ),
+ Case(
+ "an inline object of fabricated records assigned in a catch",
+ "async function run() {\n try {\n await go();\n } catch (e) {\n"
+ " setInvestigationData({\n host: 'WIN-FIN-DB01',\n domain: 'c2.evil-corp.io',\n"
+ " confidence: 0.88,\n });\n }\n}\n",
+ "catch-inline-object",
+ ),
+ Case(
+ "a line mentioning an exempt placeholder before a real bypass",
+ "const q = saved ?? SAMPLE_KQL; const rows = data ?? MOCK_ALERTS;\n",
+ "render-fallback",
+ ),
+ Case(
+ "an ungated constant sitting beside a correctly gated one",
+ "const MOCK_GOOD = [{ host: 'WIN-DC01', risk: 1, score: 2 }];\n"
+ "const MOCK_BAD = [{ host: 'WIN-SQL02', risk: 3, score: 4 }];\n"
+ "export function V() {\n"
+ " const { data } = useSWR(k, f, { fallbackData: demoFallback(MOCK_GOOD) });\n"
+ " return <b>{data?.length}{MOCK_BAD.length}</b>;\n}\n",
+ "inline-records",
+ ),
+ # ── and the directions it must not fire in ──────────────────────────
+ Case(
+ "a navigation list: proper nouns, no state asserted",
+ "const NAV_ITEMS = [\n { label: 'Detection Rules', href: '/detection' },\n"
+ " { label: 'Threat Intel', href: '/threat-intel' },\n];\nexport function V() { return <b>{NAV_ITEMS.length}</b>; }\n",
+ None,
+ ),
+ Case(
+ "a vendor vocabulary map: proper nouns, no state",
+ "const BACKEND_LABELS = { qradar: 'IBM QRadar', sentinel: 'Microsoft Sentinel' };\n"
+ "export function V() { return <b>{BACKEND_LABELS.qradar}</b>; }\n",
+ None,
+ ),
+ Case(
+ "a contact list: a mailbox beside the link you are meant to click",
+ "const FACTS = [{ label: 'Security disclosures', value: '[email protected]', href: 'https://example.com' }];\n"
+ "export function V() { return <b>{FACTS.length}</b>; }\n",
+ None,
+ ),
+ Case(
+ "the documented `if (canUseDemoData())` idiom",
+ "function load() {\n try {\n go();\n } catch {\n if (canUseDemoData()) {\n setRows(MOCK_ROWS);\n }\n }\n}\n",
+ None,
+ ),
+ Case(
+ "a demo predicate bound to a name and used a line later",
+ "const useFallback = !!error && canUseDemoData();\nconst rules = data?.rules ?? (useFallback ? DEMO_RULES : []);\n",
+ None,
+ ),
+ Case(
+ "a component that withholds itself outside demo mode",
+ "const MOCK_ASSETS = [{ ip: '203.0.113.42', status: 'critical' }];\n"
+ "export function V() {\n if (!canUseDemoData()) {\n return <Empty />;\n }\n return <b>{MOCK_ASSETS.length}</b>;\n}\n",
+ None,
+ ),
+ Case(
+ "a comment explaining why a mock is *not* reached at render",
+ "const MOCK_INDICATORS = [{ ip: '185.220.101.45', confidence: 95, score: 3 }];\n"
+ "export function V() {\n const { data } = useSWR(k, f, { fallbackData: demoFallback(MOCK_INDICATORS) });\n"
+ " // Not `?? MOCK_INDICATORS`: the fallbackData above already withholds it.\n"
+ " return <b>{data?.length ?? 0}</b>;\n}\n",
+ None,
+ ),
+ Case(
+ "an editor placeholder on its own",
+ "const q = saved ?? SAMPLE_KQL;\n",
+ None,
+ ),
+ Case(
+ "a named mock reported at its bypass is not reported twice",
+ "const MOCK_ROWS = [{ host: 'WIN-DC01', risk: 4, score: 9 }];\n"
+ "export function V() {\n return <b>{(data ?? MOCK_ROWS).length}</b>;\n}\n",
+ "render-fallback",
+ ),
+ )
+
+
+def _self_test_results(tmp: pathlib.Path) -> list[tuple[str, bool]]:
+ """Run every case against a fresh `apps/web/src` built from scratch."""
+ results: list[tuple[str, bool]] = []
+ for index, case in enumerate(self_test_cases()):
+ root = tmp / f"case{index}" / "apps" / "web" / "src" / "components" / "x"
+ root.mkdir(parents=True)
+ (root / f"V{case.suffix}").write_text(case.source, encoding="utf-8")
+ rules = [finding.rule for finding in inspect(root.parent.parent).findings]
+ if case.expect is None:
+ results.append((f"stays quiet on {case.description}", not rules))
+ else:
+ results.append((f"catches, as [{case.expect}], {case.description}", rules == [case.expect]))
+ return results
+
+
+def _ratchet_results() -> list[tuple[str, bool]]:
+ """The recorded-site ledger, in both directions.
+
+ Every case constructs its own `Finding` and its own one-entry ledger. A
+ self-test elsewhere in this tree shared one mutable baseline across cases,
+ so the first case poisoned it and the rest "caught" what they inherited.
+ """
+
+ def finding() -> Finding:
+ return Finding("returned-sample", "apps/web/src/components/cases/CaseWorkspace.tsx", 329, "detail")
+
+ def ledger(count: int) -> dict[tuple[str, str], tuple[int, str]]:
+ return {("components/cases/CaseWorkspace.tsx", "returned-sample"): (count, "recorded reason")}
+
+ original = KNOWN_UNGATED.copy()
+ results: list[tuple[str, bool]] = []
+ try:
+ for description, recorded, found, expect_stale in (
+ ("a recorded site still present is not stale", 1, 1, False),
+ ("a recorded site that has been fixed must be deleted", 1, 0, True),
+ ("a second violation cannot hide behind a recorded one", 1, 2, True),
+ ("a recorded site is suppressed from the failure list", 1, 1, False),
+ ):
+ KNOWN_UNGATED.clear()
+ KNOWN_UNGATED.update(ledger(recorded))
+ findings = [finding() for _ in range(found)]
+ stale = bool(stale_known_ungated_entries(findings))
+ results.append((f"RATCHET: {description}", stale == expect_stale))
+ KNOWN_UNGATED.clear()
+ KNOWN_UNGATED.update(ledger(1))
+ suppressed = _known_ungated_key(finding()) is not None
+ unrelated = _known_ungated_key(Finding("state-seed", "apps/web/src/components/other/X.tsx", 1, "d")) is None
+ results.append(("RATCHET: an entry covers only the rule and file it names", suppressed and unrelated))
+ finally:
+ KNOWN_UNGATED.clear()
+ KNOWN_UNGATED.update(original)
+ return results
+
+
+def self_test() -> int:
+ """Prove the gate still detects each shape, and still refuses nothing."""
+ import tempfile
+
+ print("check_mock_data_gated self-test\n")
+ with tempfile.TemporaryDirectory(prefix="aisoc-mock-gate-") as tmp:
+ extra = _self_test_results(pathlib.Path(tmp))
+ extra += _ratchet_results()
+
+ empty_corpus = pathlib.Path(tempfile.mkdtemp(prefix="aisoc-mock-empty-"))
+ (empty_corpus / "apps" / "web" / "src").mkdir(parents=True)
+ extra.append(("refuses a corpus directory that exists and holds no TypeScript", _refuses_empty_corpus(empty_corpus)))
+
+ return self_test_main(pathlib.Path(__file__).name, [], extra)
+
+
+def _refuses_empty_corpus(tree: pathlib.Path) -> bool:
+ """The corpus floor, exercised in-process.
+
+ `scan()` over a directory with no sources finds nothing, which is why the
+ count has to be the verdict rather than the findings list. Without this a
+ renamed directory or a changed suffix reads exactly like a clean console.
+ """
+ return inspect(tree / "apps" / "web" / "src").files == 0
+
+
+# ── CLI ─────────────────────────────────────────────────────────────────────
+
+
+def main(argv: list[str] | None = None) -> int:
+ parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
+ parser.add_argument("--root", default="apps/web/src", help="directory to scan, relative to the repository root")
parser.add_argument(
"--list-records",
action="store_true",
- help="list every module-scope fabricated-record array and whether it is gated",
+ help="list every module-scope fabricated-record literal and whether it is gated",
)
- args = parser.parse_args()
+ parser.add_argument("--list-known", action="store_true", help="list the recorded known-ungated sites and their reasons")
+ parser.add_argument(SELF_TEST_FLAG, action="store_true", help="prove this gate detects the shapes it claims to")
+ args = parser.parse_args(argv)
+
+ if args.self_test:
+ return self_test()
+ base = repo_root()
root = pathlib.Path(args.root)
+ if not root.is_absolute():
+ root = base / root
if not root.is_dir():
- print(f"error: {root} is not a directory", file=sys.stderr)
+ print(f"error: {root} is not a directory — nothing was scanned, so nothing can be certified", file=sys.stderr)
+ return 2
+
+ report = inspect(root)
+
+ # Found nothing and scanned nothing print the same word. A renamed console
+ # directory, a changed suffix or a wrong `--root` all produce zero findings
+ # over zero files, and without this the gate would call that clean.
+ if report.files == 0:
+ print(f"error: no .ts/.tsx sources under {root} — the corpus is empty, so a clean result would be invented", file=sys.stderr)
return 2
if args.list_records:
- for rel, number, name, gated in find_inline_records(root):
- state = "gated" if gated else ("allow-listed" if _allowlist_key(rel, name) else "UNGATED")
- print(f"[{state}] {rel}:{number} const {name}")
+ for record in sorted(report.records, key=lambda r: (r.path, r.line)):
+ state = "gated" if record.gated else ("allow-listed" if _allowlist_key(record.path, record.name) else "UNGATED")
+ print(f"[{state:>12}] {record.path}:{record.line} const {record.name}")
+ print(f"\n{len(report.records)} fabricated-record literal(s) across {report.files} file(s) under {root}")
return 0
- problems = scan(root)
+ if args.list_known:
+ for (suffix, rule), (count, reason) in sorted(KNOWN_UNGATED.items()):
+ print(f"{suffix} [{rule}] x{count}\n {reason}")
+ print(f"\n{len(KNOWN_UNGATED)} recorded site(s), {sum(c for c, _ in KNOWN_UNGATED.values())} finding(s). Shrink-only.")
+ return 0
- stale = stale_allowlist_entries(root)
+ stale = [
+ *(f"ALLOWED_ILLUSTRATIVE {entry} — no longer matches anything; remove it" for entry in stale_allowlist_entries(root)),
+ *(f"RENDER_FALLBACK_EXEMPT {entry} — no longer matches anything; remove it" for entry in stale_render_exempt_entries(root)),
+ *(f"KNOWN_UNGATED {entry}" for entry in stale_known_ungated_entries(report.findings)),
+ ]
if stale:
- print(f"{len(stale)} stale allow-list entry(ies) in ALLOWED_ILLUSTRATIVE:\n", file=sys.stderr)
+ print(f"{len(stale)} stale allow-list entry(ies):\n", file=sys.stderr)
for entry in stale:
- print(f" {entry} — no longer matches anything; remove it", file=sys.stderr)
+ print(f" {entry}", file=sys.stderr)
return 1
- if problems:
- print(f"{len(problems)} ungated sample-data site(s):\n", file=sys.stderr)
- for problem in problems:
- print(f" {problem}", file=sys.stderr)
+ outstanding = [finding for finding in report.findings if _known_ungated_key(finding) is None]
+ if outstanding:
+ print(f"{len(outstanding)} ungated sample-data site(s):\n", file=sys.stderr)
+ for finding in outstanding:
+ print(f" {finding}", file=sys.stderr)
print(
"\nSee apps/web/src/lib/demoFallback.ts. Fabricated security data must never render as a tenant's real state.",
file=sys.stderr,
)
return 1
- print("All sample-data fallbacks are gated behind demo mode.")
+ gated = sum(1 for record in report.records if record.gated)
+ recorded = len(report.findings)
+ print(f"scanned {report.files} .ts/.tsx file(s) under {root}")
+ print(f" {len(report.records)} module-scope fabricated-record literal(s): {gated} gated, {len(report.records) - gated} reported")
+ if recorded:
+ # The unqualified sentence is a claim about the whole console, so it is
+ # only printed when it is true of the whole console.
+ print("No new ungated sample-data sites.")
+ print(f" {recorded} known-ungated site(s) still outstanding — `--list-known` for the list. This number may only go down.")
+ else:
+ print("All sample-data fallbacks are gated behind demo mode.")
return 0
diff --git a/tests/test_mock_data_gate.py b/tests/test_mock_data_gate.py
index 111834c1a..ccbb30c14 100644
--- a/tests/test_mock_data_gate.py
+++ b/tests/test_mock_data_gate.py
@@ -282,3 +282,270 @@ const TACTICS = [
def test_the_shipped_allowlist_entries_all_still_match():
"""Run against the real tree, not a fixture."""
assert gate.stale_allowlist_entries(_REPO / "apps" / "web" / "src") == []
+
+
+def test_the_render_fallback_exemptions_all_still_match():
+ """The other allow-list, in the direction it had no check for.
+
+ `ALLOWED_ILLUSTRATIVE` was verified both ways from the start and
+ `RENDER_FALLBACK_EXEMPT` was not, so an entry here could outlive the code
+ it excused and go on excusing whatever was written next under that name.
+ """
+ assert gate.stale_render_exempt_entries(_REPO / "apps" / "web" / "src") == []
+
+
+# ── The blind spots the relaxed detector closes ─────────────────────────────
+#
+# One test per shape, each naming what it would have missed. The fixtures are
+# reduced from the live sites, so a regression fails here with the same
+# sentence a reviewer would need anyway.
+
+
+def _rules(tmp_path: Path, relative: str, source: str) -> list[str]:
+ """The *rule names* a fixture produces, not just how many findings.
+
+ Several of these rules overlap by construction — a parenthesised
+ `?? (flag ? DEMO_X : [])` satisfies both conditional rules — so asserting
+ a count would let a case pass with the fix it names reverted.
+ """
+ root = _tree(tmp_path, relative, source)
+ return [finding.rule for finding in gate.inspect(root).findings]
+
+
+def test_a_dataset_of_pure_strings_is_fabricated_without_any_number(tmp_path):
+ """(a) The dominant blind spot.
+
+ Fifteen invented ATT&CK coverage verdicts with no number anywhere in the
+ literal; the view derives and prints "Coverage 50%" from them, so the
+ headline figure never exists as something the gate could have matched.
+ Requiring two numeric keys asked for the symptom rather than the claim.
+ """
+ source = (
+ "const TECHNIQUES = [\n"
+ " { id: 'T1071', name: 'Application Layer Protocol', status: 'partial', priority: 'medium' },\n"
+ " { id: 'T1078', name: 'Valid Accounts', status: 'gap', priority: 'high' },\n"
+ "];\nexport function V() { return <b>{TECHNIQUES.length}</b>; }\n"
+ )
+ assert _rules(tmp_path, "components/coverage/View.tsx", source) == ["inline-records"]
+ assert not gate._asserts_state("{ name: 'Valid Accounts', href: '/x' }"), "a label with a link asserts nothing"
+
+
+def test_a_ternary_is_the_same_bypass_as_a_nullish_coalesce(tmp_path):
+ """(b) `cond ? real : MOCK_X` — identical meaning, and it matched nothing."""
+ source = "const metrics = isValidMetrics ? rawMetrics : MOCK_SLA_METRICS;\n"
+ assert _rules(tmp_path, "components/sla/View.tsx", source) == ["ternary-fallback"]
+
+
+def test_a_demo_factory_reached_by_return_is_caught(tmp_path):
+ """(c) The factory rule was widened for `setTimeline(makeDemoTimeline())`
+ and the `return` form was never covered, so a whole fabricated case was
+ substituted for any id the API failed on."""
+ source = "function pick() {\n if (useFallback) return buildDemoCase(caseId);\n return undefined;\n}\n"
+ assert _rules(tmp_path, "components/cases/View.tsx", source) == ["returned-sample"]
+
+
+def test_an_inline_object_assigned_in_a_catch_is_caught(tmp_path):
+ """(d) The docstring always promised this and the pattern never delivered
+ it: `MOCK_ASSIGN` needs a bare named identifier, so an invented
+ investigation written directly into the setter escaped."""
+ source = (
+ "async function run() {\n try {\n await go();\n } catch (e) {\n"
+ " setInvestigationData({\n host: 'WIN-FIN-DB01',\n domain: 'c2.evil-corp.io',\n"
+ " confidence: 0.88,\n });\n }\n}\n"
+ )
+ assert _rules(tmp_path, "components/cases/View.tsx", source) == ["catch-inline-object"]
+
+
+def test_an_ordinary_state_object_in_a_catch_is_not_a_finding(tmp_path):
+ """The other direction for (d): the rule is about fabricated records, not
+ about every object literal that reaches a setter from an error path."""
+ source = "async function run() {\n try {\n await go();\n } catch (e) {\n setPage({ size: 20, index: 0 });\n }\n}\n"
+ assert _rules(tmp_path, "components/misc/View.tsx", source) == []
+
+
+def test_an_object_literal_is_scanned_like_an_array(tmp_path):
+ """(e) `MODULE_ARRAY` only matched `= [`, so a fabricated alert, an audit
+ page and a chat context written `= {` were all invisible."""
+ source = "const CONTEXT = { caseId: 'CS-2187', alertCount: 7 };\nexport function V() { return <b>{CONTEXT.caseId}</b>; }\n"
+ assert _rules(tmp_path, "components/copilot/View.tsx", source) == ["inline-records"]
+
+
+def test_a_mock_seeding_usestate_is_caught(tmp_path):
+ """(f) `useState(MOCK_X)` renders on first paint with no fetch behind it."""
+ source = "const [items] = useState(MOCK_HANDOFF_ITEMS);\n"
+ assert _rules(tmp_path, "components/shifts/View.tsx", source) == ["state-seed"]
+
+
+def test_a_single_record_is_enough(tmp_path):
+ """(g) `blob.count("{") < 3` short-circuited a one-row array, which is the
+ shape of a dataset asserting a corpus size."""
+ source = "const ROWS = [{ host: 'WIN-DC01', user: 'svc_admin' }];\nexport function V() { return <b>{ROWS.length}</b>; }\n"
+ assert _rules(tmp_path, "components/alerts/View.tsx", source) == ["inline-records"]
+
+
+def test_a_parenthesised_nullish_fallback_is_caught(tmp_path):
+ """(h) `\\s*` cannot cross a `(`, so one character defeated the rule."""
+ source = "const saved = savedState.data ?? (demoMode ? DEMO_SAVED : []);\n"
+ assert _rules(tmp_path, "components/hunt/View.tsx", source) == ["render-fallback"]
+
+
+def test_gating_is_decided_per_constant_not_per_file(tmp_path):
+ """(i) One `demoFallback` anywhere marked *every* module-scope array in
+ the file as gated, so a correctly-gated constant beside an ungated one
+ read clean."""
+ source = (
+ "const MOCK_GOOD = [{ host: 'WIN-DC01', risk: 1, score: 2 }];\n"
+ "const MOCK_BAD = [{ host: 'WIN-SQL02', risk: 3, score: 4 }];\n"
+ "export function V() {\n"
+ " const { data } = useSWR(k, f, { fallbackData: demoFallback(MOCK_GOOD) });\n"
+ " return <b>{data?.length}{MOCK_BAD.length}</b>;\n}\n"
+ )
+ root = _tree(tmp_path, "components/misc/View.tsx", source)
+ records = {record.name: record.gated for record in gate.find_inline_records(root)}
+ assert records == {"MOCK_GOOD": True, "MOCK_BAD": False}
+
+
+def test_a_fabricated_dataset_in_a_ts_module_is_visible(tmp_path):
+ """(j) The record glob was `*.tsx` only, so moving a dataset into the
+ `lib/` module beside the component hid it — which is exactly where a
+ dataset ends up the first time two components need it."""
+ source = "export const ROWS = [{ host: 'WIN-DC01', risk: 91, score: 12 }];\nexport const used = ROWS.length;\n"
+ assert _rules(tmp_path, "lib/rows.ts", source) == ["inline-records"]
+
+
+def test_an_exempt_placeholder_does_not_excuse_the_rest_of_its_line(tmp_path):
+ """(j) `_is_exempt_name` was a substring test against the whole line, so
+ any line mentioning an exempt name was skipped — including one that also
+ carried a real violation."""
+ source = "const q = saved ?? SAMPLE_KQL; const rows = data ?? MOCK_ALERTS;\n"
+ assert _rules(tmp_path, "components/hunt/View.tsx", source) == ["render-fallback"]
+
+
+def test_an_exempt_placeholder_alone_is_still_exempt(tmp_path):
+ source = "const q = saved ?? SAMPLE_KQL;\n"
+ assert _rules(tmp_path, "components/hunt/View.tsx", source) == []
+
+
+# ── Quiet on the idioms the project documents ───────────────────────────────
+
+
+def test_a_demo_predicate_bound_to_a_name_still_gates(tmp_path):
+ """`const useFallback = !!error && canUseDemoData()` is how two live views
+ gate correctly. A gate reading only the literal call reports them."""
+ source = "const useFallback = !!error && canUseDemoData();\nconst rules = data?.rules ?? (useFallback ? DEMO_RULES : []);\n"
+ assert _rules(tmp_path, "components/detections/View.tsx", source) == []
+
+
+def test_a_component_that_withholds_itself_is_gated_throughout(tmp_path):
+ """`if (!canUseDemoData()) return <Empty />` covers the whole component,
+ including the `useState` seeded six lines above it."""
+ source = (
+ "const MOCK_ASSETS = [{ ip: '203.0.113.42', status: 'critical' }];\n"
+ "export function V() {\n const [rows] = useState(MOCK_ASSETS);\n"
+ " if (!canUseDemoData()) {\n return <Empty />;\n }\n return <b>{rows.length}</b>;\n}\n"
+ )
+ assert _rules(tmp_path, "components/easm/View.tsx", source) == []
+
+
+def test_a_guard_that_does_not_leave_gates_nothing(tmp_path):
+ """The other direction: a `!canUseDemoData()` that logs and continues
+ withholds nothing, so it must not credit the file."""
+ source = (
+ "const MOCK_ASSETS = [{ ip: '203.0.113.42', status: 'critical' }];\n"
+ "export function V() {\n if (!canUseDemoData()) {\n console.warn('no demo');\n }\n"
+ " return <b>{MOCK_ASSETS.length}</b>;\n}\n"
+ )
+ assert _rules(tmp_path, "components/easm/View.tsx", source) == ["inline-records"]
+
+
+def test_a_comment_about_a_mock_is_not_a_use_of_it(tmp_path):
+ """Three live components carry a comment explaining why the mock is *not*
+ reached at render. Counting that sentence as a use site made the gate
+ report the files whose authors had already written down the fix."""
+ source = (
+ "const MOCK_INDICATORS = [{ ip: '185.220.101.45', confidence: 95, score: 3 }];\n"
+ "export function V() {\n const { data } = useSWR(k, f, { fallbackData: demoFallback(MOCK_INDICATORS) });\n"
+ " // Not `?? MOCK_INDICATORS`: the fallbackData above already withholds it.\n"
+ " return <b>{data?.length ?? 0}</b>;\n}\n"
+ )
+ assert _rules(tmp_path, "components/threat-intel/View.tsx", source) == []
+
+
+def test_a_mailbox_beside_a_link_is_contact_information(tmp_path):
+ """The one place relaxing the identity test over-reached: the marketing
+ pages publish the project's own disclosure and sales addresses."""
+ source = (
+ "const FACTS = [{ label: 'Security disclosures', value: '[email protected]', href: 'https://example.com' }];\n"
+ "export function V() { return <b>{FACTS.length}</b>; }\n"
+ )
+ assert _rules(tmp_path, "app/(marketing)/about/page.tsx", source) == []
+
+
+def test_a_mailbox_with_no_link_is_still_estate_data(tmp_path):
+ """And the direction that keeps it honest: an audit row naming a user."""
+ source = (
+ "const DEMO_AUDIT = [{ user: '[email protected]', action: 'login' }];\n"
+ "export function V() { return <b>{DEMO_AUDIT.length}</b>; }\n"
+ )
+ assert _rules(tmp_path, "components/settings/View.tsx", source) == ["inline-records"]
+
+
+# ── The corpus floor ────────────────────────────────────────────────────────
+
+
+def test_an_empty_corpus_is_refused_rather_than_certified(tmp_path):
+ """Found nothing and scanned nothing print the same word.
+
+ A renamed console directory or a changed suffix produces zero findings
+ over zero files, and the gate must not call that clean.
+ """
+ root = tmp_path / "apps" / "web" / "src"
+ root.mkdir(parents=True)
+ report = gate.inspect(root)
+ assert report.files == 0
+ assert report.problems == []
+ assert gate.main(["--root", str(root)]) == 2
+
+
+def test_an_absent_corpus_is_refused(tmp_path):
+ assert gate.main(["--root", str(tmp_path / "nope")]) == 2
+
+
+# ── The known-ungated ledger, in both directions ────────────────────────────
+
+
+def _finding() -> gate.Finding:
+ """A fresh object per call. A self-test elsewhere in this tree shared one
+ mutable baseline across cases and every later case inherited the first
+ case's state."""
+ return gate.Finding("returned-sample", "apps/web/src/components/cases/CaseWorkspace.tsx", 329, "detail")
+
+
+def test_a_recorded_site_that_is_still_present_is_not_stale(monkeypatch):
+ monkeypatch.setattr(gate, "KNOWN_UNGATED", {("components/cases/CaseWorkspace.tsx", "returned-sample"): (1, "why")})
+ assert gate.stale_known_ungated_entries([_finding()]) == []
+
+
+def test_a_recorded_site_that_has_been_fixed_must_be_deleted(monkeypatch):
+ monkeypatch.setattr(gate, "KNOWN_UNGATED", {("components/cases/CaseWorkspace.tsx", "returned-sample"): (1, "why")})
+ stale = gate.stale_known_ungated_entries([])
+ assert len(stale) == 1 and "has been fixed" in stale[0]
+
+
+def test_a_second_violation_cannot_hide_behind_a_recorded_one(monkeypatch):
+ monkeypatch.setattr(gate, "KNOWN_UNGATED", {("components/cases/CaseWorkspace.tsx", "returned-sample"): (1, "why")})
+ stale = gate.stale_known_ungated_entries([_finding(), _finding()])
+ assert len(stale) == 1 and "has grown" in stale[0]
+
+
+def test_the_recorded_ledger_matches_the_tree_exactly():
+ """Run against the real tree. Every recorded site must still be reported
+ by the detector, and no recorded site may have grown."""
+ findings = gate.inspect(_REPO / "apps" / "web" / "src").findings
+ assert gate.stale_known_ungated_entries(findings) == []
+
+
+def test_every_recorded_entry_carries_a_reason():
+ for key, (count, reason) in gate.KNOWN_UNGATED.items():
+ assert count > 0, key
+ assert reason.strip(), key