Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: mv2 firefox csp header #27770

Merged
merged 53 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
f8014e2
fix: mv2 firefox csp header
itsyoboieltr Oct 10, 2024
51e7033
Merge branch 'develop' into fix-firefox-csp
itsyoboieltr Oct 11, 2024
c6bd1bd
feat: nonce
itsyoboieltr Oct 14, 2024
1905247
fix: nonce
itsyoboieltr Oct 15, 2024
cd551fd
fix: random url
itsyoboieltr Oct 15, 2024
a35b17e
Merge branch 'develop' into fix-firefox-csp
itsyoboieltr Oct 15, 2024
d1a84c2
fix: webpack tests
itsyoboieltr Oct 15, 2024
885c320
fix: regex
itsyoboieltr Oct 15, 2024
ba383e4
fix: regex whitespace
itsyoboieltr Oct 15, 2024
c048d41
feat: addNonceToCsp with tests
itsyoboieltr Oct 15, 2024
dc0d053
Merge branch 'develop' into fix-firefox-csp
itsyoboieltr Oct 17, 2024
95b3ad8
fix: limit types
itsyoboieltr Oct 17, 2024
ab2896d
fix: getPlatform
itsyoboieltr Oct 17, 2024
9a7cd35
feat: CSP
itsyoboieltr Oct 17, 2024
d1e0bce
fix: new RegExp
itsyoboieltr Oct 18, 2024
267a35c
feat: comments
itsyoboieltr Oct 18, 2024
52053ab
feat: array of directives
itsyoboieltr Oct 18, 2024
8f68b2a
fix: lint
itsyoboieltr Oct 21, 2024
9baaa86
feat: respect whitespace
itsyoboieltr Oct 21, 2024
fe9188e
feat: make it simpler
itsyoboieltr Oct 22, 2024
f32f598
fix: filename
itsyoboieltr Oct 22, 2024
2e53360
Merge branch 'develop' into fix-firefox-csp
itsyoboieltr Oct 22, 2024
f72aa1d
fix: casing
itsyoboieltr Oct 22, 2024
a939b23
feat: tests
itsyoboieltr Oct 22, 2024
fe6f667
Merge branch 'develop' into fix-firefox-csp
itsyoboieltr Oct 24, 2024
3d903f9
Merge branch 'develop' into fix-firefox-csp
itsyoboieltr Oct 25, 2024
8e65468
feat: nonce explanation
itsyoboieltr Oct 25, 2024
ce4709f
feat: nonceExpression
itsyoboieltr Oct 25, 2024
5483bd8
feat: checkURLForProviderInjection
itsyoboieltr Oct 25, 2024
83f3908
feat: e2e test
itsyoboieltr Oct 30, 2024
a014742
fix: getRuntimeURLExpression placement
itsyoboieltr Oct 30, 2024
851cc5e
fix: location mock
itsyoboieltr Oct 30, 2024
73a22d3
fix: placement
itsyoboieltr Oct 30, 2024
30494d5
Merge branch 'develop' into fix-firefox-csp
itsyoboieltr Oct 31, 2024
031f530
Update development/webpack/utils/plugins/SelfInjectPlugin/types.ts
itsyoboieltr Oct 31, 2024
dab24be
feat: @types/serve-handler
itsyoboieltr Oct 31, 2024
49b6924
Merge branch 'develop' into fix-firefox-csp
itsyoboieltr Oct 31, 2024
41498b0
fix: csp header in e2e
itsyoboieltr Oct 31, 2024
8a3f90a
Merge branch 'develop' into fix-firefox-csp
itsyoboieltr Oct 31, 2024
b6e2e25
Update content-security-policy.spec.ts
itsyoboieltr Oct 31, 2024
31c2349
Merge branch 'develop' into fix-firefox-csp
itsyoboieltr Oct 31, 2024
75e16be
feat: ui toggle
itsyoboieltr Nov 4, 2024
b482cac
Update background.js
itsyoboieltr Nov 4, 2024
289dc50
Update settings-search.test.js
itsyoboieltr Nov 4, 2024
5163ec9
fix: description
itsyoboieltr Nov 5, 2024
ab1f3df
feat: only show on firefox
itsyoboieltr Nov 5, 2024
33508e7
Update settings-search.test.js
itsyoboieltr Nov 5, 2024
0fb3c3f
Update settings-search.test.js
itsyoboieltr Nov 5, 2024
f50e960
Merge branch 'develop' into fix-firefox-csp
itsyoboieltr Nov 6, 2024
07d10be
Merge branch 'develop' into fix-firefox-csp
itsyoboieltr Nov 6, 2024
3bf6b9b
fix
itsyoboieltr Nov 6, 2024
6e9ed23
fixture
itsyoboieltr Nov 6, 2024
cec02cb
Update fixture-builder.js
itsyoboieltr Nov 6, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions app/scripts/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import {
// TODO: Remove restricted import
// eslint-disable-next-line import/no-restricted-paths
import { getCurrentChainId } from '../../ui/selectors';
import { addNonceToCsp } from '../../shared/modules/add-nonce-to-csp';
import migrations from './migrations';
import Migrator from './lib/migrator';
import ExtensionPlatform from './platforms/extension';
Expand Down Expand Up @@ -327,6 +328,27 @@ function maybeDetectPhishing(theController) {
);
}

