Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ on:
branches: [ main ]
pull_request:

permissions:
contents: read

jobs:
python-tests:
runs-on: ubuntu-22.04
Expand All @@ -27,9 +30,9 @@ jobs:
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]
name: Unit Tests with Python ${{ matrix.python-version }}
steps:
- uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4
- uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5
with:
python-version: ${{ matrix.python-version }}
- name: Install Dependencies
Expand All @@ -50,9 +53,9 @@ jobs:
python-version: ["3.11"]
name: Unit Tests On Windows Python ${{ matrix.python-version }}
steps:
- uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4
- uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5
with:
python-version: ${{ matrix.python-version }}
- name: Install Dependencies
Expand Down
7 changes: 5 additions & 2 deletions .github/workflows/docker.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,16 @@ on:
branches: [ main ]
pull_request:

permissions:
contents: read

jobs:
psf-black:
name: Verify we can create Docker images
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4
- uses: docker/build-push-action@v6
- uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
- uses: docker/build-push-action@10e90e3645eae34f1e60eeb005ba3a3d33f178e8 # v6
with:
context: .
push: false
Expand Down
7 changes: 5 additions & 2 deletions .github/workflows/style.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,17 @@ on:
branches: [ main ]
pull_request:

permissions:
contents: read

jobs:
psf-black:
name: Format with psf-black
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4
- uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
- name: Set up Python 3.9
uses: actions/setup-python@v5
uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5
with:
python-version: 3.9
- name: Install Black and ISort
Expand Down
6 changes: 5 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ WORKDIR /opt/storage-testbench

COPY . /opt/storage-testbench/

RUN python3 -m pip install -e .
RUN python3 -m pip install -e . && \
useradd --create-home --shell /usr/sbin/nologin testbench && \
chown -R testbench:testbench /opt/storage-testbench

USER testbench

