Skip to content

feat: check redirect targets against edit rules and external_directory - #14

Merged
preved911 merged 1 commit into
mainfrom
fix/redirect-check
Jul 29, 2026
Merged

feat: check redirect targets against edit rules and external_directory#14
preved911 merged 1 commit into
mainfrom
fix/redirect-check

Conversation

@preved911

Copy link
Copy Markdown
Owner

Redirects (2>&1, >/dev/null, > file, etc.) were invisible to the plugin because unbash stores them in cmd.redirects, not cmd.suffix. The reconstructed command text silently dropped redirects, bypassing all security checks.

Changes

  • src/chain.ts: Added RedirectInfo interface and redirects field to ChainSegment. Redirect operators and targets are now included in getCommandText. Statement-level redirects ({ echo hi; } > file) are captured too.
  • src/config.ts: Added editRules to PluginConfig, parsed from permission.edit.
  • src/enforce.ts: New resolveRedirectTargets() function:
    • Well-known redirects (FD redirects like 2>&1, /dev/null, heredocs) → always allowed
    • File redirects inside cwd → checked against editRules only
    • File redirects outside cwd → checked against editRules and external_directory
    • Most-restrictive-wins across bash permission, external directory, and redirect checks

Tests

7 new redirect enforcement tests + 6 new chain parsing tests. All 66 tests pass.

@github-actions

Copy link
Copy Markdown

👀 AI Code Review

The PR adds the ability to capture and check redirect targets against edit rules and external directories, improving command safety by including previously ignored redirects. It introduces a new interface for redirect information and various rules for handling file and well-known redirects.


Powered by GPT-4o via GitHub Models

Redirects (2>&1, >/dev/null, > file, etc.) were invisible to the plugin
because unbash stores them in cmd.redirects, not cmd.suffix. This meant
the reconstructed command text silently dropped redirects, bypassing all
security checks.

- chain.ts: include redirects in ChainSegment with wellKnown flag
- config.ts: add permission.edit rule parsing
- enforce.ts: check non-well-known redirects against edit rules;
  if target is outside cwd, also check external_directory
@preved911
preved911 force-pushed the fix/redirect-check branch from 8f05d68 to 85a1939 Compare July 29, 2026 21:36
@github-actions

Copy link
Copy Markdown

👀 AI Code Review

The PR implements redirect checks against edit rules and adds support for file and file descriptor redirects. All tests pass, and the structure appears well-considered for security enforcement.


Powered by GPT-4o via GitHub Models

Comment thread src/chain.ts
function isWellKnownRedirect(redir: Redirect): boolean {
const target = redir.target?.text ?? redir.content ?? "";
if (target === "/dev/null") return true;
if (/^\d+$/.test(target)) return true;

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

What is matching for this rule?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Matches targets that are purely numeric — like the 1 in 2>&1. The idea is that FD redirect targets (e.g. 1, 2) are always single/all digits.

However, you are right to flag this — > 12345 (redirect to a file literally named 12345) would also match. I can tighten the check to only mark it well-known when fileDescriptor is explicitly set AND the target is all digits, which eliminates the ambiguity. Want me to make that change?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Is it cover redirects like &>?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Yes — the well-known check looks at the target, not the operator. \u0026>/dev/null has target /dev/null → well-known. \u0026> output.txt has target output.txt → checked against edit rules. So > redirects are fully covered.

@preved911
preved911 merged commit 5951484 into main Jul 29, 2026
2 checks passed
preved911 added a commit that referenced this pull request Sep 11, 2026
Includes redirect-target checks against edit rules (#14) and complex-chain
rejection with readability instruction (#15).
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