Skip to content

treat malformed s-maxage as absent in shared-cache auth check - #874

Merged
ok2c merged 1 commit into
apache:masterfrom
dxbjavid:cache-malformed-smaxage-auth
Aug 17, 2026
Merged

treat malformed s-maxage as absent in shared-cache auth check#874
ok2c merged 1 commit into
apache:masterfrom
dxbjavid:cache-malformed-smaxage-auth

Conversation

@dxbjavid

Copy link
Copy Markdown
Contributor

A shared cache must not store a response to a request that carried an Authorization header unless the response permits it through s-maxage, must-revalidate or public (RFC 9111 3.5). The guard treats any s-maxage as qualifying, but a malformed value like s-maxage=foo is parsed as 0 rather than -1, so it looks present and the authenticated response is stored in the shared cache and can later be handed to other clients on a revalidated hit. This tracks whether a valid s-maxage was actually supplied and uses that in the check, so a malformed directive is treated as absent as the spec requires; the numeric value is left unchanged so freshness behaviour is not affected.

@dxbjavid
dxbjavid force-pushed the cache-malformed-smaxage-auth branch from a52dab3 to e3ab5b3 Compare August 17, 2026 12:53

private static long parseSeconds(final String name, final String value) {
final long delta = CacheSupport.deltaSeconds(value);
if (delta == -1 && LOG.isDebugEnabled()) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dxbjavid Why not making it even simpler?

public static long deltaSeconds(final String s) {
    if (TextUtils.isEmpty(s)) {
        return -1;
    }
    try {
        long ageValue = Long.parseLong(s);
        if (ageValue < 0) {
            ageValue = -1;  // Handle negative age values as invalid
        } else if (ageValue > Integer.MAX_VALUE) {
            ageValue = MAX_AGE.toSeconds();
        }
        return ageValue;
    } catch (final NumberFormatException ignore) {
        return -Integer.MAX_VALUE;
    }
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dxbjavid In fact my original implementation of #deltaSeconds is wrong. It should be returning -1, not 0 if the value cannot be parsed.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agreed, that's the cleaner root cause. i've pulled it into deltaSeconds so an unparseable value now returns -1 instead of 0, which means a malformed s-maxage reads as absent everywhere it's used and the original shared-cache auth check catches it with no extra plumbing. that let me drop the sentinel and revert the parser and policy changes entirely, so the fix is now just the one line in deltaSeconds plus the two tests that were asserting the old 0. full cache suite is green.

@dxbjavid
dxbjavid force-pushed the cache-malformed-smaxage-auth branch from e3ab5b3 to 5d7b7be Compare August 17, 2026 16:41
@ok2c
ok2c merged commit 6ed50d9 into apache:master Aug 17, 2026
10 checks passed
@ok2c

ok2c commented Aug 17, 2026

Copy link
Copy Markdown
Member

@dxbjavid cherry-picked to 5.6.x

@dxbjavid

Copy link
Copy Markdown
Contributor Author

TY!

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.

2 participants