5b8aa9
From c52e1b7388c17466a551391cdf81964bf0b7aef0 Mon Sep 17 00:00:00 2001
5b8aa9
From: ThiefMaster <adrian@planetcoding.net>
5b8aa9
Date: Thu, 23 Nov 2017 10:32:13 +0100
5b8aa9
Subject: [PATCH 2/3] Fix ValueError for some invalid Range requests
5b8aa9
5b8aa9
fixes #2526
5b8aa9
---
5b8aa9
 CHANGES               |  8 ++++++++
5b8aa9
 flask/helpers.py      |  3 ++-
5b8aa9
 tests/test_helpers.py | 21 ++++++++++++++++++++-
5b8aa9
 3 files changed, 30 insertions(+), 2 deletions(-)
5b8aa9
5b8aa9
diff --git a/CHANGES b/CHANGES
5b8aa9
index 3456276a..b32b98cb 100644
5b8aa9
--- a/CHANGES
5b8aa9
+++ b/CHANGES
5b8aa9
@@ -15,6 +15,14 @@ Major release, unreleased
5b8aa9
   method returns compressed response by default, and pretty response in
5b8aa9
   debug mode.
5b8aa9
 
5b8aa9
+Version 0.12.3
5b8aa9
+--------------
5b8aa9
+
5b8aa9
+Bugfix release, unreleased
5b8aa9
+
5b8aa9
+- Fix a ValueError caused by invalid Range requests in some cases
5b8aa9
+
5b8aa9
+
5b8aa9
 Version 0.12.2
5b8aa9
 --------------
5b8aa9
 
5b8aa9
diff --git a/flask/helpers.py b/flask/helpers.py
5b8aa9
index 4bb1d1c9..caaad9a3 100644
5b8aa9
--- a/flask/helpers.py
5b8aa9
+++ b/flask/helpers.py
5b8aa9
@@ -591,7 +591,8 @@ def send_file(filename_or_fp, mimetype=None, as_attachment=False,
5b8aa9
                 rv = rv.make_conditional(request, accept_ranges=True,
5b8aa9
                                          complete_length=fsize)
5b8aa9
             except RequestedRangeNotSatisfiable:
5b8aa9
-                file.close()
5b8aa9
+                if file is not None:
5b8aa9
+                    file.close()
5b8aa9
                 raise
5b8aa9
         else:
5b8aa9
             rv = rv.make_conditional(request)
5b8aa9
diff --git a/tests/test_helpers.py b/tests/test_helpers.py
5b8aa9
index 9320ef71..69350751 100644
5b8aa9
--- a/tests/test_helpers.py
5b8aa9
+++ b/tests/test_helpers.py
5b8aa9
@@ -468,7 +468,7 @@ class TestSendfile(object):
5b8aa9
 
5b8aa9
     @pytest.mark.skipif(
5b8aa9
         not callable(getattr(Range, 'to_content_range_header', None)),
5b8aa9
-        reason="not implement within werkzeug"
5b8aa9
+        reason="not implemented within werkzeug"
5b8aa9
     )
5b8aa9
     def test_send_file_range_request(self):
5b8aa9
         app = flask.Flask(__name__)
5b8aa9
@@ -529,6 +529,25 @@ class TestSendfile(object):
5b8aa9
         assert rv.status_code == 200
5b8aa9
         rv.close()
5b8aa9
 
5b8aa9
+    @pytest.mark.skipif(
5b8aa9
+        not callable(getattr(Range, 'to_content_range_header', None)),
5b8aa9
+        reason="not implemented within werkzeug"
5b8aa9
+    )
5b8aa9
+    def test_send_file_range_request_xsendfile_invalid(self):
5b8aa9
+        # https://github.com/pallets/flask/issues/2526
5b8aa9
+        app = flask.Flask(__name__)
5b8aa9
+        app.use_x_sendfile = True
5b8aa9
+
5b8aa9
+        @app.route('/')
5b8aa9
+        def index():
5b8aa9
+            return flask.send_file('static/index.html', conditional=True)
5b8aa9
+
5b8aa9
+        c = app.test_client()
5b8aa9
+
5b8aa9
+        rv = c.get('/', headers={'Range': 'bytes=1000-'})
5b8aa9
+        assert rv.status_code == 416
5b8aa9
+        rv.close()
5b8aa9
+
5b8aa9
     def test_attachment(self):
5b8aa9
         app = flask.Flask(__name__)
5b8aa9
         with app.test_request_context():
5b8aa9
-- 
5b8aa9
2.21.0
5b8aa9