Sanitise the log in a shape CodeQL can see - #38
Merged
Conversation
The log-forging fix worked and CodeQL kept reporting it. Twelve cs/log-forging alerts stood against main after the fix merged, because the sanitiser was a filter over char.IsControl inside a Select, and CodeQL's C# taint tracking does not model that as sanitising the flow. I read "refs/pull/32/merge reports 0 alerts" as proof it was clear; that ref simply had no analysis, so the 0 meant no data. The scan on the merge commit itself reports 12. Chained string.Replace does the same job in a shape the query recognises. CR and LF as before, and ESC with them -- a terminal tailing a log acts on escape sequences, and a route can carry one. The narrowing is deliberate and it is a narrowing: char.IsControl covered every control character, this covers the three that do something. The rest are ugly in a log rather than dangerous. An alert nobody can close is an alert everybody learns to scroll past, which is the real cost of leaving twelve of them standing on correct code.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The log-forging fix in #32 worked, and CodeQL kept reporting it. Twelve
cs/log-forgingalerts stand againstmainright now, on code that is in fact fixed.I got the verification wrong. I read
refs/pull/32/mergereporting 0 alerts as proof the fix was recognised. That ref simply had no analysis — the 0 meant no data, not no findings. The scan of the merge commit itself reports 12.Why the old shape wasn't recognised
CodeQL's C# taint tracking doesn't model a filter-inside-a-
Selectas sanitising the flow, so the tainted route value still reaches the log as far as the query is concerned.The new shape
Chained
string.Replaceis a shape the query recognises. ESC joins CR and LF because a terminal tailing a log acts on escape sequences and a route can carry one.This is a narrowing, deliberately.
char.IsControlcovered every control character; this covers the three that actually do something. The rest are ugly in a log rather than dangerous.An alert nobody can close is an alert everybody learns to scroll past — that's the real cost of leaving twelve standing on correct code.
Verification
448 tests green on both net8.0 and net10.0.
Assert.Samestill holds for an ordinary name:string.Replacereturns the same reference when nothing matches, so the common path still allocates nothing.New test for the ESC case; the existing CR/LF test is unchanged and still passes.
The real check is CodeQL itself — whether the alert count on
maingoes from 12 to 0 after this merges. I'll confirm that rather than assert it.