Skip to content

fix(policy): split shell commands on newlines and read quoted URLs (#116) - #442

Merged
codeyevsky merged 2 commits into
solongate:mainfrom
leninworld:fix-policy-newline-quoted-url-issue-116
Nov 23, 2026
Merged

codeyevsky merged 2 commits into
solongate:mainfrom
leninworld:fix-policy-newline-quoted-url-issue-116

Conversation

@leninworld

Copy link
Copy Markdown
Contributor

This PR fixes issue #116, points 1 and 2. Point 3 (code in commandFields) is already on main.

Before, with a deny rule commandConstraints.denied: ["rm *"] ("enabled": true, permission EXECUTE), both the OPA evaluator and FallbackEvaluate blocked echo hi; rm -rf /tmp/x but allowed:

  • echo hi\nrm -rf /tmp/x: ExtractCommands returned one command, "echo hi\nrm -rf /tmp/x".
  • X=rm\n$X -rf /tmp/x: the assignment was never recognised, so $X stayed unresolved.

With urlConstraints.denied: ["*evil.example*"], curl https://evil.example/x was blocked, but curl 'https://evil.example/x' and curl "https://evil.example/x" were allowed, because ExtractURLs returned nothing. HTTPPrefix is anchored, and the token still had its quotes.

Change (packages/policy/extract_commands.go):

  • NormalizeShellCommand turns each newline that is outside quotes into a statement break before the existing ;/&&/|| split. A backslash-newline continuation joins the two lines, as sh does (r\<newline>m is rm). Inside single quotes it stays literal. ExtractCommands and ExtractPipelines split its output, so both see the separate statements, and assignments carry across lines as they already do across ;.
  • A newline inside single or double quotes is not a break. Otherwise a multi-line git commit -m "title\n\nbody" would become one "command" per body line. In whitelist mode that would block it under an ALLOW rule like git *.
  • A # comment runs to the end of its line. An apostrophe or a trailing backslash inside it doesn't open a quote or continue the line, so # don't touch\nrm -rf /tmp/x exposes the rm, and so does a comment line ending in a backslash (# note \) with rm -rf /tmp/x on the next line. bash -c runs the rm in both cases.
  • Empty statements from blank lines are dropped.
  • ExtractURLs strips matched quote pairs (the existing dequote) from each token and from a whole value before matching. That includes a quoted token followed directly by ;, & or |, as in curl 'https://evil.example/x'; echo ok.

Heredoc bodies sit outside quotes, so their lines are now checked as statements too. For a heredoc that is only data (cat > f <<EOF) this can match a deny rule it didn't before. I accepted that false positive on purpose, because the guard can't tell data from a heredoc fed to bash, which runs it.

Tests: I added rows to the existing tables in extract_commands_test.go (TestNormalizeShellCommandStatementsAndQuotes, TestExtractCommandsSplitsAndNormalizes, TestExtractURLsFromToolArgs). Every new row fails on main except two that pass on both and guard against regressions: "newline inside double quotes" (the commit-message case above) and "line continuation inside single quotes is literal".

Tested with Go 1.26.11 on macOS (arm64):

  • packages/policy: gofmt -l . is empty, and go vet ./... and go test -count=1 ./... pass.
  • packages/guard: go vet ./... and go test -count=1 ./... pass.
  • ./scripts/sg-parity-check.sh 10/10 and ./scripts/sg-layers-check.sh 25/25.
  • A throwaway test (not committed) ran EvaluatePolicy and FallbackEvaluate on the inputs above. On main both allow them; with this change both block them.

sg-unit-check.sh stops at its gofmt step on main, because some guard files end without a newline. I didn't try it against a live agent client.

Fixes #116

…olongate#116)

A newline outside quotes now ends a statement, so a multi-line command
can no longer hide a statement from commandConstraints, and assignments
carry across lines as they do across ';'. Newlines inside quotes, and
comments, keep their shell meaning; a backslash-newline joins lines
except inside single quotes.

ExtractURLs strips matched quotes from a token (also when a shell
operator follows it), so a quoted URL meets urlConstraints.

Fixes solongate#116

Signed-off-by: Lenin Mookiah <2639584+leninworld@users.noreply.github.com>
@codacy-production

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 31 complexity · 0 duplication

Metric Results
Complexity 31
Duplication 0

View in Codacy

AI Reviewer: first review requested successfully. AI can make mistakes. Always validate suggestions.

Run reviewer

TIP This summary will be updated as you push new changes.

@codacy-production codacy-production Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull Request Overview

⚠️ Resolve issues before merging

Several shell-parsing edge cases can cause executable commands to be missed, weakening policy enforcement. In particular, heredoc contents and comments can corrupt quote state or assignment resolution, and operators adjacent to quoted URLs prevent URL extraction.

Additional automated coverage is missing for double-quoted URLs adjacent to operators, CRLF handling, and pipeline behavior after normalization.

Test suggestions

  • Split commands on ordinary, blank, and trailing newlines
  • Resolve variable assignments across newline-separated statements
  • Join backslash-newline continuations outside quotes
  • Preserve backslash-newline inside single quotes
  • Preserve newlines inside double-quoted arguments
  • Ignore apostrophes and trailing backslashes in comments when finding later commands
  • Extract whole quoted URLs and quoted URLs followed by shell operators
  • Extract double-quoted URLs followed by shell operators
  • Handle CRLF newline and continuation forms
  • Verify pipeline extraction behavior after newline normalization
Prompt proposal for missing tests
Consider implementing these tests if applicable:
1. Extract double-quoted URLs followed by shell operators
2. Handle CRLF newline and continuation forms
3. Verify pipeline extraction behavior after newline normalization

TIP Add review instructions in .codacy/instructions/review.md, or an AGENTS.md, so the reviewer knows your conventions.
TIP How was this review? Give us feedback

Comment thread packages/policy/extract_commands.go Resolved
Comment thread packages/policy/extract_commands.go Resolved
Comment thread packages/policy/extract_commands.go Resolved
Comment thread packages/policy/extract_commands.go Resolved
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.

policy: extract_commands.go misses quoted URLs, newline-separated statements, and the code field

2 participants