Skip to content

feat(discovery): report skipped scans and hook policy [WEB-6392] - #440

Merged
iyadzargar merged 28 commits into
stagingfrom
WEB-6392
Nov 30, 2026
Merged

iyadzargar merged 28 commits into
stagingfrom
WEB-6392

Conversation

@iyadzargar

@iyadzargar iyadzargar commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Refs WEB-6392

What

  1. Scans say why. When discovery skips a scan, it now sends a skipped report with a reason. Before, it sent nothing and the backend later marked the scan as timed out.
  2. Long scans stay alive. During a scan, discovery sends an in_progress heartbeat every 120 seconds.
  3. Hook policy per tool. The report now carries a hook_policy object for each tool, with has_our_hook true only when a working Unbound hook is on the device.
On the device Before After
Discovery skips the scan Nothing sent, later a false timeout skipped with a reason
Scan runs a long time No signal until the end Heartbeat every 120 seconds
Console hook points at a missing script Not visible has_our_hook: false

Why

  1. False timeouts. One customer had 6,762 scan timeouts that were long or skipped scans.
  2. Broken hooks hid. A managed policy can name the Unbound hook while the script is missing. We found this on a real Mac.

How

  1. Hook policy module. hook_policy.py reads each tool's settings, merges managed-settings drop-ins the way Claude Code does and resolves the hook program. It nulls only the keys that an unreadable file decides.
  2. Heartbeat budget. ai_tools_discovery.py sends heartbeats on their own send budget. A failed heartbeat never uses the final report's budget and never goes to Sentry.

Testing

  • python3 -m pytest tests/: 3,805 passed, 30 skipped, 0 failed
  • install.ps1 on a Windows Server 2022 Azure VM as SYSTEM: hook policy mdm with our hook, a skipped scan while another scan ran, and a heartbeat at 120 seconds
  • Not tested: a real macOS VM, because access is still pending

Proof

  • 3805 passed, 30 skipped, 610 subtests passed in 237.19s on this branch, rebased onto staging.

Risk and rollout

  • Deploy after backend. Merge after websentry-ai/ai-gateway-data#3575. The current backend rejects a skipped scan with 400 and drops hook_policy.
  • Old fields stay. The report keeps every existing field, so an old backend still accepts completed scans.

Unbound Review · Score 4/5 (P2s only) · f372e1e

  • codex (own score 4/5): The PR adds skipped-scan reports, periodic scan heartbeats, and per-tool hook policy reporting. The production changes reviewed did not reveal a verified defect, but the new tests violate repository testing rules.
  • claude (own score 4/5): The PR makes three changes. A run turned away by the lock now sends a bounded skipped scan event; on Linux it does this without creating a device id. A daemon thread re-sends in_progress every 120 s and is stopped before the completed, failed or abort report. A new hook_policy object, built from the Claude Code, Codex and VS Code settings files, is added to each tool's report. I traced the heartbeat's start and stop points in main(), how it

findings: 0 P0 · 0 P1 · 2 P2 (top 5 P0/P1 inline) · engines: codex ✅ · claude ✅

Findings not posted inline
  • P2 tests/test_scan_outcomes.py:291 Use localhost servers for transport tests — The heartbeat failure test mocks the subprocess used to invoke curl instead of exercising a real local HTTP server. (codex)
  • P2 tests/test_scan_outcomes.py:206 Mock sleeps in the new tests — The new heartbeat tests use real sleeps, including repeated waits for background activity. (codex)

rules applied: CLAUDE.md

@iyadzargar

Copy link
Copy Markdown
Contributor Author

/review

@vigneshsubbiah16 vigneshsubbiah16 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛡️ Automated Security Review (consensus)

1 finding — 0 high-confidence, 1 to triage. Reviewers: Claude, Semgrep, Gitleaks.

  • [LOW] has_our_hook can be faked from files the user controls 🟡 — scripts/coding_discovery_tools/hook_policy.py:262
    • What: _runs_our_hook counts ~/.claude/hooks/unbound.py as a hook target whenever any file exists at that path. The top-ranked "console" source is also read from ~/.claude/remote-settings.json, which is in the user's own home folder. So a non-admin user can produce overriding_policy: "console" with overriding_policy_has_our_hook: true and hooks_disabled: false.
    • Why: The PR exists to catch broken or missing hooks. A user who swaps in a no-op unbound.py and writes a cached console policy that points at it gets reported as protected. The backend would then show a working Unbound hook on a device where none really runs. ⚠️ Whether Claude Code itself trusts a hand-edited remote-settings.json can't be seen from this diff. The script-swap case holds either way.
    • Fix: Report where the evidence came from (for example source_writable_by_user: true), so the backend can weigh user-writable sources lower. Also consider requiring the per-user unbound.py to be owned by root or to match a known hash before it counts. hooks_extractor.root_owned is already imported elsewhere in this PR.
    • Flagged by: Claude

Pre-existing (not introduced by this PR)

  • Semgrep python-logger-credential-disclosure at utils.py:2348 and utils.py:2357. This PR does not touch these lines. They log the credentials file path and the parse error, not the credential value. Not raised as a finding.

