Skip to content

Commit c50532d

Browse files
committed
Allow disabling of CORS
It is desirable to disable CORS for development purposes in order to validate same-origin policy and web security concepts. Additionally, json-server could be used to show proxy concepts. This change adds a new CLI option `--no-cors` which disables CORS by not adding the CORS middleware. The change is backwards compatible since the default behavior, i.e. adding CORS headers is retained.
1 parent b751cec commit c50532d

File tree

4 files changed

+46
-2
lines changed

4 files changed

+46
-2
lines changed

src/cli/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ module.exports = function () {
3636
alias: 'ro',
3737
description: 'Allow only GET requests'
3838
},
39+
'no-cors': {
40+
alias: 'nc',
41+
description: 'Disable Cross-Origin Resource Sharing'
42+
},
3943
snapshots: {
4044
alias: 'S',
4145
description: 'Set snapshots directory',
@@ -58,6 +62,7 @@ module.exports = function () {
5862
.boolean('watch')
5963
.boolean('read-only')
6064
.boolean('quiet')
65+
.boolean('no-cors')
6166
.help('help').alias('help', 'h')
6267
.version(pkg.version).alias('version', 'v')
6368
.example('$0 db.json', '')

src/cli/run.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ function createApp (source, object, routes, argv) {
4444

4545
var defaultsOpts = {
4646
logger: !argv.quiet,
47-
readOnly: argv.readOnly
47+
readOnly: argv.readOnly,
48+
noCors: argv.noCors
4849
}
4950

5051
if (argv.static) {

src/server/defaults.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ module.exports = function (opts) {
3232
}
3333

3434
// Enable CORS for all the requests, including static files
35-
arr.push(cors({ origin: true, credentials: true }))
35+
if (!opts.noCors) {
36+
arr.push(cors({ origin: true, credentials: true }))
37+
}
3638

3739
if (process.env.NODE_ENV === 'development') {
3840
// only use in development

test/cli/index.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,15 @@ describe('cli', function () {
5353
request.get('/posts').expect(200, done)
5454
})
5555

56+
it('should send CORS headers', function (done) {
57+
var origin = 'http://example.com'
58+
59+
request.get('/posts')
60+
.set('Origin', origin)
61+
.expect('access-control-allow-origin', origin)
62+
.expect(200, done)
63+
})
64+
5665
})
5766

5867
describe('seed.js', function () {
@@ -140,6 +149,33 @@ describe('cli', function () {
140149

141150
})
142151

152+
describe('db.json --no-cors=true', function () {
153+
154+
beforeEach(function (done) {
155+
child = cli(['fixtures/seed.js', '--no-cors=true'])
156+
serverReady(PORT, done)
157+
})
158+
159+
it('should not send Access-Control-Allow-Origin headers', function (done) {
160+
var origin = 'http://example.com'
161+
162+
request.get('/posts')
163+
.set('Origin', origin)
164+
.expect(200)
165+
.end(function (err, res) {
166+
if (err) {
167+
done(err)
168+
return
169+
} else if ('access-control-allow-origin' in res.headers) {
170+
done(new Error('CORS headers were not excluded from response'))
171+
} else {
172+
done()
173+
}
174+
})
175+
})
176+
177+
})
178+
143179
// FIXME test fails on OS X and maybe on Windows
144180
// But manually updating db.json works...
145181
if (os.platform() === 'linux') {

0 commit comments

Comments
 (0)