Microsoft is acquiring GitHub. Decentraleyes is leaving GitHub. Welcome to its new home!

To participate, please register, or sign in with an existing GitLab.com, Bitbucket, or GitHub account.

Past contributions on GitHub? Be sure to reclaim your Comments, Issues, and Pull Requests.

Modularize request sanitation logic

parent 6d6a7b68
......@@ -12,6 +12,7 @@
"interceptor": true,
"mappings": true,
"requestAnalyzer": true,
"requestSanitizer": true,
"Resource": true,
"resources": true,
"Setting": true,
......
/**
* Request Sanitizer
* Belongs to Decentraleyes.
*
* @author Thomas Rientjes
* @since 2018-01-10
* @license MPL 2.0
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/.
*/
'use strict';
/**
* Request Sanitizer
*/
var requestSanitizer = {};
/**
* Public Methods
*/
requestSanitizer.enable = function () {
let onBeforeSendHeaders = chrome.webRequest.onBeforeSendHeaders;
onBeforeSendHeaders.addListener(requestSanitizer._stripMetadata, {
'urls': stateManager.validHosts
}, [WebRequest.BLOCKING, WebRequest.HEADERS]);
};
requestSanitizer.disable = function () {
let onBeforeSendHeaders = chrome.webRequest.onBeforeSendHeaders;
onBeforeSendHeaders.removeListener(requestSanitizer._stripMetadata, {
'urls': stateManager.validHosts
}, [WebRequest.BLOCKING, WebRequest.HEADERS]);
};
/**
* Private Methods
*/
requestSanitizer._stripMetadata = function (requestDetails) {
for (let i = 0; i < requestDetails.requestHeaders.length; ++i) {
if (requestDetails.requestHeaders[i].name === WebRequest.ORIGIN_HEADER) {
requestDetails.requestHeaders.splice(i--, 1);
} else if (requestDetails.requestHeaders[i].name === WebRequest.REFERER_HEADER) {
requestDetails.requestHeaders.splice(i--, 1);
}
}
return {
'requestHeaders': requestDetails.requestHeaders
};
};
......@@ -148,22 +148,6 @@ stateManager._updateTab = function (details) {
}
};
stateManager._stripMetadata = function (requestDetails) {
for (let i = 0; i < requestDetails.requestHeaders.length; ++i) {
if (requestDetails.requestHeaders[i].name === WebRequest.ORIGIN_HEADER) {
requestDetails.requestHeaders.splice(i--, 1);
} else if (requestDetails.requestHeaders[i].name === WebRequest.REFERER_HEADER) {
requestDetails.requestHeaders.splice(i--, 1);
}
}
return {
'requestHeaders': requestDetails.requestHeaders
};
};
stateManager._handleStorageChanged = function (changes) {
if ('showIconBadge' in changes) {
......@@ -180,19 +164,10 @@ stateManager._handleStorageChanged = function (changes) {
if ('stripMetadata' in changes) {
let onBeforeSendHeaders;
onBeforeSendHeaders = chrome.webRequest.onBeforeSendHeaders;
onBeforeSendHeaders.removeListener(stateManager._stripMetadata, {
'urls': stateManager.validHosts
}, [WebRequest.BLOCKING, WebRequest.HEADERS]);
requestSanitizer.disable();
if (changes.stripMetadata.newValue !== false) {
onBeforeSendHeaders.addListener(stateManager._stripMetadata, {
'urls': stateManager.validHosts
}, [WebRequest.BLOCKING, WebRequest.HEADERS]);
requestSanitizer.enable();
}
}
};
......@@ -272,10 +247,7 @@ chrome.webRequest.onBeforeRedirect.addListener(function (requestDetails) {
chrome.storage.local.get({'stripMetadata': true}, function (options) {
if (options === null || options.stripMetadata !== false) {
chrome.webRequest.onBeforeSendHeaders.addListener(stateManager._stripMetadata, {
'urls': stateManager.validHosts
}, [WebRequest.BLOCKING, WebRequest.HEADERS]);
requestSanitizer.enable();
}
});
......
......@@ -17,6 +17,7 @@
<script src="../../core/files.js"></script>
<script src="../../core/resources.js"></script>
<script src="../../core/mappings.js"></script>
<script src="../../core/request-sanitizer.js"></script>
<script src="../../core/state-manager.js"></script>
<script src="../../core/request-analyzer.js"></script>
<script src="../../core/interceptor.js"></script>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment