Skip to content

Commit 4d97d78

Browse files
committed
fix(routes): read a ServeMux pattern only for Handle and HandleFunc
The Go 1.22 pattern split ran for every route-verb callee, so other Go calls whose name ends in Get/Post/Put/Delete/Patch became route registrations: st.Get("users/123") on a project method minted __route__GET__/123 and lost its CALLS edge to Store.Get, and c.Get("https://api.example.com/v1/x") split "https:" off as a host and minted __route__GET__//api.example.com/v1/x, in both passes. cbm_service_pattern_mux_route_path reads the pattern only for a callee ending ".Handle" or ".HandleFunc", and never for an argument containing "://". The extractor's handler capture and both passes (route_reg_path, find_route_path_in_args, cbm_pipeline_route_reg_method) go through it, so the two passes still mint identical Routes. Two negative cases extend pipeline_go_servemux_method_patterns_issue2537 in both passes, and the infrascan unit covers the gate. Refs DeusData#2537 Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent ad78993 commit 4d97d78

8 files changed

Lines changed: 137 additions & 17 deletions

File tree

‎internal/cbm/extract_calls.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4559,7 +4559,8 @@ CBMInvocationDescriptor handle_calls(CBMExtractCtx *ctx, TSNode node, const CBML
45594559
if (call.first_string_arg &&
45604560
(call.first_string_arg[0] == '/' ||
45614561
(ctx->language == CBM_LANG_GO &&
4562-
cbm_service_pattern_mux_split(call.first_string_arg, NULL, 0) != NULL)) &&
4562+
cbm_service_pattern_mux_route_path(call.callee_name, call.first_string_arg) !=
4563+
NULL)) &&
45634564
!(laravel_uri && php_route_action_is_string(node))) {
45644565
call.second_arg_name = extract_handler_arg(ctx, args);
45654566
}

‎internal/cbm/service_patterns.c‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1227,6 +1227,21 @@ const char *cbm_service_pattern_mux_split(const char *pattern, char *method, siz
12271227
return slash;
12281228
}
12291229

1230+
const char *cbm_service_pattern_mux_route_path(const char *callee_name, const char *pattern) {
1231+
static const char *const mux_callees[] = {".Handle", ".HandleFunc"};
1232+
if (!callee_name || !pattern || strstr(pattern, "://") != NULL) {
1233+
return NULL;
1234+
}
1235+
size_t clen = strlen(callee_name);
1236+
for (size_t i = 0; i < sizeof(mux_callees) / sizeof(mux_callees[0]); i++) {
1237+
size_t slen = strlen(mux_callees[i]);
1238+
if (clen >= slen && strcmp(callee_name + clen - slen, mux_callees[i]) == 0) {
1239+
return cbm_service_pattern_mux_split(pattern, NULL, 0);
1240+
}
1241+
}
1242+
return NULL;
1243+
}
1244+
12301245
const char *cbm_service_pattern_broker(const char *resolved_qn) {
12311246
if (!resolved_qn) {
12321247
return NULL;

‎internal/cbm/service_patterns.h‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,14 @@ const char *cbm_service_pattern_route_method(const char *callee_name);
9595
* on the file language. */
9696
const char *cbm_service_pattern_mux_split(const char *pattern, char *method, size_t method_sz);
9797

98+
/* The path of a ServeMux registration's pattern (#2537): only a callee ending
99+
* ".Handle" or ".HandleFunc" registers one, and never with a URL ("://").
100+
* Another route-verb call names no pattern: `store.Get("users/123")` is no
101+
* host "users" + path "/123", `client.Get("https://...")` no host "https:".
102+
* The split of cbm_service_pattern_mux_split for such a registration, else
103+
* NULL. The grammar is Go's: callers gate on the file language. */
104+
const char *cbm_service_pattern_mux_route_path(const char *callee_name, const char *pattern);
105+
98106
/* Get the broker name for an async QN (e.g., "pubsub" from a Pub/Sub QN).
99107
* Returns NULL if not an async pattern. */
100108
const char *cbm_service_pattern_broker(const char *resolved_qn);

‎src/pipeline/pass_calls.c‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -573,8 +573,9 @@ cbm_svc_kind_t cbm_pipeline_external_import_service(cbm_external_import_index_t
573573

574574
/* The path a route registration's first argument names: the argument itself
575575
* when it opens with a slash, or in Go the path of a ServeMux pattern that
576-
* names a method or host first ("POST /v1/x", #2537). NULL when it names no
577-
* path. MUST match pass_parallel.c find_route_path_in_args. */
576+
* names a method or host first ("POST /v1/x", #2537; Handle / HandleFunc
577+
* only). NULL when it names no path. MUST match pass_parallel.c
578+
* find_route_path_in_args. */
578579
static const char *route_reg_path(const CBMCall *call, CBMLanguage lang) {
579580
const char *arg = call->first_string_arg;
580581
if (!arg) {
@@ -583,7 +584,7 @@ static const char *route_reg_path(const CBMCall *call, CBMLanguage lang) {
583584
if (arg[0] == '/') {
584585
return arg;
585586
}
586-
return cbm_pipeline_mux_route_path(arg, lang);
587+
return cbm_pipeline_mux_route_path(call, lang);
587588
}
588589

589590
/* Handle a route registration call: create Route node + HANDLES edge.

‎src/pipeline/pass_parallel.c‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2263,11 +2263,12 @@ static bool is_route_path_shaped(const char *val) {
22632263
return val && val[0] == '/' && !cbm_service_pattern_is_comment_text(val);
22642264
}
22652265

2266-
const char *cbm_pipeline_mux_route_path(const char *arg, CBMLanguage lang) {
2266+
const char *cbm_pipeline_mux_route_path(const CBMCall *call, CBMLanguage lang) {
2267+
const char *arg = call->first_string_arg;
22672268
if (lang != CBM_LANG_GO || !arg || cbm_service_pattern_is_comment_text(arg)) {
22682269
return NULL;
22692270
}
2270-
return cbm_service_pattern_mux_split(arg, NULL, 0);
2271+
return cbm_service_pattern_mux_route_path(call->callee_name, arg);
22712272
}
22722273

22732274
const char *cbm_pipeline_route_reg_method(const CBMCall *call, CBMLanguage lang, char *buf,
@@ -2276,7 +2277,7 @@ const char *cbm_pipeline_route_reg_method(const CBMCall *call, CBMLanguage lang,
22762277
if (method && strcmp(method, "ANY") != 0) {
22772278
return method;
22782279
}
2279-
if (cbm_pipeline_mux_route_path(call->first_string_arg, lang) &&
2280+
if (cbm_pipeline_mux_route_path(call, lang) &&
22802281
cbm_service_pattern_mux_split(call->first_string_arg, buf, buf_sz) && buf[0] != '\0') {
22812282
return buf;
22822283
}
@@ -2291,9 +2292,9 @@ static const char *find_route_path_in_args(const CBMCall *call, const char **out
22912292
*out_handler = call->second_arg_name;
22922293
return call->first_string_arg;
22932294
}
2294-
/* 1b. Go ServeMux pattern with a method or host before the path (#2537).
2295-
* MUST match pass_calls.c route_reg_path. */
2296-
const char *mux_path = cbm_pipeline_mux_route_path(call->first_string_arg, lang);
2295+
/* 1b. Go ServeMux pattern with a method or host before the path (#2537),
2296+
* read for Handle / HandleFunc only. MUST match pass_calls.c route_reg_path. */
2297+
const char *mux_path = cbm_pipeline_mux_route_path(call, lang);
22972298
if (mux_path) {
22982299
*out_handler = call->second_arg_name;
22992300
return mux_path;

‎src/pipeline/pipeline_internal.h‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -788,11 +788,12 @@ bool cbm_pipeline_http_client_call_url(const cbm_gbuf_t *gbuf, const char *proje
788788
void cbm_pipeline_close_call_props(char *props, int n, const CBMCall *call, bool with_line);
789789

790790
/* Go 1.22 ServeMux registrations may name a method and a host before the path
791-
* ("POST /v1/x", "GET api.example.com/v1/x", #2537). For a Go file, the path
792-
* inside such a route-registration argument; NULL for any other language and
793-
* for an argument that is no such pattern (a plain "/v1/x" included). The one
794-
* reader of that grammar behind both passes (pass_parallel.c). */
795-
const char *cbm_pipeline_mux_route_path(const char *arg, CBMLanguage lang);
791+
* ("POST /v1/x", "GET api.example.com/v1/x", #2537). For a Go `Handle` /
792+
* `HandleFunc` call, the path inside its first argument; NULL for any other
793+
* language or callee (`store.Get("users/123")`), for a URL argument, and for an
794+
* argument that is no such pattern (a plain "/v1/x" included). The one reader
795+
* of that grammar behind both passes (pass_parallel.c). */
796+
const char *cbm_pipeline_mux_route_path(const CBMCall *call, CBMLanguage lang);
796797

797798
/* The method of a route registration's Route: the verb its callee names
798799
* (`router.GET`), else the method its Go ServeMux pattern names

‎tests/test_infrascan.c‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,8 @@ TEST(infrascan_data_flows_test_handlers_pair_only_within_their_file) {
551551

552552
/* Issue #2537: the Go 1.22 ServeMux grammar "[METHOD ][HOST]/[PATH]". The path
553553
* is found inside the written pattern; a plain path, a lowercase word, a
554-
* pattern without a path and a method too long for the buffer are no split. */
554+
* pattern without a path and a method too long for the buffer are no split.
555+
* Only a Handle / HandleFunc registration is read with it. */
555556
TEST(infrascan_servemux_pattern_split_issue2537) {
556557
char m[16];
557558
const char *pat = "POST /v1/payments";
@@ -579,6 +580,20 @@ TEST(infrascan_servemux_pattern_split_issue2537) {
579580
ASSERT_NULL(cbm_service_pattern_mux_split(NULL, m, sizeof(m)));
580581
ASSERT_NULL(cbm_service_pattern_mux_split("ABCDEFGHIJKLMNOP /x", m, sizeof(m)));
581582
ASSERT_STR_EQ(m, "");
583+
584+
/* only Handle / HandleFunc register a pattern, and never with a URL */
585+
pat = "POST /v1/payments";
586+
ASSERT_TRUE(cbm_service_pattern_mux_route_path("mux.HandleFunc", pat) == pat + 5);
587+
pat = "api.example.com/v1/x";
588+
ASSERT_TRUE(cbm_service_pattern_mux_route_path("s.mux.Handle", pat) == pat + 15);
589+
ASSERT_NULL(cbm_service_pattern_mux_route_path("st.Get", "users/123"));
590+
ASSERT_NULL(cbm_service_pattern_mux_route_path("c.Get", "https://api.example.com/v1/x"));
591+
ASSERT_NULL(cbm_service_pattern_mux_route_path("mux.Handle", "https://api.example.com/v1/x"));
592+
ASSERT_NULL(cbm_service_pattern_mux_route_path("PatchCollector.Delete", "storage.k8s.io/v1"));
593+
ASSERT_NULL(cbm_service_pattern_mux_route_path("r.Route", "GET /v1/x"));
594+
ASSERT_NULL(cbm_service_pattern_mux_route_path("mux.HandleFunc", "/v1/x"));
595+
ASSERT_NULL(cbm_service_pattern_mux_route_path(NULL, "GET /v1/x"));
596+
ASSERT_NULL(cbm_service_pattern_mux_route_path("mux.HandleFunc", NULL));
582597
PASS();
583598
}
584599

‎tests/test_pipeline.c‎

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13376,14 +13376,64 @@ TEST(pipeline_route_prefix_bridge_is_order_free_and_root_bridges_nothing) {
1337613376
PASS();
1337713377
}
1337813378

13379+
/* Route nodes whose qualified name contains `part`. */
13380+
static int route_nodes_containing(cbm_store_t *s, const char *project, const char *part) {
13381+
cbm_node_t *routes = NULL;
13382+
int route_count = 0;
13383+
if (cbm_store_find_nodes_by_label(s, project, "Route", &routes, &route_count) != CBM_STORE_OK) {
13384+
return -1;
13385+
}
13386+
int matches = 0;
13387+
for (int i = 0; i < route_count; i++) {
13388+
if (routes[i].qualified_name && strstr(routes[i].qualified_name, part)) {
13389+
matches++;
13390+
}
13391+
}
13392+
if (routes) {
13393+
cbm_store_free_nodes(routes, route_count);
13394+
}
13395+
return matches;
13396+
}
13397+
13398+
/* CALLS edges out of the function `source_name` into a Route node: the edge a
13399+
* route registration emits beside its Route (an HTTP client call's edge to a
13400+
* Route is HTTP_CALLS instead). */
13401+
static int calls_into_route_count(cbm_store_t *s, const char *project, const char *source_name) {
13402+
cbm_edge_t *edges = NULL;
13403+
int edge_count = 0;
13404+
if (cbm_store_find_edges_by_type(s, project, "CALLS", &edges, &edge_count) != CBM_STORE_OK) {
13405+
return -1;
13406+
}
13407+
int matches = 0;
13408+
for (int i = 0; i < edge_count; i++) {
13409+
cbm_node_t source = {0};
13410+
cbm_node_t target = {0};
13411+
int source_ok = cbm_store_find_node_by_id(s, edges[i].source_id, &source) == CBM_STORE_OK;
13412+
int target_ok = cbm_store_find_node_by_id(s, edges[i].target_id, &target) == CBM_STORE_OK;
13413+
if (source_ok && target_ok && source.name && target.label &&
13414+
strcmp(source.name, source_name) == 0 && strcmp(target.label, "Route") == 0) {
13415+
matches++;
13416+
}
13417+
cbm_node_free_fields(&source);
13418+
cbm_node_free_fields(&target);
13419+
}
13420+
if (edges) {
13421+
cbm_store_free_edges(edges, edge_count);
13422+
}
13423+
return matches;
13424+
}
13425+
1337913426
/* Issue #2537: since Go 1.22 a ServeMux pattern may name a method and a host
1338013427
* before the path ("POST /v1/payments", "GET api.example.com/v1/x"). Such a
1338113428
* registration minted no Route and no HANDLES edge, because every registration
1338213429
* gate wanted the first argument to open with a slash, while the same file
1338313430
* written with "/v1/payments" had both. The method now comes from the pattern
1338413431
* and a host is dropped from the path, in the sequential and the parallel pass
1338513432
* alike. Only Go reads that grammar: a Python registration of "GET /py" is still
13386-
* no route. */
13433+
* no route. Only a ServeMux registration (Handle / HandleFunc) is read with it:
13434+
* another route-verb call names no pattern and mints no Route, neither
13435+
* `st.Get("users/123")` on a project method (which keeps its CALLS edge) nor
13436+
* `c.Get("https://api.example.com/v1/x")` on a client. */
1338713437
TEST(pipeline_go_servemux_method_patterns_issue2537) {
1338813438
char tmp[256];
1338913439
snprintf(tmp, sizeof(tmp), "/tmp/cbm_servemux_XXXXXX");
@@ -13401,6 +13451,16 @@ TEST(pipeline_go_servemux_method_patterns_issue2537) {
1340113451
"func hostOnly(w http.ResponseWriter, r *http.Request) {}\n"
1340213452
"func legacyList(w http.ResponseWriter, r *http.Request) {}\n"
1340313453
"\n"
13454+
"type Store struct{ m map[string]string }\n"
13455+
"\n"
13456+
"func (s *Store) Get(k string) string { return s.m[k] }\n"
13457+
"\n"
13458+
"func lookup(st *Store) string { return st.Get(\"users/123\") }\n"
13459+
"\n"
13460+
"func fetchRemote(c *http.Client) {\n"
13461+
"\t_, _ = c.Get(\"https://api.example.com/v1/x\")\n"
13462+
"}\n"
13463+
"\n"
1340413464
"func main() {\n"
1340513465
"\tmux := http.NewServeMux()\n"
1340613466
"\tmux.HandleFunc(\"POST /v1/payments\", createPayment)\n"
@@ -13438,6 +13498,11 @@ TEST(pipeline_go_servemux_method_patterns_issue2537) {
1343813498
int legacy[2] = {-1, -1};
1343913499
int py_any[2] = {-1, -1};
1344013500
int py_get[2] = {-1, -1};
13501+
int store_calls[2] = {-1, -1};
13502+
int store_route[2] = {-1, -1};
13503+
int lookup_reg[2] = {-1, -1};
13504+
int remote_route[2] = {-1, -1};
13505+
int remote_reg[2] = {-1, -1};
1344113506
for (int mode = 0; mode < 2; mode++) {
1344213507
if (mode == 0) {
1344313508
cbm_setenv("CBM_INDEX_SINGLE_THREAD", "1", 1);
@@ -13463,6 +13528,11 @@ TEST(pipeline_go_servemux_method_patterns_issue2537) {
1346313528
legacy[mode] = handles_to_route(s, project, "legacyList", "__route__ANY__/v1/legacy");
1346413529
py_any[mode] = handles_to_route(s, project, "py_view", "__route__ANY__/py");
1346513530
py_get[mode] = handles_to_route(s, project, "py_view", "__route__GET__/py");
13531+
store_calls[mode] = named_edge_count(s, project, "CALLS", "lookup", "Get");
13532+
store_route[mode] = route_nodes_containing(s, project, "__route__GET__/123");
13533+
lookup_reg[mode] = calls_into_route_count(s, project, "lookup");
13534+
remote_route[mode] = route_nodes_containing(s, project, "//api.example.com");
13535+
remote_reg[mode] = calls_into_route_count(s, project, "fetchRemote");
1346613536
}
1346713537
if (s) {
1346813538
cbm_store_close(s);
@@ -13496,6 +13566,14 @@ TEST(pipeline_go_servemux_method_patterns_issue2537) {
1349613566
/* another language's "GET /py" is not read as a ServeMux pattern */
1349713567
ASSERT_EQ(py_any[mode], 0);
1349813568
ASSERT_EQ(py_get[mode], 0);
13569+
/* a project method named Get keeps its CALLS edge; "users/123" is no
13570+
* host + path */
13571+
ASSERT_EQ(store_calls[mode], 1);
13572+
ASSERT_EQ(store_route[mode], 0);
13573+
ASSERT_EQ(lookup_reg[mode], 0);
13574+
/* a client Get on a URL registers no route: "https:" is no host */
13575+
ASSERT_EQ(remote_route[mode], 0);
13576+
ASSERT_EQ(remote_reg[mode], 0);
1349913577
}
1350013578
PASS();
1350113579
}

0 commit comments

Comments
 (0)