Skip to content

Commit 00484ef

Browse files
committed
feat: Allow multiple origins
1 parent 177891e commit 00484ef

File tree

5 files changed

+43
-6
lines changed

5 files changed

+43
-6
lines changed

spec/Middlewares.spec.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,35 @@ describe('middlewares', () => {
287287
expect(headers['Access-Control-Allow-Origin']).toEqual('https://parseplatform.org/');
288288
});
289289

290+
it('should support multiple origins if several are defined in allowOrigin', () => {
291+
AppCache.put(fakeReq.body._ApplicationId, {
292+
allowOrigin: 'https://a.com,https://b.com,https://c.com',
293+
});
294+
const headers = {};
295+
const res = {
296+
header: (key, value) => {
297+
headers[key] = value;
298+
},
299+
};
300+
const allowCrossDomain = middlewares.allowCrossDomain(fakeReq.body._ApplicationId);
301+
// Test with the first domain
302+
fakeReq.headers.origin = 'https://a.com';
303+
allowCrossDomain(fakeReq, res, () => {});
304+
expect(headers['Access-Control-Allow-Origin']).toEqual('https://a.com');
305+
// Test with the second domain
306+
fakeReq.headers.origin = 'https://b.com';
307+
allowCrossDomain(fakeReq, res, () => {});
308+
expect(headers['Access-Control-Allow-Origin']).toEqual('https://b.com');
309+
// Test with the third domain
310+
fakeReq.headers.origin = 'https://c.com';
311+
allowCrossDomain(fakeReq, res, () => {});
312+
expect(headers['Access-Control-Allow-Origin']).toEqual('https://c.com');
313+
// Test with an unauthorized domain
314+
fakeReq.headers.origin = 'https://unauthorized.com';
315+
allowCrossDomain(fakeReq, res, () => {});
316+
expect(headers['Access-Control-Allow-Origin']).toEqual('https://a.com');
317+
});
318+
290319
it('should use user provided on field userFromJWT', done => {
291320
AppCache.put(fakeReq.body._ApplicationId, {
292321
masterKey: 'masterKey',

src/Options/Definitions.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ module.exports.ParseServerOptions = {
8181
},
8282
allowOrigin: {
8383
env: 'PARSE_SERVER_ALLOW_ORIGIN',
84-
help: 'Sets the origin to Access-Control-Allow-Origin',
84+
help: 'Sets the origins to Access-Control-Allow-Origin',
8585
},
8686
analyticsAdapter: {
8787
env: 'PARSE_SERVER_ANALYTICS_ADAPTER',

src/Options/docs.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Options/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ export interface ParseServerOptions {
6161
appName: ?string;
6262
/* Add headers to Access-Control-Allow-Headers */
6363
allowHeaders: ?(string[]);
64-
/* Sets the origin to Access-Control-Allow-Origin */
65-
allowOrigin: ?string;
64+
/* Sets the origins to Access-Control-Allow-Origin */
65+
allowOrigin: ?(string[]);
6666
/* Adapter module for the analytics */
6767
analyticsAdapter: ?Adapter<AnalyticsAdapter>;
6868
/* Adapter module for the files sub-system */

src/middlewares.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -384,8 +384,16 @@ export function allowCrossDomain(appId) {
384384
if (config && config.allowHeaders) {
385385
allowHeaders += `, ${config.allowHeaders.join(', ')}`;
386386
}
387-
const allowOrigin = (config && config.allowOrigin) || '*';
388-
res.header('Access-Control-Allow-Origin', allowOrigin);
387+
388+
// Support for multiple origins
389+
const allowedOrigins =
390+
config && config.allowOrigin
391+
? config.allowOrigin.split(',').map(domain => domain.trim())
392+
: ['*'];
393+
const requestOrigin = req.headers.origin;
394+
const originToSet =
395+
requestOrigin && allowedOrigins.includes(requestOrigin) ? requestOrigin : allowedOrigins[0];
396+
res.header('Access-Control-Allow-Origin', originToSet);
389397
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
390398
res.header('Access-Control-Allow-Headers', allowHeaders);
391399
res.header('Access-Control-Expose-Headers', 'X-Parse-Job-Status-Id, X-Parse-Push-Status-Id');

0 commit comments

Comments
 (0)