Skip to content

fix: catch ValueError for malformed timestamps in decode_signed_value - #3702

Open
abhi-0203 wants to merge 1 commit into
tornadoweb:masterfrom
abhi-0203:fix/malformed-timestamp-valueerror
Open

fix: catch ValueError for malformed timestamps in decode_signed_value#3702
abhi-0203 wants to merge 1 commit into
tornadoweb:masterfrom
abhi-0203:fix/malformed-timestamp-valueerror

Conversation

@abhi-0203

Copy link
Copy Markdown

Summary

Both _decode_signed_value_v1 and _decode_signed_value_v2 in tornado/web.py raise ValueError when the timestamp field is non-numeric after passing HMAC signature verification. This causes get_signed_cookie to raise instead of returning None for malformed cookies.

Changes

  • tornado/web.py: Wrap int() calls for timestamp parsing in both v1 and v2 decoders with try/except ValueError, returning None on failure (consistent with all other malformed-cookie code paths in the same functions).
  • tornado/test/web_test.py: Add test_malformed_timestamp_v1 and test_malformed_timestamp_v2 that construct signed cookies with non-decimal timestamps and verify they are rejected with None instead of raising.

Reproducer

from tornado.escape import utf8
from tornado.web import _create_signature_v2, decode_signed_value

def field(value: bytes) -> bytes:
    return str(len(value)).encode("ascii") + b":" + value

secret = b"s"
name = "a"
prefix = b"|".join([b"2", field(b"0"), field(b"a"), field(utf8(name)), field(b""), b""])
cookie = prefix + _create_signature_v2(secret, prefix)

# Raises ValueError before this patch, returns None after
decode_signed_value(secret, name, cookie, clock=lambda: 1_500_000_000)

Test results

All 12 SignedValueTest tests pass (including the 2 new ones).

Closes #3701

Both v1 and v2 decoders raised ValueError when the timestamp field
was non-numeric after passing HMAC verification. Wrap int() calls in
try/except ValueError and return None, consistent with the documented
behavior of get_signed_cookie returning None for invalid cookies.

Closes tornadoweb#3701
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.

decode_signed_value raises ValueError for authenticated malformed timestamps

1 participant