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

Fix multiple injections #87

Merged
merged 3 commits into from
Mar 11, 2018
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
4 changes: 2 additions & 2 deletions html/demos/interception/iframe.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
<iframe width="330" height="94" src="sub-iframe.html"></iframe>
<iframe width="330" height="94" src="sub-iframe.html"></iframe>

<iframe id="iframe-a" width="330" height="94"></iframe>
<iframe id="iframe-b" width="330" height="94"></iframe>
<iframe id="iframe-a" src="null.html" width="330" height="94"></iframe>
<iframe id="iframe-b" src="null.html" width="330" height="94"></iframe>
</body>
<script type="text/javascript">
if(document.cookie) {
Expand Down
2 changes: 1 addition & 1 deletion html/demos/interception/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
<iframe width="360" height="490" src="iframe.html"></iframe>
<iframe width="360" height="490" src="iframe.html"></iframe>

<iframe id="iframe-a" width="360" height="490"></iframe>
<iframe id="iframe-a" width="360" src="null.html" height="490"></iframe>
</body>
<script type="text/javascript">
if(document.cookie) {
Expand Down
1 change: 1 addition & 0 deletions html/demos/interception/null.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
null frame
7 changes: 5 additions & 2 deletions js/background/injections/content/filter_response_data.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@ if(injection_strategy != 'cookie') {
var data_html_string = undefined;

// TODO remove cache for development enviroment
load_interceptor_element(function(element) {
load_interceptor_element('filterResponseData', function(element) {
interceptor_html_string = element_to_html_string(element);
});

load_data_element(function(element) {
load_data_element('filterResponseData', function(element) {
data_element = element;
});

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

// TODO
// it's an html document?
// will it be rendered by the browser?
if(should_intercept_request(url)) {
var options = full_options_for_url(url);

Expand Down
22 changes: 11 additions & 11 deletions js/background/strategy.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
var injection_strategy = 'cookie';

if(
typeof browser !== 'undefined'
&&
browser.webRequest
&&
browser.webRequest.filterResponseData
) {

// has filterResponseData support (Firefox 57+ only)
injection_strategy = 'filterResponseData';
}
// if(
// typeof browser !== 'undefined'
// &&
// browser.webRequest
// &&
// browser.webRequest.filterResponseData
// ) {
//
// // has filterResponseData support (Firefox 57+ only)
// injection_strategy = 'filterResponseData';
// }
2 changes: 1 addition & 1 deletion js/content/injections/data.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
injections_controller(function(should_inject) {
if(should_inject) {
load_data_element(function(element) {
load_data_element('contentScript', function(element) {
document.documentElement.insertBefore(
element, document.documentElement.firstChild
);
Expand Down
2 changes: 1 addition & 1 deletion js/content/injections/interceptor.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
injections_controller(function(should_inject) {
if(should_inject) {
load_interceptor_element(function(element) {
load_interceptor_element('contentScript', function(element) {
document.documentElement.insertBefore(
element, document.documentElement.firstChild
);
Expand Down
176 changes: 98 additions & 78 deletions js/content/interceptor.js
Original file line number Diff line number Diff line change
@@ -1,111 +1,131 @@
var original_window_setTimeout = window.setTimeout;
var original_window_setInterval = window.setInterval;
var should_luminous_run = (
document.getElementsByClassName('luminous-interceptor').length == 1
);

var cached_options = { injection_disabled: false };
var collect_details = false;
var remove_duplicated_luminous_elements = function(class_name) {
var elements = document.getElementsByClassName(class_name);
var i = elements.length;

var json_options_element = undefined;

var get_options = function() {
if(!json_options_element) {
json_options_element = document.getElementById('luminous-options');
if(i > 1) {
while(i--) { if(i > 0) { elements[i].remove(); } }
}
}

if(json_options_element && json_options_element.getAttribute('data-changed') == 'true') {
cached_options = JSON.parse(json_options_element.innerHTML);
remove_duplicated_luminous_elements('luminous-data');
remove_duplicated_luminous_elements('luminous-options');
remove_duplicated_luminous_elements('luminous-interceptor');

collect_details = cached_options['collect_details'];
// --------------------------------
if(should_luminous_run) {
var original_window_setTimeout = window.setTimeout;
var original_window_setInterval = window.setInterval;

json_options_element.setAttribute('data-changed', 'false');
}
var cached_options = { injection_disabled: false };
var collect_details = false;

return cached_options;
}
var json_options_element = undefined;

var luminous_data_element = document.getElementById('luminous-data');
var get_options = function() {
if(!json_options_element) {
json_options_element = document.getElementById('luminous-options');
}

var is_allowed = function(kind, type) {
var options = get_options();
if(json_options_element && json_options_element.getAttribute('data-changed') == 'true') {
cached_options = JSON.parse(json_options_element.innerHTML);

if(!options['disabled']) options['disabled'] = {};
if(!options['disabled'][kind]) options['disabled'][kind] = {};
collect_details = cached_options['collect_details'];

return !options['disabled'][kind][type];
}
json_options_element.setAttribute('data-changed', 'false');
}

return cached_options;
}

// -------------------------------------------------
var dispatch_stack_fifo = [];
var luminous_data_element = document.getElementById('luminous-data');

var dispatch_stack_timer = undefined;
var is_allowed = function(kind, type) {
var options = get_options();

var process_dispatch_stack = function() {
clearTimeout(dispatch_stack_timer);
dispatch_stack_timer = undefined;
if(!options['disabled']) options['disabled'] = {};
if(!options['disabled'][kind]) options['disabled'][kind] = {};

if(luminous_data_element.getAttribute('data-ready')) {
luminous_data_element.dispatchEvent(
new MessageEvent('luminous-message', { data: dispatch_stack_fifo })
);
} else {
dispatch_stack_timer = original_window_setTimeout(function() {
process_dispatch_stack()
}, 100, '__INTERNAL_LUMINOUS_CODE__');
return !options['disabled'][kind][type];
}

dispatch_stack_fifo = [];
}
// -------------------------------------------------
var dispatch_stack_fifo = [];

var dispatch_stack_timer = undefined;

var process_dispatch_stack = function() {
clearTimeout(dispatch_stack_timer);
dispatch_stack_timer = undefined;

// -------------------------------------------------
var increment_counter = function(kind, type, allowed, target, code, time) {
original_window_setTimeout(function(
_, kind, type, allowed, target, code, time
) {
if(collect_details && code) { var code = ('' + code).slice(0, 400); } else { var code = undefined; }

dispatch_stack_fifo.push({
kind: kind,
type: type,
time: time,
target: target.toString(),
code: code,
allowed: allowed,
domain: document.location.host,
url: document.location.href
});


if(!dispatch_stack_timer) {
if(luminous_data_element.getAttribute('data-ready')) {
luminous_data_element.dispatchEvent(
new MessageEvent('luminous-message', { data: dispatch_stack_fifo })
);
} else {
dispatch_stack_timer = original_window_setTimeout(function() {
process_dispatch_stack()
}, 100, '__INTERNAL_LUMINOUS_CODE__'); // STACK_TIMER_01
}, 100, '__INTERNAL_LUMINOUS_CODE__');
}
}, 0, '__INTERNAL_LUMINOUS_CODE__', kind, type, allowed, target, code, time);
}

if(!get_options()['injection_disabled']) {
// #load_injectors
dispatch_stack_fifo = [];
}

// interceptors/event_target/remove_event_listener.js
// interceptors/event_target/add_event_listener.js
// -------------------------------------------------
var increment_counter = function(kind, type, allowed, target, code, time) {
original_window_setTimeout(function(
_, kind, type, allowed, target, code, time
) {
if(collect_details && code) { var code = ('' + code).slice(0, 400); } else { var code = undefined; }

dispatch_stack_fifo.push({
kind: kind,
type: type,
time: time,
target: target.toString(),
code: code,
allowed: allowed,
domain: document.location.host,
url: document.location.href
});


if(!dispatch_stack_timer) {
dispatch_stack_timer = original_window_setTimeout(function() {
process_dispatch_stack()
}, 100, '__INTERNAL_LUMINOUS_CODE__'); // STACK_TIMER_01
}
}, 0, '__INTERNAL_LUMINOUS_CODE__', kind, type, allowed, target, code, time);
}

if(!get_options()['injection_disabled']) {
// #load_injectors

// interceptors/battery_status/get_battery.js
// interceptors/event_target/remove_event_listener.js
// interceptors/event_target/add_event_listener.js

// interceptors/fetch/fetch.js
// interceptors/battery_status/get_battery.js

// interceptors/gamepad/get_gamepads.js
// interceptors/fetch/fetch.js

// interceptors/geolocation/get_current_position.js
// interceptors/geolocation/watch_position.js
// interceptors/gamepad/get_gamepads.js

// interceptors/navigator_id/user_agent.js
// interceptors/geolocation/get_current_position.js
// interceptors/geolocation/watch_position.js

// interceptors/schedulers/set_interval.js
// interceptors/schedulers/set_timeout.js
// interceptors/navigator_id/user_agent.js

// interceptors/web_socket/send.js
// interceptors/schedulers/set_interval.js
// interceptors/schedulers/set_timeout.js

// interceptors/xml_http_request/open.js
// interceptors/xml_http_request/send.js
// interceptors/web_socket/send.js

// /load_injectors
// interceptors/xml_http_request/open.js
// interceptors/xml_http_request/send.js

// /load_injectors
}
}
6 changes: 0 additions & 6 deletions js/content/readers/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,6 @@ injections_controller(function(should_inject) {
if(add_listener_not_found_count > 10) {
clearInterval(add_listener_to_data_element);
}

console.log(
'add_listener_to_data_element: ' + add_listener_not_found_count
);
}
}, 50);

Expand All @@ -80,8 +76,6 @@ injections_controller(function(should_inject) {
if(get_tab_id_not_found_count > 10) {
clearInterval(get_tab_id);
}

console.log('get_tab_id: ' + get_tab_id_not_found_count);
}
}, 50);
});
40 changes: 20 additions & 20 deletions js/content/strategy.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
var injection_strategy = 'cookie';

if(typeof browser !== 'undefined') {
// Probably a Gecko-based browser

// TODO A very bad strategy, if there is any
// spoofing of useragent, this will not work ...
var current_browser = UAParser().browser;

if(
current_browser.name == 'Firefox'
&&
parseInt(current_browser.major) < 57
) {
// Firefox without filterResponseData support...
injection_strategy = 'cookie';
} else {
// Let's hope it's a Firefox 57+ or
// something with filterResponseData support.
injection_strategy = 'filterResponseData';
}
}
// if(typeof browser !== 'undefined') {
// // Probably a Gecko-based browser
//
// // TODO A very bad strategy, if there is any
// // spoofing of useragent, this will not work ...
// var current_browser = UAParser().browser;
//
// if(
// current_browser.name == 'Firefox'
// &&
// parseInt(current_browser.major) < 57
// ) {
// // Firefox without filterResponseData support...
// injection_strategy = 'cookie';
// } else {
// // Let's hope it's a Firefox 57+ or
// // something with filterResponseData support.
// injection_strategy = 'filterResponseData';
// }
// }
7 changes: 0 additions & 7 deletions js/settings/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,8 @@ var set_sync_option = function(name, value, namespace, value_as_namespace) {
if(value_as_namespace) {
if(!sync_data[namespace][name]) sync_data[namespace][name] = {};

// console.log(
// '[' + namespace + '][' + name + ']['+value_as_namespace+'] = ' + value
// )

sync_data[namespace][name][value_as_namespace] = value;
} else {
// console.log(
// '[' + namespace + '][' + name + '] = ' + value
// )
sync_data[namespace][name] = value;
}
}
Expand Down
3 changes: 2 additions & 1 deletion js/utils/injections/data.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
var load_data_element = function(callback_function) {
var load_data_element = function(from, callback_function) {

var data_input_injection = document.createElement('input');
data_input_injection.type = 'hidden';
data_input_injection.id = 'luminous-data';
data_input_injection.setAttribute('data-from', from);
data_input_injection.setAttribute('class', 'luminous-data');

callback_function(data_input_injection);
Expand Down
3 changes: 2 additions & 1 deletion js/utils/injections/interceptor.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var load_interceptor_element = function(callback_function) {
var load_interceptor_element = function(from, callback_function) {

var load_content = function(path, content_callback_function) {
var request = new XMLHttpRequest();
Expand Down Expand Up @@ -43,6 +43,7 @@ var load_interceptor_element = function(callback_function) {
javascript_injection.setAttribute('class', 'luminous-interceptor');
javascript_injection.type = 'text/javascript';
javascript_injection.setAttribute('nonce', '3b34aae43a');
javascript_injection.setAttribute('data-from', from);
javascript_injection.innerHTML = full_content;

callback_function(javascript_injection);
Expand Down
Loading