wombatu-kun commented on code in PR #16500:
URL: https://github.com/apache/iceberg/pull/16500#discussion_r3315057274
##########
core/src/main/java/org/apache/iceberg/util/ContentFileUtil.java:
##########
@@ -156,6 +156,27 @@ public static String dvDesc(DeleteFile deleteFile) {
deleteFile.referencedDataFile());
}
+ /**
+ * Validates that the deletion-vector offset and length on a {@link
DeleteFile} are well-formed
+ * before they are consumed by a reader. Hostile or corrupted manifest
metadata may otherwise
+ * trigger a {@link NegativeArraySizeException}, an invalid seek, or a
multi-gigabyte allocation
+ * when the DV blob is read.
+ */
+ public static void validateDV(DeleteFile dv) {
+ Preconditions.checkArgument(
+ dv.contentOffset() != null, "Invalid DV, offset cannot be null: %s",
dvDesc(dv));
+ Preconditions.checkArgument(
+ dv.contentSizeInBytes() != null, "Invalid DV, length cannot be null:
%s", dvDesc(dv));
+ Preconditions.checkArgument(
+ dv.contentOffset() >= 0, "Invalid DV, offset must be non-negative:
%s", dvDesc(dv));
+ Preconditions.checkArgument(
+ dv.contentSizeInBytes() >= 0, "Invalid DV, length must be
non-negative: %s", dvDesc(dv));
+ Preconditions.checkArgument(
+ dv.contentSizeInBytes() <= Integer.MAX_VALUE,
Review Comment:
Fixed in f76cef519.
##########
core/src/main/java/org/apache/iceberg/DVUtil.java:
##########
@@ -52,6 +52,7 @@ static PositionDeleteIndex readDV(DeleteFile deleteFile,
FileIO fileIO) {
ContentFileUtil.isDV(deleteFile),
"Cannot read, not a deletion vector: %s",
deleteFile.location());
+ ContentFileUtil.validateDV(deleteFile);
Review Comment:
Done in f76cef519. DVUtil is now public so BaseDeleteLoader (data module)
can call it.
##########
core/src/main/java/org/apache/iceberg/FileMetadata.java:
##########
@@ -263,6 +263,12 @@ public DeleteFile build() {
if (format == FileFormat.PUFFIN) {
Preconditions.checkArgument(contentOffset != null, "Content offset is
required for DV");
Preconditions.checkArgument(contentSizeInBytes != null, "Content size
is required for DV");
+ Preconditions.checkArgument(
+ contentOffset >= 0, "Content offset must be non-negative for DV:
%s", contentOffset);
+ Preconditions.checkArgument(
+ contentSizeInBytes >= 0,
Review Comment:
Done in f76cef519.
##########
data/src/test/java/org/apache/iceberg/TestBaseDeleteLoader.java:
##########
@@ -0,0 +1,86 @@
+/*
+ * 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
+ *
+ * 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.
+ */
+package org.apache.iceberg;
+
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+import org.apache.iceberg.data.BaseDeleteLoader;
+import org.apache.iceberg.data.DeleteLoader;
+import org.apache.iceberg.io.InputFile;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList;
+import org.junit.jupiter.api.Test;
+
+public class TestBaseDeleteLoader {
+
+ private static final String DATA_FILE = "/tmp/data.parquet";
+
+ private static final DeleteFile VALID_DV =
+ FileMetadata.deleteFileBuilder(PartitionSpec.unpartitioned())
+ .ofPositionDeletes()
+ .withFormat(FileFormat.PUFFIN)
+ .withPath("/tmp/dv.puffin")
+ .withFileSizeInBytes(10)
+ .withRecordCount(1)
+ .withReferencedDataFile(DATA_FILE)
+ .withContentOffset(0L)
+ .withContentSizeInBytes(10L)
+ .build();
+
+ @Test
+ public void loadPositionDeletesRejectsNegativeContentOffset() {
+ DeleteFile dv = tamperedDV(-1L, 10L);
+ DeleteLoader loader = new BaseDeleteLoader(file -> failingInputFile());
+
+ assertThatThrownBy(() -> loader.loadPositionDeletes(ImmutableList.of(dv),
DATA_FILE))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessageContaining("offset must be non-negative");
+ }
+
+ @Test
+ public void loadPositionDeletesRejectsNegativeContentSize() {
+ DeleteFile dv = tamperedDV(0L, -1L);
+ DeleteLoader loader = new BaseDeleteLoader(file -> failingInputFile());
+
+ assertThatThrownBy(() -> loader.loadPositionDeletes(ImmutableList.of(dv),
DATA_FILE))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessageContaining("length must be non-negative");
+ }
+
+ // Wraps a well-formed DV but reports tampered (offset, size). Simulates a
corrupted manifest,
+ // since the FileMetadata builder now rejects negative values at
construction time.
+ private static DeleteFile tamperedDV(Long offset, Long size) {
+ return new Delegates.DelegatingDeleteFile(VALID_DV) {
Review Comment:
Done in f76cef519 (Mockito mock).
##########
data/src/test/java/org/apache/iceberg/TestBaseDeleteLoader.java:
##########
@@ -0,0 +1,86 @@
+/*
+ * 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
+ *
+ * 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.
+ */
+package org.apache.iceberg;
+
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+import org.apache.iceberg.data.BaseDeleteLoader;
+import org.apache.iceberg.data.DeleteLoader;
+import org.apache.iceberg.io.InputFile;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList;
+import org.junit.jupiter.api.Test;
+
+public class TestBaseDeleteLoader {
+
+ private static final String DATA_FILE = "/tmp/data.parquet";
+
+ private static final DeleteFile VALID_DV =
+ FileMetadata.deleteFileBuilder(PartitionSpec.unpartitioned())
+ .ofPositionDeletes()
+ .withFormat(FileFormat.PUFFIN)
+ .withPath("/tmp/dv.puffin")
+ .withFileSizeInBytes(10)
+ .withRecordCount(1)
+ .withReferencedDataFile(DATA_FILE)
+ .withContentOffset(0L)
+ .withContentSizeInBytes(10L)
+ .build();
+
+ @Test
+ public void loadPositionDeletesRejectsNegativeContentOffset() {
+ DeleteFile dv = tamperedDV(-1L, 10L);
+ DeleteLoader loader = new BaseDeleteLoader(file -> failingInputFile());
+
+ assertThatThrownBy(() -> loader.loadPositionDeletes(ImmutableList.of(dv),
DATA_FILE))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessageContaining("offset must be non-negative");
+ }
+
+ @Test
+ public void loadPositionDeletesRejectsNegativeContentSize() {
+ DeleteFile dv = tamperedDV(0L, -1L);
+ DeleteLoader loader = new BaseDeleteLoader(file -> failingInputFile());
+
+ assertThatThrownBy(() -> loader.loadPositionDeletes(ImmutableList.of(dv),
DATA_FILE))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessageContaining("length must be non-negative");
+ }
+
+ // Wraps a well-formed DV but reports tampered (offset, size). Simulates a
corrupted manifest,
+ // since the FileMetadata builder now rejects negative values at
construction time.
+ private static DeleteFile tamperedDV(Long offset, Long size) {
+ return new Delegates.DelegatingDeleteFile(VALID_DV) {
+ @Override
+ public Long contentOffset() {
+ return offset;
+ }
+
+ @Override
+ public Long contentSizeInBytes() {
+ return size;
+ }
+ };
+ }
+
+ // The validator must fire before any I/O attempt — if it does not, the
loader will call this
Review Comment:
Done in f76cef519. Removed across the whole branch diff.
##########
core/src/main/java/org/apache/iceberg/DVUtil.java:
##########
@@ -64,6 +65,35 @@ static PositionDeleteIndex readDV(DeleteFile deleteFile,
FileIO fileIO) {
}
}
+ /**
+ * Validates that the deletion-vector offset and length on a {@link
DeleteFile} are well-formed
+ * before they are consumed by a reader. Hostile or corrupted manifest
metadata may otherwise
+ * trigger a {@link NegativeArraySizeException}, an invalid seek, or a
multi-gigabyte allocation
+ * when the DV blob is read.
+ */
Review Comment:
Done 0c4b66d75.
##########
core/src/test/java/org/apache/iceberg/TestFileMetadata.java:
##########
@@ -0,0 +1,118 @@
+/*
+ * 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
+ *
+ * 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.
+ */
+package org.apache.iceberg;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatCode;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+import org.junit.jupiter.api.Test;
+
+public class TestFileMetadata {
+
+ @Test
+ public void dvBuilderRejectsNegativeContentOffset() {
+ assertThatThrownBy(
+ () ->
+ FileMetadata.deleteFileBuilder(PartitionSpec.unpartitioned())
+ .ofPositionDeletes()
+ .withFormat(FileFormat.PUFFIN)
+ .withPath("/tmp/dv.puffin")
+ .withFileSizeInBytes(10)
+ .withRecordCount(1)
+ .withReferencedDataFile("/tmp/data.parquet")
+ .withContentOffset(-1L)
+ .withContentSizeInBytes(10L)
+ .build())
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessageContaining("Content offset must be non-negative for DV");
+ }
+
+ @Test
+ public void dvBuilderRejectsNegativeContentSize() {
+ assertThatThrownBy(
+ () ->
+ FileMetadata.deleteFileBuilder(PartitionSpec.unpartitioned())
+ .ofPositionDeletes()
+ .withFormat(FileFormat.PUFFIN)
+ .withPath("/tmp/dv.puffin")
+ .withFileSizeInBytes(10)
+ .withRecordCount(1)
+ .withReferencedDataFile("/tmp/data.parquet")
+ .withContentOffset(0L)
+ .withContentSizeInBytes(-1L)
+ .build())
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessageContaining("Content size must be non-negative for DV");
+ }
+
+ @Test
+ public void dvBuilderRejectsContentSizeAtIntegerMax() {
+ assertThatThrownBy(
+ () ->
+ FileMetadata.deleteFileBuilder(PartitionSpec.unpartitioned())
+ .ofPositionDeletes()
+ .withFormat(FileFormat.PUFFIN)
+ .withPath("/tmp/dv.puffin")
+ .withFileSizeInBytes(10)
+ .withRecordCount(1)
+ .withReferencedDataFile("/tmp/data.parquet")
+ .withContentOffset(0L)
+ .withContentSizeInBytes(Integer.MAX_VALUE)
+ .build())
Review Comment:
Done 0c4b66d75. Added a validDvBuilder() returning a valid builder; each
test now overrides only the single invalid field.
--
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]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]