Skip to content

CLI-246 Move prompt submit hook from shell scripts#158

Open
kirill-knize-sonarsource wants to merge 1 commit intotask/kk/CLI-244-245-callback-infrastructurefrom
task/kk/CLI-246-prompt-submit
Open

CLI-246 Move prompt submit hook from shell scripts#158
kirill-knize-sonarsource wants to merge 1 commit intotask/kk/CLI-244-245-callback-infrastructurefrom
task/kk/CLI-246-prompt-submit

Conversation

@kirill-knize-sonarsource
Copy link
Copy Markdown
Member

No description provided.

@kirill-knize-sonarsource kirill-knize-sonarsource changed the base branch from master to task/kk/CLI-244-245-callback-infrastructure April 8, 2026 17:58
@sonar-review-alpha
Copy link
Copy Markdown
Contributor

sonar-review-alpha bot commented Apr 8, 2026

Summary

What changed:
This PR implements a TypeScript-based handler for the UserPromptSubmit hook callback that scans prompts for secrets before they're sent to Claude. Previously this logic lived in shell scripts; it's now part of the CLI's callback infrastructure.

How it works:

  • The hook reads a JSON payload from stdin containing the user's prompt
  • Passes the prompt text to the sonar-secrets binary via stdin (new --input mode)
  • If secrets are detected, outputs a JSON decision to block the submission
  • Gracefully allows submission if unauthenticated, binary not installed, stdin invalid, or scan fails

Why:
Consolidates security scanning logic into the main CLI codebase rather than distributed shell scripts, making it testable, maintainable, and consistent across platforms.

What reviewers should know

Where to start:

  1. src/cli/commands/hook/agent-prompt-submit.ts — the main handler; read top-to-bottom for the flow
  2. src/lib/process.ts — additions to SpawnOptions and spawnProcess to support stdin piping (lines 31, 59, 75-77)
  3. src/cli/commands/analyze/secrets.ts — the new runSecretsBinaryOnText() helper that wraps the binary
  4. src/cli/command-tree.ts — how it wires into the claude-prompt-submit hook command

Non-obvious decisions:

  • Graceful degradation is intentional — if anything goes wrong (missing binary, no auth, spawn error), the prompt is allowed through rather than blocking. This prevents the hook from becoming a denial-of-service vector.
  • The stdinData field in SpawnOptions (and the write in spawnProcess) enables piping prompt text to the binary instead of using a tempfile or args — cleaner and more secure.
  • readStdinJson() silently returns early if parsing fails — this allows the hook to run with various payloads without throwing.

Testing:

  • Integration tests exercise the real binary with real prompts (including a test secret token)
  • Unit tests cover error paths (scan throws, exitCode null) that can't happen with the real binary
  • Tests verify both "secrets found" (block) and "no secrets" (allow) paths

  • Generate Walkthrough
  • Generate Diagram

🗣️ Give feedback

@hashicorp-vault-sonar-prod
Copy link
Copy Markdown

hashicorp-vault-sonar-prod bot commented Apr 8, 2026

CLI-246

sonar-review-alpha[bot]

This comment was marked as resolved.

@kirill-knize-sonarsource kirill-knize-sonarsource force-pushed the task/kk/CLI-244-245-callback-infrastructure branch 2 times, most recently from e5bc9f8 to 6f2f053 Compare April 8, 2026 19:17
@kirill-knize-sonarsource kirill-knize-sonarsource force-pushed the task/kk/CLI-246-prompt-submit branch from a3f18c4 to 5f71102 Compare April 8, 2026 19:34
sonar-review-alpha[bot]

This comment was marked as outdated.

@kirill-knize-sonarsource kirill-knize-sonarsource force-pushed the task/kk/CLI-244-245-callback-infrastructure branch from 6f2f053 to aa6bc64 Compare April 8, 2026 19:58
@kirill-knize-sonarsource kirill-knize-sonarsource force-pushed the task/kk/CLI-246-prompt-submit branch from 5f71102 to 143d5f1 Compare April 8, 2026 19:58
sonar-review-alpha[bot]

This comment was marked as outdated.

@kirill-knize-sonarsource kirill-knize-sonarsource force-pushed the task/kk/CLI-246-prompt-submit branch from 143d5f1 to 31cb0f1 Compare April 8, 2026 20:18
@kirill-knize-sonarsource kirill-knize-sonarsource force-pushed the task/kk/CLI-244-245-callback-infrastructure branch from aa6bc64 to 3a31a77 Compare April 8, 2026 20:30
Copy link
Copy Markdown
Contributor

@sonar-review-alpha sonar-review-alpha bot left a comment

Choose a reason for hiding this comment

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

Conclusion: Solid implementation. The refactoring of secrets-scan.ts is clean, the graceful-failure approach is consistent with the existing claudePreToolUse handler, and test coverage is thorough. One logic duplication worth addressing before CLI-247 lands.

🗣️ Give feedback

Comment on lines +40 to +47

const prompt = payload.prompt;
if (!prompt) return;

const auth = await resolveAuth().catch(() => null);
if (!auth) return; // not authenticated — allow gracefully

const binaryPath = resolveSecretsBinaryPath();
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.

