Skip to content

Commit

Permalink
Make sure redirect URL fragment always starts with #.
Browse files Browse the repository at this point in the history
  • Loading branch information
azasypkin committed Oct 9, 2019
1 parent 0a86b77 commit 8b51a1c
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,44 @@ describe('SAMLAuthenticationProvider', () => {
});
});

it('prepends redirect URL fragment with `#` if it does not have one.', async () => {
const request = httpServerMock.createKibanaRequest();

mockOptions.client.callAsInternalUser.withArgs('shield.samlPrepare').resolves({
id: 'some-request-id',
redirect: 'https://idp-host/path/login?SAMLRequest=some%20request%20',
});

const authenticationResult = await provider.login(
request,
{
step: SAMLLoginStep.RedirectURLFragmentCaptured,
redirectURLFragment: '../some-fragment',
},
{ redirectURL: '/test-base-path/some-path' }
);

sinon.assert.calledWithExactly(
mockOptions.client.callAsInternalUser,
'shield.samlPrepare',
{ body: { realm: 'test-realm' } }
);

expect(mockOptions.logger.warn).toHaveBeenCalledTimes(1);
expect(mockOptions.logger.warn).toHaveBeenCalledWith(
'Redirect URL fragment does not start with `#`.'
);

expect(authenticationResult.redirected()).toBe(true);
expect(authenticationResult.redirectURL).toBe(
'https://idp-host/path/login?SAMLRequest=some%20request%20'
);
expect(authenticationResult.state).toEqual({
requestId: 'some-request-id',
redirectURL: '/test-base-path/some-path#../some-fragment',
});
});

it('redirects non-AJAX requests to the IdP remembering only redirect URL path if fragment is too large.', async () => {
const request = httpServerMock.createKibanaRequest();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,13 @@ export class SAMLAuthenticationProvider extends BaseAuthenticationProvider {
return AuthenticationResult.failed(Boom.badRequest(message));
}

let redirectURL = `${state.redirectURL}${attempt.redirectURLFragment}`;
let redirectURLFragment = attempt.redirectURLFragment;
if (redirectURLFragment.length > 0 && !redirectURLFragment.startsWith('#')) {
this.logger.warn('Redirect URL fragment does not start with `#`.');
redirectURLFragment = `#${redirectURLFragment}`;
}

let redirectURL = `${state.redirectURL}${redirectURLFragment}`;
const redirectURLSize = new ByteSizeValue(Buffer.byteLength(redirectURL));
if (this.maxRedirectURLSize.isLessThan(redirectURLSize)) {
this.logger.warn(
Expand Down

0 comments on commit 8b51a1c

Please sign in to comment.