|
21 | 21 | #include "foundation/compat_fs.h" |
22 | 22 | #include "foundation/compat.h" |
23 | 23 | #include "foundation/log.h" |
| 24 | +#include "foundation/str_util.h" /* cbm_validate_shell_arg — git shell-out hardening */ |
24 | 25 |
|
25 | 26 | #include "zstd_store.h" |
26 | 27 |
|
@@ -201,10 +202,45 @@ static int write_file_atomic(const char *path, const char *data, size_t len, |
201 | 202 | return 0; |
202 | 203 | } |
203 | 204 |
|
| 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 | + |
204 | 230 | /* Get current git HEAD hash. buf must be >= CBM_SZ_64. Returns false on error. */ |
205 | 231 | static bool git_head_hash(const char *repo_path, char *buf, size_t bufsz) { |
206 | 232 | 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 | + } |
208 | 244 | FILE *fp = cbm_popen(cmd, "r"); |
209 | 245 | if (!fp) { |
210 | 246 | buf[0] = '\0'; |
@@ -357,8 +393,15 @@ static void ensure_gitattributes(const char *repo_path) { |
357 | 393 | } |
358 | 394 |
|
359 | 395 | /* Best-effort: configure merge driver */ |
| 396 | + if (!cbm_artifact_repo_path_is_shell_safe(repo_path)) { |
| 397 | + return; |
| 398 | + } |
360 | 399 | 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 | + } |
362 | 405 | FILE *p = cbm_popen(cmd, "r"); |
363 | 406 | if (p) { |
364 | 407 | (void)cbm_pclose(p); |
|
0 commit comments