| 1386 | } |
| 1387 | |
| 1388 | static bool ha_normalized_tool_path(yyjson_val *root, yyjson_val *tool_input, char *path, |
| 1389 | size_t path_size) { |
| 1390 | if (!root || !tool_input || !yyjson_is_obj(tool_input) || !path || path_size == 0U) { |
| 1391 | return false; |
| 1392 | } |
| 1393 | const char *source = ha_obj_str(tool_input, "file_path"); |
| 1394 | if (!source) { |
| 1395 | source = ha_obj_str(tool_input, "path"); |
| 1396 | } |
| 1397 | if (!source || !source[0] || strlen(source) >= path_size) { |
| 1398 | return false; |
| 1399 | } |
| 1400 | snprintf(path, path_size, "%s", source); |
| 1401 | for (char *cursor = path; *cursor; cursor++) { |
| 1402 | if (*cursor == '\\') { |
| 1403 | *cursor = '/'; |
| 1404 | } |
| 1405 | } |
| 1406 | if (cbm_hook_path_is_abs(path)) { |
| 1407 | return true; |
| 1408 | } |
| 1409 | |
| 1410 | /* An explicitly supplied relative cwd is untrusted and ambiguous. Do not |
| 1411 | * silently reinterpret it against the hook process cwd. */ |
| 1412 | const char *payload_cwd = ha_obj_str(root, "cwd"); |
| 1413 | if (payload_cwd) { |
| 1414 | char supplied[4096]; |
| 1415 | if (strlen(payload_cwd) >= sizeof(supplied)) { |
| 1416 | return false; |
| 1417 | } |
| 1418 | snprintf(supplied, sizeof(supplied), "%s", payload_cwd); |
| 1419 | for (char *cursor = supplied; *cursor; cursor++) { |
| 1420 | if (*cursor == '\\') { |
| 1421 | *cursor = '/'; |
| 1422 | } |
| 1423 | } |
| 1424 | if (!cbm_hook_path_is_abs(supplied)) { |
| 1425 | return false; |
| 1426 | } |
| 1427 | } |
| 1428 | |
| 1429 | char cwd_buffer[4096]; |
| 1430 | const char *cwd = ha_normalized_cwd(root, cwd_buffer, sizeof(cwd_buffer)); |
| 1431 | if (!cwd) { |
| 1432 | return false; |
| 1433 | } |
| 1434 | char relative[4096]; |
| 1435 | snprintf(relative, sizeof(relative), "%s", path); |
| 1436 | int written = snprintf(path, path_size, "%s/%s", cwd, relative); |
| 1437 | return written > 0 && (size_t)written < path_size && cbm_hook_path_is_abs(path); |
| 1438 | } |
| 1439 | |
| 1440 | static const char *ha_active_tier(yyjson_val *root, const char *event) { |
| 1441 | if (!event || strcmp(event, "SubagentStart") != 0) { |
no test coverage detected