Skip to content

Commit c52e1b7

Browse files
committed
Fix ValueError for some invalid Range requests
fixes pallets#2526
1 parent f347d3c commit c52e1b7

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

CHANGES

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ Major release, unreleased
1515
method returns compressed response by default, and pretty response in
1616
debug mode.
1717

18+
Version 0.12.3
19+
--------------
20+
21+
Bugfix release, unreleased
22+
23+
- Fix a ValueError caused by invalid Range requests in some cases
24+
25+
1826
Version 0.12.2
1927
--------------
2028

flask/helpers.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,8 @@ def send_file(filename_or_fp, mimetype=None, as_attachment=False,
591591
rv = rv.make_conditional(request, accept_ranges=True,
592592
complete_length=fsize)
593593
except RequestedRangeNotSatisfiable:
594-
file.close()
594+
if file is not None:
595+
file.close()
595596
raise
596597
else:
597598
rv = rv.make_conditional(request)

tests/test_helpers.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ def __getattr__(self, name):
468468

469469
@pytest.mark.skipif(
470470
not callable(getattr(Range, 'to_content_range_header', None)),
471-
reason="not implement within werkzeug"
471+
reason="not implemented within werkzeug"
472472
)
473473
def test_send_file_range_request(self):
474474
app = flask.Flask(__name__)
@@ -529,6 +529,25 @@ def index():
529529
assert rv.status_code == 200
530530
rv.close()
531531

532+
@pytest.mark.skipif(
533+
not callable(getattr(Range, 'to_content_range_header', None)),
534+
reason="not implemented within werkzeug"
535+
)
536+
def test_send_file_range_request_xsendfile_invalid(self):
537+
# https://github.com/pallets/flask/issues/2526
538+
app = flask.Flask(__name__)
539+
app.use_x_sendfile = True
540+
541+
@app.route('/')
542+
def index():
543+
return flask.send_file('static/index.html', conditional=True)
544+
545+
c = app.test_client()
546+
547+
rv = c.get('/', headers={'Range': 'bytes=1000-'})
548+
assert rv.status_code == 416
549+
rv.close()
550+
532551
def test_attachment(self):
533552
app = flask.Flask(__name__)
534553
with app.test_request_context():

0 commit comments

Comments
 (0)