Skip to content

Commit 3173df5

Browse files
committed
Reject malformed version output before capability discovery
1 parent e83bc00 commit 3173df5

2 files changed

Lines changed: 28 additions & 3 deletions

File tree

‎src/main/java/io/github/easy4j/opencli/capability/OpenCliCapabilityDiscovery.java‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.util.Map;
2727
import java.util.Objects;
2828
import java.util.TreeMap;
29+
import java.util.regex.Matcher;
2930
import java.util.regex.Pattern;
3031

3132
/** Fixed, bounded metadata probes through the existing local subprocess runtime. */
@@ -34,7 +35,7 @@ public final class OpenCliCapabilityDiscovery {
3435
private static final long MAX_PROBE_MILLIS = 10_000;
3536
private static final long MAX_CLEANUP_MILLIS = 1_000;
3637
private static final Pattern VERSION = Pattern.compile(
37-
"[0-9]+\\.[0-9]+\\.[0-9]+(?:-[0-9A-Za-z.-]+)?(?:\\+[0-9A-Za-z.-]+)?");
38+
"[ \t\r\n]*([0-9]+\\.[0-9]+\\.[0-9]+(?:-[0-9A-Za-z.-]+)?(?:\\+[0-9A-Za-z.-]+)?)[ \t\r\n]*");
3839

3940
private final OpenCliExecutor executor;
4041
private long nextAttempt;
@@ -140,10 +141,11 @@ private OpenCliCapabilitySnapshot observe(OpenCliHelpScope scope) {
140141
String phase = "VERSION";
141142
try {
142143
OpenCliResult versionResult = local.invokeLocalOnly(Collections.singletonList("--version"));
143-
String version = versionResult.getStdout().trim();
144-
if (versionResult.getExecutionDetails().isStdoutTruncated() || !VERSION.matcher(version).matches()) {
144+
Matcher versionOutput = VERSION.matcher(versionResult.getStdout());
145+
if (versionResult.getExecutionDetails().isStdoutTruncated() || !versionOutput.matches()) {
145146
return snapshot.status(Status.UNKNOWN).reason("INVALID_VERSION_OUTPUT").build();
146147
}
148+
String version = versionOutput.group(1);
147149
snapshot.observedVersion(version);
148150
if (scope != OpenCliHelpScope.ROOT_HELP && !"1.8.8".equals(version)) {
149151
return snapshot.status(Status.UNSUPPORTED).reason("UNVERIFIED_HELP_VERSION").build();

‎src/test/java/io/github/easy4j/opencli/capability/OpenCliCapabilityDiscoveryTest.java‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,21 @@ void malformedRootRegistryLeavesLocalInvocationAvailable() {
6767
}
6868
}
6969

70+
@Test
71+
void invalidVersionOutputPreventsTheHelpProbe(@TempDir Path directory) {
72+
Path marker = directory.resolve("help-started");
73+
OpenCliProperties props = properties("invalid-version");
74+
props.getEnvironment().put("OPENCLI_TEST_DISCOVERY_HELP_MARKER", marker.toString());
75+
try (OpenCliClient client = new OpenCliClient(props)) {
76+
OpenCliCapabilitySnapshot result = client.discoverCapabilities();
77+
assertFalse(Files.exists(marker), "help was started after malformed version output");
78+
assertEquals(OpenCliCapabilitySnapshot.Status.UNKNOWN, result.getStatus());
79+
assertEquals("INVALID_VERSION_OUTPUT", result.getReason());
80+
assertNull(result.getObservedVersion());
81+
assertNull(result.getHelp());
82+
}
83+
}
84+
7085
@Test
7186
void pathLookupAndRemoteModeAreUnsupported() {
7287
try (OpenCliClient client = new OpenCliClient()) {
@@ -269,6 +284,10 @@ public static void main(String[] args) throws Exception {
269284
mode = new String(Files.readAllBytes(Paths.get(control)), StandardCharsets.UTF_8);
270285
}
271286
if (Arrays.equals(args, new String[]{"--version"})) {
287+
if ("invalid-version".equals(mode)) {
288+
System.out.println("1.8.8" + (char) 0);
289+
return;
290+
}
272291
System.out.println("new-version-error".equals(mode) ? "1.8.9" : "1.8.8");
273292
return;
274293
}
@@ -300,6 +319,10 @@ public static void main(String[] args) throws Exception {
300319
if (!Arrays.equals(args, new String[]{"--help", "-f", "json"})) {
301320
System.exit(2);
302321
}
322+
String marker = System.getenv("OPENCLI_TEST_DISCOVERY_HELP_MARKER");
323+
if (Objects.nonNull(marker)) {
324+
Files.createFile(Paths.get(marker));
325+
}
303326
if ("hold-old".equals(mode)) {
304327
Path path = Paths.get(control).getParent();
305328
Files.createFile(path.resolve("entered"));

0 commit comments

Comments
 (0)