Skip to content

Commit c997fb4

Browse files
committed
Reject malformed structured help catalogs
1 parent 7d85857 commit c997fb4

3 files changed

Lines changed: 54 additions & 3 deletions

File tree

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

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,14 @@ private static boolean isHelpRoot(JsonNode root) {
7373
return false;
7474
}
7575
boolean directory = root.path("commands").isArray();
76-
if (directory && "opencli".equals(text(root.path("name"))) && root.path("site_adapters").isObject()) {
76+
if (directory && "opencli".equals(text(root.path("name"))) && root.has("site_adapters")) {
77+
JsonNode sites = root.path("site_adapters").path("sites");
78+
if (!sites.isArray()) {
79+
return false;
80+
}
81+
for (JsonNode site : sites) {
82+
requiredName(site);
83+
}
7784
return true;
7885
}
7986
if (directory && OpenCliStrings.isNotBlank(text(root.path("site")))) {
@@ -99,7 +106,7 @@ private static OpenCliHelpSchema schema(JsonNode node) {
99106
.command(command)
100107
.namespace(text(node.path("namespace")))
101108
.site(site)
102-
.aliases(array(node.path("aliases"), OpenCliHelpParser::requiredText))
109+
.aliases(array(node.path("aliases"), OpenCliHelpParser::requiredName))
103110
.commands(array(node.path("commands"), OpenCliHelpParser::schema))
104111
.positionals(array(node.path("positionals"), OpenCliHelpParser::argument))
105112
.commandOptions(array(node.path("command_options"), OpenCliHelpParser::argument))
@@ -116,7 +123,7 @@ private static OpenCliHelpSchema.Argument argument(JsonNode node) {
116123
}
117124
JsonNode defaultValue = node.path("default");
118125
return OpenCliHelpSchema.Argument.builder()
119-
.name(requiredText(node.path("name")))
126+
.name(requiredName(node.path("name")))
120127
.flags(text(node.path("flags")))
121128
.type(text(node.path("type")))
122129
.takesValue(text(node.path("takes_value")))
@@ -161,6 +168,14 @@ private static String requiredText(JsonNode node) {
161168
return value;
162169
}
163170

171+
private static String requiredName(JsonNode node) {
172+
String value = text(node);
173+
if (OpenCliStrings.isBlank(value)) {
174+
throw invalidSchema();
175+
}
176+
return value;
177+
}
178+
164179
private static Boolean bool(JsonNode node) {
165180
if (node.isMissingNode()) {
166181
return null;

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,17 @@ void plainHelpLeavesLocalInvocationAvailable() {
4646
}
4747
}
4848

49+
@Test
50+
void malformedRootRegistryLeavesLocalInvocationAvailable() {
51+
try (OpenCliClient client = new OpenCliClient(properties("malformed-root"))) {
52+
OpenCliCapabilitySnapshot result = client.discoverCapabilities();
53+
assertEquals(OpenCliCapabilitySnapshot.Status.UNKNOWN, result.getStatus());
54+
assertEquals("INVALID_SCHEMA", result.getReason());
55+
assertEquals("1.8.8", result.getObservedVersion());
56+
assertEquals("raw available", client.getExecutor().invoke("npm", "search").getStdout());
57+
}
58+
}
59+
4960
@Test
5061
void pathLookupAndRemoteModeAreUnsupported() {
5162
try (OpenCliClient client = new OpenCliClient()) {
@@ -119,6 +130,10 @@ public static void main(String[] args) throws Exception {
119130
System.out.print("Usage: opencli\n");
120131
return;
121132
}
133+
if ("malformed-root".equals(mode)) {
134+
System.out.print("{\"name\":\"opencli\",\"commands\":[],\"site_adapters\":{}}");
135+
return;
136+
}
122137
try (InputStream input = DiscoveryFixture.class.getResourceAsStream(
123138
"/opencli-contracts/v1/structured-help-1.8.8/root.json");
124139
Scanner scan = new Scanner(input, "UTF-8").useDelimiter("\\A")) {

‎src/test/java/io/github/easy4j/opencli/contract/OpenCliHelpParserTest.java‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,27 @@ void rejectsControlBytesOutsideAJsonHelpDocument() {
9696
assertEquals(OpenCliHelpResult.Status.AVAILABLE, OpenCliHelpParser.parse(" \t" + valid + "\r\n").getStatus());
9797
}
9898

99+
@Test
100+
void distinguishesAnExplicitlyEmptyRootCatalogFromAMalformedRegistry() {
101+
String root = "{\"name\":\"opencli\",\"commands\":[],\"site_adapters\":";
102+
assertUnknown(root + "{}}", OpenCliHelpResult.Reason.INVALID_SCHEMA);
103+
assertUnknown(root + "{\"sites\":{}}}", OpenCliHelpResult.Reason.INVALID_SCHEMA);
104+
assertUnknown(root + "{\"sites\":[\u0020null]}}", OpenCliHelpResult.Reason.INVALID_SCHEMA);
105+
assertEquals(OpenCliHelpResult.Status.AVAILABLE,
106+
OpenCliHelpParser.parse(root + "{\"count\":0,\"sites\":[]}}").getStatus());
107+
}
108+
109+
@Test
110+
void requiresArgumentAndAliasNamesWhilePreservingAnEmptyChoice() {
111+
String command = "{\"site\":\"npm\",\"name\":\"search\",\"command\":\"opencli npm search\",";
112+
assertUnknown(command + "\"positionals\":[{\"name\":\" \"}]}", OpenCliHelpResult.Reason.INVALID_SCHEMA);
113+
assertUnknown(command + "\"positionals\":[],\"aliases\":[\"\"]}", OpenCliHelpResult.Reason.INVALID_SCHEMA);
114+
OpenCliHelpResult result = OpenCliHelpParser.parse(command
115+
+ "\"command_options\":[{\"name\":\"query\",\"choices\":[\"\",\"all\"]}]}");
116+
assertEquals(OpenCliHelpResult.Status.AVAILABLE, result.getStatus());
117+
assertEquals("", result.getSchema().getCommandOptions().get(0).getChoices().get(0));
118+
}
119+
99120
private static void assertUnknown(String raw, OpenCliHelpResult.Reason reason) {
100121
OpenCliHelpResult result = OpenCliHelpParser.parse(raw);
101122
assertEquals(OpenCliHelpResult.Status.UNKNOWN, result.getStatus());

0 commit comments

Comments
 (0)