iemejia commented on code in PR #3854: URL: https://github.com/apache/avro/pull/3854#discussion_r3568074276
########## lang/c++/test/DecompressionLimitTests.cc: ########## @@ -0,0 +1,177 @@ +/* + * 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. + */ + +// AVRO-4285: a data-file block is decompressed according to the file's codec. A +// block with a very high compression ratio (or a malformed block) 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, which +// these tests set to a small value via AVRO_MAX_DECOMPRESS_LENGTH. + +#include <cstdlib> +#include <filesystem> +#include <optional> +#include <sstream> +#include <string> + +#include <boost/test/included/unit_test.hpp> + +#include "Compiler.hh" +#include "DataFile.hh" +#include "Exception.hh" +#include "ValidSchema.hh" + +namespace avro { + +static void setDecompressLimit(const char *value) { +#ifdef _WIN32 + _putenv_s("AVRO_MAX_DECOMPRESS_LENGTH", value); Review Comment: Fixed — setDecompressLimit/unsetDecompressLimit now return the putenv/setenv/unsetenv status, and every direct call asserts it with BOOST_REQUIRE_EQUAL(..., 0). ########## lang/c++/test/DecompressionLimitTests.cc: ########## @@ -0,0 +1,177 @@ +/* + * 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. + */ + +// AVRO-4285: a data-file block is decompressed according to the file's codec. A +// block with a very high compression ratio (or a malformed block) 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, which +// these tests set to a small value via AVRO_MAX_DECOMPRESS_LENGTH. + +#include <cstdlib> +#include <filesystem> +#include <optional> +#include <sstream> +#include <string> + +#include <boost/test/included/unit_test.hpp> + +#include "Compiler.hh" +#include "DataFile.hh" +#include "Exception.hh" +#include "ValidSchema.hh" + +namespace avro { + +static void setDecompressLimit(const char *value) { +#ifdef _WIN32 + _putenv_s("AVRO_MAX_DECOMPRESS_LENGTH", value); +#else + setenv("AVRO_MAX_DECOMPRESS_LENGTH", value, 1); Review Comment: Fixed — setDecompressLimit/unsetDecompressLimit now return the putenv/setenv/unsetenv status, and every direct call asserts it with BOOST_REQUIRE_EQUAL(..., 0). ########## lang/c++/test/DecompressionLimitTests.cc: ########## @@ -0,0 +1,177 @@ +/* + * 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. + */ + +// AVRO-4285: a data-file block is decompressed according to the file's codec. A +// block with a very high compression ratio (or a malformed block) 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, which +// these tests set to a small value via AVRO_MAX_DECOMPRESS_LENGTH. + +#include <cstdlib> +#include <filesystem> +#include <optional> +#include <sstream> +#include <string> + +#include <boost/test/included/unit_test.hpp> + +#include "Compiler.hh" +#include "DataFile.hh" +#include "Exception.hh" +#include "ValidSchema.hh" + +namespace avro { + +static void setDecompressLimit(const char *value) { +#ifdef _WIN32 + _putenv_s("AVRO_MAX_DECOMPRESS_LENGTH", value); +#else + setenv("AVRO_MAX_DECOMPRESS_LENGTH", value, 1); +#endif +} + +static void unsetDecompressLimit() { +#ifdef _WIN32 + _putenv_s("AVRO_MAX_DECOMPRESS_LENGTH", ""); Review Comment: Fixed — setDecompressLimit/unsetDecompressLimit now return the putenv/setenv/unsetenv status, and every direct call asserts it with BOOST_REQUIRE_EQUAL(..., 0). ########## lang/c++/test/DecompressionLimitTests.cc: ########## @@ -0,0 +1,177 @@ +/* + * 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. + */ + +// AVRO-4285: a data-file block is decompressed according to the file's codec. A +// block with a very high compression ratio (or a malformed block) 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, which +// these tests set to a small value via AVRO_MAX_DECOMPRESS_LENGTH. + +#include <cstdlib> +#include <filesystem> +#include <optional> +#include <sstream> +#include <string> + +#include <boost/test/included/unit_test.hpp> + +#include "Compiler.hh" +#include "DataFile.hh" +#include "Exception.hh" +#include "ValidSchema.hh" + +namespace avro { + +static void setDecompressLimit(const char *value) { +#ifdef _WIN32 + _putenv_s("AVRO_MAX_DECOMPRESS_LENGTH", value); +#else + setenv("AVRO_MAX_DECOMPRESS_LENGTH", value, 1); +#endif +} + +static void unsetDecompressLimit() { +#ifdef _WIN32 + _putenv_s("AVRO_MAX_DECOMPRESS_LENGTH", ""); +#else + unsetenv("AVRO_MAX_DECOMPRESS_LENGTH"); Review Comment: Fixed — setDecompressLimit/unsetDecompressLimit now return the putenv/setenv/unsetenv status, and every direct call asserts it with BOOST_REQUIRE_EQUAL(..., 0). ########## lang/c++/test/DecompressionLimitTests.cc: ########## @@ -0,0 +1,177 @@ +/* + * 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. + */ + +// AVRO-4285: a data-file block is decompressed according to the file's codec. A +// block with a very high compression ratio (or a malformed block) 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, which +// these tests set to a small value via AVRO_MAX_DECOMPRESS_LENGTH. + +#include <cstdlib> +#include <filesystem> +#include <optional> +#include <sstream> +#include <string> + +#include <boost/test/included/unit_test.hpp> + +#include "Compiler.hh" +#include "DataFile.hh" +#include "Exception.hh" +#include "ValidSchema.hh" + +namespace avro { + +static void setDecompressLimit(const char *value) { +#ifdef _WIN32 + BOOST_REQUIRE_EQUAL(_putenv_s("AVRO_MAX_DECOMPRESS_LENGTH", value), 0); +#else + BOOST_REQUIRE_EQUAL(setenv("AVRO_MAX_DECOMPRESS_LENGTH", value, 1), 0); +#endif +} + +static void unsetDecompressLimit() { +#ifdef _WIN32 + BOOST_REQUIRE_EQUAL(_putenv_s("AVRO_MAX_DECOMPRESS_LENGTH", ""), 0); +#else + BOOST_REQUIRE_EQUAL(unsetenv("AVRO_MAX_DECOMPRESS_LENGTH"), 0); +#endif +} Review Comment: Fixed — the helpers now return a status code instead of asserting internally, and the guard destructor uses BOOST_CHECK_EQUAL (non-throwing), so a failure there can't abort a noexcept destructor. ########## lang/c++/test/DecompressionLimitTests.cc: ########## @@ -0,0 +1,177 @@ +/* + * 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. + */ + +// AVRO-4285: a data-file block is decompressed according to the file's codec. A +// block with a very high compression ratio (or a malformed block) 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, which +// these tests set to a small value via AVRO_MAX_DECOMPRESS_LENGTH. + +#include <cstdlib> +#include <filesystem> +#include <optional> +#include <sstream> +#include <string> + +#include <boost/test/included/unit_test.hpp> + +#include "Compiler.hh" +#include "DataFile.hh" +#include "Exception.hh" +#include "ValidSchema.hh" + +namespace avro { + +static void setDecompressLimit(const char *value) { +#ifdef _WIN32 + BOOST_REQUIRE_EQUAL(_putenv_s("AVRO_MAX_DECOMPRESS_LENGTH", value), 0); +#else + BOOST_REQUIRE_EQUAL(setenv("AVRO_MAX_DECOMPRESS_LENGTH", value, 1), 0); +#endif +} + +static void unsetDecompressLimit() { +#ifdef _WIN32 + BOOST_REQUIRE_EQUAL(_putenv_s("AVRO_MAX_DECOMPRESS_LENGTH", ""), 0); +#else + BOOST_REQUIRE_EQUAL(unsetenv("AVRO_MAX_DECOMPRESS_LENGTH"), 0); +#endif +} + +// Saves AVRO_MAX_DECOMPRESS_LENGTH on construction and restores it on +// destruction, so a test's override does not leak into other test cases that +// share the process. +struct DecompressLimitGuard { + std::optional<std::string> previous; + DecompressLimitGuard() { + const char *env = std::getenv("AVRO_MAX_DECOMPRESS_LENGTH"); + if (env != nullptr) { + previous = std::string(env); + } + } + ~DecompressLimitGuard() { + if (previous) { + setDecompressLimit(previous->c_str()); + } else { + unsetDecompressLimit(); + } + } Review Comment: Done — destructor uses BOOST_CHECK_EQUAL on the returned status. ########## lang/c++/test/DecompressionLimitTests.cc: ########## @@ -0,0 +1,177 @@ +/* + * 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. + */ + +// AVRO-4285: a data-file block is decompressed according to the file's codec. A +// block with a very high compression ratio (or a malformed block) 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, which +// these tests set to a small value via AVRO_MAX_DECOMPRESS_LENGTH. + +#include <cstdlib> +#include <filesystem> +#include <optional> +#include <sstream> +#include <string> + +#include <boost/test/included/unit_test.hpp> + +#include "Compiler.hh" +#include "DataFile.hh" +#include "Exception.hh" +#include "ValidSchema.hh" + +namespace avro { + +static void setDecompressLimit(const char *value) { +#ifdef _WIN32 + BOOST_REQUIRE_EQUAL(_putenv_s("AVRO_MAX_DECOMPRESS_LENGTH", value), 0); +#else + BOOST_REQUIRE_EQUAL(setenv("AVRO_MAX_DECOMPRESS_LENGTH", value, 1), 0); +#endif +} + +static void unsetDecompressLimit() { +#ifdef _WIN32 + BOOST_REQUIRE_EQUAL(_putenv_s("AVRO_MAX_DECOMPRESS_LENGTH", ""), 0); +#else + BOOST_REQUIRE_EQUAL(unsetenv("AVRO_MAX_DECOMPRESS_LENGTH"), 0); +#endif +} + +// Saves AVRO_MAX_DECOMPRESS_LENGTH on construction and restores it on +// destruction, so a test's override does not leak into other test cases that +// share the process. +struct DecompressLimitGuard { + std::optional<std::string> previous; + DecompressLimitGuard() { + const char *env = std::getenv("AVRO_MAX_DECOMPRESS_LENGTH"); + if (env != nullptr) { + previous = std::string(env); + } + } + ~DecompressLimitGuard() { + if (previous) { + setDecompressLimit(previous->c_str()); + } else { + unsetDecompressLimit(); + } + } +}; + +static ValidSchema stringSchema() { + std::istringstream iss("\"string\""); + ValidSchema vs; + compileJsonSchema(iss, vs); + return vs; +} + +static std::string tempFile(const char *name) { + return (std::filesystem::temp_directory_path() / name).string(); +} + +// Write a single, highly compressible value with the given codec, then read it +// back with a small decompression limit and confirm the read is rejected. +static void checkCodecRejectsOversized(Codec codec, const char *name) { + DecompressLimitGuard guard; + ValidSchema schema = stringSchema(); + std::string path = tempFile(name); + std::string big(4 * 1024 * 1024, 'a'); // 4 MiB, compresses tiny + + // Let any writer exception propagate: a failure here is a real problem + // (e.g. permissions or I/O), not a reason to silently skip. Codecs that are + // not compiled in are excluded via the #ifdef guards on the callers below. + { + DataFileWriter<std::string> writer(path.c_str(), schema, 64 * 1024 * 1024, codec); + writer.write(big); + writer.close(); + } + + setDecompressLimit("1048576"); // 1 MiB, smaller than the 4 MiB block Review Comment: Done — this call is now BOOST_REQUIRE_EQUAL(setDecompressLimit("1048576"), 0). ########## lang/c++/test/DecompressionLimitTests.cc: ########## @@ -0,0 +1,177 @@ +/* + * 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. + */ + +// AVRO-4285: a data-file block is decompressed according to the file's codec. A +// block with a very high compression ratio (or a malformed block) 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, which +// these tests set to a small value via AVRO_MAX_DECOMPRESS_LENGTH. + +#include <cstdlib> +#include <filesystem> +#include <optional> +#include <sstream> +#include <string> + +#include <boost/test/included/unit_test.hpp> + +#include "Compiler.hh" +#include "DataFile.hh" +#include "Exception.hh" +#include "ValidSchema.hh" + +namespace avro { + +static void setDecompressLimit(const char *value) { +#ifdef _WIN32 + BOOST_REQUIRE_EQUAL(_putenv_s("AVRO_MAX_DECOMPRESS_LENGTH", value), 0); +#else + BOOST_REQUIRE_EQUAL(setenv("AVRO_MAX_DECOMPRESS_LENGTH", value, 1), 0); +#endif +} + +static void unsetDecompressLimit() { +#ifdef _WIN32 + BOOST_REQUIRE_EQUAL(_putenv_s("AVRO_MAX_DECOMPRESS_LENGTH", ""), 0); +#else + BOOST_REQUIRE_EQUAL(unsetenv("AVRO_MAX_DECOMPRESS_LENGTH"), 0); +#endif +} + +// Saves AVRO_MAX_DECOMPRESS_LENGTH on construction and restores it on +// destruction, so a test's override does not leak into other test cases that +// share the process. +struct DecompressLimitGuard { + std::optional<std::string> previous; + DecompressLimitGuard() { + const char *env = std::getenv("AVRO_MAX_DECOMPRESS_LENGTH"); + if (env != nullptr) { + previous = std::string(env); + } + } + ~DecompressLimitGuard() { + if (previous) { + setDecompressLimit(previous->c_str()); + } else { + unsetDecompressLimit(); + } + } +}; + +static ValidSchema stringSchema() { + std::istringstream iss("\"string\""); + ValidSchema vs; + compileJsonSchema(iss, vs); + return vs; +} + +static std::string tempFile(const char *name) { + return (std::filesystem::temp_directory_path() / name).string(); +} + +// Write a single, highly compressible value with the given codec, then read it +// back with a small decompression limit and confirm the read is rejected. +static void checkCodecRejectsOversized(Codec codec, const char *name) { + DecompressLimitGuard guard; + ValidSchema schema = stringSchema(); + std::string path = tempFile(name); + std::string big(4 * 1024 * 1024, 'a'); // 4 MiB, compresses tiny + + // Let any writer exception propagate: a failure here is a real problem + // (e.g. permissions or I/O), not a reason to silently skip. Codecs that are + // not compiled in are excluded via the #ifdef guards on the callers below. + { + DataFileWriter<std::string> writer(path.c_str(), schema, 64 * 1024 * 1024, codec); + writer.write(big); + writer.close(); + } + + setDecompressLimit("1048576"); // 1 MiB, smaller than the 4 MiB block + + bool rejected = false; + try { + DataFileReader<std::string> reader(path.c_str(), schema); + std::string out; + reader.read(out); // triggers block decompression + } catch (const Exception &e) { + // Assert it failed specifically because of the decompression limit, not + // an unrelated I/O or corruption error that would give a false positive. + rejected = std::string(e.what()).find("exceeds the maximum allowed") != std::string::npos; + BOOST_CHECK_MESSAGE(rejected, + std::string("unexpected exception for ") + name + ": " + e.what()); + } + BOOST_CHECK(std::filesystem::remove(path)); + BOOST_CHECK_MESSAGE(rejected, std::string("codec not bounded: ") + name); +} + +static void testDeflateDecompressionLimit() { + checkCodecRejectsOversized(DEFLATE_CODEC, "avro_decompress_limit_deflate.avro"); +} + +static void testSnappyDecompressionLimit() { +#ifdef SNAPPY_CODEC_AVAILABLE + checkCodecRejectsOversized(SNAPPY_CODEC, "avro_decompress_limit_snappy.avro"); +#else + BOOST_TEST_MESSAGE("Snappy codec not available; skipping"); +#endif +} + +static void testZstdDecompressionLimit() { +#ifdef ZSTD_CODEC_AVAILABLE + checkCodecRejectsOversized(ZSTD_CODEC, "avro_decompress_limit_zstd.avro"); +#else + BOOST_TEST_MESSAGE("Zstandard codec not available; skipping"); +#endif +} + +static void testWithinLimitStillReads() { + DecompressLimitGuard guard; + ValidSchema schema = stringSchema(); + std::string path = tempFile("avro_decompress_within_limit.avro"); + std::string payload = "hello world"; + + { + DataFileWriter<std::string> writer(path.c_str(), schema, 64 * 1024 * 1024, DEFLATE_CODEC); + writer.write(payload); + writer.close(); + } + + setDecompressLimit("1048576"); Review Comment: Done — the within-limit test's setDecompressLimit is now BOOST_REQUIRE_EQUAL(..., 0). ########## lang/c++/test/DecompressionLimitTests.cc: ########## @@ -0,0 +1,177 @@ +/* + * 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. + */ + +// AVRO-4285: a data-file block is decompressed according to the file's codec. A +// block with a very high compression ratio (or a malformed block) 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, which +// these tests set to a small value via AVRO_MAX_DECOMPRESS_LENGTH. + +#include <cstdlib> +#include <filesystem> +#include <optional> +#include <sstream> +#include <string> + +#include <boost/test/included/unit_test.hpp> + +#include "Compiler.hh" +#include "DataFile.hh" +#include "Exception.hh" +#include "ValidSchema.hh" + +namespace avro { + +static void setDecompressLimit(const char *value) { +#ifdef _WIN32 + _putenv_s("AVRO_MAX_DECOMPRESS_LENGTH", value); +#else + setenv("AVRO_MAX_DECOMPRESS_LENGTH", value, 1); +#endif +} + +static void unsetDecompressLimit() { +#ifdef _WIN32 + _putenv_s("AVRO_MAX_DECOMPRESS_LENGTH", ""); +#else + unsetenv("AVRO_MAX_DECOMPRESS_LENGTH"); +#endif +} + +// Saves AVRO_MAX_DECOMPRESS_LENGTH on construction and restores it on +// destruction, so a test's override does not leak into other test cases that +// share the process. +struct DecompressLimitGuard { + std::optional<std::string> previous; + DecompressLimitGuard() { + const char *env = std::getenv("AVRO_MAX_DECOMPRESS_LENGTH"); + if (env != nullptr) { + previous = std::string(env); + } + } + ~DecompressLimitGuard() { + if (previous) { + setDecompressLimit(previous->c_str()); + } else { + unsetDecompressLimit(); + } + } +}; + +static ValidSchema stringSchema() { + std::istringstream iss("\"string\""); + ValidSchema vs; + compileJsonSchema(iss, vs); + return vs; +} + +static std::string tempFile(const char *name) { + return (std::filesystem::temp_directory_path() / name).string(); +} + +// Write a single, highly compressible value with the given codec, then read it +// back with a small decompression limit and confirm the read is rejected. +static void checkCodecRejectsOversized(Codec codec, const char *name) { + DecompressLimitGuard guard; + ValidSchema schema = stringSchema(); + std::string path = tempFile(name); + std::string big(4 * 1024 * 1024, 'a'); // 4 MiB, compresses tiny + + // Let any writer exception propagate: a failure here is a real problem + // (e.g. permissions or I/O), not a reason to silently skip. Codecs that are + // not compiled in are excluded via the #ifdef guards on the callers below. + { + DataFileWriter<std::string> writer(path.c_str(), schema, 64 * 1024 * 1024, codec); + writer.write(big); + writer.close(); + } + + setDecompressLimit("1048576"); // 1 MiB, smaller than the 4 MiB block + + bool rejected = false; + try { + DataFileReader<std::string> reader(path.c_str(), schema); + std::string out; + reader.read(out); // triggers block decompression + } catch (const Exception &e) { + // Assert it failed specifically because of the decompression limit, not + // an unrelated I/O or corruption error that would give a false positive. + rejected = std::string(e.what()).find("exceeds the maximum allowed") != std::string::npos; + BOOST_CHECK_MESSAGE(rejected, + std::string("unexpected exception for ") + name + ": " + e.what()); + } + std::filesystem::remove(path); Review Comment: Already asserted — cleanup uses BOOST_CHECK(std::filesystem::remove(path)). ########## lang/c++/test/DecompressionLimitTests.cc: ########## @@ -0,0 +1,177 @@ +/* + * 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. + */ + +// AVRO-4285: a data-file block is decompressed according to the file's codec. A +// block with a very high compression ratio (or a malformed block) 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, which +// these tests set to a small value via AVRO_MAX_DECOMPRESS_LENGTH. + +#include <cstdlib> +#include <filesystem> +#include <optional> +#include <sstream> +#include <string> + +#include <boost/test/included/unit_test.hpp> + +#include "Compiler.hh" +#include "DataFile.hh" +#include "Exception.hh" +#include "ValidSchema.hh" + +namespace avro { + +static void setDecompressLimit(const char *value) { +#ifdef _WIN32 + _putenv_s("AVRO_MAX_DECOMPRESS_LENGTH", value); +#else + setenv("AVRO_MAX_DECOMPRESS_LENGTH", value, 1); +#endif +} + +static void unsetDecompressLimit() { +#ifdef _WIN32 + _putenv_s("AVRO_MAX_DECOMPRESS_LENGTH", ""); +#else + unsetenv("AVRO_MAX_DECOMPRESS_LENGTH"); +#endif +} + +// Saves AVRO_MAX_DECOMPRESS_LENGTH on construction and restores it on +// destruction, so a test's override does not leak into other test cases that +// share the process. +struct DecompressLimitGuard { + std::optional<std::string> previous; + DecompressLimitGuard() { + const char *env = std::getenv("AVRO_MAX_DECOMPRESS_LENGTH"); + if (env != nullptr) { + previous = std::string(env); + } + } + ~DecompressLimitGuard() { + if (previous) { + setDecompressLimit(previous->c_str()); + } else { + unsetDecompressLimit(); + } + } +}; + +static ValidSchema stringSchema() { + std::istringstream iss("\"string\""); + ValidSchema vs; + compileJsonSchema(iss, vs); + return vs; +} + +static std::string tempFile(const char *name) { + return (std::filesystem::temp_directory_path() / name).string(); +} + +// Write a single, highly compressible value with the given codec, then read it +// back with a small decompression limit and confirm the read is rejected. +static void checkCodecRejectsOversized(Codec codec, const char *name) { + DecompressLimitGuard guard; + ValidSchema schema = stringSchema(); + std::string path = tempFile(name); + std::string big(4 * 1024 * 1024, 'a'); // 4 MiB, compresses tiny + + // Let any writer exception propagate: a failure here is a real problem + // (e.g. permissions or I/O), not a reason to silently skip. Codecs that are + // not compiled in are excluded via the #ifdef guards on the callers below. + { + DataFileWriter<std::string> writer(path.c_str(), schema, 64 * 1024 * 1024, codec); + writer.write(big); + writer.close(); + } + + setDecompressLimit("1048576"); // 1 MiB, smaller than the 4 MiB block + + bool rejected = false; + try { + DataFileReader<std::string> reader(path.c_str(), schema); + std::string out; + reader.read(out); // triggers block decompression + } catch (const Exception &e) { + // Assert it failed specifically because of the decompression limit, not + // an unrelated I/O or corruption error that would give a false positive. + rejected = std::string(e.what()).find("exceeds the maximum allowed") != std::string::npos; + BOOST_CHECK_MESSAGE(rejected, + std::string("unexpected exception for ") + name + ": " + e.what()); + } + std::filesystem::remove(path); + BOOST_CHECK_MESSAGE(rejected, std::string("codec not bounded: ") + name); +} + +static void testDeflateDecompressionLimit() { + checkCodecRejectsOversized(DEFLATE_CODEC, "avro_decompress_limit_deflate.avro"); +} + +static void testSnappyDecompressionLimit() { +#ifdef SNAPPY_CODEC_AVAILABLE + checkCodecRejectsOversized(SNAPPY_CODEC, "avro_decompress_limit_snappy.avro"); +#else + BOOST_TEST_MESSAGE("Snappy codec not available; skipping"); +#endif +} + +static void testZstdDecompressionLimit() { +#ifdef ZSTD_CODEC_AVAILABLE + checkCodecRejectsOversized(ZSTD_CODEC, "avro_decompress_limit_zstd.avro"); +#else + BOOST_TEST_MESSAGE("Zstandard codec not available; skipping"); +#endif +} + +static void testWithinLimitStillReads() { + DecompressLimitGuard guard; + ValidSchema schema = stringSchema(); + std::string path = tempFile("avro_decompress_within_limit.avro"); + std::string payload = "hello world"; + + { + DataFileWriter<std::string> writer(path.c_str(), schema, 64 * 1024 * 1024, DEFLATE_CODEC); + writer.write(payload); + writer.close(); + } + + setDecompressLimit("1048576"); + + std::string out; + { + DataFileReader<std::string> reader(path.c_str(), schema); + BOOST_CHECK(reader.read(out)); + } + std::filesystem::remove(path); Review Comment: Same — removal is BOOST_CHECK(std::filesystem::remove(path)). ########## lang/c++/impl/DataFile.cc: ########## @@ -627,12 +679,39 @@ void DataFileReaderBase::readDataBlock() { const uint8_t *data; size_t len; + size_t maxLength = maxDecompressLength(); + // zlib tracks output in a uLong (z_stream::total_out), which is only + // 32-bit on some platforms (e.g. Windows). Cap the working limit to + // uLong's range so the buffer-size arithmetic below (which uses + // total_out) cannot wrap when a larger AVRO_MAX_DECOMPRESS_LENGTH is + // configured. + const uLong zlibMax = std::numeric_limits<uLong>::max(); + if (maxLength > zlibMax) { + maxLength = zlibMax; + } while (ret != Z_STREAM_END && st->next(&data, &len)) { zs.avail_in = static_cast<uInt>(len); zs.next_in = const_cast<Bytef *>(data); do { if (zs.total_out == uncompressed.size()) { - uncompressed.resize(uncompressed.size() + zlibBufGrowSize); + // Reject a block that would decompress to more than the + // allowed maximum, before growing the buffer further. + if (uncompressed.size() >= maxLength) { + (void) inflateEnd(&zs); + // At the trigger uncompressed.size() == maxLength and + // inflate still has output, so the block is at least + // maxLength + 1 bytes. Report that (like the snappy/zstd + // errors) so the message is accurate and consistent. + throw Exception( + "Decompressed block size {} exceeds the maximum allowed of {} bytes", + uncompressed.size() + 1, maxLength); Review Comment: Fixed — the deflate over-limit message computes the would-be size with a saturating +1, so it can't wrap to 0 at size_t's maximum. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
