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.
131 lines
4.5 KiB
TypeScript
131 lines
4.5 KiB
TypeScript
import { describe, it, expect } from 'vitest';
|
|
import { PipelineCompiler } from '@/application/Parser/Script/Compiler/Expressions/Pipes/PipelineCompiler';
|
|
import { IPipelineCompiler } from '@/application/Parser/Script/Compiler/Expressions/Pipes/IPipelineCompiler';
|
|
import { IPipeFactory } from '@/application/Parser/Script/Compiler/Expressions/Pipes/PipeFactory';
|
|
import { PipeStub } from '@tests/unit/shared/Stubs/PipeStub';
|
|
import { PipeFactoryStub } from '@tests/unit/shared/Stubs/PipeFactoryStub';
|
|
import { getAbsentStringTestCases } from '@tests/unit/shared/TestCases/AbsentTests';
|
|
|
|
describe('PipelineCompiler', () => {
|
|
describe('compile', () => {
|
|
describe('throws for invalid arguments', () => {
|
|
interface ITestCase {
|
|
name: string;
|
|
act: (test: PipelineTestRunner) => PipelineTestRunner;
|
|
expectedError: string;
|
|
}
|
|
const testCases: ITestCase[] = [
|
|
...getAbsentStringTestCases().map((testCase) => ({
|
|
name: `"value" is ${testCase.valueName}`,
|
|
act: (test) => test.withValue(testCase.absentValue),
|
|
expectedError: 'missing value',
|
|
})),
|
|
...getAbsentStringTestCases().map((testCase) => ({
|
|
name: `"pipeline" is ${testCase.valueName}`,
|
|
act: (test) => test.withPipeline(testCase.absentValue),
|
|
expectedError: 'missing pipeline',
|
|
})),
|
|
{
|
|
name: '"pipeline" does not start with pipe',
|
|
act: (test) => test.withPipeline('pipeline |'),
|
|
expectedError: 'pipeline does not start with pipe',
|
|
},
|
|
];
|
|
for (const testCase of testCases) {
|
|
it(testCase.name, () => {
|
|
// act
|
|
const runner = new PipelineTestRunner();
|
|
testCase.act(runner);
|
|
const act = () => runner.compile();
|
|
// assert
|
|
expect(act).to.throw(testCase.expectedError);
|
|
});
|
|
}
|
|
});
|
|
describe('compiles pipeline as expected', () => {
|
|
const testCases = [
|
|
{
|
|
name: 'compiles single pipe as expected',
|
|
pipes: [
|
|
new PipeStub().withName('doublePrint').withApplier((value) => `${value}-${value}`),
|
|
],
|
|
pipeline: '| doublePrint',
|
|
value: 'value',
|
|
expected: 'value-value',
|
|
},
|
|
{
|
|
name: 'compiles multiple pipes as expected',
|
|
pipes: [
|
|
new PipeStub().withName('prependLetterA').withApplier((value) => `A-${value}`),
|
|
new PipeStub().withName('prependLetterB').withApplier((value) => `B-${value}`),
|
|
],
|
|
pipeline: '| prependLetterA | prependLetterB',
|
|
value: 'value',
|
|
expected: 'B-A-value',
|
|
},
|
|
{
|
|
name: 'compiles with relaxed whitespace placing',
|
|
pipes: [
|
|
new PipeStub().withName('appendNumberOne').withApplier((value) => `${value}1`),
|
|
new PipeStub().withName('appendNumberTwo').withApplier((value) => `${value}2`),
|
|
new PipeStub().withName('appendNumberThree').withApplier((value) => `${value}3`),
|
|
],
|
|
pipeline: ' | appendNumberOne|appendNumberTwo| appendNumberThree',
|
|
value: 'value',
|
|
expected: 'value123',
|
|
},
|
|
{
|
|
name: 'can reuse same pipe',
|
|
pipes: [
|
|
new PipeStub().withName('removeFirstChar').withApplier((value) => `${value.slice(1)}`),
|
|
],
|
|
pipeline: ' | removeFirstChar | removeFirstChar | removeFirstChar',
|
|
value: 'value',
|
|
expected: 'ue',
|
|
},
|
|
];
|
|
for (const testCase of testCases) {
|
|
it(testCase.name, () => {
|
|
// arrange
|
|
const runner = new PipelineTestRunner()
|
|
.withValue(testCase.value)
|
|
.withPipeline(testCase.pipeline)
|
|
.withFactory(new PipeFactoryStub().withPipes(testCase.pipes));
|
|
// act
|
|
const actual = runner.compile();
|
|
// expect
|
|
expect(actual).to.equal(testCase.expected);
|
|
});
|
|
}
|
|
});
|
|
});
|
|
});
|
|
|
|
class PipelineTestRunner implements IPipelineCompiler {
|
|
private value = 'non-empty-value';
|
|
|
|
private pipeline = '| validPipeline';
|
|
|
|
private factory: IPipeFactory = new PipeFactoryStub();
|
|
|
|
public withValue(value: string) {
|
|
this.value = value;
|
|
return this;
|
|
}
|
|
|
|
public withPipeline(pipeline: string) {
|
|
this.pipeline = pipeline;
|
|
return this;
|
|
}
|
|
|
|
public withFactory(factory: IPipeFactory) {
|
|
this.factory = factory;
|
|
return this;
|
|
}
|
|
|
|
public compile(): string {
|
|
const sut = new PipelineCompiler(this.factory);
|
|
return sut.compile(this.value, this.pipeline);
|
|
}
|
|
}
|