Skip to content

Commit

Permalink
vuln-fix: Temporary File Information Disclosure
Browse files Browse the repository at this point in the history
This fixes temporary file information disclosure vulnerability due to the use
of the vulnerable `File.createTempFile()` method. The vulnerability is fixed by
using the `Files.createTempFile()` method which sets the correct posix permissions.

Weakness: CWE-377: Insecure Temporary File
Severity: Medium
CVSSS: 5.5
Detection: CodeQL & OpenRewrite (https://public.moderne.io/recipes/org.openrewrite.java.security.SecureTempFileCreation)

Reported-by: Jonathan Leitschuh <[email protected]>
Signed-off-by: Jonathan Leitschuh <[email protected]>

Bug-tracker: JLLeitschuh/security-research#18


Co-authored-by: Moderne <[email protected]>
  • Loading branch information
2 people authored and asturio committed Apr 5, 2023
1 parent be7e7bd commit e74c1c9
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 3 deletions.
3 changes: 2 additions & 1 deletion openpdf/src/main/java/com/lowagie/text/pdf/PdfStamper.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.security.SignatureException;
import java.security.cert.Certificate;
import java.util.Calendar;
Expand Down Expand Up @@ -710,7 +711,7 @@ public static PdfStamper createSignature(PdfReader reader, OutputStream os, char
}
else {
if (tempFile.isDirectory())
tempFile = File.createTempFile("pdf", null, tempFile);
tempFile = Files.createTempFile(tempFile.toPath(), "pdf", null).toFile();
FileOutputStream fout = new FileOutputStream(tempFile);
stp = new PdfStamper(reader, fout, pdfVersion, append);
stp.sigApp = new PdfSignatureAppearance(stp.stamper);
Expand Down
3 changes: 2 additions & 1 deletion openpdf/src/test/java/com/lowagie/text/pdf/LargePdfTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import java.io.File;
import java.io.FileOutputStream;
import java.nio.file.Files;

/**
* This will create a file which is ~14GB, then attempt to read it.
Expand All @@ -20,7 +21,7 @@ public class LargePdfTest {
@Disabled
@Test
void writeLargePdf() throws Exception {
File largeFile = File.createTempFile("largePDFFile", ".pdf");
File largeFile = Files.createTempFile("largePDFFile", ".pdf").toFile();
Document document = PdfTestBase.createPdf(
new FileOutputStream(largeFile));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.File;
import java.io.FileOutputStream;
import java.nio.file.Files;

import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -34,7 +35,7 @@ void testSimplePdf() throws Exception {
@Test
void testTryWithResources_with_os_before_doc() throws Exception {
try (PdfReader reader = new PdfReader("./src/test/resources/HelloWorldMeta.pdf");
FileOutputStream os = new FileOutputStream(File.createTempFile("temp-file-name", ".pdf"));
FileOutputStream os = new FileOutputStream(Files.createTempFile("temp-file-name", ".pdf").toFile());
Document document = new Document()
) {
PdfWriter writer = PdfWriter.getInstance(document, os);
Expand Down

0 comments on commit e74c1c9

Please sign in to comment.