fix: send auth and x-user when validating change reason#993
fix: send auth and x-user when validating change reason#993
Conversation
Signed-off-by: datron <Datron@users.noreply.github.com>
Changed Files |
WalkthroughThe PR updates change-reason validation to include authenticated user context. A helper function is renamed and refactored to accept a ChangesUser Context in Change-Reason Validation
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 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 |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
crates/experimentation_platform/src/api/experiments/helpers.rs (1)
806-820: ⚡ Quick winCheck HTTP status before deserializing the response body.
res.status()is never inspected. If CAC returns a 5xx (e.g., the function endpoint is temporarily unavailable), the code tries to parse the error body asFunctionExecutionResponse, fails, and surfacesbad_argument!("Change reason validation failed.")to the caller — misleading them into thinking their change reason text is invalid when the real issue is a server-side problem. The correct behavior for a non-2xx response isunexpected_error!.♻️ Proposed fix
match response { - Ok(res) => match res.json::<FunctionExecutionResponse>().await { - Ok(response) => { - log::info!("Change reason validation function response: {:?}", response); - Ok(()) - } - Err(err) => { - log::error!( - "Change reason validation function returned false for change reason: {:?} with error: {:?}", - change_reason, - err - ); - Err(bad_argument!("Change reason validation failed.")) - } - }, + Ok(res) => { + let status = res.status(); + if !status.is_success() { + let body = res.text().await.unwrap_or_default(); + log::error!( + "Change reason validation endpoint returned {} for change reason: {:?}, body: {}", + status, + change_reason, + body + ); + return Err(unexpected_error!( + "Change reason validation endpoint returned unexpected status: {}", + status + )); + } + match res.json::<FunctionExecutionResponse>().await { + Ok(response) => { + log::info!("Change reason validation function response: {:?}", response); + Ok(()) + } + Err(err) => { + log::error!( + "Change reason validation function returned false for change reason: {:?} with error: {:?}", + change_reason, + err + ); + Err(bad_argument!("Change reason validation failed.")) + } + } + }, Err(error) => {🤖 Prompt for 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. In `@crates/experimentation_platform/src/api/experiments/helpers.rs` around lines 806 - 820, The current match arm deserializes res.json::<FunctionExecutionResponse>().await without checking res.status(), which makes server errors (5xx/4xx) get misreported as bad_argument! for the change_reason; update the logic in the match handling of response to first inspect res.status(). If res.status().is_success() then parse res.json::<FunctionExecutionResponse>().await and proceed as now; otherwise log the status and body and return unexpected_error! (instead of bad_argument!) to surface server-side failures related to the FunctionExecutionResponse call for change_reason validation.
🤖 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.
Nitpick comments:
In `@crates/experimentation_platform/src/api/experiments/helpers.rs`:
- Around line 806-820: The current match arm deserializes
res.json::<FunctionExecutionResponse>().await without checking res.status(),
which makes server errors (5xx/4xx) get misreported as bad_argument! for the
change_reason; update the logic in the match handling of response to first
inspect res.status(). If res.status().is_success() then parse
res.json::<FunctionExecutionResponse>().await and proceed as now; otherwise log
the status and body and return unexpected_error! (instead of bad_argument!) to
surface server-side failures related to the FunctionExecutionResponse call for
change_reason validation.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: bf6d8c1b-c1ce-493d-a017-dbbc88457e81
📒 Files selected for processing (3)
crates/experimentation_platform/src/api/experiment_groups/handlers.rscrates/experimentation_platform/src/api/experiments/handlers.rscrates/experimentation_platform/src/api/experiments/helpers.rs
There was a problem hiding this comment.
Pull request overview
This PR fixes change-reason validation requests getting redirected to the login flow by ensuring the internal function test call includes both the x-user header and an internal Authorization token.
Changes:
- Renamed the change-reason validation helper and extended its signature to accept
&User. - Added
x-userandAuthorization: Internal <token>headers to the change-reason validation function HTTP request. - Updated experiment and experiment-group handlers to pass the authenticated
userthrough to the validation helper.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| crates/experimentation_platform/src/api/experiments/helpers.rs | Adds x-user + internal auth header to the change-reason validation function call and updates helper signature/name. |
| crates/experimentation_platform/src/api/experiments/handlers.rs | Updates experiment endpoints to call the renamed helper and pass &user. |
| crates/experimentation_platform/src/api/experiment_groups/handlers.rs | Updates experiment-group endpoints to call the renamed helper and pass &user. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| header::AUTHORIZATION, | ||
| format!("Internal {}", state.superposition_token), | ||
| ) | ||
| .json(&payload) | ||
| .send() | ||
| .await; |
Problem
Change reason function is being redirected to login page because of missing x-user and auth token
Solution
Add x-user and auth token
Summary by CodeRabbit