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

Protocol sanity check #16

Merged
merged 9 commits into from
Jul 12, 2022
Merged
Show file tree
Hide file tree
Changes from 6 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
6 changes: 3 additions & 3 deletions jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ const config: Config.InitialOptions = {
coverageThreshold: {
global: {
branches: 20,
functions: 50,
lines: 66,
statements: 66,
functions: 40,
lines: 62,
statements: 62,
},
},

Expand Down
19 changes: 18 additions & 1 deletion src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ describe('Phishing warning page', () => {
});

afterEach(() => {
onDomContentLoad = undefined;
adonesky1 marked this conversation as resolved.
Show resolved Hide resolved
// onDomContentLoad = undefined;
document.getElementsByTagName('html')[0].innerHTML = '';
});

Expand Down Expand Up @@ -103,6 +103,23 @@ describe('Phishing warning page', () => {
'should redirect to the site after the user continues at their own risk',
);

it('should show a different message if the URL contains an unsupported protocol', async () => {
/* eslint-disable-next-line */
mockLocation(getUrl('example.com', 'javascript:alert("example")'));

await import('./index');
// non-null assertion used because TypeScript doesn't know the event handler was run
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
onDomContentLoad!(new Event('DOMContentLoaded'));

const redirectWarningMessage = window.document.getElementById(
'redirect-warning-message',
);
expect(redirectWarningMessage?.innerText).toBe(
"This URL does not use a supported protocol so we won't give you the option to skip this warning.",
);
});

it('should throw an error if the hostname is missing', async () => {
mockLocation(getUrl(undefined, 'https://example.com'));

Expand Down
29 changes: 29 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,20 @@ function setupOpenSelfInNewTabLink() {
newTabLink.setAttribute('href', window.location.href);
}

/**
* Checks to see if the suspectHref is a valid format to forward on
* Specifically checks the protocol of the passed href.
*
* @param href - The href value to check.
* @returns Boolean on if its valid to attack to a href prop.
*/
function isValidSuspectHref(href: string) {
const allowedProtocols = ['http:', 'https:'];
const parsedSuspectHref = new URL(href);

return allowedProtocols.indexOf(parsedSuspectHref.protocol) !== -1;
}

/**
* Initialize the phishing warning page streams.
*/
Expand Down Expand Up @@ -120,7 +134,22 @@ function start() {
throw new Error('Unable to locate unsafe continue link');
}

if (isValidSuspectHref(suspectHref) === false) {
const redirectWarningMessage = document.getElementById(
'redirect-warning-message',
);
if (redirectWarningMessage) {
redirectWarningMessage.innerHTML = `<br />`;
redirectWarningMessage.innerText = `This URL does not use a supported protocol so we won't give you the option to skip this warning.`;
}
}

continueLink.addEventListener('click', async () => {
if (isValidSuspectHref(suspectHref) === false) {
console.log(`Disallowed Protocol, cannot continue.`);
adonesky1 marked this conversation as resolved.
Show resolved Hide resolved
return;
}

phishingSafelistStream.write({
jsonrpc: '2.0',
method: 'safelistPhishingDomain',
Expand Down
4 changes: 2 additions & 2 deletions static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ <h1>
Note that this warning list is compiled on a voluntary basis. This
list may be inaccurate or incomplete. Just because a domain does not
appear on this list is not an implicit guarantee of that domain's
safety. As always, your transactions are your own responsibility. If
safety. As always, your transactions are your own responsibility. <span id="redirect-warning-message">If
you wish to interact with any domain on our warning list, you can do
so by <a id="unsafe-continue">continuing at your own risk</a>.
so by <a id="unsafe-continue">continuing at your own risk</a>.</span>
</p>
<p>
If you think this domain is incorrectly flagged or if a blocked
Expand Down