1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-14 23:33:31 +00:00

[PM-3756] Disable node integration and enable context isolation in desktop (#6975)

* Disable node integration and enable context isolation

* Review comments

* Log in renderer through IPC

* Missed imports

* Mock electron API

* resourcesPath is undefined in the preload, but process.windowsStore works correctly

* Replace fromBufferToUtf8 conditional implementation for the `buffer` package

The current non-node implementation is different than the node implementation,
as the non-node would break when the contents can't be parsed as a URI component.
Replacing the impl by the `buffer` package makes the result match in both environments.

* Fix lint

* Add some more tests

* Remove buffer from devDependencies
This commit is contained in:
Daniel García
2024-02-08 18:00:19 +01:00
committed by GitHub
parent 070d8556cf
commit 4be25e3df3
16 changed files with 137 additions and 44 deletions

View File

@@ -258,6 +258,7 @@ describe("Utils Service", () => {
});
}
const asciiHelloWorld = "hello world";
const asciiHelloWorldArray = [104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100];
const b64HelloWorldString = "aGVsbG8gd29ybGQ=";
@@ -623,4 +624,59 @@ describe("Utils Service", () => {
expect(Utils.daysRemaining(new Date(2024, 9, 2, 10))).toBe(366);
});
});
describe("fromBufferToUtf8(...)", () => {
const originalIsNode = Utils.isNode;
afterEach(() => {
Utils.isNode = originalIsNode;
});
runInBothEnvironments("should convert an ArrayBuffer to a utf8 string", () => {
const buffer = new Uint8Array(asciiHelloWorldArray).buffer;
const str = Utils.fromBufferToUtf8(buffer);
expect(str).toBe(asciiHelloWorld);
});
runInBothEnvironments("should handle an empty buffer", () => {
const buffer = new ArrayBuffer(0);
const str = Utils.fromBufferToUtf8(buffer);
expect(str).toBe("");
});
runInBothEnvironments("should convert a binary ArrayBuffer to a binary string", () => {
const cases = [
{
input: [
174, 21, 17, 79, 39, 130, 132, 173, 49, 180, 113, 118, 160, 15, 47, 99, 57, 208, 141,
187, 54, 194, 153, 12, 37, 130, 155, 213, 125, 196, 241, 101,
],
output: "<22>O'<27><><EFBFBD>1<EFBFBD>qv<71>/c9Ѝ<39>6™ %<25><><EFBFBD>}<7D><>e",
},
{
input: [
88, 17, 69, 41, 75, 69, 128, 225, 252, 219, 146, 72, 162, 14, 139, 120, 30, 239, 105,
229, 14, 131, 174, 119, 61, 88, 108, 135, 60, 88, 120, 145,
],
output: "XE)KE<4B><45><EFBFBD>ےH<DB92><0E>x<1E>i<EFBFBD><0E><>w=Xl<58><Xx<58>",
},
{
input: [
121, 110, 81, 148, 48, 67, 209, 43, 3, 39, 143, 184, 237, 184, 213, 183, 84, 157, 47, 6,
31, 183, 99, 142, 155, 156, 192, 107, 118, 64, 176, 36,
],
output: "ynQ<6E>0C<30>+'<27><><EFBFBD><EFBFBD>շT<D5B7>/<1F>c<EFBFBD><63><EFBFBD><EFBFBD>kv@<40>$",
},
];
cases.forEach((c) => {
const buffer = new Uint8Array(c.input).buffer;
const str = Utils.fromBufferToUtf8(buffer);
// Match the expected output
expect(str).toBe(c.output);
// Make sure it matches with the Node.js Buffer output
expect(str).toBe(Buffer.from(buffer).toString("utf8"));
});
});
});
});

View File

@@ -1,6 +1,7 @@
/* eslint-disable no-useless-escape */
import * as path from "path";
import { Buffer as BufferLib } from "buffer/";
import { Observable, of, switchMap } from "rxjs";
import { getHostname, parse } from "tldts";
import { Merge } from "type-fest";
@@ -145,13 +146,7 @@ export class Utils {
}
static fromBufferToUtf8(buffer: ArrayBuffer): string {
if (Utils.isNode) {
return Buffer.from(buffer).toString("utf8");
} else {
const bytes = new Uint8Array(buffer);
const encodedString = String.fromCharCode.apply(null, bytes);
return decodeURIComponent(escape(encodedString));
}
return BufferLib.from(buffer).toString("utf8");
}
static fromBufferToByteString(buffer: ArrayBuffer): string {