Skip to content

Return None from Range.parse for 'bytes=-' instead of raising ValueError - #503

Open
syahra712 wants to merge 1 commit into
Pylons:mainfrom
syahra712:fix/range-parse-empty-bounds
Open

syahra712 wants to merge 1 commit into
Pylons:mainfrom
syahra712:fix/range-parse-empty-bounds

Conversation

@syahra712

Copy link
Copy Markdown

A request with the header Range: bytes=- makes WebOb raise instead of ignoring the malformed header:

>>> from webob import Request, Response
>>> req = Request.blank("/", headers={"Range": "bytes=-"})
>>> req.range
ValueError: invalid literal for int() with base 10: ''
>>> req.get_response(Response(body=b"hello", conditional_response=True))
ValueError: invalid literal for int() with base 10: ''

Because the header comes from the client, any app that uses conditional_response=True (for example static file serving) returns a 500 for that request.

Cause: _rx_range is bytes *= *(\d*) *- *(\d*), so bytes=- matches with both groups empty. Range.parse then takes the suffix branch and calls int(""). Other malformed values already return None (the documented "may return None if header is invalid").

Fix: return None when both positions are empty. The header is then ignored, as for other invalid ranges.

Tests:

  • Parse cases in test_range_parse.
  • A conditional-response test that expects a 200 with the full body.

Both fail without the change. The full suite passes (2399 passed), and black and isort are clean. I also added a Bugfix entry to CHANGES.txt.

This doesn't overlap with #502, which changes range_for_length for oversized suffix ranges.

AI-assisted (Claude): I used it to help read the code and draft the test. I reviewed and ran everything myself.

A Range header with neither a first nor a last byte position matched the
regex with two empty groups, and int('') raised ValueError. That made
req.range, and any conditional_response app serving the request, raise.
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