🤖 consensus review · reviewers: Claude, Semgrep, Gitleaks · head 9c2eca63 · 2026-11-26T11:38Z

…fake the os only in hook_policy tests [WEB-6392]
@iyadzargar

Copy link
Copy Markdown
Contributor Author

/review code

@vigneshsubbiah16 vigneshsubbiah16 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Security consensus: no issues found. (reviewers: claude, semgrep, gitleaks)


🤖 consensus review · reviewers: Claude, Semgrep, Gitleaks · head 85dc0e09 · 2026-11-26T12:05Z

…at the scan ended, fix windows test fakes [WEB-6392]
@iyadzargar

Copy link
Copy Markdown
Contributor Author

/review code

@vigneshsubbiah16 vigneshsubbiah16 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛡️ Automated Security Review (consensus)

1 finding — 0 high-confidence, 1 to triage. Reviewers: claude, semgrep, gitleaks.

  • [LOW] "Console" hook policy is read from a file the user can write 🟡 — scripts/coding_discovery_tools/hook_policy.py:106
    • What: _claude_code_policy ranks ~/.claude/remote-settings.json as the top admin source ("console"). It reads that file from the user's own home folder. The values it produces (overriding_policy, overriding_policy_has_our_hook, merge_sources_enabled, hooks_disabled) are then reported as admin-policy facts.
    • Why: A local user without admin rights can write or edit that cache. They could make the backend show overriding_policy: "console" with has_our_hook: true, which hides the broken-hook case this PR is meant to surface. They could also invent a console override. The MDM sources (/Library/Managed Preferences, HKLM) and the managed-settings directory are owned by root or admin, so they don't have this problem. ⚠️ How much this matters depends on whether Claude Code itself trusts a cache the user edited. That can't be seen from this diff.
    • Fix: Mark values that come from the user-writable cache as user-controlled. One option is a separate label such as "console_cache" plus a flag saying who owns the file. Another is to check that the file is not writable by the user before treating it as authoritative. In the backend, don't treat has_our_hook: true from this source as proof that the hook is enforced.
    • Flagged by: claude

Pre-existing (not introduced by this PR)

  • semgrep python-logger-credential-disclosure at scripts/coding_discovery_tools/utils.py:2348 and :2357: this PR does not touch these lines. The log calls record a file path and an exception message, not the contents of the credential, so this is very likely a false positive.

🤖 consensus review · reviewers: Claude, Semgrep, Gitleaks · head 57ef787f · 2026-11-26T12:23Z

@iyadzargar

Copy link
Copy Markdown
Contributor Author

/review code

@vigneshsubbiah16 vigneshsubbiah16 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Security consensus: no issues found. (reviewers: Claude (lead), Semgrep, Gitleaks)


🤖 consensus review · reviewers: Claude, Semgrep, Gitleaks · head 4d805439 · 2026-11-26T12:42Z

@iyadzargar

Copy link
Copy Markdown
Contributor Author

/review code

@iyadzargar

Copy link
Copy Markdown
Contributor Author

/review code

@vigneshsubbiah16 vigneshsubbiah16 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Security consensus: no issues found. (reviewers: claude, semgrep, gitleaks)


🤖 consensus review · reviewers: Claude, Semgrep, Gitleaks · head 0d4688f5 · 2026-11-26T15:43Z

@vigneshsubbiah16 vigneshsubbiah16 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Security consensus: no issues found. (reviewers: claude, semgrep, gitleaks)


🤖 consensus review · reviewers: Claude, Semgrep, Gitleaks · head 9e9cafb9 · 2026-11-26T16:03Z

@iyadzargar

Copy link
Copy Markdown
Contributor Author

/review code

@iyadzargar

Copy link
Copy Markdown
Contributor Author

/review code

@vigneshsubbiah16 vigneshsubbiah16 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Security consensus: no issues found. (reviewers: claude, semgrep, gitleaks)


🤖 consensus review · reviewers: Claude, Semgrep, Gitleaks · head 381cab56 · 2026-11-26T16:34Z

@iyadzargar

Copy link
Copy Markdown
Contributor Author

/review code

@iyadzargar

Copy link
Copy Markdown
Contributor Author

/review code

@vigneshsubbiah16 vigneshsubbiah16 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛡️ Automated Security Review (consensus)

