forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request elastic#8151 from epixa/headercheck
Separate xsrf handling and version checking
- 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