Prevent phishing validation bypass via credentialed URLs using tldts (#1560)

* feat: add tldts dependency and update handler to extract hostname from sender URL

* feat: add URL parsing functionality to improve sender URL extraction
This commit is contained in:
Arjun Porwal 2025-06-24 02:43:47 +05:30 committed by GitHub
parent dcb3543441
commit 824cda7e88
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 23 additions and 1 deletions

View File

@ -36,6 +36,7 @@
"@polkadot/util-crypto": "^13.5.2", "@polkadot/util-crypto": "^13.5.2",
"eventemitter3": "^5.0.1", "eventemitter3": "^5.0.1",
"rxjs": "^7.8.1", "rxjs": "^7.8.1",
"tldts": "^7.0.8",
"tslib": "^2.8.1" "tslib": "^2.8.1"
}, },
"devDependencies": { "devDependencies": {

View File

@ -13,6 +13,7 @@ import type { AuthResponse } from './State.js';
import type State from './State.js'; import type State from './State.js';
import { combineLatest, type Subscription } from 'rxjs'; import { combineLatest, type Subscription } from 'rxjs';
import { parse } from 'tldts';
import { checkIfDenied } from '@polkadot/phishing'; import { checkIfDenied } from '@polkadot/phishing';
import { keyring } from '@polkadot/ui-keyring'; import { keyring } from '@polkadot/ui-keyring';
@ -215,6 +216,22 @@ export default class Tabs {
}); });
} }
private parseUrl (rawUrl: string): string {
let from = 'extension';
if (rawUrl) {
try {
const { hostname } = parse(rawUrl);
from = hostname || '<unknown>'; // Only use the hostname
} catch {
from = '<unknown>';
}
}
return from;
}
private async redirectIfPhishing (url: string): Promise<boolean> { private async redirectIfPhishing (url: string): Promise<boolean> {
const isInDenyList = await checkIfDenied(url); const isInDenyList = await checkIfDenied(url);
@ -229,7 +246,9 @@ export default class Tabs {
public async handle<TMessageType extends MessageTypes> (id: string, type: TMessageType, request: RequestTypes[TMessageType], url: string, port?: chrome.runtime.Port): Promise<ResponseTypes[keyof ResponseTypes]> { public async handle<TMessageType extends MessageTypes> (id: string, type: TMessageType, request: RequestTypes[TMessageType], url: string, port?: chrome.runtime.Port): Promise<ResponseTypes[keyof ResponseTypes]> {
if (type === 'pub(phishing.redirectIfDenied)') { if (type === 'pub(phishing.redirectIfDenied)') {
return this.redirectIfPhishing(url); const parsedUrl = this.parseUrl(url);
return this.redirectIfPhishing(parsedUrl);
} }
if (type !== 'pub(authorize.tab)') { if (type !== 'pub(authorize.tab)') {

View File

@ -31,6 +31,7 @@ export default function handler<TMessageType extends MessageTypes> ({ id, messag
const from = isExtension const from = isExtension
? 'extension' ? 'extension'
: sender?.url || sender?.tab?.url || '<unknown>'; : sender?.url || sender?.tab?.url || '<unknown>';
const source = `${from}: ${id}: ${message}`; const source = `${from}: ${id}: ${message}`;
console.log(` [in] ${source}`); // :: ${JSON.stringify(request)}`); console.log(` [in] ${source}`); // :: ${JSON.stringify(request)}`);

View File

@ -944,6 +944,7 @@ __metadata:
"@polkadot/util-crypto": "npm:^13.5.2" "@polkadot/util-crypto": "npm:^13.5.2"
eventemitter3: "npm:^5.0.1" eventemitter3: "npm:^5.0.1"
rxjs: "npm:^7.8.1" rxjs: "npm:^7.8.1"
tldts: "npm:^7.0.8"
tslib: "npm:^2.8.1" tslib: "npm:^2.8.1"
languageName: unknown languageName: unknown
linkType: soft linkType: soft