dgraph-io/dgraph · warning

Couldn't Parse max-age

Error message

Couldn't Parse max-age

What it means

ParseMaxAge extracts the max-age value from a Cache-Control header string. If the string contains neither a 'max-age' nor 's-maxage' directive, it returns this error. FetchJWK uses it to cache JSON Web Keys, so it signals the HTTP response had no parseable cache lifetime.

Source

Thrown at graphql/authorization/utils.go:25

import (
	"strconv"
	"strings"
	"time"

	"github.com/pkg/errors"
)

func ParseMaxAge(CacheControlHeaderStr string) (int64, error) {
	splittedHeaderStr := strings.Split(CacheControlHeaderStr, ",")
	for _, str := range splittedHeaderStr {
		strTrimSpace := strings.TrimSpace(str)
		if strings.HasPrefix(strTrimSpace, "max-age") || strings.HasPrefix(strTrimSpace, "s-maxage") {
			maxAge, err := strconv.Atoi(strings.Split(str, "=")[1])
			return int64(maxAge), err
		}
	}
	return 0, errors.Errorf("Couldn't Parse max-age")
}

func ParseExpires(ExpiresHeaderStr string) (int64, error) {
	expDate, err := time.Parse(time.RFC1123, ExpiresHeaderStr)
	if err != nil {
		return 0, err
	}
	currDate := time.Now().Round(time.Second)
	diff := expDate.Sub(currDate).Seconds()
	return int64(diff), nil
}

View on GitHub (pinned to 759e242be6)

Solutions

  1. Ensure the JWKS endpoint returns a Cache-Control header with a valid max-age or s-maxage integer
  2. Add an Expires header as fallback (ParseExpires handles it)
  3. Return valid cache headers from the identity provider / reverse proxy
  4. Handle the error in FetchJWK by falling back to a default cache duration

Example fix

// before
Cache-Control: no-store
// after
Cache-Control: public, max-age=3600
Defensive patterns

Strategy: fallback

Validate before calling

const m = cacheControl.match(/(?:s-)?max-age=(\d+)/);
if (!m) console.warn('no max-age in Cache-Control header:', cacheControl);

Try / catch

try {
  ttl = ParseMaxAge(cacheControl);
} catch (err) {
  ttl = defaultJWKCacheTTL; // e.g. 60s
}

Prevention

When it happens

Trigger: Calling FetchJWK against a JWKS endpoint whose response has a Cache-Control header without max-age/s-maxage, or a header that fails strconv.Atoi on the value after '='.

Common situations: JWKS server sends 'Cache-Control: no-cache' or 'no-store'; malformed header like 'max-age=abc'; header with quotes around the value ('max-age="3600"').

Related errors


AI-assisted analysis of dgraph-io/dgraph@759e242be6 (2026-09-01). Data as JSON: /api/errors/7e557aab73b3673f. Report an issue: GitHub.