-
Notifications
You must be signed in to change notification settings - Fork 5
fix(notify): bound hook input and provider payload sizes (#618) #666
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 1 commit
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
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,37 @@ | ||
| package adapter | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "io" | ||
| ) | ||
|
|
||
| // MaxInputBytes is the hard ceiling for hook and stdin payloads. Reading stops | ||
| // one byte past this limit so oversized input is detected without unbounded | ||
| // allocation. | ||
| const MaxInputBytes = 256 * 1024 | ||
|
|
||
| // ErrInputTooLarge is returned when hook or stdin input exceeds MaxInputBytes. | ||
| var ErrInputTooLarge = fmt.Errorf("input exceeds %d-byte limit", MaxInputBytes) | ||
|
|
||
| // ReadBounded reads from r up to MaxInputBytes. If more data is available it | ||
| // returns ErrInputTooLarge without retaining the excess. | ||
| func ReadBounded(r io.Reader) ([]byte, error) { | ||
| limited := io.LimitReader(r, int64(MaxInputBytes)+1) | ||
| raw, err := io.ReadAll(limited) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("read input: %w", err) | ||
| } | ||
| if len(raw) > MaxInputBytes { | ||
| return nil, ErrInputTooLarge | ||
| } | ||
| return raw, nil | ||
| } | ||
|
|
||
| // CheckSize refuses a pre-buffered payload (for example a Codex argv JSON | ||
| // blob) that exceeds MaxInputBytes. | ||
| func CheckSize(raw []byte) error { | ||
| if len(raw) > MaxInputBytes { | ||
| return ErrInputTooLarge | ||
| } | ||
| 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 |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| package adapter | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "errors" | ||
| "io" | ||
| "strings" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestReadBounded_BelowLimit(t *testing.T) { | ||
| in := bytes.Repeat([]byte("a"), MaxInputBytes-1) | ||
| got, err := ReadBounded(bytes.NewReader(in)) | ||
| if err != nil { | ||
| t.Fatalf("ReadBounded: %v", err) | ||
| } | ||
| if len(got) != MaxInputBytes-1 { | ||
| t.Fatalf("len = %d, want %d", len(got), MaxInputBytes-1) | ||
| } | ||
| } | ||
|
|
||
| func TestReadBounded_AtLimit(t *testing.T) { | ||
| in := bytes.Repeat([]byte("b"), MaxInputBytes) | ||
| got, err := ReadBounded(bytes.NewReader(in)) | ||
| if err != nil { | ||
| t.Fatalf("ReadBounded: %v", err) | ||
| } | ||
| if len(got) != MaxInputBytes { | ||
| t.Fatalf("len = %d, want %d", len(got), MaxInputBytes) | ||
| } | ||
| } | ||
|
|
||
| func TestReadBounded_AboveLimit(t *testing.T) { | ||
| in := bytes.Repeat([]byte("c"), MaxInputBytes+1) | ||
| got, err := ReadBounded(bytes.NewReader(in)) | ||
| if !errors.Is(err, ErrInputTooLarge) { | ||
| t.Fatalf("err = %v, want ErrInputTooLarge", err) | ||
| } | ||
| if got != nil { | ||
| t.Fatalf("got %d bytes, want nil on oversized input", len(got)) | ||
| } | ||
| } | ||
|
|
||
| func TestReadBounded_AboveLimitDoesNotDrainUnbounded(t *testing.T) { | ||
| // A reader that can produce far more than the limit must still stop after | ||
| // MaxInputBytes+1 so memory stays bounded. | ||
| r := &countingReader{limit: MaxInputBytes * 4} | ||
| _, err := ReadBounded(r) | ||
| if !errors.Is(err, ErrInputTooLarge) { | ||
| t.Fatalf("err = %v, want ErrInputTooLarge", err) | ||
| } | ||
| if r.read > MaxInputBytes+1 { | ||
| t.Fatalf("read %d bytes, want at most %d", r.read, MaxInputBytes+1) | ||
| } | ||
| } | ||
|
|
||
| func TestAutoDetect_AboveLimit(t *testing.T) { | ||
| in := strings.Repeat("x", MaxInputBytes+1) | ||
| _, err := AutoDetect(strings.NewReader(in)) | ||
| if !errors.Is(err, ErrInputTooLarge) { | ||
| t.Fatalf("err = %v, want ErrInputTooLarge", err) | ||
| } | ||
| } | ||
|
|
||
| func TestCheckSize_Boundaries(t *testing.T) { | ||
| if err := CheckSize(bytes.Repeat([]byte("d"), MaxInputBytes)); err != nil { | ||
| t.Fatalf("at limit: %v", err) | ||
| } | ||
| if err := CheckSize(bytes.Repeat([]byte("e"), MaxInputBytes+1)); !errors.Is(err, ErrInputTooLarge) { | ||
| t.Fatalf("above limit: %v", err) | ||
| } | ||
| } | ||
|
|
||
| type countingReader struct { | ||
| limit int | ||
| read int | ||
| } | ||
|
|
||
| func (c *countingReader) Read(p []byte) (int, error) { | ||
| if c.read >= c.limit { | ||
| return 0, io.EOF | ||
| } | ||
| n := len(p) | ||
| if c.read+n > c.limit { | ||
| n = c.limit - c.read | ||
| } | ||
| for i := 0; i < n; i++ { | ||
| p[i] = 'z' | ||
| } | ||
| c.read += n | ||
| return n, 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
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
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.