Skip to content

Commit 747665d

Browse files
authored
Merge pull request DeusData#2752 from gaurav0107/fix/2537-go-net-http-servemux-patterns-with-a-met
fix(routes): read Go 1.22 ServeMux method and host patterns
2 parents e082387 + 3e075d5 commit 747665d

9 files changed

Lines changed: 445 additions & 11 deletions

File tree

‎internal/cbm/extract_calls.c‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4557,8 +4557,14 @@ CBMInvocationDescriptor handle_calls(CBMExtractCtx *ctx, TSNode node, const CBML
45574557
call.first_string_arg = route_path_with_group_prefix(ctx, node, call.callee_name,
45584558
call.first_string_arg);
45594559
/* A slashless registration's string action would only be
4560-
* guessed at (#1146): mint its Route without a handler. */
4561-
if (call.first_string_arg && call.first_string_arg[0] == '/' &&
4560+
* guessed at (#1146): mint its Route without a handler. A Go
4561+
* ServeMux pattern names its method or host before the slash
4562+
* ("POST /v1/x", #2537) and keeps its handler. */
4563+
if (call.first_string_arg &&
4564+
(call.first_string_arg[0] == '/' ||
4565+
(ctx->language == CBM_LANG_GO &&
4566+
cbm_service_pattern_mux_route_path(call.callee_name, call.first_string_arg) !=
4567+
NULL)) &&
45624568
!(laravel_uri && php_route_action_is_string(node))) {
45634569
call.second_arg_name = extract_handler_arg(ctx, args);
45644570
}

‎internal/cbm/service_patterns.c‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1183,6 +1183,65 @@ 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+
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+
11861245
const char *cbm_service_pattern_broker(const char *resolved_qn) {
11871246
if (!resolved_qn) {
11881247
return NULL;

‎internal/cbm/service_patterns.h‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,25 @@ 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+
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+
87106
/* Get the broker name for an async QN (e.g., "pubsub" from a Pub/Sub QN).
88107
* Returns NULL if not an async pattern. */
89108
const char *cbm_service_pattern_broker(const char *resolved_qn);

‎src/pipeline/pass_calls.c‎

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -577,17 +577,34 @@ cbm_svc_kind_t cbm_pipeline_external_import_service(cbm_external_import_index_t
577577
return CBM_SVC_NONE;
578578
}
579579

580+
/* The path a route registration's first argument names: the argument itself
581+
* when it opens with a slash, or in Go the path of a ServeMux pattern that
582+
* names a method or host first ("POST /v1/x", #2537; Handle / HandleFunc
583+
* only). NULL when it names no path. MUST match pass_parallel.c
584+
* find_route_path_in_args. */
585+
static const char *route_reg_path(const CBMCall *call, CBMLanguage lang) {
586+
const char *arg = call->first_string_arg;
587+
if (!arg) {
588+
return NULL;
589+
}
590+
if (arg[0] == '/') {
591+
return arg;
592+
}
593+
return cbm_pipeline_mux_route_path(call, lang);
594+
}
595+
580596
/* Handle a route registration call: create Route node + HANDLES edge.
581597
* route_mount is the framework mount of the registering file ("/api" for a
582598
* Laravel 11+ `withRouting(api: ...)` file, #1146) or "". */
583599
static bool handle_route_registration(cbm_pipeline_ctx_t *ctx, const CBMCall *call,
584600
const cbm_gbuf_node_t *source_node, const char *module_qn,
585601
const char **imp_keys, const char **imp_vals, int imp_count,
586602
const char *route_mount, CBMLanguage lang) {
587-
const char *method = cbm_service_pattern_route_method(call->callee_name);
603+
char mux_method[CBM_SZ_16];
604+
const char *method = cbm_pipeline_route_reg_method(call, lang, mux_method, sizeof(mux_method));
588605
char mounted[CBM_SZ_256];
589606
const char *path =
590-
cbm_laravel_mount_route(route_mount, call->first_string_arg, mounted, sizeof(mounted));
607+
cbm_laravel_mount_route(route_mount, route_reg_path(call, lang), mounted, sizeof(mounted));
591608
char route_qn[CBM_ROUTE_QN_SIZE];
592609
char cpath[CBM_SZ_256];
593610
snprintf(route_qn, sizeof(route_qn), "__route__%s__%s", method ? method : "ANY",
@@ -824,7 +841,7 @@ static bool emit_classified_edge(cbm_pipeline_ctx_t *ctx, const CBMCall *call,
824841
bool suppress_plain_calls, const char *route_mount,
825842
CBMLanguage lang) {
826843
cbm_svc_kind_t svc = cbm_service_pattern_match(res->qualified_name);
827-
if (svc == CBM_SVC_ROUTE_REG && call->first_string_arg && call->first_string_arg[0] == '/') {
844+
if (svc == CBM_SVC_ROUTE_REG && route_reg_path(call, lang) != NULL) {
828845
return handle_route_registration(ctx, call, source, module_qn, imp_keys, imp_vals,
829846
imp_count, route_mount, lang);
830847
}
@@ -924,8 +941,8 @@ static bool calls_emit_route_call(cbm_pipeline_ctx_t *ctx, const CBMCall *call,
924941
const char **imp_keys, const char **imp_vals, int imp_count,
925942
const char *route_mount, CBMLanguage lang,
926943
CBMCallEvidence *evidence) {
927-
if (cbm_service_pattern_route_method(call->callee_name) == NULL || !call->first_string_arg ||
928-
call->first_string_arg[0] != '/') {
944+
if (cbm_service_pattern_route_method(call->callee_name) == NULL ||
945+
route_reg_path(call, lang) == NULL) {
929946
return false;
930947
}
931948
evidence->calls_emitted |= handle_route_registration(

‎src/pipeline/pass_parallel.c‎

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2272,13 +2272,42 @@ static bool is_route_path_shaped(const char *val) {
22722272
return val && val[0] == '/' && !cbm_service_pattern_is_comment_text(val);
22732273
}
22742274

2275-
static const char *find_route_path_in_args(const CBMCall *call, const char **out_handler) {
2275+
const char *cbm_pipeline_mux_route_path(const CBMCall *call, CBMLanguage lang) {
2276+
const char *arg = call->first_string_arg;
2277+
if (lang != CBM_LANG_GO || !arg || cbm_service_pattern_is_comment_text(arg)) {
2278+
return NULL;
2279+
}
2280+
return cbm_service_pattern_mux_route_path(call->callee_name, arg);
2281+
}
2282+
2283+
const char *cbm_pipeline_route_reg_method(const CBMCall *call, CBMLanguage lang, char *buf,
2284+
size_t buf_sz) {
2285+
const char *method = cbm_service_pattern_route_method(call->callee_name);
2286+
if (method && strcmp(method, "ANY") != 0) {
2287+
return method;
2288+
}
2289+
if (cbm_pipeline_mux_route_path(call, lang) &&
2290+
cbm_service_pattern_mux_split(call->first_string_arg, buf, buf_sz) && buf[0] != '\0') {
2291+
return buf;
2292+
}
2293+
return method;
2294+
}
2295+
2296+
static const char *find_route_path_in_args(const CBMCall *call, const char **out_handler,
2297+
CBMLanguage lang) {
22762298
*out_handler = NULL;
22772299
/* 1. First string arg starting with / */
22782300
if (is_route_path_shaped(call->first_string_arg)) {
22792301
*out_handler = call->second_arg_name;
22802302
return call->first_string_arg;
22812303
}
2304+
/* 1b. Go ServeMux pattern with a method or host before the path (#2537),
2305+
* read for Handle / HandleFunc only. MUST match pass_calls.c route_reg_path. */
2306+
const char *mux_path = cbm_pipeline_mux_route_path(call, lang);
2307+
if (mux_path) {
2308+
*out_handler = call->second_arg_name;
2309+
return mux_path;
2310+
}
22822311
/* 2. Keyword args (prefix=, path=, route=, etc.) */
22832312
const char *found = NULL;
22842313
for (int ai = 0; ai < call->arg_count && !found; ai++) {
@@ -2438,7 +2467,8 @@ static bool emit_route_registration(cbm_gbuf_t *gbuf, const cbm_gbuf_node_t *sou
24382467
const cbm_registry_t *registry, const cbm_gbuf_t *main_gbuf,
24392468
const char **ik, const char **iv, int ic,
24402469
const char *route_mount, CBMLanguage lang) {
2441-
const char *method = cbm_service_pattern_route_method(call->callee_name);
2470+
char mux_method[CBM_SZ_16];
2471+
const char *method = cbm_pipeline_route_reg_method(call, lang, mux_method, sizeof(mux_method));
24422472
char mounted[CBM_SZ_256];
24432473
route_path = cbm_laravel_mount_route(route_mount, route_path, mounted, sizeof(mounted));
24442474
char rqn[CBM_ROUTE_QN_SIZE];
@@ -2824,7 +2854,7 @@ static bool emit_service_edge(cbm_gbuf_t *gbuf, const cbm_gbuf_node_t *source,
28242854

28252855
if (svc == CBM_SVC_ROUTE_REG) {
28262856
const char *handler_ref = NULL;
2827-
const char *route_path = find_route_path_in_args(call, &handler_ref);
2857+
const char *route_path = find_route_path_in_args(call, &handler_ref, lang);
28282858
if (route_path) {
28292859
return emit_route_registration(gbuf, source, call, route_path, handler_ref, module_qn,
28302860
registry, main_gbuf, imp_keys, imp_vals, imp_count,
@@ -2888,7 +2918,7 @@ static bool emit_xlang_refused_route(cbm_gbuf_t *gbuf, const cbm_gbuf_node_t *so
28882918
return false;
28892919
}
28902920
const char *handler_ref = NULL;
2891-
const char *route_path = find_route_path_in_args(call, &handler_ref);
2921+
const char *route_path = find_route_path_in_args(call, &handler_ref, lang);
28922922
if (route_path) {
28932923
return emit_route_registration(gbuf, source, call, route_path, handler_ref, module_qn,
28942924
registry, main_gbuf, imp_keys, imp_vals, imp_count,

‎src/pipeline/pipeline_internal.h‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -797,6 +797,22 @@ bool cbm_pipeline_http_client_call_url(const cbm_gbuf_t *gbuf, const char *proje
797797
* behind both passes' HTTP_CALLS / ASYNC_CALLS props (pass_parallel.c). */
798798
void cbm_pipeline_close_call_props(char *props, int n, const CBMCall *call, bool with_line);
799799

800+
/* Go 1.22 ServeMux registrations may name a method and a host before the path
801+
* ("POST /v1/x", "GET api.example.com/v1/x", #2537). For a Go `Handle` /
802+
* `HandleFunc` call, the path inside its first argument; NULL for any other
803+
* language or callee (`store.Get("users/123")`), for a URL argument, and for an
804+
* argument that is no such pattern (a plain "/v1/x" included). The one reader
805+
* of that grammar behind both passes (pass_parallel.c). */
806+
const char *cbm_pipeline_mux_route_path(const CBMCall *call, CBMLanguage lang);
807+
808+
/* The method of a route registration's Route: the verb its callee names
809+
* (`router.GET`), else the method its Go ServeMux pattern names
810+
* (`mux.HandleFunc("POST /v1/x", h)`, #2537), else the callee's own answer
811+
* (ANY or NULL). `buf` (CBM_SZ_16) holds a pattern method. Shared by both
812+
* passes so their Routes stay byte-identical (pass_parallel.c). */
813+
const char *cbm_pipeline_route_reg_method(const CBMCall *call, CBMLanguage lang, char *buf,
814+
size_t buf_sz);
815+
800816
/* Per-file index of the names a file's imports bind onto EXTERNAL modules:
801817
* local name -> the binding import. Built lazily, on the first call that
802818
* 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
@@ -6431,6 +6431,40 @@ TEST(extract_go_binary_concat_url_no_literal_suffix_issue1249) {
64316431
PASS();
64326432
}
64336433

6434+
/* Issue #2537: a Go 1.22 ServeMux pattern may open with a method and/or a host
6435+
* ("POST /v1/payments", "GET api.example.com/v1/x"). The written pattern stays
6436+
* the first string argument, and its handler is captured exactly as for a
6437+
* plain "/v1/payments"; it was dropped because the pattern does not open with
6438+
* a slash. */
6439+
TEST(extract_go_servemux_method_pattern_handler_issue2537) {
6440+
CBMFileResult *r = extract("package main\n"
6441+
"import \"net/http\"\n"
6442+
"func setup(mux *http.ServeMux) {\n"
6443+
" mux.HandleFunc(\"POST /v1/payments\", createPayment)\n"
6444+
" mux.Handle(\"GET api.example.com/v1/x\", hostHandler)\n"
6445+
"}\n",
6446+
CBM_LANG_GO, "t", "routes.go");
6447+
ASSERT_NOT_NULL(r);
6448+
ASSERT_FALSE(r->has_error);
6449+
6450+
const CBMCall *method = find_call_by_callee(r, "mux.HandleFunc");
6451+
ASSERT_NOT_NULL(method);
6452+
ASSERT_NOT_NULL(method->first_string_arg);
6453+
ASSERT_STR_EQ(method->first_string_arg, "POST /v1/payments");
6454+
ASSERT_NOT_NULL(method->second_arg_name);
6455+
ASSERT_STR_EQ(method->second_arg_name, "createPayment");
6456+
6457+
const CBMCall *host = find_call_by_callee(r, "mux.Handle");
6458+
ASSERT_NOT_NULL(host);
6459+
ASSERT_NOT_NULL(host->first_string_arg);
6460+
ASSERT_STR_EQ(host->first_string_arg, "GET api.example.com/v1/x");
6461+
ASSERT_NOT_NULL(host->second_arg_name);
6462+
ASSERT_STR_EQ(host->second_arg_name, "hostHandler");
6463+
6464+
cbm_free_result(r);
6465+
PASS();
6466+
}
6467+
64346468
/* Issue #2291: the JS/TS twin of #1249. A BFF that builds its backend URL as
64356469
* `${process.env.BACKEND_URL}/wallet/topup` from a configurable base must
64366470
* index the literal path after the base, exactly as `BACKEND_URL + "/x"`
@@ -13970,6 +14004,7 @@ SUITE(extraction) {
1397014004
RUN_TEST(extract_ts_config_object_url_issue2235);
1397114005
RUN_TEST(extract_go_binary_concat_url_issue1249);
1397214006
RUN_TEST(extract_go_binary_concat_url_no_literal_suffix_issue1249);
14007+
RUN_TEST(extract_go_servemux_method_pattern_handler_issue2537);
1397314008
RUN_TEST(extract_ts_template_base_url_issue2291);
1397414009
RUN_TEST(extract_ts_template_base_url_no_literal_path_issue2291);
1397514010
RUN_TEST(extract_py_concat_const_url_issue706);

‎tests/test_infrascan.c‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -809,12 +809,61 @@ TEST(infrascan_data_flows_test_handlers_pair_only_within_their_file) {
809809
PASS();
810810
}
811811

812+
/* Issue #2537: the Go 1.22 ServeMux grammar "[METHOD ][HOST]/[PATH]". The path
813+
* is found inside the written pattern; a plain path, a lowercase word, a
814+
* pattern without a path and a method too long for the buffer are no split.
815+
* Only a Handle / HandleFunc registration is read with it. */
816+
TEST(infrascan_servemux_pattern_split_issue2537) {
817+
char m[16];
818+
const char *pat = "POST /v1/payments";
819+
ASSERT_TRUE(cbm_service_pattern_mux_split(pat, m, sizeof(m)) == pat + 5);
820+
ASSERT_STR_EQ(m, "POST");
821+
pat = "GET \t /v1/payments/{id}";
822+
ASSERT_TRUE(cbm_service_pattern_mux_split(pat, m, sizeof(m)) == pat + 6);
823+
ASSERT_STR_EQ(m, "GET");
824+
pat = "GET api.example.com/v1/x";
825+
ASSERT_TRUE(cbm_service_pattern_mux_split(pat, m, sizeof(m)) == pat + 19);
826+
ASSERT_STR_EQ(m, "GET");
827+
pat = "api.example.com:8443/v1/x";
828+
ASSERT_TRUE(cbm_service_pattern_mux_split(pat, m, sizeof(m)) == pat + 20);
829+
ASSERT_STR_EQ(m, "");
830+
pat = "DELETE /";
831+
ASSERT_TRUE(cbm_service_pattern_mux_split(pat, NULL, 0) == pat + 7);
832+
833+
ASSERT_NULL(cbm_service_pattern_mux_split("/v1/x", m, sizeof(m)));
834+
ASSERT_NULL(cbm_service_pattern_mux_split("get /v1/x", m, sizeof(m)));
835+
ASSERT_NULL(cbm_service_pattern_mux_split("POST", m, sizeof(m)));
836+
ASSERT_NULL(cbm_service_pattern_mux_split("POST ", m, sizeof(m)));
837+
ASSERT_NULL(cbm_service_pattern_mux_split("GET host", m, sizeof(m)));
838+
ASSERT_NULL(cbm_service_pattern_mux_split("GET a b/x", m, sizeof(m)));
839+
ASSERT_NULL(cbm_service_pattern_mux_split("", m, sizeof(m)));
840+
ASSERT_NULL(cbm_service_pattern_mux_split(NULL, m, sizeof(m)));
841+
ASSERT_NULL(cbm_service_pattern_mux_split("ABCDEFGHIJKLMNOP /x", m, sizeof(m)));
842+
ASSERT_STR_EQ(m, "");
843+
844+
/* only Handle / HandleFunc register a pattern, and never with a URL */
845+
pat = "POST /v1/payments";
846+
ASSERT_TRUE(cbm_service_pattern_mux_route_path("mux.HandleFunc", pat) == pat + 5);
847+
pat = "api.example.com/v1/x";
848+
ASSERT_TRUE(cbm_service_pattern_mux_route_path("s.mux.Handle", pat) == pat + 15);
849+
ASSERT_NULL(cbm_service_pattern_mux_route_path("st.Get", "users/123"));
850+
ASSERT_NULL(cbm_service_pattern_mux_route_path("c.Get", "https://api.example.com/v1/x"));
851+
ASSERT_NULL(cbm_service_pattern_mux_route_path("mux.Handle", "https://api.example.com/v1/x"));
852+
ASSERT_NULL(cbm_service_pattern_mux_route_path("PatchCollector.Delete", "storage.k8s.io/v1"));
853+
ASSERT_NULL(cbm_service_pattern_mux_route_path("r.Route", "GET /v1/x"));
854+
ASSERT_NULL(cbm_service_pattern_mux_route_path("mux.HandleFunc", "/v1/x"));
855+
ASSERT_NULL(cbm_service_pattern_mux_route_path(NULL, "GET /v1/x"));
856+
ASSERT_NULL(cbm_service_pattern_mux_route_path("mux.HandleFunc", NULL));
857+
PASS();
858+
}
859+
812860
SUITE(infrascan) {
813861
RUN_TEST(infrascan_http_route_literal_guard_rejects_filesystem_paths);
814862
RUN_TEST(infrascan_http_route_literal_guard_rejects_document_extensions);
815863
RUN_TEST(infrascan_client_navigation_is_js_family_only);
816864
RUN_TEST(infrascan_service_pattern_match_rejects_ids_inside_words);
817865
RUN_TEST(infrascan_service_pattern_match_keeps_real_library_qns);
866+
RUN_TEST(infrascan_servemux_pattern_split_issue2537);
818867
RUN_TEST(infrascan_route_nodes_skip_bad_http_url_paths);
819868
RUN_TEST(infrascan_http_calls_join_matching_handler_route);
820869
RUN_TEST(infrascan_decorator_route_metadata_keeps_its_escapes);

0 commit comments

Comments
 (0)