Skip to content

Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.

fix(pipeline): escape dynamic edge properties as JSON

tmonestudio283321f07fab
diff --git a/src/pipeline/pass_definitions.c b/src/pipeline/pass_definitions.c
index 747f7e16b..dc414ee75 100644
--- a/src/pipeline/pass_definitions.c
+++ b/src/pipeline/pass_definitions.c
@@ -24,6 +24,7 @@ enum { PD_JSON_FIELD_OVERHEAD = 6 };
#include "foundation/compat.h"
#include "foundation/compat_fs.h"
#include "foundation/limits.h"
+#include "foundation/str_util.h"
#include "cbm.h"
#include "arena.h"
#include "iris_export_xml.h"
@@ -470,9 +471,10 @@ static int create_import_edges_for_file(cbm_pipeline_ctx_t *ctx, const CBMFileRe
const cbm_gbuf_node_t *target =
cbm_pipeline_resolve_import_node(ctx, rel, file_qn, imp, namespace_map);
if (target && target->id != source_node->id) {
+ char esc_ln[CBM_SZ_128];
+ cbm_json_escape(esc_ln, sizeof(esc_ln), imp->local_name ? imp->local_name : "");
char imp_props[CBM_SZ_256];
- snprintf(imp_props, sizeof(imp_props), "{\"local_name\":\"%s\"}",
- imp->local_name ? imp->local_name : "");
+ snprintf(imp_props, sizeof(imp_props), "{\"local_name\":\"%s\"}", esc_ln);
cbm_gbuf_insert_edge(ctx->gbuf, source_node->id, target->id, "IMPORTS", imp_props);
count++;
}
diff --git a/src/pipeline/pass_parallel.c b/src/pipeline/pass_parallel.c
index a34b45eb9..3abed9e00 100644
--- a/src/pipeline/pass_parallel.c
+++ b/src/pipeline/pass_parallel.c
@@ -1921,10 +1921,12 @@ static void emit_graphql_edge(cbm_gbuf_t *gbuf, const cbm_gbuf_node_t *source, c
cbm_gbuf_upsert_node(gbuf, "Route", p, route_qn, "", 0, 0, "{\"source\":\"graphql\"}");
char esc_c[CBM_SZ_256];
+ char esc_op[CBM_SZ_512];
cbm_json_escape(esc_c, sizeof(esc_c), call->callee_name);
+ cbm_json_escape(esc_op, sizeof(esc_op), p);
char props[CBM_SZ_1K];
snprintf(props, sizeof(props), "{\"callee\":\"%s\",\"operation\":\"%s\",\"confidence\":%.2f}",
- esc_c, p, res->confidence);
+ esc_c, esc_op, res->confidence);
cbm_gbuf_insert_edge(gbuf, source->id, route_id, "GRAPHQL_CALLS", props);
}
diff --git a/tests/test_lang_contract.c b/tests/test_lang_contract.c
index c8abfd118..bbc5651cd 100644
--- a/tests/test_lang_contract.c
+++ b/tests/test_lang_contract.c
@@ -24,6 +24,7 @@
#include <store/store.h>
#include <pipeline/pipeline.h>
#include <foundation/log.h>
+#include <sqlite3.h>
#include <string.h>
#include <stdlib.h>
@@ -1518,7 +1519,9 @@ TEST(contract_edge_parallel_service_edges) {
{"gql.py", "def gql(query_string):\n return query_string\n"},
{"client.py",
"from gql import gql\n\n\ndef fetch_user():\n"
- " return gql(\"query GetUser { user { id name } }\")\n\n\n"
+ /* The embedded double quote survives extraction from this single-quoted
+ * Python string and breaks GRAPHQL_CALLS JSON unless operation is escaped. */
+ " return gql('query GetUser { user(name: \"quoted\") { id name } }')\n\n\n"
"def create_user():\n"
" return gql(\"mutation CreateUser { addUser(name: \\\"x\\\") { id } }\")\n"},
/* TRPC_CALLS: local createTRPCProxyClient (same-module resolution). */
@@ -1559,6 +1562,19 @@ TEST(contract_edge_parallel_service_edges) {
int grpc = store ? cbm_store_count_edges_by_type(store, lp.project, "GRPC_CALLS") : -1;
int trpc = store ? cbm_store_count_edges_by_type(store, lp.project, "TRPC_CALLS") : -1;
int infra = store ? cbm_store_count_edges_by_type(store, lp.project, "INFRA_MAPS") : -1;
+ int invalid_props = -1;
+ if (store) {
+ sqlite3_stmt *stmt = NULL;
+ sqlite3 *db = cbm_store_get_db(store);
+ if (db && sqlite3_prepare_v2(db,
+ "SELECT count(*) FROM edges WHERE properties IS NOT NULL "
+ "AND properties != '' AND json_valid(properties)=0;",
+ -1, &stmt, NULL) == SQLITE_OK &&
+ sqlite3_step(stmt) == SQLITE_ROW) {
+ invalid_props = sqlite3_column_int(stmt, 0);
+ }
+ sqlite3_finalize(stmt);
+ }
if (graphql < 1 || grpc < 1 || trpc < 1 || infra < 1) {
fprintf(stderr,
" [EDGE] parallel-service: GRAPHQL_CALLS=%d GRPC_CALLS=%d TRPC_CALLS=%d "
@@ -1571,6 +1587,7 @@ TEST(contract_edge_parallel_service_edges) {
ASSERT_TRUE(grpc >= 1);
ASSERT_TRUE(trpc >= 1);
ASSERT_TRUE(infra >= 1);
+ ASSERT_EQ(invalid_props, 0);
PASS();
}