Skip to content

Commit d15071c

Browse files
committed
fix: suppress spurious route nodes
Tighten service-pattern matching so short framework ids such as gin. do not match unrelated QNs like plugin.*, while keeping separator-based wrappers such as requests_get working. Reject bare HTTP-verb decorators like unittest.mock.patch as route decorators unless they have a receiver, and skip package manifests when promoting infra URL string refs to Route nodes. Validation: build/c/test-runner builds; CBM_ONLY_SUITE=infrascan 4/4; extraction 195/195; depindex 32/32; lang_contract 31/31; edge_types_probe 52/52; isolated autorun index now stores 0 Route nodes. Signed-off-by: Andrew Hundt <[email protected]>
1 parent cf481bd commit d15071c

6 files changed

Lines changed: 91 additions & 6 deletions

File tree

‎internal/cbm/extract_defs.c‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1247,16 +1247,27 @@ static bool try_route_from_decorator_call(CBMArena *a, TSNode dchild, const char
12471247
if (!method) {
12481248
return false;
12491249
}
1250+
const char *dot = fn_text ? strrchr(fn_text, '.') : NULL;
1251+
bool has_receiver = dot && dot[SKIP_CHAR] != '\0';
1252+
bool is_generic_route = fn_text &&
1253+
(strcmp(dot ? dot + SKIP_CHAR : fn_text, "route") == 0 ||
1254+
strcmp(dot ? dot + SKIP_CHAR : fn_text, "api_route") == 0);
12501255

12511256
TSNode args = find_decorator_args(dchild);
12521257
if (!ts_node_is_null(args)) {
12531258
const char *path = extract_route_path_from_args(a, args, source);
12541259
if (path) {
1260+
if (!has_receiver && !is_generic_route) {
1261+
return false;
1262+
}
12551263
*out_path = path;
12561264
*out_method = method;
12571265
return true;
12581266
}
12591267
}
1268+
if (!has_receiver) {
1269+
return false;
1270+
}
12601271
*out_path = "/";
12611272
*out_method = method;
12621273
return true;

‎internal/cbm/service_patterns.c‎

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ static const lib_pattern_t http_libraries[] = {
9595
{"Net::HTTP", CBM_SVC_HTTP, NULL},
9696

9797
/* PHP */
98+
{"GuzzleHttp", CBM_SVC_HTTP, NULL},
9899
{"Guzzle", CBM_SVC_HTTP, NULL},
99100
{"guzzle", CBM_SVC_HTTP, NULL},
100101
{"curl", CBM_SVC_HTTP, NULL},
@@ -534,17 +535,41 @@ static const method_suffix_t method_suffixes[] = {
534535

535536
/* ── Matching implementation ───────────────────────────────────── */
536537

537-
/* Check if any library identifier appears as a substring in the QN.
538-
* Case-sensitive: "requests" matches "project.venv.requests.api.get"
539-
* but not "Requests". Library names are specific enough to avoid
540-
* false positives even with substring matching. */
538+
static bool qn_token_char(char ch) {
539+
return (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') ||
540+
(ch >= '0' && ch <= '9');
541+
}
542+
543+
static bool qn_pattern_occurrence_matches(const char *qn, const char *hit,
544+
const char *pattern) {
545+
size_t plen = strlen(pattern);
546+
if (plen == 0) {
547+
return false;
548+
}
549+
if (qn_token_char(pattern[0]) && hit > qn && qn_token_char(hit[-1])) {
550+
return false;
551+
}
552+
if (qn_token_char(pattern[plen - 1]) && qn_token_char(hit[plen])) {
553+
return false;
554+
}
555+
return true;
556+
}
557+
558+
/* Check if a library identifier appears as a token-aligned substring in the QN.
559+
* Case-sensitive: "requests" matches "project.venv.requests.api.get" but not
560+
* "myrequests". The boundary check also prevents short framework ids like
561+
* "gin." from firing inside unrelated names such as "plugin.". */
541562
static const lib_pattern_t *match_qn(const char *qn, const lib_pattern_t *patterns) {
542563
if (!qn || !qn[0]) {
543564
return NULL;
544565
}
545566
for (int i = 0; patterns[i].library_id != NULL; i++) {
546-
if (strstr(qn, patterns[i].library_id) != NULL) {
547-
return &patterns[i];
567+
const char *p = qn;
568+
while ((p = strstr(p, patterns[i].library_id)) != NULL) {
569+
if (qn_pattern_occurrence_matches(qn, p, patterns[i].library_id)) {
570+
return &patterns[i];
571+
}
572+
p++;
548573
}
549574
}
550575
return NULL;

‎src/pipeline/pipeline.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ enum { CBM_DIR_PERMS = 0755, PL_RING = 4, PL_RING_MASK = 3, PL_SEQ_PASSES = 6 };
2424
#include "store/store.h"
2525
#include "discover/discover.h"
2626
#include "discover/userconfig.h"
27+
#include "depindex/depindex.h"
2728
#include "foundation/platform.h"
2829
#include "foundation/compat_fs.h"
2930
#include "foundation/log.h"
@@ -621,6 +622,9 @@ static void cbm_pipeline_process_infra_bindings(cbm_gbuf_t *gbuf, const cbm_file
621622
}
622623

623624
static bool is_infra_file(const char *fp) {
625+
if (cbm_is_manifest_path(fp)) {
626+
return false;
627+
}
624628
return fp != NULL &&
625629
(strstr(fp, ".yaml") != NULL || strstr(fp, ".yml") != NULL ||
626630
strstr(fp, ".tf") != NULL || strstr(fp, ".hcl") != NULL || strstr(fp, ".toml") != NULL);

‎tests/test_extraction.c‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2645,6 +2645,32 @@ TEST(extract_java_method_annotations_issue382) {
26452645
PASS();
26462646
}
26472647

2648+
TEST(extract_python_mock_patch_is_not_route) {
2649+
CBMFileResult *r = extract("from unittest.mock import patch\n\n"
2650+
"@patch(\"subprocess.run\")\n"
2651+
"def test_cmd(mock_run):\n"
2652+
" pass\n\n"
2653+
"@app.patch(\"/items/{id}\")\n"
2654+
"def update_item():\n"
2655+
" pass\n",
2656+
CBM_LANG_PYTHON, "t", "test_routes.py");
2657+
ASSERT_NOT_NULL(r);
2658+
ASSERT_FALSE(r->has_error);
2659+
2660+
const CBMDefinition *mocked = find_def_by_name(r, "test_cmd");
2661+
ASSERT_NOT_NULL(mocked);
2662+
ASSERT_NULL(mocked->route_path);
2663+
ASSERT_NULL(mocked->route_method);
2664+
2665+
const CBMDefinition *route = find_def_by_name(r, "update_item");
2666+
ASSERT_NOT_NULL(route);
2667+
ASSERT_STR_EQ(route->route_path, "/items/{id}");
2668+
ASSERT_STR_EQ(route->route_method, "PATCH");
2669+
2670+
cbm_free_result(r);
2671+
PASS();
2672+
}
2673+
26482674
/* Issue #213: large TS files were indexed as a File node with zero children. */
26492675
TEST(extract_large_ts_has_functions_issue213) {
26502676
enum { NFUNCS = 4000 };
@@ -3163,6 +3189,7 @@ SUITE(extraction) {
31633189
RUN_TEST(js_index_module_qn_not_collide_with_folder);
31643190
RUN_TEST(python_regular_module_qn_unchanged);
31653191
RUN_TEST(extract_java_method_annotations_issue382);
3192+
RUN_TEST(extract_python_mock_patch_is_not_route);
31663193
RUN_TEST(extract_large_ts_has_functions_issue213);
31673194

31683195
/* Per-function complexity metrics (Tier A) */

‎tests/test_infrascan.c‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,20 @@ TEST(infrascan_http_route_literal_guard_rejects_filesystem_paths) {
5353
PASS();
5454
}
5555

56+
TEST(infrascan_service_pattern_match_uses_qn_boundaries) {
57+
ASSERT_EQ(cbm_service_pattern_match(
58+
"proj.plugins.autorun.tests.test_plugin._dispatch"),
59+
CBM_SVC_NONE);
60+
ASSERT_EQ(cbm_service_pattern_match("proj.myrequests.client.get"), CBM_SVC_NONE);
61+
62+
ASSERT_EQ(cbm_service_pattern_match("proj.gin.router.GET"), CBM_SVC_ROUTE_REG);
63+
ASSERT_EQ(cbm_service_pattern_match("proj.express.router.get"), CBM_SVC_ROUTE_REG);
64+
ASSERT_EQ(cbm_service_pattern_match("proj.venv.requests.api.get"), CBM_SVC_HTTP);
65+
ASSERT_EQ(cbm_service_pattern_match("proj.service.requests_get"), CBM_SVC_HTTP);
66+
ASSERT_EQ(cbm_service_pattern_match("proj.GuzzleHttp.Client.get"), CBM_SVC_HTTP);
67+
PASS();
68+
}
69+
5670
TEST(infrascan_route_nodes_skip_bad_http_url_paths) {
5771
cbm_gbuf_t *gb = cbm_gbuf_new("test", "/tmp/cbm_infrascan_route_guard");
5872
ASSERT_NOT_NULL(gb);
@@ -133,6 +147,7 @@ TEST(infrascan_http_calls_join_matching_handler_route) {
133147

134148
SUITE(infrascan) {
135149
RUN_TEST(infrascan_http_route_literal_guard_rejects_filesystem_paths);
150+
RUN_TEST(infrascan_service_pattern_match_uses_qn_boundaries);
136151
RUN_TEST(infrascan_route_nodes_skip_bad_http_url_paths);
137152
RUN_TEST(infrascan_http_calls_join_matching_handler_route);
138153
}

‎tests/test_main.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,9 @@ int main(void) {
165165
if (strstr("watcher", only_suite)) RUN_SUITE(watcher);
166166
if (strstr("security", only_suite)) RUN_SUITE(security);
167167
if (strstr("artifact", only_suite)) RUN_SUITE(artifact);
168+
if (strstr("extraction", only_suite)) RUN_SUITE(extraction);
169+
if (strstr("lang_contract", only_suite)) RUN_SUITE(lang_contract);
170+
if (strstr("edge_types_probe", only_suite)) RUN_SUITE(edge_types_probe);
168171
TEST_SUMMARY();
169172
return 0;
170173
}

0 commit comments

Comments
 (0)