Skip to content

fix(middleware): handle invalid request-id header values gracefully#61

Closed
echobt wants to merge 2 commits intodocs/standardize-timeout-documentationfrom
fix/middleware-headervalue-unwrap
Closed

fix(middleware): handle invalid request-id header values gracefully#61
echobt wants to merge 2 commits intodocs/standardize-timeout-documentationfrom
fix/middleware-headervalue-unwrap

Conversation

@echobt
Copy link
Contributor

@echobt echobt commented Feb 4, 2026

Summary

This PR addresses an unsafe .unwrap() call in the request ID middleware that could cause a panic if the request ID contains invalid HTTP header characters.

Problem

In src/cortex-app-server/src/middleware.rs, the request_id_middleware function used .unwrap() when converting the request ID string to a HeaderValue:

HeaderValue::from_str(&request_id).unwrap()

If a client provided a request ID header containing invalid characters (control characters, non-ASCII bytes, etc.), this would cause a panic and crash the request handler.

Solution

Replace the unsafe .unwrap() with .unwrap_or_else() that provides a fallback value:

HeaderValue::from_str(&request_id)
    .unwrap_or_else(|_| HeaderValue::from_static("invalid-request-id"))

This ensures that even when clients provide malformed request IDs, the server continues to function and returns a response with a safe fallback header value.

Testing

  • Verified compilation with cargo check -p cortex-app-server

echobt added a commit that referenced this pull request Feb 4, 2026
This PR consolidates the following error handling fixes:
- #48: Handle semaphore and init failures gracefully in async_utils
- #54: Improve error handling in session storage operations (includes TOCTOU race fixes)
- #55: Add validation for threshold, ratio, and token count fields
- #56: Replace unwrap with proper error handling for client access
- #57: Use unwrap_or_default for SystemTime operations
- #61: Handle invalid request-id header values gracefully
- #65: Improve error handling for timestamp and JSON operations in streaming

Key changes:
- Added graceful handling for semaphore and init failures
- Bound ToolResponseStore size and cleanup consumed entries
- Eliminated TOCTOU races in MCP server and plugin registry
- Replaced unwrap() with proper error handling throughout
- Added validation for config fields
- Improved error propagation in middleware
@echobt
Copy link
Contributor Author

echobt commented Feb 4, 2026

Consolidated into #73 - fix: consolidated error handling improvements

@echobt echobt closed this Feb 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant