Skip to content

Commit 606052a

Browse files
fix: gate URL-builder resolution to JS/TS and to call arguments
handle_url_builders ran for every vendored grammar, so a C helper returning an absolute pathname became a URL builder and minted a Route node plus an HTTP_CALLS edge in a language that speaks no HTTP. Gate recording to JavaScript, TypeScript and TSX. Builder entries now carry a flag in the per-file constant map: only a call_expression argument resolves one, so handing a builder to a callback builds no request. A builder whose returns are not all route-shaped literals is declined instead of attributing its one literal to call sites that take the computed branch. Five negative tests cover the cross-language case, the mixed builder, the ambiguity tombstone, the builder reference and the non-URL helper. Signed-off-by: Charles Queiroz <fcqueiroz@liquibase.com>
1 parent 5bbc7b7 commit 606052a

4 files changed

Lines changed: 212 additions & 21 deletions

File tree

‎internal/cbm/cbm.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,7 @@ typedef struct {
551551
typedef struct {
552552
const char *names[CBM_MAX_STRING_CONSTANTS];
553553
const char *values[CBM_MAX_STRING_CONSTANTS];
554+
bool is_url_builder[CBM_MAX_STRING_CONSTANTS];
554555
int count;
555556
} CBMStringConstantMap;
556557

‎internal/cbm/extract_calls.c‎

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,29 @@ enum { MIN_PRINTABLE = 0x20 };
3030
/* Handler arg scan start index (skip first positional). */
3131
enum { HANDLER_START_IDX = 1 };
3232

33-
/* Look up a module-level string constant by name. */
33+
/* Look up a module-level string constant by name. URL-builder entries share the
34+
* map but are not constants: a bare `thingPath` reference is the function, not
35+
* the URL it would build (issue #1009). */
3436
static const char *lookup_string_constant(const CBMExtractCtx *ctx, const char *name) {
3537
if (!name || !name[0]) {
3638
return NULL;
3739
}
3840
const CBMStringConstantMap *map = &ctx->string_constants;
3941
for (int i = 0; i < map->count; i++) {
40-
if (strcmp(map->names[i], name) == 0) {
42+
if (!map->is_url_builder[i] && strcmp(map->names[i], name) == 0) {
43+
return map->values[i];
44+
}
45+
}
46+
return NULL;
47+
}
48+
49+
static const char *lookup_url_builder(const CBMExtractCtx *ctx, const char *name) {
50+
if (!name || !name[0]) {
51+
return NULL;
52+
}
53+
const CBMStringConstantMap *map = &ctx->string_constants;
54+
for (int i = 0; i < map->count; i++) {
55+
if (map->is_url_builder[i] && strcmp(map->names[i], name) == 0) {
4156
return map->values[i];
4257
}
4358
}
@@ -1868,7 +1883,7 @@ static void extract_call_args(CBMExtractCtx *ctx, TSNode args, CBMCall *call) {
18681883
if (!ts_node_is_null(fn) && strcmp(ts_node_type(fn), "identifier") == 0) {
18691884
char *fname = cbm_node_text(ctx->arena, fn, ctx->source);
18701885
if (fname) {
1871-
ca->value = lookup_string_constant(ctx, fname);
1886+
ca->value = lookup_url_builder(ctx, fname);
18721887
}
18731888
}
18741889
}
@@ -2105,7 +2120,7 @@ static const char *extract_url_or_topic_arg(CBMExtractCtx *ctx, TSNode args) {
21052120
TSNode fn = ts_node_child_by_field_name(arg, TS_FIELD("function"));
21062121
if (!ts_node_is_null(fn) && strcmp(ts_node_type(fn), "identifier") == 0) {
21072122
char *fname = cbm_node_text(ctx->arena, fn, ctx->source);
2108-
const char *val = fname ? lookup_string_constant(ctx, fname) : NULL;
2123+
const char *val = fname ? lookup_url_builder(ctx, fname) : NULL;
21092124
if (val) {
21102125
return val;
21112126
}

‎internal/cbm/extract_unified.c‎

Lines changed: 81 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,18 +1122,19 @@ static const char *builder_template_text(CBMExtractCtx *ctx, TSNode node) {
11221122
piece = "{}";
11231123
if (ts_node_named_child_count(c) > 0) {
11241124
TSNode expr = ts_node_named_child(c, 0);
1125-
const char *ek = ts_node_type(expr);
1126-
TSNode name_node = expr;
1127-
if (strcmp(ek, "call_expression") == 0) {
1128-
name_node = ts_node_child_by_field_name(expr, TS_FIELD("function"));
1129-
}
1125+
/* `${basePath(id)}` inlines a builder, `${BASE}` a const. A bare
1126+
* name never resolves to a builder: that reads the function. */
1127+
bool want_builder = strcmp(ts_node_type(expr), "call_expression") == 0;
1128+
TSNode name_node =
1129+
want_builder ? ts_node_child_by_field_name(expr, TS_FIELD("function")) : expr;
11301130
if (!ts_node_is_null(name_node) &&
11311131
strcmp(ts_node_type(name_node), "identifier") == 0) {
11321132
char *nm = cbm_node_text(ctx->arena, name_node, ctx->source);
11331133
if (nm) {
11341134
const CBMStringConstantMap *map = &ctx->string_constants;
11351135
for (int mi = 0; mi < map->count; mi++) {
1136-
if (map->values[mi] && strcmp(map->names[mi], nm) == 0) {
1136+
if (map->values[mi] && map->is_url_builder[mi] == want_builder &&
1137+
strcmp(map->names[mi], nm) == 0) {
11371138
piece = map->values[mi];
11381139
break;
11391140
}
@@ -1187,17 +1188,23 @@ static const char *url_builder_literal_text(CBMExtractCtx *ctx, TSNode value_nod
11871188
return text;
11881189
}
11891190

1190-
/* Record `name -> url` in the per-file constant map so call-site resolution
1191-
* (lookup_string_constant) can see it. A builder that returns two DIFFERENT
1192-
* urls is ambiguous: tombstone it with a NULL value so lookups miss. */
1191+
/* A route-shaped URL literal: an absolute path that classifies as a URL. */
1192+
static const char *builder_route_url(CBMExtractCtx *ctx, TSNode value_node) {
1193+
const char *url = url_builder_literal_text(ctx, value_node);
1194+
if (!url || url[0] != '/' || cbm_classify_string(url, (int)strlen(url)) != CBM_STRREF_URL) {
1195+
return NULL;
1196+
}
1197+
return url;
1198+
}
1199+
11931200
static void record_url_builder(CBMExtractCtx *ctx, const char *name, const char *url) {
11941201
if (!name || !name[0] || !url) {
11951202
return;
11961203
}
11971204
CBMStringConstantMap *map = &ctx->string_constants;
11981205
for (int i = 0; i < map->count; i++) {
11991206
if (strcmp(map->names[i], name) == 0) {
1200-
if (map->values[i] && strcmp(map->values[i], url) != 0) {
1207+
if (map->is_url_builder[i] && map->values[i] && strcmp(map->values[i], url) != 0) {
12011208
map->values[i] = NULL; /* ambiguous builder */
12021209
}
12031210
return;
@@ -1206,26 +1213,79 @@ static void record_url_builder(CBMExtractCtx *ctx, const char *name, const char
12061213
if (map->count < CBM_MAX_STRING_CONSTANTS) {
12071214
map->names[map->count] = (char *)name;
12081215
map->values[map->count] = (char *)url;
1216+
map->is_url_builder[map->count] = true;
12091217
map->count++;
12101218
}
12111219
}
12121220

1221+
/* Every `return` this function owns yields a route-shaped URL literal. A body
1222+
* that also returns a computed path (`return computePath(kind)`) would attribute
1223+
* its one literal to call sites that never produce it, so a mixed builder is
1224+
* declined rather than guessed. Returns inside a nested function belong to that
1225+
* function, not to this one. */
1226+
static bool builder_returns_only_urls(CBMExtractCtx *ctx, TSNode func) {
1227+
TSNode body = ts_node_child_by_field_name(func, TS_FIELD("body"));
1228+
if (ts_node_is_null(body)) {
1229+
return false;
1230+
}
1231+
bool only_urls = true;
1232+
TSTreeCursor cursor = ts_tree_cursor_new(body);
1233+
for (;;) {
1234+
TSNode node = ts_tree_cursor_current_node(&cursor);
1235+
if (strcmp(ts_node_type(node), "return_statement") == 0 &&
1236+
ts_node_eq(cbm_find_enclosing_func(node, ctx->language), func)) {
1237+
if (ts_node_named_child_count(node) == 0 ||
1238+
!builder_route_url(ctx, ts_node_named_child(node, 0))) {
1239+
only_urls = false;
1240+
break;
1241+
}
1242+
}
1243+
if (ts_tree_cursor_goto_first_child(&cursor)) {
1244+
continue;
1245+
}
1246+
if (ts_tree_cursor_goto_next_sibling(&cursor)) {
1247+
continue;
1248+
}
1249+
bool found = false;
1250+
while (ts_tree_cursor_goto_parent(&cursor)) {
1251+
if (ts_tree_cursor_goto_next_sibling(&cursor)) {
1252+
found = true;
1253+
break;
1254+
}
1255+
}
1256+
if (!found) {
1257+
break;
1258+
}
1259+
}
1260+
ts_tree_cursor_delete(&cursor);
1261+
return only_urls;
1262+
}
1263+
12131264
/* URL-builder helper pattern (issue #1009): a small function whose return value
12141265
* is a URL-shaped literal, consumed as `client(buildPath(id))`. The literal
12151266
* never appears as a call argument, so first_string_arg resolution cannot see
1216-
* it; record `functionName -> url` in the same per-file constant map used for
1217-
* module-level consts and let the call-site call_expression branch resolve it.
1218-
* Covers `return`-statement bodies and arrow-function expression bodies. */
1267+
* it; record `functionName -> url` in the per-file constant map and let the
1268+
* call-site call_expression branch resolve it. Covers `return`-statement bodies
1269+
* and arrow-function expression bodies.
1270+
*
1271+
* JS/TS only. The predicate accepts any absolute pathname, and `return` plus a
1272+
* string literal is also the shape of every C or Go helper handing back a
1273+
* filesystem path (`/etc/...`, `/proc/self/...`); recording those would mint a
1274+
* Route node and an HTTP_CALLS edge in a language that speaks no HTTP. */
12191275
static void handle_url_builders(CBMExtractCtx *ctx, TSNode node, const WalkState *state) {
1276+
if (ctx->language != CBM_LANG_JAVASCRIPT && ctx->language != CBM_LANG_TYPESCRIPT &&
1277+
ctx->language != CBM_LANG_TSX) {
1278+
return;
1279+
}
12201280
const char *kind = ts_node_type(node);
12211281

12221282
if (strcmp(kind, "arrow_function") == 0) {
12231283
TSNode body = ts_node_child_by_field_name(node, TS_FIELD("body"));
12241284
if (ts_node_is_null(body) || strcmp(ts_node_type(body), "statement_block") == 0) {
12251285
return;
12261286
}
1227-
const char *url = url_builder_literal_text(ctx, body);
1228-
if (!url || url[0] != '/' || cbm_classify_string(url, (int)strlen(url)) != CBM_STRREF_URL) {
1287+
const char *url = builder_route_url(ctx, body);
1288+
if (!url) {
12291289
return;
12301290
}
12311291
TSNode parent = ts_node_parent(node);
@@ -1247,8 +1307,12 @@ static void handle_url_builders(CBMExtractCtx *ctx, TSNode node, const WalkState
12471307
if (ts_node_named_child_count(node) == 0) {
12481308
return;
12491309
}
1250-
const char *url = url_builder_literal_text(ctx, ts_node_named_child(node, 0));
1251-
if (!url || url[0] != '/' || cbm_classify_string(url, (int)strlen(url)) != CBM_STRREF_URL) {
1310+
const char *url = builder_route_url(ctx, ts_node_named_child(node, 0));
1311+
if (!url) {
1312+
return;
1313+
}
1314+
TSNode func = cbm_find_enclosing_func(node, ctx->language);
1315+
if (ts_node_is_null(func) || !builder_returns_only_urls(ctx, func)) {
12521316
return;
12531317
}
12541318
const char *name = strrchr(state->enclosing_func_qn, '.');

‎tests/test_extraction.c‎

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3427,6 +3427,112 @@ TEST(extract_ts_url_builder_composed_issue1009) {
34273427
PASS();
34283428
}
34293429

3430+
static bool any_call_arg_resolves_to(CBMFileResult *r, const char *url) {
3431+
for (int i = 0; i < r->calls.count; i++) {
3432+
const CBMCall *c = &r->calls.items[i];
3433+
if (c->first_string_arg && strcmp(c->first_string_arg, url) == 0) {
3434+
return true;
3435+
}
3436+
for (int a = 0; a < c->arg_count; a++) {
3437+
if (c->args[a].value && strcmp(c->args[a].value, url) == 0) {
3438+
return true;
3439+
}
3440+
}
3441+
}
3442+
return false;
3443+
}
3444+
3445+
TEST(extract_c_url_builder_gated_issue1009) {
3446+
CBMFileResult *r = extract("static const char *cfg_path(void) {\n"
3447+
" return \"/etc/myapp/conf.d\";\n"
3448+
"}\n"
3449+
"void init(void) {\n"
3450+
" parse_config(cfg_path());\n"
3451+
"}\n",
3452+
CBM_LANG_C, "t", "conf.c");
3453+
ASSERT_NOT_NULL(r);
3454+
ASSERT_FALSE(r->has_error);
3455+
ASSERT_FALSE(any_call_arg_resolves_to(r, "/etc/myapp/conf.d"));
3456+
cbm_free_result(r);
3457+
PASS();
3458+
}
3459+
3460+
/* One literal return next to a computed one would attribute the literal to every
3461+
* call site, including those taking the computed branch. A builder whose returns
3462+
* are not all URL literals is declined. */
3463+
TEST(extract_ts_url_builder_mixed_returns_issue1009) {
3464+
CBMFileResult *r = extract("function pathFor(kind: string): string {\n"
3465+
" if (kind === 'user') return '/api/users';\n"
3466+
" return computePath(kind);\n"
3467+
"}\n"
3468+
"export function load(kind: string) {\n"
3469+
" return apiGet(pathFor(kind));\n"
3470+
"}\n",
3471+
CBM_LANG_TYPESCRIPT, "t", "mixed.ts");
3472+
ASSERT_NOT_NULL(r);
3473+
ASSERT_FALSE(r->has_error);
3474+
ASSERT_FALSE(any_call_arg_resolves_to(r, "/api/users"));
3475+
cbm_free_result(r);
3476+
PASS();
3477+
}
3478+
3479+
/* Two different literal URLs make the call site's route unknowable, so the
3480+
* builder is tombstoned and lookups miss. */
3481+
TEST(extract_ts_url_builder_ambiguous_issue1009) {
3482+
CBMFileResult *r = extract("function pathFor(kind: string): string {\n"
3483+
" if (kind === 'user') return '/api/users';\n"
3484+
" return '/api/teams';\n"
3485+
"}\n"
3486+
"export function load(kind: string) {\n"
3487+
" return apiGet(pathFor(kind));\n"
3488+
"}\n",
3489+
CBM_LANG_TYPESCRIPT, "t", "ambiguous.ts");
3490+
ASSERT_NOT_NULL(r);
3491+
ASSERT_FALSE(r->has_error);
3492+
ASSERT_FALSE(any_call_arg_resolves_to(r, "/api/users"));
3493+
ASSERT_FALSE(any_call_arg_resolves_to(r, "/api/teams"));
3494+
cbm_free_result(r);
3495+
PASS();
3496+
}
3497+
3498+
/* Handing a builder to a callback position builds no URL. Only a call to it
3499+
* resolves, so the mapping function does not inherit an HTTP_CALLS edge to the
3500+
* route. */
3501+
TEST(extract_ts_url_builder_reference_issue1009) {
3502+
CBMFileResult *r = extract("function thingPath(id: string): string {\n"
3503+
" return `/api/v1/things/${id}`;\n"
3504+
"}\n"
3505+
"export function loadAll(ids: string[]) {\n"
3506+
" return ids.map(thingPath);\n"
3507+
"}\n",
3508+
CBM_LANG_TYPESCRIPT, "t", "reference.ts");
3509+
ASSERT_NOT_NULL(r);
3510+
ASSERT_FALSE(r->has_error);
3511+
ASSERT_FALSE(any_call_arg_resolves_to(r, "/api/v1/things/{}"));
3512+
cbm_free_result(r);
3513+
PASS();
3514+
}
3515+
3516+
/* A helper returning an ordinary string is not a URL builder, so its call site
3517+
* gets no resolved string argument. */
3518+
TEST(extract_ts_url_builder_non_url_issue1009) {
3519+
CBMFileResult *r = extract("function label(): string {\n"
3520+
" return 'plain text';\n"
3521+
"}\n"
3522+
"export function render() {\n"
3523+
" return send(label());\n"
3524+
"}\n",
3525+
CBM_LANG_TYPESCRIPT, "t", "label.ts");
3526+
ASSERT_NOT_NULL(r);
3527+
ASSERT_FALSE(r->has_error);
3528+
const CBMCall *c = find_call_by_callee(r, "send");
3529+
ASSERT_NOT_NULL(c);
3530+
ASSERT_NULL(c->first_string_arg);
3531+
ASSERT_FALSE(any_call_arg_resolves_to(r, "plain text"));
3532+
cbm_free_result(r);
3533+
PASS();
3534+
}
3535+
34303536
/* Issue #1006: JS/TS template-literal URLs must flatten ${...} substitutions
34313537
* to the canonical "{}" placeholder, both as call arguments (HTTP_CALLS) and
34323538
* as URL-shaped string_refs collected from const/return positions. */
@@ -5845,6 +5951,11 @@ SUITE(extraction) {
58455951
RUN_TEST(extract_go_binary_concat_url_no_literal_suffix_issue1249);
58465952
RUN_TEST(extract_ts_url_builder_issue1009);
58475953
RUN_TEST(extract_ts_url_builder_composed_issue1009);
5954+
RUN_TEST(extract_c_url_builder_gated_issue1009);
5955+
RUN_TEST(extract_ts_url_builder_mixed_returns_issue1009);
5956+
RUN_TEST(extract_ts_url_builder_ambiguous_issue1009);
5957+
RUN_TEST(extract_ts_url_builder_reference_issue1009);
5958+
RUN_TEST(extract_ts_url_builder_non_url_issue1009);
58485959
RUN_TEST(extract_java_no_double_class_qn);
58495960
RUN_TEST(extract_go_no_filename_in_module_qn);
58505961
RUN_TEST(extract_large_ts_has_functions_issue213);

0 commit comments

Comments
 (0)