-
Notifications
You must be signed in to change notification settings - Fork 914
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Separate xsrf handling and version checking
Traditionally we've relied on the kbn-version header for csrf protection, but that is also used for the client-side Kibana version check that alerts users when their client is out of date and needs to be refreshed, so it must match the version of Kibana exactly. This poses a problem for any programmatic access that would only get set up once but may run repeatedly throughout the future (e.g. watcher), so there's now the additional option of specifying the kbn-xsrf header instead. The value of the header does not matter, but its very presence is all that is necessary to avoid xsrf attacks. The xsrf protection just needs either one of these headers to exist.
- Loading branch information
Showing
5 changed files
with
131 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import expect from 'expect.js'; | ||
import { fromNode } from 'bluebird'; | ||
import { resolve } from 'path'; | ||
import * as kbnTestServer from '../../../../test/utils/kbn_server'; | ||
|
||
const src = resolve.bind(null, __dirname, '../../../../src'); | ||
|
||
const versionHeader = 'kbn-version'; | ||
const version = require(src('../package.json')).version; | ||
|
||
describe('version_check request filter', function () { | ||
function makeRequest(kbnServer, opts) { | ||
return fromNode(cb => { | ||
kbnTestServer.makeRequest(kbnServer, opts, (resp) => { | ||
cb(null, resp); | ||
}); | ||
}); | ||
} | ||
|
||
async function makeServer() { | ||
const kbnServer = kbnTestServer.createServer(); | ||
|
||
await kbnServer.ready(); | ||
|
||
kbnServer.server.route({ | ||
path: '/version_check/test/route', | ||
method: 'GET', | ||
handler: function (req, reply) { | ||
reply(null, 'ok'); | ||
} | ||
}); | ||
|
||
return kbnServer; | ||
}; | ||
|
||
let kbnServer; | ||
beforeEach(async () => kbnServer = await makeServer()); | ||
afterEach(async () => await kbnServer.close()); | ||
|
||
it('accepts requests with the correct version passed in the version header', async function () { | ||
const resp = await makeRequest(kbnServer, { | ||
url: '/version_check/test/route', | ||
method: 'GET', | ||
headers: { | ||
[versionHeader]: version, | ||
}, | ||
}); | ||
|
||
expect(resp.statusCode).to.be(200); | ||
expect(resp.payload).to.be('ok'); | ||
}); | ||
|
||
it('rejects requests with an incorrect version passed in the version header', async function () { | ||
const resp = await makeRequest(kbnServer, { | ||
url: '/version_check/test/route', | ||
method: 'GET', | ||
headers: { | ||
[versionHeader]: `invalid:${version}`, | ||
}, | ||
}); | ||
|
||
expect(resp.statusCode).to.be(400); | ||
expect(resp.headers).to.have.property(versionHeader, version); | ||
expect(resp.payload).to.match(/"Browser client is out of date/); | ||
}); | ||
|
||
it('accepts requests that do not include a version header', async function () { | ||
const resp = await makeRequest(kbnServer, { | ||
url: '/version_check/test/route', | ||
method: 'GET' | ||
}); | ||
|
||
expect(resp.statusCode).to.be(200); | ||
expect(resp.payload).to.be('ok'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { badRequest } from 'boom'; | ||
|
||
export default function (kbnServer, server, config) { | ||
const versionHeader = 'kbn-version'; | ||
const actualVersion = config.get('pkg.version'); | ||
|
||
server.ext('onPostAuth', function (req, reply) { | ||
const versionRequested = req.headers[versionHeader]; | ||
|
||
if (versionRequested && versionRequested !== actualVersion) { | ||
return reply(badRequest('Browser client is out of date, please refresh the page', { | ||
expected: actualVersion, | ||
got: versionRequested | ||
})); | ||
} | ||
|
||
return reply.continue(); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters