Skip to content

Commit e830293

Browse files
authored
Merge pull request DeusData#1065 from tmonestudio/codex/fix-json-edge-properties
fix(pipeline): escape dynamic edge properties as JSON
2 parents 33df139 + 283321f commit e830293

3 files changed

Lines changed: 25 additions & 4 deletions

File tree

‎src/pipeline/pass_definitions.c‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ enum { PD_JSON_FIELD_OVERHEAD = 6 };
2424
#include "foundation/compat.h"
2525
#include "foundation/compat_fs.h"
2626
#include "foundation/limits.h"
27+
#include "foundation/str_util.h"
2728
#include "cbm.h"
2829
#include "arena.h"
2930
#include "iris_export_xml.h"
@@ -464,9 +465,10 @@ static int create_import_edges_for_file(cbm_pipeline_ctx_t *ctx, const CBMFileRe
464465
const cbm_gbuf_node_t *target =
465466
cbm_pipeline_resolve_import_node(ctx, rel, file_qn, imp, namespace_map);
466467
if (target && target->id != source_node->id) {
468+
char esc_ln[CBM_SZ_128];
469+
cbm_json_escape(esc_ln, sizeof(esc_ln), imp->local_name ? imp->local_name : "");
467470
char imp_props[CBM_SZ_256];
468-
snprintf(imp_props, sizeof(imp_props), "{\"local_name\":\"%s\"}",
469-
imp->local_name ? imp->local_name : "");
471+
snprintf(imp_props, sizeof(imp_props), "{\"local_name\":\"%s\"}", esc_ln);
470472
cbm_gbuf_insert_edge(ctx->gbuf, source_node->id, target->id, "IMPORTS", imp_props);
471473
count++;
472474
}

‎src/pipeline/pass_parallel.c‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1941,10 +1941,12 @@ static void emit_graphql_edge(cbm_gbuf_t *gbuf, const cbm_gbuf_node_t *source, c
19411941
cbm_gbuf_upsert_node(gbuf, "Route", p, route_qn, "", 0, 0, "{\"source\":\"graphql\"}");
19421942

19431943
char esc_c[CBM_SZ_256];
1944+
char esc_op[CBM_SZ_512];
19441945
cbm_json_escape(esc_c, sizeof(esc_c), call->callee_name);
1946+
cbm_json_escape(esc_op, sizeof(esc_op), p);
19451947
char props[CBM_SZ_1K];
19461948
snprintf(props, sizeof(props), "{\"callee\":\"%s\",\"operation\":\"%s\",\"confidence\":%.2f}",
1947-
esc_c, p, res->confidence);
1949+
esc_c, esc_op, res->confidence);
19481950
cbm_gbuf_insert_edge(gbuf, source->id, route_id, "GRAPHQL_CALLS", props);
19491951
}
19501952

‎tests/test_lang_contract.c‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <store/store.h>
2525
#include <pipeline/pipeline.h>
2626
#include <foundation/log.h>
27+
#include <sqlite3.h>
2728

2829
#include <string.h>
2930
#include <stdlib.h>
@@ -1656,7 +1657,9 @@ TEST(contract_edge_parallel_service_edges) {
16561657
{"gql.py", "def gql(query_string):\n return query_string\n"},
16571658
{"client.py",
16581659
"from gql import gql\n\n\ndef fetch_user():\n"
1659-
" return gql(\"query GetUser { user { id name } }\")\n\n\n"
1660+
/* The embedded double quote survives extraction from this single-quoted
1661+
* Python string and breaks GRAPHQL_CALLS JSON unless operation is escaped. */
1662+
" return gql('query GetUser { user(name: \"quoted\") { id name } }')\n\n\n"
16601663
"def create_user():\n"
16611664
" return gql(\"mutation CreateUser { addUser(name: \\\"x\\\") { id } }\")\n"},
16621665
/* TRPC_CALLS: local createTRPCProxyClient (same-module resolution). */
@@ -1697,6 +1700,19 @@ TEST(contract_edge_parallel_service_edges) {
16971700
int grpc = store ? cbm_store_count_edges_by_type(store, lp.project, "GRPC_CALLS") : -1;
16981701
int trpc = store ? cbm_store_count_edges_by_type(store, lp.project, "TRPC_CALLS") : -1;
16991702
int infra = store ? cbm_store_count_edges_by_type(store, lp.project, "INFRA_MAPS") : -1;
1703+
int invalid_props = -1;
1704+
if (store) {
1705+
sqlite3_stmt *stmt = NULL;
1706+
sqlite3 *db = cbm_store_get_db(store);
1707+
if (db && sqlite3_prepare_v2(db,
1708+
"SELECT count(*) FROM edges WHERE properties IS NOT NULL "
1709+
"AND properties != '' AND json_valid(properties)=0;",
1710+
-1, &stmt, NULL) == SQLITE_OK &&
1711+
sqlite3_step(stmt) == SQLITE_ROW) {
1712+
invalid_props = sqlite3_column_int(stmt, 0);
1713+
}
1714+
sqlite3_finalize(stmt);
1715+
}
17001716
if (graphql < 1 || grpc < 1 || trpc < 1 || infra < 1) {
17011717
fprintf(stderr,
17021718
" [EDGE] parallel-service: GRAPHQL_CALLS=%d GRPC_CALLS=%d TRPC_CALLS=%d "
@@ -1709,6 +1725,7 @@ TEST(contract_edge_parallel_service_edges) {
17091725
ASSERT_TRUE(grpc >= 1);
17101726
ASSERT_TRUE(trpc >= 1);
17111727
ASSERT_TRUE(infra >= 1);
1728+
ASSERT_EQ(invalid_props, 0);
17121729
PASS();
17131730
}
17141731

0 commit comments

Comments
 (0)