Skip to content

Commit 971111c

Browse files
authored
Merge pull request DeusData#2070 from DeusData/fix/daemon-session-root-separators
fix(daemon): keep one separator spelling for the session root on Windows
2 parents e2d538a + 9846c3f commit 971111c

4 files changed

Lines changed: 85 additions & 0 deletions

File tree

‎src/daemon/application.c‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1588,6 +1588,33 @@ static bool application_index_args_normalize_defaults(yyjson_mut_val *root) {
15881588
return true;
15891589
}
15901590

1591+
/* One directory is one root. The auto-index job spells repo_path the way the
1592+
* session policy holds it - the platform's native form, backslashes on
1593+
* Windows - while an explicit index_repository request arrives in the
1594+
* handler's forward-slash spelling. Compared byte-exact the two never matched
1595+
* on Windows, and the request was refused as an options conflict instead of
1596+
* joining the job already running for its root. The policy keeps its
1597+
* spelling: the sensitive-root and allowed-root containment checks match it
1598+
* byte-exact against HOME and the granted roots, and respelling it there
1599+
* admitted $HOME. So the fold happens here, on this comparison's private copy,
1600+
* and nothing the daemon stores changes. */
1601+
static bool application_index_args_fold_repo_path(yyjson_mut_doc *document) {
1602+
yyjson_mut_val *root = yyjson_mut_doc_get_root(document);
1603+
yyjson_mut_val *repo_path = yyjson_mut_obj_get(root, "repo_path");
1604+
if (!repo_path || !yyjson_mut_is_str(repo_path)) {
1605+
return true;
1606+
}
1607+
char *folded = strdup(yyjson_mut_get_str(repo_path));
1608+
if (!folded) {
1609+
return false;
1610+
}
1611+
cbm_normalize_path_sep(folded);
1612+
yyjson_mut_val *key = yyjson_mut_str(document, "repo_path");
1613+
yyjson_mut_val *value = yyjson_mut_strcpy(document, folded);
1614+
free(folded);
1615+
return key && value && yyjson_mut_obj_replace(root, key, value);
1616+
}
1617+
15911618
static bool application_index_args_equal(const char *left, const char *right) {
15921619
if (!left || !right) {
15931620
return false;
@@ -1600,6 +1627,8 @@ static bool application_index_args_equal(const char *left, const char *right) {
16001627
yyjson_mut_val *right_root = right_copy ? yyjson_mut_doc_get_root(right_copy) : NULL;
16011628
bool equal = application_index_args_normalize_defaults(left_root) &&
16021629
application_index_args_normalize_defaults(right_root) &&
1630+
application_index_args_fold_repo_path(left_copy) &&
1631+
application_index_args_fold_repo_path(right_copy) &&
16031632
yyjson_mut_equals(left_root, right_root);
16041633
yyjson_mut_doc_free(left_copy);
16051634
yyjson_mut_doc_free(right_copy);
@@ -1608,6 +1637,10 @@ static bool application_index_args_equal(const char *left, const char *right) {
16081637
return equal;
16091638
}
16101639

1640+
bool cbm_daemon_application_index_args_equal_for_test(const char *left, const char *right) {
1641+
return application_index_args_equal(left, right);
1642+
}
1643+
16111644
/* Caller holds application->mutex. Keeping watcher ownership validation and
16121645
* this admission in the same critical section closes the unwatch race. */
16131646
static cbm_daemon_application_job_t *application_job_subscribe_locked(

‎src/daemon/application_internal.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,10 @@ int cbm_daemon_application_busy_queue_waits_for_test(void);
4545
char *cbm_daemon_application_framable_response_for_test(char *response,
4646
const cbm_jsonrpc_request_t *request);
4747

48+
/* The job registry's index-argument equality: the running job's args against
49+
* a request's. Exposed because the two repo_path spellings it folds - the
50+
* session policy's native root and a handler's forward-slash one - only differ
51+
* on Windows, while the fold itself runs on every platform. */
52+
bool cbm_daemon_application_index_args_equal_for_test(const char *left, const char *right);
53+
4854
#endif /* CBM_DAEMON_APPLICATION_INTERNAL_H */

‎tests/test_daemon.c‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,26 @@ TEST(daemon_sessions_keep_distinct_roots_and_allowed_root_policy) {
467467
PASS();
468468
}
469469

470+
/* The session policy keeps a root in the spelling the daemon canonicalized it
471+
* to - the platform's native form, backslashes on Windows. The sensitive-root
472+
* and allowed-root containment checks compare that spelling byte-exact against
473+
* HOME and the granted roots, so a root respelled with forward slashes on the
474+
* way in stopped matching them, and $HOME was admitted for auto-index and
475+
* watch on Windows. One directory being one root for the job registry is
476+
* folded at that comparison, never by respelling the policy. */
477+
TEST(daemon_session_context_keeps_the_policy_spelling_of_a_root) {
478+
cbm_mcp_server_t *srv = cbm_mcp_server_new(NULL);
479+
ASSERT_NOT_NULL(srv);
480+
cbm_mcp_server_set_background_tasks(srv, false);
481+
482+
ASSERT_TRUE(cbm_mcp_server_set_session_context(srv, "C:\\repos\\cbm", "C:\\repos"));
483+
ASSERT_STR_EQ(cbm_mcp_server_session_root(srv), "C:\\repos\\cbm");
484+
ASSERT_STR_EQ(cbm_mcp_server_allowed_root(srv), "C:\\repos");
485+
486+
cbm_mcp_server_free(srv);
487+
PASS();
488+
}
489+
470490
SUITE(daemon) {
471491
RUN_TEST(daemon_client_ids_are_connection_bound);
472492
RUN_TEST(daemon_shared_job_survives_until_final_subscriber_disconnects);
@@ -480,4 +500,5 @@ SUITE(daemon) {
480500
RUN_TEST(daemon_bridge_rejects_embedded_nul_body);
481501
RUN_TEST(daemon_bridge_rejects_oversized_headers);
482502
RUN_TEST(daemon_sessions_keep_distinct_roots_and_allowed_root_policy);
503+
RUN_TEST(daemon_session_context_keeps_the_policy_spelling_of_a_root);
483504
}

‎tests/test_daemon_application.c‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5382,6 +5382,30 @@ TEST(daemon_application_oversized_reply_is_a_jsonrpc_error_not_a_death) {
53825382
PASS();
53835383
}
53845384

5385+
/* One directory is one root for the job registry. The auto-index job spells
5386+
* repo_path the way the session policy holds it - the platform's native form,
5387+
* backslashes on Windows - while an explicit index_repository request arrives
5388+
* in the handler's forward-slash spelling. Compared byte-exact the two never
5389+
* matched on Windows, and the request was refused as an options conflict
5390+
* instead of joining the job already running for its root. The fold runs on
5391+
* every platform, so this binds wherever the suite runs; every other option
5392+
* stays exact. */
5393+
TEST(daemon_application_index_args_compare_repo_path_separator_equivalently) {
5394+
ASSERT_TRUE(cbm_daemon_application_index_args_equal_for_test(
5395+
"{\"repo_path\":\"C:\\\\repos\\\\cbm\"}", "{\"repo_path\":\"C:/repos/cbm\"}"));
5396+
ASSERT_TRUE(cbm_daemon_application_index_args_equal_for_test(
5397+
"{\"repo_path\":\"C:\\\\repos\\\\cbm\",\"mode\":\"full\"}",
5398+
"{\"mode\":\"full\",\"repo_path\":\"C:/repos/cbm\"}"));
5399+
ASSERT_FALSE(cbm_daemon_application_index_args_equal_for_test(
5400+
"{\"repo_path\":\"C:\\\\repos\\\\cbm\"}", "{\"repo_path\":\"C:/repos/cbm2\"}"));
5401+
ASSERT_FALSE(cbm_daemon_application_index_args_equal_for_test(
5402+
"{\"repo_path\":\"C:\\\\repos\\\\cbm\"}", "{\"repo_path\":\"C:/repos/cbm/sub\"}"));
5403+
ASSERT_FALSE(cbm_daemon_application_index_args_equal_for_test(
5404+
"{\"repo_path\":\"C:\\\\repos\\\\cbm\",\"mode\":\"incremental\"}",
5405+
"{\"repo_path\":\"C:/repos/cbm\"}"));
5406+
PASS();
5407+
}
5408+
53855409
SUITE(daemon_application) {
53865410
RUN_TEST(daemon_application_oversized_reply_is_a_jsonrpc_error_not_a_death);
53875411
RUN_TEST(daemon_application_new_session_does_not_retain_initial_store);
@@ -5400,6 +5424,7 @@ SUITE(daemon_application) {
54005424
RUN_TEST(daemon_application_initialize_coalesces_auto_index_for_full_sessions);
54015425
RUN_TEST(daemon_application_sensitive_root_blocks_auto_index_but_preserves_controls);
54025426
RUN_TEST(daemon_application_sensitive_root_blocks_watch_but_preserves_controls);
5427+
RUN_TEST(daemon_application_index_args_compare_repo_path_separator_equivalently);
54035428
RUN_TEST(daemon_application_auto_index_honors_tracked_file_limit);
54045429
RUN_TEST(daemon_application_auto_index_file_count_handles_literal_metacharacter_path);
54055430
RUN_TEST(daemon_application_auto_index_file_count_supports_non_git_roots);

0 commit comments

Comments
 (0)