contentscript-start.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*******************************************************************************
  2. ηMatrix - a browser extension to black/white list requests.
  3. Copyright (C) 2017-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. 'use strict';
  19. // Injected into content pages
  20. (function () {
  21. if (typeof vAPI !== 'object') {
  22. return;
  23. }
  24. vAPI.selfWorkerSrcReported = vAPI.selfWorkerSrcReported || false;
  25. var reGoodWorkerSrc = /(?:frame|worker)-src[^;,]+?'none'/;
  26. var handler = function(ev) {
  27. if (ev.isTrusted !== true
  28. || ev.originalPolicy.includes('report-uri about:blank') === false) {
  29. return false;
  30. }
  31. // Firefox and Chromium differs in how they fill the
  32. // 'effectiveDirective' property.
  33. // ηMatrix: what does Pale Moon/Basilisk do?
  34. if (ev.effectiveDirective.startsWith('worker-src') === false
  35. && ev.effectiveDirective.startsWith('frame-src') === false) {
  36. return false;
  37. }
  38. // Further validate that the policy violation is relevant to ηMatrix:
  39. // the event still could have been fired as a result of a CSP header
  40. // not injected by ηMatrix.
  41. if (reGoodWorkerSrc.test(ev.originalPolicy) === false) {
  42. return false;
  43. }
  44. // We do not want to report internal resources more than once.
  45. // However, we do want to report external resources each time.
  46. // TODO: this could eventually lead to duplicated reports for external
  47. // resources if another extension uses the same approach as
  48. // ηMatrix. Think about what could be done to avoid duplicate
  49. // reports.
  50. if (ev.blockedURI.includes('://') === false) {
  51. if (vAPI.selfWorkerSrcReported) {
  52. return true;
  53. }
  54. vAPI.selfWorkerSrcReported = true;
  55. }
  56. vAPI.messaging.send('contentscript.js', {
  57. what: 'securityPolicyViolation',
  58. directive: 'worker-src',
  59. blockedURI: ev.blockedURI,
  60. documentURI: ev.documentURI,
  61. blocked: ev.disposition === 'enforce'
  62. });
  63. return true;
  64. };
  65. document.addEventListener('securitypolicyviolation', function (ev) {
  66. if (!handler(ev)) {
  67. return;
  68. }
  69. ev.stopPropagation();
  70. ev.preventDefault();
  71. }, true);
  72. })();