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

Origin in per domain rules #498

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
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
7 changes: 5 additions & 2 deletions src/_locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,11 @@
"optionsGeneralToolbarIconColorWhiteSimple": {
"message": "white (simple)"
},
"optionsDomainPattern": {
"message": "Domain Pattern"
"optionsTargetPattern": {
"message": "Target Domain Pattern"
},
"optionsOptionalOriginPattern": {
"message": "Origin Domain Pattern (Optional)"
},
"optionsExclusionPattern": {
"message": "Exclusion Pattern"
Expand Down
97 changes: 62 additions & 35 deletions src/background/isolation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@ import { BrowserAction } from './browseraction';
import { PageAction } from './pageaction';
import { Storage } from './storage';
import { Utils } from './utils';
import { StorageLocal } from '~/types';
import {
PreferencesSchema,
Debug,
IsolationAction,
Tab,
MacAssignment,
Debug,
PreferencesSchema,
Tab,
WebRequestOnBeforeRequestDetails,
} from '~/types';

Expand Down Expand Up @@ -398,20 +397,29 @@ export class Isolation {
const parsedRequestURL = new URL(request.url);

for (const patternPreferences of this.pref.isolation.domain) {
const domainPattern = patternPreferences.pattern;
const targetDomainPattern = patternPreferences.targetPattern;
const originDomainPattern = patternPreferences.originPattern;

if (
!this.utils.matchDomainPattern(
(tab.url === 'about:blank' &&
openerTab &&
openerTab.url.startsWith('http') &&
openerTab.url) ||
tab.url,
domainPattern
)
) {
const originUrl =
(tab.url === 'about:blank' &&
openerTab &&
openerTab.url.startsWith('http') &&
openerTab.url) ||
tab.url;

const originUrlMatches = this.utils.matchDomainPattern(
originUrl,
originDomainPattern
);
const targetUrlMatches = this.utils.matchDomainPattern(
request.url,
targetDomainPattern
);

if (!targetUrlMatches || !originUrlMatches) {
continue;
}

if (patternPreferences.excluded) {
for (const excludedDomainPattern of Object.keys(
patternPreferences.excluded
Expand All @@ -434,7 +442,8 @@ export class Isolation {
const navigationPreferences = patternPreferences.navigation;
this.debug(
'[shouldIsolateNavigation] found pattern',
domainPattern,
targetDomainPattern,
originDomainPattern,
navigationPreferences
);

Expand Down Expand Up @@ -484,8 +493,26 @@ export class Isolation {
}

for (const patternPreferences of this.pref.isolation.domain) {
const domainPattern = patternPreferences.pattern;
if (!this.utils.matchDomainPattern(request.url, domainPattern)) {
const targetDomainPattern = patternPreferences.targetPattern;
const originDomainPattern = patternPreferences.originPattern;

const originUrl =
(tab.url === 'about:blank' &&
openerTab &&
openerTab.url.startsWith('http') &&
openerTab.url) ||
tab.url;

const originUrlMatches = this.utils.matchDomainPattern(
originUrl,
originDomainPattern
);
const targetUrlMatches = this.utils.matchDomainPattern(
request.url,
targetDomainPattern
);

if (!targetUrlMatches || !originUrlMatches) {
continue;
}
if (!patternPreferences.always) {
Expand All @@ -495,7 +522,7 @@ export class Isolation {
const preferences = patternPreferences.always;
this.debug(
'[shouldIsolateAlways] found pattern for incoming request url',
domainPattern,
targetDomainPattern,
preferences
);
if (preferences.action === 'disabled') {
Expand Down Expand Up @@ -530,26 +557,26 @@ export class Isolation {
return false;
}

if (!this.utils.matchDomainPattern(tab.url, domainPattern)) {
if (!this.utils.matchDomainPattern(tab.url, targetDomainPattern)) {
let openerMatches = false;
if (
openerTab &&
openerTab.url.startsWith('http') &&
this.utils.matchDomainPattern(openerTab.url, domainPattern)
this.utils.matchDomainPattern(openerTab.url, targetDomainPattern)
) {
openerMatches = true;
this.debug(
'[shouldIsolateAlways] opener tab url matched the pattern',
openerTab.url,
domainPattern
targetDomainPattern
);
}
if (!openerMatches) {
this.debug(
'[shouldIsolateAlways] isolating because the tab/opener url doesnt match the pattern',
tab.url,
openerTab,
domainPattern
targetDomainPattern
);
return true;
}
Expand Down Expand Up @@ -607,20 +634,20 @@ export class Isolation {
return true;

case 'notsamedomainexact':
if (target !== origin) {
this.debug(
'[checkIsolationPreferenceAgainstUrl] isolating based on "notsamedomainexact"'
);
return true;
}
// if (target !== origin) {
// this.debug(
// '[checkIsolationPreferenceAgainstUrl] isolating based on "notsamedomainexact"'
// );
// return true;
// }

case 'notsamedomain':
if (!this.utils.sameDomain(origin, target)) {
this.debug(
'[checkIsolationPreferenceAgainstUrl] isolating based on "notsamedomain"'
);
return true;
}
// if (!this.utils.sameDomain(origin, target)) {
// this.debug(
// '[checkIsolationPreferenceAgainstUrl] isolating based on "notsamedomain"'
// );
// return true;
// }
}
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/background/migration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export class Migration {
perDomainIsolation.push(
Object.assign(
{
pattern: domainPattern,
targetPattern: domainPattern,
},
preferences.isolation.domain[domainPattern]
)
Expand Down
79 changes: 40 additions & 39 deletions src/background/mouseclick.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
ClickType,
ClickMessage,
WebRequestOnBeforeRequestDetails,
IsolationDomain,
} from '~/types';

export class MouseClick {
Expand Down Expand Up @@ -100,7 +101,7 @@ export class MouseClick {
): boolean => {
if (preferences.action === 'always') {
this.debug(
'[checkClick] click handled based on preference "always"',
'[checkClickPreferences] click handled based on preference "always"',
preferences
);
return true;
Expand All @@ -117,46 +118,46 @@ export class MouseClick {
}

if (preferences.action === 'notsamedomainexact') {
if (parsedSenderTabURL.hostname !== parsedClickedURL.hostname) {
this.debug(
'[checkClickPreferences] click handled based on preference "notsamedomainexact"',
preferences,
parsedClickedURL,
parsedSenderTabURL
);
return true;
} else {
this.debug(
'[checkClickPreferences] click not handled based on preference "notsamedomainexact"',
preferences,
parsedClickedURL,
parsedSenderTabURL
);
return false;
}
// if (parsedSenderTabURL.hostname !== parsedClickedURL.hostname) {
// this.debug(
// '[checkClickPreferences] click handled based on preference "notsamedomainexact"',
// preferences,
// parsedClickedURL,
// parsedSenderTabURL
// );
// return true;
// } else {
// this.debug(
// '[checkClickPreferences] click not handled based on preference "notsamedomainexact"',
// preferences,
// parsedClickedURL,
// parsedSenderTabURL
// );
// return false;
// }
}

if (preferences.action === 'notsamedomain') {
if (
this.utils.sameDomain(
parsedSenderTabURL.hostname,
parsedClickedURL.hostname
)
) {
this.debug(
'[checkClickPreferences] click not handled from preference "notsamedomain"',
parsedClickedURL,
parsedSenderTabURL
);
return false;
} else {
this.debug(
'[checkClickPreferences] click handled from preference "notsamedomain"',
parsedClickedURL,
parsedSenderTabURL
);
return true;
}
// if (
// this.utils.sameDomain(
// parsedSenderTabURL.hostname,
// parsedClickedURL.hostname
// )
// ) {
// this.debug(
// '[checkClickPreferences] click not handled from preference "notsamedomain"',
// parsedClickedURL,
// parsedSenderTabURL
// );
// return false;
// } else {
// this.debug(
// '[checkClickPreferences] click handled from preference "notsamedomain"',
// parsedClickedURL,
// parsedSenderTabURL
// );
// return true;
// }
}

this.debug('[checkClickPreferences] this should never happen');
Expand All @@ -174,7 +175,7 @@ export class MouseClick {
this.debug('[checkClick] checking click', type, message, sender);

for (const domainPatternPreferences of this.pref.isolation.domain) {
const domainPattern = domainPatternPreferences.pattern;
const domainPattern = domainPatternPreferences.targetPattern;
if (!this.utils.matchDomainPattern(tab.url, domainPattern)) {
continue;
}
Expand Down
3 changes: 2 additions & 1 deletion src/background/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ export class Utils {
return parsedOrigin.domain === parsedTarget.domain;
}

matchDomainPattern(url: string, domainPattern: string): boolean {
matchDomainPattern(url: string, domainPattern?: string): boolean {
if (!domainPattern) return true;
if (domainPattern.startsWith('/')) {
const regexp = domainPattern.match(/^\s*\/(.*)\/([gimsuy]+)?\s*$/);
if (!regexp) {
Expand Down
14 changes: 11 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ export interface TmpTabOptions {

export type IsolationAction =
| 'never'
| 'notsamedomain'
| 'notsamedomainexact'
| 'notsamedomain' // legacy
| 'notsamedomainexact' // legacy
| 'always'
| 'global';

Expand Down Expand Up @@ -84,7 +84,8 @@ export interface IsolationGlobal {
}

export interface IsolationDomain extends IsolationGlobal {
pattern: string;
targetPattern: string;
originPattern?: string;
always: {
action: 'enabled' | 'disabled';
allowedInPermanent: boolean;
Expand Down Expand Up @@ -279,3 +280,10 @@ export interface WebRequestOnBeforeRequestDetails
extends browser.webRequest._OnBeforeRequestDetails {
cookieStoreId: string;
}

export interface ToolTip {
hidden: boolean;
position?: 'bottom left' | 'top left';
}

export type DomainPatternType = 'target' | 'origin' | 'exclusion';
Loading