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

Can't configure Express cors to work with peer server #196

Closed
Ib-R opened this issue Jun 4, 2020 · 9 comments
Closed

Can't configure Express cors to work with peer server #196

Ib-R opened this issue Jun 4, 2020 · 9 comments

Comments

@Ib-R
Copy link

Ib-R commented Jun 4, 2020

I'm having an issue:

  • I have peer server running with express, CORS middleware and it works with all routes except the peer server.
  • Running peer version "0.5.3"
  • Nodejs version "12.14.1"
  • The platform "Windows 10"
  • This is how the server code look
const corsOptions = {
	origin: "https://mydomain.com",
	optionsSuccessStatus: 200,
};

const peerServer = ExpressPeerServer(httpsServer, {
	debug: true,
	path: "/",
});

app.use(cors(corsOptions));
app.use("/", peerServer);

app.get('/test', (req, res, next) => {
	res.json({hello: 123});
})

httpsServer.listen(port);
  • I get no error messages, just the CORS header is set to "*" which is the default

I have a suggestion:

  • It would be a good idea if there's a way to configure the CORS for the peer server
  • I tried to edit the index.js file inside (peer/dist/src/api) and added the CORS options and it did work
exports.Api = ({ config, realm, messageHandler }) => {
    const authMiddleware = new auth_1.AuthMiddleware(config, realm);
    const app = express_1.default.Router();
    const jsonParser = body_parser_1.default.json();
    app.use(cors_1.default({
	origin: "https://mydomain.com",
	optionsSuccessStatus: 200,
}));
    app.get("/", (_, res) => {
        res.send(app_json_1.default);
    });
    app.use("/:key", public_1.default({ config, realm }));
    app.use("/:key/:id/:token", authMiddleware.handle, jsonParser, calls_1.default({ realm, messageHandler }));
    return app;
};
@sunshinecool
Copy link

Can you elaborate on your frontend, app server and peerjs server setup? If you want to enable your domain to call the peerjs server, you need to enable CORS on your app server as well.

@Ib-R
Copy link
Author

Ib-R commented Jun 15, 2020

I'm trying to enable CORS on my server so no one can access it but my own front-end app and the configuration works as expected only the peer signaling doesn't respect it.

@sunshinecool
Copy link

I would suggest adding authentication to your app instead of using cors, its easy to fake the origin header to seem like your frontend is sending requests. But this is a legit usecase, I wonder where the cors rules is enforced in express, if it happens before routing the requests (in app) this should have worked, but doesn't seem to be the case. Maybe worth looking at express codebase. I am just shooting in the dark at this point.

@Ib-R
Copy link
Author

Ib-R commented Jun 15, 2020

@sunshinecool thnx for your response, am just trying to add extra layer of security and as u can see in my code snippet the CORS does work for any route actually but the peer library override it that's the problem

@afrokick
Copy link
Member

As @xenoterracide suggested in #221, we can add --cors flag, will it works?

@xenoterracide
Copy link

and of course then add that to the docker container...

github-actions bot pushed a commit that referenced this issue Feb 14, 2023
# [1.0.0-rc.10](v1.0.0-rc.9...v1.0.0-rc.10) (2023-02-14)

### Features

* set the PEERSERVER_PATH with an environment variable ([084fb8a](084fb8a)), closes [#213](#213)
* set the PORT with an environment variable ([68a3398](68a3398)), closes [#213](#213)
* specify cors options via cli or js ([05f12cd](05f12cd)), closes [#196](#196) [#221](#221)
@github-actions
Copy link

🎉 This issue has been resolved in version 1.0.0-rc.10 🎉

The release is available on:

Your semantic-release bot 📦🚀

@jonasgloning
Copy link
Member

jonasgloning commented Feb 14, 2023

You can now pass your corsOptions object with the corsOptions property of the config object.

const peerServer = ExpressPeerServer(httpsServer, {
	debug: true,
	path: "/",
        corsOptions
});

github-actions bot pushed a commit that referenced this issue Mar 7, 2023
# [1.0.0](v0.6.1...v1.0.0) (2023-03-07)

### Bug Fixes

* **deps:** update dependency ws to v8 ([1ecc94b](1ecc94b))
* import from ESM only environments ([476299e](476299e))
* more accurate types ([68f973a](68f973a)), closes [#182](#182)
* **npm audit:** Updates all dependencies that cause `npm audit` to issue a warning ([1aaafbc](1aaafbc)), closes [#287](#287)
* the server could crash if a client sends invalid frames ([29394de](29394de))

### Features

* drop Node {10,11,12,13} support ([b70ed79](b70ed79))
* ESM support ([2b73b5c](2b73b5c))
* remove deprecated XHR fallback ([d900145](d900145))
* set the PEERSERVER_PATH with an environment variable ([084fb8a](084fb8a)), closes [#213](#213)
* set the PORT with an environment variable ([68a3398](68a3398)), closes [#213](#213)
* specify cors options via cli or js ([05f12cd](05f12cd)), closes [#196](#196) [#221](#221)

### Performance Improvements

* use the builtin UUID generator for Peer ids instead of the `uuid` module ([5d882dd](5d882dd))

### BREAKING CHANGES

* Requires PeerJS >= 1.0
* Node >= 14 required

14 is the oldest currently supported version. See https://github.com/nodejs/release#release-schedule
@github-actions
Copy link

github-actions bot commented Mar 7, 2023

🎉 This issue has been resolved in version 1.0.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants