Skip to content

Commit be294e6

Browse files
fix: fail gracefully when no cache headers are available (#623)
* fix: fail gracefully when no cache headers * PR fixes
1 parent a886851 commit be294e6

File tree

4 files changed

+18
-2500
lines changed

4 files changed

+18
-2500
lines changed

auth/token_verifier.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -432,10 +432,7 @@ func (k *httpKeySource) refreshKeys(ctx context.Context) error {
432432
return err
433433
}
434434

435-
maxAge, err := findMaxAge(resp)
436-
if err != nil {
437-
return err
438-
}
435+
maxAge := findMaxAge(resp)
439436

440437
k.CachedKeys = append([]*publicKey(nil), newKeys...)
441438
k.ExpiryTime = k.Clock.Now().Add(*maxAge)
@@ -476,19 +473,20 @@ func parsePublicKey(kid string, key []byte) (*publicKey, error) {
476473
return &publicKey{kid, pk}, nil
477474
}
478475

479-
func findMaxAge(resp *http.Response) (*time.Duration, error) {
476+
func findMaxAge(resp *http.Response) *time.Duration {
480477
cc := resp.Header.Get("cache-control")
481478
for _, value := range strings.Split(cc, ",") {
482479
value = strings.TrimSpace(value)
483480
if strings.HasPrefix(value, "max-age=") {
484481
sep := strings.Index(value, "=")
485482
seconds, err := strconv.ParseInt(value[sep+1:], 10, 64)
486483
if err != nil {
487-
return nil, err
484+
seconds = 0
488485
}
489486
duration := time.Duration(seconds) * time.Second
490-
return &duration, nil
487+
return &duration
491488
}
492489
}
493-
return nil, errors.New("Could not find expiry time from HTTP headers")
490+
defaultDuration := time.Duration(0) * time.Second
491+
return &defaultDuration
494492
}

auth/token_verifier_test.go

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -140,38 +140,25 @@ func TestFindMaxAge(t *testing.T) {
140140
{"max-age=100", 100},
141141
{"public, max-age=100", 100},
142142
{"public,max-age=100", 100},
143+
{"public, max-age=100, must-revalidate, no-transform", 100},
144+
{"", 0},
145+
{"max-age 100", 0},
146+
{"max-age: 100", 0},
147+
{"max-age2=100", 0},
148+
{"max-age=foo", 0},
149+
{"private,", 0},
143150
}
144151
for _, tc := range cases {
145152
resp := &http.Response{
146153
Header: http.Header{"Cache-Control": {tc.cc}},
147154
}
148-
age, err := findMaxAge(resp)
149-
if err != nil {
150-
t.Errorf("findMaxAge(%q) = %v", tc.cc, err)
151-
} else if *age != (time.Duration(tc.want) * time.Second) {
155+
age := findMaxAge(resp)
156+
if *age != (time.Duration(tc.want) * time.Second) {
152157
t.Errorf("findMaxAge(%q) = %v; want = %v", tc.cc, *age, tc.want)
153158
}
154159
}
155160
}
156161

157-
func TestFindMaxAgeError(t *testing.T) {
158-
cases := []string{
159-
"",
160-
"max-age 100",
161-
"max-age: 100",
162-
"max-age2=100",
163-
"max-age=foo",
164-
}
165-
for _, tc := range cases {
166-
resp := &http.Response{
167-
Header: http.Header{"Cache-Control": []string{tc}},
168-
}
169-
if age, err := findMaxAge(resp); age != nil || err == nil {
170-
t.Errorf("findMaxAge(%q) = (%v, %v); want = (nil, err)", tc, age, err)
171-
}
172-
}
173-
}
174-
175162
func TestParsePublicKeys(t *testing.T) {
176163
b, err := ioutil.ReadFile("../testdata/public_certs.json")
177164
if err != nil {

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ require (
3535
go.opentelemetry.io/otel/metric v1.24.0 // indirect
3636
go.opentelemetry.io/otel/trace v1.24.0 // indirect
3737
golang.org/x/crypto v0.21.0 // indirect
38-
golang.org/x/net v0.22.0 // indirect
38+
golang.org/x/net v0.23.0 // indirect
3939
golang.org/x/sync v0.6.0 // indirect
4040
golang.org/x/sys v0.18.0 // indirect
4141
golang.org/x/text v0.14.0 // indirect

0 commit comments

Comments
 (0)