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:
undergroundwires
2023-08-25 14:31:30 +02:00
parent 62f8bfac2f
commit e9e0001ef8
83 changed files with 1846 additions and 769 deletions

View File

@@ -9,7 +9,7 @@ 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, AbsentObjectTestCases } from '@tests/unit/shared/TestCases/AbsentTests';
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';
@@ -85,7 +85,7 @@ describe('ApplicationParser', () => {
});
it('defaults to metadata from factory', () => {
// arrange
const expectedMetadata = AppMetadataFactory.Current;
const expectedMetadata: IAppMetadata = AppMetadataFactory.Current.instance;
const infoParserStub = new ProjectInformationParserStub();
// act
new ApplicationParserBuilder()
@@ -157,7 +157,7 @@ describe('ApplicationParser', () => {
value: testCase.absentValue,
expectedError: 'missing collections',
})).filter((test) => test.value !== undefined /* the default value is set */),
...AbsentObjectTestCases.map((testCase) => ({
...getAbsentObjectTestCases().map((testCase) => ({
name: `given absent item "${testCase.valueName}"`,
value: [testCase.absentValue],
expectedError: 'missing collection provided',

View File

@@ -1,7 +1,7 @@
import { describe, it } from 'vitest';
import { NodeDataError, INodeDataErrorContext } from '@/application/Parser/NodeValidation/NodeDataError';
import { NodeData } from '@/application/Parser/NodeValidation/NodeData';
import { AbsentObjectTestCases, AbsentStringTestCases, itEachAbsentTestCase } from '@tests/unit/shared/TestCases/AbsentTests';
import { getAbsentObjectTestCases, getAbsentStringTestCases, itEachAbsentTestCase } from '@tests/unit/shared/TestCases/AbsentTests';
import { expectThrowsError } from '@tests/unit/shared/Assertions/ExpectThrowsError';
export interface ITestScenario {
@@ -16,7 +16,7 @@ export class NodeValidationTestRunner {
describe('throws given invalid names', () => {
// arrange
const testCases = [
...AbsentStringTestCases.map((testCase) => ({
...getAbsentStringTestCases().map((testCase) => ({
testName: `missing name (${testCase.valueName})`,
nameValue: testCase.absentValue,
expectedMessage: 'missing name',
@@ -42,7 +42,7 @@ export class NodeValidationTestRunner {
) {
describe('throws given missing node data', () => {
itEachAbsentTestCase([
...AbsentObjectTestCases,
...getAbsentObjectTestCases(),
{
valueName: 'empty object',
absentValue: {},

View File

@@ -9,7 +9,7 @@ import { ExpressionEvaluationContextStub } from '@tests/unit/shared/Stubs/Expres
import { IPipelineCompiler } from '@/application/Parser/Script/Compiler/Expressions/Pipes/IPipelineCompiler';
import { PipelineCompilerStub } from '@tests/unit/shared/Stubs/PipelineCompilerStub';
import { IReadOnlyFunctionParameterCollection } from '@/application/Parser/Script/Compiler/Function/Parameter/IFunctionParameterCollection';
import { AbsentObjectTestCases, itEachAbsentObjectValue } from '@tests/unit/shared/TestCases/AbsentTests';
import { getAbsentObjectTestCases, itEachAbsentObjectValue } from '@tests/unit/shared/TestCases/AbsentTests';
import { IExpressionEvaluationContext } from '@/application/Parser/Script/Compiler/Expressions/Expression/ExpressionEvaluationContext';
describe('Expression', () => {
@@ -91,7 +91,7 @@ describe('Expression', () => {
expectedError: string,
sutBuilder?: (builder: ExpressionBuilder) => ExpressionBuilder,
}[] = [
...AbsentObjectTestCases.map((testCase) => ({
...getAbsentObjectTestCases().map((testCase) => ({
name: `throws if arguments is ${testCase.valueName}`,
context: testCase.absentValue,
expectedError: 'missing context',

View File

@@ -1,6 +1,6 @@
import { describe } from 'vitest';
import { EscapeDoubleQuotes } from '@/application/Parser/Script/Compiler/Expressions/Pipes/PipeDefinitions/EscapeDoubleQuotes';
import { AbsentStringTestCases } from '@tests/unit/shared/TestCases/AbsentTests';
import { getAbsentStringTestCases } from '@tests/unit/shared/TestCases/AbsentTests';
import { runPipeTests } from './PipeTestRunner';
describe('EscapeDoubleQuotes', () => {
@@ -23,7 +23,7 @@ describe('EscapeDoubleQuotes', () => {
input: '""hello world""',
expectedOutput: '"^"""^""hello world"^"""^""',
},
...AbsentStringTestCases.map((testCase) => ({
...getAbsentStringTestCases().map((testCase) => ({
name: 'returns as it is when if input is missing',
input: testCase.absentValue,
expectedOutput: testCase.absentValue,

View File

@@ -1,7 +1,7 @@
import { describe, it, expect } from 'vitest';
import { PipeFactory } from '@/application/Parser/Script/Compiler/Expressions/Pipes/PipeFactory';
import { PipeStub } from '@tests/unit/shared/Stubs/PipeStub';
import { AbsentStringTestCases, itEachAbsentObjectValue } from '@tests/unit/shared/TestCases/AbsentTests';
import { getAbsentStringTestCases, itEachAbsentObjectValue } from '@tests/unit/shared/TestCases/AbsentTests';
describe('PipeFactory', () => {
describe('ctor', () => {
@@ -82,7 +82,7 @@ describe('PipeFactory', () => {
function testPipeNameValidation(testRunner: (invalidName: string) => void) {
const testCases = [
// Validate missing value
...AbsentStringTestCases.map((testCase) => ({
...getAbsentStringTestCases().map((testCase) => ({
name: `empty pipe name (${testCase.valueName})`,
value: testCase.absentValue,
expectedError: 'empty pipe name',

View File

@@ -4,7 +4,7 @@ import { IPipelineCompiler } from '@/application/Parser/Script/Compiler/Expressi
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 { AbsentStringTestCases } from '@tests/unit/shared/TestCases/AbsentTests';
import { getAbsentStringTestCases } from '@tests/unit/shared/TestCases/AbsentTests';
describe('PipelineCompiler', () => {
describe('compile', () => {
@@ -15,12 +15,12 @@ describe('PipelineCompiler', () => {
expectedError: string;
}
const testCases: ITestCase[] = [
...AbsentStringTestCases.map((testCase) => ({
...getAbsentStringTestCases().map((testCase) => ({
name: `"value" is ${testCase.valueName}`,
act: (test) => test.withValue(testCase.absentValue),
expectedError: 'missing value',
})),
...AbsentStringTestCases.map((testCase) => ({
...getAbsentStringTestCases().map((testCase) => ({
name: `"pipeline" is ${testCase.valueName}`,
act: (test) => test.withPipeline(testCase.absentValue),
expectedError: 'missing pipeline',

View File

@@ -1,7 +1,7 @@
import { describe } from 'vitest';
import { ExpressionPosition } from '@/application/Parser/Script/Compiler/Expressions/Expression/ExpressionPosition';
import { WithParser } from '@/application/Parser/Script/Compiler/Expressions/SyntaxParsers/WithParser';
import { AbsentStringTestCases } from '@tests/unit/shared/TestCases/AbsentTests';
import { getAbsentStringTestCases } from '@tests/unit/shared/TestCases/AbsentTests';
import { SyntaxParserTestsRunner } from './SyntaxParserTestsRunner';
describe('WithParser', () => {
@@ -86,7 +86,7 @@ describe('WithParser', () => {
describe('renders scope conditionally', () => {
describe('does not render scope if argument is undefined', () => {
runner.expectResults(
...AbsentStringTestCases.map((testCase) => ({
...getAbsentStringTestCases().map((testCase) => ({
name: `does not render when value is "${testCase.valueName}"`,
code: '{{ with $parameter }}dark{{ end }} ',
args: (args) => args

View File

@@ -6,7 +6,7 @@ import { IFunctionCall } from '@/application/Parser/Script/Compiler/Function/Cal
import { FunctionCallStub } from '@tests/unit/shared/Stubs/FunctionCallStub';
import { FunctionBodyType, ISharedFunction } from '@/application/Parser/Script/Compiler/Function/ISharedFunction';
import {
AbsentStringTestCases, itEachAbsentCollectionValue, itEachAbsentObjectValue,
getAbsentStringTestCases, itEachAbsentCollectionValue, itEachAbsentObjectValue,
itEachAbsentStringValue,
} from '@tests/unit/shared/TestCases/AbsentTests';
@@ -100,7 +100,7 @@ describe('SharedFunction', () => {
// arrange
const testData = [
'expected-revert-code',
...AbsentStringTestCases.map((testCase) => testCase.absentValue),
...getAbsentStringTestCases().map((testCase) => testCase.absentValue),
];
for (const data of testData) {
// act

View File

@@ -1,5 +1,5 @@
import { describe, it, expect } from 'vitest';
import { AbsentStringTestCases } from '@tests/unit/shared/TestCases/AbsentTests';
import { getAbsentStringTestCases } from '@tests/unit/shared/TestCases/AbsentTests';
export function testParameterName(action: (parameterName: string) => string) {
describe('name', () => {
@@ -22,7 +22,7 @@ export function testParameterName(action: (parameterName: string) => string) {
describe('throws if invalid', () => {
// arrange
const testCases = [
...AbsentStringTestCases.map((test) => ({
...getAbsentStringTestCases().map((test) => ({
name: test.valueName,
value: test.absentValue,
expectedError: 'missing parameter name',

View File

@@ -3,13 +3,13 @@ import { CodeSubstituter } from '@/application/Parser/ScriptingDefinition/CodeSu
import { IExpressionsCompiler } from '@/application/Parser/Script/Compiler/Expressions/IExpressionsCompiler';
import { ProjectInformationStub } from '@tests/unit/shared/Stubs/ProjectInformationStub';
import { ExpressionsCompilerStub } from '@tests/unit/shared/Stubs/ExpressionsCompilerStub';
import { AbsentObjectTestCases, AbsentStringTestCases } from '@tests/unit/shared/TestCases/AbsentTests';
import { getAbsentObjectTestCases, getAbsentStringTestCases } from '@tests/unit/shared/TestCases/AbsentTests';
describe('CodeSubstituter', () => {
describe('throws with invalid parameters', () => {
// arrange
const testCases = [
...AbsentStringTestCases.map((testCase) => ({
...getAbsentStringTestCases().map((testCase) => ({
name: `given code: ${testCase.valueName}`,
expectedError: 'missing code',
parameters: {
@@ -17,7 +17,7 @@ describe('CodeSubstituter', () => {
info: new ProjectInformationStub(),
},
})),
...AbsentObjectTestCases.map((testCase) => ({
...getAbsentObjectTestCases().map((testCase) => ({
name: `given info: ${testCase.valueName}`,
expectedError: 'missing info',
parameters: {