Skip to content

Commit 2af22d1

Browse files
authored
Be explicit about the SXG cache lifetime. (#323)
The previous behavior didn't specify a max age, which leads to unspecified behavior. Set to "max-age=0" for now, which allows caching on intermediaries, but recommends they update every time. A future change could relax that max age. However, be careful when doing so, as the failure behavior of an expired SXG is a different UX than that of an expired unsigned response.
1 parent 31ac32e commit 2af22d1

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

packager/signer/signer.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -504,10 +504,17 @@ func (this *Signer) serveSignedExchange(resp http.ResponseWriter, fetchResp *htt
504504
resp.Header().Set("AMP-Cache-Transform", act)
505505
}
506506

507-
// TODO(twifkak): Add Cache-Control: public with expiry to match when we think the AMP Cache
508-
// should fetch an update (half-way between signature date & expires).
509507
resp.Header().Set("Content-Type", accept.SxgContentType)
510-
resp.Header().Set("Cache-Control", "no-transform")
508+
// We set a zero freshness lifetime on the SXG, so that naive caching
509+
// intermediaries won't inhibit the update of this resource on AMP
510+
// caches. AMP caches are recommended to base their update strategies
511+
// on a combination of inner and outer resource lifetime.
512+
//
513+
// If you change this code to set a Cache-Control based on the inner
514+
// resource, you need to ensure that its max-age is no longer than the
515+
// lifetime of the signature (6 days, per above). Maybe an even tighter
516+
// bound than that, based on data about client clock skew.
517+
resp.Header().Set("Cache-Control", "no-transform, max-age=0")
511518
resp.Header().Set("X-Content-Type-Options", "nosniff")
512519
if _, err := resp.Write(body.Bytes()); err != nil {
513520
log.Println("Error writing response:", err)

packager/signer/validation_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ import (
1313

1414
func urlFrom(url *url.URL, err *util.HTTPError) *url.URL { return url }
1515

16-
func errorFrom(url *url.URL, err *util.HTTPError) *util.HTTPError { return err }
17-
1816
func urlOrDie(spec string) *url.URL {
1917
url, err := url.Parse(spec)
2018
if err != nil {
@@ -24,6 +22,8 @@ func urlOrDie(spec string) *url.URL {
2422
}
2523

2624
func TestParseURL(t *testing.T) {
25+
errorFrom := func(url *url.URL, err *util.HTTPError) *util.HTTPError { return err }
26+
2727
assert.EqualError(t, errorFrom(parseURL("", "sign")), "sign URL is unspecified")
2828
if err := errorFrom(parseURL("abc-@#79!%^/", "sign")); assert.NotNil(t, err) {
2929
assert.Contains(t, err.Error(), "Error parsing sign URL")

0 commit comments

Comments
 (0)