Skip to content

Commit d03ec18

Browse files
authored
feat: allow custom cors origin header (#6772)
1 parent 6fc42a5 commit d03ec18

File tree

5 files changed

+45
-1
lines changed

5 files changed

+45
-1
lines changed

spec/Middlewares.spec.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,42 @@ describe('middlewares', () => {
357357
);
358358
});
359359

360+
it('should set default Access-Control-Allow-Origin if allowOrigin is empty', () => {
361+
AppCache.put(fakeReq.body._ApplicationId, {
362+
allowOrigin: undefined,
363+
});
364+
const headers = {};
365+
const res = {
366+
header: (key, value) => {
367+
headers[key] = value;
368+
},
369+
};
370+
const allowCrossDomain = middlewares.allowCrossDomain(
371+
fakeReq.body._ApplicationId
372+
);
373+
allowCrossDomain(fakeReq, res, () => {});
374+
expect(headers['Access-Control-Allow-Origin']).toEqual('*');
375+
});
376+
377+
it('should set custom origin to Access-Control-Allow-Origin if allowOrigin is provided', () => {
378+
AppCache.put(fakeReq.body._ApplicationId, {
379+
allowOrigin: 'https://parseplatform.org/',
380+
});
381+
const headers = {};
382+
const res = {
383+
header: (key, value) => {
384+
headers[key] = value;
385+
},
386+
};
387+
const allowCrossDomain = middlewares.allowCrossDomain(
388+
fakeReq.body._ApplicationId
389+
);
390+
allowCrossDomain(fakeReq, res, () => {});
391+
expect(headers['Access-Control-Allow-Origin']).toEqual(
392+
'https://parseplatform.org/'
393+
);
394+
});
395+
360396
it('should use user provided on field userFromJWT', (done) => {
361397
AppCache.put(fakeReq.body._ApplicationId, {
362398
masterKey: 'masterKey',

src/Options/Definitions.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ module.exports.ParseServerOptions = {
2828
help: 'Add headers to Access-Control-Allow-Headers',
2929
action: parsers.arrayParser,
3030
},
31+
allowOrigin: {
32+
env: 'PARSE_SERVER_ALLOW_ORIGIN',
33+
help: 'Sets the origin to Access-Control-Allow-Origin',
34+
},
3135
analyticsAdapter: {
3236
env: 'PARSE_SERVER_ANALYTICS_ADAPTER',
3337
help: 'Adapter module for the analytics',

src/Options/docs.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* @property {Boolean} allowClientClassCreation Enable (or disable) client class creation, defaults to true
55
* @property {Boolean} allowCustomObjectId Enable (or disable) custom objectId
66
* @property {String[]} allowHeaders Add headers to Access-Control-Allow-Headers
7+
* @property {String} allowOrigin Sets the origin to Access-Control-Allow-Origin
78
* @property {Adapter<AnalyticsAdapter>} analyticsAdapter Adapter module for the analytics
89
* @property {String} appId Your Parse Application ID
910
* @property {String} appName Sets the app name

src/Options/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ export interface ParseServerOptions {
2929
appName: ?string;
3030
/* Add headers to Access-Control-Allow-Headers */
3131
allowHeaders: ?(string[]);
32+
/* Sets the origin to Access-Control-Allow-Origin */
33+
allowOrigin: ?string;
3234
/* Adapter module for the analytics */
3335
analyticsAdapter: ?Adapter<AnalyticsAdapter>;
3436
/* Adapter module for the files sub-system */

src/middlewares.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,8 @@ export function allowCrossDomain(appId) {
316316
if (config && config.allowHeaders) {
317317
allowHeaders += `, ${config.allowHeaders.join(', ')}`;
318318
}
319-
res.header('Access-Control-Allow-Origin', '*');
319+
const allowOrigin = (config && config.allowOrigin) || '*';
320+
res.header('Access-Control-Allow-Origin', allowOrigin);
320321
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
321322
res.header('Access-Control-Allow-Headers', allowHeaders);
322323
res.header(

0 commit comments

Comments
 (0)