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

Adding return after rejects in a few places. Implementing Ghostery side of GH-885. Fix for GH-871. #10

Merged
merged 10 commits into from Mar 23, 2018
@@ -856,7 +856,7 @@
"message": "Nunca"
},
"settings_seconds": {
"message": "segundos"
"message": "s"
},
"settings_hide_alert_trusted": {
"message": "Ocultar el cuadro púrpura en sitios web de confianza"
@@ -856,7 +856,7 @@
"message": "Jamais"
},
"settings_seconds": {
"message": "secondes"
"message": "s"
},
"settings_hide_alert_trusted": {
"message": "Masquer la case violette sur les sites de confiance"
@@ -200,6 +200,7 @@ function getSiteData() {

if (!tab) {
reject(new Error('Tab not found. Cannot gather page data'));
return;
}

resolve({
@@ -1007,7 +1008,7 @@ messageCenter.on('enabled', () => {
* }
* }
*/
log('GOT OFFER!!!!!!!!!!!!!!!!!!!!!!');
log('GOT OFFER', msg);
// first check that the message is from core and is the one we expect
if (msg.origin === 'offers-core' &&
msg.type === 'push-offer' &&
@@ -23,7 +23,7 @@ import conf from './Conf';
import foundBugs from './FoundBugs';
import globals from './Globals';
import latency from './Latency';
import Policy from './Policy';
import Policy, { BLOCK_REASON_SS_UNBLOCKED, BLOCK_REASON_C2P_ALLOWED_THROUGH } from './Policy';
import PolicySmartBlock from './PolicySmartBlock';
import PurpleBox from './PurpleBox';
import surrogatedb from './SurrogateDb';
@@ -372,21 +372,16 @@ class EventHandlers {
return { cancel: false };
}

let app_id;
let cat_id;
let incognito;
let tab_host;
let fromRedirect;
let block;
if (bug_id) {
app_id = bugDb.db.bugs[bug_id].aid;
cat_id = bugDb.db.apps[app_id].cat;
incognito = tabInfo.getTabInfo(tab_id, 'incognito');
tab_host = tabInfo.getTabInfo(tab_id, 'host');
fromRedirect = globals.REDIRECT_MAP.has(request_id);
block = this._checkBlocking(app_id, cat_id, tab_id, tab_host, page_url, request_id);
const app_id = bugDb.db.bugs[bug_id].aid;
const cat_id = bugDb.db.apps[app_id].cat;
const incognito = tabInfo.getTabInfo(tab_id, 'incognito');
const tab_host = tabInfo.getTabInfo(tab_id, 'host');
const fromRedirect = globals.REDIRECT_MAP.has(request_id);
const { block, reason } = this._checkBlocking(app_id, cat_id, tab_id, tab_host, page_url, request_id);
if (!block && reason === BLOCK_REASON_SS_UNBLOCKED) {
// The way to pass this flag to Cliqz handlers
details.ghosteryWhitelisted = true;
}

// Latency initialization needs to be synchronous to avoid race condition with onCompleted, etc.
// TODO can URLs repeat within a redirect chain? what are the cases of repeating URLs (trackers only, ...)?
if (block === false) {
@@ -759,6 +754,12 @@ class EventHandlers {
return true;
}

/**
* @typedef {Object} BlockWithReason
* @property {boolean} block indicates if the tracker should be blocked.
* @property {string} reason indicates the reason for the block result.
*/

/**
* Determine whether this request should be blocked
*
@@ -767,10 +768,10 @@ class EventHandlers {
* @param {number} app_id tracker id
* @param {number} cat_id tracker category id
* @param {number} tab_id tab id
* @param {string} tab_host tab url host
* @param {string} page_url full tab url
* @param {string} tab_host tab url host
* @param {string} page_url full tab url
* @param {number} request_id request id
* @return {boolean}
* @return {BlockWithReason} block result with reason
*/
_checkBlocking(app_id, cat_id, tab_id, tab_host, page_url, request_id) {
const fromRedirect = globals.REDIRECT_MAP.has(request_id);
@@ -779,7 +780,7 @@ class EventHandlers {
// If we let page-level c2p trackers through, we don't want to block it
// along with all subsequent top-level redirects.
if (fromRedirect && globals.LET_REDIRECTS_THROUGH) {
block = false;
block = { block: false, reason: BLOCK_REASON_C2P_ALLOWED_THROUGH };
} else {
block = this.policy.shouldBlock(app_id, cat_id, tab_id, tab_host, page_url);
}
@@ -72,9 +72,7 @@ class Metrics {
prefsSet({
utm_source: this.utm_source,
utm_campaign: this.utm_campaign
})
.then(prefs => resolve(prefs))
.catch(err => reject(err));
});
});
resolve();
});
@@ -95,9 +93,7 @@ class Metrics {
prefsSet({
utm_source: this.utm_source,
utm_campaign: this.utm_campaign
})
.then(reject)
.catch(reject);
});
}
});
resolve();
@@ -20,9 +20,20 @@ import conf from './Conf';
import { processUrl } from '../utils/utils';
import globals from './Globals';

