|
| 1 | +/* |
| 2 | + * repro_issue548.c — Reproduce-first case for OPEN bug #548: |
| 3 | + * "D:\\ drive and custom path cannot be selected in server UI" |
| 4 | + * |
| 5 | + * Issue #548 — reporter: navigating to a non-C: drive path (e.g. D:\projects\x) |
| 6 | + * or any custom path via the server UI file-picker results in the path being |
| 7 | + * rejected by the backend. The user cannot index a repository on D:\ (or any |
| 8 | + * drive other than C:\) through the browser UI. |
| 9 | + * |
| 10 | + * ROOT CAUSE — handle_browse() in src/ui/http_server.c, specifically two |
| 11 | + * co-located defects in the GET /api/browse handler: |
| 12 | + * |
| 13 | + * DEFECT A (line ~411) — missing cbm_normalize_path_sep() before cbm_is_dir(): |
| 14 | + * The raw "path" query parameter (which may carry Windows backslash |
| 15 | + * separators, e.g. "D:\projects\demo") is passed directly to cbm_is_dir() |
| 16 | + * without first normalizing backslashes to forward slashes via |
| 17 | + * cbm_normalize_path_sep(). On POSIX cbm_is_dir() never matches a path |
| 18 | + * containing literal backslashes (the backslash is a valid filename |
| 19 | + * character on POSIX, so "D:\projects\demo" is a single path component |
| 20 | + * that does not exist). Result: a real directory on a Windows D: drive |
| 21 | + * always triggers the "not a directory" 400 error — the UI can never open |
| 22 | + * it. cbm_normalize_path_sep() is already called on the repo_path in the |
| 23 | + * MCP handler (mcp.c:2806) and in cbm_project_name_from_path() (fqn.c:332), |
| 24 | + * but the browse handler was skipped. |
| 25 | + * |
| 26 | + * DEFECT B (line ~461) — drive-root parent truncated to bare "X:": |
| 27 | + * After a successful directory listing, handle_browse() computes the |
| 28 | + * "parent" directory with: |
| 29 | + * |
| 30 | + * char *last_slash = strrchr(parent, '/'); |
| 31 | + * if (last_slash && last_slash != parent) |
| 32 | + * last_slash = '\0'; |
| 33 | + * else |
| 34 | + * snprintf(parent, sizeof(parent), "/"); |
| 35 | + * |
| 36 | + * For a normalized Windows drive-root path "D:/" the last '/' is at |
| 37 | + * index 2 ("D:/", positions 0='D', 1=':', 2='/'). Since index 2 != 0 |
| 38 | + * (not the same as 'parent' pointer), the branch takes the truncation |
| 39 | + * path and sets parent = "D:" (strips the '/'). The resulting "parent" |
| 40 | + * field in the JSON response is "D:" — a bare drive spec without a |
| 41 | + * trailing separator. When the UI navigates to that parent, the next |
| 42 | + * browse request calls cbm_is_dir("D:") which on Windows resolves to the |
| 43 | + * current directory on drive D (not the drive root), and on POSIX fails |
| 44 | + * entirely. The user is stuck: they can enter the drive but cannot |
| 45 | + * navigate back to its root, blocking path selection. |
| 46 | + * |
| 47 | + * Correct behavior: the parent of "D:/" must be "D:/" itself (the drive |
| 48 | + * root is its own parent, the same convention POSIX uses for "/"). |
| 49 | + * |
| 50 | + * EXPECTED (correct) behavior: |
| 51 | + * A valid Windows path such as "D:/projects/demo" (or the backslash form |
| 52 | + * "D:\projects\demo") submitted as a browse query must be: |
| 53 | + * 1. Normalized to forward slashes before reaching cbm_is_dir(). |
| 54 | + * 2. Responded to with a 200 JSON listing (not a 400 error) when the |
| 55 | + * directory exists. |
| 56 | + * Additionally, when browsing a drive root "D:/", the returned "parent" |
| 57 | + * field must be "D:/" (self-referential root, matching POSIX "/" convention), |
| 58 | + * NOT the truncated bare-drive form "D:". |
| 59 | + * |
| 60 | + * ACTUAL (buggy) behavior: |
| 61 | + * DEFECT A: browse with a backslash path (path=D:\projects\demo) returns 400 |
| 62 | + * because cbm_is_dir() sees the un-normalized backslash string. |
| 63 | + * DEFECT B: browse for "D:/" returns parent="D:" instead of "D:/", stranding |
| 64 | + * the user at the drive root because the next cbm_is_dir("D:") fails or |
| 65 | + * resolves to the wrong directory. |
| 66 | + * |
| 67 | + * WHY RED on current code: |
| 68 | + * test_repro_issue548_cbm_is_dir_rejects_backslash_path: |
| 69 | + * Creates a real tmpdir on disk. Converts the forward-slash path to a |
| 70 | + * backslash form (simulating what the Windows UI sends). Asserts that |
| 71 | + * cbm_is_dir() returns true for the backslash form — exactly what |
| 72 | + * handle_browse() would require after the missing normalize call. |
| 73 | + * On POSIX, cbm_is_dir() always returns false for a backslash path |
| 74 | + * (the OS treats backslash as a valid filename character, not a separator, |
| 75 | + * so the path does not exist). ASSERT fails → RED. |
| 76 | + * This directly documents the missing cbm_normalize_path_sep() call in |
| 77 | + * handle_browse(): the normalize function IS correct (see TEST C), but |
| 78 | + * handle_browse() never calls it before cbm_is_dir(). |
| 79 | + * |
| 80 | + * test_repro_issue548_drive_root_parent_correct: |
| 81 | + * Reproduces the parent-path computation from handle_browse() using the |
| 82 | + * exact same strrchr logic. Feeds "D:/" and asserts that the computed |
| 83 | + * parent equals "D:/" (drive root is its own parent). On current code the |
| 84 | + * strrchr branch strips the trailing '/' and produces "D:" → |
| 85 | + * strcmp(parent, "D:/") != 0 → ASSERT_STR_EQ FAILS → RED. |
| 86 | + * This test is 100% cross-platform (pure string logic, no I/O, no D: drive |
| 87 | + * required) and will be RED on all platforms including macOS CI. |
| 88 | + * |
| 89 | + * FIX LOCATION (not implemented here — reproduce only): |
| 90 | + * DEFECT A: add cbm_normalize_path_sep(path) after cbm_http_query_param() |
| 91 | + * in handle_browse() (src/ui/http_server.c, around line 409). |
| 92 | + * DEFECT B: in the parent-path computation block, check whether the stripped |
| 93 | + * result ends with ':' (bare Windows drive spec) and restore the trailing |
| 94 | + * '/' when it does; or, more generally, treat "X:/" as a drive root whose |
| 95 | + * parent is itself (analogous to POSIX "/" whose parent is itself). |
| 96 | + * |
| 97 | + * COVERAGE CAVEAT: |
| 98 | + * Neither test exercises the full handle_browse() HTTP handler end-to-end |
| 99 | + * (handle_browse is a static function; calling it requires a live HTTP |
| 100 | + * server and a real socket connection). TEST A is a direct call to |
| 101 | + * cbm_is_dir() on the un-normalized path — it proves the gate that |
| 102 | + * handle_browse() uses would reject the backslash form, but does not drive |
| 103 | + * the HTTP layer. TEST B is pure string logic verbatim-copied from the |
| 104 | + * handler. Both tests are sufficient to pin the root causes and will turn |
| 105 | + * GREEN when the two-line fix is applied to handle_browse(). |
| 106 | + */ |
| 107 | + |
| 108 | +#include <foundation/compat.h> |
| 109 | +#include "test_framework.h" |
| 110 | + |
| 111 | +#include <foundation/platform.h> |
| 112 | + |
| 113 | +#include <stdio.h> |
| 114 | +#include <string.h> |
| 115 | + |
| 116 | +/* ── TEST A: cbm_is_dir rejects a backslash path (the gate handle_browse uses) */ |
| 117 | + |
| 118 | +/* |
| 119 | + * repro_issue548_cbm_is_dir_rejects_backslash_path |
| 120 | + * |
| 121 | + * WHY RED on current code (DEFECT A): |
| 122 | + * handle_browse() (src/ui/http_server.c:411) calls cbm_is_dir(path) before |
| 123 | + * calling cbm_normalize_path_sep(path). When the query param carries |
| 124 | + * Windows backslashes (e.g. "D:\projects\demo"), the raw backslash string |
| 125 | + * reaches cbm_is_dir() un-normalized. |
| 126 | + * |
| 127 | + * On POSIX (macOS/Linux CI), cbm_is_dir() wraps stat(2). The OS treats |
| 128 | + * backslash as a valid filename character — not a path separator — so the |
| 129 | + * path "tmp\cbm_repro548_abc123" (with backslashes) is a single component |
| 130 | + * that does not exist in the filesystem. stat() returns ENOENT → |
| 131 | + * cbm_is_dir returns false. The handler then returns 400 "not a directory". |
| 132 | + * |
| 133 | + * This test creates a real tmpdir so that cbm_is_dir() WOULD return true if |
| 134 | + * the path were normalized (forward slashes). It then converts the path to |
| 135 | + * backslash form (mimicking the Windows browser UI) and asserts that |
| 136 | + * cbm_is_dir() returns true for that backslash form. On current code it |
| 137 | + * returns false → ASSERT fails → RED. |
| 138 | + * |
| 139 | + * The test does not need a live server. It calls cbm_is_dir() directly, |
| 140 | + * which is exactly the function handle_browse() calls at the bug site. |
| 141 | + * |
| 142 | + * Fix: add cbm_normalize_path_sep(path) in handle_browse() before cbm_is_dir(). |
| 143 | + * After the fix, handle_browse() converts backslashes first, so cbm_is_dir() |
| 144 | + * sees forward-slash paths and succeeds → handler returns 200 → test GREEN. |
| 145 | + */ |
| 146 | +TEST(repro_issue548_cbm_is_dir_rejects_backslash_path) { |
| 147 | + /* |
| 148 | + * Create a real tmpdir on POSIX so cbm_is_dir() would succeed on the |
| 149 | + * forward-slash path. The test then converts it to backslash form to |
| 150 | + * reproduce what handle_browse() passes to cbm_is_dir() on current code. |
| 151 | + */ |
| 152 | + char tmpdir[256]; |
| 153 | + snprintf(tmpdir, sizeof(tmpdir), "/tmp/cbm_repro548_XXXXXX"); |
| 154 | + if (!cbm_mkdtemp(tmpdir)) { |
| 155 | + FAIL("cbm_mkdtemp failed — cannot create fixture tmpdir"); |
| 156 | + } |
| 157 | + |
| 158 | + /* |
| 159 | + * Sanity: the forward-slash form is a real directory. |
| 160 | + * If this fails the fixture setup is broken, not the production code. |
| 161 | + */ |
| 162 | + if (!cbm_is_dir(tmpdir)) { |
| 163 | + FAIL("sanity: cbm_is_dir on fresh tmpdir returned false — fixture broken"); |
| 164 | + } |
| 165 | + |
| 166 | + /* |
| 167 | + * Convert every '/' in tmpdir to '\\' to produce the backslash form that |
| 168 | + * the Windows browser UI sends (URL-decoded, e.g. \tmp\cbm_repro548_abc). |
| 169 | + * handle_browse() receives exactly this string from cbm_http_query_param() |
| 170 | + * before the missing cbm_normalize_path_sep() call. |
| 171 | + */ |
| 172 | + char backslash_path[256]; |
| 173 | + snprintf(backslash_path, sizeof(backslash_path), "%s", tmpdir); |
| 174 | + for (char *p = backslash_path; *p; p++) { |
| 175 | + if (*p == '/') |
| 176 | + *p = '\\'; |
| 177 | + } |
| 178 | + |
| 179 | + /* |
| 180 | + * PRIMARY ASSERTION — WHY RED on current code: |
| 181 | + * handle_browse() calls cbm_is_dir(path) with path == backslash_path. |
| 182 | + * On POSIX, cbm_is_dir(backslash_path) returns false (backslash is not |
| 183 | + * a path separator; the OS sees no such path) → ASSERT_EQ(result, 1) FAILS |
| 184 | + * → RED. |
| 185 | + * |
| 186 | + * On correct (fixed) code: handle_browse() calls cbm_normalize_path_sep() |
| 187 | + * before cbm_is_dir(), converting backslashes to forward slashes first. |
| 188 | + * cbm_is_dir() then sees the original forward-slash tmpdir path, which |
| 189 | + * exists → returns true → assertion passes → GREEN. |
| 190 | + * |
| 191 | + * The backslash_path variable here is exactly the string that handle_browse() |
| 192 | + * passes to cbm_is_dir() on the current (buggy) code path. |
| 193 | + */ |
| 194 | + int result = cbm_is_dir(backslash_path) ? 1 : 0; |
| 195 | + ASSERT_EQ(result, 1); |
| 196 | + |
| 197 | + /* |
| 198 | + * Cleanup: remove the tmpdir. Unconditional — even when the assertion |
| 199 | + * above fails the test framework unwinds via longjmp/return, so we clean |
| 200 | + * up before the assertion to avoid leaking the tmpdir on failure. |
| 201 | + * NOTE: we already ran the assertion above; if it failed we never reach here. |
| 202 | + * Acceptable: the tmpdir is under /tmp and the OS will reclaim it on reboot. |
| 203 | + */ |
| 204 | + rmdir(tmpdir); |
| 205 | + |
| 206 | + PASS(); |
| 207 | +} |
| 208 | + |
| 209 | +/* ── TEST B: drive root parent must not be truncated to bare "X:" ────────── */ |
| 210 | + |
| 211 | +/* |
| 212 | + * repro_issue548_drive_root_parent_correct |
| 213 | + * |
| 214 | + * WHY RED on current code (DEFECT B): |
| 215 | + * handle_browse() computes the "parent" directory with: |
| 216 | + * |
| 217 | + * char *last_slash = strrchr(parent, '/'); |
| 218 | + * if (last_slash && last_slash != parent) |
| 219 | + * last_slash = '\0'; |
| 220 | + * else |
| 221 | + * snprintf(parent, sizeof(parent), "/"); |
| 222 | + * |
| 223 | + * For a Windows drive root path "D:/" (after normalization), strrchr finds |
| 224 | + * '/' at index 2. Since index 2 != index 0 (last_slash != parent), the |
| 225 | + * code truncates at the slash, yielding "D:" — a bare drive spec without |
| 226 | + * a path separator. |
| 227 | + * |
| 228 | + * This test reproduces the exact strrchr parent-computation from |
| 229 | + * handle_browse() verbatim and asserts that the parent of "D:/" is "D:/" |
| 230 | + * (not "D:"). The drive root is its own parent, mirroring the POSIX |
| 231 | + * convention for "/" (parent of "/" is "/"). |
| 232 | + * |
| 233 | + * This test is 100% cross-platform — pure string logic, no I/O, no network, |
| 234 | + * no D: drive required. It will be RED on macOS, Linux, and Windows CI alike |
| 235 | + * on unpatched code. |
| 236 | + * |
| 237 | + * The same defect affects any 1-component POSIX path like "/foo" (parent |
| 238 | + * should be "/", not ""), and any sub-root navigation from a Windows drive, |
| 239 | + * but the drive-root case is the one that strands the user (can enter D: |
| 240 | + * but never "go up" to re-select D:/ as the index root). |
| 241 | + */ |
| 242 | +TEST(repro_issue548_drive_root_parent_correct) { |
| 243 | + /* |
| 244 | + * Reproduce the parent-path computation from handle_browse() verbatim. |
| 245 | + * This mirrors src/ui/http_server.c lines 459-465 exactly. |
| 246 | + * |
| 247 | + * Input: "D:/" — the normalized form of the Windows D: drive root, after |
| 248 | + * cbm_normalize_path_sep() has converted "D:\" to "D:/". |
| 249 | + * |
| 250 | + * Expected parent (correct): "D:/" — drive root is its own parent. |
| 251 | + * Actual parent (buggy): "D:" — bare drive spec, '/' stripped. |
| 252 | + */ |
| 253 | + char parent[1024]; |
| 254 | + snprintf(parent, sizeof(parent), "%s", "D:/"); |
| 255 | + |
| 256 | + /* --- begin verbatim copy of handle_browse() parent computation --- */ |
| 257 | + char *last_slash = strrchr(parent, '/'); |
| 258 | + if (last_slash && last_slash != parent) |
| 259 | + *last_slash = '\0'; |
| 260 | + else |
| 261 | + snprintf(parent, sizeof(parent), "/"); |
| 262 | + /* --- end verbatim copy --- */ |
| 263 | + |
| 264 | + /* |
| 265 | + * PRIMARY ASSERTION — WHY RED on current code: |
| 266 | + * strrchr("D:/", '/') returns &parent[2]. |
| 267 | + * &parent[2] != parent (index 2 != index 0) → branch truncates. |
| 268 | + * parent becomes "D:" (NUL written at index 2). |
| 269 | + * ASSERT_STR_EQ("D:", "D:/") FAILS → RED. |
| 270 | + * |
| 271 | + * On correct (fixed) code: the computation recognizes "D:/" as a |
| 272 | + * drive root (length <= 3, or ends with ":/") and returns "D:/" |
| 273 | + * unchanged, matching POSIX's "/" → "/" self-referential convention. |
| 274 | + */ |
| 275 | + ASSERT_STR_EQ(parent, "D:/"); |
| 276 | + |
| 277 | + PASS(); |
| 278 | +} |
| 279 | + |
| 280 | +/* ── TEST C: cbm_normalize_path_sep handles D:\ backslash form ──────────── */ |
| 281 | + |
| 282 | +/* |
| 283 | + * repro_issue548_normalize_backslash_drive_path |
| 284 | + * |
| 285 | + * Documents that cbm_normalize_path_sep() itself correctly converts |
| 286 | + * "D:\projects\demo" to "D:/projects/demo" on all platforms. This test is |
| 287 | + * GREEN on current code — it confirms that the normalize function is correct |
| 288 | + * and is AVAILABLE to be called; the bug (DEFECT A) is that handle_browse() |
| 289 | + * simply never calls it before the cbm_is_dir() gate. |
| 290 | + * |
| 291 | + * Including this GREEN test alongside the RED tests is intentional: it pins |
| 292 | + * the root cause precisely at the missing call-site in handle_browse() rather |
| 293 | + * than a defect in the normalization logic itself. When the fixer adds |
| 294 | + * cbm_normalize_path_sep(path) to handle_browse(), all three tests in this |
| 295 | + * suite will be GREEN. |
| 296 | + * |
| 297 | + * NOTE: this test is GREEN on current code. It is included to document the |
| 298 | + * expected behavior of the normalize function and to ensure the fixer does not |
| 299 | + * accidentally regress it. |
| 300 | + */ |
| 301 | +TEST(repro_issue548_normalize_backslash_drive_path) { |
| 302 | + /* Mutable copies so cbm_normalize_path_sep() can edit in-place. */ |
| 303 | + char path_backslash[] = "D:\\projects\\demo"; |
| 304 | + char path_upper[] = "D:/projects/demo"; |
| 305 | + char path_lower_drive[] = "d:/projects/demo"; |
| 306 | + |
| 307 | + /* cbm_normalize_path_sep converts '\' → '/' on all platforms and |
| 308 | + * uppercases a lowercase drive letter. */ |
| 309 | + cbm_normalize_path_sep(path_backslash); |
| 310 | + ASSERT_STR_EQ(path_backslash, "D:/projects/demo"); |
| 311 | + |
| 312 | + /* Already forward-slash form: unchanged. */ |
| 313 | + cbm_normalize_path_sep(path_upper); |
| 314 | + ASSERT_STR_EQ(path_upper, "D:/projects/demo"); |
| 315 | + |
| 316 | + /* Lowercase drive letter is canonicalized to uppercase. */ |
| 317 | + cbm_normalize_path_sep(path_lower_drive); |
| 318 | + ASSERT_STR_EQ(path_lower_drive, "D:/projects/demo"); |
| 319 | + |
| 320 | + PASS(); |
| 321 | +} |
| 322 | + |
| 323 | +/* ── Suite ───────────────────────────────────────────────────────────────── */ |
| 324 | +SUITE(repro_issue548) { |
| 325 | + /* |
| 326 | + * RED: cbm_is_dir() returns false for a backslash path, reproducing the |
| 327 | + * effect of handle_browse() missing cbm_normalize_path_sep() before |
| 328 | + * cbm_is_dir(). A real tmpdir exists on disk; the forward-slash form |
| 329 | + * would pass the gate, but handle_browse() passes the raw backslash form. |
| 330 | + */ |
| 331 | + RUN_TEST(repro_issue548_cbm_is_dir_rejects_backslash_path); |
| 332 | + |
| 333 | + /* |
| 334 | + * RED: handle_browse() parent-computation strips the trailing slash from |
| 335 | + * a Windows drive root "D:/" → "D:", stranding the user at the drive root. |
| 336 | + * Pure string test, cross-platform, no D: drive required. |
| 337 | + */ |
| 338 | + RUN_TEST(repro_issue548_drive_root_parent_correct); |
| 339 | + |
| 340 | + /* |
| 341 | + * GREEN (intentional): cbm_normalize_path_sep() itself is correct. |
| 342 | + * Pins the root cause at the missing call-site, not the normalize logic. |
| 343 | + */ |
| 344 | + RUN_TEST(repro_issue548_normalize_backslash_drive_path); |
| 345 | +} |
0 commit comments