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

Improve filterResponseData #110

Merged
merged 5 commits into from
Mar 22, 2019
Merged
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
53 changes: 51 additions & 2 deletions js/background/injections/content/filter_response_data.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,30 @@
if(injection_strategy != 'cookie') {

var should_intercept_content_type = function(content_type) {
var should_intercept = false;

// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Type
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types
var splitted_content_type = content_type.toLowerCase().split(
';'
)[0].toLowerCase().split('/');

var type = splitted_content_type[0];
var subtype = splitted_content_type[1];

// TODO: Improve content_type check to fix issue #107:
// https://github.com/gbaptista/luminous/issues/107
if (['text', 'application'].includes(type)) {
should_intercept = /html/.test(subtype);
}

return should_intercept;
}

var interceptor_html_string = undefined;
var data_html_string = undefined;

// TODO remove cache for development enviroment
// TODO: remove cache for development enviroment
load_interceptor_element('filterResponseData', function(element) {
interceptor_html_string = element_to_html_string(element);
});
Expand All @@ -14,9 +36,27 @@ if(injection_strategy != 'cookie') {
var encoder = new TextEncoder();
var decoder = new TextDecoder('utf-8');

var requests_content_types = {};

var detect_request_type = function(request_details) {
requests_content_types[request_details.requestId] = undefined;

var url = url_for_request(request_details);

if(should_intercept_request(url)) {
for(let header of request_details.responseHeaders) {
if(header.name.toLowerCase() == 'content-type') {
requests_content_types[request_details.requestId] = header.value;
}
}
}
}

var inject_interceptor_and_settings = function(request_details) {
var url = url_for_request(request_details);

// TODO: Improve should_intercept_request to fix issue #107:
// https://github.com/gbaptista/luminous/issues/107
if(should_intercept_request(url)) {
var options = full_options_for_url(url);

Expand All @@ -32,12 +72,14 @@ if(injection_strategy != 'cookie') {
var content_to_write = options_html_string + data_html_string + interceptor_html_string;

filter.ondata = event => {
var content_type = requests_content_types[request_details.requestId];

var content = decoder.decode(event.data, {stream: true});

// binary content? Image, PDF...
var binary = /\0/.exec(content);

if(!binary) {
if(!binary && should_intercept_content_type(content_type)) {
var match = /DOCTYPE(.|[\r\n])*?>/i.exec(content);

if(match && match.index < 10 && match[0].length < 200) {
Expand All @@ -61,6 +103,13 @@ if(injection_strategy != 'cookie') {
return {};
}

chrome.webRequest.onHeadersReceived.addListener(
detect_request_type,
{ urls: ['<all_urls>'], types: ['main_frame', 'sub_frame'] },
['responseHeaders', 'blocking']
);


chrome.webRequest.onBeforeRequest.addListener(
inject_interceptor_and_settings,
{ urls: ['<all_urls>'], types: ['main_frame', 'sub_frame'] },
Expand Down
3 changes: 3 additions & 0 deletions js/background/strategy.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ if(
&&
browser.webRequest.filterResponseData
) {
// TODO: Use sync_data['advanced']['injection_strategy']
// if 'TryFilterResponseData': 'filterResponseData'
// else: 'cookie'

// has filterResponseData support (Firefox 57+ only)
injection_strategy = 'filterResponseData';
Expand Down
17 changes: 16 additions & 1 deletion js/content/initializer.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
var should_inject_this_document = function() {
// thanks to: https://github.com/EFForg/privacybadger/pull/1954
if (
document instanceof HTMLDocument === false &&
(
document instanceof XMLDocument === false ||
document.createElement('div') instanceof HTMLDivElement === false
)
) {
return false;
}

return true;
}

var intialize_luminous_injections = function(from) {
if(from == 'cookie') {
var injection_enabled = Cookies.get('le');
Expand All @@ -10,7 +25,7 @@ var intialize_luminous_injections = function(from) {

if(from == 'cookie') {
if(injection_enabled != 'f') {
fn(true);
fn(should_inject_this_document());
}
} else {
fn(false);
Expand Down
8 changes: 6 additions & 2 deletions js/content/strategy.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@ if(typeof browser !== 'undefined') {
// Firefox without filterResponseData support...
injection_strategy = 'cookie';
} else {
// Let's hope it's a Firefox 57+ or
// something with filterResponseData support.
// TODO: Use sync_data['advanced']['injection_strategy']
// if 'TryFilterResponseData': 'filterResponseData'
// else: 'cookie'

// Let's hope it's a Firefox 57+ or something
// with filterResponseData support.
injection_strategy = 'filterResponseData';
}
}