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(yaml): accept a UTF-8 BOM as a document prologue (#1656)

Martin Vogel4cd84422ac61
diff --git a/src/cli/config_yaml_edit.c b/src/cli/config_yaml_edit.c
index fda822201..9be176e18 100644
--- a/src/cli/config_yaml_edit.c
+++ b/src/cli/config_yaml_edit.c
@@ -298,13 +298,20 @@ static int yaml_validate_key(const char *key, size_t *out_len) {
return 0;
}
+static int yaml_validate_text_bytes_from(const char *data, size_t len, size_t from);
+
static int yaml_validate_text_bytes(const char *data, size_t len) {
if (len >= YAML_UTF8_BOM_LEN && (unsigned char)data[0] == YAML_BOM_BYTE_0 &&
(unsigned char)data[YAML_UNIT] == YAML_BOM_BYTE_1 &&
(unsigned char)data[YAML_ENTRY_INDENT] == YAML_BOM_BYTE_2) {
return YAML_ERROR;
}
- for (size_t i = 0; i < len; i++) {
+ return yaml_validate_text_bytes_from(data, len, 0U);
+}
+
+/* Body of the byte validation, entered past any prologue. */
+static int yaml_validate_text_bytes_from(const char *data, size_t len, size_t from) {
+ for (size_t i = from; i < len; i++) {
unsigned char c = (unsigned char)data[i];
if (c == '\t' || c == '\0' || c == YAML_DELETE_BYTE) {
return YAML_ERROR;
@@ -737,9 +744,21 @@ static int yaml_read_file(const char *path, char **out_data, size_t *out_len,
#endif
#endif
data[len] = '\0';
- if (yaml_validate_text_bytes(data, len) != 0) {
- free(data);
- return YAML_ERROR;
+ /* Documents may open with a UTF-8 BOM (#1656) — validated past it here,
+ * treated as a prologue by yaml_doc_init, preserved verbatim on write.
+ * Non-document inputs (keys, blocks, identity scalars) keep the strict
+ * no-BOM rule via yaml_validate_text_bytes. */
+ {
+ size_t prologue = 0U;
+ if (len >= YAML_UTF8_BOM_LEN && (unsigned char)data[0] == YAML_BOM_BYTE_0 &&
+ (unsigned char)data[YAML_UNIT] == YAML_BOM_BYTE_1 &&
+ (unsigned char)data[YAML_ENTRY_INDENT] == YAML_BOM_BYTE_2) {
+ prologue = YAML_UTF8_BOM_LEN;
+ }
+ if (yaml_validate_text_bytes_from(data, len, prologue) != 0) {
+ free(data);
+ return YAML_ERROR;
+ }
}
*out_data = data;
*out_len = len;
@@ -1163,6 +1182,22 @@ static int yaml_doc_init(yaml_doc_t *doc, const char *data, size_t len) {
if (yaml_build_lines(doc) != 0) {
return YAML_ERROR;
}
+ /* A UTF-8 BOM is a prologue, not content (#1656): Windows-authored
+ * configs routinely carry one (PowerShell 5.1 Set-Content -Encoding UTF8
+ * writes it), and treating its bytes as the first key made every edit op
+ * fail content-independently. Skip it for structure; edits splice ranges
+ * at line offsets, so the BOM survives every write byte-for-byte. */
+ if (doc->line_count > 0U && doc->len >= YAML_DOC_MARKER_LEN &&
+ (unsigned char)data[0] == 0xEFU && (unsigned char)data[1] == 0xBBU &&
+ (unsigned char)data[2] == 0xBFU) {
+ yaml_line_t *first = &doc->lines[0];
+ if (first->start == 0U && !first->blank) {
+ first->start = YAML_DOC_MARKER_LEN;
+ if (first->start + first->indent >= first->text_end) {
+ first->blank = true;
+ }
+ }
+ }
if (yaml_mark_dquote_continuations(doc) != 0) {
yaml_doc_free(doc);
return YAML_ERROR;
diff --git a/tests/test_cli.c b/tests/test_cli.c
index 634cee89e..7f2314d54 100644
--- a/tests/test_cli.c
+++ b/tests/test_cli.c
@@ -3006,19 +3006,31 @@ TEST(cli_opencode_moved_entry_without_authority_refuses_issue1630) {
FAIL("cbm_mkdtemp failed");
char configpath[512];
snprintf(configpath, sizeof(configpath), "%s/opencode.jsonc", tmpdir);
+#ifdef _WIN32
+ /* A conclusively-missing fixed-drive path authorizes the repair; a
+ * POSIX-shaped or non-local path can never be proven absent (PATHEXT /
+ * remote rules) and stays refused. */
const char *initial = "{\n"
" \"mcp\": {\n"
" \"codebase-memory-mcp\": {\n"
- " \"command\": [\"/old/place/codebase-memory-mcp\"],\n"
+ " \"command\": "
+ "[\"C:\\\\cbm-definitely-missing\\\\codebase-memory-mcp.exe\"],\n"
" \"type\": \"local\"\n"
" }\n"
" }\n"
"}\n";
write_test_file(configpath, initial);
-#ifdef _WIN32
- /* On Windows the conclusive dead-path probe authorizes the repair. */
ASSERT_EQ(cbm_upsert_opencode_mcp("/opt/codebase-memory-mcp", configpath), 0);
#else
+ const char *initial = "{\n"
+ " \"mcp\": {\n"
+ " \"codebase-memory-mcp\": {\n"
+ " \"command\": [\"/old/place/codebase-memory-mcp\"],\n"
+ " \"type\": \"local\"\n"
+ " }\n"
+ " }\n"
+ "}\n";
+ write_test_file(configpath, initial);
ASSERT(cbm_upsert_opencode_mcp("/opt/codebase-memory-mcp", configpath) != 0);
const char *data = read_test_file(configpath);
ASSERT_NOT_NULL(data);
diff --git a/tests/test_config_yaml_edit.c b/tests/test_config_yaml_edit.c
index 077976a10..0c431fddd 100644
--- a/tests/test_config_yaml_edit.c
+++ b/tests/test_config_yaml_edit.c
@@ -1050,6 +1050,51 @@ TEST(config_yaml_edit_hermes_accepts_interior_apostrophe_and_plain_wrap_issue163
PASS();
}
+TEST(config_yaml_edit_accepts_utf8_bom_issue1656) {
+ /* PowerShell 5.1's `Set-Content -Encoding UTF8` writes a BOM, so real
+ * Windows-authored Hermes configs start with EF BB BF — and every edit op
+ * failed content-independently (#1656's 26-byte repro is the reporter's
+ * 23-byte file plus this BOM). The BOM is a prologue: skip it for
+ * structure, preserve it byte-for-byte on write. */
+ const char *initial = "\xEF\xBB\xBFmodel:\n default: test\n";
+ yaml_fixture_t fixture;
+ ASSERT_EQ(yaml_fixture_init(&fixture, initial), 0);
+ ASSERT_EQ(yaml_hermes_upsert(&fixture), CBM_YAML_IDENTITY_EDIT_OK);
+ ASSERT_EQ(yaml_hermes_hook_upsert(&fixture), CBM_YAML_IDENTITY_EDIT_OK);
+ char *after = yaml_read_alloc(fixture.path);
+ ASSERT_NOT_NULL(after);
+ ASSERT_EQ(memcmp(after, "\xEF\xBB\xBFmodel:", 9), 0);
+ ASSERT_NOT_NULL(strstr(after, " default: test\n"));
+ ASSERT_NOT_NULL(strstr(after, "command: \"/opt/codebase-memory-mcp\"\n"));
+ ASSERT_NOT_NULL(strstr(after, "cbm-context"));
+ free(after);
+ th_cleanup(fixture.dir);
+ PASS();
+}
+
+TEST(config_yaml_edit_bom_before_our_own_section_stays_single_issue1656) {
+ /* When the BOM immediately precedes OUR section key, the key lookup must
+ * still see `mcp_servers` — otherwise the upsert misses the existing
+ * section and appends a duplicate. */
+ const char *initial = "\xEF\xBB\xBFmcp_servers:\n"
+ " codebase-memory-mcp:\n"
+ " command: \"/opt/codebase-memory-mcp\"\n";
+ yaml_fixture_t fixture;
+ ASSERT_EQ(yaml_fixture_init(&fixture, initial), 0);
+ ASSERT_EQ(yaml_hermes_upsert(&fixture), CBM_YAML_IDENTITY_EDIT_OK);
+ char *after = yaml_read_alloc(fixture.path);
+ ASSERT_NOT_NULL(after);
+ size_t sections = 0U;
+ for (const char *c = after; (c = strstr(c, "mcp_servers:")) != NULL; c++) {
+ sections++;
+ }
+ ASSERT_EQ((int)sections, 1);
+ ASSERT_EQ(memcmp(after, "\xEF\xBB\xBF", 3), 0);
+ free(after);
+ th_cleanup(fixture.dir);
+ PASS();
+}
+
/* ── Owned-entry repair (#1631 galaxy + goose upgrades) ──────────────────────
*
* Byte-identity alone freezes users on any OLD canonical our past writers
@@ -1910,6 +1955,8 @@ SUITE(config_yaml_edit) {
RUN_TEST(config_yaml_edit_hermes_accepts_escaped_newline_in_double_quote_issue1631);
RUN_TEST(config_yaml_edit_hermes_hook_accepts_column_zero_sequence_issue1631);
RUN_TEST(config_yaml_edit_hermes_accepts_interior_apostrophe_and_plain_wrap_issue1631);
+ RUN_TEST(config_yaml_edit_accepts_utf8_bom_issue1656);
+ RUN_TEST(config_yaml_edit_bom_before_our_own_section_stays_single_issue1656);
RUN_TEST(config_yaml_edit_repairs_prior_unquoted_command_entry_issue1631);
RUN_TEST(config_yaml_edit_repairs_prior_goose_block_without_name);
RUN_TEST(config_yaml_edit_still_refuses_truly_foreign_entry_under_our_key);