Skip to content
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

Configuring access-control-allow-origin for development environment purposes #295

Open
sysaxis opened this issue Nov 4, 2021 · 3 comments

Comments

@sysaxis
Copy link

sysaxis commented Nov 4, 2021

Imagine a situation, where you are running the service on one port and the frontend dev server on another. It's impossible to use sockjs connections, because it will end up with CORS error (origin differs from the backend server url).

How about enable configuring the default response header for Access-Control-Allow-Origin in middleware.js#L123? If you are worried about the potential of unintended security flaws then add an explicit check to not allow overriding it in production environment.

@brycekahle
Copy link
Contributor

I don't follow your scenario. Can you describe it in more detail, perhaps a diagram?

The origin used in the line you linked is the Origin header sent by the browser when making the requests. It can be completely different from the backend server URL, and that is OK.

@sysaxis
Copy link
Author

sysaxis commented Nov 5, 2021

Server: http://localhost:8081
Frontend dev: http://localhost:8080

For some reason, the connection fails due to CORS:
Access to XMLHttpRequest at 'http://localhost:8081/mypath/info?t=1636117522701' from origin 'http://localhost:8080' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

The "disable_cors" parameter has not been set, thus CORS is enabled. It seems that the issue is in something else. The request doesn't even reach any of the checkpoints where sockjs sets the CORS headers (xhr and eventsource transport).

My current workaround was adding a proxy path (http://localhost:8081/ -> http://localhost:8080/) and the CORS issue got resolved when the application was opened at the proxy path.

I think it's important to mention that I am using expressjs along with sockjs. It may be the reason for such behaviour, because websocket connections also fail for unknown reason (which is why the application falls back to xhr polling).
`
// example
const app = express();
app.use(cors())
// app.use(...);

const server = app.listen(8081);
const sock = sockjs.createServer();
// ...subscribe to events
sock.installHandlers(server, {
prefix: '/mypath.*'
}
`

@brycekahle
Copy link
Contributor

Using express is fine, there is an example of how to use it even: https://github.com/sockjs/sockjs-node/blob/v0.3/examples/express/server.js

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

No branches or pull requests

2 participants