Skip to content

Commit bd996c8

Browse files
authored
Merge pull request DeusData#1012 from DeusData/fix/cli-unknown-flag-reject
fix(cli): reject unknown tool flags with a nearest-flag suggestion
2 parents 0756b2b + 2950e4e commit bd996c8

2 files changed

Lines changed: 103 additions & 0 deletions

File tree

‎src/cli/cli.c‎

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4520,6 +4520,62 @@ static char *cli_heap_msgf(const char *fmt, const char *arg) {
45204520
return cbm_strdup(buf);
45214521
}
45224522

4523+
/* Levenshtein distance for near-miss flag suggestions (two-row DP; inputs
4524+
* are schema property names, well under the buffer sizes used here). */
4525+
static int cli_edit_distance(const char *a, const char *b) {
4526+
enum { CLI_ED_MAX = 128 };
4527+
size_t la = strlen(a);
4528+
size_t lb = strlen(b);
4529+
if (la >= CLI_ED_MAX || lb >= CLI_ED_MAX) {
4530+
return CLI_ED_MAX;
4531+
}
4532+
int prev[CLI_ED_MAX + 1];
4533+
int cur[CLI_ED_MAX + 1];
4534+
for (size_t j = 0; j <= lb; j++) {
4535+
prev[j] = (int)j;
4536+
}
4537+
for (size_t i = 1; i <= la; i++) {
4538+
cur[0] = (int)i;
4539+
for (size_t j = 1; j <= lb; j++) {
4540+
int cost = (a[i - 1] == b[j - 1]) ? 0 : 1;
4541+
int del = prev[j] + 1;
4542+
int ins = cur[j - 1] + 1;
4543+
int sub = prev[j - 1] + cost;
4544+
int m = del < ins ? del : ins;
4545+
cur[j] = m < sub ? m : sub;
4546+
}
4547+
memcpy(prev, cur, (lb + 1) * sizeof(int));
4548+
}
4549+
return prev[lb];
4550+
}
4551+
4552+
/* Closest schema property to `key` for a "did you mean" suggestion, or NULL
4553+
* when nothing is plausibly near (distance > half the key length, min 2). */
4554+
static const char *cli_closest_prop(yyjson_val *props, const char *key) {
4555+
const char *best = NULL;
4556+
int best_d = 0;
4557+
size_t idx;
4558+
size_t max;
4559+
yyjson_val *k;
4560+
yyjson_val *v;
4561+
yyjson_obj_foreach(props, idx, max, k, v) {
4562+
const char *name = yyjson_get_str(k);
4563+
if (!name) {
4564+
continue;
4565+
}
4566+
int d = cli_edit_distance(key, name);
4567+
if (!best || d < best_d) {
4568+
best = name;
4569+
best_d = d;
4570+
}
4571+
}
4572+
int limit = (int)(strlen(key) / 2);
4573+
if (limit < 2) {
4574+
limit = 2;
4575+
}
4576+
return (best && best_d <= limit) ? best : NULL;
4577+
}
4578+
45234579
/* True if the schema's required[] array contains `key`. */
45244580
static bool cli_schema_required_has(yyjson_val *required, const char *key) {
45254581
if (!required || !yyjson_is_arr(required)) {
@@ -4661,6 +4717,34 @@ char *cbm_cli_build_args_json(const char *tool_name, int argc, char **argv, char
46614717
cli_kebab_to_snake(key);
46624718
const char *type = cli_schema_type(props, key);
46634719

4720+
/* Unknown flag for a known tool: reject loudly (#997). Silently
4721+
* typing it as a string ships it as an ignored JSON arg — the
4722+
* server applies its default and the caller gets silently-wrong
4723+
* output (e.g. `trace_path --max-depth 1` traced at depth 3). */
4724+
if (props && !type) {
4725+
char kebab_key[CLI_BUF_256];
4726+
snprintf(kebab_key, sizeof(kebab_key), "%s", key);
4727+
cli_snake_to_kebab(kebab_key);
4728+
const char *close = cli_closest_prop(props, key);
4729+
char suggestion[CLI_BUF_256] = "";
4730+
if (close) {
4731+
char close_kebab[CLI_BUF_256];
4732+
snprintf(close_kebab, sizeof(close_kebab), "%s", close);
4733+
cli_snake_to_kebab(close_kebab);
4734+
snprintf(suggestion, sizeof(suggestion), " (did you mean --%s?)", close_kebab);
4735+
}
4736+
if (err_out) {
4737+
char buf[CLI_BUF_512];
4738+
snprintf(buf, sizeof(buf),
4739+
"unknown flag --%s for this tool%s — run 'cli %s --help' for the "
4740+
"supported flags",
4741+
kebab_key, suggestion, tool_name);
4742+
*err_out = cbm_strdup(buf);
4743+
}
4744+
ok = false;
4745+
break;
4746+
}
4747+
46644748
if (type && strcmp(type, "array") == 0 && !have_value) {
46654749
if (err_out) {
46664750
*err_out = cli_heap_msgf("flag --%s requires a value", key);

‎tests/test_cli.c‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2957,6 +2957,24 @@ TEST(cli_build_args_json_bare_boolean_issue680) {
29572957
PASS();
29582958
}
29592959

2960+
/* An unknown flag for a KNOWN tool must be rejected loudly, not silently
2961+
* typed as a string and dropped server-side (#997). GF1 eval: `trace_path
2962+
* --max-depth 1` was accepted, the real --depth stayed at default 3, and
2963+
* the trace silently returned hop-2/3 results — silent-wrong output. The
2964+
* error names the closest valid flag as a suggestion. */
2965+
TEST(cli_build_args_json_unknown_flag_rejected) {
2966+
char *err = NULL;
2967+
char *argv[] = {"--max-depth", "1"};
2968+
char *json = cbm_cli_build_args_json("trace_path", 2, argv, &err);
2969+
ASSERT_NULL(json);
2970+
ASSERT_NOT_NULL(err);
2971+
ASSERT(strstr(err, "unknown flag") != NULL);
2972+
ASSERT(strstr(err, "max-depth") != NULL);
2973+
ASSERT(strstr(err, "--depth") != NULL); /* nearest-flag suggestion */
2974+
free(err);
2975+
PASS();
2976+
}
2977+
29602978
/* A repeated array-typed flag accumulates into a JSON array. */
29612979
TEST(cli_build_args_json_repeated_array_issue680) {
29622980
char *err = NULL;
@@ -3226,6 +3244,7 @@ SUITE(cli) {
32263244
RUN_TEST(cli_build_args_json_string_flag_issue680);
32273245
RUN_TEST(cli_build_args_json_integer_flag_issue680);
32283246
RUN_TEST(cli_build_args_json_bare_boolean_issue680);
3247+
RUN_TEST(cli_build_args_json_unknown_flag_rejected);
32293248
RUN_TEST(cli_build_args_json_repeated_array_issue680);
32303249
RUN_TEST(cli_build_args_json_kebab_to_snake_issue680);
32313250
RUN_TEST(cli_build_args_json_key_equals_value_issue680);

0 commit comments

Comments
 (0)