fix: prevent lexer byte-offset drift on invalid UTF-8 input - #4
Merged
Mzack9999 merged 2 commits intoJun 15, 2026
Merged
Conversation
Mzack9999
approved these changes
Jun 15, 2026
1 task
dwisiswant0
pushed a commit
to projectdiscovery/nuclei
that referenced
this pull request
Jun 17, 2026
…8 input (#7464) Upgrade github.com/projectdiscovery/govaluate from v0.0.0-20260504230327-80320480bb6e to v0.0.0-20260615100919-5ee2581bbf7e to consume the fix merged in projectdiscovery/govaluate#4. The govaluate lexer advanced strPosition by utf8.RuneLen(utf8.RuneError) == 3 for invalid bytes, even though only 1 byte was consumed. This caused byte-offset drift past the actual string length and a `slice bounds out of range` panic in readUntilFalse. Nuclei's expressions.Evaluate only caught govaluate errors, not panics, so targets returning invalid UTF-8 in response data could crash the entire process. Fixes #7462
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.
Summary
Fix a slice-bounds panic caused by invalid UTF-8 input drifting the lexer's byte cursor away from the original expression.
This issue was originally identified while investigating a Nuclei crash reported in projectdiscovery/nuclei#7462.
Root Cause
lexerStreammaintains two positions while parsing expressions:position(rune index)strPosition(byte offset into the original source string)When invalid UTF-8 bytes are encountered, Go's string iteration decodes them as
utf8.RuneErrorwhile consuming a single byte from the source string.However, the lexer advanced
strPositionusing:For
utf8.RuneError,utf8.RuneLenreturns3, causing the byte cursor to drift ahead of the actual source position.A later token could then enter the reuse-string fast path in
readUntilFalse()and attempt to slice beyond the end ofsourceString, resulting in:Fix
Use the actual decoded byte width when handling
utf8.RuneErrorduring both forward and backward cursor movement.For valid UTF-8 input, the existing fast path and behavior remain unchanged.
Tests
Added regression coverage for:
Verified that expressions continue to tokenize correctly and no panic occurs.
Validation
All tests pass.
Risk Assessment
Low risk.
The change is isolated to lexer cursor accounting for invalid UTF-8 input.
References
slice bounds out of rangepanic inreadUntilFalseon invalid UTF-8 input nuclei#7462