1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-06 00:13:28 +00:00

chore: [PM-28640] revert script injection change

* chore: revert script injection change

* Removed async

* Adjust tests.

* Revert fido2.background.ts changes.

---------

Co-authored-by: Andreas Coroiu <andreas.coroiu@gmail.com>
This commit is contained in:
Todd Martin
2025-12-02 13:24:22 -05:00
committed by GitHub
parent dd99190ca2
commit 57b6d8ba58
5 changed files with 8 additions and 30 deletions

View File

@@ -13,7 +13,6 @@ type SharedFido2ScriptRegistrationOptions = SharedFido2ScriptInjectionDetails &
matches: string[]; matches: string[];
excludeMatches: string[]; excludeMatches: string[];
allFrames: true; allFrames: true;
world?: "MAIN" | "ISOLATED";
}; };
type Fido2ExtensionMessage = { type Fido2ExtensionMessage = {

View File

@@ -203,7 +203,6 @@ describe("Fido2Background", () => {
{ file: Fido2ContentScript.PageScriptDelayAppend }, { file: Fido2ContentScript.PageScriptDelayAppend },
{ file: Fido2ContentScript.ContentScript }, { file: Fido2ContentScript.ContentScript },
], ],
world: "ISOLATED",
...sharedRegistrationOptions, ...sharedRegistrationOptions,
}); });
}); });

View File

@@ -176,7 +176,6 @@ export class Fido2Background implements Fido2BackgroundInterface {
{ file: await this.getFido2PageScriptAppendFileName() }, { file: await this.getFido2PageScriptAppendFileName() },
{ file: Fido2ContentScript.ContentScript }, { file: Fido2ContentScript.ContentScript },
], ],
world: "ISOLATED",
...this.sharedRegistrationOptions, ...this.sharedRegistrationOptions,
}); });
} }

View File

@@ -29,48 +29,38 @@ describe("FIDO2 page-script for manifest v2", () => {
expect(window.document.createElement).not.toHaveBeenCalled(); expect(window.document.createElement).not.toHaveBeenCalled();
}); });
it("appends the `page-script.js` file to the document head when the contentType is `text/html`", async () => { it("appends the `page-script.js` file to the document head when the contentType is `text/html`", () => {
const scriptContents = "test-script-contents";
jest.spyOn(window.document.head, "prepend").mockImplementation((node) => { jest.spyOn(window.document.head, "prepend").mockImplementation((node) => {
createdScriptElement = node as HTMLScriptElement; createdScriptElement = node as HTMLScriptElement;
return node; return node;
}); });
window.fetch = jest.fn().mockResolvedValue({
text: () => Promise.resolve(scriptContents),
} as Response);
// FIXME: Remove when updating file. Eslint update // FIXME: Remove when updating file. Eslint update
// eslint-disable-next-line @typescript-eslint/no-require-imports // eslint-disable-next-line @typescript-eslint/no-require-imports
require("./fido2-page-script-delay-append.mv2.ts"); require("./fido2-page-script-delay-append.mv2.ts");
await jest.runAllTimersAsync();
expect(window.document.createElement).toHaveBeenCalledWith("script"); expect(window.document.createElement).toHaveBeenCalledWith("script");
expect(chrome.runtime.getURL).toHaveBeenCalledWith(Fido2ContentScript.PageScript); expect(chrome.runtime.getURL).toHaveBeenCalledWith(Fido2ContentScript.PageScript);
expect(window.document.head.prepend).toHaveBeenCalledWith(expect.any(HTMLScriptElement)); expect(window.document.head.prepend).toHaveBeenCalledWith(expect.any(HTMLScriptElement));
expect(createdScriptElement.innerHTML).toBe(scriptContents); expect(createdScriptElement.src).toBe(`chrome-extension://id/${Fido2ContentScript.PageScript}`);
}); });
it("appends the `page-script.js` file to the document element if the head is not available", async () => { it("appends the `page-script.js` file to the document element if the head is not available", () => {
const scriptContents = "test-script-contents";
window.document.documentElement.removeChild(window.document.head); window.document.documentElement.removeChild(window.document.head);
jest.spyOn(window.document.documentElement, "prepend").mockImplementation((node) => { jest.spyOn(window.document.documentElement, "prepend").mockImplementation((node) => {
createdScriptElement = node as HTMLScriptElement; createdScriptElement = node as HTMLScriptElement;
return node; return node;
}); });
window.fetch = jest.fn().mockResolvedValue({
text: () => Promise.resolve(scriptContents),
} as Response);
// FIXME: Remove when updating file. Eslint update // FIXME: Remove when updating file. Eslint update
// eslint-disable-next-line @typescript-eslint/no-require-imports // eslint-disable-next-line @typescript-eslint/no-require-imports
require("./fido2-page-script-delay-append.mv2.ts"); require("./fido2-page-script-delay-append.mv2.ts");
await jest.runAllTimersAsync();
expect(window.document.createElement).toHaveBeenCalledWith("script"); expect(window.document.createElement).toHaveBeenCalledWith("script");
expect(chrome.runtime.getURL).toHaveBeenCalledWith(Fido2ContentScript.PageScript); expect(chrome.runtime.getURL).toHaveBeenCalledWith(Fido2ContentScript.PageScript);
expect(window.document.documentElement.prepend).toHaveBeenCalledWith( expect(window.document.documentElement.prepend).toHaveBeenCalledWith(
expect.any(HTMLScriptElement), expect.any(HTMLScriptElement),
); );
expect(createdScriptElement.innerHTML).toBe(scriptContents); expect(createdScriptElement.src).toBe(`chrome-extension://id/${Fido2ContentScript.PageScript}`);
}); });
}); });

View File

@@ -2,26 +2,17 @@
* This script handles injection of the FIDO2 override page script into the document. * This script handles injection of the FIDO2 override page script into the document.
* This is required for manifest v2, but will be removed when we migrate fully to manifest v3. * This is required for manifest v2, but will be removed when we migrate fully to manifest v3.
*/ */
void (async function (globalContext) { (function (globalContext) {
if (globalContext.document.contentType !== "text/html") { if (globalContext.document.contentType !== "text/html") {
return; return;
} }
const script = globalContext.document.createElement("script"); const script = globalContext.document.createElement("script");
// We're removing stack trace information in the page script instead
// eslint-disable-next-line @bitwarden/platform/no-page-script-url-leakage
script.src = chrome.runtime.getURL("content/fido2-page-script.js");
script.async = false; script.async = false;
const pageScriptUrl = chrome.runtime.getURL("content/fido2-page-script.js");
// Inject the script contents directly to avoid leaking the extension URL
try {
const response = await fetch(pageScriptUrl);
const scriptContents = await response.text();
script.innerHTML = scriptContents;
} catch {
// eslint-disable-next-line no-console
console.error("Failed to load FIDO2 page script contents. Injection failed.");
return;
}
// We are ensuring that the script injection is delayed in the event that we are loading // We are ensuring that the script injection is delayed in the event that we are loading
// within an iframe element. This prevents an issue with web mail clients that load content // within an iframe element. This prevents an issue with web mail clients that load content
// using ajax within iframes. In particular, Zimbra web mail client was observed to have this issue. // using ajax within iframes. In particular, Zimbra web mail client was observed to have this issue.