vuln-fix: Temporary File Information Disclosure

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 <Jonathan.Leitschuh@gmail.com>
Signed-off-by: Jonathan Leitschuh <Jonathan.Leitschuh@gmail.com>

Bug-tracker: https://github.com/JLLeitschuh/security-research/issues/18


Co-authored-by: Moderne <team@moderne.io>
This commit is contained in:
Jonathan Leitschuh 2022-11-19 03:01:07 +00:00 committed by Andrew Gaul
parent 25bcb7961e
commit b282b5cbfe
5 changed files with 11 additions and 6 deletions

View File

@ -27,6 +27,7 @@ import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import javax.ws.rs.core.HttpHeaders; import javax.ws.rs.core.HttpHeaders;
@ -96,7 +97,7 @@ public class MiscApiMockTest extends BaseDockerMockTest {
public void testBuildContainerUsingPayload() throws Exception { public void testBuildContainerUsingPayload() throws Exception {
MockWebServer server = mockWebServer(new MockResponse().setResponseCode(200)); MockWebServer server = mockWebServer(new MockResponse().setResponseCode(200));
MiscApi api = api(DockerApi.class, server.url("/").toString()).getMiscApi(); MiscApi api = api(DockerApi.class, server.url("/").toString()).getMiscApi();
File file = File.createTempFile("docker", "tmp"); File file = Files.createTempFile("docker", "tmp").toFile();
FileInputStream data = new FileInputStream(file); FileInputStream data = new FileInputStream(file);
Payload payload = Payloads.newInputStreamPayload(data); Payload payload = Payloads.newInputStreamPayload(data);
payload.getContentMetadata().setContentLength(file.length()); payload.getContentMetadata().setContentLength(file.length());

View File

@ -31,6 +31,7 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.URLDecoder; import java.net.URLDecoder;
import java.nio.file.Files;
import java.util.Random; import java.util.Random;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -268,7 +269,7 @@ public abstract class BaseHttpCommandExecutorServiceIntegrationTest extends Base
Payload payload = null; Payload payload = null;
try { try {
f = File.createTempFile("jclouds", "tmp"); f = Files.createTempFile("jclouds", "tmp").toFile();
long length = (new Random().nextInt(32) + 1) * 1024L * 1024L; long length = (new Random().nextInt(32) + 1) * 1024L * 1024L;
TestUtils.randomByteSource().slice(0, length).copyTo(Files.asByteSink(f)); TestUtils.randomByteSource().slice(0, length).copyTo(Files.asByteSink(f));

View File

@ -43,6 +43,7 @@ import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; import java.lang.annotation.Target;
import java.net.URI; import java.net.URI;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.nio.file.Files;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
@ -1036,7 +1037,7 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest {
public void testMultipartWithParamFilePart() throws Exception { public void testMultipartWithParamFilePart() throws Exception {
Invokable<?, ?> method = method(TestMultipartForm.class, "withParamFilePart", String.class, Invokable<?, ?> method = method(TestMultipartForm.class, "withParamFilePart", String.class,
File.class); File.class);
File file = File.createTempFile("foo", "bar"); File file = Files.createTempFile("foo", "bar").toFile();
try { try {
Files.append("foobledata", file, UTF_8); Files.append("foobledata", file, UTF_8);
@ -1082,7 +1083,7 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest {
public void testMultipartWithParamFileBinaryPart() throws Exception { public void testMultipartWithParamFileBinaryPart() throws Exception {
Invokable<?, ?> method = method(TestMultipartForm.class, "withParamFileBinaryPart", Invokable<?, ?> method = method(TestMultipartForm.class, "withParamFileBinaryPart",
String.class, File.class); String.class, File.class);
File file = File.createTempFile("foo", "bar"); File file = Files.createTempFile("foo", "bar").toFile();
try { try {
Files.write(new byte[] { 17, 26, 39, 40, 50 }, file); Files.write(new byte[] { 17, 26, 39, 40, 50 }, file);

View File

@ -28,6 +28,7 @@ import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.io.PrintStream; import java.io.PrintStream;
import java.net.InetAddress; import java.net.InetAddress;
import java.nio.file.Files;
import java.util.List; import java.util.List;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
@ -161,7 +162,7 @@ public class JschSshClientLiveTest {
@Test @Test
public void testPutAndGet() throws IOException { public void testPutAndGet() throws IOException {
temp = File.createTempFile("foo", "bar"); temp = Files.createTempFile("foo", "bar").toFile();
try { try {
SshClient client = setupClient(); SshClient client = setupClient();
client.put(temp.getAbsolutePath(), Payloads.newStringPayload("rabbit")); client.put(temp.getAbsolutePath(), Payloads.newStringPayload("rabbit"));

View File

@ -26,6 +26,7 @@ import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.io.PrintStream; import java.io.PrintStream;
import java.net.InetAddress; import java.net.InetAddress;
import java.nio.file.Files;
import org.jclouds.compute.domain.ExecChannel; import org.jclouds.compute.domain.ExecChannel;
import org.jclouds.compute.domain.ExecResponse; import org.jclouds.compute.domain.ExecResponse;
@ -148,7 +149,7 @@ public class SshjSshClientLiveTest {
} }
public void testPutAndGet() throws IOException { public void testPutAndGet() throws IOException {
temp = File.createTempFile("foo", "bar"); temp = Files.createTempFile("foo", "bar").toFile();
try { try {
SshClient client = setupClient(); SshClient client = setupClient();
client.put(temp.getAbsolutePath(), Payloads.newStringPayload("rabbit")); client.put(temp.getAbsolutePath(), Payloads.newStringPayload("rabbit"));