/**
* Overrides the Content-Security-Policy Header, acting as a workaround for an MV2 Firefox Bug.
*/
function overrideContentSecurityPolicyHeader() {
browser.webRequest.onHeadersReceived.addListener(
({ responseHeaders }) => {
for (const header of responseHeaders) {
if (header.name.toLowerCase() === 'content-security-policy') {
header.value = addNonceToCsp(
header.value,
btoa(browser.runtime.getURL('/')),
);
}
}
return { responseHeaders };
},
{ urls: ['http://*/*', 'https://*/*'] },
['blocking', 'responseHeaders'],
);
}

// These are set after initialization
let connectRemote;
let connectExternalExtension;
Expand Down Expand Up @@ -473,6 +495,10 @@ async function initialize() {

if (!isManifestV3) {
await loadPhishingWarningPage();
const { name } = await browser.runtime.getBrowserInfo();
if (name === PLATFORM_FIREFOX) {
overrideContentSecurityPolicyHeader();
}
}
await sendReadyMessageToTabs();
log.info('MetaMask initialization complete.');
Expand Down
2 changes: 1 addition & 1 deletion development/build/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ function getBuildName({
function makeSelfInjecting(filePath) {
const fileContents = readFileSync(filePath, 'utf8');
const textContent = JSON.stringify(fileContents);
const js = `{let d=document,s=d.createElement('script');s.textContent=${textContent};d.documentElement.appendChild(s).remove();}`;
const js = `{let d=document,s=d.createElement('script');s.textContent=${textContent};s.nonce=btoa(browser.runtime.getURL('/'));d.documentElement.appendChild(s).remove();}`;
writeFileSync(filePath, js, 'utf8');
}

Expand Down
4 changes: 2 additions & 2 deletions development/webpack/test/plugins.SelfInjectPlugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ describe('SelfInjectPlugin', () => {
// reference the `sourceMappingURL`
assert.strictEqual(
newSource,
`{let d=document,s=d.createElement('script');s.textContent="${source}\\n//# sourceMappingURL=${filename}.map"+\`\\n//# sourceURL=\${(globalThis.browser||chrome).runtime.getURL("${filename}")};\`;d.documentElement.appendChild(s).remove()}`,
`{let d=document,s=d.createElement('script');s.textContent="${source}\\n//# sourceMappingURL=${filename}.map"+\`\\n//# sourceURL=\${(globalThis.browser||chrome).runtime.getURL("${filename}")};\`;s.nonce=btoa(browser.runtime.getURL('/'));d.documentElement.appendChild(s).remove()}`,
);
} else {
// the new source should NOT reference the new sourcemap, since it's
Expand All @@ -66,7 +66,7 @@ describe('SelfInjectPlugin', () => {
// console.
assert.strictEqual(
newSource,
`{let d=document,s=d.createElement('script');s.textContent="console.log(3);"+\`\\n//# sourceURL=\${(globalThis.browser||chrome).runtime.getURL("${filename}")};\`;d.documentElement.appendChild(s).remove()}`,
`{let d=document,s=d.createElement('script');s.textContent="console.log(3);"+\`\\n//# sourceURL=\${(globalThis.browser||chrome).runtime.getURL("${filename}")};\`;s.nonce=btoa(browser.runtime.getURL('/'));d.documentElement.appendChild(s).remove()}`,
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ export class SelfInjectPlugin {
`\`\\n//# sourceURL=\${${this.options.sourceUrlExpression(file)}};\``,
);
newSource.add(`;`);
newSource.add(`s.nonce=btoa(browser.runtime.getURL('/'));`);
// add and immediately remove the script to avoid modifying the DOM.
newSource.add(`d.documentElement.appendChild(s).remove()`);
newSource.add(`}`);
Expand Down
77 changes: 77 additions & 0 deletions shared/modules/add-nonce-to-csp.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { addNonceToCsp } from './add-nonce-to-csp';

describe('addNonceToCsp', () => {
it('empty string', () => {
const input = '';
const expected = '';
const output = addNonceToCsp(input, 'test');
expect(output).toBe(expected);
});

it('one empty directive', () => {
const input = 'script-src';
const expected = `script-src 'nonce-test'`;
const output = addNonceToCsp(input, 'test');
expect(output).toBe(expected);
});

it('one directive, one value', () => {
const input = 'script-src default.example';
const expected = `script-src default.example 'nonce-test'`;
const output = addNonceToCsp(input, 'test');
expect(output).toBe(expected);
});

it('one directive, two values', () => {
const input = "script-src 'self' default.example";
const expected = `script-src 'self' default.example 'nonce-test'`;
const output = addNonceToCsp(input, 'test');
expect(output).toBe(expected);
});

it('multiple directives', () => {
const input =
"default-src 'self'; script-src 'unsafe-eval' scripts.example; object-src; style-src styles.example";
const expected = `default-src 'self'; script-src 'unsafe-eval' scripts.example 'nonce-test'; object-src; style-src styles.example`;
const output = addNonceToCsp(input, 'test');
expect(output).toBe(expected);
});

it('non-ASCII directives', () => {
const input = 'script-src default.example;\u0080;style-src style.example';
const expected = `script-src default.example 'nonce-test';\u0080;style-src style.example`;
const output = addNonceToCsp(input, 'test');
expect(output).toBe(expected);
});

it('uppercase directive names', () => {
const input = 'SCRIPT-SRC DEFAULT.EXAMPLE';
const expected = `SCRIPT-SRC DEFAULT.EXAMPLE 'nonce-test'`;
const output = addNonceToCsp(input, 'test');
expect(output).toBe(expected);
});

it('duplicate directive names', () => {
const input =
'default-src default.example; script-src script.example; script-src script.example';
const expected = `default-src default.example; script-src script.example 'nonce-test'; script-src script.example`;
const output = addNonceToCsp(input, 'test');
expect(output).toBe(expected);
});

it('nonce value contains script-src', () => {
const input =
"default-src 'self' 'nonce-script-src'; script-src 'self' https://example.com";
const expected = `default-src 'self' 'nonce-script-src'; script-src 'self' https://example.com 'nonce-test'`;
const output = addNonceToCsp(input, 'test');
expect(output).toBe(expected);
});

it('url value contains script-src', () => {
const input =
"default-src 'self' https://script-src.com; script-src 'self' https://example.com";
const expected = `default-src 'self' https://script-src.com; script-src 'self' https://example.com 'nonce-test'`;
const output = addNonceToCsp(input, 'test');
expect(output).toBe(expected);
});
});
13 changes: 13 additions & 0 deletions shared/modules/add-nonce-to-csp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* Adds a nonce value to the script-src directive of the Content-Security-Policy Header.
*
* @param header - the Content-Security-Policy Header
* @param nonce - the nonce value to add
* @returns the Content-Security-Policy Header with the nonce value added
*/
export const addNonceToCsp = (header: string, nonce: string) => {
return header.replace(
/(^|;[\t\n\f\r ]*)script-src([^;]*)/iu,
(match) => `${match} 'nonce-${nonce}'`,
);
};