Skip to content

Commit b0e71f0

Browse files
committed
fix(win): harden artifact.c git shell-outs against unquoted/unvalidated repo paths
artifact.c shells out to git twice via cbm_popen with the repo path interpolated straight into the command: git -C '%s' rev-parse HEAD 2>/dev/null (git_head_hash) git -C '%s' config merge.ours.driver true 2>/dev/null (ensure_gitattributes) Both used SINGLE quotes with NO validation of repo_path. cmd.exe does not honor single quotes, so on Windows a repo path containing a space broke argument grouping (the artifact commit hash and merge-driver config silently failed for any spaced repo path), and an embedded quote or shell metacharacter could break out of the intended argument. This was the only unvalidated git shell interpolation left in the tree. Different bug class from the CreateProcess argv-quoting fix (which re-parses a single command STRING back into argv); this is popen/shell command-string quoting — hence a separate, atomic PR per review. Fix: validate repo_path with cbm_artifact_repo_path_is_shell_safe() and switch to double quotes + a per-platform null device, matching git_context.c (the best-hardened git shell-out): cbm_validate_shell_arg rejects quote / backslash / substitution metacharacters, and on Windows we also reject the cmd.exe expansion metacharacters % ! ^. Double quotes are honored by both POSIX sh and cmd.exe, so a legitimate spaced repo path now works — the concrete regression the single-quote form caused. The validator is exposed (artifact.h) and guarded by a new test_artifact.c suite: plain + spaced paths accepted; quote / ; / $() / backtick / pipe injection rejected; the cmd.exe metacharacters % ! ^ rejected on Windows (allowed on POSIX, where they are literal inside double quotes). Refs: DeusData#881 Signed-off-by: Flipper <jacobphilipp@ymail.com>
1 parent 94ba1e2 commit b0e71f0

3 files changed

Lines changed: 99 additions & 2 deletions

File tree

