Skip to content

Commit

Permalink
Detect cookies on login page (elastic#120944)
Browse files Browse the repository at this point in the history
# Conflicts:
#	x-pack/plugins/security/public/authentication/login/login_app.ts
  • Loading branch information
thomheymann committed Dec 21, 2021
1 parent 3ee6fa4 commit f34947b
Show file tree
Hide file tree
Showing 7 changed files with 293 additions and 7 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Expand Up @@ -18,7 +18,7 @@ describe('loginApp', () => {

loginApp.create({
...coreSetupMock,
config: { loginAssistanceMessage: '' },
config: { loginAssistanceMessage: '', sameSiteCookies: undefined },
});

expect(coreSetupMock.http.anonymousPaths.register).toHaveBeenCalledTimes(1);
Expand All @@ -44,7 +44,7 @@ describe('loginApp', () => {

loginApp.create({
...coreSetupMock,
config: { loginAssistanceMessage: 'some-message' },
config: { loginAssistanceMessage: 'some-message', sameSiteCookies: undefined },
});

const [[{ mount }]] = coreSetupMock.application.register.mock.calls;
Expand Down
Expand Up @@ -19,7 +19,7 @@ interface CreateDeps {
application: ApplicationSetup;
http: HttpSetup;
getStartServices: StartServicesAccessor;
config: Pick<ConfigType, 'loginAssistanceMessage'>;
config: Pick<ConfigType, 'loginAssistanceMessage' | 'sameSiteCookies'>;
}

export const loginApp = Object.freeze({
Expand All @@ -41,6 +41,7 @@ export const loginApp = Object.freeze({
notifications: coreStart.notifications,
fatalErrors: coreStart.fatalErrors,
loginAssistanceMessage: config.loginAssistanceMessage,
sameSiteCookies: config.sameSiteCookies,
});
},
});
Expand Down
Expand Up @@ -5,6 +5,7 @@
* 2.0.
*/

import { EuiFlexItem } from '@elastic/eui';
import { act } from '@testing-library/react';
import { shallow } from 'enzyme';
import React from 'react';
Expand Down Expand Up @@ -75,6 +76,20 @@ describe('LoginPage', () => {
});

describe('disabled form states', () => {
const originalNavigator = window.navigator;
const originalTop = window.top;

afterEach(function () {
Object.defineProperty(window, 'navigator', {
value: originalNavigator,
writable: true,
});
Object.defineProperty(window, 'top', {
value: originalTop,
writable: true,
});
});

it('renders as expected when secure connection is required but not present', async () => {
const coreStartMock = coreMock.createStart();
httpMock.get.mockResolvedValue(createLoginState({ requiresSecureConnection: true }));
Expand Down Expand Up @@ -183,6 +198,94 @@ describe('LoginPage', () => {

expect(wrapper.find(DisabledLoginForm)).toMatchSnapshot();
});

it('renders CTA and cross-origin cookie warning when cookies are disabled, document is embedded inside iframe, and cross-origin cookies are blocked', async () => {
const coreStartMock = coreMock.createStart();
httpMock.get.mockResolvedValue(createLoginState());

Object.defineProperty(window, 'navigator', {
value: { cookieEnabled: false },
writable: true,
});
Object.defineProperty(window, 'top', {
value: {},
writable: true,
});

const wrapper = shallow(
<LoginPage
http={httpMock}
notifications={coreStartMock.notifications}
fatalErrors={coreStartMock.fatalErrors}
loginAssistanceMessage=""
sameSiteCookies="Lax"
/>
);

await act(async () => {
await nextTick();
wrapper.update();
});

expect(wrapper.find(EuiFlexItem).children()).toMatchSnapshot();
});

it('renders CTA and browser settings warning when cookies are disabled, document is embedded inside iframe, and cross-origin cookies are allowed', async () => {
const coreStartMock = coreMock.createStart();
httpMock.get.mockResolvedValue(createLoginState());

Object.defineProperty(window, 'navigator', {
value: { cookieEnabled: false },
writable: true,
});
Object.defineProperty(window, 'top', {
value: {},
writable: true,
});

const wrapper = shallow(
<LoginPage
http={httpMock}
notifications={coreStartMock.notifications}
fatalErrors={coreStartMock.fatalErrors}
loginAssistanceMessage=""
sameSiteCookies="None"
/>
);

await act(async () => {
await nextTick();
wrapper.update();
});

expect(wrapper.find(EuiFlexItem).children()).toMatchSnapshot();
});

it('renders warning when cookies are disabled and document is not embedded inside iframe', async () => {
const coreStartMock = coreMock.createStart();
httpMock.get.mockResolvedValue(createLoginState());

Object.defineProperty(window, 'navigator', {
value: { cookieEnabled: false },
writable: true,
});

const wrapper = shallow(
<LoginPage
http={httpMock}
notifications={coreStartMock.notifications}
fatalErrors={coreStartMock.fatalErrors}
loginAssistanceMessage=""
/>
);

await act(async () => {
await nextTick();
wrapper.update();
});

expect(wrapper.find(DisabledLoginForm)).toMatchSnapshot();
});
});

describe('enabled form state', () => {
Expand Down

0 comments on commit f34947b

Please sign in to comment.