Skip to content

Conversation

@BYK
Copy link
Member

@BYK BYK commented Nov 20, 2025

Fixes #1137.

Restrict CORS Origins for Sidecar Security

This PR implements a security enhancement by restricting Cross-Origin Resource Sharing (CORS) for the Spotlight Sidecar to a predefined set of trusted origins.

Why this change?
This change prevents unauthorized domains from making requests to the Spotlight Sidecar, significantly improving its security posture without hindering local development workflows.

Key Changes:

  • New isAllowedOrigin Utility: A new function (packages/spotlight/src/server/utils/cors.ts) was created to validate incoming origins. It permits localhost, 127.0.0.1, and [::1] (IPv6 localhost) with any port or protocol. It also allows https://spotlightjs.com and its HTTPS subdomains (https://*.spotlightjs.com), specifically requiring HTTPS and default ports (443 or unspecified). All other origins are rejected.
  • CORS Middleware Configuration: The Hono CORS middleware in packages/spotlight/src/server/main.ts now utilizes the isAllowedOrigin function to dynamically validate request origins, replacing the previously permissive configuration.
  • Comprehensive Test Coverage: Extensive unit tests for isAllowedOrigin and integration tests for actual CORS header behavior have been added (packages/spotlight/src/server/routes/__tests__/server.test.ts) to ensure correct and secure operation across all scenarios.

Open in Cursor Open in Web

@BYK BYK deployed to Preview November 20, 2025 10:29 — with GitHub Actions Active
@vercel
Copy link

vercel bot commented Nov 20, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
spotlightjs Ready Ready Preview Comment Nov 20, 2025 11:49am

@cursor cursor bot deployed to Preview November 20, 2025 11:49 Active
@BYK BYK marked this pull request as ready for review November 20, 2025 11:51
@BYK BYK requested a review from MathurAditya724 November 20, 2025 11:51
@BYK BYK enabled auto-merge (squash) November 20, 2025 11:51
const hostname = url.hostname.toLowerCase();

// Allow localhost with any port and protocol (http or https)
if (hostname === "localhost" || hostname === "127.0.0.1" || hostname === "[::1]") {
Copy link

Choose a reason for hiding this comment

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

Bug: The isAllowedOrigin function incorrectly checks for hostname === "[::1]" when the URL API strips brackets, causing IPv6 localhost to be rejected.
Severity: CRITICAL | Confidence: High

🔍 Detailed Analysis

The isAllowedOrigin function incorrectly compares the hostname extracted from a URL. The JavaScript URL API parses IPv6 addresses like [::1] to ::1 (without brackets) for the hostname property. However, the if condition at packages/spotlight/src/server/utils/cors.ts:22 checks for hostname === "[::1]". This mismatch causes the comparison "::1" === "[::1]" to evaluate to false, preventing IPv6 localhost addresses from being correctly identified as allowed origins. This leads to all requests from IPv6 localhost addresses ([::1]) being incorrectly rejected by the CORS middleware, breaking local development for IPv6 users.

💡 Suggested Fix

Modify packages/spotlight/src/server/utils/cors.ts:22 to change the comparison from hostname === "[::1]" to hostname === "::1" to correctly identify IPv6 localhost addresses.

🤖 Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent.
Verify if this is a real issue. If it is, propose a fix; if not, explain why it's not
valid.

Location: packages/spotlight/src/server/utils/cors.ts#L22

Potential issue: The `isAllowedOrigin` function incorrectly compares the `hostname`
extracted from a URL. The JavaScript `URL` API parses IPv6 addresses like `[::1]` to
`::1` (without brackets) for the `hostname` property. However, the `if` condition at
`packages/spotlight/src/server/utils/cors.ts:22` checks for `hostname === "[::1]"`. This
mismatch causes the comparison `"::1" === "[::1]"` to evaluate to `false`, preventing
IPv6 localhost addresses from being correctly identified as allowed origins. This leads
to all requests from IPv6 localhost addresses (`[::1]`) being incorrectly rejected by
the CORS middleware, breaking local development for IPv6 users.

Did we get this right? 👍 / 👎 to inform future reviews.
Reference_id: 2843300

@BYK BYK merged commit d9ed848 into main Nov 20, 2025
21 checks passed
@BYK BYK deleted the cursor/secure-sidecar-with-cors-origin-restrictions-ccdd branch November 20, 2025 11:54
BYK added a commit that referenced this pull request Nov 21, 2025
BYK added a commit that referenced this pull request Nov 21, 2025
betegon added a commit that referenced this pull request Dec 1, 2025
This PR was opened by the [Changesets
release](https://github.com/changesets/action) GitHub action. When
you're ready to do a release, you can merge this and publish to npm
yourself or [setup this action to publish
automatically](https://github.com/changesets/action#with-publishing). If
you're not ready to do a release yet, that's fine, whenever you add more
changesets to main, this PR will be updated.


# Releases
## @spotlightjs/[email protected]

### Minor Changes

- Added spotlight sdk for helping others to build on top of it
([#1140](#1140))

- Support COMPOSE_FILE environment variable for Docker Compose projects
([#1131](#1131))

- Prompt user to choose between docker compose and package.json when
both are present
([#1120](#1120))

### Patch Changes

- Refactor docker compose support
([#1121](#1121))

- disable sentry in development mode
([#1143](#1143))

- **Security:** Restrict CORS origins for Sidecar to prevent
unauthorized access
([#1138](#1138))

    The Sidecar now only accepts requests from trusted origins:

    -   `localhost` with any port or protocol (http/https)
- `https://spotlightjs.com` and `https://*.spotlightjs.com` (HTTPS only,
default port)

⚠️ **Potentially Breaking:** If you were accessing the Sidecar from
other origins (e.g., custom domains, non-HTTPS spotlightjs.com), those
connections will now be rejected. This change improves security by
preventing malicious websites from connecting to your local Sidecar
instance.

- Fix file capture error handling to log errors instead of crashing when
SPOTLIGHT_CAPTURE is enabled
([#1142](#1142))

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Miguel Betegón <[email protected]>
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.

Restrict CORS headers for localhost:* and https://local.spotlightjs.com

4 participants