Skip to content

Commit 812d075

Browse files
authored
Merge pull request DeusData#2854 from divakantpandey/fix/2768-angular-http-client-method
fix(pipeline): infer HTTP method for Angular and verb-suffix client calls (DeusData#2768)
2 parents c07cb8e + 6b6213e commit 812d075

6 files changed

Lines changed: 463 additions & 6 deletions

File tree

‎internal/cbm/service_patterns.c‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,25 @@ static const lib_pattern_t http_libraries[] = {
4646
* case-sensitive, so without this entry `Axios.get(url)` fell through to
4747
* the `.get` route-suffix fallback and became a route registration. */
4848
{"Axios", CBM_SVC_HTTP, NULL},
49+
/* Angular HttpClient (#2768). The receiver rows end in a dot, like "gin.":
50+
* a bare "this.http" / "$http" would also match this.httpAdapter.get
51+
* (NestJS route registration) and $httpBackend.expectGET (an AngularJS test
52+
* double), because an uppercase letter after an id counts as CamelCase
53+
* glue. A chain wrapped before the verb keeps its line break in the callee
54+
* text ("this.http\n .post", prettier's style), so each receiver also
55+
* has a line-break twin: LF, and CR for CRLF files. Java/C# HttpClient is
56+
* already listed below. */
57+
{"@angular/common/http", CBM_SVC_HTTP, NULL},
58+
{"angular.common.http", CBM_SVC_HTTP, NULL},
59+
{"this.http.", CBM_SVC_HTTP, NULL},
60+
{"this.http\n", CBM_SVC_HTTP, NULL},
61+
{"this.http\r", CBM_SVC_HTTP, NULL},
62+
{"httpClient.", CBM_SVC_HTTP, NULL},
63+
{"httpClient\n", CBM_SVC_HTTP, NULL},
64+
{"httpClient\r", CBM_SVC_HTTP, NULL},
65+
{"$http.", CBM_SVC_HTTP, NULL},
66+
{"$http\n", CBM_SVC_HTTP, NULL},
67+
{"$http\r", CBM_SVC_HTTP, NULL},
4968
{"superagent", CBM_SVC_HTTP, NULL},
5069
{"needle", CBM_SVC_HTTP, NULL},
5170
{"node-fetch", CBM_SVC_HTTP, NULL},

‎src/pipeline/pass_cross_repo.c‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -720,6 +720,16 @@ static cr_match_result_t match_http_routes(cbm_store_t *src_store, const char *s
720720
char method[CBM_SZ_32] = {0};
721721
json_str_prop(props, "url_path", url_path, sizeof(url_path));
722722
json_str_prop(props, "method", method, sizeof(method));
723+
if (!method[0]) {
724+
char callee[CBM_SZ_256] = {0};
725+
json_str_prop(props, "callee", callee, sizeof(callee));
726+
if (callee[0]) {
727+
const char *m = cbm_service_pattern_http_method(callee);
728+
if (m) {
729+
snprintf(method, sizeof(method), "%s", m);
730+
}
731+
}
732+
}
723733
if (!url_path[0]) {
724734
continue;
725735
}

‎src/pipeline/pass_parallel.c‎

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2621,19 +2621,37 @@ static void detect_url_in_args(cbm_gbuf_t *gbuf, const cbm_gbuf_node_t *source,
26212621
if (!cbm_service_pattern_is_http_route_literal(norm, call->callee_name)) {
26222622
continue;
26232623
}
2624+
const char *method = cbm_service_pattern_http_method(call->callee_name);
2625+
const char *prefix = method ? method : "ANY";
26242626
char route_qn[CBM_ROUTE_QN_SIZE];
26252627
char cpath[CBM_SZ_256];
2626-
snprintf(route_qn, sizeof(route_qn), "__route__ANY__%s",
2628+
snprintf(route_qn, sizeof(route_qn), "__route__%s__%s", prefix,
26272629
cbm_route_canon_path(norm, cpath, sizeof(cpath)));
2628-
int64_t route_id = cbm_gbuf_upsert_node(gbuf, "Route", norm, route_qn, "", 0, 0,
2629-
"{\"source\":\"arg_url\"}");
2630+
char route_props[CBM_SZ_256];
2631+
if (method) {
2632+
snprintf(route_props, sizeof(route_props), "{\"source\":\"arg_url\",\"method\":\"%s\"}",
2633+
method);
2634+
} else {
2635+
snprintf(route_props, sizeof(route_props), "{\"source\":\"arg_url\"}");
2636+
}
2637+
int64_t route_id =
2638+
cbm_gbuf_upsert_node(gbuf, "Route", norm, route_qn, "", 0, 0, route_props);
26302639
char esc_c[CBM_SZ_256];
26312640
char esc_n[CBM_SZ_256];
26322641
cbm_json_escape(esc_c, sizeof(esc_c), call->callee_name);
26332642
cbm_json_escape(esc_n, sizeof(esc_n), norm);
2634-
char eprops[CBM_SZ_512];
2635-
snprintf(eprops, sizeof(eprops),
2636-
"{\"callee\":\"%s\",\"url_path\":\"%s\",\"via\":\"arg_url\"}", esc_c, esc_n);
2643+
/* Two escaped 255-byte strings plus the fixed keys and the verb: 1K
2644+
* never truncates the JSON (512 could, once the method was added). */
2645+
char eprops[CBM_SZ_1K];
2646+
if (method) {
2647+
snprintf(
2648+
eprops, sizeof(eprops),
2649+
"{\"callee\":\"%s\",\"url_path\":\"%s\",\"via\":\"arg_url\",\"method\":\"%s\"}",
2650+
esc_c, esc_n, method);
2651+
} else {
2652+
snprintf(eprops, sizeof(eprops),
2653+
"{\"callee\":\"%s\",\"url_path\":\"%s\",\"via\":\"arg_url\"}", esc_c, esc_n);
2654+
}
26372655
cbm_gbuf_insert_edge(gbuf, source->id, route_id, "HTTP_CALLS", eprops);
26382656
break;
26392657
}

‎tests/test_cross_repo.c‎

Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -944,10 +944,201 @@ TEST(cross_repo_caller_without_local_handler_keeps_cross_http_issue1459) {
944944
PASS();
945945
}
946946

947+
static bool cross_repo_seed_server_route(const cross_repo_fixture_t *fixture,
948+
const char *target_project, const char *method,
949+
const char *route_path, const char *handler_name) {
950+
char target_path[512];
951+
if (!cross_repo_project_path(fixture, target_project, target_path, sizeof(target_path))) {
952+
return false;
953+
}
954+
cbm_store_t *target = cbm_store_open_path(target_path);
955+
if (!target) {
956+
return false;
957+
}
958+
bool ok = cbm_store_upsert_project(target, target_project, fixture->cache) == CBM_STORE_OK;
959+
char target_route_qn[256];
960+
char handler_qn[256];
961+
char route_name[128];
962+
snprintf(target_route_qn, sizeof(target_route_qn), "__route__%s__%s", method, route_path);
963+
snprintf(handler_qn, sizeof(handler_qn), "%s.%s", target_project, handler_name);
964+
snprintf(route_name, sizeof(route_name), "%s %s", method, route_path);
965+
966+
cbm_node_t target_route = {.project = target_project,
967+
.label = "Route",
968+
.name = route_name,
969+
.qualified_name = target_route_qn,
970+
.file_path = "server.java"};
971+
cbm_node_t handler = {.project = target_project,
972+
.label = "Function",
973+
.name = handler_name,
974+
.qualified_name = handler_qn,
975+
.file_path = "server.java"};
976+
int64_t target_route_id = ok ? cbm_store_upsert_node(target, &target_route) : 0;
977+
int64_t handler_id = ok ? cbm_store_upsert_node(target, &handler) : 0;
978+
cbm_edge_t handles = {.project = target_project,
979+
.source_id = handler_id,
980+
.target_id = target_route_id,
981+
.type = "HANDLES"};
982+
ok = ok && target_route_id > 0 && handler_id > 0 && cbm_store_insert_edge(target, &handles) > 0;
983+
cbm_store_close(target);
984+
return ok;
985+
}
986+
987+
static bool cross_repo_seed_client_call(const cross_repo_fixture_t *fixture,
988+
const char *source_project, const char *caller_name,
989+
const char *callee_name, const char *url_path,
990+
const char *method) {
991+
char source_path[512];
992+
if (!cross_repo_project_path(fixture, source_project, source_path, sizeof(source_path))) {
993+
return false;
994+
}
995+
cbm_store_t *source = cbm_store_open_path(source_path);
996+
if (!source) {
997+
return false;
998+
}
999+
bool ok = cbm_store_upsert_project(source, source_project, fixture->cache) == CBM_STORE_OK;
1000+
char caller_qn[256];
1001+
char local_route_qn[256];
1002+
char edge_props[512];
1003+
snprintf(caller_qn, sizeof(caller_qn), "%s.%s", source_project, caller_name);
1004+
snprintf(local_route_qn, sizeof(local_route_qn), "__route__%s__%s", method ? method : "ANY",
1005+
url_path);
1006+
if (method) {
1007+
snprintf(edge_props, sizeof(edge_props),
1008+
"{\"callee\":\"%s\",\"url_path\":\"%s\",\"method\":\"%s\"}", callee_name,
1009+
url_path, method);
1010+
} else {
1011+
snprintf(edge_props, sizeof(edge_props),
1012+
"{\"callee\":\"%s\",\"url_path\":\"%s\"}", callee_name, url_path);
1013+
}
1014+
cbm_node_t caller = {.project = source_project,
1015+
.label = "Function",
1016+
.name = caller_name,
1017+
.qualified_name = caller_qn,
1018+
.file_path = "item.service.ts"};
1019+
cbm_node_t local_route = {.project = source_project,
1020+
.label = "Route",
1021+
.name = url_path,
1022+
.qualified_name = local_route_qn,
1023+
.file_path = "item.service.ts"};
1024+
int64_t caller_id = ok ? cbm_store_upsert_node(source, &caller) : 0;
1025+
int64_t local_route_id = ok ? cbm_store_upsert_node(source, &local_route) : 0;
1026+
cbm_edge_t http_call = {.project = source_project,
1027+
.source_id = caller_id,
1028+
.target_id = local_route_id,
1029+
.type = "HTTP_CALLS",
1030+
.properties_json = edge_props};
1031+
ok = ok && caller_id > 0 && local_route_id > 0 && cbm_store_insert_edge(source, &http_call) > 0;
1032+
cbm_store_close(source);
1033+
return ok;
1034+
}
1035+
1036+
static int cross_repo_count_edges_like(const cross_repo_fixture_t *fixture, const char *project,
1037+
const char *edge_type, const char *pattern) {
1038+
char path[512];
1039+
if (!cross_repo_project_path(fixture, project, path, sizeof(path))) {
1040+
return -1;
1041+
}
1042+
cbm_store_t *store = cbm_store_open_path_query(path);
1043+
if (!store) {
1044+
return -1;
1045+
}
1046+
sqlite3_stmt *s = NULL;
1047+
int count = -1;
1048+
if (sqlite3_prepare_v2(
1049+
cbm_store_get_db(store),
1050+
"SELECT COUNT(*) FROM edges WHERE project = ?1 AND type = ?2 AND properties LIKE ?3",
1051+
-1, &s, NULL) == SQLITE_OK) {
1052+
sqlite3_bind_text(s, 1, project, -1, SQLITE_STATIC);
1053+
sqlite3_bind_text(s, 2, edge_type, -1, SQLITE_STATIC);
1054+
sqlite3_bind_text(s, 3, pattern, -1, SQLITE_STATIC);
1055+
if (sqlite3_step(s) == SQLITE_ROW) {
1056+
count = sqlite3_column_int(s, 0);
1057+
}
1058+
sqlite3_finalize(s);
1059+
}
1060+
cbm_store_close(store);
1061+
return count;
1062+
}
1063+
1064+
/* #2768: Angular HttpClient calls must match cross-repo routes with exact HTTP verbs.
1065+
* When client makes GET requests to /items/list, /items/42, and /items/job/10,
1066+
* and the server defines overlapping routes:
1067+
* DELETE /items/{id} (seeded first in DB)
1068+
* GET /items/{id} (seeded second in DB)
1069+
* POST /items/{parentId}/{id} (seeded first in DB)
1070+
* GET /items/job/{jobId} (seeded second in DB)
1071+
* the fuzzy route matcher must enforce method compatibility, linking the GET calls
1072+
* to their respective GET endpoints and never to DELETE or POST endpoints.
1073+
* getItem and getJob are seeded WITHOUT a method property (an edge written by an
1074+
* index that did not record the verb): the matcher must infer GET from the
1075+
* callee's verb suffix, or the method-less fuzzy match lands on the DELETE / POST
1076+
* route seeded first. getItems keeps its explicit "GET". */
1077+
TEST(cross_repo_angular_http_client_exact_verb_matching_issue2768) {
1078+
cross_repo_fixture_t fixture;
1079+
bool setup = cross_repo_fixture_begin(&fixture) &&
1080+
/* Target server with overlapping endpoints */
1081+
cross_repo_seed_server_route(&fixture, "api-2768", "GET", "/items/list",
1082+
"handle_list_items") &&
1083+
cross_repo_seed_server_route(&fixture, "api-2768", "DELETE", "/items/{}",
1084+
"handle_delete_item") &&
1085+
cross_repo_seed_server_route(&fixture, "api-2768", "GET", "/items/{}",
1086+
"handle_get_item") &&
1087+
cross_repo_seed_server_route(&fixture, "api-2768", "POST", "/items/{}/{}",
1088+
"handle_post_nested_item") &&
1089+
cross_repo_seed_server_route(&fixture, "api-2768", "GET", "/items/job/{}",
1090+
"handle_get_job") &&
1091+
/* Angular client making GET requests */
1092+
cross_repo_seed_client_call(&fixture, "angular-2768", "getItems",
1093+
"this.http.get", "/items/list", "GET") &&
1094+
cross_repo_seed_client_call(&fixture, "angular-2768", "getItem",
1095+
"this.http.get", "/items/42", NULL) &&
1096+
cross_repo_seed_client_call(&fixture, "angular-2768", "getJob",
1097+
"this.http.get", "/items/job/10", NULL);
1098+
if (!setup) {
1099+
cross_repo_fixture_end(&fixture);
1100+
FAIL("failed to seed issue 2768 fixture");
1101+
}
1102+
1103+
const char *target = "api-2768";
1104+
cbm_cross_repo_result_t fwd = cbm_cross_repo_match("angular-2768", &target, 1);
1105+
ASSERT_FALSE(fwd.failed);
1106+
ASSERT_EQ(fwd.http_edges, 3);
1107+
1108+
/* Assert that client CROSS_HTTP_CALLS edges are linked to GET handlers, not DELETE or POST */
1109+
ASSERT_EQ(cross_repo_count_edges_like(&fixture, "angular-2768", "CROSS_HTTP_CALLS",
1110+
"%handle_delete_item%"),
1111+
0);
1112+
ASSERT_EQ(cross_repo_count_edges_like(&fixture, "angular-2768", "CROSS_HTTP_CALLS",
1113+
"%handle_post_nested_item%"),
1114+
0);
1115+
ASSERT_EQ(cross_repo_count_edges_like(&fixture, "angular-2768", "CROSS_HTTP_CALLS",
1116+
"%handle_list_items%"),
1117+
1);
1118+
ASSERT_EQ(cross_repo_count_edges_like(&fixture, "angular-2768", "CROSS_HTTP_CALLS",
1119+
"%handle_get_item%"),
1120+
1);
1121+
ASSERT_EQ(cross_repo_count_edges_like(&fixture, "angular-2768", "CROSS_HTTP_CALLS",
1122+
"%handle_get_job%"),
1123+
1);
1124+
1125+
/* Reverse pass */
1126+
const char *consumer = "angular-2768";
1127+
cbm_cross_repo_result_t rev = cbm_cross_repo_match("api-2768", &consumer, 1);
1128+
ASSERT_FALSE(rev.failed);
1129+
ASSERT_EQ(rev.http_edges, 3);
1130+
1131+
cross_repo_fixture_end(&fixture);
1132+
cbm_cross_repo_result_free(&fwd);
1133+
cbm_cross_repo_result_free(&rev);
1134+
PASS();
1135+
}
1136+
9471137
SUITE(cross_repo) {
9481138
RUN_TEST(cross_repo_caller_local_handler_blocks_cross_http_issue1459);
9491139
RUN_TEST(cross_repo_methodless_call_to_local_get_route_blocks_cross_http_issue1459);
9501140
RUN_TEST(cross_repo_caller_without_local_handler_keeps_cross_http_issue1459);
1141+
RUN_TEST(cross_repo_angular_http_client_exact_verb_matching_issue2768);
9511142
RUN_TEST(cross_repo_accepts_project_with_missed_shadow_row_issue1609);
9521143
RUN_TEST(cross_repo_null_target_fails_without_dereference);
9531144
RUN_TEST(cross_repo_wildcard_keeps_projects_containing_internal_tokens);

‎tests/test_infrascan.c‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,13 @@ TEST(infrascan_service_pattern_match_rejects_ids_inside_words) {
173173
{"kafkaesque.story.tell", CBM_SVC_NONE},
174174
/* "Axios" glued to a lowercase continuation */
175175
{"geo.Axiosphere.map", CBM_SVC_NONE},
176+
/* Angular receiver rows end in a dot (#2768): a longer receiver that
177+
* only starts with the same letters is not the HttpClient (NestJS
178+
* route registration, AngularJS test double) */
179+
{"this.httpAdapter.get", CBM_SVC_NONE},
180+
{"this.httpServer.post", CBM_SVC_NONE},
181+
{"$httpBackend.expectGET", CBM_SVC_NONE},
182+
{"this.httpAdapter\n .get", CBM_SVC_NONE},
176183
{NULL, CBM_SVC_NONE},
177184
};
178185
ASSERT_EQ(svc_case_mismatches(cases), 0);
@@ -202,6 +209,18 @@ TEST(infrascan_service_pattern_match_keeps_real_library_qns) {
202209
{"Axios.post", CBM_SVC_HTTP},
203210
{"AxiosInstance.get", CBM_SVC_HTTP},
204211
{"curl_exec", CBM_SVC_HTTP},
212+
/* Angular HttpClient receivers (#2768); `_httpClient` is the common
213+
* C# field spelling of the same client */
214+
{"this.http.get", CBM_SVC_HTTP},
215+
{"this.httpClient.post", CBM_SVC_HTTP},
216+
{"httpClient.put", CBM_SVC_HTTP},
217+
{"_httpClient.GetAsync", CBM_SVC_HTTP},
218+
{"$http.get", CBM_SVC_HTTP},
219+
/* a chain wrapped before the verb keeps the line break (LF / CRLF) */
220+
{"this.http\n .post", CBM_SVC_HTTP},
221+
{"this.http\r\n .delete", CBM_SVC_HTTP},
222+
{"this.httpClient\n .put", CBM_SVC_HTTP},
223+
{"$http\n .get", CBM_SVC_HTTP},
205224
{"surf.get", CBM_SVC_HTTP},
206225
{"hyper.Client.request", CBM_SVC_HTTP},
207226
{"phin.promisified", CBM_SVC_HTTP},

0 commit comments

Comments
 (0)