Skip to content

Commit

Permalink
fix(http): Allows 0 as an Expires header value
Browse files Browse the repository at this point in the history
This is allowed by the RFC and is common with a few OIDC providers.

Partially addresses #136 as a temporary solution until k8s uses the
top level package
  • Loading branch information
Taylor Thomas committed Jul 11, 2017
1 parent d68c0e2 commit 57af5c3
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
7 changes: 6 additions & 1 deletion http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,12 @@ func expires(date, expires string) (time.Duration, bool, error) {
return 0, false, nil
}

te, err := time.Parse(time.RFC1123, expires)
var te time.Time
var err error
if expires == "0" {
return 0, false, nil
}
te, err = time.Parse(time.RFC1123, expires)
if err != nil {
return 0, false, err
}
Expand Down
7 changes: 7 additions & 0 deletions http/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,13 @@ func TestExpiresPass(t *testing.T) {
wantTTL: 0,
wantOK: false,
},
// Expires set to false
{
date: "Thu, 01 Dec 1983 22:00:00 GMT",
exp: "0",
wantTTL: 0,
wantOK: false,
},
// Expires < Date
{
date: "Fri, 02 Dec 1983 01:00:00 GMT",
Expand Down
28 changes: 22 additions & 6 deletions oidc/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -473,15 +473,19 @@ func (g *fakeProviderConfigGetterSetter) Set(cfg ProviderConfig) error {
}

type fakeProviderConfigHandler struct {
cfg ProviderConfig
maxAge time.Duration
cfg ProviderConfig
maxAge time.Duration
noExpires bool
}

func (s *fakeProviderConfigHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
b, _ := json.Marshal(&s.cfg)
if s.maxAge.Seconds() >= 0 {
w.Header().Set("Cache-Control", fmt.Sprintf("public, max-age=%d", int(s.maxAge.Seconds())))
}
if s.noExpires {
w.Header().Set("Expires", "0")
}
w.Header().Set("Content-Type", "application/json")
w.Write(b)
}
Expand Down Expand Up @@ -552,10 +556,11 @@ func TestHTTPProviderConfigGetter(t *testing.T) {
now := fc.Now().UTC()

tests := []struct {
dsc string
age time.Duration
cfg ProviderConfig
ok bool
dsc string
age time.Duration
cfg ProviderConfig
noExpires bool
ok bool
}{
// everything is good
{
Expand Down Expand Up @@ -596,6 +601,17 @@ func TestHTTPProviderConfigGetter(t *testing.T) {
},
ok: true,
},
// An expires header set to 0
{
dsc: "https://example.com",
age: time.Minute,
cfg: ProviderConfig{
Issuer: &url.URL{Scheme: "https", Host: "example.com"},
ExpiresAt: now.Add(time.Minute),
},
ok: true,
noExpires: true,
},
}

for i, tt := range tests {
Expand Down

0 comments on commit 57af5c3

Please sign in to comment.