Skip to content

feat(cors): Add CORS policy check and response to browsers. #450

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

Open
wants to merge 17 commits into
base: main
Choose a base branch
from

Conversation

AOrazaev
Copy link

No description provided.

@AOrazaev
Copy link
Author

@microsoft-github-policy-service agree

@AOrazaev AOrazaev force-pushed the orazaev/cors_support branch from b21f59d to 770eae1 Compare May 20, 2025 01:24
@AOrazaev AOrazaev force-pushed the orazaev/cors_support branch from bfe66d6 to 9eeab1e Compare May 28, 2025 22:41
@pavelfeldman pavelfeldman requested a review from Copilot June 2, 2025 20:39
Copy link

@Copilot 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 support for handling CORS by performing a policy check in the transport layer and updating the command-line options as well as the documentation accordingly.

  • Introduces a new checkCors function in the transport module that applies CORS headers if the Origin header is allowed.
  • Adds a new CLI option (--cors-allowed-origins) and the corresponding configuration property for CORS handling.
  • Updates the README to document the new CORS option alongside the existing allowed-/blocked-origins options.

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.

File Description
src/transport.ts Adds CORS check functionality and OPTIONS handling for SSE and streamable transports.
src/program.ts Adds new CLI option for specifying allowed origins via CORS policy.
src/config.ts Adds corsAllowedOrigins to configuration options.
README.md Updates documentation to include the new CORS policy option.
Comments suppressed due to low confidence (1)

README.md:133

  • The documentation now lists both '--allowed-origins' and '--cors-allowed-origins'. It would be helpful to clarify their distinct purposes or consider consolidating them to avoid potential confusion.
--cors-allowed-origins <origin>  semicolon-separated list of allowed origins by CORS policy.

async function handleSSE(server: Server, req: http.IncomingMessage, res: http.ServerResponse, url: URL, sessions: Map<string, SSEServerTransport>) {
if (checkCors(server.config, req, res) && req.method === 'OPTIONS') {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if (checkCors(server.config, req, res) && req.method === 'OPTIONS') {
if (req.method === 'OPTIONS') {
if (checkCors(server.config, req, res)) {
...
} else {
...
}
return;
}

Copy link
Author

Choose a reason for hiding this comment

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

Hey Pavel, thanks for your suggestion!

Unfortunately, as far as I understand, this particular suggestion will break the behavior. Browser will require CORS headers to be populated not only for "preflight" request (OPTIONS), but also for every consequent request (POST/GET). So OPTIONS will succeed, actual POST will break because of cors.

This is the reason I've put the function check in front. I understand that this is probably not an obvious behavior and it happens implicitly. Feel free to suggest how to make it better and more explicit - I'll happily make a change.

Thank you!

Copy link
Member

Choose a reason for hiding this comment

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

const { corsAllowed } = applyCorsHeaders(...);
if (req.method === 'OPTIONS' && corsAllowed)
  return /*204*/;

AOrazaev and others added 3 commits June 4, 2025 14:36
Copy link
Member

@pavelfeldman pavelfeldman left a comment

Choose a reason for hiding this comment

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

We usually write tests for the changes. You can consider reusing the sse tests and accessing it from the test server (server fixture).

async function handleSSE(server: Server, req: http.IncomingMessage, res: http.ServerResponse, url: URL, sessions: Map<string, SSEServerTransport>) {
if (checkCors(server.config, req, res) && req.method === 'OPTIONS') {
Copy link
Member

Choose a reason for hiding this comment

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

const { corsAllowed } = applyCorsHeaders(...);
if (req.method === 'OPTIONS' && corsAllowed)
  return /*204*/;

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.

2 participants