Skip to content

Conversation

isedev
Copy link

@isedev isedev commented Mar 1, 2021

Resolves #69.

@coveralls
Copy link

coveralls commented Mar 1, 2021

Coverage Status

Coverage decreased (-0.2%) to 93.947% when pulling f86c392 on isedev:feature/issue-69 into c4bcea2 on pires:main.

@isedev isedev closed this Mar 1, 2021
@isedev isedev reopened this Mar 1, 2021
@isedev isedev marked this pull request as ready for review March 1, 2021 21:07
Copy link
Owner

@pires pires left a comment

Choose a reason for hiding this comment

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

Thank you very much for putting work into making this lib more secure! <3

Copy link
Owner

@pires pires left a comment

Choose a reason for hiding this comment

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

Please, add tests that prove the buffer growing unchecked as a first commit then this commit that fixes it?

if err != nil {
// The header must be terminated by CRLF and cannot be more than 107 bytes long.
buf, err := reader.Peek(107)
if err != nil && err != io.EOF {
Copy link
Owner

Choose a reason for hiding this comment

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

Per docs:

If Peek returns fewer than n bytes, it also returns an error explaining why the read is short.
Is the error io.EOF? It's unclear from the docs and the code didn't immediately tell me it is.

Copy link
Author

Choose a reason for hiding this comment

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

So there could be different errors - e.g. network read error. However, io.EOF is returned when there is less than 107 bytes available in the stream at that time, based on my tests. I will add some unit tests to cover various cases to validate this.

if err != nil {
// The header must be terminated by CRLF and cannot be more than 107 bytes long.
buf, err := reader.Peek(107)
if err != nil && err != io.EOF {
return nil, ErrLineMustEndWithCrlf
Copy link
Owner

Choose a reason for hiding this comment

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

Please, add a new error here, something like ErrLineSizeOverflow=errors.New("proxyproto: header is invalid, must be of size <= 108 bytes")

Copy link
Owner

Choose a reason for hiding this comment

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

This is to be captured in the tests I asked for.

Copy link
Author

Choose a reason for hiding this comment

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

ok, will do.

// At this point, can't be sure CR precedes LF which will be validated next.
line, err := reader.ReadString('\n')
if err != nil {
// The header must be terminated by CRLF and cannot be more than 107 bytes long.
Copy link
Owner

Choose a reason for hiding this comment

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

Please, add a note here justifying the 107 bytes long, such as per spec

(...) a 108-byte buffer is always enough to store all the line and a trailing zero for string processing.

Copy link
Author

Choose a reason for hiding this comment

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

ok

return nil, ErrLineMustEndWithCrlf
}
if !strings.HasSuffix(line, crlf) {
pos := bytes.Index(buf, []byte(crlf))
if pos == -1 {
Copy link
Owner

Choose a reason for hiding this comment

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

The existing tests already capture this.

Copy link
Author

Choose a reason for hiding this comment

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

Not clear what you mean by this. The Peek can return a buffer without CRLF at this point. It can also return a buffer that extends beyond the CRLF (e.g. short header, but more bytes available in the stream). So HasSuffix does not work here. Perhaps I've misunderstood your point though...

Copy link
Owner

Choose a reason for hiding this comment

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

Just a comment that existing tests (in main branch) already capture a scenario where the string read has LF but not CRLF.

@pires pires self-assigned this Mar 2, 2021
@pires pires added the bug label Mar 2, 2021
@pires pires added this to the 0.5 milestone Mar 2, 2021
@isedev
Copy link
Author

isedev commented Mar 3, 2021

I'll close this PR as the suggested updates don't resolve all problems and I want to base the fix on the commits in #71. I'll submit new fixes shortly.

@isedev isedev closed this Mar 3, 2021
@isedev isedev deleted the feature/issue-69 branch March 3, 2021 02:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

parseVersion1() is not secure
3 participants