-
-
Notifications
You must be signed in to change notification settings - Fork 182
Fix blocking mux reads and RAW download parsing #221
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
Open
durck
wants to merge
3
commits into
NHAS:unstable
Choose a base branch
from
durck:fix/raw-download-request-read-v2
base: unstable
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| package tcp | ||
|
|
||
| import ( | ||
| "io" | ||
| "net" | ||
| "strings" | ||
| "testing" | ||
| "time" | ||
| ) | ||
|
|
||
| func TestReadRawDownloadName(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| chunks []string | ||
| want string | ||
| wantErr bool | ||
| }{ | ||
| { | ||
| name: "shell script", | ||
| chunks: []string{"RAWclient.sh\n"}, | ||
| want: "client.sh", | ||
| }, | ||
| { | ||
| name: "powershell script", | ||
| chunks: []string{"RAWclient.ps1\r\n"}, | ||
| want: "client.ps1", | ||
| }, | ||
| { | ||
| name: "raw binary without newline", | ||
| chunks: []string{"RAWpayload.bin"}, | ||
| want: "payload.bin", | ||
| }, | ||
| { | ||
| name: "max length filename", | ||
| chunks: []string{"RAW" + strings.Repeat("a", rawDownloadMaxNameLength) + "\n"}, | ||
| want: strings.Repeat("a", rawDownloadMaxNameLength), | ||
| }, | ||
| { | ||
| name: "split raw request", | ||
| chunks: []string{"R", "AW", "payload.bin", "\n"}, | ||
| want: "payload.bin", | ||
| }, | ||
| { | ||
| name: "missing raw prefix", | ||
| chunks: []string{"GET /payload.bin HTTP/1.1\r\n"}, | ||
| wantErr: true, | ||
| }, | ||
| { | ||
| name: "empty filename", | ||
| chunks: []string{"RAW\n"}, | ||
| wantErr: true, | ||
| }, | ||
| { | ||
| name: "filename too long", | ||
| chunks: []string{"RAW" + strings.Repeat("a", rawDownloadMaxNameLength+1) + "\n"}, | ||
| wantErr: true, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| conn := &chunkedConn{chunks: byteChunks(tt.chunks)} | ||
|
|
||
| got, err := readRawDownloadName(conn) | ||
| if !conn.readDeadline.IsZero() { | ||
| t.Fatal("read deadline was not reset") | ||
| } | ||
| if tt.wantErr { | ||
| if err == nil { | ||
| t.Fatal("expected error") | ||
| } | ||
| return | ||
| } | ||
| if err != nil { | ||
| t.Fatalf("readRawDownloadName failed: %v", err) | ||
| } | ||
| if got != tt.want { | ||
| t.Fatalf("filename = %q, want %q", got, tt.want) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func byteChunks(chunks []string) [][]byte { | ||
| result := make([][]byte, 0, len(chunks)) | ||
| for _, chunk := range chunks { | ||
| result = append(result, []byte(chunk)) | ||
| } | ||
| return result | ||
| } | ||
|
|
||
| type chunkedConn struct { | ||
| chunks [][]byte | ||
| readDeadline time.Time | ||
| } | ||
|
|
||
| func (c *chunkedConn) Read(b []byte) (int, error) { | ||
| if len(c.chunks) == 0 { | ||
| return 0, io.EOF | ||
| } | ||
|
|
||
| n := copy(b, c.chunks[0]) | ||
| if n == len(c.chunks[0]) { | ||
| c.chunks = c.chunks[1:] | ||
| } else { | ||
| c.chunks[0] = c.chunks[0][n:] | ||
| } | ||
| return n, nil | ||
| } | ||
|
|
||
| func (c *chunkedConn) Write([]byte) (int, error) { | ||
| return 0, nil | ||
| } | ||
|
|
||
| func (c *chunkedConn) Close() error { | ||
| return nil | ||
| } | ||
|
|
||
| func (c *chunkedConn) LocalAddr() net.Addr { | ||
| return nil | ||
| } | ||
|
|
||
| func (c *chunkedConn) RemoteAddr() net.Addr { | ||
| return nil | ||
| } | ||
|
|
||
| func (c *chunkedConn) SetDeadline(time.Time) error { | ||
| return nil | ||
| } | ||
|
|
||
| func (c *chunkedConn) SetReadDeadline(t time.Time) error { | ||
| c.readDeadline = t | ||
| return nil | ||
| } | ||
|
|
||
| func (c *chunkedConn) SetWriteDeadline(time.Time) error { | ||
| return nil | ||
| } |
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 |
|---|---|---|
|
|
@@ -13,18 +13,8 @@ type bufferedConn struct { | |
| func (bc *bufferedConn) Read(b []byte) (n int, err error) { | ||
| if len(bc.prefix) > 0 { | ||
| n = copy(b, bc.prefix) | ||
|
|
||
| bc.prefix = bc.prefix[n:] | ||
|
|
||
| var err error | ||
| if len(b)-n > 0 { | ||
| // If we havent exhausted the size of b, read some more | ||
| var actualRead int | ||
| actualRead, err = bc.conn.Read(b[n:]) | ||
| n += actualRead | ||
| } | ||
|
|
||
| return n, err | ||
| return n, nil | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is good as in hindsight this definitely was a mistake |
||
| } | ||
|
|
||
| return bc.conn.Read(b) | ||
|
|
||
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,56 @@ | ||
| package mux | ||
|
|
||
| import ( | ||
| "net" | ||
| "testing" | ||
| "time" | ||
| ) | ||
|
|
||
| func TestBufferedConnReadUsesPrefixWithoutBlocking(t *testing.T) { | ||
| request := []byte("GET /main.sh HTTP/1.0\r\nHost: example\r\n\r\n") | ||
| blocking := &blockingConn{} | ||
| bc := &bufferedConn{prefix: append([]byte(nil), request...), conn: blocking} | ||
|
|
||
| buf := make([]byte, 4096) | ||
| type readResult struct { | ||
| n int | ||
| err error | ||
| } | ||
| done := make(chan readResult, 1) | ||
| go func() { | ||
| n, err := bc.Read(buf) | ||
| done <- readResult{n: n, err: err} | ||
| }() | ||
|
|
||
| select { | ||
| case result := <-done: | ||
| if result.err != nil { | ||
| t.Fatalf("read failed: %v", result.err) | ||
| } | ||
| if result.n != len(request) { | ||
| t.Fatalf("read %d bytes, want %d", result.n, len(request)) | ||
| } | ||
| case <-time.After(100 * time.Millisecond): | ||
| t.Fatal("bufferedConn.Read blocked waiting for underlying conn") | ||
| } | ||
| if blocking.readCalled { | ||
| t.Fatal("bufferedConn should not read from underlying conn while prefix has data") | ||
| } | ||
| } | ||
|
|
||
| type blockingConn struct { | ||
| readCalled bool | ||
| } | ||
|
|
||
| func (c *blockingConn) Read([]byte) (int, error) { | ||
| c.readCalled = true | ||
| select {} | ||
| } | ||
|
|
||
| func (c *blockingConn) Write([]byte) (int, error) { return 0, nil } | ||
| func (c *blockingConn) Close() error { return nil } | ||
| func (c *blockingConn) LocalAddr() net.Addr { return nil } | ||
| func (c *blockingConn) RemoteAddr() net.Addr { return nil } | ||
| func (c *blockingConn) SetDeadline(time.Time) error { return nil } | ||
| func (c *blockingConn) SetReadDeadline(time.Time) error { return nil } | ||
| func (c *blockingConn) SetWriteDeadline(time.Time) error { return nil } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This chain of random functions makes it much much harder to reason about what this code is doing