Skip to content

Commit 971afc0

Browse files
authored
Merge pull request DeusData#2463 from DeusData/fix/http-axios-capitalized-import
fix(pipeline): classify capitalized `Axios` calls as HTTP clients
2 parents 59a44d8 + d3059ef commit 971afc0

3 files changed

Lines changed: 128 additions & 0 deletions

File tree

‎internal/cbm/service_patterns.c‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ static const lib_pattern_t http_libraries[] = {
4141

4242
/* JavaScript / TypeScript */
4343
{"axios", CBM_SVC_HTTP, NULL},
44+
/* `import Axios from "axios"`: the capitalized default-import binding
45+
* (also axios's own Axios / AxiosInstance types). Matching is
46+
* case-sensitive, so without this entry `Axios.get(url)` fell through to
47+
* the `.get` route-suffix fallback and became a route registration. */
48+
{"Axios", CBM_SVC_HTTP, NULL},
4449
{"superagent", CBM_SVC_HTTP, NULL},
4550
{"needle", CBM_SVC_HTTP, NULL},
4651
{"node-fetch", CBM_SVC_HTTP, NULL},

‎tests/test_infrascan.c‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ TEST(infrascan_service_pattern_match_rejects_ids_inside_words) {
113113
{"gqlalchemy.Memgraph.execute", CBM_SVC_NONE},
114114
{"celeryconfig.broker_url", CBM_SVC_NONE},
115115
{"kafkaesque.story.tell", CBM_SVC_NONE},
116+
/* "Axios" glued to a lowercase continuation */
117+
{"geo.Axiosphere.map", CBM_SVC_NONE},
116118
{NULL, CBM_SVC_NONE},
117119
};
118120
ASSERT_EQ(svc_case_mismatches(cases), 0);
@@ -136,6 +138,11 @@ TEST(infrascan_service_pattern_match_keeps_real_library_qns) {
136138
{"package:dio/dio.dart", CBM_SVC_HTTP},
137139
{"dio.Dio.get", CBM_SVC_HTTP},
138140
{"pkg.net.curl.get", CBM_SVC_HTTP},
141+
/* axios under a capitalized default-import binding
142+
* (`import Axios from "axios"`) and axios's own instance type */
143+
{"Axios.get", CBM_SVC_HTTP},
144+
{"Axios.post", CBM_SVC_HTTP},
145+
{"AxiosInstance.get", CBM_SVC_HTTP},
139146
{"curl_exec", CBM_SVC_HTTP},
140147
{"surf.get", CBM_SVC_HTTP},
141148
{"hyper.Client.request", CBM_SVC_HTTP},

‎tests/test_pipeline.c‎

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7016,6 +7016,120 @@ TEST(pipeline_tsjs_receiver_parallel_keeps_service_edges) {
70167016
PASS();
70177017
}
70187018

7019+
/* Route-registration CALLS edges (via=route_registration) out of the function
7020+
* `source_name`. */
7021+
static int route_registrations_from(cbm_store_t *s, const char *project, const char *source_name) {
7022+
cbm_edge_t *edges = NULL;
7023+
int edge_count = 0;
7024+
if (cbm_store_find_edges_by_type(s, project, "CALLS", &edges, &edge_count) != CBM_STORE_OK) {
7025+
return -1;
7026+
}
7027+
int matches = 0;
7028+
for (int i = 0; i < edge_count; i++) {
7029+
cbm_node_t source = {0};
7030+
int source_ok = cbm_store_find_node_by_id(s, edges[i].source_id, &source) == CBM_STORE_OK;
7031+
if (source_ok && source.name && strcmp(source.name, source_name) == 0 &&
7032+
edges[i].properties_json &&
7033+
strstr(edges[i].properties_json, "\"via\":\"route_registration\"")) {
7034+
matches++;
7035+
}
7036+
cbm_node_free_fields(&source);
7037+
}
7038+
if (edges) {
7039+
cbm_store_free_edges(edges, edge_count);
7040+
}
7041+
return matches;
7042+
}
7043+
7044+
/* `import Axios from "axios"` -- the default-import binding capitalized, a
7045+
* common convention -- must classify `Axios.get/post/delete(url)` as HTTP
7046+
* client calls exactly like `axios.*`. The library table matched only the
7047+
* lowercase id, so the calls fell through to the `.get`/`.post` route-suffix
7048+
* fallback and became route REGISTRATIONS: on
7049+
* microsoft/Game-Control-Puzzle-Event-Administration-Tools all 69 client
7050+
* calls masqueraded as server Routes. A real Express registration in the same
7051+
* project stays a registration. `fillers` > 0 forces the parallel resolver,
7052+
* 0 keeps the sequential one; both classify through the same table. */
7053+
static int capitalized_axios_case(int fillers) {
7054+
char tmp[256];
7055+
snprintf(tmp, sizeof(tmp), "/tmp/cbm_axios_cap_XXXXXX");
7056+
if (!cbm_mkdtemp(tmp)) {
7057+
FAIL("tmpdir");
7058+
}
7059+
7060+
write_temp_file(tmp, "src/service.ts",
7061+
"import Axios from \"axios\";\n"
7062+
"export function getSettings(eventId: string) {\n"
7063+
" return Axios.get(`/api/admin/events/${eventId}/settings`);\n"
7064+
"}\n"
7065+
"export function addUser(eventId: string, participant: unknown) {\n"
7066+
" return Axios.post(`/api/admin/users/${eventId}`, participant);\n"
7067+
"}\n"
7068+
"export function removeUser(id: string) {\n"
7069+
" return Axios.delete(`/api/admin/users/${id}`);\n"
7070+
"}\n");
7071+
write_temp_file(tmp, "src/server.ts",
7072+
"function listUsers(req: unknown, res: unknown) {}\n"
7073+
"export function mount(router: any) {\n"
7074+
" router.get('/api/admin/users', listUsers);\n"
7075+
"}\n");
7076+
for (int i = 0; i < fillers; i++) {
7077+
char name[64];
7078+
char body[128];
7079+
snprintf(name, sizeof(name), "src/filler%d.ts", i);
7080+
snprintf(body, sizeof(body), "export function filler%d(): number {\n return %d;\n}\n", i,
7081+
i);
7082+
write_temp_file(tmp, name, body);
7083+
}
7084+
7085+
char *old_workers = getenv("CBM_WORKERS");
7086+
char *saved = old_workers ? strdup(old_workers) : NULL;
7087+
cbm_setenv("CBM_WORKERS", "4", 1);
7088+
7089+
char db_path[512];
7090+
snprintf(db_path, sizeof(db_path), "%s/axios_cap.db", tmp);
7091+
cbm_pipeline_t *p = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);
7092+
ASSERT_NOT_NULL(p);
7093+
ASSERT_EQ(cbm_pipeline_run(p), 0);
7094+
const char *project = cbm_pipeline_project_name(p);
7095+
7096+
cbm_store_t *s = cbm_store_open_path(db_path);
7097+
ASSERT_NOT_NULL(s);
7098+
7099+
static const struct {
7100+
const char *caller;
7101+
const char *typed_route;
7102+
} client[] = {{"getSettings", "__route__GET__"},
7103+
{"addUser", "__route__POST__"},
7104+
{"removeUser", "__route__DELETE__"}};
7105+
for (size_t i = 0; i < sizeof(client) / sizeof(client[0]); i++) {
7106+
ASSERT_EQ(route_registrations_from(s, project, client[i].caller), 0);
7107+
ASSERT_EQ(http_calls_to_route(s, project, client[i].caller, client[i].typed_route), 1);
7108+
}
7109+
/* Control: the server-side registration is untouched. */
7110+
ASSERT_EQ(route_registrations_from(s, project, "mount"), 1);
7111+
ASSERT_EQ(http_calls_to_route(s, project, "mount", "__route__"), 0);
7112+
7113+
cbm_store_close(s);
7114+
cbm_pipeline_free(p);
7115+
if (saved) {
7116+
cbm_setenv("CBM_WORKERS", saved, 1);
7117+
free(saved);
7118+
} else {
7119+
cbm_unsetenv("CBM_WORKERS");
7120+
}
7121+
th_rmtree(tmp);
7122+
PASS();
7123+
}
7124+
7125+
TEST(pipeline_capitalized_axios_import_is_http_client) {
7126+
return capitalized_axios_case(52);
7127+
}
7128+
7129+
TEST(pipeline_capitalized_axios_import_is_http_client_sequential) {
7130+
return capitalized_axios_case(0);
7131+
}
7132+
70197133
/* Python bare-call local-binding suppression, sequential path. The bare-call
70207134
* counterpart of the receiver guard above: `run` is a PARAMETER, so `run()`
70217135
* cannot be the module-level `run` and must not bind SatoriLive.run.
@@ -16482,6 +16596,8 @@ SUITE(pipeline) {
1648216596
RUN_TEST(pipeline_go_bare_ref_never_binds_field);
1648316597
RUN_TEST(pipeline_go_bare_ref_never_binds_field_parallel);
1648416598
RUN_TEST(pipeline_tsjs_receiver_parallel_keeps_service_edges);
16599+
RUN_TEST(pipeline_capitalized_axios_import_is_http_client);
16600+
RUN_TEST(pipeline_capitalized_axios_import_is_http_client_sequential);
1648516601
RUN_TEST(pipeline_python_receiver_parallel_suppresses_weak_method_edges);
1648616602
RUN_TEST(pipeline_python_bare_local_binding_suppresses_weak_edge);
1648716603
RUN_TEST(pipeline_python_bare_local_binding_parallel_suppresses_weak_edge);

0 commit comments

Comments
 (0)