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(infra): secret material in config values never reaches the graph

Martin Vogelf7622fc1a2e0
diff --git a/src/pipeline/pipeline.c b/src/pipeline/pipeline.c
index 36846c819..9e8a66c8e 100644
--- a/src/pipeline/pipeline.c
+++ b/src/pipeline/pipeline.c
@@ -1522,6 +1522,53 @@ static void *gh_compute_thread_fn(void *arg) {
return NULL;
}
+/* ── Secret hygiene: config values that are secret material stay out ─── */
+
+#define INFRA_SLEN(lit) (sizeof(lit) - SKIP_ONE)
+
+/* A file of values, not declarations: .tfvars assigns Terraform variables,
+ * .env the process environment -- where deployments keep their secrets. */
+static bool infra_values_file(const char *fp) {
+ const char *slash = strrchr(fp, '/');
+ const char *base = slash ? slash + SKIP_ONE : fp;
+ size_t n = strlen(base);
+ static const char *const suffixes[] = {".tfvars", ".tfvars.json", ".env"};
+ for (size_t i = 0; i < sizeof(suffixes) / sizeof(suffixes[0]); i++) {
+ size_t s = strlen(suffixes[i]);
+ if (n >= s && strcmp(base + n - s, suffixes[i]) == 0) {
+ return true;
+ }
+ }
+ return strcmp(base, ".env") == 0 || strncmp(base, ".env.", INFRA_SLEN(".env.")) == 0;
+}
+
+/* A manifest's data / stringData / binaryData hold configuration payload
+ * (a Secret keeps its values there); a value under them never declares an
+ * endpoint the service exposes. */
+static bool infra_payload_key(const char *key_path) {
+ if (!key_path) {
+ return false;
+ }
+ size_t first = strcspn(key_path, ".");
+ return (first == INFRA_SLEN("data") && strncmp(key_path, "data", first) == 0) ||
+ (first == INFRA_SLEN("stringData") && strncmp(key_path, "stringData", first) == 0) ||
+ (first == INFRA_SLEN("binaryData") && strncmp(key_path, "binaryData", first) == 0);
+}
+
+/* `url` as a route: scheme, host and path. Userinfo (`user:pass@`), the query
+ * and the fragment are where credentials and tokens ride; none is part of
+ * the endpoint. */
+static void infra_url_route_form(const char *url, char *out, size_t out_sz) {
+ const char *scheme_end = strstr(url, "://");
+ const char *host = scheme_end ? scheme_end + INFRA_SLEN("://") : url;
+ const char *path = host + strcspn(host, "/?#");
+ const char *at = memchr(host, '@', (size_t)(path - host));
+ const char *host_start = at ? at + SKIP_ONE : host;
+ size_t path_len = strcspn(path, "?#");
+ snprintf(out, out_sz, "%.*s%.*s%.*s", (int)(host - url), url, (int)(path - host_start),
+ host_start, (int)path_len, path);
+}
+
/* Extract Route nodes from URL strings found in config files (YAML, HCL, TOML).
* These are infrastructure-defined endpoints (Cloud Scheduler, Terraform). */
/* Process infra bindings: topic→URL pairs from IaC configs.
@@ -1530,10 +1577,17 @@ static void *gh_compute_thread_fn(void *arg) {
/* Process one infra binding: create Route node + INFRA_MAPS edge. */
static int process_one_infra_binding(cbm_gbuf_t *gbuf, const CBMInfraBinding *ib,
const char *rel_path) {
+ /* Secret hygiene, as for config routes: a values file or a token-shaped
+ * URL mints nothing; a kept URL loses its credentials and query. */
+ if (infra_values_file(rel_path) || cbm_is_secret_value(ib->target_url)) {
+ return 0;
+ }
+ char url[CBM_ROUTE_QN_SIZE];
+ infra_url_route_form(ib->target_url, url, sizeof(url));
char url_route_qn[CBM_ROUTE_QN_SIZE];
- snprintf(url_route_qn, sizeof(url_route_qn), "__route__infra__%s", ib->target_url);
- int64_t url_route_id = cbm_gbuf_upsert_node(gbuf, "Route", ib->target_url, url_route_qn,
- rel_path, 0, 0, "{\"source\":\"infra\"}");
+ snprintf(url_route_qn, sizeof(url_route_qn), "__route__infra__%s", url);
+ int64_t url_route_id = cbm_gbuf_upsert_node(gbuf, "Route", url, url_route_qn, rel_path, 0, 0,
+ "{\"source\":\"infra\"}");
char topic_route_qn[CBM_ROUTE_QN_SIZE];
snprintf(topic_route_qn, sizeof(topic_route_qn), "__route__%s__%s",
ib->broker ? ib->broker : "async", ib->source_name);
@@ -1554,7 +1608,7 @@ static int process_one_infra_binding(cbm_gbuf_t *gbuf, const CBMInfraBinding *ib
}
char props[CBM_SZ_512];
snprintf(props, sizeof(props), "{\"broker\":\"%s\",\"topic\":\"%s\",\"endpoint\":\"%s\"}",
- ib->broker ? ib->broker : "async", ib->source_name, ib->target_url);
+ ib->broker ? ib->broker : "async", ib->source_name, url);
cbm_gbuf_insert_edge(gbuf, topic_route_id, url_route_id, "INFRA_MAPS", props);
return SKIP_ONE;
}
@@ -1681,8 +1735,10 @@ static void try_upsert_infra_route(cbm_gbuf_t *gbuf, const CBMStringRef *sr, con
if (!is_exposed_endpoint_key(sr->key_path)) {
return;
}
+ char url[CBM_ROUTE_QN_SIZE];
+ infra_url_route_form(sr->value, url, sizeof(url));
char route_qn[CBM_ROUTE_QN_SIZE];
- snprintf(route_qn, sizeof(route_qn), "__route__infra__%s", sr->value);
+ snprintf(route_qn, sizeof(route_qn), "__route__infra__%s", url);
char route_props[CBM_SZ_512];
if (sr->key_path) {
snprintf(route_props, sizeof(route_props), "{\"source\":\"infra\",\"key_path\":\"%s\"}",
@@ -1690,7 +1746,7 @@ static void try_upsert_infra_route(cbm_gbuf_t *gbuf, const CBMStringRef *sr, con
} else {
snprintf(route_props, sizeof(route_props), "{\"source\":\"infra\"}");
}
- cbm_gbuf_upsert_node(gbuf, "Route", sr->value, route_qn, fp, 0, 0, route_props);
+ cbm_gbuf_upsert_node(gbuf, "Route", url, route_qn, fp, 0, 0, route_props);
}
/* A URL string_ref that does NOT denote a route the service serves: a value
@@ -1705,6 +1761,11 @@ static bool route_sr_denied(const CBMStringRef *sr) {
if (!sr->key_path) {
return true;
}
+ /* Secret hygiene: payload values and token-shaped values are denied by
+ * value, so no sibling ref of the same URL mints it either. */
+ if (infra_payload_key(sr->key_path) || cbm_is_secret_value(sr->value)) {
+ return true;
+ }
return !is_exposed_endpoint_key(sr->key_path);
}
@@ -1726,7 +1787,8 @@ static void cbm_pipeline_extract_infra_routes(const cbm_pipeline_ctx_t *ctx, cbm
CBMHashTable *denied = cbm_ht_create(16);
for (int pass = 0; pass < 2; pass++) {
for (int i = 0; i < file_count; i++) {
- if (!is_infra_file(files[i].rel_path) || is_ci_tooling_config(files[i].rel_path)) {
+ if (!is_infra_file(files[i].rel_path) || is_ci_tooling_config(files[i].rel_path) ||
+ infra_values_file(files[i].rel_path)) {
continue;
}
bool loaded = false;
diff --git a/tests/test_grammar_probe_f.c b/tests/test_grammar_probe_f.c
index 5785ba0ab..7fc7ee08d 100644
--- a/tests/test_grammar_probe_f.c
+++ b/tests/test_grammar_probe_f.c
@@ -35,6 +35,7 @@
#include <store/store.h>
#include <pipeline/pipeline.h>
#include <foundation/log.h>
+#include <sqlite3.h>
#include <string.h>
#include <stdlib.h>
@@ -143,6 +144,7 @@ typedef struct {
int imports; /* IMPORTS edges */
int depends_on; /* DEPENDS_ON edges */
int infra_maps; /* INFRA_MAPS edges */
+ int routes; /* Route label */
} GpfMetrics;
static GpfMetrics gpf_metrics_files(const GpfFile *files, int nfiles) {
@@ -161,6 +163,7 @@ static GpfMetrics gpf_metrics_files(const GpfFile *files, int nfiles) {
m.imports = cbm_store_count_edges_by_type(store, lp.project, "IMPORTS");
m.depends_on = cbm_store_count_edges_by_type(store, lp.project, "DEPENDS_ON");
m.infra_maps = cbm_store_count_edges_by_type(store, lp.project, "INFRA_MAPS");
+ m.routes = gpf_count_label(store, lp.project, "Route");
}
gpf_cleanup(&lp, store);
return m;
@@ -845,6 +848,73 @@ TEST(probe_k8s_selector_across_documents) {
PASS();
}
+/* ── Secret hygiene: config values that are secret material stay out ── */
+
+/* Does any node name / QN / properties or edge properties contain `needle`? */
+static int gpf_graph_mentions(const GpfFile *files, int nfiles, const char *needle) {
+ GpfProj lp;
+ cbm_store_t *store = gpf_index_files(&lp, files, nfiles);
+ int hits = -1;
+ if (store) {
+ hits = 0;
+ static const char *const sql[] = {
+ "SELECT COUNT(*) FROM nodes WHERE project=?1 AND (instr(name,?2) OR "
+ "instr(qualified_name,?2) OR instr(properties,?2))",
+ "SELECT COUNT(*) FROM edges WHERE project=?1 AND instr(properties,?2)"};
+ for (size_t q = 0; q < sizeof(sql) / sizeof(sql[0]); q++) {
+ sqlite3_stmt *st = NULL;
+ if (sqlite3_prepare_v2(cbm_store_get_db(store), sql[q], -1, &st, NULL) == SQLITE_OK) {
+ sqlite3_bind_text(st, 1, lp.project, -1, SQLITE_STATIC);
+ sqlite3_bind_text(st, 2, needle, -1, SQLITE_STATIC);
+ if (sqlite3_step(st) == SQLITE_ROW) {
+ hits += sqlite3_column_int(st, 0);
+ }
+ }
+ sqlite3_finalize(st);
+ }
+ }
+ gpf_cleanup(&lp, store);
+ return hits;
+}
+
+/* A Secret's stringData is secret material, whatever its keys are called. */
+TEST(probe_secret_stringdata_mints_no_route) {
+ static const GpfFile files[] = {{"alerting-secret.yaml",
+ "apiVersion: v1\n"
+ "kind: Secret\n"
+ "metadata:\n"
+ " name: alerting\n"
+ "stringData:\n"
+ " webhook_url: https://hooks.example.com/T0/B0/s3cr3tpath\n"}};
+ ASSERT_EQ(gpf_graph_mentions(files, 1, "s3cr3tpath"), 0);
+ PASS();
+}
+
+/* An endpoint keeps its path; the credentials and query riding on it stay
+ * out of the graph. */
+TEST(probe_endpoint_url_loses_credentials) {
+ static const GpfFile files[] = {{"subscription.yaml",
+ "subscription:\n"
+ " push_endpoint: https://svc:[email protected]/push"
+ "?token=abc123\n"}};
+ GpfMetrics m = gpf_metrics_files(files, 1);
+ ASSERT_TRUE(m.ok);
+ ASSERT_EQ(m.routes, 1);
+ ASSERT_EQ(gpf_graph_mentions(files, 1, "hunter2"), 0);
+ ASSERT_EQ(gpf_graph_mentions(files, 1, "abc123"), 0);
+ ASSERT_TRUE(gpf_graph_mentions(files, 1, "https://svc.example.com/push") > 0);
+ PASS();
+}
+
+/* .tfvars assigns values -- a variable file is where secrets live; the
+ * declaration in the .tf is what the graph keeps. */
+TEST(probe_tfvars_values_mint_no_route) {
+ static const GpfFile files[] = {
+ {"prod.tfvars", "push_endpoint = \"https://svc.example.com/tfvars-only-path\"\n"}};
+ ASSERT_EQ(gpf_graph_mentions(files, 1, "tfvars-only-path"), 0);
+ PASS();
+}
+
/* ── Bundles: a Service selects inside what is deployed together ───── */
#define GPF_DEPLOY(name, label) \
@@ -2416,6 +2486,9 @@ SUITE(grammar_probe_f) {
RUN_TEST(probe_k8s_identity_core_namespaced_and_cluster_scoped);
RUN_TEST(probe_k8s_anchored_and_tagged_names);
RUN_TEST(probe_k8s_selector_flow_mapping);
+ RUN_TEST(probe_secret_stringdata_mints_no_route);
+ RUN_TEST(probe_endpoint_url_loses_credentials);
+ RUN_TEST(probe_tfvars_values_mint_no_route);
RUN_TEST(probe_k8s_selector_stays_in_its_directory);
RUN_TEST(probe_k8s_selector_within_kustomize_build);
RUN_TEST(probe_k8s_kustomize_namespace_is_effective);