Skip to content

Commit a92a6f4

Browse files
authored
Merge pull request #503 from keystonejs/user-configurable-cors
Make CORS configurable by end users
2 parents 3133e15 + 9136f6e commit a92a6f4

File tree

5 files changed

+38
-7
lines changed

5 files changed

+38
-7
lines changed

.changeset/93d02184/changes.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"releases": [{ "name": "@voussoir/server", "type": "minor" }],
3+
"dependents": [
4+
{ "name": "@voussoir/test-utils", "type": "patch", "dependencies": ["@voussoir/server"] },
5+
{
6+
"name": "@voussoir/cypress-project-access-control",
7+
"type": "patch",
8+
"dependencies": ["@voussoir/test-utils", "@voussoir/server"]
9+
},
10+
{
11+
"name": "@voussoir/cypress-project-basic",
12+
"type": "patch",
13+
"dependencies": ["@voussoir/test-utils", "@voussoir/server"]
14+
},
15+
{
16+
"name": "@voussoir/cypress-project-login",
17+
"type": "patch",
18+
"dependencies": ["@voussoir/test-utils", "@voussoir/server"]
19+
},
20+
{
21+
"name": "@voussoir/cypress-project-twitter-login",
22+
"type": "patch",
23+
"dependencies": ["@voussoir/server"]
24+
}
25+
]
26+
}

.changeset/93d02184/changes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Makes CORS user configurable

packages/server/WebServer/graphql.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,13 @@ module.exports = function createGraphQLMiddleware(keystone, { apiPath, graphiqlP
124124
}
125125
},
126126
});
127-
server.applyMiddleware({ app, path: apiPath });
127+
server.applyMiddleware({
128+
app,
129+
path: apiPath,
130+
// Prevent ApolloServer from overriding Keystone's CORS configuration.
131+
// https://www.apollographql.com/docs/apollo-server/api/apollo-server.html#ApolloServer-applyMiddleware
132+
cors: false,
133+
});
128134
if (graphiqlPath) {
129135
app.use(graphiqlPath, (req, res) => {
130136
if (req.user && req.sessionID) {

packages/server/WebServer/index.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,9 @@ module.exports = class WebServer {
2323
this.app.use(require('express-pino-logger')(this.config.pinoOptions));
2424
}
2525

26-
this.app.use(
27-
cors({
28-
origin: true,
29-
credentials: true,
30-
})
31-
);
26+
if (this.config.cors) {
27+
this.app.use(cors(this.config.cors));
28+
}
3229

3330
if (this.config.authStrategy) {
3431
// Setup the session as the very first thing.

packages/server/WebServer/initConfig.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const defaultConfig = {
66
port: process.env.PORT || 3000,
77
apiPath: '/admin/api',
88
graphiqlPath: '/admin/graphiql',
9+
cors: { origin: true, credentials: true },
910
};
1011

1112
const remapKeys = {

0 commit comments

Comments
 (0)