Skip to content

Commit c548d8e

Browse files
fix(pipeline): infer HTTP method for Angular and verb-suffix client calls (DeusData#2768)
Signed-off-by: Divakant Pandey <divakant01@gmail.com>
1 parent 9f0fdcb commit c548d8e

7 files changed

Lines changed: 332 additions & 10 deletions

File tree

‎internal/cbm/service_patterns.c‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@ 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/common/http", CBM_SVC_HTTP, NULL},
50+
{"angular.common.http", CBM_SVC_HTTP, NULL},
51+
{"HttpClient", CBM_SVC_HTTP, NULL},
52+
{"httpClient", CBM_SVC_HTTP, NULL},
53+
{"this.http", CBM_SVC_HTTP, NULL},
54+
{"this.httpClient", CBM_SVC_HTTP, NULL},
55+
{"$http", CBM_SVC_HTTP, NULL},
4956
{"superagent", CBM_SVC_HTTP, NULL},
5057
{"needle", CBM_SVC_HTTP, NULL},
5158
{"node-fetch", CBM_SVC_HTTP, NULL},

‎src/pipeline/pass_calls.c‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -939,15 +939,20 @@ static int calls_emit_unresolved_service(cbm_pipeline_ctx_t *ctx, const CBMCall
939939
const char **imp_keys, const char **imp_vals,
940940
int imp_count, const char *route_mount, CBMLanguage lang,
941941
CBMCallEvidence *evidence) {
942-
if (calls_emit_route_call(ctx, call, source_node, module_qn, imp_keys, imp_vals, imp_count,
943-
route_mount, lang, evidence)) {
944-
return SKIP_ONE;
945-
}
946942
cbm_svc_kind_t svc = cbm_service_pattern_match(call->callee_name);
947943
if (svc == CBM_SVC_NONE && cbm_service_pattern_is_global_fetch(call->callee_name)) {
948944
svc = CBM_SVC_HTTP;
949945
}
950-
return calls_emit_service_call(ctx, call, source_node, svc, NULL, evidence);
946+
if (svc == CBM_SVC_HTTP || svc == CBM_SVC_ASYNC) {
947+
if (calls_emit_service_call(ctx, call, source_node, svc, NULL, evidence)) {
948+
return SKIP_ONE;
949+
}
950+
}
951+
if (calls_emit_route_call(ctx, call, source_node, module_qn, imp_keys, imp_vals, imp_count,
952+
route_mount, lang, evidence)) {
953+
return SKIP_ONE;
954+
}
955+
return 0;
951956
}
952957

953958
/* Apply the field-call policy after a registry target has been found. */

‎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: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2591,19 +2591,34 @@ static void detect_url_in_args(cbm_gbuf_t *gbuf, const cbm_gbuf_node_t *source,
25912591
if (!cbm_service_pattern_is_http_route_literal(norm, call->callee_name)) {
25922592
continue;
25932593
}
2594+
const char *method = cbm_service_pattern_http_method(call->callee_name);
2595+
const char *prefix = method ? method : "ANY";
25942596
char route_qn[CBM_ROUTE_QN_SIZE];
25952597
char cpath[CBM_SZ_256];
2596-
snprintf(route_qn, sizeof(route_qn), "__route__ANY__%s",
2598+
snprintf(route_qn, sizeof(route_qn), "__route__%s__%s", prefix,
25972599
cbm_route_canon_path(norm, cpath, sizeof(cpath)));
2598-
int64_t route_id = cbm_gbuf_upsert_node(gbuf, "Route", norm, route_qn, "", 0, 0,
2599-
"{\"source\":\"arg_url\"}");
2600+
char route_props[CBM_SZ_256];
2601+
if (method) {
2602+
snprintf(route_props, sizeof(route_props), "{\"source\":\"arg_url\",\"method\":\"%s\"}",
2603+
method);
2604+
} else {
2605+
snprintf(route_props, sizeof(route_props), "{\"source\":\"arg_url\"}");
2606+
}
2607+
int64_t route_id =
2608+
cbm_gbuf_upsert_node(gbuf, "Route", norm, route_qn, "", 0, 0, route_props);
26002609
char esc_c[CBM_SZ_256];
26012610
char esc_n[CBM_SZ_256];
26022611
cbm_json_escape(esc_c, sizeof(esc_c), call->callee_name);
26032612
cbm_json_escape(esc_n, sizeof(esc_n), norm);
26042613
char eprops[CBM_SZ_512];
2605-
snprintf(eprops, sizeof(eprops),
2606-
"{\"callee\":\"%s\",\"url_path\":\"%s\",\"via\":\"arg_url\"}", esc_c, esc_n);
2614+
if (method) {
2615+
snprintf(eprops, sizeof(eprops),
2616+
"{\"callee\":\"%s\",\"url_path\":\"%s\",\"via\":\"arg_url\",\"method\":\"%s\"}",
2617+
esc_c, esc_n, method);
2618+
} else {
2619+
snprintf(eprops, sizeof(eprops),
2620+
"{\"callee\":\"%s\",\"url_path\":\"%s\",\"via\":\"arg_url\"}", esc_c, esc_n);
2621+
}
26072622
cbm_gbuf_insert_edge(gbuf, source->id, route_id, "HTTP_CALLS", eprops);
26082623
break;
26092624
}

‎src/pipeline/pass_route_nodes.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ static void route_edge_visitor(const cbm_gbuf_edge_t *edge, void *userdata) {
113113
json_extract(edge->properties_json, "method", method_buf, sizeof(method_buf));
114114
const char *broker =
115115
json_extract(edge->properties_json, "broker", broker_buf, sizeof(broker_buf));
116+
if (!method && strcmp(edge->type, "HTTP_CALLS") == 0 && callee) {
117+
method = cbm_service_pattern_http_method(callee);
118+
}
116119

117120
/* Build Route QN */
118121
char route_qn[CBM_ROUTE_QN_SIZE];

‎tests/test_cross_repo.c‎

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -944,10 +944,197 @@ 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+
TEST(cross_repo_angular_http_client_exact_verb_matching_issue2768) {
1074+
cross_repo_fixture_t fixture;
1075+
bool setup = cross_repo_fixture_begin(&fixture) &&
1076+
/* Target server with overlapping endpoints */
1077+
cross_repo_seed_server_route(&fixture, "api-2768", "GET", "/items/list",
1078+
"handle_list_items") &&
1079+
cross_repo_seed_server_route(&fixture, "api-2768", "DELETE", "/items/{}",
1080+
"handle_delete_item") &&
1081+
cross_repo_seed_server_route(&fixture, "api-2768", "GET", "/items/{}",
1082+
"handle_get_item") &&
1083+
cross_repo_seed_server_route(&fixture, "api-2768", "POST", "/items/{}/{}",
1084+
"handle_post_nested_item") &&
1085+
cross_repo_seed_server_route(&fixture, "api-2768", "GET", "/items/job/{}",
1086+
"handle_get_job") &&
1087+
/* Angular client making GET requests */
1088+
cross_repo_seed_client_call(&fixture, "angular-2768", "getItems",
1089+
"this.http.get", "/items/list", "GET") &&
1090+
cross_repo_seed_client_call(&fixture, "angular-2768", "getItem",
1091+
"this.http.get", "/items/42", "GET") &&
1092+
cross_repo_seed_client_call(&fixture, "angular-2768", "getJob",
1093+
"this.http.get", "/items/job/10", "GET");
1094+
if (!setup) {
1095+
cross_repo_fixture_end(&fixture);
1096+
FAIL("failed to seed issue 2768 fixture");
1097+
}
1098+
1099+
const char *target = "api-2768";
1100+
cbm_cross_repo_result_t fwd = cbm_cross_repo_match("angular-2768", &target, 1);
1101+
ASSERT_FALSE(fwd.failed);
1102+
ASSERT_EQ(fwd.http_edges, 3);
1103+
1104+
/* Assert that client CROSS_HTTP_CALLS edges are linked to GET handlers, not DELETE or POST */
1105+
ASSERT_EQ(cross_repo_count_edges_like(&fixture, "angular-2768", "CROSS_HTTP_CALLS",
1106+
"%handle_delete_item%"),
1107+
0);
1108+
ASSERT_EQ(cross_repo_count_edges_like(&fixture, "angular-2768", "CROSS_HTTP_CALLS",
1109+
"%handle_post_nested_item%"),
1110+
0);
1111+
ASSERT_EQ(cross_repo_count_edges_like(&fixture, "angular-2768", "CROSS_HTTP_CALLS",
1112+
"%handle_list_items%"),
1113+
1);
1114+
ASSERT_EQ(cross_repo_count_edges_like(&fixture, "angular-2768", "CROSS_HTTP_CALLS",
1115+
"%handle_get_item%"),
1116+
1);
1117+
ASSERT_EQ(cross_repo_count_edges_like(&fixture, "angular-2768", "CROSS_HTTP_CALLS",
1118+
"%handle_get_job%"),
1119+
1);
1120+
1121+
/* Reverse pass */
1122+
const char *consumer = "angular-2768";
1123+
cbm_cross_repo_result_t rev = cbm_cross_repo_match("api-2768", &consumer, 1);
1124+
ASSERT_FALSE(rev.failed);
1125+
ASSERT_EQ(rev.http_edges, 3);
1126+
1127+
cross_repo_fixture_end(&fixture);
1128+
cbm_cross_repo_result_free(&fwd);
1129+
cbm_cross_repo_result_free(&rev);
1130+
PASS();
1131+
}
1132+
9471133
SUITE(cross_repo) {
9481134
RUN_TEST(cross_repo_caller_local_handler_blocks_cross_http_issue1459);
9491135
RUN_TEST(cross_repo_methodless_call_to_local_get_route_blocks_cross_http_issue1459);
9501136
RUN_TEST(cross_repo_caller_without_local_handler_keeps_cross_http_issue1459);
1137+
RUN_TEST(cross_repo_angular_http_client_exact_verb_matching_issue2768);
9511138
RUN_TEST(cross_repo_accepts_project_with_missed_shadow_row_issue1609);
9521139
RUN_TEST(cross_repo_null_target_fails_without_dereference);
9531140
RUN_TEST(cross_repo_wildcard_keeps_projects_containing_internal_tokens);

0 commit comments

Comments
 (0)