Improve desktop security by isolating Electron
Enable `contextIsolation` in Electron to securely expose a limited set of Node.js APIs to the renderer process. It: 1. Isolates renderer and main process contexts. It ensures that the powerful main process functions aren't directly accessible from renderer process(es), adding a security boundary. 2. Mitigates remote exploitation risks. By isolating contexts, potential malicious code injections in the renderer can't directly reach and compromise the main process. 3. Reduces attack surface. 4. Protect against prototype pollution: It prevents tampering of JavaScript object prototypes in one context from affecting another context, improving app reliability and security. Supporting changes include: - Extract environment and system operations classes to the infrastructure layer. This removes node dependencies from core domain and application code. - Introduce `ISystemOperations` to encapsulate OS interactions. Use it from `CodeRunner` to isolate node API usage. - Add a preloader script to inject validated environment variables into renderer context. This keeps Electron integration details encapsulated. - Add new sanity check to fail fast on issues with preloader injected variables. - Improve test coverage of runtime sanity checks and environment components. Move validation logic into separate classes for Single Responsibility. - Improve absent value test case generation.
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
import { describe } from 'vitest';
|
||||
import { OperatingSystem } from '@/domain/OperatingSystem';
|
||||
import { convertPlatformToOs } from '@/presentation/electron/preload/NodeOsMapper';
|
||||
|
||||
describe('NodeOsMapper', () => {
|
||||
describe('convertPlatformToOs', () => {
|
||||
describe('determines desktop OS', () => {
|
||||
// arrange
|
||||
interface IDesktopTestCase {
|
||||
nodePlatform: NodeJS.Platform;
|
||||
expectedOs: OperatingSystem;
|
||||
}
|
||||
const testCases: readonly IDesktopTestCase[] = [ // https://nodejs.org/api/process.html#process_process_platform
|
||||
{
|
||||
nodePlatform: 'aix',
|
||||
expectedOs: undefined,
|
||||
},
|
||||
{
|
||||
nodePlatform: 'darwin',
|
||||
expectedOs: OperatingSystem.macOS,
|
||||
},
|
||||
{
|
||||
nodePlatform: 'freebsd',
|
||||
expectedOs: undefined,
|
||||
},
|
||||
{
|
||||
nodePlatform: 'linux',
|
||||
expectedOs: OperatingSystem.Linux,
|
||||
},
|
||||
{
|
||||
nodePlatform: 'openbsd',
|
||||
expectedOs: undefined,
|
||||
},
|
||||
{
|
||||
nodePlatform: 'sunos',
|
||||
expectedOs: undefined,
|
||||
},
|
||||
{
|
||||
nodePlatform: 'win32',
|
||||
expectedOs: OperatingSystem.Windows,
|
||||
},
|
||||
];
|
||||
testCases.forEach(({ nodePlatform, expectedOs }) => {
|
||||
it(nodePlatform, () => {
|
||||
// act
|
||||
const actualOs = convertPlatformToOs(nodePlatform);
|
||||
// assert
|
||||
expect(actualOs).to.equal(expectedOs, printMessage());
|
||||
function printMessage(): string {
|
||||
return `Expected: "${OperatingSystem[expectedOs]}"\n`
|
||||
+ `Actual: "${OperatingSystem[actualOs]}"\n`
|
||||
+ `Platform: "${nodePlatform}"`;
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,62 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { provideWindowVariables } from '@/presentation/electron/preload/WindowVariablesProvider';
|
||||
import { SystemOperationsStub } from '@tests/unit/shared/Stubs/SystemOperationsStub';
|
||||
import { OperatingSystem } from '@/domain/OperatingSystem';
|
||||
import { ISystemOperations } from '@/infrastructure/Environment/SystemOperations/ISystemOperations';
|
||||
|
||||
describe('WindowVariablesProvider', () => {
|
||||
describe('provideWindowVariables', () => {
|
||||
it('returns expected system', () => {
|
||||
// arrange
|
||||
const expectedValue = new SystemOperationsStub();
|
||||
// act
|
||||
const variables = new TestContext()
|
||||
.withSystem(expectedValue)
|
||||
.provideWindowVariables();
|
||||
// assert
|
||||
expect(variables.system).to.equal(expectedValue);
|
||||
});
|
||||
it('returns expected os', () => {
|
||||
// arrange
|
||||
const expectedValue = OperatingSystem.WindowsPhone;
|
||||
// act
|
||||
const variables = new TestContext()
|
||||
.withOs(expectedValue)
|
||||
.provideWindowVariables();
|
||||
// assert
|
||||
expect(variables.os).to.equal(expectedValue);
|
||||
});
|
||||
it('`isDesktop` is true', () => {
|
||||
// arrange
|
||||
const expectedValue = true;
|
||||
// act
|
||||
const variables = new TestContext()
|
||||
.provideWindowVariables();
|
||||
// assert
|
||||
expect(variables.isDesktop).to.equal(expectedValue);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
class TestContext {
|
||||
private system: ISystemOperations = new SystemOperationsStub();
|
||||
|
||||
private os: OperatingSystem = OperatingSystem.Android;
|
||||
|
||||
public withSystem(system: ISystemOperations): this {
|
||||
this.system = system;
|
||||
return this;
|
||||
}
|
||||
|
||||
public withOs(os: OperatingSystem): this {
|
||||
this.os = os;
|
||||
return this;
|
||||
}
|
||||
|
||||
public provideWindowVariables() {
|
||||
return provideWindowVariables(
|
||||
() => this.system,
|
||||
() => this.os,
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user