ECOPROJECT-4721 | fix: Validate JWT source_id in agent handlers#1213
Conversation
|
Warning Review limit reached
More reviews will be available in 38 minutes and 38 seconds. Learn how PR review limits work. Your organization has run out of usage credits. Purchase more in the billing tab. ⌛ How to resolve this issue?After more reviews become available, a review can be triggered using the 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 include higher PR review limits than trial, open-source, and free plans. In all cases, reviews become available again over time. During sustained high-volume PR review activity, CodeRabbit may temporarily slow when the next review becomes available. Please see our Fair Usage Limits Policy for further information. ℹ️ Review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: ASSERTIVE Plan: Pro Run ID: 📒 Files selected for processing (6)
📝 WalkthroughWalkthroughAgent authentication is refactored from conditional middleware registration into a feature-flagged factory pattern; a new "none" authenticator handles disabled scenarios. Handler endpoints enforce source ownership via authorization checks that return 403 on mismatch. Tests migrate to AgentJWT contexts and validate both successful operations and authorization rejection. ChangesAgent Authentication and Authorization
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
c87fba0 to
29e0ec6
Compare
|
/hold |
682f244 to
7d4c8e0
Compare
|
/hold remove |
0536d2c to
f5f099b
Compare
There was a problem hiding this comment.
Actionable comments posted: 4
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@internal/auth/agent_authenticator_test.go`:
- Around line 163-185: Update the test for NewNoneAgentAuthenticator's
Authenticator middleware to assert that the middleware sets an AgentJWT with the
expected SourceID in the request context and that the request body remains
readable for downstream handlers: modify the inner handler returned by serveHTTP
(used with noneAuthenticator.Authenticator) to retrieve the AgentJWT from
r.Context(), verify its SourceID equals "test-source-123", then attempt to read
the request body (e.g., ioutil.ReadAll or io.ReadAll) and validate the body
content matches the original JSON; keep the test's HTTP setup and assertions but
replace the simple 200-only handler with these context and body checks.
In `@internal/auth/none_agent_authenticator.go`:
- Around line 27-33: Validate that req.SourceID is non-empty before constructing
the AgentJWT: in the middleware/function that builds the AgentJWT (the block
creating AgentJWT with ExpireAt/IssueAt/Issuer/OrgID/SourceID), check
req.SourceID (or req.SourceId) and return a clear error response (e.g., a 400
Bad Request or a descriptive error) when it's empty or missing so you don't
produce an AgentJWT with an empty SourceID; include a concise error message like
"missing sourceId" and update any callers to handle that error path.
- Around line 22-25: The request body is consumed by json.NewDecoder when
decoding req, breaking downstream handlers; fix Authenticate/ServeHTTP in
none_agent_authenticator.go by reading and buffering the entire body (e.g.,
ioutil.ReadAll or io.ReadAll into bodyBytes), decode from a bytes.Reader or a
copy of those bytes to populate req, then restore r.Body by assigning r.Body =
io.NopCloser(bytes.NewReader(bodyBytes)) before calling next.ServeHTTP so
downstream handlers (e.g., UpdateSourceInventory, UpdateAgentStatus) can re-read
the payload; preserve the existing error handling (http.Error on decode failure)
and only restore/forward the body when decoding succeeds.
In `@internal/handlers/v1alpha1/agent.go`:
- Around line 85-90: The handler dereferences request.Body (and uses
request.Body.SourceId) before validating it, which can panic on empty requests;
add a nil-check and validation at the start of UpdateAgentStatus (before calling
auth.MustHaveAgent or any request.Body.SourceId access) and return a
400/UpdateAgentStatus400JSONResponse with a clear message when request.Body is
nil or missing SourceId; then proceed to compare agentJWT.SourceID to
request.Body.SourceId and return the existing UpdateAgentStatus403JSONResponse
only after the body is validated.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: ASSERTIVE
Plan: Pro
Run ID: b5731741-f593-4632-8f88-5c2314c227aa
📒 Files selected for processing (6)
internal/api_server/agentserver/server.gointernal/auth/agent_authenticator.gointernal/auth/agent_authenticator_test.gointernal/auth/none_agent_authenticator.gointernal/handlers/v1alpha1/agent.gointernal/handlers/v1alpha1/agent_test.go
|
/hold cancel |
Signed-off-by: Aviel Segev <asegev@redhat.com>
f5f099b to
e2d5955
Compare
|
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: nirarg The full list of commands accepted by this bot can be found here. The pull request process is described here DetailsNeeds approval from an approver in each of these files:
Approvers can indicate their approval by writing |
|
/retest |
|
/lgtm |
Summary
Fixes critical security vulnerability where agent API handlers ignored JWT source_id claim, allowing cross-tenant access.
Changes
UpdateSourceInventoryandUpdateAgentStatushandlerssource_idmatches the target source and return 403 if notAgentJWTin context and added new authorization testsSecurity Impact
Prevents:
🤖 Generated with Claude Code
Summary by CodeRabbit
New Features
Security & Authorization