Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@

<body>
<release version="5.9.0" date="xxxx xx, 2026" description="Firefox 160, Chrome/Edge 158, Fetch, Intl, CSP, CORS, Cookies, Bugfixes">
<action type="fix" dev="Vidya m">
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.
</action>
<action type="fix" dev="RhinoTeam">
core-js: Skip holes of the array returned by the flatMap callback.
</action>
Expand Down
11 changes: 8 additions & 3 deletions src/main/java/org/htmlunit/HttpWebConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
27 changes: 27 additions & 0 deletions src/main/java/org/htmlunit/util/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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.
* <p>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.</p>
*
* @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);
}
}
42 changes: 42 additions & 0 deletions src/test/java/org/htmlunit/HttpWebConnectionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Path> files = Files.list(tempDirectory)) {
final List<Path> 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)}
Expand Down
25 changes: 25 additions & 0 deletions src/test/java/org/htmlunit/util/DebuggingWebConnectionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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, "<html></html>");

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).
Expand Down
32 changes: 32 additions & 0 deletions src/test/java/org/htmlunit/util/FileUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}
}
Loading