Copilot commented on code in PR #3627:
URL: https://github.com/apache/maven-surefire/pull/3627#discussion_r4651911903
##########
surefire-api/src/main/java/org/apache/maven/surefire/api/util/TempFileManager.java:
##########
@@ -148,26 +152,42 @@ public synchronized File createTempFile(String prefix,
String suffix) {
throw new UncheckedIOException(new IOException("Unable to create
temporary file " + tempFile));
}
- if (tempFile.exists() || tempFiles.contains(tempFile)) {
- // temporary file already exists? Try another name
+ if (tempFiles.contains(tempFile)) {
+ // already handed out in this JVM? Try another name
return createTempFile(prefix, suffix);
- } else {
- // create temporary directory
- if (!tempDir.exists()) {
- if (!tempDir.mkdirs()) {
- throw new UncheckedIOException(
- new IOException("Unable to create temporary
directory " + tempDir.getAbsolutePath()));
- }
- }
+ }
+ // create temporary directory with owner-only permissions where
supported
+ if (!tempDir.exists()) {
try {
- tempFile.createNewFile();
+ Files.createDirectories(tempDir.toPath());
+ setOwnerOnlyPermissions(tempDir.toPath(), "rwx------");
} catch (IOException ex) {
- throw new UncheckedIOException("Unable to create temporary
file " + tempFile.getAbsolutePath(), ex);
+ throw new UncheckedIOException(
+ new IOException("Unable to create temporary directory
" + tempDir.getAbsolutePath(), ex));
}
+ }
+
+ Path path = tempFile.toPath();
+ try {
+ Files.createFile(path);
+ setOwnerOnlyPermissions(path, "rw-------");
Review Comment:
The file is initially created with the process's default permissions and
restricted only afterward. In a shared or pre-existing temp directory, another
process can open the initially readable empty file and retain that descriptor
for subsequent writes, including SystemPropertyManager's boot properties; chmod
does not revoke an already-open descriptor. Apply the owner-only POSIX
permissions as creation attributes so this change actually provides owner-only
creation, falling back to default creation only on non-POSIX filesystems.
##########
maven-failsafe-plugin/src/main/java/org/apache/maven/plugin/failsafe/util/FailsafeSummaryXmlUtils.java:
##########
@@ -100,6 +115,24 @@ public static RunResult toRunResult(File
failsafeSummaryXml) throws IOException
parseBoolean(timeout));
} catch (XPathExpressionException | NumberFormatException e) {
throw new IOException("Could not parse " +
failsafeSummaryXml.getPath(), e);
+ } catch (ParserConfigurationException | SAXException e) {
+ throw new IOException("Could not parse " +
failsafeSummaryXml.getPath(), e);
+ }
+ }
+
+ private static void setFeature(DocumentBuilderFactory factory, String
feature, boolean value) {
+ try {
+ factory.setFeature(feature, value);
+ } catch (ParserConfigurationException e) {
+ // Optional hardening feature unsupported by this parser; secure
processing is still enabled.
+ }
+ }
Review Comment:
If a JAXP provider does not support `disallow-doctype-decl`, this catch lets
it continue parsing DOCTYPE-bearing summaries, contrary to the rejection
required by #3613 and the PR description. Secure processing and external-access
restrictions do not themselves prohibit DOCTYPE declarations. Propagate
configuration failures so `toRunResult` fails closed through its existing
IOException wrapper.
##########
surefire-api/src/test/java/org/apache/maven/surefire/api/util/TempFileManagerTest.java:
##########
@@ -82,6 +84,37 @@ public void testCreateTempFileAndDelete() throws Exception {
assertThat(tfm.getTempDir()).doesNotExist();
}
+ @Test
+ public void testCreateTempFileDoesNotReturnPreplantedSymlink() throws
Exception {
+ String subDirName = TempFileManagerTest.class.getSimpleName() + new
Random().nextLong();
+ TempFileManager tfm = TempFileManager.instance(subDirName);
+ File first = tfm.createTempFile("prefix", ".tmp");
+ String firstBase = first.getName().substring(0,
first.getName().lastIndexOf('_'));
+ int firstId =
Integer.parseInt(first.getName().substring(firstBase.length() + 1,
first.getName().length() - 4));
+ java.nio.file.Path link = tfm.getTempDir().toPath().resolve(firstBase
+ "_" + (firstId + 1) + ".tmp");
+ try {
+ Files.createSymbolicLink(link,
tfm.getTempDir().toPath().resolve("missing-target"));
+ } catch (UnsupportedOperationException e) {
+ return;
Review Comment:
On Windows without symbolic-link creation privileges,
Files.createSymbolicLink throws an IOException/FileSystemException, not
UnsupportedOperationException. Consequently this new test fails on otherwise
supported Windows installations instead of skipping the unavailable symlink
scenario. Use a capability-based JUnit assumption that also handles the
privilege-denied case while preserving genuine setup failures.
##########
surefire-extensions-api/src/main/java/org/apache/maven/surefire/extensions/util/LineConsumerThread.java:
##########
@@ -59,26 +63,40 @@ public LineConsumerThread(
@Override
public void run() {
- try (Scanner stream = new Scanner(channel, encoding.name());
- CountdownCloseable c = countdownCloseable; ) {
- boolean isError = false;
- while (stream.hasNextLine()) {
- try {
- String line = stream.nextLine();
- isError |= stream.ioException() != null;
- if (!isError && !disabled) {
- eventHandler.handleEvent(line);
+ try (Reader reader = Channels.newReader(channel,
encoding.newDecoder(), 4096);
Review Comment:
A fresh CharsetDecoder reports malformed and unmappable input by default,
whereas Scanner replaced those bytes. Raw fork stderr can contain
native-process output or bytes that do not match the Maven JVM's default
charset; one such sequence now throws an IOException, silently ends this
consumer, and closes the stderr channel, losing all subsequent diagnostics.
Configure both decoder error actions to REPLACE so the capped reader preserves
the previous behavior.
##########
surefire-extensions-api/src/main/java/org/apache/maven/surefire/extensions/util/LineConsumerThread.java:
##########
@@ -59,26 +63,40 @@ public LineConsumerThread(
@Override
public void run() {
- try (Scanner stream = new Scanner(channel, encoding.name());
- CountdownCloseable c = countdownCloseable; ) {
- boolean isError = false;
- while (stream.hasNextLine()) {
- try {
- String line = stream.nextLine();
- isError |= stream.ioException() != null;
- if (!isError && !disabled) {
- eventHandler.handleEvent(line);
+ try (Reader reader = Channels.newReader(channel,
encoding.newDecoder(), 4096);
+ CountdownCloseable c = countdownCloseable) {
+ StringBuilder line = new StringBuilder();
+ boolean truncated = false;
+ CharBuffer buffer = CharBuffer.allocate(4096);
+ while (reader.read(buffer) != -1) {
+ buffer.flip();
+ while (buffer.hasRemaining()) {
+ char character = buffer.get();
+ if (character == '\n') {
Review Comment:
The loop recognizes only LF and discards CR, so `first\rsecond\r` produces
one `firstsecond` event instead of two lines. A line exactly at the cap
followed by CRLF also incorrectly receives the truncation marker. Preserve
Scanner's line separators and treat CRLF as one delimiter, including across
read buffers.
##########
surefire-report-parser/src/main/java/org/apache/maven/plugins/surefire/report/TestSuiteXmlParser.java:
##########
@@ -100,6 +110,23 @@ public List<ReportTestSuite> parse(InputStreamReader
stream)
return suites;
}
+ private static void setFeature(SAXParserFactory factory, String feature,
boolean value)
+ throws ParserConfigurationException, SAXNotRecognizedException,
SAXNotSupportedException {
+ try {
+ factory.setFeature(feature, value);
+ } catch (SAXNotRecognizedException | SAXNotSupportedException e) {
+ // The parser does not know this optional hardening feature;
secure processing is still enabled.
+ }
Review Comment:
Ignoring unsupported features allows this parser to proceed without the
DOCTYPE restriction required by #3615. Secure processing limits parsing
resources but does not prohibit DOCTYPE declarations, so an alternate JAXP
provider can accept reports that the PR promises to reject. Let these
configuration exceptions propagate through parse's existing exception contract
instead of silently treating the required restrictions as optional.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]