|
| 1 | +// Copyright 2024 The Chromium Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import * as Protocol from '../../generated/protocol.js'; |
| 6 | +import * as IssuesManager from '../../models/issues_manager/issues_manager.js'; |
| 7 | +import {createFakeSetting, createTarget} from '../../testing/EnvironmentHelpers.js'; |
| 8 | +import {describeWithMockConnection} from '../../testing/MockConnection.js'; |
| 9 | + |
| 10 | +import * as Security from './security.js'; |
| 11 | + |
| 12 | +function getTestCookieIssue( |
| 13 | + readCookie?: boolean, exclusionReason?: Protocol.Audits.CookieExclusionReason, |
| 14 | + warningReason?: Protocol.Audits.CookieWarningReason, cookieName?: string): Protocol.Audits.InspectorIssue { |
| 15 | + // if no exclusion or warning reason provided, use a default |
| 16 | + if (!exclusionReason && !warningReason) { |
| 17 | + exclusionReason = Protocol.Audits.CookieExclusionReason.ExcludeThirdPartyPhaseout; |
| 18 | + } |
| 19 | + |
| 20 | + return { |
| 21 | + code: Protocol.Audits.InspectorIssueCode.CookieIssue, |
| 22 | + details: { |
| 23 | + cookieIssueDetails: { |
| 24 | + cookie: { |
| 25 | + name: cookieName + 'test', |
| 26 | + path: '/', |
| 27 | + domain: 'a.' + cookieName + 'test', |
| 28 | + }, |
| 29 | + cookieExclusionReasons: exclusionReason ? [exclusionReason] : [], |
| 30 | + cookieWarningReasons: warningReason ? [warningReason] : [], |
| 31 | + operation: readCookie ? Protocol.Audits.CookieOperation.ReadCookie : Protocol.Audits.CookieOperation.SetCookie, |
| 32 | + cookieUrl: 'a.' + cookieName + 'test', |
| 33 | + }, |
| 34 | + }, |
| 35 | + }; |
| 36 | +} |
| 37 | + |
| 38 | +describeWithMockConnection('CookieReportView', () => { |
| 39 | + let mockView: sinon.SinonStub; |
| 40 | + |
| 41 | + beforeEach(() => { |
| 42 | + mockView = sinon.stub(); |
| 43 | + createTarget(); |
| 44 | + const showThirdPartyIssuesSetting = createFakeSetting('third party flag', true); |
| 45 | + IssuesManager.IssuesManager.IssuesManager.instance({ |
| 46 | + forceNew: false, |
| 47 | + ensureFirst: false, |
| 48 | + showThirdPartyIssuesSetting, |
| 49 | + }); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should contain no rows if no issues were created', async () => { |
| 53 | + const view = new Security.CookieReportView.CookieReportView(undefined, mockView); |
| 54 | + |
| 55 | + assert.strictEqual(view.gridData.length, 0); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should have row when there was a preexisting cookie issue', async () => { |
| 59 | + // @ts-ignore |
| 60 | + globalThis.addIssueForTest(getTestCookieIssue()); |
| 61 | + |
| 62 | + const view = new Security.CookieReportView.CookieReportView(undefined, mockView); |
| 63 | + await view.pendingUpdate(); |
| 64 | + |
| 65 | + assert.strictEqual(view.gridData.length, 1); |
| 66 | + }); |
| 67 | + |
| 68 | + it('should add row when issue added after view creation', async () => { |
| 69 | + const view = new Security.CookieReportView.CookieReportView(undefined, mockView); |
| 70 | + |
| 71 | + await view.pendingUpdate(); |
| 72 | + assert.strictEqual(view.gridData.length, 0); |
| 73 | + |
| 74 | + // @ts-ignore |
| 75 | + globalThis.addIssueForTest(getTestCookieIssue()); |
| 76 | + |
| 77 | + await view.pendingUpdate(); |
| 78 | + assert.strictEqual(view.gridData.length, 1); |
| 79 | + }); |
| 80 | + |
| 81 | + it('should ignore non-third-party-cookie related exclusionReason', async () => { |
| 82 | + const view = new Security.CookieReportView.CookieReportView(undefined, mockView); |
| 83 | + |
| 84 | + // @ts-ignore |
| 85 | + globalThis.addIssueForTest( |
| 86 | + getTestCookieIssue(undefined, Protocol.Audits.CookieExclusionReason.ExcludeSameSiteNoneInsecure)); |
| 87 | + |
| 88 | + await view.pendingUpdate(); |
| 89 | + assert.strictEqual(view.gridData.length, 0); |
| 90 | + |
| 91 | + // Make sure ExcludeThirdPartyPhaseout (default) is added. |
| 92 | + // @ts-ignore |
| 93 | + globalThis.addIssueForTest(getTestCookieIssue()); |
| 94 | + |
| 95 | + await view.pendingUpdate(); |
| 96 | + assert.strictEqual(view.gridData.length, 1); |
| 97 | + assert.strictEqual(view.gridData[0].data.status, 'Blocked'); |
| 98 | + }); |
| 99 | + |
| 100 | + it('should ignore non-third-party-cookie related warningReason', async () => { |
| 101 | + const view = new Security.CookieReportView.CookieReportView(undefined, mockView); |
| 102 | + |
| 103 | + // @ts-ignore |
| 104 | + globalThis.addIssueForTest( |
| 105 | + getTestCookieIssue(undefined, undefined, Protocol.Audits.CookieWarningReason.WarnSameSiteLaxCrossDowngradeLax)); |
| 106 | + |
| 107 | + await view.pendingUpdate(); |
| 108 | + assert.strictEqual(view.gridData.length, 0); |
| 109 | + |
| 110 | + // Make sure warning 3pc warning reasons are added |
| 111 | + // @ts-ignore |
| 112 | + globalThis.addIssueForTest(getTestCookieIssue( |
| 113 | + undefined, undefined, Protocol.Audits.CookieWarningReason.WarnDeprecationTrialMetadata, 'metadata')); |
| 114 | + // @ts-ignore |
| 115 | + globalThis.addIssueForTest(getTestCookieIssue( |
| 116 | + undefined, undefined, Protocol.Audits.CookieWarningReason.WarnThirdPartyCookieHeuristic, 'heuristic')); |
| 117 | + // @ts-ignore |
| 118 | + globalThis.addIssueForTest(getTestCookieIssue( |
| 119 | + undefined, undefined, Protocol.Audits.CookieWarningReason.WarnThirdPartyPhaseout, 'phaseout')); |
| 120 | + |
| 121 | + await view.pendingUpdate(); |
| 122 | + assert.strictEqual(view.gridData.length, 3); |
| 123 | + assert.strictEqual(view.gridData[0].data.status, 'Allowed By Exception'); |
| 124 | + assert.strictEqual(view.gridData[1].data.status, 'Allowed By Exception'); |
| 125 | + assert.strictEqual(view.gridData[2].data.status, 'Allowed'); |
| 126 | + }); |
| 127 | + |
| 128 | + it('should only have a single entry for same cookie with a read and a write operations', async () => { |
| 129 | + const view = new Security.CookieReportView.CookieReportView(undefined, mockView); |
| 130 | + |
| 131 | + // @ts-ignore |
| 132 | + globalThis.addIssueForTest(getTestCookieIssue(true)); |
| 133 | + // @ts-ignore |
| 134 | + globalThis.addIssueForTest(getTestCookieIssue(false)); |
| 135 | + |
| 136 | + await view.pendingUpdate(); |
| 137 | + assert.strictEqual(view.gridData.length, 1); |
| 138 | + }); |
| 139 | +}); |
0 commit comments