Skip to content

Commit a3ae751

Browse files
authored
Merge pull request DeusData#1400 from AmirF194/fix/1249-binary-concat-route-url
fix(extract): resolve Go route paths built via string concatenation
2 parents fda23bd + 478ce25 commit a3ae751

2 files changed

Lines changed: 77 additions & 0 deletions

File tree

‎internal/cbm/extract_calls.c‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2005,6 +2005,24 @@ static const char *extract_keyword_url(CBMExtractCtx *ctx, TSNode arg) {
20052005
return extract_string_value(ctx, val_node);
20062006
}
20072007

2008+
// `prefixVar + "/route"` (Go's idiomatic configurable-base-path pattern):
2009+
// recover the literal suffix. A right side that is not itself a literal is
2010+
// left unresolved rather than guessed (issue #1249).
2011+
static const char *extract_binary_concat_suffix(CBMExtractCtx *ctx, TSNode node) {
2012+
TSNode op_node = ts_node_child_by_field_name(node, TS_FIELD("operator"));
2013+
if (!ts_node_is_null(op_node)) {
2014+
char *op = cbm_node_text(ctx->arena, op_node, ctx->source);
2015+
if (!op || strcmp(op, "+") != 0) {
2016+
return NULL;
2017+
}
2018+
}
2019+
TSNode rhs = ts_node_child_by_field_name(node, TS_FIELD("right"));
2020+
if (ts_node_is_null(rhs) || !is_string_like(ts_node_type(rhs))) {
2021+
return NULL;
2022+
}
2023+
return strip_and_validate_string_arg(ctx->arena, cbm_node_text(ctx->arena, rhs, ctx->source));
2024+
}
2025+
20082026
// Try to extract URL/topic from a positional argument (string or constant).
20092027
static const char *extract_positional_url(CBMExtractCtx *ctx, TSNode arg, const char *ak) {
20102028
/* JS/TS template literals: `/things/${id}` normalizes to "/things/{}" so the
@@ -2015,6 +2033,12 @@ static const char *extract_positional_url(CBMExtractCtx *ctx, TSNode arg, const
20152033
return strip_and_validate_string_arg(ctx->arena, (char *)flat);
20162034
}
20172035
}
2036+
if (strcmp(ak, "binary_expression") == 0) {
2037+
const char *suffix = extract_binary_concat_suffix(ctx, arg);
2038+
if (suffix) {
2039+
return suffix;
2040+
}
2041+
}
20182042
if (is_string_like(ak)) {
20192043
char *text = cbm_node_text(ctx->arena, arg, ctx->source);
20202044
const char *validated = strip_and_validate_string_arg(ctx->arena, text);

‎tests/test_extraction.c‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3230,6 +3230,57 @@ TEST(extract_ts_template_string_url_issue1006) {
32303230
PASS();
32313231
}
32323232

3233+
/* Issue #1249: a mux route built as `configVar + "/literal"` (Go's idiomatic
3234+
* configurable-base-path pattern) must index the literal suffix, both for a
3235+
* route registration and for an outbound URL built the same way. A real BFF
3236+
* with 47 such registrations produced only 9 Route nodes before this fix. */
3237+
TEST(extract_go_binary_concat_url_issue1249) {
3238+
CBMFileResult *r = extract("package main\n"
3239+
"import \"net/http\"\n"
3240+
"func setup(mux *http.ServeMux, base string) {\n"
3241+
" mux.HandleFunc(base+\"/login\", loginHandler)\n"
3242+
"}\n"
3243+
"func report(host string, port string) {\n"
3244+
" http.Get(\"http://\" + host + \":\" + port + \"/log\")\n"
3245+
"}\n",
3246+
CBM_LANG_GO, "t", "routes.go");
3247+
ASSERT_NOT_NULL(r);
3248+
ASSERT_FALSE(r->has_error);
3249+
3250+
const CBMCall *reg = find_call_by_callee(r, "mux.HandleFunc");
3251+
ASSERT_NOT_NULL(reg);
3252+
ASSERT_NOT_NULL(reg->first_string_arg);
3253+
ASSERT_STR_EQ(reg->first_string_arg, "/login");
3254+
3255+
const CBMCall *out = find_call_by_callee(r, "http.Get");
3256+
ASSERT_NOT_NULL(out);
3257+
ASSERT_NOT_NULL(out->first_string_arg);
3258+
ASSERT_STR_EQ(out->first_string_arg, "/log");
3259+
3260+
cbm_free_result(r);
3261+
PASS();
3262+
}
3263+
3264+
/* Same issue: when the right side of the concatenation is not itself a
3265+
* literal (`base + suffixVar`), there is no literal route to recover. The
3266+
* fix must leave this unresolved rather than fabricate a path. */
3267+
TEST(extract_go_binary_concat_url_no_literal_suffix_issue1249) {
3268+
CBMFileResult *r = extract("package main\n"
3269+
"func setup(mux *http.ServeMux, base string, suffix string) {\n"
3270+
" mux.HandleFunc(base+suffix, dynHandler)\n"
3271+
"}\n",
3272+
CBM_LANG_GO, "t", "routes.go");
3273+
ASSERT_NOT_NULL(r);
3274+
ASSERT_FALSE(r->has_error);
3275+
3276+
const CBMCall *reg = find_call_by_callee(r, "mux.HandleFunc");
3277+
ASSERT_NOT_NULL(reg);
3278+
ASSERT_NULL(reg->first_string_arg);
3279+
3280+
cbm_free_result(r);
3281+
PASS();
3282+
}
3283+
32333284

32343285
/* Reproduce-first: Java module QN must derive from the CONTAINING DIRECTORY, not
32353286
* the filename stem, so a top-level class `Outer` in `Outer.java` is `t.Outer`,
@@ -5450,6 +5501,8 @@ SUITE(extraction) {
54505501
RUN_TEST(extract_java_method_annotations_issue382);
54515502
RUN_TEST(extract_java_jaxrs_path_composition_issue1005);
54525503
RUN_TEST(extract_ts_template_string_url_issue1006);
5504+
RUN_TEST(extract_go_binary_concat_url_issue1249);
5505+
RUN_TEST(extract_go_binary_concat_url_no_literal_suffix_issue1249);
54535506
RUN_TEST(extract_java_no_double_class_qn);
54545507
RUN_TEST(extract_go_no_filename_in_module_qn);
54555508
RUN_TEST(extract_large_ts_has_functions_issue213);

0 commit comments

Comments
 (0)