diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index e0be76ce..1001199b 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -19,6 +19,9 @@ on: branches: [ main ] pull_request: +permissions: + contents: read + jobs: python-tests: runs-on: ubuntu-22.04 @@ -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 @@ -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 diff --git a/.github/workflows/docker.yaml b/.github/workflows/docker.yaml index 4e95469e..83ecfaf3 100644 --- a/.github/workflows/docker.yaml +++ b/.github/workflows/docker.yaml @@ -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 diff --git a/.github/workflows/style.yaml b/.github/workflows/style.yaml index 19fe460e..fe494932 100644 --- a/.github/workflows/style.yaml +++ b/.github/workflows/style.yaml @@ -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 diff --git a/Dockerfile b/Dockerfile index 1f12a184..84a53c74 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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", \ diff --git a/gcs/object.py b/gcs/object.py index 4979c40e..c8b03e4c 100644 --- a/gcs/object.py +++ b/gcs/object.py @@ -16,7 +16,6 @@ import base64 import datetime -import gzip import hashlib import json import re @@ -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( @@ -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) diff --git a/testbench/common.py b/testbench/common.py index a87344ef..52aa097f 100644 --- a/testbench/common.py +++ b/testbench/common.py @@ -15,7 +15,6 @@ """Common utils""" import base64 -import gzip import io import json import platform @@ -26,6 +25,7 @@ import time import types import urllib.parse +import zlib from functools import wraps import flask @@ -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. @@ -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) diff --git a/tests/test_common.py b/tests/test_common.py index 7b83012f..38c80b3e 100644 --- a/tests/test_common.py +++ b/tests/test_common.py @@ -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() diff --git a/tests/test_object.py b/tests/test_object.py index e2f95f8f..cc4d173c 100644 --- a/tests/test_object.py +++ b/tests/test_object.py @@ -18,6 +18,7 @@ import base64 import datetime +import gzip import hashlib import json import unittest @@ -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"", + 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()