Verified Commit a8e340ab authored by Thomas Rientjes's avatar Thomas Rientjes
Browse files

Modularize request sanitation logic

parent 6d6a7b68
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
"interceptor": true, "interceptor": true,
"mappings": true, "mappings": true,
"requestAnalyzer": true, "requestAnalyzer": true,
"requestSanitizer": true,
"Resource": true, "Resource": true,
"resources": true, "resources": true,
"Setting": 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) { ...@@ -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) { stateManager._handleStorageChanged = function (changes) {
if ('showIconBadge' in changes) { if ('showIconBadge' in changes) {
...@@ -180,19 +164,10 @@ stateManager._handleStorageChanged = function (changes) { ...@@ -180,19 +164,10 @@ stateManager._handleStorageChanged = function (changes) {
if ('stripMetadata' in changes) { if ('stripMetadata' in changes) {
let onBeforeSendHeaders; requestSanitizer.disable();
onBeforeSendHeaders = chrome.webRequest.onBeforeSendHeaders;
onBeforeSendHeaders.removeListener(stateManager._stripMetadata, {
'urls': stateManager.validHosts
}, [WebRequest.BLOCKING, WebRequest.HEADERS]);
if (changes.stripMetadata.newValue !== false) { if (changes.stripMetadata.newValue !== false) {
requestSanitizer.enable();
onBeforeSendHeaders.addListener(stateManager._stripMetadata, {
'urls': stateManager.validHosts
}, [WebRequest.BLOCKING, WebRequest.HEADERS]);
} }
} }
}; };
...@@ -272,10 +247,7 @@ chrome.webRequest.onBeforeRedirect.addListener(function (requestDetails) { ...@@ -272,10 +247,7 @@ chrome.webRequest.onBeforeRedirect.addListener(function (requestDetails) {
chrome.storage.local.get({'stripMetadata': true}, function (options) { chrome.storage.local.get({'stripMetadata': true}, function (options) {
if (options === null || options.stripMetadata !== false) { if (options === null || options.stripMetadata !== false) {
requestSanitizer.enable();
chrome.webRequest.onBeforeSendHeaders.addListener(stateManager._stripMetadata, {
'urls': stateManager.validHosts
}, [WebRequest.BLOCKING, WebRequest.HEADERS]);
} }
}); });
......
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
<script src="../../core/files.js"></script> <script src="../../core/files.js"></script>
<script src="../../core/resources.js"></script> <script src="../../core/resources.js"></script>
<script src="../../core/mappings.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/state-manager.js"></script>
<script src="../../core/request-analyzer.js"></script> <script src="../../core/request-analyzer.js"></script>
<script src="../../core/interceptor.js"></script> <script src="../../core/interceptor.js"></script>
......
Supports Markdown
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