‎src/pipeline/artifact.c‎

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ enum {
2121
#include "foundation/compat_fs.h"
2222
#include "foundation/compat.h"
2323
#include "foundation/log.h"
24+
#include "foundation/str_util.h" /* cbm_validate_shell_arg — git shell-out hardening */
2425

2526
#include "zstd_store.h"
2627

@@ -201,10 +202,45 @@ static int write_file_atomic(const char *path, const char *data, size_t len,
201202
return 0;
202203
}
203204

205+
#ifdef _WIN32
206+
#define ARTIFACT_NULL_DEV "NUL"
207+
#else
208+
#define ARTIFACT_NULL_DEV "/dev/null"
209+
#endif
210+
211+
/* See artifact.h. Mirrors git_context.c's git_validate_repo_path (the best-hardened
212+
* git shell-out): cbm_validate_shell_arg rejects quote / backslash / substitution
213+
* metacharacters, and on Windows we also reject the cmd.exe expansion metacharacters
214+
* % ! ^. Callers then use DOUBLE quotes (honored by both POSIX sh and cmd.exe, unlike
215+
* single quotes on cmd.exe), so a repo path may legitimately contain spaces. */
216+
bool cbm_artifact_repo_path_is_shell_safe(const char *repo_path) {
217+
if (!cbm_validate_shell_arg(repo_path)) {
218+
return false;
219+
}
220+
#ifdef _WIN32
221+
for (const char *p = repo_path; *p; p++) {
222+
if (*p == '%' || *p == '!' || *p == '^') {
223+
return false;
224+
}
225+
}
226+
#endif
227+
return true;
228+
}
229+
204230
/* Get current git HEAD hash. buf must be >= CBM_SZ_64. Returns false on error. */
205231
static bool git_head_hash(const char *repo_path, char *buf, size_t bufsz) {
206232
char cmd[CBM_SZ_1K];
207-
snprintf(cmd, sizeof(cmd), "git -C '%s' rev-parse HEAD 2>/dev/null", repo_path);
233+
if (!cbm_artifact_repo_path_is_shell_safe(repo_path)) {
234+
buf[0] = '\0';
235+
return false;
236+
}
237+
int n =
238+
snprintf(cmd, sizeof(cmd), "git -C \"%s\" rev-parse HEAD 2>" ARTIFACT_NULL_DEV, repo_path);
239+
if (n < 0 || (size_t)n >= sizeof(cmd)) {
240+
buf[0] = '\0'; /* truncated command → don't run a malformed shell string (parity with
241+
git_context.c) */
242+
return false;
243+
}
208244
FILE *fp = cbm_popen(cmd, "r");
209245
if (!fp) {
210246
buf[0] = '\0';
@@ -357,8 +393,15 @@ static void ensure_gitattributes(const char *repo_path) {
357393
}
358394

359395
/* Best-effort: configure merge driver */
396+
if (!cbm_artifact_repo_path_is_shell_safe(repo_path)) {
397+
return;
398+
}
360399
char cmd[CBM_SZ_1K];
361-
snprintf(cmd, sizeof(cmd), "git -C '%s' config merge.ours.driver true 2>/dev/null", repo_path);
400+
int n = snprintf(cmd, sizeof(cmd),
401+
"git -C \"%s\" config merge.ours.driver true 2>" ARTIFACT_NULL_DEV, repo_path);
402+
if (n < 0 || (size_t)n >= sizeof(cmd)) {
403+
return; /* truncated command → skip (parity with git_context.c) */
404+
}
362405
FILE *p = cbm_popen(cmd, "r");
363406
if (p) {
364407
(void)cbm_pclose(p);

‎src/pipeline/artifact.h‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,12 @@ bool cbm_artifact_exists(const char *repo_path);
5353
* Returns NULL if artifact doesn't exist or has no commit field. */
5454
char *cbm_artifact_commit(const char *repo_path);
5555

56+
/* Whether repo_path is safe to interpolate into a double-quoted `git -C "…"` shell
57+
* command (as artifact.c does via cbm_popen). Rejects quote / backslash / shell
58+
* substitution metacharacters (cbm_validate_shell_arg); on Windows also rejects the
59+
* cmd.exe expansion metacharacters % ! ^. Spaces ARE allowed — double quotes handle
60+
* them on both POSIX sh and cmd.exe (single quotes, which cmd.exe does not honor,
61+
* were the pre-existing bug). Exposed so the shell-safety contract is unit-tested. */
62+
bool cbm_artifact_repo_path_is_shell_safe(const char *repo_path);
63+
5664
#endif /* CBM_ARTIFACT_H */

‎tests/test_artifact.c‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,53 @@ TEST(artifact_null_safety) {
318318
PASS();
319319
}
320320

321+
/* ── git shell-out path safety ────────────────────────────────────────────────
322+
*
323+
* artifact.c shells out to git via cbm_popen with the repo path interpolated into
324+
* the command. It previously used single quotes (`git -C '%s'`) with NO validation
325+
* — but cmd.exe does not honor single quotes, so on Windows a repo path with a space
326+
* broke argument grouping, and an embedded quote/metacharacter could break out of the
327+
* intended argument entirely. The hardening validates the path and switches to double
328+
* quotes; cbm_artifact_repo_path_is_shell_safe() is the guard. Rejecting quotes and
329+
* shell/cmd.exe metacharacters is the contract; spaces must stay allowed (double
330+
* quotes handle them) — that is the concrete regression the single-quote form caused. */
331+
TEST(artifact_repo_path_shell_safe_accepts_plain_and_spaced) {
332+
ASSERT_TRUE(cbm_artifact_repo_path_is_shell_safe("/home/user/repo"));
333+
ASSERT_TRUE(cbm_artifact_repo_path_is_shell_safe("C:/Users/me/repo"));
334+
ASSERT_TRUE(cbm_artifact_repo_path_is_shell_safe("/home/user/my repo")); /* space OK */
335+
PASS();
336+
}
337+
338+
TEST(artifact_repo_path_shell_safe_rejects_injection) {
339+
ASSERT_FALSE(cbm_artifact_repo_path_is_shell_safe(NULL));
340+
ASSERT_FALSE(cbm_artifact_repo_path_is_shell_safe("it's")); /* single quote */
341+
ASSERT_FALSE(cbm_artifact_repo_path_is_shell_safe("a\"b")); /* double quote */
342+
ASSERT_FALSE(cbm_artifact_repo_path_is_shell_safe("x; rm -rf /")); /* command sep */
343+
ASSERT_FALSE(cbm_artifact_repo_path_is_shell_safe("$(whoami)")); /* substitution */
344+
ASSERT_FALSE(cbm_artifact_repo_path_is_shell_safe("a`id`b")); /* backtick */
345+
ASSERT_FALSE(cbm_artifact_repo_path_is_shell_safe("a|b")); /* pipe */
346+
PASS();
347+
}
348+
349+
TEST(artifact_repo_path_shell_safe_rejects_cmd_metachars_on_windows) {
350+
#ifdef _WIN32
351+
/* cmd.exe expands %VAR%, delayed !VAR!, and escapes with ^ even inside double
352+
* quotes — git_context.c rejects these on Windows and this must match. */
353+
ASSERT_FALSE(cbm_artifact_repo_path_is_shell_safe("C:/a%USERPROFILE%b"));
354+
ASSERT_FALSE(cbm_artifact_repo_path_is_shell_safe("C:/a!b"));
355+
ASSERT_FALSE(cbm_artifact_repo_path_is_shell_safe("C:/a^b"));
356+
#else
357+
/* POSIX shells treat % ! ^ literally inside double quotes — allowed. */
358+
ASSERT_TRUE(cbm_artifact_repo_path_is_shell_safe("/a%b"));
359+
ASSERT_TRUE(cbm_artifact_repo_path_is_shell_safe("/a^b"));
360+
#endif
361+
PASS();
362+
}
363+
321364
SUITE(artifact) {
365+
RUN_TEST(artifact_repo_path_shell_safe_accepts_plain_and_spaced);
366+
RUN_TEST(artifact_repo_path_shell_safe_rejects_injection);
367+
RUN_TEST(artifact_repo_path_shell_safe_rejects_cmd_metachars_on_windows);
322368
RUN_TEST(artifact_export_fast_roundtrip);
323369
RUN_TEST(artifact_export_best_roundtrip);
324370
RUN_TEST(artifact_exists_check);

0 commit comments

Comments
 (0)