Repository navigation
Sanitize filename in Blobstore send_blob Content-Disposition header #164
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
riddhi-shivhare
merged 4 commits into
GoogleCloudPlatform:main
from
riddhi-shivhare:fix-blobstore-content-disposition
Nov 27, 2026
+183
−8
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
388d2c6
Escape filename in Blobstore send_blob Content-Disposition header
riddhi-shivhare abf9625
Replace quotes and backslashes instead of escaping them
riddhi-shivhare d99a3b5
Send no filename parameter when the blob has no filename
riddhi-shivhare 72d8698
Address review comments
riddhi-shivhare File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
143 changes: 143 additions & 0 deletions
143
tests/google/appengine/ext/blobstore/blobstore_handlers_test.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,143 @@ | ||
| #!/usr/bin/env python | ||
| # | ||
| # Copyright 2026 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| # | ||
|
|
||
|
|
||
| """Tests for the download handler in google.appengine.ext.blobstore.""" | ||
|
|
||
| import datetime | ||
|
|
||
| from google.appengine.ext.blobstore import blobstore | ||
| from absl.testing import absltest | ||
|
|
||
|
|
||
| class SendBlobContentDispositionTest(absltest.TestCase): | ||
| """Tests the Content-Disposition header set by send_blob.""" | ||
|
|
||
| def setUp(self): | ||
| super().setUp() | ||
| self.handler = blobstore.BlobstoreDownloadHandler() | ||
|
|
||
| def _create_blob_info(self, filename): | ||
| return blobstore.BlobInfo( | ||
| blobstore.BlobKey('my blob'), | ||
| { | ||
| 'filename': filename, | ||
| 'content_type': b'application/pdf', | ||
| 'size': 1024, | ||
| 'creation': datetime.datetime(2009, 1, 2, 3, 4, 5), | ||
| }, | ||
| ) | ||
|
|
||
| def _send_blob(self, blob_key_or_info, save_as): | ||
| headers = self.handler.send_blob({}, blob_key_or_info, save_as=save_as) | ||
| return headers['Content-Disposition'] | ||
|
|
||
| def test_bytes_filename(self): | ||
| self.assertEqual( | ||
| b'attachment; filename="alternate.txt"', | ||
| self._send_blob('my blob', b'alternate.txt'), | ||
| ) | ||
|
|
||
| def test_text_filename(self): | ||
| self.assertEqual( | ||
| b'attachment; filename="alternate.txt"; ' | ||
| b"filename*=utf-8''alternate.txt", | ||
| self._send_blob('my blob', 'alternate.txt'), | ||
| ) | ||
|
|
||
| def test_non_ascii_uploaded_filename(self): | ||
| self.assertEqual( | ||
| b'attachment; filename="\xe2\x82\xac rates";' | ||
| b" filename*=utf-8''%E2%82%AC%20rates", | ||
| self._send_blob(self._create_blob_info('\u20ac rates'), True), | ||
| ) | ||
|
|
||
| def test_double_quote_is_replaced(self): | ||
| self.assertEqual( | ||
| b'attachment; filename="a_; filename=_evil.exe"', | ||
| self._send_blob('my blob', b'a"; filename="evil.exe'), | ||
| ) | ||
|
|
||
| def test_double_quote_is_replaced_in_text_filename(self): | ||
| self.assertEqual( | ||
| b'attachment; filename="benign.txt_; filename=_setup.exe"; ' | ||
| b"filename*=utf-8''benign.txt%22%3B%20filename%3D%22setup.exe", | ||
| self._send_blob('my blob', 'benign.txt"; filename="setup.exe'), | ||
| ) | ||
|
|
||
| def test_trailing_backslash_is_replaced(self): | ||
| self.assertEqual( | ||
| b'attachment; filename="evil_"', | ||
| self._send_blob('my blob', b'evil\\'), | ||
| ) | ||
|
|
||
| def test_backslash_is_replaced(self): | ||
| self.assertEqual( | ||
| b'attachment; filename="a_b.txt"', | ||
| self._send_blob('my blob', b'a\\b.txt'), | ||
| ) | ||
|
|
||
| def test_backslash_and_double_quote_are_replaced(self): | ||
| self.assertEqual( | ||
| b'attachment; filename="a__b"', | ||
| self._send_blob('my blob', b'a\\"b'), | ||
| ) | ||
|
|
||
| def test_text_filename_keeps_backslash_in_extended_parameter(self): | ||
| self.assertEqual( | ||
| b"attachment; filename=\"a_b\"; filename*=utf-8''a%5Cb", | ||
| self._send_blob('my blob', 'a\\b'), | ||
| ) | ||
|
|
||
| def test_slash_is_encoded_in_extended_parameter(self): | ||
| self.assertEqual( | ||
| b'attachment; filename="dir/file.txt"; ' | ||
| b"filename*=utf-8''dir%2Ffile.txt", | ||
| self._send_blob('my blob', 'dir/file.txt'), | ||
| ) | ||
|
|
||
| def test_control_characters_removed_from_text_filename(self): | ||
| self.assertEqual( | ||
| b'attachment; filename="a_Set-Cookie: x=1"; ' | ||
| b"filename*=utf-8''a%22Set-Cookie%3A%20x%3D1", | ||
| self._send_blob('my blob', 'a"\r\n\x00\t\x7f\x85Set-Cookie: x=1'), | ||
| ) | ||
|
|
||
| def test_control_characters_removed_from_uploaded_filename(self): | ||
| self.assertEqual( | ||
| b'attachment; filename="x_Location: evil"', | ||
| self._send_blob( | ||
| self._create_blob_info(b'x"\r\nLocation: evil'), True | ||
| ), | ||
| ) | ||
|
|
||
| def test_no_filename_when_only_control_characters(self): | ||
| self.assertEqual(b'attachment', self._send_blob('my blob', '\r\n')) | ||
|
|
||
| def test_no_filename_when_bytes_are_only_control_characters(self): | ||
| self.assertEqual( | ||
| b'attachment', self._send_blob('my blob', b'\r\n\x00\x7f') | ||
| ) | ||
|
|
||
| def test_no_filename_when_blob_info_has_none(self): | ||
| self.assertEqual( | ||
| b'attachment', self._send_blob(self._create_blob_info(None), True) | ||
| ) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| absltest.main() | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.