Skip to content

Commit

Permalink
vuln-fix: Temporary File Information Disclosure (#18)
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]>


Bug-tracker: JLLeitschuh/security-research#18

Co-authored-by: Moderne <[email protected]>
  • Loading branch information
JLLeitschuh and TeamModerne authored Mar 5, 2023
1 parent f8f2633 commit 38da9fa
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Files;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
Expand Down Expand Up @@ -121,7 +122,7 @@ public File download( String url, List<TransferListener> transferListeners, Mess
try
{
// create the landing file in /tmp for the downloaded source archive
downloaded = File.createTempFile( "download-", null );
downloaded = Files.createTempFile( "download-", null ).toFile();

// delete when the JVM exits, to avoid polluting the temp dir...
downloaded.deleteOnExit();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;

import org.apache.maven.shared.utils.io.FileUtils;

Expand Down Expand Up @@ -66,7 +67,7 @@ protected void initFile()
// TODO: Log this in the debug log-level...
if ( unsafeGetFile() == null )
{
File tempFile = File.createTempFile( tempFilePrefix, tempFileSuffix );
File tempFile = Files.createTempFile( tempFilePrefix, tempFileSuffix ).toFile();

if ( tempFileDeleteOnExit )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Collections;

import org.apache.commons.lang3.exception.ExceptionUtils;
Expand Down Expand Up @@ -108,7 +109,7 @@ public void testShouldFailToDownloadMalformedURL()
public void testShouldDownloadFromTempFileWithNoTransferListeners()
throws IOException, DownloadFailedException
{
File tempFile = File.createTempFile( "download-source", "test" );
File tempFile = Files.createTempFile( "download-source", "test" ).toFile();
tempFile.deleteOnExit();

setupDefaultMockConfiguration();
Expand All @@ -125,7 +126,7 @@ public void testShouldDownloadFromTempFileWithNoTransferListeners()
public void testShouldDownloadFromTempFileTwiceAndUseCache()
throws IOException, DownloadFailedException
{
File tempFile = File.createTempFile( "download-source", "test" );
File tempFile = Files.createTempFile( "download-source", "test" ).toFile();
tempFile.deleteOnExit();

setupDefaultMockConfiguration();
Expand All @@ -150,7 +151,7 @@ public void testShouldDownloadFromTempFileTwiceAndUseCache()
public void testShouldDownloadFromTempFileWithOneTransferListener()
throws IOException, DownloadFailedException
{
File tempFile = File.createTempFile( "download-source", "test" );
File tempFile = Files.createTempFile( "download-source", "test" ).toFile();
tempFile.deleteOnExit();

setupDefaultMockConfiguration();
Expand All @@ -174,7 +175,7 @@ public void testShouldDownloadFromTempFileWithOneTransferListener()
public void testShouldFailToDownloadWhenWagonProtocolNotFound()
throws IOException
{
File tempFile = File.createTempFile( "download-source", "test" );
File tempFile = Files.createTempFile( "download-source", "test" ).toFile();
tempFile.deleteOnExit();

setupMocksWithWagonManagerGetException( new UnsupportedProtocolException( "not supported" ) );
Expand All @@ -200,7 +201,7 @@ public void testShouldFailToDownloadWhenWagonProtocolNotFound()
public void testShouldFailToDownloadWhenWagonConnectThrowsConnectionException()
throws IOException
{
File tempFile = File.createTempFile( "download-source", "test" );
File tempFile = Files.createTempFile( "download-source", "test" ).toFile();
tempFile.deleteOnExit();

setupMocksWithWagonConnectionException( new ConnectionException( "connect error" ) );
Expand All @@ -226,7 +227,7 @@ public void testShouldFailToDownloadWhenWagonConnectThrowsConnectionException()
public void testShouldFailToDownloadWhenWagonConnectThrowsAuthenticationException()
throws IOException
{
File tempFile = File.createTempFile( "download-source", "test" );
File tempFile = Files.createTempFile( "download-source", "test" ).toFile();
tempFile.deleteOnExit();

setupMocksWithWagonConnectionException( new AuthenticationException( "bad credentials" ) );
Expand All @@ -252,7 +253,7 @@ public void testShouldFailToDownloadWhenWagonConnectThrowsAuthenticationExceptio
public void testShouldFailToDownloadWhenWagonGetThrowsTransferFailedException()
throws IOException
{
File tempFile = File.createTempFile( "download-source", "test" );
File tempFile = Files.createTempFile( "download-source", "test" ).toFile();
tempFile.deleteOnExit();

setupMocksWithWagonGetException( new TransferFailedException( "bad transfer" ) );
Expand All @@ -278,7 +279,7 @@ public void testShouldFailToDownloadWhenWagonGetThrowsTransferFailedException()
public void testShouldFailToDownloadWhenWagonGetThrowsResourceDoesNotExistException()
throws IOException
{
File tempFile = File.createTempFile( "download-source", "test" );
File tempFile = Files.createTempFile( "download-source", "test" ).toFile();
tempFile.deleteOnExit();

setupMocksWithWagonGetException( new ResourceDoesNotExistException( "bad resource" ) );
Expand All @@ -304,7 +305,7 @@ public void testShouldFailToDownloadWhenWagonGetThrowsResourceDoesNotExistExcept
public void testShouldFailToDownloadWhenWagonGetThrowsAuthorizationException()
throws IOException
{
File tempFile = File.createTempFile( "download-source", "test" );
File tempFile = Files.createTempFile( "download-source", "test" ).toFile();
tempFile.deleteOnExit();

setupMocksWithWagonGetException( new AuthorizationException( "bad transfer" ) );
Expand All @@ -330,7 +331,7 @@ public void testShouldFailToDownloadWhenWagonGetThrowsAuthorizationException()
public void testShouldFailToDownloadWhenWagonDisconnectThrowsConnectionException()
throws IOException, DownloadFailedException
{
File tempFile = File.createTempFile( "download-source", "test" );
File tempFile = Files.createTempFile( "download-source", "test" ).toFile();
tempFile.deleteOnExit();

setupMocksWithWagonDisconnectException( new ConnectionException( "not connected" ) );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

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

import junit.framework.TestCase;

Expand All @@ -37,7 +38,7 @@ public class ArtifactLocationTest
public void testShouldConstructFromTempFileSpecification()
throws IOException
{
File f = File.createTempFile( "artifact-location.", ".test" );
File f = Files.createTempFile( "artifact-location.", ".test" ).toFile();
f.deleteOnExit();

Artifact a = new DefaultArtifact( "group", "artifact", VersionRange.createFromVersion( "1" ), null, "jar",
Expand All @@ -53,7 +54,7 @@ public void testShouldConstructFromTempFileSpecification()
public void testShouldRead()
throws IOException
{
File f = File.createTempFile( "url-location.", ".test" );
File f = Files.createTempFile( "url-location.", ".test" ).toFile();
f.deleteOnExit();

String testStr = "This is a test";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Collections;

import junit.framework.TestCase;
Expand Down Expand Up @@ -106,7 +107,7 @@ public void testShouldFailToResolveSpecWithTwoTokens()
public void testShouldResolveSpecWithThreeTokensUsingDefaultType()
throws IOException
{
File tempFile = File.createTempFile( "artifact-location.", ".temp" );
File tempFile = Files.createTempFile( "artifact-location.", ".temp" ).toFile();
tempFile.deleteOnExit();

Artifact artifact = createMock( Artifact.class );
Expand Down Expand Up @@ -150,7 +151,7 @@ public void testShouldResolveSpecWithThreeTokensUsingDefaultType()
public void testShouldResolveSpecWithThreeTokensUsingCustomizedDefaultType()
throws IOException
{
File tempFile = File.createTempFile( "artifact-location.", ".temp" );
File tempFile = Files.createTempFile( "artifact-location.", ".temp" ).toFile();
tempFile.deleteOnExit();

Artifact artifact = createMock( Artifact.class );
Expand Down Expand Up @@ -194,7 +195,7 @@ public void testShouldResolveSpecWithThreeTokensUsingCustomizedDefaultType()
public void testShouldResolveSpecWithFourTokens()
throws IOException
{
File tempFile = File.createTempFile( "artifact-location.", ".temp" );
File tempFile = Files.createTempFile( "artifact-location.", ".temp" ).toFile();
tempFile.deleteOnExit();

Artifact artifact = createMock( Artifact.class );
Expand Down Expand Up @@ -238,7 +239,7 @@ public void testShouldResolveSpecWithFourTokens()
public void testShouldResolveSpecWithFiveTokens()
throws IOException
{
File tempFile = File.createTempFile( "artifact-location.", ".temp" );
File tempFile = Files.createTempFile( "artifact-location.", ".temp" ).toFile();
tempFile.deleteOnExit();

Artifact artifact = createMock( Artifact.class );
Expand Down Expand Up @@ -283,7 +284,7 @@ public void testShouldResolveSpecWithFiveTokens()
public void testShouldResolveSpecWithFiveTokensAndEmptyTypeToken()
throws IOException
{
File tempFile = File.createTempFile( "artifact-location.", ".temp" );
File tempFile = Files.createTempFile( "artifact-location.", ".temp" ).toFile();
tempFile.deleteOnExit();

Artifact artifact = createMock( Artifact.class );
Expand Down Expand Up @@ -328,7 +329,7 @@ public void testShouldResolveSpecWithFiveTokensAndEmptyTypeToken()
public void testShouldResolveSpecWithMoreThanFiveTokens()
throws IOException
{
File tempFile = File.createTempFile( "artifact-location.", ".temp" );
File tempFile = Files.createTempFile( "artifact-location.", ".temp" ).toFile();
tempFile.deleteOnExit();

Artifact artifact = createMock( Artifact.class );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.file.Files;

import junit.framework.TestCase;

Expand All @@ -37,7 +38,7 @@ public class FileLocationTest

public void testShouldConstructWithFileThenRetrieveSameFile() throws IOException
{
File file = File.createTempFile( "test.", ".file-location" );
File file = Files.createTempFile( "test.", ".file-location" ).toFile();
file.deleteOnExit();

FileLocation location = new FileLocation( file, file.getAbsolutePath() );
Expand All @@ -48,7 +49,7 @@ public void testShouldConstructWithFileThenRetrieveSameFile() throws IOException

public void testShouldReadFileContentsUsingByteBuffer() throws IOException
{
File file = File.createTempFile( "test.", ".file-location" );
File file = Files.createTempFile( "test.", ".file-location" ).toFile();
file.deleteOnExit();

String testStr = "This is a test";
Expand All @@ -67,7 +68,7 @@ public void testShouldReadFileContentsUsingByteBuffer() throws IOException

public void testShouldReadFileContentsUsingStream() throws IOException
{
File file = File.createTempFile( "test.", ".file-location" );
File file = Files.createTempFile( "test.", ".file-location" ).toFile();
file.deleteOnExit();

String testStr = "This is a test";
Expand All @@ -88,7 +89,7 @@ public void testShouldReadFileContentsUsingStream() throws IOException

public void testShouldReadFileContentsUsingByteArray() throws IOException
{
File file = File.createTempFile( "test.", ".file-location" );
File file = Files.createTempFile( "test.", ".file-location" ).toFile();
file.deleteOnExit();

String testStr = "This is a test";
Expand All @@ -107,7 +108,7 @@ public void testShouldReadFileContentsUsingByteArray() throws IOException

public void testShouldReadThenClose() throws IOException
{
File file = File.createTempFile( "test.", ".file-location" );
File file = Files.createTempFile( "test.", ".file-location" ).toFile();
file.deleteOnExit();

String testStr = "This is a test";
Expand All @@ -128,7 +129,7 @@ public void testShouldReadThenClose() throws IOException

public void testShouldOpenThenFailToSetFile() throws IOException
{
File file = File.createTempFile( "test.", ".file-location" );
File file = Files.createTempFile( "test.", ".file-location" ).toFile();
file.deleteOnExit();

TestFileLocation location = new TestFileLocation( file.getAbsolutePath() );
Expand All @@ -148,7 +149,7 @@ public void testShouldOpenThenFailToSetFile() throws IOException

public void testShouldConstructWithoutFileThenSetFileThenOpen() throws IOException
{
File file = File.createTempFile( "test.", ".file-location" );
File file = Files.createTempFile( "test.", ".file-location" ).toFile();
file.deleteOnExit();

TestFileLocation location = new TestFileLocation( file.getAbsolutePath() );
Expand All @@ -159,7 +160,7 @@ public void testShouldConstructWithoutFileThenSetFileThenOpen() throws IOExcepti

public void testShouldConstructWithLocationThenRetrieveEquivalentFile() throws IOException
{
File file = File.createTempFile( "test.", ".file-location" );
File file = Files.createTempFile( "test.", ".file-location" ).toFile();
file.deleteOnExit();

Location location = new TestFileLocation( file.getAbsolutePath() );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

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

import org.apache.maven.shared.io.logging.DefaultMessageHolder;
import org.apache.maven.shared.io.logging.MessageHolder;
Expand All @@ -33,7 +34,7 @@ public class FileLocatorStrategyTest

public void testShouldResolveExistingTempFileLocation() throws IOException
{
File f = File.createTempFile( "file-locator.", ".test" );
File f = Files.createTempFile( "file-locator.", ".test" ).toFile();
f.deleteOnExit();

FileLocatorStrategy fls = new FileLocatorStrategy();
Expand All @@ -51,7 +52,7 @@ public void testShouldResolveExistingTempFileLocation() throws IOException

public void testShouldFailToResolveNonExistentFileLocation() throws IOException
{
File f = File.createTempFile( "file-locator.", ".test" );
File f = Files.createTempFile( "file-locator.", ".test" ).toFile();
f.delete();

FileLocatorStrategy fls = new FileLocatorStrategy();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;

import org.apache.commons.io.FileUtils;

Expand All @@ -33,7 +34,7 @@ public class URLLocationTest

public void testShouldConstructFromUrlAndTempFileSpecifications() throws IOException
{
File f = File.createTempFile( "url-location.", ".test" );
File f = Files.createTempFile( "url-location.", ".test" ).toFile();
f.deleteOnExit();

URL url = f.toURL();
Expand All @@ -43,7 +44,7 @@ public void testShouldConstructFromUrlAndTempFileSpecifications() throws IOExcep

public void testShouldTransferFromTempFile() throws IOException
{
File f = File.createTempFile( "url-location.", ".test" );
File f = Files.createTempFile( "url-location.", ".test" ).toFile();
f.deleteOnExit();

URL url = f.toURL();
Expand All @@ -56,7 +57,7 @@ public void testShouldTransferFromTempFile() throws IOException

public void testShouldTransferFromTempFileThenRead() throws IOException
{
File f = File.createTempFile( "url-location.", ".test" );
File f = Files.createTempFile( "url-location.", ".test" ).toFile();
f.deleteOnExit();

String testStr = "This is a test";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

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

import junit.framework.TestCase;

Expand Down Expand Up @@ -54,7 +55,7 @@ public void testShouldFailToResolveWithMalformedUrl()

public void testShouldResolveUrlForTempFile() throws IOException
{
File tempFile = File.createTempFile( "prefix.", ".suffix" );
File tempFile = Files.createTempFile( "prefix.", ".suffix" ).toFile();
tempFile.deleteOnExit();

String testStr = "This is a test.";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.HashSet;
import java.util.Set;

Expand All @@ -41,7 +42,7 @@ public void testGetIncludedSources() throws IOException, InclusionScanException
File baseDir = new File("target" );
baseDir.deleteOnExit();
baseDir.mkdirs();
File f = File.createTempFile( "source1.", ".test", baseDir );
File f = Files.createTempFile( baseDir.toPath(), "source1.", ".test" ).toFile();

Set<String> sourceIncludes = new HashSet<>();
sourceIncludes.add( "**/*" + f.getName() );
Expand Down

0 comments on commit 38da9fa

Please sign in to comment.