POST /api/index — body: {"root_path": "/abs/path", "project_name": "..."} */
| 1665 | |
| 1666 | /* POST /api/index — body: {"root_path": "/abs/path", "project_name": "..."} */ |
| 1667 | static void handle_index_start(cbm_http_server_t *server, cbm_http_conn_t *c, |
| 1668 | const cbm_http_req_t *req) { |
| 1669 | if (!server || !server->index_executor) { |
| 1670 | cbm_http_replyf(c, 503, g_cors_json, |
| 1671 | "{\"error\":\"daemon index coordinator unavailable\"}"); |
| 1672 | return; |
| 1673 | } |
| 1674 | if (req->body_len == 0 || req->body_len > 4096) { |
| 1675 | cbm_http_replyf(c, 400, g_cors_json, "{\"error\":\"invalid body\"}"); |
| 1676 | return; |
| 1677 | } |
| 1678 | |
| 1679 | yyjson_doc *doc = yyjson_read(req->body, req->body_len, 0); |
| 1680 | if (!doc) { |
| 1681 | cbm_http_replyf(c, 400, g_cors_json, "{\"error\":\"invalid json\"}"); |
| 1682 | return; |
| 1683 | } |
| 1684 | yyjson_val *root = yyjson_doc_get_root(doc); |
| 1685 | yyjson_val *v_path = yyjson_obj_get(root, "root_path"); |
| 1686 | if (!v_path || !yyjson_is_str(v_path)) { |
| 1687 | yyjson_doc_free(doc); |
| 1688 | cbm_http_replyf(c, 400, g_cors_json, "{\"error\":\"missing root_path\"}"); |
| 1689 | return; |
| 1690 | } |
| 1691 | const char *rpath = yyjson_get_str(v_path); |
| 1692 | yyjson_val *v_project_name = yyjson_obj_get(root, "project_name"); |
| 1693 | const char *project_name = yyjson_is_str(v_project_name) ? yyjson_get_str(v_project_name) : ""; |
| 1694 | |
| 1695 | /* Check path exists */ |
| 1696 | if (!cbm_is_dir(rpath)) { |
| 1697 | yyjson_doc_free(doc); |
| 1698 | cbm_http_replyf(c, 400, g_cors_json, "{\"error\":\"directory not found\"}"); |
| 1699 | return; |
| 1700 | } |
| 1701 | |
| 1702 | /* Same workspace boundary the MCP indexing tool applies, through the same |
| 1703 | * function. This route used to check only that the path was a directory, so |
| 1704 | * it accepted roots the MCP path refused — an operator's boundary held on one |
| 1705 | * entry point and not the other. Canonicalize first: the policy is defined |
| 1706 | * over resolved paths, and a symlink would otherwise launder the verdict. */ |
| 1707 | char canonical_root[INDEX_JOB_ROOT_CAP]; |
| 1708 | char boundary_err[1024]; |
| 1709 | if (!cbm_canonical_path(rpath, canonical_root, sizeof(canonical_root))) { |
| 1710 | yyjson_doc_free(doc); |
| 1711 | cbm_http_replyf(c, 400, g_cors_json, "{\"error\":\"cannot resolve root_path\"}"); |
| 1712 | return; |
| 1713 | } |
| 1714 | if (!cbm_workspace_root_allowed(canonical_root, cbm_workspace_home_dir(), |
| 1715 | cbm_workspace_cache_dir(), getenv("CBM_ALLOWED_ROOT"), |
| 1716 | boundary_err, sizeof(boundary_err))) { |
| 1717 | yyjson_doc_free(doc); |
| 1718 | char escaped[1024]; |
| 1719 | cbm_json_escape(escaped, (int)sizeof(escaped), boundary_err); |
| 1720 | cbm_http_replyf(c, 403, g_cors_json, "{\"error\":\"%s\"}", escaped); |
| 1721 | return; |
| 1722 | } |
| 1723 | /* The job carries exactly the string the checks above ran on. The field |
| 1724 | * has the resolved buffer's capacity, so a resolved path always fits; the |
no test coverage detected