httpsb.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*******************************************************************************
  2. ηMatrix - a browser extension to black/white list requests.
  3. Copyright (C) 2014-2019 Raymond Hill
  4. Copyright (C) 2019-2022 Alessio Vanni
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see {http://www.gnu.org/licenses/}.
  15. Home: https://gitlab.com/vannilla/ematrix
  16. uMatrix Home: https://github.com/gorhill/uMatrix
  17. */
  18. /* global chrome, ηMatrix */
  19. 'use strict';
  20. /******************************************************************************/
  21. (function() {
  22. Cu.import('chrome://ematrix/content/lib/UriTools.jsm');
  23. var ηm = ηMatrix;
  24. ηm.pMatrix = new ηm.Matrix();
  25. ηm.pMatrix.setSwitch('matrix-off', 'about-scheme', 1);
  26. ηm.pMatrix.setSwitch('matrix-off', 'chrome-extension-scheme', 1);
  27. ηm.pMatrix.setSwitch('matrix-off', 'chrome-scheme', 1);
  28. ηm.pMatrix.setSwitch('matrix-off', 'moz-extension-scheme', 1);
  29. ηm.pMatrix.setSwitch('matrix-off', 'opera-scheme', 1);
  30. // https://discourse.mozilla.org/t/support-umatrix/5131/157
  31. ηm.pMatrix.setSwitch('matrix-off', 'wyciwyg-scheme', 1);
  32. ηm.pMatrix.setSwitch('matrix-off', 'behind-the-scene', 1);
  33. ηm.pMatrix.setSwitch('referrer-spoof', 'behind-the-scene', 2);
  34. ηm.pMatrix.setSwitch('https-strict', 'behind-the-scene', 2);
  35. // Global rules
  36. ηm.pMatrix.setSwitch('referrer-spoof', '*', 1);
  37. ηm.pMatrix.setSwitch('noscript-spoof', '*', 1);
  38. ηm.pMatrix.setCell('*', '*', '*', ηm.Matrix.Red);
  39. ηm.pMatrix.setCell('*', '*', 'script', ηm.Matrix.Red);
  40. ηm.pMatrix.setCell('*', '*', 'css', ηm.Matrix.Green);
  41. ηm.pMatrix.setCell('*', '*', 'image', ηm.Matrix.Green);
  42. ηm.pMatrix.setCell('*', '*', 'frame', ηm.Matrix.Red);
  43. // 1st-party rules
  44. ηm.pMatrix.setCell('*', '1st-party', '*', ηm.Matrix.Green);
  45. ηm.pMatrix.setCell('*', '1st-party', 'frame', ηm.Matrix.Green);
  46. ηm.tMatrix = new ηm.Matrix();
  47. ηm.tMatrix.assign(ηm.pMatrix);
  48. })();
  49. /******************************************************************************/
  50. ηMatrix.hostnameFromURL = function(url) {
  51. var hn = UriTools.hostnameFromURI(url);
  52. return hn === '' ? '*' : hn;
  53. };
  54. ηMatrix.scopeFromURL = ηMatrix.hostnameFromURL;
  55. /******************************************************************************/
  56. ηMatrix.evaluateURL = function(srcURL, desHostname, type) {
  57. var srcHostname = UriTools.hostnameFromURI(srcURL);
  58. return this.tMatrix.evaluateCellZXY(srcHostname, desHostname, type);
  59. };
  60. /******************************************************************************/
  61. // Whitelist something
  62. ηMatrix.whitelistTemporarily = function(srcHostname, desHostname, type) {
  63. this.tMatrix.whitelistCell(srcHostname, desHostname, type);
  64. };
  65. ηMatrix.whitelistPermanently = function(srcHostname, desHostname, type) {
  66. if ( this.pMatrix.whitelistCell(srcHostname, desHostname, type) ) {
  67. this.saveMatrix();
  68. }
  69. };
  70. /******************************************************************************/
  71. // Auto-whitelisting the `all` cell is a serious action, hence this will be
  72. // done only from within a scope.
  73. ηMatrix.autoWhitelistAllTemporarily = function(pageURL) {
  74. var srcHostname = UriTools.hostnameFromURI(pageURL);
  75. if ( this.mustBlock(srcHostname, '*', '*') === false ) {
  76. return false;
  77. }
  78. this.tMatrix.whitelistCell(srcHostname, '*', '*');
  79. return true;
  80. };
  81. /******************************************************************************/
  82. // Blacklist something
  83. ηMatrix.blacklistTemporarily = function(srcHostname, desHostname, type) {
  84. this.tMatrix.blacklistCell(srcHostname, desHostname, type);
  85. };
  86. ηMatrix.blacklistPermanently = function(srcHostname, desHostname, type) {
  87. if ( this.pMatrix.blacklist(srcHostname, desHostname, type) ) {
  88. this.saveMatrix();
  89. }
  90. };
  91. /******************************************************************************/
  92. // Remove something from both black and white lists.
  93. ηMatrix.graylistTemporarily = function(srcHostname, desHostname, type) {
  94. this.tMatrix.graylistCell(srcHostname, desHostname, type);
  95. };
  96. ηMatrix.graylistPermanently = function(srcHostname, desHostname, type) {
  97. if ( this.pMatrix.graylistCell(srcHostname, desHostname, type) ) {
  98. this.saveMatrix();
  99. }
  100. };
  101. /******************************************************************************/
  102. // TODO: Should type be transposed by the caller or in place here? Not an
  103. // issue at this point but to keep in mind as this function is called
  104. // more and more from different places.
  105. ηMatrix.filterRequest = function(fromURL, type, toURL) {
  106. // Block request?
  107. var srcHostname = this.hostnameFromURL(fromURL);
  108. var desHostname = this.hostnameFromURL(toURL);
  109. // If no valid hostname, use the hostname of the source.
  110. // For example, this case can happen with data URI.
  111. if ( desHostname === '' ) {
  112. desHostname = srcHostname;
  113. }
  114. // Blocked by matrix filtering?
  115. return this.mustBlock(srcHostname, desHostname, type);
  116. };
  117. /******************************************************************************/
  118. ηMatrix.mustBlock = function(srcHostname, desHostname, type) {
  119. return this.tMatrix.mustBlock(srcHostname, desHostname, type);
  120. };
  121. ηMatrix.mustAllow = function(srcHostname, desHostname, type) {
  122. return this.mustBlock(srcHostname, desHostname, type) === false;
  123. };
  124. /******************************************************************************/
  125. // Commit temporary permissions.
  126. ηMatrix.commitPermissions = function(persist) {
  127. this.pMatrix.assign(this.tMatrix);
  128. if ( persist ) {
  129. this.saveMatrix();
  130. }
  131. };
  132. /******************************************************************************/
  133. // Reset all rules to their default state.
  134. ηMatrix.revertAllRules = function() {
  135. this.tMatrix.assign(this.pMatrix);
  136. };
  137. /******************************************************************************/
  138. ηMatrix.turnOff = function() {
  139. vAPI.app.start();
  140. };
  141. ηMatrix.turnOn = function() {
  142. vAPI.app.stop();
  143. };
  144. /******************************************************************************/
  145. ηMatrix.formatCount = function(count) {
  146. if ( typeof count !== 'number' ) {
  147. return '';
  148. }
  149. var s = count.toFixed(0);
  150. if ( count >= 1000 ) {
  151. if ( count < 10000 ) {
  152. s = '>' + s.slice(0,1) + 'K';
  153. } else if ( count < 100000 ) {
  154. s = s.slice(0,2) + 'K';
  155. } else if ( count < 1000000 ) {
  156. s = s.slice(0,3) + 'K';
  157. } else if ( count < 10000000 ) {
  158. s = s.slice(0,1) + 'M';
  159. } else {
  160. s = s.slice(0,-6) + 'M';
  161. }
  162. }
  163. return s;
  164. };