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

Fix OIDC refresh token flow when using the cookie splitter #1580

Merged
Merged
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
65 changes: 65 additions & 0 deletions server/session/cookie_splitter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/
import { Request as HapiRequest, ResponseObject as HapiResponseObject } from '@hapi/hapi';
import { httpServerMock } from '../../../../src/core/server/http/http_server.mocks';
import { merge } from 'lodash';
import {
clearSplitCookies,
getExtraAuthStorageValue,
Expand Down Expand Up @@ -171,4 +172,68 @@ describe('Test extra auth storage', () => {

expect(unsplitValue).toEqual('abcdefghi');
});

test('should check for cookie values updated in the same request', async () => {
const cookiePrefix = 'testcookie';
const additionalCookies = 5;

const mockRequest = httpServerMock.createRawRequest();

const extendedMockRequest = merge(mockRequest, {
_states: {
[cookiePrefix + '1']: {
name: cookiePrefix + '1',
value: 'abc',
},
[cookiePrefix + '2']: {
name: cookiePrefix + '2',
value: 'def',
},
[cookiePrefix + '3']: {
name: cookiePrefix + '3',
value: 'ghi',
},
},
}) as HapiRequest;

const osRequest = OpenSearchDashboardsRequest.from(extendedMockRequest);
const unsplitValue = unsplitCookiesIntoValue(osRequest, cookiePrefix, additionalCookies);

expect(unsplitValue).toEqual('abcdefghi');
});

test('should not mix cookie values updated in the same request with previous cookie values', async () => {
const cookiePrefix = 'testcookie';
const additionalCookies = 5;

const mockRequest = httpServerMock.createRawRequest({
state: {
[cookiePrefix + '1']: 'abc',
[cookiePrefix + '2']: 'def',
[cookiePrefix + '3']: 'ghi',
},
});

const extendedMockRequest = merge(mockRequest, {
_states: {
[cookiePrefix + '1']: {
name: cookiePrefix + '1',
value: 'jkl',
},
[cookiePrefix + '2']: {
name: cookiePrefix + '2',
value: 'mno',
},
[cookiePrefix + '3']: {
name: cookiePrefix + '3',
value: 'pqr',
},
},
}) as HapiRequest;

const osRequest = OpenSearchDashboardsRequest.from(extendedMockRequest);
const unsplitValue = unsplitCookiesIntoValue(osRequest, cookiePrefix, additionalCookies);

expect(unsplitValue).toEqual('jklmnopqr');
});
});
24 changes: 22 additions & 2 deletions server/session/cookie_splitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ export interface ExtraAuthStorageOptions {

type CookieAuthWithResponseObject = HapiRequest['cookieAuth'] & { h: HapiResponseObject };

interface HapiStates {
[cookieName: string]: {
name: string;
value: string;
};
}

export type HapiRequestWithStates = HapiRequest & { _states: HapiStates };

export function getExtraAuthStorageValue(
request: OpenSearchDashboardsRequest,
options: ExtraAuthStorageOptions
Expand Down Expand Up @@ -134,12 +143,23 @@ export function unsplitCookiesIntoValue(
cookiePrefix: string,
additionalCookies: number
): string {
const rawRequest: HapiRequest = ensureRawRequest(request);
const rawRequest: HapiRequestWithStates = ensureRawRequest(request) as HapiRequestWithStates;
let fullCookieValue = '';

// We don't want to mix and match between _states and .state.
// If we find the first additional cookie in _states, we
// use _states for all subsequent additional cookies
const requestHasNewerCookieState = rawRequest._states && rawRequest._states[cookiePrefix + 1];
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@cwperks Here I deviated a bit from the original code that I posted in your PR.

I added a check to make sure that we don't mix and match between whatever is in '_states' and '.state'.
Probably just a theoretical risk, but if the new token would use less cookies than the previous one, the unsplit cookie would contain a mix between the two without this check in place. Hope that's ok.

Copy link
Member

Choose a reason for hiding this comment

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

This makes sense to me. We would not want to end up with a bad cookie caused due to mix-up.


for (let i = 1; i <= additionalCookies; i++) {
const cookieName = cookiePrefix + i;
if (rawRequest.state[cookieName]) {
if (
requestHasNewerCookieState &&
rawRequest._states[cookieName] &&
rawRequest._states[cookieName].value
) {
fullCookieValue = fullCookieValue + rawRequest._states[cookieName].value;
} else if (!requestHasNewerCookieState && rawRequest.state[cookieName]) {
fullCookieValue = fullCookieValue + rawRequest.state[cookieName];
}
}
Expand Down
Loading