[MM-52587] Clean up URL utils, use isInternalURL when possible (#2702)

This commit is contained in:
Devin Binnie
2023-05-03 08:48:41 -04:00
committed by GitHub
parent f3a4417464
commit e227c6bf1d
30 changed files with 481 additions and 634 deletions

View File

@@ -11,7 +11,6 @@ import {TrustedOrigin, PermissionType} from 'types/trustedOrigin';
import {UPDATE_PATHS} from 'common/communication';
import {Logger} from 'common/log';
import urlUtils from 'common/utils/url';
import * as Validator from 'common/Validator';
import {trustedOriginsStoreFile} from './constants';
@@ -64,7 +63,7 @@ export class TrustedOriginsStore {
// if permissions or targetUrl are invalid, this function will throw an error
// this function stablishes all the permissions at once, overwriting whatever was before
// to enable just one permission use addPermission instead.
set = (targetURL: string, permissions: Record<PermissionType, boolean>) => {
set = (targetURL: URL, permissions: Record<PermissionType, boolean>) => {
if (!this.data) {
return;
}
@@ -72,45 +71,29 @@ export class TrustedOriginsStore {
if (!validPermissions) {
throw new Error(`Invalid permissions set for trusting ${targetURL}`);
}
this.data.set(urlUtils.getHost(targetURL), validPermissions);
this.data.set(targetURL.origin, validPermissions);
};
// enables usage of `targetURL` for `permission`
addPermission = (targetURL: string, permission: PermissionType) => {
const origin = urlUtils.getHost(targetURL);
this.set(origin, {[permission]: true});
addPermission = (targetURL: URL, permission: PermissionType) => {
this.set(targetURL, {[permission]: true});
}
delete = (targetURL: string) => {
let host;
try {
host = urlUtils.getHost(targetURL);
this.data?.delete(host);
} catch {
return false;
}
return true;
delete = (targetURL: URL) => {
return this.data?.delete(targetURL.origin);
}
isExisting = (targetURL: string) => {
return this.data?.has(urlUtils.getHost(targetURL)) || false;
isExisting = (targetURL: URL) => {
return this.data?.has(targetURL.origin) || false;
};
// if user hasn't set his preferences, it will return null (falsy)
checkPermission = (targetURL: string, permission: PermissionType) => {
checkPermission = (targetURL: URL, permission: PermissionType) => {
if (!permission) {
log.error(`Missing permission request on ${targetURL}`);
return null;
}
let origin;
try {
origin = urlUtils.getHost(targetURL);
} catch (e) {
log.error(`invalid host to retrieve permissions: ${targetURL}: ${e}`);
return null;
}
const urlPermissions = this.data?.get(origin);
const urlPermissions = this.data?.get(targetURL.origin);
return urlPermissions ? urlPermissions[permission] : undefined;
}
}