xiMatrix

filter net requests according to source, destination and type  https://addons.mozilla.org/firefox/addon/ximatrix/
git clone https://git.ce9e.org/xiMatrix.git

commit
ee274ff052b9333f961dcc98a9a372dbeac1476c
parent
8813c660c9c02c21c6f97353fef2106263e29fb8
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2022-07-30 18:14
split out some code to shared.js

Diffstat

M bg.js 55 +++++++++++--------------------------------------------
M manifest.json 2 +-
M popup.html 1 +
M popup.js 23 +----------------------
A shared.js 38 ++++++++++++++++++++++++++++++++++++++

5 files changed, 52 insertions, 67 deletions


diff --git a/bg.js b/bg.js

@@ -1,26 +1,8 @@
    1     1 /* global browser */
    2     2 
    3    -1 const TYPES = {
    4    -1     'stylesheet': 'css',
    5    -1     'font': 'font',
    6    -1     'image': 'media',
    7    -1     'imageset': 'media',
    8    -1     'media': 'media',
    9    -1     'script': 'script',
   10    -1     'beacon': 'xhr',
   11    -1     'xmlhttprequest': 'xhr',
   12    -1     'websocket': 'xhr',
   13    -1     'sub_frame': 'frame',
   14    -1 };
   15    -1 
   16     3 var rules = {};
   17     4 var requests = {};
   18     5 
   19    -1 var getHostname = function(url) {
   20    -1     var u = new URL(url);
   21    -1     return u.hostname;
   22    -1 };
   23    -1 
   24     6 var setRule = function(context, hostname, type, rule) {
   25     7     if (hostname === 'first-party') {
   26     8         context = '*';
@@ -64,26 +46,6 @@ var clearRequests = function(tabId) {
   64    46     }
   65    47 };
   66    48 
   67    -1 var shouldAllow = function(context, hostname, type) {
   68    -1     var hostnames = ['*', hostname];
   69    -1     if (context === hostname) {
   70    -1         hostnames.push('first-party');
   71    -1     }
   72    -1     var parts = hostname.split('.');
   73    -1     while (parts.length > 2) {
   74    -1         parts.shift();
   75    -1         hostnames.push(parts.join('.'));
   76    -1     }
   77    -1 
   78    -1     return [context, '*'].some(c => {
   79    -1         return rules[c] && hostnames.some(h => {
   80    -1             return rules[c][h] && [type, '*'].some(t => {
   81    -1                 return rules[c][h][t];
   82    -1             });
   83    -1         });
   84    -1     });
   85    -1 };
   86    -1 
   87    49 var getCurrentTab = function() {
   88    50     return browser.tabs.query({
   89    51         active: true,
@@ -95,9 +57,14 @@ browser.runtime.onMessage.addListener(msg => {
   95    57     if (msg.type === 'get') {
   96    58         return getCurrentTab().then(tab => {
   97    59             var context = getHostname(tab.url);
   -1    60 
   -1    61             var restrictedRules = {};
   -1    62             restrictedRules['*'] = rules['*'] || {};
   -1    63             restrictedRules[context] = rules[context] || {};
   -1    64 
   98    65             return {
   99    66                 context: context,
  100    -1                 rules: rules,
   -1    67                 rules: restrictedRules,
  101    68                 requests: requests[tab.id] || {},
  102    69             };
  103    70         });
@@ -126,24 +93,24 @@ browser.webRequest.onBeforeRequest.addListener(details => {
  126    93         context = getHostname(details.frameAncestors[last].url);
  127    94     }
  128    95     var hostname = getHostname(details.url);
  129    -1     var type = TYPES[details.type] || 'other';
   -1    96     var type = TYPE_MAP[details.type] || 'other';
  130    97 
  131    98     pushRequest(details.tabId, hostname, type);
  132    99 
  133    -1     return {cancel: !shouldAllow(context, hostname, type)};
   -1   100     return {cancel: !shouldAllow(rules, context, hostname, type)};
  134   101 }, {urls: ['<all_urls>']}, ['blocking']);
  135   102 
  136   103 browser.webRequest.onHeadersReceived.addListener(function(details) {
  137   104     var context = getHostname(details.url);
  138   105     var policy = [];
  139   106 
  140    -1     if (!shouldAllow(context, 'inline', 'css')) {
   -1   107     if (!shouldAllow(rules, context, 'inline', 'css')) {
  141   108         policy.push("style-src 'self' *");
  142   109     }
  143    -1     if (!shouldAllow(context, 'inline', 'script')) {
   -1   110     if (!shouldAllow(rules, context, 'inline', 'script')) {
  144   111         policy.push("script-src 'self' *");
  145   112     }
  146    -1     if (!shouldAllow(context, 'inline', 'media')) {
   -1   113     if (!shouldAllow(rules, context, 'inline', 'media')) {
  147   114         policy.push("img-src 'self' *");
  148   115     }
  149   116 

diff --git a/manifest.json b/manifest.json

@@ -14,7 +14,7 @@
   14    14     "128": "icon-128.png"
   15    15   },
   16    16   "background": {
   17    -1     "scripts": ["bg.js"]
   -1    17     "scripts": ["shared.js", "bg.js"]
   18    18   },
   19    19   "permissions": [
   20    20     "storage",

diff --git a/popup.html b/popup.html

@@ -5,6 +5,7 @@
    5     5 </head>
    6     6 <body>
    7     7 	<table></table>
   -1     8 	<script src="shared.js"></script>
    8     9 	<script src="popup.js"></script>
    9    10 </body>
   10    11 </html>

diff --git a/popup.js b/popup.js

@@ -1,7 +1,5 @@
    1     1 /* global browser */
    2     2 
    3    -1 var TYPES = ['font', 'css', 'media', 'script', 'xhr', 'frame', 'other'];
    4    -1 
    5     3 var table = document.querySelector('table');
    6     4 
    7     5 var sendMessage = function(type, data) {
@@ -41,29 +39,10 @@ var getHostnames = function(data) {
   41    39 };
   42    40 
   43    41 sendMessage('get').then(data => {
   44    -1     var shouldAllow = function(context, hostname, type) {
   45    -1         var hostnames = ['*', hostname];
   46    -1         if (context === hostname) {
   47    -1             hostnames.push('first-party');
   48    -1         }
   49    -1         var parts = hostname.split('.');
   50    -1         while (parts.length > 2) {
   51    -1             parts.shift();
   52    -1             hostnames.push(parts.join('.'));
   53    -1         }
   54    -1 
   55    -1         return [context, '*'].some(c => {
   56    -1             return data.rules[c] && hostnames.some(h => {
   57    -1                 return data.rules[c][h] && [type, '*'].some(t => {
   58    -1                     return !!data.rules[c][h][t];
   59    -1                 });
   60    -1             });
   61    -1         });
   62    -1     };
   63    -1 
   64    42     var updateInherit = function() {
   65    43         table.querySelectorAll('input').forEach(input => {
   66    44             input.classList.toggle('inherit-allow', shouldAllow(
   -1    45                 data.rules,
   67    46                 data.context,
   68    47                 input.dataset.hostname,
   69    48                 input.dataset.type,

diff --git a/shared.js b/shared.js

@@ -0,0 +1,38 @@
   -1     1 const TYPES = ['font', 'css', 'media', 'script', 'xhr', 'frame', 'other'];
   -1     2 const TYPE_MAP = {
   -1     3     'stylesheet': 'css',
   -1     4     'font': 'font',
   -1     5     'image': 'media',
   -1     6     'imageset': 'media',
   -1     7     'media': 'media',
   -1     8     'script': 'script',
   -1     9     'beacon': 'xhr',
   -1    10     'xmlhttprequest': 'xhr',
   -1    11     'websocket': 'xhr',
   -1    12     'sub_frame': 'frame',
   -1    13 };
   -1    14 
   -1    15 var getHostname = function(url) {
   -1    16     var u = new URL(url);
   -1    17     return u.hostname;
   -1    18 };
   -1    19 
   -1    20 var shouldAllow = function(rules, context, hostname, type) {
   -1    21     var hostnames = ['*', hostname];
   -1    22     if (context === hostname) {
   -1    23         hostnames.push('first-party');
   -1    24     }
   -1    25     var parts = hostname.split('.');
   -1    26     while (parts.length > 2) {
   -1    27         parts.shift();
   -1    28         hostnames.push(parts.join('.'));
   -1    29     }
   -1    30 
   -1    31     return [context, '*'].some(c => {
   -1    32         return rules[c] && hostnames.some(h => {
   -1    33             return rules[c][h] && [type, '*'].some(t => {
   -1    34                 return !!rules[c][h][t];
   -1    35             });
   -1    36         });
   -1    37     });
   -1    38 };