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.
227 lines
9.1 KiB
TypeScript
227 lines
9.1 KiB
TypeScript
import { describe, it, expect } from 'vitest';
|
|
import type { CollectionData } from '@/application/collections/';
|
|
import { parseProjectInformation } from '@/application/Parser/ProjectInformationParser';
|
|
import { CategoryCollectionParserType, parseApplication } from '@/application/Parser/ApplicationParser';
|
|
import { IAppMetadata } from '@/infrastructure/Metadata/IAppMetadata';
|
|
import WindowsData from '@/application/collections/windows.yaml';
|
|
import MacOsData from '@/application/collections/macos.yaml';
|
|
import LinuxData from '@/application/collections/linux.yaml';
|
|
import { OperatingSystem } from '@/domain/OperatingSystem';
|
|
import { CategoryCollectionStub } from '@tests/unit/shared/Stubs/CategoryCollectionStub';
|
|
import { CollectionDataStub } from '@tests/unit/shared/Stubs/CollectionDataStub';
|
|
import { getAbsentCollectionTestCases, getAbsentObjectTestCases } from '@tests/unit/shared/TestCases/AbsentTests';
|
|
import { AppMetadataStub } from '@tests/unit/shared/Stubs/AppMetadataStub';
|
|
import { AppMetadataFactory } from '@/infrastructure/Metadata/AppMetadataFactory';
|
|
import { CategoryCollectionParserStub } from '@tests/unit/shared/Stubs/CategoryCollectionParserStub';
|
|
import { ProjectInformationParserStub } from '@tests/unit/shared/Stubs/ProjectInformationParserStub';
|
|
import { ProjectInformationStub } from '@tests/unit/shared/Stubs/ProjectInformationStub';
|
|
|
|
describe('ApplicationParser', () => {
|
|
describe('parseApplication', () => {
|
|
describe('categoryParser', () => {
|
|
it('returns result from the parser', () => {
|
|
// arrange
|
|
const os = OperatingSystem.macOS;
|
|
const data = new CollectionDataStub();
|
|
const expected = new CategoryCollectionStub()
|
|
.withOs(os);
|
|
const parser = new CategoryCollectionParserStub()
|
|
.withReturnValue(data, expected)
|
|
.getStub();
|
|
const sut = new ApplicationParserBuilder()
|
|
.withCategoryCollectionParser(parser)
|
|
.withCollectionsData([data]);
|
|
// act
|
|
const app = sut.parseApplication();
|
|
// assert
|
|
const actual = app.getCollection(os);
|
|
expect(expected).to.equal(actual);
|
|
});
|
|
});
|
|
describe('project information', () => {
|
|
it('informationParser is used to create application info', () => {
|
|
// arrange
|
|
const expectedInformation = new ProjectInformationStub();
|
|
const informationParserStub = new ProjectInformationParserStub()
|
|
.withReturnValue(expectedInformation);
|
|
const sut = new ApplicationParserBuilder()
|
|
.withProjectInformationParser(informationParserStub.getStub());
|
|
// act
|
|
const app = sut.parseApplication();
|
|
// assert
|
|
const actualInformation = app.info;
|
|
expect(expectedInformation).to.deep.equal(actualInformation);
|
|
});
|
|
it('informationParser is used to parse collection info', () => {
|
|
// arrange
|
|
const expectedInformation = new ProjectInformationStub();
|
|
const informationParserStub = new ProjectInformationParserStub()
|
|
.withReturnValue(expectedInformation);
|
|
const collectionParserStub = new CategoryCollectionParserStub();
|
|
const sut = new ApplicationParserBuilder()
|
|
.withProjectInformationParser(informationParserStub.getStub())
|
|
.withCategoryCollectionParser(collectionParserStub.getStub());
|
|
// act
|
|
sut.parseApplication();
|
|
// assert
|
|
expect(collectionParserStub.arguments).to.have.length.above(0);
|
|
const actualyUsedInfos = collectionParserStub.arguments.map((arg) => arg.info);
|
|
expect(actualyUsedInfos.every((info) => info === expectedInformation));
|
|
});
|
|
});
|
|
describe('metadata', () => {
|
|
it('used to parse expected metadata', () => {
|
|
// arrange
|
|
const expectedMetadata = new AppMetadataStub();
|
|
const infoParserStub = new ProjectInformationParserStub();
|
|
// act
|
|
new ApplicationParserBuilder()
|
|
.withMetadata(expectedMetadata)
|
|
.withProjectInformationParser(infoParserStub.getStub())
|
|
.parseApplication();
|
|
// assert
|
|
expect(infoParserStub.arguments).to.have.lengthOf(1);
|
|
expect(infoParserStub.arguments[0]).to.equal(expectedMetadata);
|
|
});
|
|
it('defaults to metadata from factory', () => {
|
|
// arrange
|
|
const expectedMetadata: IAppMetadata = AppMetadataFactory.Current.instance;
|
|
const infoParserStub = new ProjectInformationParserStub();
|
|
// act
|
|
new ApplicationParserBuilder()
|
|
.withMetadata(undefined) // force using default
|
|
.withProjectInformationParser(infoParserStub.getStub())
|
|
.parseApplication();
|
|
// assert
|
|
expect(infoParserStub.arguments).to.have.lengthOf(1);
|
|
expect(infoParserStub.arguments[0]).to.equal(expectedMetadata);
|
|
});
|
|
});
|
|
describe('collectionsData', () => {
|
|
describe('set as expected', () => {
|
|
// arrange
|
|
const testCases = [
|
|
{
|
|
name: 'single collection',
|
|
input: [new CollectionDataStub()],
|
|
output: [new CategoryCollectionStub().withOs(OperatingSystem.macOS)],
|
|
},
|
|
{
|
|
name: 'multiple collections',
|
|
input: [
|
|
new CollectionDataStub().withOs('windows'),
|
|
new CollectionDataStub().withOs('macos'),
|
|
],
|
|
output: [
|
|
new CategoryCollectionStub().withOs(OperatingSystem.macOS),
|
|
new CategoryCollectionStub().withOs(OperatingSystem.Windows),
|
|
],
|
|
},
|
|
];
|
|
// act
|
|
for (const testCase of testCases) {
|
|
it(testCase.name, () => {
|
|
let categoryParserStub = new CategoryCollectionParserStub();
|
|
for (let i = 0; i < testCase.input.length; i++) {
|
|
categoryParserStub = categoryParserStub
|
|
.withReturnValue(testCase.input[i], testCase.output[i]);
|
|
}
|
|
const sut = new ApplicationParserBuilder()
|
|
.withCategoryCollectionParser(categoryParserStub.getStub())
|
|
.withCollectionsData(testCase.input);
|
|
// act
|
|
const app = sut.parseApplication();
|
|
// assert
|
|
expect(app.collections).to.deep.equal(testCase.output);
|
|
});
|
|
}
|
|
});
|
|
it('defaults to expected data', () => {
|
|
// arrange
|
|
const expected = [WindowsData, MacOsData, LinuxData];
|
|
const categoryParserStub = new CategoryCollectionParserStub();
|
|
const sut = new ApplicationParserBuilder()
|
|
.withCollectionsData(undefined)
|
|
.withCategoryCollectionParser(categoryParserStub.getStub());
|
|
// act
|
|
sut.parseApplication();
|
|
// assert
|
|
const actual = categoryParserStub.arguments.map((args) => args.data);
|
|
expect(actual).to.deep.equal(expected);
|
|
});
|
|
describe('throws when data is invalid', () => {
|
|
// arrange
|
|
const testCases = [
|
|
...getAbsentCollectionTestCases<CollectionData>().map((testCase) => ({
|
|
name: `given absent collection "${testCase.valueName}"`,
|
|
value: testCase.absentValue,
|
|
expectedError: 'missing collections',
|
|
})).filter((test) => test.value !== undefined /* the default value is set */),
|
|
...getAbsentObjectTestCases().map((testCase) => ({
|
|
name: `given absent item "${testCase.valueName}"`,
|
|
value: [testCase.absentValue],
|
|
expectedError: 'missing collection provided',
|
|
})),
|
|
];
|
|
for (const testCase of testCases) {
|
|
it(testCase.name, () => {
|
|
const sut = new ApplicationParserBuilder()
|
|
.withCollectionsData(testCase.value);
|
|
// act
|
|
const act = () => sut.parseApplication();
|
|
// assert
|
|
expect(act).to.throw(testCase.expectedError);
|
|
});
|
|
}
|
|
});
|
|
});
|
|
});
|
|
});
|
|
|
|
class ApplicationParserBuilder {
|
|
private categoryCollectionParser
|
|
: CategoryCollectionParserType = new CategoryCollectionParserStub().getStub();
|
|
|
|
private projectInformationParser
|
|
: typeof parseProjectInformation = new ProjectInformationParserStub().getStub();
|
|
|
|
private metadata: IAppMetadata = new AppMetadataStub();
|
|
|
|
private collectionsData: CollectionData[] = [new CollectionDataStub()];
|
|
|
|
public withCategoryCollectionParser(
|
|
categoryCollectionParser: CategoryCollectionParserType,
|
|
): this {
|
|
this.categoryCollectionParser = categoryCollectionParser;
|
|
return this;
|
|
}
|
|
|
|
public withProjectInformationParser(
|
|
projectInformationParser: typeof parseProjectInformation,
|
|
): this {
|
|
this.projectInformationParser = projectInformationParser;
|
|
return this;
|
|
}
|
|
|
|
public withMetadata(
|
|
environment: IAppMetadata,
|
|
): this {
|
|
this.metadata = environment;
|
|
return this;
|
|
}
|
|
|
|
public withCollectionsData(collectionsData: CollectionData[]): this {
|
|
this.collectionsData = collectionsData;
|
|
return this;
|
|
}
|
|
|
|
public parseApplication(): ReturnType<typeof parseApplication> {
|
|
return parseApplication(
|
|
this.categoryCollectionParser,
|
|
this.projectInformationParser,
|
|
this.metadata,
|
|
this.collectionsData,
|
|
);
|
|
}
|
|
}
|