Fix panic parsing a rockspec comment that ends at EOF#5053
Open
arpitjain099 wants to merge 1 commit into
Open
Conversation
The hand-written rockspec parser reads a byte past the end of the buffer in two spots when a comment runs right up to the end of the file. In parseRockspecBlock, when a block starts with a leading comment that consumes the rest of the file, the SkipWhitespace afterward leaves the index at len(data) and the following `c = data[*i]` reads out of range. In parseComment, `data[*i]` is read after the index is advanced to check for a CR/LF pair, so a bare carriage return as the last byte reads past the end. Both cases show up with a rockspec whose final line is a comment ending in a lone \r with no trailing newline. That is malformed but harmless input, and the panic aborts the whole Lua cataloger, so every valid Lua package in the same scan gets dropped. Guard both reads with a length check and return cleanly at EOF. Added table cases covering a comment-only file and a trailing comment, both ending in a bare CR. Signed-off-by: arpitjain099 <arpitjain099@gmail.com>
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.
While reading through the Lua cataloger I hit a couple of unchecked byte reads in the hand-written rockspec parser that can panic on malformed input. This tightens the bounds checks so a bad rockspec fails gracefully instead of taking the whole cataloger down.
There are two reads that go one byte past the end of the buffer when a comment runs right up to the end of the file:
parseRockspecBlock: when a block starts with a leading comment that consumes the rest of the file, theSkipWhitespaceright after it leaves the index atlen(data), and the followingc = data[*i]reads out of range. Added an EOF check that returns the (empty) block cleanly.parseComment:data[*i]is read after the index has been advanced, to detect a CR/LF pair. If a lone\ris the last byte, that read is out of bounds. Guarded it with*i < len(data).You can reproduce both with a rockspec whose last line is a comment ending in a bare carriage return and no trailing newline, for example
--\ron its own, or a trailing-- x\r. That's malformed but harmless input. The problem is that the panic aborts the entire Lua cataloger, so a single odd file drops every valid Lua package in that scan rather than just skipping the one file.I added two table cases to
rockspec_parser_test.gocovering a comment-only file and a trailing comment, both ending in a bare CR. Before the change they panic with an index-out-of-range; after it they parse without error.go test ./syft/pkg/cataloger/lua/...passes.This is a robustness fix for malformed lockfiles, not a security report.