diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index ead6a72eaa..d4c060c251 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -8,6 +8,12 @@
+
+ A response that exceeds the max in memory size is stored in a temporary file that is only
+ readable by the owner; so far the file was created in the shared temp directory with the
+ default permissions (world readable on Posix systems). The same for the report folder of
+ the DebuggingWebConnection.
+
core-js: Skip holes of the array returned by the flatMap callback.
diff --git a/src/main/java/org/htmlunit/HttpWebConnection.java b/src/main/java/org/htmlunit/HttpWebConnection.java
index ca97b54566..cee5486726 100644
--- a/src/main/java/org/htmlunit/HttpWebConnection.java
+++ b/src/main/java/org/htmlunit/HttpWebConnection.java
@@ -30,6 +30,7 @@
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.file.Files;
+import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@@ -783,10 +784,14 @@ public static DownloadedContent downloadContent(final InputStream is, final int
while ((nbRead = is.read(buffer)) != -1) {
bos.write(buffer, 0, nbRead);
if (maxInMemory > 0 && bos.size() > maxInMemory) {
- // we have exceeded the max for memory, let's write everything to a temporary file
- final File file = File.createTempFile("htmlunit", ".tmp", tempFileDirectory);
+ // we have exceeded the max for memory, let's write everything to a temporary file;
+ // Files.createTempFile() restricts the access to the owner of the file
+ final Path path = tempFileDirectory == null
+ ? Files.createTempFile("htmlunit", ".tmp")
+ : Files.createTempFile(tempFileDirectory.toPath(), "htmlunit", ".tmp");
+ final File file = path.toFile();
file.deleteOnExit();
- try (OutputStream fos = Files.newOutputStream(file.toPath())) {
+ try (OutputStream fos = Files.newOutputStream(path)) {
bos.writeTo(fos); // what we have already read
IOUtils.copyLarge(is, fos); // what remains from the server response
}
diff --git a/src/main/java/org/htmlunit/util/DebuggingWebConnection.java b/src/main/java/org/htmlunit/util/DebuggingWebConnection.java
index 295f10037b..0f16da2e8d 100644
--- a/src/main/java/org/htmlunit/util/DebuggingWebConnection.java
+++ b/src/main/java/org/htmlunit/util/DebuggingWebConnection.java
@@ -95,7 +95,7 @@ public DebuggingWebConnection(final WebConnection webConnection,
if (reportFolder_.exists()) {
FileUtils.forceDelete(reportFolder_);
}
- FileUtils.forceMkdir(reportFolder_);
+ org.htmlunit.util.FileUtils.createUserOnlyDirectories(reportFolder_.toPath());
javaScriptFile_ = new File(reportFolder_, "hu.js");
createOverview();
}
diff --git a/src/main/java/org/htmlunit/util/FileUtils.java b/src/main/java/org/htmlunit/util/FileUtils.java
index 611b9834e9..6f1e09e30d 100644
--- a/src/main/java/org/htmlunit/util/FileUtils.java
+++ b/src/main/java/org/htmlunit/util/FileUtils.java
@@ -14,6 +14,11 @@
*/
package org.htmlunit.util;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.attribute.PosixFilePermissions;
+
/**
* File utility class for functions not covered by third-party libraries.
*
@@ -79,4 +84,26 @@ public static String sanitizeFilename(final String name) {
private static boolean isTrimmed(final char c) {
return c == '.' || Character.isWhitespace(c);
}
+
+ /**
+ * Creates the given directory and all the nonexistent parent directories; a directory
+ * created by this method is accessible for the owner only ({@code rwx------}). This is
+ * relevant for directories inside the shared temp directory, where the default permissions
+ * (taken from the umask) usually allow every other user of the machine to read the content.
+ * On file systems without posix support the permissions are not set, the directory is
+ * created with the defaults of the platform. Nothing is done if the directory already
+ * exists; the permissions of an existing directory are not changed.
+ *
+ * @param directory the directory to create
+ * @throws IOException if the directory can't be created
+ */
+ public static void createUserOnlyDirectories(final Path directory) throws IOException {
+ if (directory.getFileSystem().supportedFileAttributeViews().contains("posix")) {
+ Files.createDirectories(directory,
+ PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString("rwx------")));
+ return;
+ }
+
+ Files.createDirectories(directory);
+ }
}
diff --git a/src/test/java/org/htmlunit/HttpWebConnectionTest.java b/src/test/java/org/htmlunit/HttpWebConnectionTest.java
index f0848663f9..f7f990f302 100644
--- a/src/test/java/org/htmlunit/HttpWebConnectionTest.java
+++ b/src/test/java/org/htmlunit/HttpWebConnectionTest.java
@@ -27,11 +27,18 @@
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.URL;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.attribute.PosixFilePermissions;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
@@ -548,6 +555,41 @@ public void makeWebResponse() throws Exception {
}
}
+ /**
+ * The response is stored in the shared temp directory as soon as it exceeds the max in memory
+ * size; the content has to be readable for the owner only.
+ * @throws Exception if the test fails
+ */
+ @Test
+ public void downloadContentTempFileAccess() throws Exception {
+ final Path tempDirectory = Files.createTempDirectory("htmlunitTest");
+ try {
+ final byte[] content = "secret response".getBytes(StandardCharsets.UTF_8);
+ final DownloadedContent downloaded = HttpWebConnection.downloadContent(
+ new ByteArrayInputStream(content), 4, tempDirectory.toFile());
+
+ try (InputStream is = downloaded.getInputStream()) {
+ assertEquals(content, IOUtils.toByteArray(is));
+ }
+
+ try (Stream files = Files.list(tempDirectory)) {
+ final List tempFiles = files.collect(Collectors.toList());
+ assertEquals(1, tempFiles.size());
+
+ final Path tempFile = tempFiles.get(0);
+ if (tempFile.getFileSystem().supportedFileAttributeViews().contains("posix")) {
+ assertEquals("rw-------",
+ PosixFilePermissions.toString(Files.getPosixFilePermissions(tempFile)));
+ }
+ }
+
+ downloaded.cleanUp();
+ }
+ finally {
+ FileUtils.deleteDirectory(tempDirectory.toFile());
+ }
+ }
+
/**
* Test for overwriting the
* {@link HttpWebConnection#downloadResponse(HttpUriRequest, WebRequest, HttpResponse, long)}
diff --git a/src/test/java/org/htmlunit/util/DebuggingWebConnectionTest.java b/src/test/java/org/htmlunit/util/DebuggingWebConnectionTest.java
index b873f8df70..842b4ff4c7 100644
--- a/src/test/java/org/htmlunit/util/DebuggingWebConnectionTest.java
+++ b/src/test/java/org/htmlunit/util/DebuggingWebConnectionTest.java
@@ -17,6 +17,9 @@
import static java.nio.charset.StandardCharsets.UTF_8;
import java.io.ByteArrayOutputStream;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.attribute.PosixFilePermissions;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
@@ -88,6 +91,28 @@ public void chooseExtension() throws Exception {
assertEquals(".txt", DebuggingWebConnection.chooseExtension(MimeType.TEXT_PLAIN));
}
+ /**
+ * The report folder is created inside the shared temp directory; the saved responses
+ * (and the request parameters) have to be readable for the owner only.
+ * @throws Exception if the test fails
+ */
+ @Test
+ public void reportFolderAccess() throws Exception {
+ final MockWebConnection mockConnection = new MockWebConnection();
+ mockConnection.setResponse(URL_FIRST, "");
+
+ final String dirName = "test-" + getClass().getSimpleName() + "-access";
+ try (DebuggingWebConnection dwc = new DebuggingWebConnection(mockConnection, dirName)) {
+ final Path reportFolder = dwc.getReportFolder().toPath();
+ if (reportFolder.getFileSystem().supportedFileAttributeViews().contains("posix")) {
+ assertEquals("rwx------",
+ PosixFilePermissions.toString(Files.getPosixFilePermissions(reportFolder)));
+ }
+
+ FileUtils.deleteDirectory(dwc.getReportFolder());
+ }
+ }
+
/**
* Ensures that Content-Encoding headers are removed when JavaScript is uncompressed.
* (was causing java.io.IOException: Not in GZIP format).
diff --git a/src/test/java/org/htmlunit/util/FileUtilsTest.java b/src/test/java/org/htmlunit/util/FileUtilsTest.java
index 09e485c651..99ecd3c928 100644
--- a/src/test/java/org/htmlunit/util/FileUtilsTest.java
+++ b/src/test/java/org/htmlunit/util/FileUtilsTest.java
@@ -16,8 +16,11 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import java.nio.file.Files;
import java.nio.file.Path;
+import java.nio.file.attribute.PosixFilePermissions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
@@ -201,4 +204,33 @@ public void sanitizeFilenameLong() {
final String name = "a".repeat(1000) + ".txt";
assertEquals(name, FileUtils.sanitizeFilename(name));
}
+
+ /**
+ * The new directory (and the created parents) is accessible for the owner only.
+ * @param folder the folder
+ * @throws Exception if the test fails
+ */
+ @Test
+ public void createUserOnlyDirectories(@TempDir final Path folder) throws Exception {
+ final Path directory = folder.resolve("parent").resolve("report");
+ FileUtils.createUserOnlyDirectories(directory);
+
+ assertTrue(Files.isDirectory(directory));
+ if (directory.getFileSystem().supportedFileAttributeViews().contains("posix")) {
+ assertEquals("rwx------", PosixFilePermissions.toString(Files.getPosixFilePermissions(directory)));
+ assertEquals("rwx------",
+ PosixFilePermissions.toString(Files.getPosixFilePermissions(directory.getParent())));
+ }
+ }
+
+ /**
+ * An already existing directory is accepted.
+ * @param folder the folder
+ * @throws Exception if the test fails
+ */
+ @Test
+ public void createUserOnlyDirectoriesExisting(@TempDir final Path folder) throws Exception {
+ FileUtils.createUserOnlyDirectories(folder);
+ assertTrue(Files.isDirectory(folder));
+ }
}