-
Notifications
You must be signed in to change notification settings - Fork 528
handler: fix panic in hook emitting logic #1337
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
+98
−2
Merged
Changes from all commits
Commits
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,95 @@ | ||
| package handler | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "net/http/httptest" | ||
| "sync" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| // TestHookEventHeaderRace demonstrates a race condition that occurs when | ||
| // the http.Header map is shared between the original request and HookEvent. | ||
| // This test will fail with `go test -race` if Header is not cloned. | ||
| // | ||
| // See: https://github.com/tus/tusd/issues/1320 | ||
| func TestHookEventHeaderRace(t *testing.T) { | ||
| req := httptest.NewRequest("POST", "/files", nil) | ||
| req.Header.Set("Upload-Length", "1000") | ||
| req.Header.Set("Tus-Resumable", "1.0.0") | ||
| req.Host = "example.com" | ||
|
|
||
| c := &httpContext{ | ||
| req: req, | ||
| } | ||
|
|
||
| info := FileInfo{ | ||
| ID: "test-id", | ||
| Size: 1000, | ||
| MetaData: map[string]string{"filename": "test.txt"}, | ||
| } | ||
|
|
||
| event := newHookEvent(c, info) | ||
|
|
||
| var wg sync.WaitGroup | ||
| const iterations = 100 | ||
|
|
||
| // Goroutine 1: Simulate async hook processing (JSON encoding) | ||
| // This is what happens in invokeHookAsync -> json.Marshal | ||
| wg.Add(1) | ||
| go func() { | ||
| defer wg.Done() | ||
| for range iterations { | ||
| // This iterates over the Header map | ||
| _, _ = json.Marshal(event.HTTPRequest) | ||
| } | ||
| }() | ||
|
|
||
| // Goroutine 2: Simulate concurrent request header modification | ||
| // This could happen if another hook event is created for the same request | ||
| // or if middleware modifies headers | ||
| wg.Add(1) | ||
| go func() { | ||
| defer wg.Done() | ||
| for range iterations { | ||
| // This writes to the Header map | ||
| c.req.Header.Set("X-Request-ID", "some-value") | ||
| c.req.Header.Del("X-Request-ID") | ||
| } | ||
| }() | ||
|
|
||
| wg.Wait() | ||
| } | ||
|
|
||
| // TestHookEventHeaderIsolation verifies that modifying the original request | ||
| // headers after creating a HookEvent does not affect the event headers. | ||
| func TestHookEventHeaderIsolation(t *testing.T) { | ||
| req := httptest.NewRequest("POST", "/files", nil) | ||
| req.Header.Set("X-Original", "original-value") | ||
| req.Host = "example.com" | ||
|
|
||
| c := &httpContext{ | ||
| req: req, | ||
| } | ||
|
|
||
| info := FileInfo{ | ||
| ID: "test-id", | ||
| Size: 1000, | ||
| } | ||
|
|
||
| event := newHookEvent(c, info) | ||
|
|
||
| // Verify original header is present | ||
| assert.Equal(t, "original-value", event.HTTPRequest.Header.Get("X-Original")) | ||
|
|
||
| // Modify the original request header | ||
| c.req.Header.Set("X-Original", "modified-value") | ||
| c.req.Header.Set("X-New", "new-value") | ||
|
|
||
| // Verify event headers are NOT affected (isolation) | ||
| assert.Equal(t, "original-value", event.HTTPRequest.Header.Get("X-Original"), | ||
| "event header should remain unchanged after modifying original request") | ||
| assert.Empty(t, event.HTTPRequest.Header.Get("X-New"), | ||
| "new header added to original request should not appear in event") | ||
| } | ||
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.
Thank you for adding these tests as a demonstration. While they are useful now, I'm not sure if we should keep them in the repo after the fix is merged.
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.
IMO, they are useful to catch if there might be a regression. It doesn't complicate or slow down, better to keep but if you insist, I can drop so that we can merge.