CMD ["python3", \
"testbench_run.py", \
Expand Down
10 changes: 7 additions & 3 deletions gcs/object.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

import base64
import datetime
import gzip
import hashlib
import json
import re
Expand Down Expand Up @@ -506,7 +505,9 @@ def _download_range(self, request, response_payload):
def rest_media(self, request, delay=time.sleep):
is_decompressive_transcode = self._decompress_on_download(request)
response_payload = (
gzip.decompress(self.media) if is_decompressive_transcode else self.media
testbench.common.decompress_gzip_bounded(self.media)
if is_decompressive_transcode
else self.media
)
range_header = request.headers.get("range")
begin, end, length, response_payload = self._download_range(
Expand All @@ -516,7 +517,10 @@ def rest_media(self, request, delay=time.sleep):
if range_header is not None and begin >= length:
testbench.error.range_not_satisfiable()

headers = {}
headers = {
"Content-Security-Policy": "sandbox",
"X-Content-Type-Options": "nosniff",
}
content_range = "bytes %d-%d/%d" % (begin, end - 1, length)

instructions = testbench.common.extract_instruction(request, None)
Expand Down
39 changes: 37 additions & 2 deletions testbench/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
"""Common utils"""

import base64
import gzip
import io
import json
import platform
Expand All @@ -26,6 +25,7 @@
import time
import types
import urllib.parse
import zlib
from functools import wraps

import flask
Expand Down Expand Up @@ -1214,6 +1214,41 @@ def consume_expect_redirect_token(db, context, method, routing_token):
db.dequeue_next_instruction(test_id, method)


MAX_GZIP_DECOMPRESSED_BYTES = 128 * 1024 * 1024


def decompress_gzip_bounded(data, max_bytes=None):
"""Decompress gzip data while enforcing a maximum decompressed output size."""
if max_bytes is None:
max_bytes = MAX_GZIP_DECOMPRESSED_BYTES
if not data:
return b""
decompressor = zlib.decompressobj(16 + zlib.MAX_WBITS)
chunks = []
total = 0
remaining = data
while remaining:
out = decompressor.decompress(remaining, max_bytes - total + 1)
total += len(out)
if total > max_bytes:
raise testbench.error.RestException(
f"Decompressed payload exceeds maximum allowed size of {max_bytes} bytes",
413,
)
chunks.append(out)
if decompressor.eof:
remaining = decompressor.unused_data
if remaining:
decompressor = zlib.decompressobj(16 + zlib.MAX_WBITS)
else:
remaining = decompressor.unconsumed_tail
if not decompressor.eof:
raise EOFError(
"Compressed file ended before the end-of-stream marker was reached"
)
return b"".join(chunks)


def handle_gzip_request(request):
"""
Handle gzip compressed JSON payloads when Content-Encoding: gzip is present on metadata requests.
Expand All @@ -1223,7 +1258,7 @@ def handle_gzip_request(request):
request.headers.get("Content-Encoding", None) == "gzip"
and request.args.get("contentEncoding", None) != "gzip"
):
request.data = gzip.decompress(request.data)
request.data = decompress_gzip_bounded(request.data)
request.environ["wsgi.input"] = io.BytesIO(request.data)


Expand Down
56 changes: 56 additions & 0 deletions tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -1334,6 +1334,62 @@ def test_handle_stall_uploads_after_bytes_grpc_cancelled(self):
grpc.StatusCode.CANCELLED, unittest.mock.ANY
)

def test_decompress_gzip_bounded_within_limit(self):
payload = b"hello world" * 10
compressed = gzip.compress(payload)
self.assertEqual(
testbench.common.decompress_gzip_bounded(
compressed, max_bytes=len(payload)
),
payload,
)

def test_decompress_gzip_bounded_concatenated_streams(self):
part1 = b"first-" * 5
part2 = b"second-" * 5
compressed = gzip.compress(part1) + gzip.compress(part2)
self.assertEqual(
testbench.common.decompress_gzip_bounded(
compressed, max_bytes=len(part1) + len(part2)
),
part1 + part2,
)

def test_decompress_gzip_bounded_exceeds_limit(self):
compressed = gzip.compress(b"a" * 1024)
with self.assertRaises(testbench.error.RestException) as rest:
testbench.common.decompress_gzip_bounded(compressed, max_bytes=128)
self.assertEqual(rest.exception.code, 413)
self.assertIn("exceeds maximum allowed size", rest.exception.msg)

def test_decompress_gzip_bounded_concatenated_exceeds_limit(self):
compressed = gzip.compress(b"a" * 80) + gzip.compress(b"b" * 80)
with self.assertRaises(testbench.error.RestException) as rest:
testbench.common.decompress_gzip_bounded(compressed, max_bytes=128)
self.assertEqual(rest.exception.code, 413)
self.assertIn("exceeds maximum allowed size", rest.exception.msg)

def test_decompress_gzip_bounded_truncated_raises_eof(self):
compressed = gzip.compress(b"hello world" * 10)
with self.assertRaises(EOFError):
testbench.common.decompress_gzip_bounded(compressed[:-4], max_bytes=1024)

def test_handle_gzip_request_exceeds_limit(self):
compressed = gzip.compress(b"a" * 1024)
request = testbench.common.FakeRequest(
args={},
headers={"Content-Encoding": "gzip"},
data=compressed,
environ={},
)
with unittest.mock.patch.object(
testbench.common, "MAX_GZIP_DECOMPRESSED_BYTES", 128
):
with self.assertRaises(testbench.error.RestException) as rest:
testbench.common.handle_gzip_request(request)
self.assertEqual(rest.exception.code, 413)
self.assertIn("exceeds maximum allowed size", rest.exception.msg)


if __name__ == "__main__":
unittest.main()
39 changes: 39 additions & 0 deletions tests/test_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import base64
import datetime
import gzip
import hashlib
import json
import unittest
Expand Down Expand Up @@ -1062,6 +1063,44 @@ def _create_request(self, data, is_multipart=False, boundary=None):
args={}, headers=headers, data=payload, environ={}
)

def test_rest_media_sets_sandbox_security_headers(self):
request = testbench.common.FakeRequest(
args={"name": "page.html"},
headers={},
data=b"<script>alert(1)</script>",
environ={},
)
blob, _ = gcs.object.Object.init_media(request, self.bucket.metadata)
blob.metadata.content_type = "text/html"
download_req = Request(
create_environ(base_url="http://localhost:8080", data=b"")
)
response = blob.rest_media(download_req)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.headers.get("Content-Type"), "text/html")
self.assertEqual(response.headers.get("Content-Security-Policy"), "sandbox")
self.assertEqual(response.headers.get("X-Content-Type-Options"), "nosniff")

def test_rest_media_bounded_gzip_transcode(self):
compressed = gzip.compress(b"x" * 2048)
request = testbench.common.FakeRequest(
args={"name": "bomb.txt"},
headers={},
data=compressed,
environ={},
)
blob, _ = gcs.object.Object.init_media(request, self.bucket.metadata)
blob.metadata.content_encoding = "gzip"
download_req = Request(
create_environ(base_url="http://localhost:8080", data=b"")
)
with unittest.mock.patch.object(
testbench.common, "MAX_GZIP_DECOMPRESSED_BYTES", 512
):
with self.assertRaises(testbench.error.RestException) as rest:
blob.rest_media(download_req)
self.assertEqual(rest.exception.code, 413)


if __name__ == "__main__":
unittest.main()
Loading