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

refactor: Refactor inpage blocklist to avoid usage of regex #8675

Merged
merged 2 commits into from
Mar 20, 2024
Merged
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
42 changes: 28 additions & 14 deletions scripts/inpage-bridge/content-script/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,31 +105,45 @@ function documentElementCheck() {
* @returns {boolean} {@code true} if the current domain is blocked
*/
function blockedDomainCheck() {
// If making any changes, please also update the same list found in the MetaMask-Mobile & SDK repositories
const blockedDomains = [
'execution.consensys.io',
'execution.metamask.io',
'uscourts.gov',
'dropbox.com',
'webbyawards.com',
'cdn.shopify.com/s/javascripts/tricorder/xtld-read-only-frame.html',
'adyen.com',
'gravityforms.com',
'harbourair.com',
'ani.gamer.com.tw',
'blueskybooking.com',
'sharefile.com',
'battle.net',
];
const currentUrl = window.location.href;
let currentRegex;
for (let i = 0; i < blockedDomains.length; i++) {
const blockedDomain = blockedDomains[i].replace('.', '\\.');
currentRegex = new RegExp(
`(?:https?:\\/\\/)(?:(?!${blockedDomain}).)*$`,
'u',
);
if (!currentRegex.test(currentUrl)) {
return true;
}
}
return false;

// Matching will happen based on the hostname, and path
const blockedUrlPaths = [
'cdn.shopify.com/s/javascripts/tricorder/xtld-read-only-frame.html',
];

const { hostname: currentHostname, pathname: currentPathname } =
window.location;

const trimTrailingSlash = (str) =>
str.endsWith('/') ? str.slice(0, -1) : str;

return (
blockedDomains.some(
(blockedDomain) =>
blockedDomain === currentHostname ||
currentHostname.endsWith(`.${blockedDomain}`),
) ||
blockedUrlPaths.some(
(blockedUrlPath) =>
trimTrailingSlash(blockedUrlPath) ===
trimTrailingSlash(currentHostname + currentPathname),
)
);
}

/**
Expand Down
Loading