Repository navigation
fix(policy): split shell commands on newlines and read quoted URLs (#116) - #442
codeyevsky merged 2 commits into
Conversation
…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>
Up to standards ✅🟢 Issues
|
| Metric | Results |
|---|---|
| Complexity | 31 |
| Duplication | 0 |
AI Reviewer: first review requested successfully. AI can make mistakes. Always validate suggestions.
TIP This summary will be updated as you push new changes.
There was a problem hiding this comment.
Pull Request Overview
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
This PR fixes issue #116, points 1 and 2. Point 3 (
codeincommandFields) is already onmain.Before, with a deny rule
commandConstraints.denied: ["rm *"]("enabled": true, permission EXECUTE), both the OPA evaluator andFallbackEvaluateblockedecho hi; rm -rf /tmp/xbut allowed:echo hi\nrm -rf /tmp/x:ExtractCommandsreturned one command,"echo hi\nrm -rf /tmp/x".X=rm\n$X -rf /tmp/x: the assignment was never recognised, so$Xstayed unresolved.With
urlConstraints.denied: ["*evil.example*"],curl https://evil.example/xwas blocked, butcurl 'https://evil.example/x'andcurl "https://evil.example/x"were allowed, becauseExtractURLsreturned nothing.HTTPPrefixis anchored, and the token still had its quotes.Change (
packages/policy/extract_commands.go):NormalizeShellCommandturns 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>misrm). Inside single quotes it stays literal.ExtractCommandsandExtractPipelinessplit its output, so both see the separate statements, and assignments carry across lines as they already do across;.git commit -m "title\n\nbody"would become one "command" per body line. In whitelist mode that would block it under an ALLOW rule likegit *.#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/xexposes therm, and so does a comment line ending in a backslash (# note \) withrm -rf /tmp/xon the next line.bash -cruns thermin both cases.ExtractURLsstrips matched quote pairs (the existingdequote) from each token and from a whole value before matching. That includes a quoted token followed directly by;,&or|, as incurl '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 tobash, which runs it.Tests: I added rows to the existing tables in
extract_commands_test.go(TestNormalizeShellCommandStatementsAndQuotes,TestExtractCommandsSplitsAndNormalizes,TestExtractURLsFromToolArgs). Every new row fails onmainexcept 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, andgo vet ./...andgo test -count=1 ./...pass.packages/guard:go vet ./...andgo test -count=1 ./...pass../scripts/sg-parity-check.sh10/10 and./scripts/sg-layers-check.sh25/25.EvaluatePolicyandFallbackEvaluateon the inputs above. Onmainboth allow them; with this change both block them.sg-unit-check.shstops at its gofmt step onmain, because some guard files end without a newline. I didn't try it against a live agent client.Fixes #116