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(security): block " < > in cbm_validate_shell_arg

Martin Vogeleca433bd5801
diff --git a/src/foundation/str_util.c b/src/foundation/str_util.c
index 738883e27..1c23c7110 100644
--- a/src/foundation/str_util.c
+++ b/src/foundation/str_util.c
@@ -249,11 +249,14 @@ bool cbm_validate_shell_arg(const char *s) {
for (const char *p = s; *p; p++) {
switch (*p) {
case '\'':
+ case '"':
case ';':
case '|':
case '&':
case '$':
case '`':
+ case '<':
+ case '>':
case '\n':
case '\r':
#ifndef _WIN32
diff --git a/src/foundation/str_util.h b/src/foundation/str_util.h
index 724dc3ef4..cab2faa50 100644
--- a/src/foundation/str_util.h
+++ b/src/foundation/str_util.h
@@ -49,7 +49,11 @@ char *cbm_str_strip_ext(CBMArena *a, const char *path);
char **cbm_str_split(CBMArena *a, const char *s, char delim, int *out_count);
/* Validate a string is safe for shell interpolation inside single quotes.
- * Rejects: ' ; | & $ ` \n \r \0 (embedded NULs via len check).
+ * Rejects: ' " ; | & $ ` < > \n \r \0 (embedded NULs via len check).
+ * The Windows search path wraps shell args in cmd.exe-level "powershell -Command
+ * \"...'%s'...\"", so " can close the cmd.exe outer quote even if PowerShell's
+ * single quotes hold; < > would then become cmd.exe redirection (file-write
+ * primitive). Blocking these unconditionally hardens both POSIX and Windows.
* Returns true if safe, false if the string contains shell metacharacters. */
bool cbm_validate_shell_arg(const char *s);
diff --git a/tests/test_security.c b/tests/test_security.c
index 21bc15f25..789809fc4 100644
--- a/tests/test_security.c
+++ b/tests/test_security.c
@@ -75,6 +75,24 @@ TEST(shell_rejects_null) {
PASS();
}
+TEST(shell_rejects_double_quote) {
+ /* On Windows, the search code path wraps args in cmd.exe-level
+ * "powershell -Command \"...'%s'...\"". A " in the input would close
+ * the cmd.exe outer quote. Block unconditionally. */
+ ASSERT_FALSE(cbm_validate_shell_arg("foo\"bar"));
+ PASS();
+}
+
+TEST(shell_rejects_redirect_out) {
+ ASSERT_FALSE(cbm_validate_shell_arg("foo>out.txt"));
+ PASS();
+}
+
+TEST(shell_rejects_redirect_in) {
+ ASSERT_FALSE(cbm_validate_shell_arg("foo<in.txt"));
+ PASS();
+}
+
TEST(shell_accepts_clean_path) {
ASSERT_TRUE(cbm_validate_shell_arg("/home/user/.local/bin/codebase-memory-mcp"));
PASS();
@@ -364,6 +382,9 @@ SUITE(security) {
RUN_TEST(shell_rejects_newline);
RUN_TEST(shell_rejects_carriage_return);
RUN_TEST(shell_rejects_null);
+ RUN_TEST(shell_rejects_double_quote);
+ RUN_TEST(shell_rejects_redirect_out);
+ RUN_TEST(shell_rejects_redirect_in);
RUN_TEST(shell_accepts_clean_path);
RUN_TEST(shell_accepts_spaces);
RUN_TEST(shell_accepts_dots_dashes);