1 finding: 0 high-confidence, 1 to triage. Reviewers: Claude, Semgrep, Gitleaks.

  • [MEDIUM] Quadratic-time TOML fallback parser lets a local user stall the root-run scan 🟡 — scripts/coding_discovery_tools/hook_policy.py:428
    • What: On Python versions before 3.11, _multiline_string_left_open counts backslashes with line[:end] and line[:end].rstrip("\\") on every escaped """ it finds. Each of those calls copies the line up to that point, so one long line full of escaped """ takes quadratic time.
    • Why: ~/.codex/config.toml is user-writable, and discovery reads it as root through _toml_hooks_feature_by_line. That parser is the main path on macOS, where the system python3 is 3.9. The 2 MB size cap still allows one line of about 500k \""" repeats, which works out to roughly 10^11–10^12 byte copies. That blocks the per-user loop for hours. Meanwhile the new 120-second in_progress heartbeat keeps the scan looking alive. So any user can blind device-wide discovery on every run until the run's self-timeout.
      • ⚠️ TestHostileFiles has a one-second limit, but it is cut off in this diff. I could not check whether it covers this input.
    • Fix:
      • Count the backslashes by walking back from end (j = end - 1; while j >= 0 and line[j] == "\\": j -= 1) rather than slicing the line.
      • Also cap the line length or the total parse work in the line parser, and return _UNREADABLE when the cap is hit.
      • Add a hostile-file test with 'x = """' + '\\"""' * 500_000 on the line parser.
    • Flagged by: Claude

Previously acknowledged (not re-flagged)

  • Telemetry is read from lower-priority managed sources (hook_policy.py:183). The thread was opened by @vigneshsubbiah16 [MEMBER]. Only the PR author answered, and that reply is not a maintainer decision. I am still not re-flagging it, because the reply points to Claude Code's documented per-variable merge of env across admin sources. I could not see the docs it cites. Either way, this is about report accuracy, not a security issue.

🤖 consensus review · reviewers: Claude, Semgrep, Gitleaks · head 11262028 · 2026-11-26T16:52Z

@iyadzargar

Copy link
Copy Markdown
Contributor Author

/review code

@vigneshsubbiah16 vigneshsubbiah16 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛡️ Automated Security Review (consensus)

1 finding: 0 high-confidence, 1 to triage. Reviewers: claude, semgrep, gitleaks.

  • [LOW] Quadratic-time TOML fallback parser lets a user stall the scan 🟡 — scripts/coding_discovery_tools/hook_policy.py:470
    • What: The pre-3.11 line parser can take quadratic time on one long line. On every """ found inside an open multi-line string, _multiline_string_left_open copies the line up to that point (line[:end]) and strips it. A line full of \""" sequences makes the parser do this copy once per sequence.
    • Why: The input is ~/.codex/config.toml, which the user controls. It is read with follow_symlinks=True and capped at _MAX_POLICY_SIZE (2 MB). On Python 3.9/3.10 this fallback is the main path (macOS system python3 is 3.9). A single 2 MB line holds hundreds of thousands of \""", and each one triggers a copy of up to 2 MB. That pins the CPU until the run's self-timeout fires. The parser runs inside the per-tool loop, so the run is marked failed. On a multi-user device, reports for other users and tools after that point are never sent. One unprivileged user can therefore keep blocking discovery reports for the whole device, even when discovery runs as root.
    • Fix: Count the backslashes before end by walking backward from end - 1, without slicing. Also skip or reject any line longer than a sane limit (for example 64 KB) before scanning it, and treat it as _UNREADABLE.
    • Flagged by: claude

Pre-existing (not introduced by this PR)

  • The two Semgrep python-logger-credential-disclosure hits at scripts/coding_discovery_tools/utils.py:2349 and :2358 are on lines this diff does not touch. They log a file path and a parse error, not the credential value. Not raised as PR findings.

🤖 consensus review · reviewers: Claude, Semgrep, Gitleaks · head 83f43d60 · 2026-11-26T17:13Z

@iyadzargar

Copy link
Copy Markdown
Contributor Author

/review code

@vigneshsubbiah16 vigneshsubbiah16 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Security consensus: no issues found. (reviewers: claude, semgrep, gitleaks)


🤖 consensus review · reviewers: Claude, Semgrep, Gitleaks · head 164cc46c · 2026-11-26T19:51Z

@iyadzargar

Copy link
Copy Markdown
Contributor Author

/review

@vigneshsubbiah16 vigneshsubbiah16 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Security consensus: no issues found. (reviewers: Claude, Semgrep, Gitleaks)


🤖 consensus review · reviewers: Claude, Semgrep, Gitleaks · head f372e1e0 · 2026-11-27T17:19Z

@vigneshsubbiah16 vigneshsubbiah16 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Security consensus: no issues found. (reviewers: Claude (lead), Semgrep, Gitleaks)


🤖 consensus review · reviewers: Claude, Semgrep, Gitleaks · head ada160ef · 2026-11-30T11:12Z

@iyadzargar
iyadzargar merged commit befbfcd into staging Nov 30, 2026
10 checks passed
anonpran added a commit that referenced this pull request Nov 30, 2026
* feat(discovery): report skipped scans and hook policy [WEB-6392] (#440)

* Group discovery errors in GlitchTip by code path, not scan folder (WEB-6914) (#449)

* Group discovery errors in GlitchTip by code path, not scan folder (WEB-6914)

The GlitchTip copy gets a fingerprint of error type, agent frames and HTTP code, so the per-scan temp folder no longer opens a new issue each scan. The Sentry copy is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Split GlitchTip network-failure issues by curl exit code (WEB-6914)

DNS, refused and TLS failures each get their own GlitchTip issue instead of one shared one.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Iyad Zargar <dev@iyadzargar.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants