Skip to content

Commit 04ba2fa

Browse files
fix(cypher): refuse a query naming more variables than a binding holds
check_projection_scope models declared names in a fixed 32-entry array and skipped the check entirely when a query declared more. Nothing bounded how many variables a pattern could declare, so the out-of-scope refusal added by DeusData#1922 switched itself off on a wide query: the same undeclared name was refused at 10 declared names and quietly accepted at 35, answering a column of empty strings. Skipping was deliberate — a wrong refusal costs the caller a working query, which is worse than the silence. The premise was wrong. A binding holds CYP_MAX_VARS node variables and CYP_MAX_EDGE_VARS edge variables in plain arrays, and binding_set and binding_set_edge drop anything past those without a word, so a query naming more cannot be answered at all. Its extra names bind to nothing and project as blanks, which reads as "the graph holds no such data". The choice was never refuse-or-stay-quiet; it was refuse, or answer wrong. So bound the input. check_pattern_var_capacity counts distinct node and edge variables across every pattern — they share one binding, and an OPTIONAL MATCH pattern sits in the same array — and refuses beyond what a binding holds, naming the limit and how to get under it. Leaving the name off a node frees its slot, because an unnamed node takes none. Bounding the input rather than each consumer also makes three other silent drops unreachable: binding_set past 16, binding_set_edge past 8, and the column collection in execute_default_projection. Those would otherwise need an error path threaded up through every caller, since both binding setters return void. The two `< 0` branches in check_projection_scope stay, with comments saying they can no longer fire. They keep the guard standing if either bound ever moves. Fixes DeusData#1995 Signed-off-by: Joshua Richter <jrichter5781@gmail.com>
1 parent 7b0f553 commit 04ba2fa

2 files changed

Lines changed: 163 additions & 2 deletions

File tree

‎src/cypher/cypher.c‎

Lines changed: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5164,13 +5164,88 @@ static const char *scope_checkable_var(const cbm_return_item_t *item) {
51645164
return item->variable;
51655165
}
51665166

5167+
/* Says which limit the query passed and how to get under it. An unnamed node
5168+
* takes no slot, so dropping a name the query never uses is the cheap way out.
5169+
* Splitting the MATCH is NOT — every pattern of one query shares one binding,
5170+
* which is why the caller counts across all of them. Separate queries do work,
5171+
* because each gets a binding of its own. */
5172+
static char *var_capacity_error(const char *kind, int limit) {
5173+
char buf[CBM_SZ_256];
5174+
snprintf(buf, sizeof(buf),
5175+
"too many %s variables: a query can name at most %d — "
5176+
"leave the name off the ones you do not use, or run separate queries",
5177+
kind, limit);
5178+
return heap_strdup(buf);
5179+
}
5180+
5181+
/* A binding holds a fixed number of variables: CYP_MAX_VARS node variables and
5182+
* CYP_MAX_EDGE_VARS edge variables, both in plain arrays (see binding_t).
5183+
* binding_set and binding_set_edge drop anything past those without a word, so
5184+
* a query naming more variables than a binding holds cannot be answered — the
5185+
* extra names bind to nothing and project as empty strings, which reads as
5186+
* "the graph holds no such data" rather than "this query is too wide".
5187+
*
5188+
* Refuse such a query instead, before any row is touched. Bounding the input
5189+
* here is also what stops collect_declared_names below overflowing its array:
5190+
* the patterns contribute at most CYP_MAX_VARS + CYP_MAX_EDGE_VARS names, plus
5191+
* one UNWIND alias, which is well inside CYP_SCOPE_MAX_NAMES.
5192+
*
5193+
* Counts DISTINCT variables across every pattern, because they all land in the
5194+
* same binding: a multi-MATCH query shares one, and an OPTIONAL MATCH pattern
5195+
* sits in this same array (q->pattern_optional marks which). A node variable
5196+
* and an edge variable may share a name and each take a slot, because the
5197+
* binding keeps the two in separate arrays. */
5198+
static char *check_pattern_var_capacity(const cbm_query_t *q) {
5199+
/* Initialized because cppcheck cannot see that scope_holds reads only the
5200+
* node_n / edge_n entries already written, and reports the first call as a
5201+
* read of an uninitialized array. */
5202+
const char *node_vars[CYP_MAX_VARS] = {NULL};
5203+
const char *edge_vars[CYP_MAX_EDGE_VARS] = {NULL};
5204+
int node_n = 0;
5205+
int edge_n = 0;
5206+
for (int pi = 0; pi < q->pattern_count; pi++) {
5207+
const cbm_pattern_t *pat = &q->patterns[pi];
5208+
for (int ni = 0; ni < pat->node_count; ni++) {
5209+
const char *var = pat->nodes[ni].variable;
5210+
if (!var || scope_holds(node_vars, node_n, var)) {
5211+
continue;
5212+
}
5213+
if (node_n >= CYP_MAX_VARS) {
5214+
return var_capacity_error("node", CYP_MAX_VARS);
5215+
}
5216+
node_vars[node_n++] = var;
5217+
}
5218+
for (int ri = 0; ri < pat->rel_count; ri++) {
5219+
const char *var = pat->rels[ri].variable;
5220+
if (!var || scope_holds(edge_vars, edge_n, var)) {
5221+
continue;
5222+
}
5223+
if (edge_n >= CYP_MAX_EDGE_VARS) {
5224+
return var_capacity_error("edge", CYP_MAX_EDGE_VARS);
5225+
}
5226+
edge_vars[edge_n++] = var;
5227+
}
5228+
}
5229+
return NULL;
5230+
}
5231+
51675232
/* Answers NULL when the query is fine, or a heap message naming the first
51685233
* variable that is not in scope. Checks one query; the caller walks a UNION. */
51695234
static char *check_projection_scope(const cbm_query_t *q) {
5235+
/* Runs first, so the rest of this function can trust that the query names
5236+
* no more variables than the arrays below can model. */
5237+
char *capacity_err = check_pattern_var_capacity(q);
5238+
if (capacity_err) {
5239+
return capacity_err;
5240+
}
5241+
51705242
const char *declared[CYP_SCOPE_MAX_NAMES];
51715243
int declared_n = collect_declared_names(q, declared, CYP_SCOPE_MAX_NAMES);
51725244
if (declared_n < 0) {
5173-
return NULL; /* too many names to model — stay quiet rather than guess */
5245+
/* Unreachable while the capacity check above holds. Kept so the guard
5246+
* still stands if either bound ever moves. Skipping the check was the
5247+
* old behaviour, and it let an out-of-scope name through in silence. */
5248+
return NULL;
51745249
}
51755250

51765251
/* A WITH still reads the pattern variables. */
@@ -5194,7 +5269,7 @@ static char *check_projection_scope(const cbm_query_t *q) {
51945269
if (q->with_clause) {
51955270
scope_n = collect_with_names(q->with_clause, after_with, CYP_SCOPE_MAX_NAMES);
51965271
if (scope_n < 0) {
5197-
return NULL;
5272+
return NULL; /* unreachable: a WITH holds at most CYP_SCOPE_MAX_NAMES items */
51985273
}
51995274
scope = after_with;
52005275
}

‎tests/test_cypher.c‎

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3202,6 +3202,89 @@ TEST(cypher_wide_with_refused_not_truncated) {
32023202
ASSERT_EQ(cbm_cypher_execute(s, ok_query, "test", 0, &r16), 0);
32033203
ASSERT_EQ(r16.col_count, 16);
32043204
cbm_cypher_result_free(&r16);
3205+
cbm_store_close(s);
3206+
PASS();
3207+
}
3208+
3209+
/* Build "MATCH (a0:NoSuchLabelXYZ)-[:CALLS]->(a1)-…->(aN-1)" into buf. The label
3210+
* matches nothing, so any query built on it is instant and needs no fixture. */
3211+
static void build_node_chain(char *buf, size_t buf_sz, int nodes) {
3212+
int off = snprintf(buf, buf_sz, "MATCH (a0:NoSuchLabelXYZ)");
3213+
for (int i = 1; i < nodes; i++) {
3214+
off += snprintf(buf + off, buf_sz - (size_t)off, "-[:CALLS]->(a%d)", i);
3215+
}
3216+
}
3217+
3218+
TEST(cypher_wide_pattern_refused) {
3219+
/* A binding holds CYP_MAX_VARS (16) node variables, and binding_set drops
3220+
* the 17th without a word. The query then answers a column of empty strings
3221+
* for every name it could not bind, which reads as "the graph holds no such
3222+
* data". Refuse the query instead of answering it wrong. */
3223+
cbm_store_t *s = setup_cypher_store();
3224+
char query[2048];
3225+
3226+
build_node_chain(query, sizeof(query), 20); /* 20 > CYP_MAX_VARS */
3227+
strncat(query, " RETURN a0.name", sizeof(query) - strlen(query) - 1);
3228+
cbm_cypher_result_t wide = {0};
3229+
ASSERT_TRUE(cbm_cypher_execute(s, query, "test", 0, &wide) != 0);
3230+
ASSERT_NOT_NULL(wide.error);
3231+
ASSERT_TRUE(strstr(wide.error, "node") != NULL); /* says which limit was passed */
3232+
cbm_cypher_result_free(&wide);
3233+
3234+
/* The width right at the bound still runs, so the guard refuses only what a
3235+
* binding genuinely cannot hold. */
3236+
build_node_chain(query, sizeof(query), 16);
3237+
strncat(query, " RETURN a0.name", sizeof(query) - strlen(query) - 1);
3238+
cbm_cypher_result_t ok = {0};
3239+
ASSERT_EQ(cbm_cypher_execute(s, query, "test", 0, &ok), 0);
3240+
cbm_cypher_result_free(&ok);
3241+
3242+
cbm_store_close(s);
3243+
PASS();
3244+
}
3245+
3246+
TEST(cypher_wide_edge_pattern_refused) {
3247+
/* Same shape on the edge table, where binding_set_edge stops at
3248+
* CYP_MAX_EDGE_VARS (8). Only NAMED relationships take a slot. */
3249+
cbm_store_t *s = setup_cypher_store();
3250+
char query[2048];
3251+
int off = snprintf(query, sizeof(query), "MATCH (a0:NoSuchLabelXYZ)");
3252+
for (int i = 1; i <= 9; i++) { /* 9 > CYP_MAX_EDGE_VARS */
3253+
off += snprintf(query + off, sizeof(query) - (size_t)off, "-[r%d:CALLS]->(a%d)", i, i);
3254+
}
3255+
snprintf(query + off, sizeof(query) - (size_t)off, " RETURN a0.name");
3256+
cbm_cypher_result_t wide = {0};
3257+
ASSERT_TRUE(cbm_cypher_execute(s, query, "test", 0, &wide) != 0);
3258+
ASSERT_NOT_NULL(wide.error);
3259+
ASSERT_TRUE(strstr(wide.error, "edge") != NULL);
3260+
cbm_cypher_result_free(&wide);
3261+
cbm_store_close(s);
3262+
PASS();
3263+
}
3264+
3265+
TEST(cypher_scope_check_survives_wide_pattern) {
3266+
/* Regression test for #1995. check_projection_scope models declared names in
3267+
* a fixed array and used to skip the check entirely when a query declared
3268+
* more than it held. So the same out-of-scope name was refused on a narrow
3269+
* query and quietly accepted on a wide one. Both must now be refused. */
3270+
cbm_store_t *s = setup_cypher_store();
3271+
char query[4096];
3272+
3273+
build_node_chain(query, sizeof(query), 10);
3274+
strncat(query, " RETURN zzz.name", sizeof(query) - strlen(query) - 1);
3275+
cbm_cypher_result_t narrow = {0};
3276+
ASSERT_TRUE(cbm_cypher_execute(s, query, "test", 0, &narrow) != 0);
3277+
ASSERT_NOT_NULL(narrow.error);
3278+
ASSERT_TRUE(strstr(narrow.error, "zzz") != NULL);
3279+
cbm_cypher_result_free(&narrow);
3280+
3281+
/* 35 declared names — this one used to answer a zzz.name column of nothing. */
3282+
build_node_chain(query, sizeof(query), 35);
3283+
strncat(query, " RETURN zzz.name", sizeof(query) - strlen(query) - 1);
3284+
cbm_cypher_result_t wide = {0};
3285+
ASSERT_TRUE(cbm_cypher_execute(s, query, "test", 0, &wide) != 0);
3286+
ASSERT_NOT_NULL(wide.error);
3287+
cbm_cypher_result_free(&wide);
32053288

32063289
cbm_store_close(s);
32073290
PASS();
@@ -4521,6 +4604,9 @@ SUITE(cypher) {
45214604
RUN_TEST(cypher_return_star_dedups_repeated_pattern_var);
45224605
RUN_TEST(cypher_return_star_after_with_names_aliases);
45234606
RUN_TEST(cypher_wide_with_refused_not_truncated);
4607+
RUN_TEST(cypher_wide_pattern_refused);
4608+
RUN_TEST(cypher_wide_edge_pattern_refused);
4609+
RUN_TEST(cypher_scope_check_survives_wide_pattern);
45244610
RUN_TEST(cypher_parse_neq);
45254611
RUN_TEST(cypher_parse_in);
45264612
RUN_TEST(cypher_parse_is_null);

0 commit comments

Comments
 (0)