-
Notifications
You must be signed in to change notification settings - Fork 0
feat(proxy): CIDR rules + HTTP Host header peeking on port 80 #39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
36ba46a
feat(proxy): cidr destinations + http host header peeking
nnemirovsky e9be033
fix(proxy): copilot review round 1 on PR #39
nnemirovsky 424d19c
fix(proxy): spoofing guard for http host peek
nnemirovsky 191cba1
fix(proxy): copilot review round 3 on PR #39
nnemirovsky 1cc33f4
fix(proxy): deny http host peek failures instead of allowing through
nnemirovsky 6a33298
fix(proxy): preserve ask semantic on host peek failure + ipv6 host parse
nnemirovsky File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| package proxy | ||
|
|
||
| import ( | ||
| "bufio" | ||
| "bytes" | ||
| "io" | ||
| "net/http" | ||
| "strings" | ||
| ) | ||
|
|
||
| // peekHTTPHost reads enough bytes from r to parse the HTTP/1.x request line | ||
| // and Host header, returning the peeked buffer and the host. Like peekSNI | ||
| // but for plain HTTP (e.g. ports 80, 8080). The caller prepends the buffer | ||
| // to subsequent reads so the upstream sees the full request. | ||
| // | ||
| // Returns an empty host when the bytes are not a valid HTTP/1.x request | ||
| // (binary protocol, partial data, malformed). In that case the caller should | ||
| // fall back to IP-based policy. Reads are bounded by maxBytes to avoid | ||
| // hanging on slow clients or very long header sets. | ||
| func peekHTTPHost(r io.Reader, maxBytes int) ([]byte, string, error) { | ||
| buf := make([]byte, 0, maxBytes) | ||
| tmp := make([]byte, 4096) | ||
|
|
||
| for len(buf) < maxBytes { | ||
| // Cap each read so a single big chunk does not push buf | ||
| // past maxBytes. | ||
| want := maxBytes - len(buf) | ||
| if want > len(tmp) { | ||
| want = len(tmp) | ||
| } | ||
| n, err := r.Read(tmp[:want]) | ||
| if n > 0 { | ||
| buf = append(buf, tmp[:n]...) | ||
| } | ||
| // Quick reject: HTTP/1.x request lines start with a method like | ||
| // GET/POST/HEAD/etc. Method tokens are uppercase ASCII letters. | ||
| // If the first byte is not in the [A-Z] range, this is not HTTP. | ||
| // Returning early on the first read avoids waiting maxBytes worth | ||
| // of data for a binary protocol that happens to be on port 80. | ||
| if len(buf) >= 1 && (buf[0] < 'A' || buf[0] > 'Z') { | ||
| return buf, "", nil | ||
| } | ||
| // Look for end of headers. http.ReadRequest needs the full header | ||
| // section before it returns; calling it on partial data yields | ||
| // io.ErrUnexpectedEOF, which we treat as "keep reading". | ||
| if idx := bytes.Index(buf, []byte("\r\n\r\n")); idx >= 0 { | ||
| host, ok := extractHTTPHost(buf[:idx+4]) | ||
| if ok { | ||
| return buf, host, nil | ||
| } | ||
| return buf, "", nil | ||
| } | ||
| if err != nil { | ||
| if len(buf) > 0 { | ||
| return buf, "", nil | ||
| } | ||
| return nil, "", err | ||
| } | ||
| } | ||
| return buf, "", nil | ||
| } | ||
|
|
||
| // extractHTTPHost parses an HTTP/1.x request prefix terminated by \r\n\r\n | ||
| // and returns the Host header value with any port stripped. The fast-path | ||
| // uses net/http's parser, which handles obs-fold, mixed case, multiple | ||
| // Host header rules, and request-line validation in one pass. | ||
| func extractHTTPHost(prefix []byte) (string, bool) { | ||
| req, err := http.ReadRequest(bufio.NewReader(bytes.NewReader(prefix))) | ||
| if err != nil { | ||
| return "", false | ||
| } | ||
| host := req.Host | ||
| if host == "" { | ||
| host = req.Header.Get("Host") | ||
| } | ||
| host = strings.TrimSpace(host) | ||
| if host == "" { | ||
| return "", false | ||
| } | ||
| // Strip port if present. IPv6 hosts in Host headers appear as | ||
| // "[::1]:80" so only strip the trailing :port when there is no | ||
| // closing bracket after the last colon. | ||
| if i := strings.LastIndex(host, ":"); i >= 0 { | ||
| if !strings.Contains(host[i:], "]") { | ||
| host = host[:i] | ||
| } | ||
| } | ||
| // IPv6 hosts may still be wrapped in [] — strip those. | ||
| host = strings.TrimPrefix(host, "[") | ||
| host = strings.TrimSuffix(host, "]") | ||
| if host == "" { | ||
| return "", false | ||
| } | ||
| return host, true | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.