Skip to content

Commit

Permalink
fix patchCSP option
Browse files Browse the repository at this point in the history
  • Loading branch information
tophf committed Nov 5, 2024
1 parent 1426680 commit ce03a9e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/background/style-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {kUrl, UCD} from '/js/consts';
import {API} from '/js/msg';
import * as prefs from '/js/prefs';
import {calcStyleDigest, styleCodeEmpty} from '/js/sections-util';
import {CHROME, FIREFOX} from '/js/ua';
import {CHROME} from '/js/ua';
import * as URLS from '/js/urls';
import {deepEqual, isEmptyObj, mapObj, stringAsRegExpStr, tryRegExp, tryURL} from '/js/util';
import {broadcast, broadcastExtension} from './broadcast';
Expand Down Expand Up @@ -238,7 +238,7 @@ export function getSectionsByUrl(url, id, isInitialApply) {
dark: isTop && colorScheme.isDark,
// TODO: enable in FF when it supports sourceURL comment in style elements (also options.html)
name: CHROME && p.exposeStyleName,
nonce: FIREFOX && tabMan.get(tab.id, 'nonce', frameId),
nonce: tabMan.get(tab.id, 'nonce', frameId),
top: isInitialApply && p.exposeIframes && (
isTop ? '' // apply.js will use location.origin
: getUrlOrigin(tab.url || tabMan.get(sender.tabId || tab.id, kUrl))
Expand Down
29 changes: 19 additions & 10 deletions src/background/style-via-webrequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const ownId = chrome.runtime.id;
const kSetCookie = 'set-cookie'; // must be lowercase
const kSubFrame = 'sub_frame';
const rxHOST = /^('non(e|ce-.+?)'|(https?:\/\/)?[^']+?[^:'])$/; // strips CSP sources covered by *
const rxNONCE = FIREFOX && /(?:^|[;,])\s*style-src\s+[^;,]*?'nonce-([-+/=\w]+)'/;
const rxNONCE = /(?:^|[;,])\s*style-src\s+[^;,]*?'nonce-([-+/=\w]+)'/;
const BLOB_URL_PREFIX = 'blob:' + ownRoot;
const WEBNAV_FILTER = {url: [{urlPrefix: 'http'}]};
const WR_FILTER = {
Expand Down Expand Up @@ -106,14 +106,18 @@ function toggle(prefKey) {
return;
}
let v;
if (!process.env.MV3 && (FIREFOX || (xhr || csp) !== (curXHR || curCSP))) {
if (process.env.BUILD === 'firefox' || FIREFOX || (
process.env.MV3
? csp !== curCSP
: (xhr || csp) !== (curXHR || curCSP)
)) {
v = chrome.webRequest.onHeadersReceived;
// unregister first since new registrations are additive internally
toggleListener(v, false, modifyHeaders);
toggleListener(v, true, modifyHeaders, WR_FILTER, [
'blocking',
'responseHeaders',
xhr && chrome.webRequest.OnHeadersReceivedOptions.EXTRA_HEADERS,
!process.env.MV3 && xhr && chrome.webRequest.OnHeadersReceivedOptions.EXTRA_HEADERS,
].filter(Boolean));
}
if (mv3init || off !== curOFF) {
Expand Down Expand Up @@ -212,7 +216,7 @@ function modifyHeaders(req) {
const secs = payload.sections;
const csp = (FIREFOX || curCSP) && findHeader(responseHeaders, 'content-security-policy');
if (csp) {
const m = FIREFOX && csp.value.match(rxNONCE);
const m = csp.value.match(rxNONCE);
if (m) tabMan.set(req.tabId, 'nonce', req.frameId, payload.cfg.nonce = m[1]);
// We don't change CSP if there are no styles when the page is loaded
// TODO: show a reminder in the popup to reload the tab when the user enables a style
Expand All @@ -222,12 +226,17 @@ function modifyHeaders(req) {
removePreloadedStyles(req, key, data);
return;
}
const blobId = curXHR && (data.blobId ??=
!process.env.MV3 && URL.createObjectURL(makeBlob(payload)).slice(BLOB_URL_PREFIX.length)
);
const cookie = blobId && makeXhrCookie(blobId);
if (blobId && (!process.env.MV3 || !findHeader(responseHeaders, kSetCookie, cookie))) {
responseHeaders.push({name: kSetCookie, value: cookie});
let blobId;
if (curXHR && (
blobId = (data.blobId ??=
!process.env.MV3 && URL.createObjectURL(makeBlob(payload)).slice(BLOB_URL_PREFIX.length)
))) {
blobId = makeXhrCookie(blobId);
if (!process.env.MV3 || !findHeader(responseHeaders, kSetCookie, blobId)) {
responseHeaders.push({name: kSetCookie, value: blobId});
} else {
blobId = false;
}
}
if (blobId || csp && curCSP) {
return {responseHeaders};
Expand Down

0 comments on commit ce03a9e

Please sign in to comment.