From 83e66b36f14a9d9791b607bdbb0ef2508dd64017 Mon Sep 17 00:00:00 2001 From: gorhill Date: Tue, 30 Aug 2016 19:57:25 -0400 Subject: [PATCH] patch or convert filters meant to target websocket network requests --- platform/chromium/vapi-background.js | 1 + src/js/static-net-filtering.js | 29 +++++++++++++++++++++++++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/platform/chromium/vapi-background.js b/platform/chromium/vapi-background.js index e9c8d42dc..7c270038a 100644 --- a/platform/chromium/vapi-background.js +++ b/platform/chromium/vapi-background.js @@ -35,6 +35,7 @@ var chrome = self.chrome; var manifest = chrome.runtime.getManifest(); vAPI.chrome = true; +vAPI.cantWebsocket = true; var noopFunc = function(){}; diff --git a/src/js/static-net-filtering.js b/src/js/static-net-filtering.js index e06589132..18140f798 100644 --- a/src/js/static-net-filtering.js +++ b/src/js/static-net-filtering.js @@ -1293,6 +1293,7 @@ FilterBucket.fromSelfie = function() { /******************************************************************************/ var FilterParser = function() { + this.cantWebsocket = vAPI.cantWebsocket; this.reHostnameRule1 = /^[0-9a-z][0-9a-z.-]*[0-9a-z]$/i; this.reHostnameRule2 = /^\**[0-9a-z][0-9a-z.-]*[0-9a-z]\^?$/i; this.reCleanupHostnameRule2 = /^\**|\^$/g; @@ -1302,6 +1303,7 @@ var FilterParser = function() { this.reHasUppercase = /[A-Z]/; this.reIsolateHostname = /^(\*?\.)?([^\x00-\x24\x26-\x2C\x2F\x3A-\x5E\x60\x7B-\x7F]+)(.*)/; this.reHasUnicode = /[^\x00-\x7F]/; + this.reWebsocketAny = /^wss?:(?:\/\/)?$/; this.domainOpt = ''; this.reset(); }; @@ -1353,11 +1355,17 @@ FilterParser.prototype.reset = function() { /******************************************************************************/ +FilterParser.prototype.bitFromType = function(type) { + return 1 << ((typeNameToTypeValue[type] >>> 4) - 1); +}; + +/******************************************************************************/ + // https://github.com/chrisaljoudi/uBlock/issues/589 // Be ready to handle multiple negated types FilterParser.prototype.parseOptType = function(raw, not) { - var typeBit = 1 << ((typeNameToTypeValue[this.toNormalizedType[raw]] >>> 4) - 1); + var typeBit = this.bitFromType(this.toNormalizedType[raw]); if ( !not ) { this.types |= typeBit; @@ -1424,6 +1432,11 @@ FilterParser.prototype.parseOptions = function(s) { } if ( this.toNormalizedType.hasOwnProperty(opt) ) { this.parseOptType(opt, not); + // Due to ABP categorizing `websocket` requests as `other`, we need + // to add `websocket` for when `other` is used. + if ( opt === 'other' ) { + this.parseOptType('websocket', not); + } continue; } if ( opt.startsWith('domain=') ) { @@ -1601,6 +1614,20 @@ FilterParser.prototype.parse = function(raw) { this.f = this.reHasUppercase.test(s) ? s.toLowerCase() : s; + // https://github.com/gorhill/uBlock/issues/1943#issuecomment-243188946 + // Convert websocket-related filter where possible to a format which + // can be handled using CSP injection. + if ( + this.cantWebsocket && + this.anchor === -1 && + this.firstParty === false && + this.thirdParty === false && + this.reWebsocketAny.test(this.f) + ) { + this.f = '*'; + this.types = this.bitFromType('websocket'); + } + return this; };