feat: allow custom cors origin header (#6772)

This commit is contained in:
Kevin Yao
2020-07-10 11:48:57 -07:00
committed by GitHub
parent 6fc42a526f
commit d03ec18bcc
5 changed files with 45 additions and 1 deletions

View File

@@ -357,6 +357,42 @@ describe('middlewares', () => {
); );
}); });
it('should set default Access-Control-Allow-Origin if allowOrigin is empty', () => {
AppCache.put(fakeReq.body._ApplicationId, {
allowOrigin: undefined,
});
const headers = {};
const res = {
header: (key, value) => {
headers[key] = value;
},
};
const allowCrossDomain = middlewares.allowCrossDomain(
fakeReq.body._ApplicationId
);
allowCrossDomain(fakeReq, res, () => {});
expect(headers['Access-Control-Allow-Origin']).toEqual('*');
});
it('should set custom origin to Access-Control-Allow-Origin if allowOrigin is provided', () => {
AppCache.put(fakeReq.body._ApplicationId, {
allowOrigin: 'https://parseplatform.org/',
});
const headers = {};
const res = {
header: (key, value) => {
headers[key] = value;
},
};
const allowCrossDomain = middlewares.allowCrossDomain(
fakeReq.body._ApplicationId
);
allowCrossDomain(fakeReq, res, () => {});
expect(headers['Access-Control-Allow-Origin']).toEqual(
'https://parseplatform.org/'
);
});
it('should use user provided on field userFromJWT', (done) => { it('should use user provided on field userFromJWT', (done) => {
AppCache.put(fakeReq.body._ApplicationId, { AppCache.put(fakeReq.body._ApplicationId, {
masterKey: 'masterKey', masterKey: 'masterKey',

View File

@@ -28,6 +28,10 @@ module.exports.ParseServerOptions = {
help: 'Add headers to Access-Control-Allow-Headers', help: 'Add headers to Access-Control-Allow-Headers',
action: parsers.arrayParser, action: parsers.arrayParser,
}, },
allowOrigin: {
env: 'PARSE_SERVER_ALLOW_ORIGIN',
help: 'Sets the origin to Access-Control-Allow-Origin',
},
analyticsAdapter: { analyticsAdapter: {
env: 'PARSE_SERVER_ANALYTICS_ADAPTER', env: 'PARSE_SERVER_ANALYTICS_ADAPTER',
help: 'Adapter module for the analytics', help: 'Adapter module for the analytics',

View File

@@ -4,6 +4,7 @@
* @property {Boolean} allowClientClassCreation Enable (or disable) client class creation, defaults to true * @property {Boolean} allowClientClassCreation Enable (or disable) client class creation, defaults to true
* @property {Boolean} allowCustomObjectId Enable (or disable) custom objectId * @property {Boolean} allowCustomObjectId Enable (or disable) custom objectId
* @property {String[]} allowHeaders Add headers to Access-Control-Allow-Headers * @property {String[]} allowHeaders Add headers to Access-Control-Allow-Headers
* @property {String} allowOrigin Sets the origin to Access-Control-Allow-Origin
* @property {Adapter<AnalyticsAdapter>} analyticsAdapter Adapter module for the analytics * @property {Adapter<AnalyticsAdapter>} analyticsAdapter Adapter module for the analytics
* @property {String} appId Your Parse Application ID * @property {String} appId Your Parse Application ID
* @property {String} appName Sets the app name * @property {String} appName Sets the app name

View File

@@ -29,6 +29,8 @@ export interface ParseServerOptions {
appName: ?string; appName: ?string;
/* Add headers to Access-Control-Allow-Headers */ /* Add headers to Access-Control-Allow-Headers */
allowHeaders: ?(string[]); allowHeaders: ?(string[]);
/* Sets the origin to Access-Control-Allow-Origin */
allowOrigin: ?string;
/* Adapter module for the analytics */ /* Adapter module for the analytics */
analyticsAdapter: ?Adapter<AnalyticsAdapter>; analyticsAdapter: ?Adapter<AnalyticsAdapter>;
/* Adapter module for the files sub-system */ /* Adapter module for the files sub-system */

View File

@@ -316,7 +316,8 @@ export function allowCrossDomain(appId) {
if (config && config.allowHeaders) { if (config && config.allowHeaders) {
allowHeaders += `, ${config.allowHeaders.join(', ')}`; allowHeaders += `, ${config.allowHeaders.join(', ')}`;
} }
res.header('Access-Control-Allow-Origin', '*'); const allowOrigin = (config && config.allowOrigin) || '*';
res.header('Access-Control-Allow-Origin', allowOrigin);
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS'); res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
res.header('Access-Control-Allow-Headers', allowHeaders); res.header('Access-Control-Allow-Headers', allowHeaders);
res.header( res.header(