-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request from GHSA-75r6-6jg8-pfcq
This change adds some logic to limit the size of the HTTP responses allowed when performing OIDC discovery on a new provider. Prior to this, Octo STS could have been manipulated into reading arbitrary amounts of data from the provided issuer endpoint. Signed-off-by: Matt Moore <[email protected]>
- Loading branch information
Showing
3 changed files
with
113 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,50 @@ | ||
// Copyright 2024 Chainguard, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package maxsize | ||
|
||
import ( | ||
"io" | ||
"net/http" | ||
) | ||
|
||
// NewRoundTripper creates a new http.RoundTripper that wraps the given | ||
// http.RoundTripper and limits the size of the response body to maxSize bytes. | ||
func NewRoundTripper(maxSize int64, inner http.RoundTripper) http.RoundTripper { | ||
return &ms{ | ||
base: inner, | ||
maxBodySize: maxSize, | ||
} | ||
} | ||
|
||
type ms struct { | ||
base http.RoundTripper // The underlying RoundTripper | ||
maxBodySize int64 // Maximum allowed response body size in bytes | ||
} | ||
|
||
// RoundTrip implements http.RoundTripper | ||
func (rt *ms) RoundTrip(req *http.Request) (*http.Response, error) { | ||
resp, err := rt.base.RoundTrip(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
resp.Body = &lr{ | ||
LimitedReader: io.LimitedReader{ | ||
R: resp.Body, | ||
N: rt.maxBodySize, | ||
}, | ||
close: resp.Body.Close, | ||
} | ||
return resp, nil | ||
} | ||
|
||
type lr struct { | ||
io.LimitedReader | ||
close func() error | ||
} | ||
|
||
// Close implements io.Closer | ||
func (r *lr) Close() error { | ||
return r.close() | ||
} |
This file contains 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,49 @@ | ||
// Copyright 2024 Chainguard, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package maxsize | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/coreos/go-oidc/v3/oidc" | ||
) | ||
|
||
func TestCompile(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
size int64 | ||
wantErr bool | ||
}{{ | ||
name: "large size", | ||
size: 1000000, // 1M bytes | ||
wantErr: false, | ||
}, { | ||
name: "medium size", | ||
size: 10000, // 10000 bytes | ||
wantErr: false, | ||
}, { | ||
name: "tiny size", | ||
size: 10, // 10 bytes | ||
wantErr: true, | ||
}} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
ctx := oidc.ClientContext(context.Background(), &http.Client{ | ||
Transport: NewRoundTripper(tt.size, http.DefaultTransport), | ||
}) | ||
for _, issuer := range []string{ | ||
"https://accounts.google.com", | ||
"https://token.actions.githubusercontent.com", | ||
"https://issuer.enforce.dev", | ||
} { | ||
if _, err := oidc.NewProvider(ctx, issuer); (err != nil) != tt.wantErr { | ||
t.Errorf("constructing %q provider: %v", issuer, err) | ||
} | ||
} | ||
}) | ||
} | ||
} |
This file contains 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