-
Notifications
You must be signed in to change notification settings - Fork 11
fix: properly type-check Bash tool input without type assertion #28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
- Remove unused BashToolInput import - Use inline type assertion with property check to avoid "as unknown as" pattern - Ensures type safety while maintaining readability 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
WalkthroughThe code updates simplify the type casting for the Changes
Possibly related PRs
Suggested labels
Poem
✨ Finishing Touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
🧹 Nitpick comments (1)
templates/hooks/index.ts (1)
31-34: Substring check is trivially bypassed—tighten the dangerous-command filterAttackers can slip through with minor variations (
rm -rf /,rm -rf / --no-preserve-root, path globs, etc.). A simple regex closes obvious holes:- if (command.includes('rm -rf /') || command.includes('rm -rf ~')) { + const dangerous = /\brm\s+-rf\s+([/~]|--no-preserve-root\b|\*)/; + if (dangerous.test(command)) {Consider expanding the deny-list or integrating a proper shell-parser if security is critical.
[security]
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
templates/hooks/index.ts(1 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
`templates/**/*`: Hook templates must be stored in the 'templates/' directory and are copied to the user's '.claude/' directory during initialization.
templates/**/*: Hook templates must be stored in the 'templates/' directory and are copied to the user's '.claude/' directory during initialization.
📄 Source: CodeRabbit Inference Engine (CLAUDE.md)
List of files the instruction was applied to:
templates/hooks/index.ts
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (6)
- GitHub Check: test (windows-latest, 20)
- GitHub Check: test (windows-latest, 18)
- GitHub Check: test (ubuntu-latest, 18)
- GitHub Check: test (macos-latest, 18)
- GitHub Check: test (macos-latest, 20)
- GitHub Check: test (ubuntu-latest, 20)
| if (payload.tool_name === 'Bash' && payload.tool_input && 'command' in payload.tool_input) { | ||
| const bashInput = payload.tool_input as BashToolInput | ||
| const command = bashInput.command | ||
| const command = (payload.tool_input as {command: string}).command | ||
| console.log(`🚀 Running command: ${command}`) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Prefer a type-guard to eliminate the unsafe cast
The direct assertion (payload.tool_input as {command: string}).command silently trusts the payload’s shape. A tiny, locally-scoped type-guard keeps the compiler on your side and avoids re-introducing the deprecated double-assertion pattern:
-type BashToolInput = {command: string}
-function isBashToolInput(input: unknown): input is BashToolInput {
- return !!input && typeof (input as any).command === 'string'
-}
-
-if (payload.tool_name === 'Bash' && isBashToolInput(payload.tool_input)) {
- const {command} = payload.tool_input
+type BashToolInput = {command: string}
+function isBashToolInput(input: unknown): input is BashToolInput {
+ return !!input && typeof (input as any).command === 'string'
+}
+
+if (payload.tool_name === 'Bash' && isBashToolInput(payload.tool_input)) {
+ const {command} = payload.tool_input
console.log(`🚀 Running command: ${command}`)This keeps the file self-contained (no additional import) while giving the compiler a real guard instead of a blind cast.
Committable suggestion skipped: line range outside the PR's diff.
🤖 Prompt for AI Agents
In templates/hooks/index.ts around lines 26 to 28, replace the direct type
assertion of payload.tool_input with a locally defined type-guard function that
checks if tool_input has a command property of type string. Use this type-guard
to safely access the command property without casting, ensuring type safety and
avoiding deprecated double-assertion patterns.
|
🎉 This PR is included in version 1.2.3 🎉 The release is available on: Your semantic-release bot 📦🚀 |
Summary
as unknown astype assertion with inline type check using'command' in payload.tool_inputas {command: string}for cleaner, more readable codeTest plan
bun run lintto ensure no linting issues🤖 Generated with Claude Code
Summary by CodeRabbit