Hardening suggestions for Stirling-PDF / junit (#3538)

I've reviewed the recently opened PR ([3537 - JUnits JUnits JUnits, so
many JUnits](https://github.com/Stirling-Tools/Stirling-PDF/pull/3537))
and have identified some area(s) that could benefit from additional
hardening measures.

These changes should help prevent potential security vulnerabilities and
improve overall code quality.

Thank you for your consideration!
🧚🤖  Powered by Pixeebot  

[Feedback](https://ask.pixee.ai/feedback) |
[Community](https://pixee-community.slack.com/signup#/domain-signup) |
[Docs](https://docs.pixee.ai/)
![](https://d1zaessa2hpsmj.cloudfront.net/pixel/v1/track?writeKey=2PI43jNm7atYvAuK7rJUz3Kcd6A&event=PR_HARDENING%7CStirling-Tools%2FStirling-PDF%7C0ea58acaa24a5b4d77853bbce9cc80196a92dda4)

---------

Co-authored-by: pixeebot[bot] <104101892+pixeebot[bot]@users.noreply.github.com>
This commit is contained in:
pixeebot[bot] 2025-05-19 10:15:46 +01:00 committed by GitHub
parent 25bfdb1daa
commit ff42a18392
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 10 additions and 8 deletions

View File

@ -1,5 +1,6 @@
package stirling.software.SPDF.service; package stirling.software.SPDF.service;
import java.nio.file.Files;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*; import static org.mockito.Mockito.*;
@ -233,7 +234,7 @@ class CustomPDFDocumentFactoryTest {
} }
private File writeTempFile(byte[] content) throws IOException { private File writeTempFile(byte[] content) throws IOException {
File file = File.createTempFile("pdf-test-", ".pdf"); File file = Files.createTempFile("pdf-test-", ".pdf").toFile();
Files.write(file.toPath(), content); Files.write(file.toPath(), content);
return file; return file;
} }

View File

@ -1,5 +1,6 @@
package stirling.software.SPDF.utils; package stirling.software.SPDF.utils;
import io.github.pixee.security.ZipSecurity;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
@ -213,7 +214,7 @@ class PDFToFileTest {
// Verify the content by unzipping it // Verify the content by unzipping it
try (ZipInputStream zipStream = try (ZipInputStream zipStream =
new ZipInputStream(new java.io.ByteArrayInputStream(response.getBody()))) { ZipSecurity.createHardenedInputStream(new java.io.ByteArrayInputStream(response.getBody()))) {
ZipEntry entry; ZipEntry entry;
boolean foundMdFiles = false; boolean foundMdFiles = false;
boolean foundImage = false; boolean foundImage = false;
@ -285,18 +286,18 @@ class PDFToFileTest {
// Verify the content by unzipping it // Verify the content by unzipping it
try (ZipInputStream zipStream = try (ZipInputStream zipStream =
new ZipInputStream(new java.io.ByteArrayInputStream(response.getBody()))) { ZipSecurity.createHardenedInputStream(new java.io.ByteArrayInputStream(response.getBody()))) {
ZipEntry entry; ZipEntry entry;
boolean foundMainHtml = false; boolean foundMainHtml = false;
boolean foundIndexHtml = false; boolean foundIndexHtml = false;
boolean foundImage = false; boolean foundImage = false;
while ((entry = zipStream.getNextEntry()) != null) { while ((entry = zipStream.getNextEntry()) != null) {
if (entry.getName().equals("test.html")) { if ("test.html".equals(entry.getName())) {
foundMainHtml = true; foundMainHtml = true;
} else if (entry.getName().equals("test_ind.html")) { } else if ("test_ind.html".equals(entry.getName())) {
foundIndexHtml = true; foundIndexHtml = true;
} else if (entry.getName().equals("test_img.png")) { } else if ("test_img.png".equals(entry.getName())) {
foundImage = true; foundImage = true;
} }
zipStream.closeEntry(); zipStream.closeEntry();
@ -436,13 +437,13 @@ class PDFToFileTest {
// Verify the content by unzipping it // Verify the content by unzipping it
try (ZipInputStream zipStream = try (ZipInputStream zipStream =
new ZipInputStream(new java.io.ByteArrayInputStream(response.getBody()))) { ZipSecurity.createHardenedInputStream(new java.io.ByteArrayInputStream(response.getBody()))) {
ZipEntry entry; ZipEntry entry;
boolean foundMainFile = false; boolean foundMainFile = false;
boolean foundMediaFiles = false; boolean foundMediaFiles = false;
while ((entry = zipStream.getNextEntry()) != null) { while ((entry = zipStream.getNextEntry()) != null) {
if (entry.getName().equals("document.odp")) { if ("document.odp".equals(entry.getName())) {
foundMainFile = true; foundMainFile = true;
} else if (entry.getName().startsWith("document_media")) { } else if (entry.getName().startsWith("document_media")) {
foundMediaFiles = true; foundMediaFiles = true;