Skip to content

Add server-side proxy for Scalar API client to bypass CORS#4050

Merged
nolannbiron merged 4 commits intomainfrom
nolann/rnd-9723-try-it-api-testing-popup-blocked-by-gitbook-enforced-cors
Feb 27, 2026
Merged

Add server-side proxy for Scalar API client to bypass CORS#4050
nolannbiron merged 4 commits intomainfrom
nolann/rnd-9723-try-it-api-testing-popup-blocked-by-gitbook-enforced-cors

Conversation

@nolannbiron
Copy link
Copy Markdown
Member

Route API requests from Scalar's "Try It" button through a server-side proxy at /~scalar/proxy, avoiding browser CORS restrictions.

  • Add x-enable-proxy spec-level extension to opt-in per OpenAPI spec
  • Extract and pass x-enable-proxy through the OpenAPI data pipeline
  • Configure Scalar's proxyUrl when x-enable-proxy is true
  • Implement proxy route handler with SSRF protection (private IP blocking, DNS resolution checks, redirect validation)
  • Strip upstream CORS headers and apply permissive ones
  • Handle X-Scalar-Cookie and X-Scalar-User-Agent forwarding

Mainly inspired by: https://github.com/scalar/scalar/tree/main/projects/proxy-scalar-com

@linear
Copy link
Copy Markdown

linear Bot commented Feb 25, 2026

@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Feb 25, 2026

🦋 Changeset detected

Latest commit: 4f8503f

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 3 packages
Name Type
@gitbook/openapi-parser Patch
@gitbook/react-openapi Patch
gitbook Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@argos-ci
Copy link
Copy Markdown

argos-ci Bot commented Feb 25, 2026

The latest updates on your projects. Learn more about Argos notifications ↗︎

Build Status Details Updated (UTC)
customers-v2-cloudflare (Inspect) 👍 Changes approved 1 changed Feb 25, 2026, 12:14 PM
customers-v2-vercel (Inspect) ✅ No changes detected - Feb 25, 2026, 12:12 PM
v2-cloudflare (Inspect) ✅ No changes detected - Feb 25, 2026, 12:18 PM
v2-vercel (Inspect) ✅ No changes detected - Feb 25, 2026, 12:17 PM

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adds a server-side proxy for the Scalar API client to bypass CORS restrictions when testing API endpoints. The proxy is opt-in via the x-enable-proxy extension in OpenAPI specs and includes SSRF protection measures.

Changes:

  • Added x-enable-proxy custom extension to OpenAPI spec-level properties with type validation
  • Implemented proxy route handler at /~scalar/proxy with SSRF protection (private IP blocking, DNS resolution checks, redirect validation)
  • Integrated proxy URL configuration into Scalar API button component based on x-enable-proxy flag

Reviewed changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 14 comments.

Show a summary per file
File Description
packages/openapi-parser/src/types.ts Added x-enable-proxy boolean property to OpenAPICustomSpecProperties interface
packages/react-openapi/src/resolveOpenAPIOperation.ts Extracted x-enable-proxy from schema with boolean type validation
packages/react-openapi/src/resolveOpenAPIOperation.test.ts Added comprehensive test coverage for x-enable-proxy extraction logic
packages/react-openapi/src/ScalarApiButton.tsx Added withProxy prop and configured Scalar's proxyUrl when enabled
packages/react-openapi/src/OpenAPICodeSample.tsx Passed x-enable-proxy value to ScalarApiButton component
packages/gitbook/src/routes/openapi-proxy.ts Implemented proxy handler with SSRF protection, header forwarding, and redirect validation
packages/gitbook/src/routes/openapi-proxy.test.ts Added extensive test coverage for proxy functionality and security checks
packages/gitbook/src/app/~scalar/proxy/route.ts Created Next.js route handlers for all HTTP methods
.changeset/bright-streets-hammer.md Added changeset documenting the new feature

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread packages/gitbook/src/routes/openapi-proxy.test.ts
Comment thread packages/gitbook/src/routes/openapi-proxy.ts
Comment thread packages/gitbook/src/routes/openapi-proxy.ts Outdated
Comment thread packages/gitbook/src/routes/openapi-proxy.ts
Comment thread packages/gitbook/src/routes/openapi-proxy.ts
Comment thread packages/gitbook/src/routes/openapi-proxy.test.ts
Comment thread packages/gitbook/src/routes/openapi-proxy.ts
Comment thread packages/gitbook/src/routes/openapi-proxy.ts Outdated
Comment thread packages/gitbook/src/routes/openapi-proxy.ts Outdated
Comment thread packages/gitbook/src/routes/openapi-proxy.ts
Route API requests from Scalar's "Try It" button through a server-side
proxy at /~scalar/proxy, avoiding browser CORS restrictions.

- Add x-enable-proxy spec-level extension to opt-in per OpenAPI spec
- Extract and pass x-enable-proxy through the OpenAPI data pipeline
- Configure Scalar's proxyUrl when x-enable-proxy is true
- Implement proxy route handler with SSRF protection (private IP
  blocking, DNS resolution checks, redirect validation)
- Strip upstream CORS headers and apply permissive ones
- Handle X-Scalar-Cookie and X-Scalar-User-Agent forwarding
Copy link
Copy Markdown
Contributor

@gregberge gregberge left a comment

Choose a reason for hiding this comment

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

It looks good to me.

@nolannbiron nolannbiron merged commit e074283 into main Feb 27, 2026
17 checks passed
@nolannbiron nolannbiron deleted the nolann/rnd-9723-try-it-api-testing-popup-blocked-by-gitbook-enforced-cors branch February 27, 2026 11:18
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.

3 participants