Skip to content

Commit

Permalink
fix: the issue with chrome.runtime.getURL introduced in Chrome 130 ca…
Browse files Browse the repository at this point in the history
…using CSP rejecting script due to different origin (GUID instead of chrome extension id) (#928)
  • Loading branch information
onv authored Oct 20, 2024
1 parent a6d882a commit f4eb4d4
Show file tree
Hide file tree
Showing 38 changed files with 99 additions and 58 deletions.
5 changes: 5 additions & 0 deletions .changeset/smart-bottles-warn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@crxjs/vite-plugin': major
---

fix: workaround for the issue with chrome.runtime.getURL introduced in Chrome 130 causing CSP rejecting script due to different origin (GUID instead of chrome extension id)
2 changes: 1 addition & 1 deletion packages/vite-plugin/src/client/es/hmr-client-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ declare const __SERVER_PORT__: string

/* -------- REDIRECT FETCH TO THE DEV SERVER ------- */

const ownOrigin = new URL(chrome.runtime.getURL('/')).origin
const ownOrigin = `chrome-extension://${chrome.runtime.id}`;
self.addEventListener('fetch', (fetchEvent) => {
const url = new URL(fetchEvent.request.url)
if (url.origin === ownOrigin) {
Expand Down
12 changes: 6 additions & 6 deletions packages/vite-plugin/src/node/archive/plugin-contentScripts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -420,11 +420,11 @@ export const pluginContentScripts: CrxPluginFn = ({
resources: ['**/*', '*'],
}

if (browser !== 'firefox') {
// change the extension origin on every reload
// not allowed in FF b/c FF does this by default
war.use_dynamic_url = true
}
// if (browser !== 'firefox') {
// // change the extension origin on every reload
// // not allowed in FF b/c FF does this by default
// war.use_dynamic_url = true
// }

manifest.web_accessible_resources.push(war)
} else {
Expand Down Expand Up @@ -522,7 +522,7 @@ export const pluginContentScripts: CrxPluginFn = ({
| WebAccessibleResourceByMatch = {
matches: script.matches,
resources: [...assets, ...imports],
use_dynamic_url: true,
use_dynamic_url: false,
}

if (css.size)
Expand Down
2 changes: 1 addition & 1 deletion packages/vite-plugin/src/node/defineManifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const defineManifest = (manifest: ManifestV3Export): ManifestV3Export =>
*/
export const defineDynamicResource = ({
matches = ['http://*/*', 'https://*/*'],
use_dynamic_url = true,
use_dynamic_url = false,
}: Omit<
WebAccessibleResourceByMatch,
'resources'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export const pluginWebAccessibleResources: CrxPluginFn = () => {
// all resources are web accessible
resources: ['**/*', '*'],
// change the extension origin on every reload
use_dynamic_url: true,
use_dynamic_url: false,
}

if (browser === 'firefox') {
Expand Down Expand Up @@ -163,7 +163,7 @@ export const pluginWebAccessibleResources: CrxPluginFn = () => {
resources: [...assets, ...imports],
use_dynamic_url: isDynamicScript
? dynamicScriptDynamicUrl
: true,
: false,
}

if (isDynamicScript || !injectCss) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Object {
"resources": Array [
"assets/content.js.hash1.js",
],
"use_dynamic_url": true,
"use_dynamic_url": false,
},
],
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Object {
"*",
"**/*",
],
"use_dynamic_url": true,
"use_dynamic_url": false,
},
],
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Object {
"resources": Array [
"assets/content.ts.hash1.js",
],
"use_dynamic_url": true,
"use_dynamic_url": false,
},
],
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Object {
"*",
"**/*",
],
"use_dynamic_url": true,
"use_dynamic_url": false,
},
],
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Object {
"resources": Array [
"assets/content.js.hash1.js",
],
"use_dynamic_url": true,
"use_dynamic_url": false,
},
],
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Object {
"*",
"**/*",
],
"use_dynamic_url": true,
"use_dynamic_url": false,
},
],
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Object {
"assets/declared-script.ts.hash1.js",
"assets/main-world.ts.hash2.js",
],
"use_dynamic_url": true,
"use_dynamic_url": false,
},
Object {
"matches": Array [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Object {
"*",
"**/*",
],
"use_dynamic_url": true,
"use_dynamic_url": false,
},
],
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Object {
"assets/index.ts.hash4.js",
"assets/index.ts.hash5.js",
],
"use_dynamic_url": true,
"use_dynamic_url": false,
},
],
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Object {
"*",
"**/*",
],
"use_dynamic_url": true,
"use_dynamic_url": false,
},
],
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ Object {
"assets/index.ts.hash6.js",
"assets/index.ts.hash7.js",
],
"use_dynamic_url": true,
"use_dynamic_url": false,
},
],
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Object {
"*",
"**/*",
],
"use_dynamic_url": true,
"use_dynamic_url": false,
},
],
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Object {
"resources": Array [
"assets/content.ts.hash2.js",
],
"use_dynamic_url": true,
"use_dynamic_url": false,
},
],
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Object {
"*",
"**/*",
],
"use_dynamic_url": true,
"use_dynamic_url": false,
},
],
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Object {
"assets/image.hash3.png",
"assets/script.ts.hash4.js",
],
"use_dynamic_url": true,
"use_dynamic_url": false,
},
],
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Object {
"*",
"**/*",
],
"use_dynamic_url": true,
"use_dynamic_url": false,
},
],
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Object {
"assets/image.hash3.png",
"assets/script.ts.hash4.js",
],
"use_dynamic_url": true,
"use_dynamic_url": false,
},
],
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Object {
"*",
"**/*",
],
"use_dynamic_url": true,
"use_dynamic_url": false,
},
],
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Object {
"assets/image.hash3.png",
"assets/script.ts.hash4.js",
],
"use_dynamic_url": true,
"use_dynamic_url": false,
},
],
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Object {
"*",
"**/*",
],
"use_dynamic_url": true,
"use_dynamic_url": false,
},
],
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Object {
"assets/content.tsx.hash2.js",
"assets/vendor.hash3.js",
],
"use_dynamic_url": true,
"use_dynamic_url": false,
},
],
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Object {
"*",
"**/*",
],
"use_dynamic_url": true,
"use_dynamic_url": false,
},
],
}
Expand All @@ -46,7 +46,7 @@ Array [
"src/content.tsx-loader.js",
"src/content.tsx.js",
"src/popup.html",
"vendor/chunk-D6CDYV2H.js__v--hash.js",
"vendor/chunk-MPSAKQTA.js__v--hash.js",
"vendor/crx-client-port.js",
"vendor/crx-client-preamble.js",
"vendor/id-__x00__react-jsx-dev-runtime.js",
Expand Down Expand Up @@ -170,15 +170,42 @@ export default App;
var _c;
$RefreshReg$(_c, \\"App\\");
if (import.meta.hot) {
let isReactRefreshBoundary = function(mod) {
if (mod == null || typeof mod !== \\"object\\") {
return false;
}
let hasExports = false;
let areAllExportsComponents = true;
for (const exportName in mod) {
hasExports = true;
if (exportName === \\"__esModule\\") {
continue;
}
const desc = Object.getOwnPropertyDescriptor(mod, exportName);
if (desc && desc.get) {
return false;
}
const exportValue = mod[exportName];
if (!RefreshRuntime.isLikelyComponentType(exportValue)) {
areAllExportsComponents = false;
}
}
return hasExports && areAllExportsComponents;
};
window.$RefreshReg$ = prevRefreshReg;
window.$RefreshSig$ = prevRefreshSig;
import.meta.hot.accept();
if (!window.__vite_plugin_react_timeout) {
window.__vite_plugin_react_timeout = setTimeout(() => {
window.__vite_plugin_react_timeout = 0;
RefreshRuntime.performReactRefresh();
}, 30);
}
import.meta.hot.accept((mod) => {
if (isReactRefreshBoundary(mod)) {
if (!window.__vite_plugin_react_timeout) {
window.__vite_plugin_react_timeout = setTimeout(() => {
window.__vite_plugin_react_timeout = 0;
RefreshRuntime.performReactRefresh();
}, 30);
}
} else {
import.meta.hot.invalidate();
}
});
}
"
`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Object {
"resources": Array [
"assets/content.ts.hash1.js",
],
"use_dynamic_url": true,
"use_dynamic_url": false,
},
],
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Object {
"*",
"**/*",
],
"use_dynamic_url": true,
"use_dynamic_url": false,
},
],
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Object {
"assets/svelte.hash.png",
"assets/vendor.hash.js",
],
"use_dynamic_url": true,
"use_dynamic_url": false,
},
],
}
Expand Down
Loading

0 comments on commit f4eb4d4

Please sign in to comment.