Repository navigation
feat(cli): Claim Sentry agent handoffs during setup #2320
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| import { | ||
| claimAgentHandoff, | ||
| type ClaimAgentHandoffResponse, | ||
| SentryApiError, | ||
| } from "@sentry/api"; | ||
| import { parseRetryDelayMs } from "@sentry/api/client"; | ||
| import { vClaimAgentHandoffResponse } from "@sentry/api/valibot"; | ||
| import { looseObject, safeParse, string } from "valibot"; | ||
| import { | ||
| DEFAULT_SENTRY_URL, | ||
| getConfiguredSentryUrl, | ||
| isPersistedDefaultUrlInEffect, | ||
| } from "./constants.js"; | ||
| import { redactCredentialText } from "./credential-redaction.js"; | ||
| import { customFetch, warnIfSaasWithEnvCa } from "./custom-ca.js"; | ||
| import { applyCustomHeaders } from "./custom-headers.js"; | ||
| import { ApiError, HostScopeError, RateLimitError } from "./errors.js"; | ||
| import { | ||
| buildHostRefusalMessage, | ||
| isAutoLoginHostTrusted, | ||
| } from "./login-host-guard.js"; | ||
| import { normalizeHttpOrigin } from "./sentry-urls.js"; | ||
| import { bootstrapAuthentication } from "./setup-code.js"; | ||
|
|
||
| const ErrorResponseSchema = looseObject({ detail: string() }); | ||
|
|
||
| function getExchangeContext(): { | ||
| host: string; | ||
| customHeadersTrusted: boolean; | ||
| } { | ||
| const configured = isPersistedDefaultUrlInEffect() | ||
| ? undefined | ||
| : getConfiguredSentryUrl(); | ||
| const host = normalizeHttpOrigin(configured ?? DEFAULT_SENTRY_URL); | ||
|
|
||
| if (!host) { | ||
| throw new ApiError( | ||
| "Invalid Sentry URL for agent handoff", | ||
| 0, | ||
| "Set SENTRY_HOST or SENTRY_URL to a credential-free HTTP(S) URL.", | ||
| ); | ||
| } | ||
|
evanpurkhiser marked this conversation as resolved.
Resolved
|
||
|
|
||
| if (!isAutoLoginHostTrusted(host)) { | ||
| throw new HostScopeError(buildHostRefusalMessage(host)); | ||
| } | ||
|
|
||
| return { host, customHeadersTrusted: configured !== undefined }; | ||
| } | ||
|
|
||
| function responseDetail(body: unknown): string | undefined { | ||
| if (body instanceof SentryApiError && body.detailMessage) { | ||
| return redactCredentialText(body.detailMessage); | ||
| } | ||
|
|
||
| const errorBody = body instanceof SentryApiError ? body.body : body; | ||
| const parsed = safeParse(ErrorResponseSchema, errorBody); | ||
| return parsed.success | ||
| ? redactCredentialText(parsed.output.detail) | ||
| : undefined; | ||
| } | ||
|
evanpurkhiser marked this conversation as resolved.
Resolved
|
||
|
|
||
| /** Claim a private handoff code from the configured Sentry instance. */ | ||
| export async function exchangeAgentHandoff( | ||
| handoffCode: string, | ||
| ): Promise<ClaimAgentHandoffResponse> { | ||
| const { host, customHeadersTrusted } = getExchangeContext(); | ||
| const headers = new Headers(); | ||
| applyCustomHeaders(headers, host, customHeadersTrusted); | ||
| warnIfSaasWithEnvCa(host); | ||
|
|
||
| const result = await claimAgentHandoff({ | ||
| baseUrl: host, | ||
| body: { handoff_code: handoffCode }, | ||
| fetch: customFetch, | ||
| headers: Object.fromEntries(headers), | ||
| redirect: "error", | ||
| }); | ||
|
|
||
| if ("error" in result) { | ||
| const response = result.response; | ||
| if (!response) { | ||
| throw new ApiError( | ||
| "Sentry agent handoff failed", | ||
| 0, | ||
| "Check your network connection and try again.", | ||
| ); | ||
| } | ||
|
|
||
| const detail = responseDetail(result.error); | ||
| if (response.status === 429) { | ||
| throw new RateLimitError( | ||
| response.url, | ||
| parseRetryDelayMs(response.status, response.headers), | ||
| detail, | ||
| ); | ||
| } | ||
|
|
||
| throw new ApiError( | ||
| "Sentry agent handoff failed", | ||
| response.status, | ||
| detail ?? "Request a new handoff from Sentry and try again.", | ||
| response.url || undefined, | ||
| ); | ||
| } | ||
|
|
||
| const parsed = safeParse(vClaimAgentHandoffResponse, result.data); | ||
| if (!parsed.success) { | ||
| throw new ApiError( | ||
| "Invalid response from the Sentry agent handoff endpoint", | ||
| result.response.status, | ||
| "Request a new handoff from Sentry and try again.", | ||
| result.response.url || undefined, | ||
| ); | ||
| } | ||
|
|
||
| return parsed.output; | ||
| } | ||
|
|
||
| /** Claim a handoff and authenticate its account before returning instructions. */ | ||
| export async function completeAgentHandoff( | ||
| handoffCode: string, | ||
| ): Promise<ClaimAgentHandoffResponse> { | ||
| const handoff = await exchangeAgentHandoff(handoffCode); | ||
|
|
||
| await bootstrapAuthentication({ | ||
| code: handoff.setupCode, | ||
| clientId: handoff.clientId, | ||
| force: true, | ||
| url: handoff.sentryUrl, | ||
| }); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Handoff client ID not persistedMedium Severity Handoff authentication exchanges the setup code with Additional Locations (1)Reviewed by Cursor Bugbot for commit 9a607f5. Configure here.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tracked separately in #2383. We should persist the issuing OAuth client ID with each stored credential and use it for refresh. This can also affect existing logins if |
||
|
|
||
| return handoff; | ||
| } | ||
|
evanpurkhiser marked this conversation as resolved.
Resolved
|
||


Uh oh!
There was an error while loading. Please reload this page.