|
| 1 | +import { test, expect } from '@playwright/test' |
| 2 | +import { readFileSync } from 'fs' |
| 3 | +import { mockWindowsMessaging, wrapWindowsScripts } from '@duckduckgo/messaging/lib/test-utils.mjs' |
| 4 | +import { perPlatform } from './type-helpers.mjs' |
| 5 | + |
| 6 | +test('Harmful APIs protections', async ({ page }, testInfo) => { |
| 7 | + const protection = HarmfulApisSpec.create(page, testInfo) |
| 8 | + await protection.enabled() |
| 9 | + const results = await protection.runTests(); |
| 10 | + // note that if protections are disabled, the browser will show a device selection pop-up, which will never be dismissed |
| 11 | + |
| 12 | + [ |
| 13 | + 'deviceOrientation', |
| 14 | + 'GenericSensor', |
| 15 | + 'UaClientHints', |
| 16 | + 'NetworkInformation', |
| 17 | + 'getInstalledRelatedApps', |
| 18 | + 'FileSystemAccess', |
| 19 | + 'WindowPlacement', |
| 20 | + 'WebBluetooth', |
| 21 | + 'WebUsb', |
| 22 | + 'WebSerial', |
| 23 | + 'WebHid', |
| 24 | + 'WebMidi', |
| 25 | + 'IdleDetection', |
| 26 | + 'WebNfc', |
| 27 | + 'StorageManager' |
| 28 | + ].forEach((name) => { |
| 29 | + for (const result of results[name]) { |
| 30 | + expect(result.result).toEqual(result.expected) |
| 31 | + } |
| 32 | + }) |
| 33 | +}) |
| 34 | + |
| 35 | +export class HarmfulApisSpec { |
| 36 | + htmlPage = '/harmful-apis/index.html' |
| 37 | + config = './integration-test/test-pages/harmful-apis/config/apis.json' |
| 38 | + |
| 39 | + /** |
| 40 | + * @param {import("@playwright/test").Page} page |
| 41 | + * @param {import("./type-helpers.mjs").Build} build |
| 42 | + * @param {import("./type-helpers.mjs").PlatformInfo} platform |
| 43 | + */ |
| 44 | + constructor (page, build, platform) { |
| 45 | + this.page = page |
| 46 | + this.build = build |
| 47 | + this.platform = platform |
| 48 | + } |
| 49 | + |
| 50 | + async enabled () { |
| 51 | + await this.installPolyfills() |
| 52 | + const config = JSON.parse(readFileSync(this.config, 'utf8')) |
| 53 | + await this.setup({ config }) |
| 54 | + await this.page.goto(this.htmlPage) |
| 55 | + } |
| 56 | + |
| 57 | + async runTests () { |
| 58 | + for (const button of await this.page.getByTestId('user-gesture-button').all()) { |
| 59 | + await button.click() |
| 60 | + } |
| 61 | + const resultsPromise = this.page.evaluate(() => { |
| 62 | + return new Promise(resolve => { |
| 63 | + window.addEventListener('results-ready', () => { |
| 64 | + // @ts-expect-error - this is added by the test framework |
| 65 | + resolve(window.results) |
| 66 | + }) |
| 67 | + }) |
| 68 | + }) |
| 69 | + await this.page.getByTestId('render-results').click() |
| 70 | + return await resultsPromise |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * In CI, the global objects such as USB might not be installed on the |
| 75 | + * version of chromium running there. |
| 76 | + */ |
| 77 | + async installPolyfills () { |
| 78 | + await this.page.addInitScript(() => { |
| 79 | + // @ts-expect-error - testing |
| 80 | + if (typeof Bluetooth === 'undefined') { |
| 81 | + globalThis.Bluetooth = {} |
| 82 | + globalThis.Bluetooth.prototype = { requestDevice: async () => { /* noop */ } } |
| 83 | + } |
| 84 | + // @ts-expect-error - testing |
| 85 | + if (typeof USB === 'undefined') { |
| 86 | + globalThis.USB = {} |
| 87 | + globalThis.USB.prototype = { requestDevice: async () => { /* noop */ } } |
| 88 | + } |
| 89 | + |
| 90 | + // @ts-expect-error - testing |
| 91 | + if (typeof Serial === 'undefined') { |
| 92 | + globalThis.Serial = {} |
| 93 | + globalThis.Serial.prototype = { requestPort: async () => { /* noop */ } } |
| 94 | + } |
| 95 | + // @ts-expect-error - testing |
| 96 | + if (typeof HID === 'undefined') { |
| 97 | + globalThis.HID = {} |
| 98 | + globalThis.HID.prototype = { requestDevice: async () => { /* noop */ } } |
| 99 | + } |
| 100 | + }) |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * @param {object} params |
| 105 | + * @param {Record<string, any>} params.config |
| 106 | + * @return {Promise<void>} |
| 107 | + */ |
| 108 | + async setup (params) { |
| 109 | + const { config } = params |
| 110 | + |
| 111 | + // read the built file from disk and do replacements |
| 112 | + const injectedJS = wrapWindowsScripts(this.build.artifact, { |
| 113 | + $CONTENT_SCOPE$: config, |
| 114 | + $USER_UNPROTECTED_DOMAINS$: [], |
| 115 | + $USER_PREFERENCES$: { |
| 116 | + platform: { name: 'windows' }, |
| 117 | + debug: true |
| 118 | + } |
| 119 | + }) |
| 120 | + |
| 121 | + await this.page.addInitScript(mockWindowsMessaging, { |
| 122 | + messagingContext: { |
| 123 | + env: 'development', |
| 124 | + context: 'contentScopeScripts', |
| 125 | + featureName: 'n/a' |
| 126 | + }, |
| 127 | + responses: {} |
| 128 | + }) |
| 129 | + |
| 130 | + // attach the JS |
| 131 | + await this.page.addInitScript(injectedJS) |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * Helper for creating an instance per platform |
| 136 | + * @param {import("@playwright/test").Page} page |
| 137 | + * @param {import("@playwright/test").TestInfo} testInfo |
| 138 | + */ |
| 139 | + static create (page, testInfo) { |
| 140 | + // Read the configuration object to determine which platform we're testing against |
| 141 | + const { platformInfo, build } = perPlatform(testInfo.project.use) |
| 142 | + return new HarmfulApisSpec(page, build, platformInfo) |
| 143 | + } |
| 144 | +} |
0 commit comments