Skip to content

Commit

Permalink
allow kbn-xsrf headers to be set on CORS request
Browse files Browse the repository at this point in the history
  • Loading branch information
mshustov committed Dec 8, 2020
1 parent d0956f8 commit 3e19081
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
19 changes: 19 additions & 0 deletions src/core/server/http/http_tools.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,25 @@ describe('getServerOptions', () => {
}
`);
});

it('properly configures CORS when cors enabled', () => {
const httpConfig = new HttpConfig(
config.schema.validate({
cors: {
enabled: true,
credentials: false,
origin: '*',
},
}),
{} as any
);

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

describe('getRequestId', () => {
Expand Down
2 changes: 2 additions & 0 deletions src/core/server/http/http_tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import uuid from 'uuid';
import { HttpConfig } from './http_config';
import { validateObject } from './prototype_pollution';

const corsAllowedHeaders = ['Accept', 'Authorization', 'Content-Type', 'If-None-Match', 'kbn-xsrf'];
/**
* Converts Kibana `HttpConfig` into `ServerOptions` that are accepted by the Hapi server.
*/
Expand All @@ -40,6 +41,7 @@ export function getServerOptions(config: HttpConfig, { configureTLS = true } = {
? {
credentials: config.cors.credentials,
origin: config.cors.origin,
headers: corsAllowedHeaders,
}
: false;
// Note that all connection options configured here should be exactly the same
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ function renderBody(kibanaUrl: string) {
</head>
<script>
fetch('${url}', {
method: 'POST',
headers: {
Authorization: 'Basic ${apiToken}',
'kbn-xsrf': 'kibana',
},
credentials: 'include',
})
Expand All @@ -47,7 +49,7 @@ export class CorsTestPlugin implements Plugin {

async setup(core: CoreSetup) {
const router = core.http.createRouter();
router.get({ path: '/cors-test', validate: false }, (_context, req, res) =>
router.post({ path: '/cors-test', validate: false }, (context, req, res) =>
res.ok({ body: 'content from kibana' })
);
}
Expand Down

0 comments on commit 3e19081

Please sign in to comment.