Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properly handle very long URL fragments captured during SAML handshake. #53505

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions x-pack/plugins/security/server/routes/authentication/saml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,14 @@ import { RouteDefinitionParams } from '..';
/**
* Defines routes required for SAML authentication.
*/
export function defineSAMLRoutes({ router, logger, authc, csp, basePath }: RouteDefinitionParams) {
export function defineSAMLRoutes({
router,
logger,
authc,
csp,
basePath,
config,
}: RouteDefinitionParams) {
router.get(
{
path: '/api/security/saml/capture-url-fragment',
Expand Down Expand Up @@ -43,11 +50,15 @@ export function defineSAMLRoutes({ router, logger, authc, csp, basePath }: Route
options: { authRequired: false },
},
(context, request, response) => {
// NodeJS limits the maximum size of the Request-Line + all HTTP headers to 8KB.
// See https://nodejs.org/api/cli.html#cli_max_http_header_size_size.
const maxRedirectURLSize = config.authc.saml?.maxRedirectURLSize.getValueInBytes() ?? 4096;
Copy link
Member Author

@azasypkin azasypkin Dec 18, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: it's technically possible to call this endpoint even if SAML is not enabled (no use case for that yet), so that config isn't available. I thought not throwing error would be better, but I don't have strong opinion here.

Technically we can just hardcode any number here and not rely on config at all (since it's tied to NodeJS limit and not to what set through config). Curious what you have in mind?

return response.custom(
createCustomResourceResponse(
`
const hash = encodeURIComponent(window.location.hash);
window.location.replace(
'${basePath.serverBasePath}/api/security/saml/start?redirectURLFragment=' + encodeURIComponent(window.location.hash)
'${basePath.serverBasePath}/api/security/saml/start?redirectURLFragment=' + (hash.length < ${maxRedirectURLSize} ? hash : '')
);
`,
'text/javascript',
Expand Down
33 changes: 33 additions & 0 deletions x-pack/test/saml_api_integration/apis/security/saml_login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,39 @@ export default function({ getService }: FtrProviderContext) {
'/api/security/saml/start?redirectURLFragment=%23%2Fworkpad'
);
});

it('does not send URL fragments that exceed size limit to the server', async () => {
const response = await supertest.get('/api/security/saml/capture-url-fragment').expect(200);

const kibanaBaseURL = url.format({ ...config.get('servers.kibana'), auth: false });
const dom = new JSDOM(response.text, {
url: kibanaBaseURL,
runScripts: 'dangerously',
resources: 'usable',
beforeParse(window) {
// JSDOM doesn't support changing of `window.location` and throws an exception if script
// tries to do that and we have to workaround this behaviour. We also need to wait until our
// script is loaded and executed, __isScriptExecuted__ is used exactly for that.
(window as Record<string, any>).__isScriptExecuted__ = new Promise(resolve => {
const hash = '#/workpad'.repeat(10);
Object.defineProperty(window, 'location', {
value: {
hash,
href: `${kibanaBaseURL}/api/security/saml/capture-url-fragment${hash}`,
replace(newLocation: string) {
this.href = newLocation;
resolve();
},
},
});
});
},
});

await (dom.window as Record<string, any>).__isScriptExecuted__;

expect(dom.window.location.href).to.be('/api/security/saml/start?redirectURLFragment=');
});
});

describe('initiating handshake', () => {
Expand Down