const POLICY_BLOCK_NOTHING = 'POLICY_BLOCK_NOTHING';
const POLICY_BLOCK_EVERYTHING = 'POLICY_BLOCK_EVERYTHING';
const POLICY_BLOCK_ADS = 'POLICY_BLOCK_ADS';
/**
* Enum for reasons returned by shouldBlock
* @type {string}
* TBD: See if we can do with integer values for performance.
*/
export const BLOCK_REASON_BLOCK_PAUSED = 'BLOCK_REASON_BLOCK_PAUSED';
export const BLOCK_REASON_GLOBAL_BLOCKED = 'BLOCK_REASON_GLOBAL_BLOCKED';
export const BLOCK_REASON_WHITELISTED = 'BLOCK_REASON_WHITELISTED';
export const BLOCK_REASON_BLACKLISTED = 'BLOCK_REASON_BLACKLISTED';
export const BLOCK_REASON_SS_UNBLOCKED = 'BLOCK_REASON_SS_UNBLOCKED';
export const BLOCK_REASON_SS_BLOCKED = 'BLOCK_REASON_SS_BLOCKED';
export const BLOCK_REASON_C2P_ALLOWED_ONCE = 'BLOCK_REASON_C2P_ALLOWED_ONCE';
export const BLOCK_REASON_C2P_ALLOWED_THROUGH = 'BLOCK_REASON_C2P_ALLOWED_THROUGH';

/**
* Class for handling site policy.
* @memberOf BackgroundClasses
@@ -92,6 +103,12 @@ class Policy {
return false;
}

/**
* @typedef {Object} BlockWithReason
* @property {boolean} block indicates if the tracker should be blocked.
* @property {string} reason indicates the reason for the block result.
*/

/**
* Check the users blocking settings (selected_app_ids and site_specific_blocks/unblocks)
* to determine whether a tracker should be blocked
@@ -100,36 +117,42 @@ class Policy {
* @param {number} tab_id tab id
* @param {string} tab_host tab url host
* @param {string} tab_url tab url
* @return {boolean}
* @return {BlockWithReason} block result with reason
*/
shouldBlock(app_id, cat_id, tab_id, tab_host, tab_url) {
if (globals.SESSION.paused_blocking) {
return false;
return { block: false, reason: BLOCK_REASON_BLOCK_PAUSED };
}

if (conf.selected_app_ids.hasOwnProperty(app_id)) {
if (conf.toggle_individual_trackers && conf.site_specific_unblocks.hasOwnProperty(tab_host) && conf.site_specific_unblocks[tab_host].includes(+app_id)) {
if (this.blacklisted(tab_url)) {
return !c2pDb.allowedOnce(tab_id, app_id);
const allowedOnce = c2pDb.allowedOnce(tab_id, app_id);
return { block: !allowedOnce, reason: allowedOnce ? BLOCK_REASON_C2P_ALLOWED_ONCE : BLOCK_REASON_BLACKLISTED };
}
return false;
return { block: false, reason: BLOCK_REASON_SS_UNBLOCKED };
}
if (this.whitelisted(tab_url)) {
return false;
return { block: false, reason: BLOCK_REASON_WHITELISTED };
}
return !c2pDb.allowedOnce(tab_id, app_id);
const allowedOnce = c2pDb.allowedOnce(tab_id, app_id);
return { block: !allowedOnce, reason: allowedOnce ? BLOCK_REASON_C2P_ALLOWED_ONCE : BLOCK_REASON_GLOBAL_BLOCKED };
}
// We get here when app_id is not selected for blocking
if (conf.toggle_individual_trackers && conf.site_specific_blocks.hasOwnProperty(tab_host) && conf.site_specific_blocks[tab_host].includes(+app_id)) {
if (this.whitelisted(tab_url)) {
return false;
return { block: false, reason: BLOCK_REASON_WHITELISTED };
}
return !c2pDb.allowedOnce(tab_id, app_id);
const allowedOnce = c2pDb.allowedOnce(tab_id, app_id);
return { block: !allowedOnce, reason: allowedOnce ? BLOCK_REASON_C2P_ALLOWED_ONCE : BLOCK_REASON_SS_BLOCKED };
}
if (this.blacklisted(tab_url)) {
return !c2pDb.allowedOnce(tab_id, app_id);
const allowedOnce = c2pDb.allowedOnce(tab_id, app_id);
return { block: !allowedOnce, reason: allowedOnce ? BLOCK_REASON_C2P_ALLOWED_ONCE : BLOCK_REASON_BLACKLISTED };
}
return false;
return { block: false, reason: BLOCK_REASON_GLOBAL_BLOCKED };
}
}

export default Policy;

@@ -534,11 +534,13 @@ function _refreshToken() {
return new Promise((resolve, reject) => {
if (!conf.login_info.logged_in) {
resolve('User not logged in');
return;
}

const decoded_user_token = conf.login_info.decoded_user_token;
if (!decoded_user_token || !decoded_user_token.exp) {
reject('User token is corrupted or null');
return;
}

const currentTime = (new Date()).getTime();
ProTip! Use n and p to navigate between commits in a pull request.