-
Notifications
You must be signed in to change notification settings - Fork 1.8k
AVRO-4289: [php] Enforce a maximum decompressed block size #3856
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
Open
iemejia
wants to merge
9
commits into
apache:main
Choose a base branch
from
iemejia:AVRO-4289-php-decompress-limit
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
6014236
AVRO-4289: [php] Enforce a maximum decompressed block size
iemejia 2ce33fa
AVRO-4289: [php] Satisfy php-cs-fixer style checks
iemejia fc78dc1
AVRO-4289: [php] Address review: autoload order, streaming inflate, s…
iemejia 0d74370
AVRO-4289: [php] Reject overlong snappy varint; avoid quadratic infla…
iemejia 1fe9871
AVRO-4289: [php] Guard short Snappy blocks and truncated length headers
iemejia e7bdb4a
AVRO-4289: [php] Clarify gzUncompress chunk-inflation comment
iemejia 6d58380
AVRO-4289: [php] Correct deflate error wording; ensure reader is clos…
iemejia d10c764
AVRO-4289: [php] Compare snappy CRC32 in a common unsigned representa…
iemejia 8171b89
AVRO-4289: [php] Drop error-suppression on inflate_add
iemejia 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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
lang/php/lib/DataFile/AvroDataIODecompressionSizeException.php
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,40 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you 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 | ||
| * | ||
| * https://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. | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Apache\Avro\DataFile; | ||
|
|
||
| /** | ||
| * Raised when a data-file block decompresses to more than the configured maximum. | ||
| * | ||
| * A block with a very high compression ratio (or a malformed block) can expand | ||
| * to far more memory than its compressed size; this exception guards against | ||
| * unbounded memory allocation while reading such a block. | ||
| */ | ||
| class AvroDataIODecompressionSizeException extends AvroDataIOException | ||
| { | ||
| public function __construct(int $maxLength) | ||
| { | ||
| parent::__construct( | ||
| sprintf('Decompressed block size exceeds the maximum allowed of %d bytes.', $maxLength) | ||
| ); | ||
| } | ||
| } |
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
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
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 |
|---|---|---|
|
|
@@ -23,6 +23,8 @@ | |
| namespace Apache\Avro\Tests; | ||
|
|
||
| use Apache\Avro\DataFile\AvroDataIO; | ||
| use Apache\Avro\DataFile\AvroDataIODecompressionSizeException; | ||
| use Apache\Avro\DataFile\AvroDataIOReader; | ||
| use PHPUnit\Framework\TestCase; | ||
|
|
||
| class DataFileTest extends TestCase | ||
|
|
@@ -386,6 +388,94 @@ public function test_differing_schemas_with_complex_objects(): void | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * A block with a very high compression ratio can expand to far more memory | ||
| * than its compressed size; reading such a block must be rejected once its | ||
| * decompressed size would exceed the configured maximum. | ||
| */ | ||
| public function test_deflate_block_decompression_limit(): void | ||
|
Comment on lines
+391
to
+396
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in fc78dc1 — added snappy/zstandard/bzip2 decompression-limit tests (skipped when the extension is unavailable). |
||
| { | ||
| $data_file = $this->add_data_file('data-decompress-limit-deflate.avr'); | ||
| $dw = AvroDataIO::openFile($data_file, 'w', '"string"', AvroDataIO::DEFLATE_CODEC); | ||
| $dw->append(str_repeat('a', 64 * 1024)); // 64 KiB, compresses tiny | ||
| $dw->close(); | ||
|
|
||
| $previous = getenv(AvroDataIOReader::MAX_DECOMPRESS_LENGTH_ENV); | ||
| putenv(AvroDataIOReader::MAX_DECOMPRESS_LENGTH_ENV.'=1024'); | ||
|
|
||
| try { | ||
| $dr = AvroDataIO::openFile($data_file); | ||
|
|
||
| try { | ||
| $dr->data(); | ||
| $this->fail('expected a decompression size exception'); | ||
| } catch (AvroDataIODecompressionSizeException $e) { | ||
| // expected | ||
| } finally { | ||
| $dr->close(); | ||
| } | ||
| } finally { | ||
| if (false === $previous) { | ||
| putenv(AvroDataIOReader::MAX_DECOMPRESS_LENGTH_ENV); | ||
| } else { | ||
| putenv(AvroDataIOReader::MAX_DECOMPRESS_LENGTH_ENV.'='.$previous); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public function test_deflate_block_within_decompression_limit(): void | ||
| { | ||
| $data_file = $this->add_data_file('data-decompress-within-limit.avr'); | ||
| $payload = 'hello world'; | ||
| $dw = AvroDataIO::openFile($data_file, 'w', '"string"', AvroDataIO::DEFLATE_CODEC); | ||
| $dw->append($payload); | ||
| $dw->close(); | ||
|
|
||
| $previous = getenv(AvroDataIOReader::MAX_DECOMPRESS_LENGTH_ENV); | ||
| putenv(AvroDataIOReader::MAX_DECOMPRESS_LENGTH_ENV.'=1048576'); | ||
|
|
||
| try { | ||
| $dr = AvroDataIO::openFile($data_file); | ||
|
|
||
| try { | ||
| $data = $dr->data(); | ||
| $this->assertSame([$payload], $data); | ||
| } finally { | ||
| $dr->close(); | ||
| } | ||
| } finally { | ||
| if (false === $previous) { | ||
| putenv(AvroDataIOReader::MAX_DECOMPRESS_LENGTH_ENV); | ||
| } else { | ||
| putenv(AvroDataIOReader::MAX_DECOMPRESS_LENGTH_ENV.'='.$previous); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public function test_snappy_block_decompression_limit(): void | ||
| { | ||
| if (!extension_loaded('snappy')) { | ||
| $this->markTestSkipped('snappy extension not available'); | ||
| } | ||
| $this->assertCodecRejectsOversizedBlock(AvroDataIO::SNAPPY_CODEC); | ||
| } | ||
|
|
||
| public function test_zstandard_block_decompression_limit(): void | ||
| { | ||
| if (!extension_loaded('zstd')) { | ||
| $this->markTestSkipped('zstd extension not available'); | ||
| } | ||
| $this->assertCodecRejectsOversizedBlock(AvroDataIO::ZSTANDARD_CODEC); | ||
| } | ||
|
|
||
| public function test_bzip2_block_decompression_limit(): void | ||
| { | ||
| if (!extension_loaded('bz2')) { | ||
| $this->markTestSkipped('bz2 extension not available'); | ||
| } | ||
| $this->assertCodecRejectsOversizedBlock(AvroDataIO::BZIP2_CODEC); | ||
| } | ||
|
|
||
| protected function add_data_file(string $data_file): string | ||
| { | ||
| $data_file = "$data_file.".self::current_timestamp(); | ||
|
|
@@ -411,4 +501,38 @@ protected static function remove_data_file($data_file): void | |
| unlink($data_file); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Write a single, highly compressible block with the given codec, then read | ||
| * it back with a small decompression limit and assert it is rejected. | ||
| */ | ||
| private function assertCodecRejectsOversizedBlock(string $codec): void | ||
| { | ||
| $data_file = $this->add_data_file(sprintf('data-decompress-limit-%s.avr', $codec)); | ||
| $dw = AvroDataIO::openFile($data_file, 'w', '"string"', $codec); | ||
| $dw->append(str_repeat('a', 64 * 1024)); // 64 KiB, compresses tiny | ||
| $dw->close(); | ||
|
|
||
| $previous = getenv(AvroDataIOReader::MAX_DECOMPRESS_LENGTH_ENV); | ||
| putenv(AvroDataIOReader::MAX_DECOMPRESS_LENGTH_ENV.'=1024'); | ||
|
|
||
| try { | ||
| $dr = AvroDataIO::openFile($data_file); | ||
|
|
||
| try { | ||
| $dr->data(); | ||
| $this->fail(sprintf('expected a decompression size exception for %s', $codec)); | ||
| } catch (AvroDataIODecompressionSizeException $e) { | ||
| // expected | ||
| } finally { | ||
| $dr->close(); | ||
| } | ||
| } finally { | ||
| if (false === $previous) { | ||
| putenv(AvroDataIOReader::MAX_DECOMPRESS_LENGTH_ENV); | ||
| } else { | ||
| putenv(AvroDataIOReader::MAX_DECOMPRESS_LENGTH_ENV.'='.$previous); | ||
| } | ||
| } | ||
| } | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated the PR description — it now says the deflate codec inflates in bounded chunks via inflate_init/inflate_add and rejects the block as soon as the accumulated output exceeds the limit, matching the implementation (no gzinflate).