Skip to content

Fix session reuse route-binding validation to prevent cross-target session confusion#312

Closed
KinshukSS2 wants to merge 3 commits intovolcano-sh:mainfrom
KinshukSS2:fix/session-route-binding
Closed

Fix session reuse route-binding validation to prevent cross-target session confusion#312
KinshukSS2 wants to merge 3 commits intovolcano-sh:mainfrom
KinshukSS2:fix/session-route-binding

Conversation

@KinshukSS2
Copy link
Copy Markdown

Fix session reuse route-binding validation

Summary

Add strict validation between route target identity and reused session identity during request forwarding.

Currently, requests using x-agentcube-session-id only validate the session ID and do not verify that the recovered sandbox belongs to the requested (namespace, name, kind) route target. This can lead to cross-target session reuse and incorrect runtime dispatch.

Changes

  • Validate reused session target against requested:
    • namespace
    • runtime name
    • runtime kind
  • Reject mismatched requests with:
    • 409 Conflict
    • SESSION_TARGET_MISMATCH
  • Add logging/metrics for mismatch attempts
  • Preserve existing behavior for new session creation and valid sticky-session reuse

Testing

  • Unit tests for matching and mismatched route/session combinations
  • Integration test for cross-runtime session replay rejection
  • Regression test for normal session reuse flow

Copilot AI review requested due to automatic review settings May 10, 2026 00:37
@volcano-sh-bot
Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please assign yaozengzeng for approval. For more information see the Kubernetes Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@volcano-sh-bot
Copy link
Copy Markdown
Contributor

Welcome @KinshukSS2! It looks like this is your first PR to volcano-sh/agentcube 🎉

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces session target validation to prevent cross-target session confusion by implementing a sessionTargetMatches check and a corresponding NewSessionTargetMismatchError. Feedback highlights that the current validation logic is "fail-open" when metadata fields are empty, which may bypass checks for new sessions. Additionally, comparing the ephemeral sandbox.Name against the requested resource name could lead to incorrect rejections. It is also recommended to include the structured logging and metrics mentioned in the PR description to improve observability.

Comment thread pkg/router/session_manager.go Outdated
Comment on lines +119 to +133
func sessionTargetMatches(sandbox *types.SandboxInfo, namespace, name, kind string) bool {
if sandbox == nil {
return false
}
if sandbox.Kind != "" && sandbox.Kind != kind {
return false
}
if sandbox.Name != "" && sandbox.Name != name {
return false
}
if sandbox.SandboxNamespace != "" && sandbox.SandboxNamespace != namespace {
return false
}
return true
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The validation logic in sessionTargetMatches has two significant issues that may undermine the intent of this pull request:

  1. Ineffective Validation: The function uses a "fail-open" approach by returning true if metadata fields like Kind or SandboxNamespace are empty in the stored SandboxInfo. However, the current implementation of createSandbox (lines 207-212) does not populate these fields when creating new sessions. Consequently, this validation will be bypassed for all newly created sessions, failing to prevent cross-target session confusion for new workloads.
  2. Potential Session Reuse Breakage: Comparing sandbox.Name against the requested name may cause incorrect rejections. sandbox.Name is populated from res.SandboxName (the actual sandbox name), which often differs from the resource name used in the route target (e.g., if the workload manager generates unique names or uses suffixes).

To address these, ensure createSandbox stores the requested target metadata and that the validation logic correctly identifies the route target identity rather than the ephemeral sandbox name.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice Catch, will make the necessary changes

Comment thread pkg/router/session_manager.go Outdated
@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented May 10, 2026

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 48.12%. Comparing base (524e55e) to head (b13d75d).
⚠️ Report is 22 commits behind head on main.
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #312      +/-   ##
==========================================
+ Coverage   47.57%   48.12%   +0.55%     
==========================================
  Files          30       30              
  Lines        2819     2874      +55     
==========================================
+ Hits         1341     1383      +42     
- Misses       1338     1343       +5     
- Partials      140      148       +8     
Flag Coverage Δ
unittests 48.12% <100.00%> (+0.55%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

- Fix fail-open vulnerability: store requested metadata (namespace, name, kind)
  in SandboxInfo instead of ephemeral sandbox name for proper session reuse validation
- Fix ephemeral name mismatch: sessionTargetMatches now compares against stored
  requested metadata instead of generated sandbox names
- Add structured logging: log session target mismatches at V(2) level for
  better observability
- Add comprehensive table-driven tests: cover perfect matches, mismatches,
  nil sandboxes, empty fields (wildcard matching), and edge cases
@KinshukSS2 KinshukSS2 force-pushed the fix/session-route-binding branch from f97dec0 to b13d75d Compare May 10, 2026 09:38
@KinshukSS2 KinshukSS2 requested a review from Copilot May 10, 2026 09:57
@KinshukSS2 KinshukSS2 closed this May 10, 2026
@KinshukSS2 KinshukSS2 deleted the fix/session-route-binding branch May 10, 2026 10:01
@KinshukSS2 KinshukSS2 review requested due to automatic review settings May 10, 2026 10:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants