Skip to content

fix(auth): use unwrap_or_default for SystemTime operations#57

Closed
echobt wants to merge 1 commit intodocs/standardize-timeout-documentationfrom
fix/auth-systemtime-unwrap
Closed

fix(auth): use unwrap_or_default for SystemTime operations#57
echobt wants to merge 1 commit intodocs/standardize-timeout-documentationfrom
fix/auth-systemtime-unwrap

Conversation

@echobt
Copy link
Contributor

@echobt echobt commented Feb 4, 2026

Summary

This PR replaces .unwrap() calls on SystemTime::duration_since(UNIX_EPOCH) with .unwrap_or_default() in the authentication module for defensive programming.

Problem

The current implementation uses .unwrap() on SystemTime::duration_since(UNIX_EPOCH) in three locations within cortex-app-server/src/auth.rs:

  • Claims::new() (line 48)
  • Claims::is_expired() (line 78)
  • AuthService::cleanup_revoked_tokens() (line 190)

While extremely unlikely in practice, duration_since(UNIX_EPOCH) can return an Err if the system clock is set to a time before the Unix epoch (January 1, 1970). In such a case, the current code would panic.

Solution

Replace all three instances of:

.duration_since(UNIX_EPOCH)
.unwrap()

with:

.duration_since(UNIX_EPOCH)
.unwrap_or_default()

This provides a safe fallback to Duration::default() (zero duration) in the unlikely event of a misconfigured system clock, preventing potential panics while maintaining normal operation for correctly configured systems.

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