-
Notifications
You must be signed in to change notification settings - Fork 242
fix(security): enhance timeout configurations and body size limits fo… #3984
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 all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
5d3ce64
fix(security): enhance timeout configurations and body size limits fo…
rchincha efbae2a
fix(tests): refactor backend result handling in proxyHTTPRequest test
rchincha aea87dd
fix(security): preserve ContentLength in proxied requests to prevent …
rchincha 5e3e695
fix(security): preserve explicit zero-length request bodies in proxyH…
rchincha 8ddcf3a
fix(security): prevent default HTTP timeout values from being set unl…
rchincha 3dceb87
fix(security): refactor timeout handling to use explicit checks for n…
rchincha e340cae
fix(tests): add wait_for_event_count function to ensure expected even…
rchincha 5f1e264
fix(security): improve timeout handling and update error responses fo…
rchincha aa204e3
fix(security): enhance HTTP timeout handling with explicit accessors …
rchincha ec06299
fix(security): increase default API key body size and timeout values …
rchincha d08b181
fix(security): unify timeout handling by replacing specific read/writ…
rchincha d5ef525
fix(security): consolidate HTTP timeout accessors and enhance timeout…
rchincha 6c37753
fix(security): simplify HTTP timeout accessors and set default values…
rchincha 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
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,113 @@ | ||
| package api | ||
|
|
||
| import ( | ||
| "context" | ||
| "io" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "net/url" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| . "github.com/smartystreets/goconvey/convey" | ||
|
|
||
| "zotregistry.dev/zot/v2/pkg/api/config" | ||
| "zotregistry.dev/zot/v2/pkg/api/constants" | ||
| ) | ||
|
|
||
| func TestProxyHTTPRequestStreamsBodyAndResponse(t *testing.T) { | ||
| Convey("proxyHTTPRequest forwards request body/headers and returns streamed response", t, func() { | ||
| requestPayload := strings.Repeat("payload-", 1024) | ||
| responsePayload := strings.Repeat("response-", 2048) | ||
|
|
||
| type backendResult struct { | ||
| body string | ||
| hopCount string | ||
| err error | ||
| } | ||
|
|
||
| resultCh := make(chan backendResult, 1) | ||
|
|
||
| backend := httptest.NewServer(http.HandlerFunc(func(response http.ResponseWriter, request *http.Request) { | ||
| body, err := io.ReadAll(request.Body) | ||
| resultCh <- backendResult{ | ||
| body: string(body), | ||
| hopCount: request.Header.Get(constants.ScaleOutHopCountHeader), | ||
| err: err, | ||
| } | ||
|
|
||
| response.WriteHeader(http.StatusCreated) | ||
| _, _ = io.WriteString(response, responsePayload) | ||
| })) | ||
| defer backend.Close() | ||
|
|
||
| backendURL, err := url.Parse(backend.URL) | ||
| So(err, ShouldBeNil) | ||
|
|
||
| conf := config.New() | ||
| conf.Cluster = &config.ClusterConfig{Members: []string{backendURL.Host}, HashKey: "loremipsumdolors"} | ||
|
|
||
| ctrlr := &Controller{Config: conf} | ||
|
|
||
| req, err := http.NewRequestWithContext(context.Background(), http.MethodPut, | ||
| "http://example.com/v2/repo/manifests/latest", strings.NewReader(requestPayload)) | ||
| So(err, ShouldBeNil) | ||
|
|
||
| resp, err := proxyHTTPRequest(context.Background(), req, backendURL.Host, ctrlr) | ||
| So(err, ShouldBeNil) | ||
| So(resp, ShouldNotBeNil) | ||
| defer resp.Body.Close() | ||
|
|
||
| respBody, err := io.ReadAll(resp.Body) | ||
| So(err, ShouldBeNil) | ||
|
|
||
| result := <-resultCh | ||
| So(result.err, ShouldBeNil) | ||
|
|
||
| remainingReqBody, err := io.ReadAll(req.Body) | ||
| So(err, ShouldBeNil) | ||
|
|
||
| So(resp.StatusCode, ShouldEqual, http.StatusCreated) | ||
| So(string(respBody), ShouldEqual, responsePayload) | ||
| So(result.body, ShouldEqual, requestPayload) | ||
| So(result.hopCount, ShouldEqual, "1") | ||
| So(len(remainingReqBody), ShouldEqual, 0) | ||
| }) | ||
| } | ||
|
|
||
| func TestProxyHTTPRequestPreservesExplicitEmptyBody(t *testing.T) { | ||
| Convey("proxyHTTPRequest preserves explicit zero-length request bodies", t, func() { | ||
| resultCh := make(chan *http.Request, 1) | ||
|
|
||
| backend := httptest.NewServer(http.HandlerFunc(func(response http.ResponseWriter, request *http.Request) { | ||
| resultCh <- request | ||
| response.WriteHeader(http.StatusNoContent) | ||
| })) | ||
| defer backend.Close() | ||
|
|
||
| backendURL, err := url.Parse(backend.URL) | ||
| So(err, ShouldBeNil) | ||
|
|
||
| conf := config.New() | ||
| conf.Cluster = &config.ClusterConfig{Members: []string{backendURL.Host}, HashKey: "loremipsumdolors"} | ||
|
|
||
| ctrlr := &Controller{Config: conf} | ||
|
|
||
| req, err := http.NewRequestWithContext(context.Background(), http.MethodPost, | ||
| "http://example.com/v2/repo/manifests/latest", http.NoBody) | ||
| So(err, ShouldBeNil) | ||
| So(req.ContentLength, ShouldEqual, 0) | ||
|
|
||
| resp, err := proxyHTTPRequest(context.Background(), req, backendURL.Host, ctrlr) | ||
| So(err, ShouldBeNil) | ||
| So(resp, ShouldNotBeNil) | ||
| defer resp.Body.Close() | ||
|
|
||
| backendReq := <-resultCh | ||
|
|
||
| So(resp.StatusCode, ShouldEqual, http.StatusNoContent) | ||
| So(backendReq.ContentLength, ShouldEqual, 0) | ||
| So(backendReq.Body, ShouldEqual, http.NoBody) | ||
| So(backendReq.TransferEncoding, ShouldBeEmpty) | ||
| }) | ||
| } |
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.