Skip to content

Commit 15a25de

Browse files
committed
support either string or array for the allowOrigin option
Added a test for when an array is set as the allowOrigin value
1 parent f88daeb commit 15a25de

File tree

5 files changed

+41
-9
lines changed

5 files changed

+41
-9
lines changed

spec/Middlewares.spec.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ 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', () => {
290+
it('should support multiple origins if several are defined in allowOrigin as a comma delimited string', () => {
291291
AppCache.put(fakeReq.body._ApplicationId, {
292292
allowOrigin: 'https://a.com,https://b.com,https://c.com',
293293
});
@@ -316,6 +316,35 @@ describe('middlewares', () => {
316316
expect(headers['Access-Control-Allow-Origin']).toEqual('https://a.com');
317317
});
318318

319+
it('should support multiple origins if several are defined in allowOrigin as an array', () => {
320+
AppCache.put(fakeReq.body._ApplicationId, {
321+
allowOrigin: ['https://a.com', 'https://b.com', 'https://c.com'],
322+
});
323+
const headers = {};
324+
const res = {
325+
header: (key, value) => {
326+
headers[key] = value;
327+
},
328+
};
329+
const allowCrossDomain = middlewares.allowCrossDomain(fakeReq.body._ApplicationId);
330+
// Test with the first domain
331+
fakeReq.headers.origin = 'https://a.com';
332+
allowCrossDomain(fakeReq, res, () => {});
333+
expect(headers['Access-Control-Allow-Origin']).toEqual('https://a.com');
334+
// Test with the second domain
335+
fakeReq.headers.origin = 'https://b.com';
336+
allowCrossDomain(fakeReq, res, () => {});
337+
expect(headers['Access-Control-Allow-Origin']).toEqual('https://b.com');
338+
// Test with the third domain
339+
fakeReq.headers.origin = 'https://c.com';
340+
allowCrossDomain(fakeReq, res, () => {});
341+
expect(headers['Access-Control-Allow-Origin']).toEqual('https://c.com');
342+
// Test with an unauthorized domain
343+
fakeReq.headers.origin = 'https://unauthorized.com';
344+
allowCrossDomain(fakeReq, res, () => {});
345+
expect(headers['Access-Control-Allow-Origin']).toEqual('https://a.com');
346+
});
347+
319348
it('should use user provided on field userFromJWT', done => {
320349
AppCache.put(fakeReq.body._ApplicationId, {
321350
masterKey: 'masterKey',

src/Options/Definitions.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ module.exports.ParseServerOptions = {
8282
allowOrigin: {
8383
env: 'PARSE_SERVER_ALLOW_ORIGIN',
8484
help:
85-
'Sets the origin to Access-Control-Allow-Origin. Can be comma delimited if multiple should be supported',
85+
'Sets the origin to Access-Control-Allow-Origin. Can be a string for a single origin or a comma separated string or array for multiple',
8686
},
8787
analyticsAdapter: {
8888
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. Can be comma delimited to support multiple */
65-
allowOrigin: ?string;
64+
/* Sets the origin to Access-Control-Allow-Origin. Can be a string for a single origin or a comma separated string or array for multiple */
65+
allowOrigin: ?(string | string[]);
6666
/* Adapter module for the analytics */
6767
analyticsAdapter: ?Adapter<AnalyticsAdapter>;
6868
/* Adapter module for the files sub-system */

src/middlewares.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -386,10 +386,13 @@ export function allowCrossDomain(appId) {
386386
}
387387

388388
// Support for multiple origins
389-
const allowedOrigins =
390-
config && config.allowOrigin
391-
? config.allowOrigin.split(',').map(domain => domain.trim())
392-
: ['*'];
389+
let allowedOrigins = config && config.allowOrigin ? config.allowOrigin : ['*'];
390+
391+
// Convert comma-separated string to an array if needed
392+
if (typeof allowedOrigins === 'string') {
393+
allowedOrigins = allowedOrigins.split(',').map(domain => domain.trim());
394+
}
395+
393396
const requestOrigin = req.headers.origin;
394397
const originToSet =
395398
requestOrigin && allowedOrigins.includes(requestOrigin) ? requestOrigin : allowedOrigins[0];

0 commit comments

Comments
 (0)