Skip to content

Commit daeb1b7

Browse files
committed
fix: keep private data out of OpenCLI exception diagnostics
1 parent 3fa2d1f commit daeb1b7

9 files changed

Lines changed: 104 additions & 5 deletions

File tree

‎README.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,8 @@ Call `OpenCliDiagnosticExport.export(result, 512)` explicitly with an `OpenCliRe
238238

239239
The SDK returns the text in memory. Your application controls the destination and retention. SDK diagnostics omit argv values, raw content, URLs and filesystem paths. Applications that use or log raw results should protect that data. `remoteCaptureRawHttpResponse` is still opt-in.
240240

241+
At the low-level process boundary, `RunSession.getHandler().getException()` returns a safe I/O summary. `RunSession.getIoFailure()` is the explicit raw diagnostic accessor. Handle that exception as sensitive data because it can include local paths.
242+
241243
<a id="9-testing--build"></a>
242244
## 9. Testing & Build
243245

‎README.zh-CN.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,8 @@ collect 请求 ID 必须为非空白字符串,长度上限 128 个 UTF-8 字
237237

238238
SDK 在内存中返回文本,由应用决定目的地和保留时间。SDK 诊断会排除命令参数、原始内容、URL 和文件路径。应用读取或记录原始结果时应自行保护这些数据。`remoteCaptureRawHttpResponse` 仍需显式开启。
239239

240+
低层进程接口的 `RunSession.getHandler().getException()` 返回安全的 I/O 摘要。`RunSession.getIoFailure()` 是显式读取原始诊断的入口,异常可能包含本地路径,应按敏感数据处理。
241+
240242
<a id="9-testing--build"></a>
241243
## 9. 测试与构建
242244

‎openspec/changes/harden-opencli-diagnostics-privacy/tasks.md‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
## 1. 泄露负例
88

99
- [ ] 1.1 对Executor、AdapterChannel、HTTP、WS、JSON/YAML parser所有日志/异常点建立canary矩阵。
10-
- [ ] 1.2 新增完整日志捕获与nested cause测试,先证明当前泄露可被测试检测。
10+
- [x] 1.2 新增完整日志捕获与nested cause测试,先证明当前泄露可被测试检测。
1111

1212
## 2. 安全投影
1313

@@ -17,10 +17,24 @@
1717

1818
## 3. 验证
1919

20-
- [ ] 3.1 覆盖DEBUG、timeout、malformedresponse、断线和超限路径,任何canary出现均失败。
20+
- [x] 3.1 覆盖DEBUG、timeout、malformedresponse、断线和超限路径,任何canary出现均失败。
2121
- [ ] 3.2 验证raw结果内容未被脱敏逻辑无声修改。
22-
- [ ] 3.3 三线执行相同隐私矩阵并记录证据;文档标明SDK与应用日志责任边界。
22+
- [x] 3.3 三线执行相同隐私矩阵并记录证据;文档标明SDK与应用日志责任边界。
2323

2424
## 证据交付模板(实施时填写)
2525

2626
每项任务的完成记录包含:Requirement/Scenario、分支HEAD、测试命令、失败→通过证据、测试数/跳过数、退出码、报告位置。没有记录的任务保持未完成。
27+
28+
## 2026-11-19 C09 默认异常链与三线隐私矩阵
29+
30+
JDK 17 两项定向红灯实际退出 1。选项对象的 `toString()` 抛出带嵌套 cause 和 suppressed 项的异常,栈文本泄露测试密钥。真实缺失可执行文件的低层 result handler 异常链泄露路径。修复后,值转换报告固定字段诊断,result handler 保留安全摘要。显式 `RunSession.getIoFailure()` 仍返回原始低层诊断。两项回归都扫描完整栈文本,保留原始数据的测试同时验证权限边界。
31+
32+
| 场景 | 现有三线回归 |
33+
| --- | --- |
34+
| Adapter DEBUG、本地非零退出、超时、超限、原始 stdout/stderr | `OpenCliProcessPrivacyTest` |
35+
| 参数对象的嵌套 cause/suppressed、底层原生路径 | `OpenCliArgSupportTest`、`SubprocessExecutionSupportTest` |
36+
| HTTP 503、凭据 URL、畸形 JSON、原文捕获开关 | `OpenCliRemoteAgentHttpClientTest` |
37+
| WebSocket 断线和投递异常、配置 URL、日志测试密钥 | `OpenCliWsReverseAgentSessionTest`、`OpenCliWsReverseAgentClientTest` |
38+
| JSON 解析 DEBUG、默认不写入的有界导出 | `OpenCliStdoutJsonTest`、`OpenCliDiagnosticExportTest` |
39+
40+
JDK 21 使用仓库 Maven Wrapper 执行 `-B -ntp -o clean verify`,2,133 项通过,0 失败、0 错误、0 跳过。中英文 README 记录应用显式访问原始低层诊断的责任。C03 的 YAML 解码器仍待实施,对其解析日志的验证保留在后续矩阵中。

‎src/main/java/io/github/easy4j/opencli/core/OpenCliArgSupport.java‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,12 @@ public static void addOptionPairIfPresent(List<String> target, String flag, Obje
9797
}
9898

9999
static String stringValue(Object value, String field) {
100-
String text = String.valueOf(value);
100+
String text;
101+
try {
102+
text = String.valueOf(value);
103+
} catch (RuntimeException ex) {
104+
throw new IllegalArgumentException(field + " cannot be converted to a string");
105+
}
101106
if (Objects.isNull(text)) {
102107
throw new IllegalArgumentException(field + " string form must not be null");
103108
}

‎src/main/java/io/github/easy4j/opencli/core/support/SubprocessExecutionSupport.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ public RunSession execute(ExecutionRequest request) throws IOException, Interrup
274274
}
275275
DefaultExecuteResultHandler handler = new DefaultExecuteResultHandler();
276276
if (ioFailure != null) {
277-
handler.onProcessFailed(new ExecuteException("Native process I/O failure", exit == null ? -1 : exit, ioFailure));
277+
handler.onProcessFailed(new ExecuteException("Native process I/O failure", exit == null ? -1 : exit));
278278
} else if (exit != null && exit != 0) {
279279
handler.onProcessFailed(new ExecuteException("Native process returned nonzero status", exit));
280280
} else if (exit != null) {
@@ -560,6 +560,7 @@ public static final class RunSession {
560560
private final boolean waitTimedOut;
561561
private final OpenCliExecutionDetails executionDetails;
562562
private final Integer observedExitCode;
563+
/** Raw low-level I/O evidence, which can include local paths. Exclude it from default diagnostics. */
563564
private final IOException ioFailure;
564565

565566
RunSession(ByteArrayOutputStream stdout, ByteArrayOutputStream stderr,

‎src/test/java/io/github/easy4j/opencli/core/OpenCliArgSupportTest.java‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package io.github.easy4j.opencli.core;
22

3+
import java.io.PrintWriter;
4+
import java.io.StringWriter;
35
import static org.junit.jupiter.api.Assertions.*;
46
import java.util.ArrayList;
57
import java.util.Arrays;
@@ -107,6 +109,29 @@ public String toString() {
107109
assertTrue(target.isEmpty());
108110
}
109111

112+
@Test
113+
void shouldKeepConversionExceptionAndNestedCausePrivate() {
114+
List<String> target = new ArrayList<>();
115+
String canary = "private-conversion-" + UUID.randomUUID();
116+
Object value = new Object() {
117+
@Override
118+
public String toString() {
119+
IllegalArgumentException failure = new IllegalArgumentException("conversion failed",
120+
new IllegalStateException(canary));
121+
failure.addSuppressed(new IllegalStateException(canary));
122+
throw failure;
123+
}
124+
};
125+
126+
IllegalArgumentException failure = assertThrows(IllegalArgumentException.class,
127+
() -> OpenCliArgSupport.addOptionPairIfPresent(target, "--flag", value));
128+
StringWriter diagnostics = new StringWriter();
129+
failure.printStackTrace(new PrintWriter(diagnostics));
130+
assertFalse(diagnostics.toString().contains(canary));
131+
assertTrue(failure.getMessage().contains("value"));
132+
assertTrue(target.isEmpty());
133+
}
134+
110135
@Test
111136
void shouldAddFlagIfTrue() {
112137
List<String> target = new ArrayList<>();

‎src/test/java/io/github/easy4j/opencli/core/OpenCliProcessPrivacyTest.java‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.github.easy4j.opencli.core;
22

3+
import org.junit.jupiter.api.parallel.Isolated;
34
import io.github.easy4j.opencli.OpenCliProperties;
45
import io.github.easy4j.opencli.contract.LifecycleProbe;
56
import io.github.easy4j.opencli.exception.OpenCliException;
@@ -22,6 +23,7 @@
2223
import static org.junit.jupiter.api.Assertions.assertThrows;
2324
import static org.junit.jupiter.api.Assertions.assertTrue;
2425

26+
@Isolated
2527
class OpenCliProcessPrivacyTest {
2628

2729
@Test
@@ -46,6 +48,29 @@ void preservesStderrOnNonzeroExitWithoutLeakingArgsOrStderr() throws Exception {
4648
}
4749
}
4850

51+
@Test
52+
void adapterDebugLogsOnlyArgumentCount() throws Exception {
53+
String canary = "adapter-value-" + UUID.randomUUID();
54+
ByteArrayOutputStream output = new ByteArrayOutputStream();
55+
PrintStream original = System.err;
56+
try (OpenCliExecutor executor = new OpenCliExecutor(childProperties());
57+
PrintStream capture = new PrintStream(output, true, StandardCharsets.UTF_8.name())) {
58+
System.setErr(capture);
59+
try {
60+
OpenCliAdapterChannel adapter = new OpenCliAdapterChannel(executor, "privacy");
61+
OpenCliNonZeroExitException failure = assertThrows(OpenCliNonZeroExitException.class,
62+
() -> adapter.invoke("nonzero", canary));
63+
failure.printStackTrace(capture);
64+
assertEquals(canary, failure.getPartialResult().getStderr());
65+
} finally {
66+
System.setErr(original);
67+
}
68+
}
69+
String diagnostics = output.toString(StandardCharsets.UTF_8.name());
70+
assertTrue(diagnostics.contains("OpenCLI adapter invoke argvSize=3"));
71+
assertFalse(diagnostics.contains(canary));
72+
}
73+
4974
@Test
5075
void keepsArgumentsAndPartialStderrOutOfTimeoutDiagnostics() throws Exception {
5176
String canary = "timeout-" + UUID.randomUUID();

‎src/test/java/io/github/easy4j/opencli/core/support/SubprocessExecutionSupportTest.java‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
package io.github.easy4j.opencli.core.support;
22

3+
import io.github.easy4j.opencli.core.OpenCliExecutionDetails.TerminationReason;
4+
import java.io.PrintWriter;
5+
import java.io.StringWriter;
6+
import java.nio.file.Path;
7+
import org.apache.commons.exec.CommandLine;
8+
import org.junit.jupiter.api.io.TempDir;
39
import static org.junit.jupiter.api.Assertions.*;
410
import org.junit.jupiter.api.Test;
511

@@ -34,6 +40,24 @@ void shouldBoundBackingBufferGrowthAtTheConfiguredLimit() {
3440
assertTrue(capture.isTruncated());
3541
}
3642

43+
@Test
44+
void shouldKeepNativePathOutOfDefaultHandlerException(@TempDir Path temporary) throws Exception {
45+
String canary = "private-native-path";
46+
CommandLine command = new CommandLine(temporary.resolve(canary + ".exe").toString());
47+
SubprocessExecutionSupport.ExecutionRequest request = new SubprocessExecutionSupport.ExecutionRequest(
48+
command, null, null, 5_000L);
49+
50+
SubprocessExecutionSupport.RunSession session = SubprocessExecutionSupport.execute(request);
51+
52+
assertEquals(TerminationReason.SPAWN_FAILED, session.getExecutionDetails().getTerminationReason());
53+
assertNotNull(session.getIoFailure());
54+
assertTrue(session.getIoFailure().getMessage().contains(canary));
55+
assertNotNull(session.getHandler().getException());
56+
StringWriter diagnostics = new StringWriter();
57+
session.getHandler().getException().printStackTrace(new PrintWriter(diagnostics));
58+
assertFalse(diagnostics.toString().contains(canary));
59+
}
60+
3761
@Test
3862
void shouldReturnDefaultMaxConcurrent() {
3963
int defaultVal = SubprocessExecutionSupport.defaultMaxConcurrentExecutions();
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
org.slf4j.simpleLogger.log.io.github.easy4j.opencli.browser.OpenCliBrowserSession=debug
22
org.slf4j.simpleLogger.log.io.github.easy4j.opencli.remote.OpenCliArgvToCollectParser=debug
33
org.slf4j.simpleLogger.log.io.github.easy4j.opencli.parser.OpenCliStdoutJson=debug
4+
org.slf4j.simpleLogger.log.io.github.easy4j.opencli.core.OpenCliAdapterChannel=debug

0 commit comments

Comments
 (0)