Skip to content

fix(RHOAIENG-52949): scope session listing by X-Ambient-Project header#895

Open
maskarb wants to merge 2 commits intomainfrom
fix/RHOAIENG-52949-session-project-scoping
Open

fix(RHOAIENG-52949): scope session listing by X-Ambient-Project header#895
maskarb wants to merge 2 commits intomainfrom
fix/RHOAIENG-52949-session-project-scoping

Conversation

@maskarb
Copy link
Contributor

@maskarb maskarb commented Mar 12, 2026

Summary

The session list endpoint (GET /api/ambient/v1/sessions) returned all sessions from the database regardless of the X-Ambient-Project header, breaking multi-tenant isolation.

Root Cause

The handler already supported project_id as a query parameter filter, but the SDK and CLI send the project via the X-Ambient-Project header — which was not read by the list handler.

Fix

Read X-Ambient-Project header as fallback when the ?project_id query param is not set. The same validated project_id = 'X' filter is applied to scope results. Query param takes precedence over header if both are set.

Test plan

  • acpctl get sessions with project set to A should only return sessions from project A
  • GET /sessions?project_id=A still works (query param precedence)
  • GET /sessions without header or param returns all sessions (backward compatible)

Jira: RHOAIENG-52949

🤖 Generated with Claude Code

@coderabbitai
Copy link

coderabbitai bot commented Mar 12, 2026

Warning

Rate limit exceeded

@maskarb has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 6 minutes and 19 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: 050481bc-57ce-4178-a126-1f99b85d50f3

📥 Commits

Reviewing files that changed from the base of the PR and between 848995e and 8f245e0.

📒 Files selected for processing (4)
  • components/ambient-api-server/plugins/common/project_scope.go
  • components/ambient-api-server/plugins/common/project_scope_test.go
  • components/ambient-api-server/plugins/projectSettings/handler.go
  • components/ambient-api-server/plugins/sessions/handler.go

Walkthrough

The List handler in the sessions plugin now retrieves project_id from query parameters with fallback to the X-Ambient-Project header. When project_id is present and valid, it applies a project-based filter to the list query, concatenating with existing search conditions.

Changes

Cohort / File(s) Summary
Sessions List Handler
components/ambient-api-server/plugins/sessions/handler.go
Added header fallback (X-Ambient-Project) for project_id derivation with query parameter precedence. Injects project filter into list query when project_id is valid.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~15 minutes

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and specifically describes the main change: adding support for X-Ambient-Project header to scope session listing, which directly addresses the multi-tenant isolation bug.
Description check ✅ Passed The description is well-organized, clearly explains the problem (multi-tenant isolation break), root cause (missing header support), solution (header fallback with query param precedence), and provides concrete test cases.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/RHOAIENG-52949-session-project-scoping
📝 Coding Plan for PR comments
  • Generate coding plan

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
components/ambient-api-server/plugins/sessions/handler.go (1)

201-210: 🛠️ Refactor suggestion | 🟠 Major

Extract project-scope resolution into a shared helper.

The validation and listArgs.Search composition here already exists in components/ambient-api-server/plugins/projectSettings/handler.go:91-107, but with different header behavior. This is the same policy drift that caused the current bug. Please centralize precedence, validation, and filter injection in one helper and reuse it across handlers.

As per coding guidelines, "Focus on major issues impacting performance, readability, maintainability and security. Avoid nitpicks and avoid verbosity."

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@components/ambient-api-server/plugins/sessions/handler.go` around lines 201 -
210, Create a shared helper (e.g., ResolveProjectScopeFilter) that takes the
incoming projectID, the existing search string (or a pointer to
ListArgs.Search), and a flag describing header-vs-param precedence, validates
projectID using safeProjectIDPattern, and returns the composed search filter or
a validation error; then replace the inline validation/assignment in handlers
(references: projectID, safeProjectIDPattern, listArgs.Search) with calls to
this helper so both sessions and projectSettings handlers reuse identical
precedence, validation, and filter-injection logic while preserving each
handler's header-behavior via the flag.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Outside diff comments:
In `@components/ambient-api-server/plugins/sessions/handler.go`:
- Around line 201-210: Create a shared helper (e.g., ResolveProjectScopeFilter)
that takes the incoming projectID, the existing search string (or a pointer to
ListArgs.Search), and a flag describing header-vs-param precedence, validates
projectID using safeProjectIDPattern, and returns the composed search filter or
a validation error; then replace the inline validation/assignment in handlers
(references: projectID, safeProjectIDPattern, listArgs.Search) with calls to
this helper so both sessions and projectSettings handlers reuse identical
precedence, validation, and filter-injection logic while preserving each
handler's header-behavior via the flag.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: d2ec076f-c441-49ea-a4b3-2e7670724d6b

📥 Commits

Reviewing files that changed from the base of the PR and between 538ccbd and 848995e.

📒 Files selected for processing (1)
  • components/ambient-api-server/plugins/sessions/handler.go

The session and project-settings list endpoints returned all records
regardless of the X-Ambient-Project header, breaking multi-tenant
isolation. Both handlers already supported project_id as a query
parameter but did not read the header.

Extract shared ApplyProjectScope helper in plugins/common that reads
the project from the query param (precedence) or X-Ambient-Project
header, validates it, and injects the filter into ListArguments.Search.
Both handlers now use this shared helper.

Jira: RHOAIENG-52949

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@maskarb maskarb force-pushed the fix/RHOAIENG-52949-session-project-scoping branch from 848995e to b04e4b7 Compare March 12, 2026 16:32
19 test cases covering:
- Header-only and query-param-only filtering
- Query param precedence over header
- No project returns no filter (backward compatible)
- Combines with existing search expressions
- Rejects SQL injection payloads via both header and query param
- Accepts valid project ID patterns

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
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