| 2972 | } |
| 2973 | |
| 2974 | static cbm_daemon_runtime_application_status_t application_set_context( |
| 2975 | cbm_daemon_application_session_t *session, const uint8_t *request, uint32_t request_length) { |
| 2976 | if (session->context_set || request_length < APPLICATION_CONTEXT_HEADER_SIZE) { |
| 2977 | return CBM_DAEMON_RUNTIME_APPLICATION_REJECTED; |
| 2978 | } |
| 2979 | uint32_t root_length = application_get_u32(request + 1); |
| 2980 | bool allowed_present = request[5] == 1; |
| 2981 | uint32_t allowed_length = application_get_u32(request + 6); |
| 2982 | uint8_t profile_value = request[10]; |
| 2983 | uint32_t event_length = application_get_u32(request + 11); |
| 2984 | uint32_t dialect_length = application_get_u32(request + 15); |
| 2985 | uint64_t expected = (uint64_t)APPLICATION_CONTEXT_HEADER_SIZE + root_length + allowed_length + |
| 2986 | event_length + dialect_length; |
| 2987 | if (request[5] > 1 || root_length == 0 || expected != request_length || |
| 2988 | (!allowed_present && allowed_length != 0) || |
| 2989 | profile_value > (uint8_t)CBM_MCP_TOOL_PROFILE_SCOUT || |
| 2990 | (profile_value != (uint8_t)CBM_MCP_TOOL_PROFILE_ALL && |
| 2991 | (event_length != 0 || dialect_length != 0))) { |
| 2992 | return CBM_DAEMON_RUNTIME_APPLICATION_REJECTED; |
| 2993 | } |
| 2994 | cbm_mcp_tool_profile_t tool_profile = (cbm_mcp_tool_profile_t)profile_value; |
| 2995 | const uint8_t *payload = request + APPLICATION_CONTEXT_HEADER_SIZE; |
| 2996 | char *root = application_text_copy(request + APPLICATION_CONTEXT_HEADER_SIZE, root_length); |
| 2997 | char *allowed = |
| 2998 | allowed_present ? application_text_copy(payload + root_length, allowed_length) : NULL; |
| 2999 | char *hook_event = |
| 3000 | event_length ? application_text_copy(payload + root_length + allowed_length, event_length) |
| 3001 | : NULL; |
| 3002 | char *hook_dialect = |
| 3003 | dialect_length ? application_text_copy( |
| 3004 | payload + root_length + allowed_length + event_length, dialect_length) |
| 3005 | : NULL; |
| 3006 | if (!root || (allowed_present && !allowed) || (event_length && !hook_event) || |
| 3007 | (dialect_length && !hook_dialect) || |
| 3008 | !cbm_hook_augment_invocation_supported(hook_event, hook_dialect)) { |
| 3009 | free(root); |
| 3010 | free(allowed); |
| 3011 | free(hook_event); |
| 3012 | free(hook_dialect); |
| 3013 | return CBM_DAEMON_RUNTIME_APPLICATION_REJECTED; |
| 3014 | } |
| 3015 | char canonical_root[APPLICATION_PATH_CAP] = {0}; |
| 3016 | char canonical_allowed[APPLICATION_PATH_CAP] = {0}; |
| 3017 | bool canonical = cbm_canonical_path(root, canonical_root, sizeof(canonical_root)); |
| 3018 | if (canonical && allowed_present) { |
| 3019 | canonical = cbm_canonical_path(allowed, canonical_allowed, sizeof(canonical_allowed)); |
| 3020 | } |
| 3021 | canonical = canonical && application_canonical_directory_exists(canonical_root); |
| 3022 | bool set = |
| 3023 | canonical && cbm_mcp_server_set_session_context(session->mcp, canonical_root, |
| 3024 | allowed_present ? canonical_allowed : NULL); |
| 3025 | free(root); |
| 3026 | free(allowed); |
| 3027 | if (!set) { |
| 3028 | free(hook_event); |
| 3029 | free(hook_dialect); |
| 3030 | return CBM_DAEMON_RUNTIME_APPLICATION_REJECTED; |
| 3031 | } |
no test coverage detected