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(18194): fix trigger to close window when clicking "back to safety" #84

Merged
merged 4 commits into from
Mar 30, 2023
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ node_modules/
# Stores VSCode versions used for testing VSCode extensions
.vscode-test

# intellij config files
/.idea

# yarn v3 (w/o zero-install)
# See: https://yarnpkg.com/getting-started/qa#which-files-should-be-gitignored
.pnp.*
Expand Down
16 changes: 15 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ function start() {
}

continueLink.addEventListener('click', async () => {
if (isValidSuspectHref(suspectHref) === false) {
if (!isValidSuspectHref(suspectHref)) {
console.log(`Disallowed Protocol, cannot continue.`);
return;
}
Expand All @@ -208,4 +208,18 @@ function start() {

window.location.href = suspectHref;
});

const backToSafetyLink = document.getElementById('back-to-safety');
if (!backToSafetyLink) {
throw new Error('Unable to locate back to safety link');
}

backToSafetyLink.addEventListener('click', async () => {
phishingSafelistStream.write({
jsonrpc: '2.0',
method: 'backToSafetyPhishingWarning',
params: [],
id: createRandomId(),
});
});
}
2 changes: 1 addition & 1 deletion static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ <h1>
</br>
<p>If we're flagging a legitimate website, please <a id="new-issue-link" href="#">report a detection problem.</a></p>
<p>If you understand the risks and still want to proceed, you can <a id="unsafe-continue">continue to the site.</a></p>
<button class="button-secondary" type="submit" onclick="window.close()">Back to safety</button>
<button class="button-secondary" type="submit" id="back-to-safety">Back to safety</button>
</div>
<div id="content__framed-body" class="content__framed-body">
<p>
Expand Down
33 changes: 19 additions & 14 deletions tests/defaults.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { test, expect } from '@playwright/test';
import config from '../playwright.config';
import { setupDefaultMocks } from './helpers/default-mocks';
import { setupStreamInitialization } from './helpers/stream-initialization';

test.beforeEach(async ({ context }) => {
await setupDefaultMocks(context);
Expand Down Expand Up @@ -55,20 +55,25 @@ test('does nothing when the user tries to bypass the warning', async ({
await expect(page.isClosed()).toBe(false);
});

test('closes the window when the user clicks "Back to safety"', async ({
page,
}) => {
// Calling `replace` here instead of `goto` to ensure that the page loads with a browser history
// of length 1. Using `goto` would result in a length of 2, because Playwright starts each page
// on `about:blank`.
// We need a 1-length history so that the browser allows `window.close()` to work.
const baseURL = config.use?.baseURL;
await page.evaluate((url) => window.location.replace(url), `${baseURL}/`);

const onClose = page.waitForEvent('close');
await page.getByRole('button', { name: 'Back to safety' }).click();
test('redirects when the user clicks "Back to safety"', async ({ page }) => {
const postMessageLogs = await setupStreamInitialization(page);
const querystring = new URLSearchParams({
hostname: 'test.com',
href: 'https://test.com',
});
await page.goto(`/#${querystring}`);

await onClose;
await page.getByRole('button', { name: 'Back to safety' }).click();
await expect(postMessageLogs.length).toBe(1);
await expect(postMessageLogs[0].message).toStrictEqual({
data: {
id: expect.any(Number),
jsonrpc: '2.0',
method: 'backToSafetyPhishingWarning',
params: [],
},
name: 'metamask-phishing-safelist',
});
});

test('logs that the service worker is registered', async ({ page }) => {
Expand Down