Skip to content

Commit

Permalink
fix 0195dff: restore ";" before code, skip // comments
Browse files Browse the repository at this point in the history
  • Loading branch information
tophf committed Aug 22, 2024
1 parent 51c9ee3 commit e14b52a
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/background/utils/preinject.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const API_EXTRA = [
const findCspHeader = h => h.name.toLowerCase() === 'content-security-policy';
const CSP_RE = /(?:^|[;,])\s*(?:script-src(-elem)?|(d)efault-src)(\s+[^;,]+)/g;
const NONCE_RE = /'nonce-([-+/=\w]+)'/;
const SAFE_IIFE_RE = /^\s*(?:\/\*[\s\S]*?\*\/\s*)*[!;]/u;
const UNSAFE_CONCAT_RE = /^\s*(?:\/\*[\s\S]*?\*\/|\/\/.*[\r\n]+|\s+)*[-+[(]/u;
const UNSAFE_INLINE = "'unsafe-inline'";
/** These bags are reused in cache to reduce memory usage,
* CACHE_KEYS is for removeStaleCacheEntry */
Expand Down Expand Up @@ -492,13 +492,16 @@ function prepareScript(script, env) {
const req = require[pathMap[url] || url];
if (/\S/.test(req)) {
injectedCode.push(...[
tmp && !SAFE_IIFE_RE.test(req) && ';',
tmp && UNSAFE_CONCAT_RE.test(req) && ';',
req,
!NEWLINE_END_RE.test(req) && '\n',
].filter(Boolean));
tmp = true;
}
}
if (tmp && UNSAFE_CONCAT_RE.test(code)) {
injectedCode.push(';');
}
codeIndex = injectedCode.length;
injectedCode.push(code);
// adding a new line in case the code ends with a line comment
Expand Down

0 comments on commit e14b52a

Please sign in to comment.