httpsb.js 7.0 KB

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