Skip to content

Commit

Permalink
server.cors.origin: * --> server.cors.origin: ["*"]
Browse files Browse the repository at this point in the history
  • Loading branch information
mshustov committed Dec 14, 2020
1 parent 30441a5 commit c764de2
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 11 deletions.
2 changes: 1 addition & 1 deletion docs/setup/settings.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ deprecation warning at startup. This setting cannot end in a slash (`/`).
| experimental[] Set to `true` to allow browser code to access response body whenever request performed with user credentials. *Default:* `false`

| `server.cors.allowOrigin:`
| experimental[] List of origins permitted to access resources. You must specify explicit hostnames and not use `*` for `server.cors.allowOrigin` when `server.cors.allowCredentials: true`. *Default:* "*"
| experimental[] List of origins permitted to access resources. You must specify explicit hostnames and not use `server.cors.allowOrigin: ["*"]` when `server.cors.allowCredentials: true`. *Default:* ["*"]

| `server.compression.referrerWhitelist:`
| Specifies an array of trusted hostnames, such as the {kib} host, or a reverse
Expand Down
4 changes: 3 additions & 1 deletion src/core/server/http/__snapshots__/http_config.test.ts.snap

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 17 additions & 3 deletions src/core/server/http/http_config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ describe('cors', () => {
})
).toThrowErrorMatchingInlineSnapshot(`
"[cors.allowOrigin]: types that failed validation:
- [cors.allowOrigin.0]: expected value to equal [*]
- [cors.allowOrigin.0]: array size is [0], but cannot be smaller than [1]
- [cors.allowOrigin.1]: array size is [0], but cannot be smaller than [1]"
`);
});
Expand All @@ -364,14 +364,28 @@ describe('cors', () => {
});

it('can be configured as "*" wildcard', () => {
expect(config.schema.validate({ cors: { allowOrigin: '*' } }).cors.allowOrigin).toBe('*');
expect(config.schema.validate({ cors: { allowOrigin: ['*'] } }).cors.allowOrigin).toEqual([
'*',
]);
});

it('cannot mix wildcard "*" with valid URLs', () => {
expect(
() =>
config.schema.validate({ cors: { allowOrigin: ['*', 'https://elastic.co'] } }).cors
.allowOrigin
).toThrowErrorMatchingInlineSnapshot(`
"[cors.allowOrigin]: types that failed validation:
- [cors.allowOrigin.0.0]: expected URI with scheme [http|https].
- [cors.allowOrigin.1.1]: expected value to equal [*]"
`);
});
});
describe('credentials', () => {
it('cannot use wildcard allowOrigin if "credentials: true"', () => {
expect(
() =>
config.schema.validate({ cors: { allowCredentials: true, allowOrigin: '*' } }).cors
config.schema.validate({ cors: { allowCredentials: true, allowOrigin: ['*'] } }).cors
.allowOrigin
).toThrowErrorMatchingInlineSnapshot(
`"[cors]: Cannot specify wildcard origin \\"*\\" with \\"credentials: true\\". Please provide a list of allowed origins."`
Expand Down
11 changes: 7 additions & 4 deletions src/core/server/http/http_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,18 @@ export const config = {
enabled: schema.boolean({ defaultValue: false }),
allowCredentials: schema.boolean({ defaultValue: false }),
allowOrigin: schema.oneOf(
[schema.literal('*'), schema.arrayOf(hostURISchema, { minSize: 1 })],
[
schema.arrayOf(hostURISchema, { minSize: 1 }),
schema.arrayOf(schema.literal('*'), { minSize: 1, maxSize: 1 }),
],
{
defaultValue: '*',
defaultValue: ['*'],
}
),
},
{
validate(value) {
if (value.allowCredentials === true && value.allowOrigin === '*') {
if (value.allowCredentials === true && value.allowOrigin.includes('*')) {
return 'Cannot specify wildcard origin "*" with "credentials: true". Please provide a list of allowed origins.';
}
},
Expand Down Expand Up @@ -169,7 +172,7 @@ export class HttpConfig {
public cors: {
enabled: boolean;
allowCredentials: boolean;
allowOrigin: '*' | string[];
allowOrigin: string[];
};
public customResponseHeaders: Record<string, string | string[]>;
public maxPayload: ByteSizeValue;
Expand Down
4 changes: 2 additions & 2 deletions src/core/server/http/http_tools.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ describe('getServerOptions', () => {
cors: {
enabled: true,
allowCredentials: false,
allowOrigin: '*',
allowOrigin: ['*'],
},
}),
{} as any,
Expand All @@ -206,7 +206,7 @@ describe('getServerOptions', () => {

expect(getServerOptions(httpConfig).routes?.cors).toEqual({
credentials: false,
origin: '*',
origin: ['*'],
headers: ['Accept', 'Authorization', 'Content-Type', 'If-None-Match', 'kbn-xsrf'],
});
});
Expand Down

0 comments on commit c764de2

Please sign in to comment.