Skip to content

Commit 137919e

Browse files
committed
fix: omit parser input from debug diagnostics
1 parent 8278dd7 commit 137919e

3 files changed

Lines changed: 27 additions & 1 deletion

File tree

‎src/main/java/io/github/easy4j/opencli/parser/OpenCliStdoutJson.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public static JsonNode parseLenient(String stdout) {
4343
try {
4444
return MAPPER.readTree(trim);
4545
} catch (JsonProcessingException e) {
46-
log.debug("stdout is not strict JSON, wrapping as text: {}", e.getMessage());
46+
log.debug("stdout is not strict JSON; wrapping as text chars={}", stdout.length());
4747
return MAPPER.getNodeFactory().textNode(stdout);
4848
}
4949
}

‎src/test/java/io/github/easy4j/opencli/parser/OpenCliStdoutJsonTest.java‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
import com.fasterxml.jackson.databind.node.TextNode;
66
import io.github.easy4j.opencli.core.OpenCliResult;
77
import io.github.easy4j.opencli.core.OpenCliTypedResult;
8+
import java.io.ByteArrayOutputStream;
9+
import java.io.PrintStream;
10+
import java.nio.charset.StandardCharsets;
11+
import java.util.UUID;
812
import org.junit.jupiter.api.Test;
913

1014
class OpenCliStdoutJsonTest {
@@ -30,6 +34,27 @@ void shouldWrapNonJsonAsTextNode() {
3034
assertEquals("not json at all", node.textValue());
3135
}
3236

37+
@Test
38+
void debugDiagnosticDoesNotExposeParserInput() throws Exception {
39+
String canary = "stdoutsecret" + UUID.randomUUID().toString().replace("-", "");
40+
ByteArrayOutputStream diagnostics = new ByteArrayOutputStream();
41+
PrintStream previousErr = System.err;
42+
JsonNode node;
43+
try (PrintStream capture = new PrintStream(diagnostics, true, "UTF-8")) {
44+
System.setErr(capture);
45+
try {
46+
node = OpenCliStdoutJson.parseLenient(canary);
47+
} finally {
48+
System.setErr(previousErr);
49+
}
50+
}
51+
52+
assertEquals(canary, node.textValue());
53+
String logText = diagnostics.toString(StandardCharsets.UTF_8.name());
54+
assertTrue(logText.contains("stdout is not strict JSON"));
55+
assertFalse(logText.contains(canary));
56+
}
57+
3358
@Test
3459
void shouldReturnEmptyTextNodeForNull() {
3560
JsonNode node = OpenCliStdoutJson.parseLenient(null);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
org.slf4j.simpleLogger.log.io.github.easy4j.opencli.parser.OpenCliStdoutJson=debug

0 commit comments

Comments
 (0)