Skip to content
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

fix(lib): Adds a 10 MiB cap to manifest size #167

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
7 changes: 7 additions & 0 deletions sdk/src/main/java/io/opentdf/platform/sdk/TDFReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@

public class TDFReader {

private static final int MAX_MANIFEST_SIZE_MB = 10;
private static final int BYTES_IN_MB = 1024 * 1024;
private static final int MAX_MANIFEST_SIZE_BYTES = MAX_MANIFEST_SIZE_MB * BYTES_IN_MB;
private final ZipReader.Entry manifest;
private final InputStream payload;

Expand All @@ -28,6 +31,10 @@ public TDFReader(SeekableByteChannel tdf) throws IOException {
}

manifest = entries.get(TDF_MANIFEST_FILE_NAME);
if (manifest.getFileSize() > MAX_MANIFEST_SIZE_BYTES) {
throw new SDKException("manifest file too large: " + (manifest.getFileSize() / (double) BYTES_IN_MB) + " MB");
}

payload = entries.get(TDF_PAYLOAD_FILE_NAME).getData();
}

Expand Down
4 changes: 4 additions & 0 deletions sdk/src/main/java/io/opentdf/platform/sdk/ZipReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ public class Entry {
private final String fileName;
final long offsetToLocalHeader;

public long getFileSize() {
return fileSize;
}

private Entry(byte[] fileName, long offsetToLocalHeader, long fileSize) {
this.fileName = new String(fileName, StandardCharsets.UTF_8);
this.offsetToLocalHeader = offsetToLocalHeader;
Expand Down
35 changes: 35 additions & 0 deletions sdk/src/test/java/io/opentdf/platform/sdk/TDFWriterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.SeekableByteChannel;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
Expand Down Expand Up @@ -72,4 +75,36 @@ void simpleTDFCreate() throws IOException {
writer.finish();
fileOutStream.close();
}

@Test
void testLargeManifest() throws IOException {
// Generate a large manifest string more than 10MB
StringBuilder largeManifestBuilder = new StringBuilder();
for (int i = 0; i < ((10 * 1024 * 1024) + 1); i++) {
largeManifestBuilder.append("a");
}
String largeManifest = largeManifestBuilder.toString();

String payload = "Hello, world!";
FileOutputStream fileOutStream = new FileOutputStream("sample.tdf");
TDFWriter writer = new TDFWriter(fileOutStream);
try (var p = writer.payload()) {
new ByteArrayInputStream(payload.getBytes(StandardCharsets.UTF_8)).transferTo(p);
}
writer.appendManifest(largeManifest);
writer.finish();
fileOutStream.close();

// Read the TDF file and check if the manifest is too large
SeekableByteChannel tdfChannel = Files.newByteChannel(Paths.get("sample.tdf"));
try {
TDFReader reader = new TDFReader(tdfChannel);
assertEquals(largeManifest, reader.manifest());
} catch (SDKException e) {
assertTrue(e.getMessage().contains("manifest file too large:"));
} finally {
Files.deleteIfExists(Paths.get("sample.tdf"));
}
}

}
Loading