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

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: 3d212819-2619-4804-9b52-efbeb7fa6896

📥 Commits

Reviewing files that changed from the base of the PR and between 538ccbd 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

Centralized project scoping: a new ApplyProjectScope helper reads project_id from query or X-Ambient-Project, validates it, and injects a project_id filter into list query args. Handlers (sessions, projectSettings) delegate project-scoping to this helper and propagate any validation errors.

Changes

Cohort / File(s) Summary
Project scope helper & tests
components/ambient-api-server/plugins/common/project_scope.go, components/ambient-api-server/plugins/common/project_scope_test.go
Add ApplyProjectScope to extract/validate project_id from query or X-Ambient-Project, injects a project_id = '<id>' filter into services.ListArguments.Search. Includes unit tests covering header/query precedence, combination with existing search, valid/invalid inputs, and injection attempts.
Sessions List Handler
components/ambient-api-server/plugins/sessions/handler.go
Replace inline project_id parsing/regex logic with call to common.ApplyProjectScope(r, listArgs); remove direct regex/imports and delegate validation/error handling.
ProjectSettings List Handler
components/ambient-api-server/plugins/projectSettings/handler.go
Replace local project_id validation and search injection with common.ApplyProjectScope(r, listArgs) call and propagate errors; remove unused imports/regex.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 10.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title directly addresses the main change: adding X-Ambient-Project header support to scope session listing, matching the primary objective of fixing multi-tenant isolation.
Description check ✅ Passed The description clearly explains the problem, root cause, and solution, all directly related to the changeset of centralizing project scope logic and reading the X-Ambient-Project header.

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

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 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
  • Generate coding plan for human review comments

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>
@ambient-code ambient-code bot added this to the Review Queue milestone Mar 13, 2026
@Gkrumbach07
Copy link
Contributor

@coderabbitai full review

@coderabbitai
Copy link

coderabbitai bot commented Mar 13, 2026

✅ Actions performed

Full review triggered.

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.

2 participants