fix(hooks): prevent false positives on quoted strings in block-env-fi… - #618
fix(hooks): prevent false positives on quoted strings in block-env-fi…#618AVPthegreat wants to merge 1 commit into
Conversation
…les and protect-env-vars (FailproofAI#49)
|
Warning Review limit reached
Next review available in: 47 minutes Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available. How can I continue?After more reviews become available, a review can be triggered using the To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews. How do review limits work?CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability. For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window. Please refer docs for additional details. Review details⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (2)
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. Comment |
|
Your PR is awaiting review by a reviewer. Till then you can join the Discord for conversation: https://discord.befailproof.ai |
There was a problem hiding this comment.
Pull request overview
This PR updates the environment-related builtin policies to reduce false-positive blocks caused by .env / env mentions inside quoted string literals (e.g., git commit messages or PR bodies), and adds a unit test covering one of those cases.
Changes:
- Added a
stripQuotedStrings()helper to remove"..."/'...'string literals before applying selected command regex checks. - Updated
protect-env-varsandblock-env-filesto run their regex detection against a “quote-stripped” version of the command. - Added a unit test ensuring a
git commit -m "… .env …"command is allowed.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
src/hooks/builtin-policies.ts |
Adds quote-stripping and applies it to env-related command checks to avoid false positives from quoted messages. |
__tests__/hooks/builtin-policies.test.ts |
Adds a unit test for allowing .env mentions in quoted git commit -m messages. |
Comments suppressed due to low confidence (1)
src/hooks/builtin-policies.ts:546
block-env-filescan now be bypassed by quoting the file path (e.g.cat ".env"orcp ".env" /tmp/x). BecausestripQuotedStrings()runs unconditionally, the.envreference disappears fromunquotedCmdand the policy allows the command.
if (ctx.toolName === "Bash") {
const unquotedCmd = stripQuotedStrings(cmd);
if (ENV_CMD_RE.test(unquotedCmd)) {
return deny("Command references .env file");
}
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| const cmd = getCommand(ctx); | ||
| const unquotedCmd = stripQuotedStrings(cmd); | ||
| // Block: env, printenv, echo $VAR, export VAR= | ||
| if (ENV_PRINTENV_RE.test(cmd)) { | ||
| if (ENV_PRINTENV_RE.test(unquotedCmd)) { | ||
| return deny("Command reads environment variables"); |
|
|
||
| it("allows git commit with .env mentioned in quoted message", async () => { | ||
| const ctx = makeCtx({ toolName: "Bash", toolInput: { command: 'git commit -m "update .env.example documentation"' } }); | ||
| expect((await policy.fn(ctx)).decision).toBe("allow"); | ||
| }); |
Summary
Fixes #49 by stripping quoted string literals (
"..."and'...') before checking command regexes inblock-env-filesandprotect-env-vars, eliminating false positives on commit messages, comments, and instructions containing.envorenv.❌ Root Cause Analysis
In
src/hooks/builtin-policies.ts,block-env-filesandprotect-env-varsused raw string regexes (ENV_CMD_REandENV_PRINTENV_RE) against the full command string:Why it failed:
git commit -m "update .env.example documentation"orgit commit -m "add env variable configuration"):.envorenvinside the string literal argument"update .env.example...".git commitorechocommand.✅ Fix Details
Added
stripQuotedStringshelper to strip double-quoted ("...") and single-quoted ('...') string argument values before evaluatingblock-env-filesandprotect-env-varscommand regexes:Updated
blockEnvFilesandprotectEnvVarsinsrc/hooks/builtin-policies.ts:🧪 Unit Tests & Verification
Added test case to
__tests__/hooks/builtin-policies.test.ts:Test Results:
vitest run __tests__/hooks/builtin-policies.test.ts✓ 470 passed (470)