Logic duplication: The auth/binary guard block here (lines 40–47) is structurally identical to the same block in claude-pre-tool-use.ts lines 48–52, and CLI-247 will add a third handler with the same pattern.

If the guard semantics ever change (e.g. a new auth type treated as unauthenticated, or a new binary resolution strategy), all handlers must be updated in sync with no compiler help.

Consider extracting a shared helper before CLI-247 adds a third copy:

async function resolveCallbackDeps(): Promise<{ auth: ResolvedAuth; binaryPath: string } | null>

Each handler would call it once and return early on null, keeping handler-specific logic separated from the shared guard.

  • Mark as noise

@kirill-knize-sonarsource kirill-knize-sonarsource force-pushed the task/kk/CLI-246-prompt-submit branch from 31cb0f1 to 140c6be Compare April 8, 2026 20:32
sonar-review-alpha[bot]

This comment was marked as outdated.

@kirill-knize-sonarsource kirill-knize-sonarsource force-pushed the task/kk/CLI-244-245-callback-infrastructure branch from 3a31a77 to c4b4665 Compare April 8, 2026 21:01
@kirill-knize-sonarsource kirill-knize-sonarsource force-pushed the task/kk/CLI-246-prompt-submit branch from 140c6be to 020d2e9 Compare April 8, 2026 21:02
sonar-review-alpha[bot]

This comment was marked as outdated.

@kirill-knize-sonarsource kirill-knize-sonarsource force-pushed the task/kk/CLI-244-245-callback-infrastructure branch 2 times, most recently from e44c9d3 to d117a3a Compare April 8, 2026 23:34
@kirill-knize-sonarsource kirill-knize-sonarsource force-pushed the task/kk/CLI-246-prompt-submit branch from 020d2e9 to 7460ff8 Compare April 8, 2026 23:52
sonar-review-alpha[bot]

This comment was marked as outdated.

@kirill-knize-sonarsource kirill-knize-sonarsource force-pushed the task/kk/CLI-246-prompt-submit branch from 7460ff8 to 6f7f1ea Compare April 9, 2026 00:08
sonar-review-alpha[bot]

This comment was marked as outdated.

@kirill-knize-sonarsource kirill-knize-sonarsource force-pushed the task/kk/CLI-244-245-callback-infrastructure branch 5 times, most recently from b83cb4f to 8617798 Compare April 9, 2026 11:05
@kirill-knize-sonarsource kirill-knize-sonarsource force-pushed the task/kk/CLI-244-245-callback-infrastructure branch 4 times, most recently from f4cbc21 to 468e45c Compare April 9, 2026 14:33
@kirill-knize-sonarsource kirill-knize-sonarsource force-pushed the task/kk/CLI-246-prompt-submit branch from 6f7f1ea to ebd83a5 Compare April 9, 2026 14:47
sonar-review-alpha[bot]

This comment was marked as outdated.

@kirill-knize-sonarsource kirill-knize-sonarsource force-pushed the task/kk/CLI-244-245-callback-infrastructure branch 2 times, most recently from 00173a3 to 9191141 Compare April 10, 2026 12:53
@kirill-knize-sonarsource kirill-knize-sonarsource force-pushed the task/kk/CLI-246-prompt-submit branch 2 times, most recently from 6fb3822 to db836c4 Compare April 10, 2026 13:05
sonar-review-alpha[bot]

This comment was marked as outdated.

@kirill-knize-sonarsource kirill-knize-sonarsource force-pushed the task/kk/CLI-246-prompt-submit branch from db836c4 to 56c1321 Compare April 10, 2026 13:25
sonar-review-alpha[bot]

This comment was marked as outdated.

@kirill-knize-sonarsource kirill-knize-sonarsource force-pushed the task/kk/CLI-246-prompt-submit branch from 56c1321 to cb918b8 Compare April 10, 2026 13:43
sonar-review-alpha[bot]

This comment was marked as outdated.

@kirill-knize-sonarsource kirill-knize-sonarsource force-pushed the task/kk/CLI-246-prompt-submit branch from cb918b8 to 356de02 Compare April 10, 2026 13:58
sonar-review-alpha[bot]

This comment was marked as outdated.

@kirill-knize-sonarsource kirill-knize-sonarsource force-pushed the task/kk/CLI-244-245-callback-infrastructure branch from 9191141 to 129cc50 Compare April 13, 2026 09:34
@kirill-knize-sonarsource kirill-knize-sonarsource force-pushed the task/kk/CLI-246-prompt-submit branch from 356de02 to 1c1f6fd Compare April 13, 2026 11:12
sonar-review-alpha[bot]

This comment was marked as outdated.

@kirill-knize-sonarsource kirill-knize-sonarsource force-pushed the task/kk/CLI-246-prompt-submit branch from 1c1f6fd to e9aa81f Compare April 13, 2026 11:28
@sonarqubecloud
Copy link
Copy Markdown

Copy link
Copy Markdown
Contributor

@sonar-review-alpha sonar-review-alpha bot left a comment

Choose a reason for hiding this comment

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

LGTM! ✅

The previously flagged logic duplication in the auth/binary guard block remains unresolved — no changes were made to address it in this PR.

🗣️ Give feedback

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.

1 participant