Files
privacy.sexy/tests/unit/infrastructure/RuntimeSanity/Common/FactoryValidator.spec.ts
undergroundwires e9e0001ef8 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.
2023-08-25 14:31:30 +02:00

117 lines
3.6 KiB
TypeScript

import { describe } from 'vitest';
import { FactoryValidator, FactoryFunction } from '@/infrastructure/RuntimeSanity/Common/FactoryValidator';
import { itEachAbsentObjectValue } from '@tests/unit/shared/TestCases/AbsentTests';
describe('FactoryValidator', () => {
describe('ctor', () => {
describe('throws when factory is absent', () => {
itEachAbsentObjectValue((absentValue) => {
// arrange
const expectedError = 'missing factory';
const factory = absentValue;
// act
const act = () => new TestableFactoryValidator(factory);
// assert
expect(act).to.throw(expectedError);
});
});
});
describe('collectErrors', () => {
it('reports error thrown by factory function', () => {
// arrange
const errorFromFactory = 'Error from factory function';
const expectedError = `Error in factory creation: ${errorFromFactory}`;
const factory: FactoryFunction<number | undefined> = () => {
throw new Error(errorFromFactory);
};
const sut = new TestableFactoryValidator(factory);
// act
const errors = [...sut.collectErrors()];
// assert
expect(errors).to.have.lengthOf(1);
expect(errors[0]).to.equal(expectedError);
});
describe('reports when factory returns falsy values', () => {
const falsyValueTestCases = [
{
name: '`false` boolean',
value: false,
},
{
name: 'number zero',
value: 0,
},
{
name: 'empty string',
value: '',
},
{
name: 'null',
value: null,
},
{
name: 'undefined',
value: undefined,
},
{
name: 'NaN (Not-a-Number)',
value: Number.NaN,
},
];
falsyValueTestCases.forEach(({ name, value }) => {
it(`reports for value: ${name}`, () => {
// arrange
const errorFromFactory = 'Factory resulted in a falsy value';
const factory: FactoryFunction<number | undefined> = () => {
return value as never;
};
const sut = new TestableFactoryValidator(factory);
// act
const errors = [...sut.collectErrors()];
// assert
expect(errors).to.have.lengthOf(1);
expect(errors[0]).to.equal(errorFromFactory);
});
});
});
it('does not report when factory returns a truthy value', () => {
// arrange
const factory: FactoryFunction<number | undefined> = () => {
return 35;
};
const sut = new TestableFactoryValidator(factory);
// act
const errors = [...sut.collectErrors()];
// assert
expect(errors).to.have.lengthOf(0);
});
it('executes factory for each method call', () => {
// arrange
let forceFalsyValue = false;
const complexFactory: FactoryFunction<number | undefined> = () => {
return forceFalsyValue ? undefined : 42;
};
const sut = new TestableFactoryValidator(complexFactory);
// act
const firstErrors = [...sut.collectErrors()];
forceFalsyValue = true;
const secondErrors = [...sut.collectErrors()];
// assert
expect(firstErrors).to.have.lengthOf(0);
expect(secondErrors).to.have.lengthOf(1);
});
});
});
class TestableFactoryValidator extends FactoryValidator<number | undefined> {
public constructor(factory: FactoryFunction<number | undefined>) {
super(factory);
}
public name = 'test';
public shouldValidate(): boolean {
return true;
}
}