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.
123 lines
4.7 KiB
TypeScript
123 lines
4.7 KiB
TypeScript
import { describe, it, expect } from 'vitest';
|
|
import { Application } from '@/domain/Application';
|
|
import { OperatingSystem } from '@/domain/OperatingSystem';
|
|
import { CategoryCollectionStub } from '@tests/unit/shared/Stubs/CategoryCollectionStub';
|
|
import { ProjectInformationStub } from '@tests/unit/shared/Stubs/ProjectInformationStub';
|
|
import { getAbsentObjectTestCases, getAbsentCollectionTestCases, itEachAbsentObjectValue } from '@tests/unit/shared/TestCases/AbsentTests';
|
|
import { ICategoryCollection } from '@/domain/ICategoryCollection';
|
|
|
|
describe('Application', () => {
|
|
describe('getCollection', () => {
|
|
it('returns undefined if not found', () => {
|
|
// arrange
|
|
const expected = undefined;
|
|
const info = new ProjectInformationStub();
|
|
const collections = [new CategoryCollectionStub().withOs(OperatingSystem.Windows)];
|
|
// act
|
|
const sut = new Application(info, collections);
|
|
const actual = sut.getCollection(OperatingSystem.Android);
|
|
// assert
|
|
expect(actual).to.equals(expected);
|
|
});
|
|
it('returns expected when multiple collections exist', () => {
|
|
// arrange
|
|
const os = OperatingSystem.Windows;
|
|
const expected = new CategoryCollectionStub().withOs(os);
|
|
const info = new ProjectInformationStub();
|
|
const collections = [expected, new CategoryCollectionStub().withOs(OperatingSystem.Android)];
|
|
// act
|
|
const sut = new Application(info, collections);
|
|
const actual = sut.getCollection(os);
|
|
// assert
|
|
expect(actual).to.equals(expected);
|
|
});
|
|
});
|
|
describe('ctor', () => {
|
|
describe('info', () => {
|
|
describe('throws if missing', () => {
|
|
itEachAbsentObjectValue((absentValue) => {
|
|
// arrange
|
|
const expectedError = 'missing project information';
|
|
const info = absentValue;
|
|
const collections = [new CategoryCollectionStub()];
|
|
// act
|
|
const act = () => new Application(info, collections);
|
|
// assert
|
|
expect(act).to.throw(expectedError);
|
|
});
|
|
});
|
|
it('sets as expected', () => {
|
|
// arrange
|
|
const expected = new ProjectInformationStub();
|
|
const collections = [new CategoryCollectionStub()];
|
|
// act
|
|
const sut = new Application(expected, collections);
|
|
// assert
|
|
expect(sut.info).to.equal(expected);
|
|
});
|
|
});
|
|
describe('collections', () => {
|
|
describe('throws on invalid value', () => {
|
|
// arrange
|
|
const testCases: readonly {
|
|
name: string,
|
|
expectedError: string,
|
|
value: readonly ICategoryCollection[],
|
|
}[] = [
|
|
...getAbsentCollectionTestCases<ICategoryCollection>().map((testCase) => ({
|
|
name: testCase.valueName,
|
|
expectedError: 'missing collections',
|
|
value: testCase.absentValue,
|
|
})),
|
|
...getAbsentObjectTestCases().map((testCase) => ({
|
|
name: `${testCase.valueName} value in list`,
|
|
expectedError: 'missing collection in the list',
|
|
value: [new CategoryCollectionStub(), testCase.absentValue],
|
|
})),
|
|
{
|
|
name: 'two collections with same OS',
|
|
expectedError: 'multiple collections with same os: windows',
|
|
value: [
|
|
new CategoryCollectionStub().withOs(OperatingSystem.Windows),
|
|
new CategoryCollectionStub().withOs(OperatingSystem.Windows),
|
|
new CategoryCollectionStub().withOs(OperatingSystem.BlackBerry),
|
|
],
|
|
},
|
|
];
|
|
for (const testCase of testCases) {
|
|
it(testCase.name, () => {
|
|
const info = new ProjectInformationStub();
|
|
const collections = testCase.value;
|
|
// act
|
|
const act = () => new Application(info, collections);
|
|
// assert
|
|
expect(act).to.throw(testCase.expectedError);
|
|
});
|
|
}
|
|
});
|
|
it('sets as expected', () => {
|
|
// arrange
|
|
const info = new ProjectInformationStub();
|
|
const expected = [new CategoryCollectionStub()];
|
|
// act
|
|
const sut = new Application(info, expected);
|
|
// assert
|
|
expect(sut.collections).to.equal(expected);
|
|
});
|
|
});
|
|
});
|
|
describe('getSupportedOsList', () => {
|
|
it('returns expected', () => {
|
|
// arrange
|
|
const expected = [OperatingSystem.Windows, OperatingSystem.macOS];
|
|
const info = new ProjectInformationStub();
|
|
const collections = expected.map((os) => new CategoryCollectionStub().withOs(os));
|
|
// act
|
|
const sut = new Application(info, collections);
|
|
const actual = sut.getSupportedOsList();
|
|
// assert
|
|
expect(actual).to.deep.equal(expected);
|
|
});
|
|
});
|
|
});
|