Skip to content

fix(cache-control): reject invalid cache control times - #238

Open
lprnmns wants to merge 1 commit into
fastify:mainfrom
lprnmns:fix/cache-control-invalid-time
Open

lprnmns wants to merge 1 commit into
fastify:mainfrom
lprnmns:fix/cache-control-invalid-time

Conversation

@lprnmns

@lprnmns lprnmns commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

cacheControl() and staticCache() convert string times with @lukeed/ms and then only check typeof time === 'number'. ms() returns undefined for a string it can't parse, so the time becomes NaN, passes the check and ends up in the header:

reply.cacheControl('max-age', 'forever') // Cache-Control: max-age=NaN
reply.staticCache('two days')            // Cache-Control: public, max-age=NaN, immutable
reply.maxAge(-1)                         // Cache-Control: max-age=-1

Caches treat these directives as invalid, and nothing tells the developer.

A shared toSeconds() helper now converts the time and asserts that it is a finite, non-negative number. maxAge() and stale() go through cacheControl(), so they are covered too. Valid inputs keep producing the same headers.

Tests cover an unparsable string, NaN, Infinity and a negative number. npm test passes, including 100% coverage and the type tests, and so does eslint.

Checklist

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant