Skip to content

Commit ad78993

Browse files
committed
fix(routes): read Go 1.22 ServeMux method and host patterns
Since Go 1.22 a net/http ServeMux pattern may name a method and a host before the path: mux.HandleFunc("POST /v1/payments", h), "GET /v1/payments/{id}", "api.example.com/v1/x". Every route registration gate wanted the first argument to open with a slash (the extractor's handler capture, find_route_path_in_args in the parallel pass, and the three gates in the sequential pass), so such a registration minted no Route and no HANDLES edge, while the same file written with "/v1/payments" had both. cbm_service_pattern_mux_split reads the "[METHOD ][HOST]/[PATH]" grammar. For Go files only, the path of such a pattern now registers the route, the Route takes the pattern's method when the callee names none (Handle/HandleFunc are ANY), and a host is dropped from the path. Both passes share cbm_pipeline_mux_route_path and cbm_pipeline_route_reg_method so their Routes stay identical. Other languages and plain "/path" patterns take the unchanged path. Fixes DeusData#2537 Signed-off-by: Gaurav Dubey <gauravdubey0107@gmail.com>
1 parent 3457b21 commit ad78993

9 files changed

Lines changed: 327 additions & 13 deletions

File tree

‎internal/cbm/extract_calls.c‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4553,8 +4553,13 @@ CBMInvocationDescriptor handle_calls(CBMExtractCtx *ctx, TSNode node, const CBML
45534553
call.first_string_arg = route_path_with_group_prefix(ctx, node, call.callee_name,
45544554
call.first_string_arg);
45554555
/* A slashless registration's string action would only be
4556-
* guessed at (#1146): mint its Route without a handler. */
4557-
if (call.first_string_arg && call.first_string_arg[0] == '/' &&
4556+
* guessed at (#1146): mint its Route without a handler. A Go
4557+
* ServeMux pattern names its method or host before the slash
4558+
* ("POST /v1/x", #2537) and keeps its handler. */
4559+
if (call.first_string_arg &&
4560+
(call.first_string_arg[0] == '/' ||
4561+
(ctx->language == CBM_LANG_GO &&
4562+
cbm_service_pattern_mux_split(call.first_string_arg, NULL, 0) != NULL)) &&
45584563
!(laravel_uri && php_route_action_is_string(node))) {
45594564
call.second_arg_name = extract_handler_arg(ctx, args);
45604565
}

‎internal/cbm/service_patterns.c‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1183,6 +1183,50 @@ const char *cbm_service_pattern_route_method(const char *callee_name) {
11831183
return NULL;
11841184
}
11851185

1186+
static bool mux_is_blank(char ch) {
1187+
return ch == ' ' || ch == '\t';
1188+
}
1189+
1190+
const char *cbm_service_pattern_mux_split(const char *pattern, char *method, size_t method_sz) {
1191+
if (method && method_sz > 0) {
1192+
method[0] = '\0';
1193+
}
1194+
if (!pattern || pattern[0] == '/') {
1195+
return NULL;
1196+
}
1197+
/* METHOD: Go cuts it at the first space or tab and trims the rest. */
1198+
const char *rest = pattern;
1199+
size_t method_len = 0;
1200+
while (pattern[method_len] >= 'A' && pattern[method_len] <= 'Z') {
1201+
method_len++;
1202+
}
1203+
if (method_len > 0 && mux_is_blank(pattern[method_len])) {
1204+
rest = pattern + method_len;
1205+
while (mux_is_blank(*rest)) {
1206+
rest++;
1207+
}
1208+
} else {
1209+
method_len = 0;
1210+
}
1211+
/* HOST: everything before the path's slash, with no blank or line break. */
1212+
const char *slash = rest;
1213+
while (*slash && *slash != '/' && !mux_is_blank(*slash) && *slash != '\n' && *slash != '\r') {
1214+
slash++;
1215+
}
1216+
if (*slash != '/' || (method_len == 0 && slash == rest)) {
1217+
return NULL;
1218+
}
1219+
if (method_len > 0 && method) {
1220+
if (method_len >= method_sz) {
1221+
method[0] = '\0';
1222+
return NULL;
1223+
}
1224+
memcpy(method, pattern, method_len);
1225+
method[method_len] = '\0';
1226+
}
1227+
return slash;
1228+
}
1229+
11861230
const char *cbm_service_pattern_broker(const char *resolved_qn) {
11871231
if (!resolved_qn) {
11881232
return NULL;

‎internal/cbm/service_patterns.h‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,17 @@ const char *cbm_service_pattern_http_method(const char *callee_name);
8484
* Returns NULL if not a known route registration method. */
8585
const char *cbm_service_pattern_route_method(const char *callee_name);
8686

87+
/* Go 1.22+ net/http ServeMux pattern "[METHOD ][HOST]/[PATH]" (#2537):
88+
* "POST /v1/x", "GET api.example.com/v1/x", "api.example.com/v1/x". Returns a
89+
* pointer to PATH's leading slash inside `pattern`, or NULL when the pattern
90+
* names neither a method nor a host before a slash (a plain "/v1/x" is NULL
91+
* too: there is nothing to split). METHOD is a run of uppercase letters ended
92+
* by spaces or tabs; it is copied to `method` (NUL-terminated, "" when the
93+
* pattern names none) when `method` is non-NULL; a method that does not fit
94+
* `method_sz` then makes the pattern NULL. The grammar is Go's: callers gate
95+
* on the file language. */
96+
const char *cbm_service_pattern_mux_split(const char *pattern, char *method, size_t method_sz);
97+
8798
/* Get the broker name for an async QN (e.g., "pubsub" from a Pub/Sub QN).
8899
* Returns NULL if not an async pattern. */
89100
const char *cbm_service_pattern_broker(const char *resolved_qn);

‎src/pipeline/pass_calls.c‎

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -571,17 +571,33 @@ cbm_svc_kind_t cbm_pipeline_external_import_service(cbm_external_import_index_t
571571
return CBM_SVC_NONE;
572572
}
573573

574+
/* The path a route registration's first argument names: the argument itself
575+
* 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. */
578+
static const char *route_reg_path(const CBMCall *call, CBMLanguage lang) {
579+
const char *arg = call->first_string_arg;
580+
if (!arg) {
581+
return NULL;
582+
}
583+
if (arg[0] == '/') {
584+
return arg;
585+
}
586+
return cbm_pipeline_mux_route_path(arg, lang);
587+
}
588+
574589
/* Handle a route registration call: create Route node + HANDLES edge.
575590
* route_mount is the framework mount of the registering file ("/api" for a
576591
* Laravel 11+ `withRouting(api: ...)` file, #1146) or "". */
577592
static void handle_route_registration(cbm_pipeline_ctx_t *ctx, const CBMCall *call,
578593
const cbm_gbuf_node_t *source_node, const char *module_qn,
579594
const char **imp_keys, const char **imp_vals, int imp_count,
580595
const char *route_mount, CBMLanguage lang) {
581-
const char *method = cbm_service_pattern_route_method(call->callee_name);
596+
char mux_method[CBM_SZ_16];
597+
const char *method = cbm_pipeline_route_reg_method(call, lang, mux_method, sizeof(mux_method));
582598
char mounted[CBM_SZ_256];
583599
const char *path =
584-
cbm_laravel_mount_route(route_mount, call->first_string_arg, mounted, sizeof(mounted));
600+
cbm_laravel_mount_route(route_mount, route_reg_path(call, lang), mounted, sizeof(mounted));
585601
char route_qn[CBM_ROUTE_QN_SIZE];
586602
char cpath[CBM_SZ_256];
587603
snprintf(route_qn, sizeof(route_qn), "__route__%s__%s", method ? method : "ANY",
@@ -815,7 +831,7 @@ static void emit_classified_edge(cbm_pipeline_ctx_t *ctx, const CBMCall *call,
815831
bool suppress_plain_calls, const char *route_mount,
816832
CBMLanguage lang) {
817833
cbm_svc_kind_t svc = cbm_service_pattern_match(res->qualified_name);
818-
if (svc == CBM_SVC_ROUTE_REG && call->first_string_arg && call->first_string_arg[0] == '/') {
834+
if (svc == CBM_SVC_ROUTE_REG && route_reg_path(call, lang) != NULL) {
819835
handle_route_registration(ctx, call, source, module_qn, imp_keys, imp_vals, imp_count,
820836
route_mount, lang);
821837
return;
@@ -1042,8 +1058,8 @@ static int resolve_single_call(cbm_pipeline_ctx_t *ctx, CBMCall *call, const CBM
10421058
* apps. Classify by callee suffix + path-shaped first arg, exactly
10431059
* like the parallel path's callee_suffix fallback; without this the
10441060
* sequential path minted zero Route nodes for such files. */
1045-
if (cbm_service_pattern_route_method(call->callee_name) != NULL && call->first_string_arg &&
1046-
call->first_string_arg[0] == '/') {
1061+
if (cbm_service_pattern_route_method(call->callee_name) != NULL &&
1062+
route_reg_path(call, lang) != NULL) {
10471063
handle_route_registration(ctx, call, source_node, module_qn, imp_keys, imp_vals,
10481064
imp_count, route_mount, lang);
10491065
return SKIP_ONE;
@@ -1220,8 +1236,8 @@ static int resolve_single_call(cbm_pipeline_ctx_t *ctx, CBMCall *call, const CBM
12201236
* unresolved-callee fallback above mints, and nothing else. Dropping
12211237
* the whole call lost every GET registration in a mixed-language repo
12221238
* while POST (no `post` to collide with) survived. */
1223-
if (cbm_service_pattern_route_method(call->callee_name) != NULL && call->first_string_arg &&
1224-
call->first_string_arg[0] == '/') {
1239+
if (cbm_service_pattern_route_method(call->callee_name) != NULL &&
1240+
route_reg_path(call, lang) != NULL) {
12251241
handle_route_registration(ctx, call, source_node, module_qn, imp_keys, imp_vals,
12261242
imp_count, route_mount, lang);
12271243
return SKIP_ONE;

‎src/pipeline/pass_parallel.c‎

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

2266-
static const char *find_route_path_in_args(const CBMCall *call, const char **out_handler) {
2266+
const char *cbm_pipeline_mux_route_path(const char *arg, CBMLanguage lang) {
2267+
if (lang != CBM_LANG_GO || !arg || cbm_service_pattern_is_comment_text(arg)) {
2268+
return NULL;
2269+
}
2270+
return cbm_service_pattern_mux_split(arg, NULL, 0);
2271+
}
2272+
2273+
const char *cbm_pipeline_route_reg_method(const CBMCall *call, CBMLanguage lang, char *buf,
2274+
size_t buf_sz) {
2275+
const char *method = cbm_service_pattern_route_method(call->callee_name);
2276+
if (method && strcmp(method, "ANY") != 0) {
2277+
return method;
2278+
}
2279+
if (cbm_pipeline_mux_route_path(call->first_string_arg, lang) &&
2280+
cbm_service_pattern_mux_split(call->first_string_arg, buf, buf_sz) && buf[0] != '\0') {
2281+
return buf;
2282+
}
2283+
return method;
2284+
}
2285+
2286+
static const char *find_route_path_in_args(const CBMCall *call, const char **out_handler,
2287+
CBMLanguage lang) {
22672288
*out_handler = NULL;
22682289
/* 1. First string arg starting with / */
22692290
if (is_route_path_shaped(call->first_string_arg)) {
22702291
*out_handler = call->second_arg_name;
22712292
return call->first_string_arg;
22722293
}
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);
2297+
if (mux_path) {
2298+
*out_handler = call->second_arg_name;
2299+
return mux_path;
2300+
}
22732301
/* 2. Keyword args (prefix=, path=, route=, etc.) */
22742302
const char *found = NULL;
22752303
for (int ai = 0; ai < call->arg_count && !found; ai++) {
@@ -2428,7 +2456,8 @@ static void emit_route_registration(cbm_gbuf_t *gbuf, const cbm_gbuf_node_t *sou
24282456
const cbm_registry_t *registry, const cbm_gbuf_t *main_gbuf,
24292457
const char **ik, const char **iv, int ic,
24302458
const char *route_mount, CBMLanguage lang) {
2431-
const char *method = cbm_service_pattern_route_method(call->callee_name);
2459+
char mux_method[CBM_SZ_16];
2460+
const char *method = cbm_pipeline_route_reg_method(call, lang, mux_method, sizeof(mux_method));
24322461
char mounted[CBM_SZ_256];
24332462
route_path = cbm_laravel_mount_route(route_mount, route_path, mounted, sizeof(mounted));
24342463
char rqn[CBM_ROUTE_QN_SIZE];
@@ -2813,7 +2842,7 @@ static void emit_service_edge(cbm_gbuf_t *gbuf, const cbm_gbuf_node_t *source,
28132842

28142843
if (svc == CBM_SVC_ROUTE_REG) {
28152844
const char *handler_ref = NULL;
2816-
const char *route_path = find_route_path_in_args(call, &handler_ref);
2845+
const char *route_path = find_route_path_in_args(call, &handler_ref, lang);
28172846
if (route_path) {
28182847
emit_route_registration(gbuf, source, call, route_path, handler_ref, module_qn,
28192848
registry, main_gbuf, imp_keys, imp_vals, imp_count, route_mount,
@@ -2876,7 +2905,7 @@ static void emit_xlang_refused_route(cbm_gbuf_t *gbuf, const cbm_gbuf_node_t *so
28762905
return;
28772906
}
28782907
const char *handler_ref = NULL;
2879-
const char *route_path = find_route_path_in_args(call, &handler_ref);
2908+
const char *route_path = find_route_path_in_args(call, &handler_ref, lang);
28802909
if (route_path) {
28812910
emit_route_registration(gbuf, source, call, route_path, handler_ref, module_qn, registry,
28822911
main_gbuf, imp_keys, imp_vals, imp_count, route_mount, lang);

‎src/pipeline/pipeline_internal.h‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -787,6 +787,21 @@ bool cbm_pipeline_http_client_call_url(const cbm_gbuf_t *gbuf, const char *proje
787787
* behind both passes' HTTP_CALLS / ASYNC_CALLS props (pass_parallel.c). */
788788
void cbm_pipeline_close_call_props(char *props, int n, const CBMCall *call, bool with_line);
789789

790+
/* 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);
796+
797+
/* The method of a route registration's Route: the verb its callee names
798+
* (`router.GET`), else the method its Go ServeMux pattern names
799+
* (`mux.HandleFunc("POST /v1/x", h)`, #2537), else the callee's own answer
800+
* (ANY or NULL). `buf` (CBM_SZ_16) holds a pattern method. Shared by both
801+
* passes so their Routes stay byte-identical (pass_parallel.c). */
802+
const char *cbm_pipeline_route_reg_method(const CBMCall *call, CBMLanguage lang, char *buf,
803+
size_t buf_sz);
804+
790805
/* Per-file index of the names a file's imports bind onto EXTERNAL modules:
791806
* local name -> the binding import. Built lazily, on the first call that
792807
* asks, from the file's extraction imports and its IMPORTS edges; one hash

‎tests/test_extraction.c‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6207,6 +6207,40 @@ TEST(extract_go_binary_concat_url_no_literal_suffix_issue1249) {
62076207
PASS();
62086208
}
62096209

6210+
/* Issue #2537: a Go 1.22 ServeMux pattern may open with a method and/or a host
6211+
* ("POST /v1/payments", "GET api.example.com/v1/x"). The written pattern stays
6212+
* the first string argument, and its handler is captured exactly as for a
6213+
* plain "/v1/payments"; it was dropped because the pattern does not open with
6214+
* a slash. */
6215+
TEST(extract_go_servemux_method_pattern_handler_issue2537) {
6216+
CBMFileResult *r = extract("package main\n"
6217+
"import \"net/http\"\n"
6218+
"func setup(mux *http.ServeMux) {\n"
6219+
" mux.HandleFunc(\"POST /v1/payments\", createPayment)\n"
6220+
" mux.Handle(\"GET api.example.com/v1/x\", hostHandler)\n"
6221+
"}\n",
6222+
CBM_LANG_GO, "t", "routes.go");
6223+
ASSERT_NOT_NULL(r);
6224+
ASSERT_FALSE(r->has_error);
6225+
6226+
const CBMCall *method = find_call_by_callee(r, "mux.HandleFunc");
6227+
ASSERT_NOT_NULL(method);
6228+
ASSERT_NOT_NULL(method->first_string_arg);
6229+
ASSERT_STR_EQ(method->first_string_arg, "POST /v1/payments");
6230+
ASSERT_NOT_NULL(method->second_arg_name);
6231+
ASSERT_STR_EQ(method->second_arg_name, "createPayment");
6232+
6233+
const CBMCall *host = find_call_by_callee(r, "mux.Handle");
6234+
ASSERT_NOT_NULL(host);
6235+
ASSERT_NOT_NULL(host->first_string_arg);
6236+
ASSERT_STR_EQ(host->first_string_arg, "GET api.example.com/v1/x");
6237+
ASSERT_NOT_NULL(host->second_arg_name);
6238+
ASSERT_STR_EQ(host->second_arg_name, "hostHandler");
6239+
6240+
cbm_free_result(r);
6241+
PASS();
6242+
}
6243+
62106244
/* Issue #2291: the JS/TS twin of #1249. A BFF that builds its backend URL as
62116245
* `${process.env.BACKEND_URL}/wallet/topup` from a configurable base must
62126246
* index the literal path after the base, exactly as `BACKEND_URL + "/x"`
@@ -13442,6 +13476,7 @@ SUITE(extraction) {
1344213476
RUN_TEST(extract_ts_config_object_url_issue2235);
1344313477
RUN_TEST(extract_go_binary_concat_url_issue1249);
1344413478
RUN_TEST(extract_go_binary_concat_url_no_literal_suffix_issue1249);
13479+
RUN_TEST(extract_go_servemux_method_pattern_handler_issue2537);
1344513480
RUN_TEST(extract_ts_template_base_url_issue2291);
1344613481
RUN_TEST(extract_ts_template_base_url_no_literal_path_issue2291);
1344713482
RUN_TEST(extract_py_concat_const_url_issue706);

‎tests/test_infrascan.c‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,12 +549,46 @@ TEST(infrascan_data_flows_test_handlers_pair_only_within_their_file) {
549549
PASS();
550550
}
551551

552+
/* Issue #2537: the Go 1.22 ServeMux grammar "[METHOD ][HOST]/[PATH]". The path
553+
* 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. */
555+
TEST(infrascan_servemux_pattern_split_issue2537) {
556+
char m[16];
557+
const char *pat = "POST /v1/payments";
558+
ASSERT_TRUE(cbm_service_pattern_mux_split(pat, m, sizeof(m)) == pat + 5);
559+
ASSERT_STR_EQ(m, "POST");
560+
pat = "GET \t /v1/payments/{id}";
561+
ASSERT_TRUE(cbm_service_pattern_mux_split(pat, m, sizeof(m)) == pat + 6);
562+
ASSERT_STR_EQ(m, "GET");
563+
pat = "GET api.example.com/v1/x";
564+
ASSERT_TRUE(cbm_service_pattern_mux_split(pat, m, sizeof(m)) == pat + 19);
565+
ASSERT_STR_EQ(m, "GET");
566+
pat = "api.example.com:8443/v1/x";
567+
ASSERT_TRUE(cbm_service_pattern_mux_split(pat, m, sizeof(m)) == pat + 20);
568+
ASSERT_STR_EQ(m, "");
569+
pat = "DELETE /";
570+
ASSERT_TRUE(cbm_service_pattern_mux_split(pat, NULL, 0) == pat + 7);
571+
572+
ASSERT_NULL(cbm_service_pattern_mux_split("/v1/x", m, sizeof(m)));
573+
ASSERT_NULL(cbm_service_pattern_mux_split("get /v1/x", m, sizeof(m)));
574+
ASSERT_NULL(cbm_service_pattern_mux_split("POST", m, sizeof(m)));
575+
ASSERT_NULL(cbm_service_pattern_mux_split("POST ", m, sizeof(m)));
576+
ASSERT_NULL(cbm_service_pattern_mux_split("GET host", m, sizeof(m)));
577+
ASSERT_NULL(cbm_service_pattern_mux_split("GET a b/x", m, sizeof(m)));
578+
ASSERT_NULL(cbm_service_pattern_mux_split("", m, sizeof(m)));
579+
ASSERT_NULL(cbm_service_pattern_mux_split(NULL, m, sizeof(m)));
580+
ASSERT_NULL(cbm_service_pattern_mux_split("ABCDEFGHIJKLMNOP /x", m, sizeof(m)));
581+
ASSERT_STR_EQ(m, "");
582+
PASS();
583+
}
584+
552585
SUITE(infrascan) {
553586
RUN_TEST(infrascan_http_route_literal_guard_rejects_filesystem_paths);
554587
RUN_TEST(infrascan_http_route_literal_guard_rejects_document_extensions);
555588
RUN_TEST(infrascan_client_navigation_is_js_family_only);
556589
RUN_TEST(infrascan_service_pattern_match_rejects_ids_inside_words);
557590
RUN_TEST(infrascan_service_pattern_match_keeps_real_library_qns);
591+
RUN_TEST(infrascan_servemux_pattern_split_issue2537);
558592
RUN_TEST(infrascan_route_nodes_skip_bad_http_url_paths);
559593
RUN_TEST(infrascan_http_calls_join_matching_handler_route);
560594
RUN_TEST(infrascan_data_flows_link_every_caller_of_a_route);

0 commit comments

Comments
 (0)