Files
privacy.sexy/tests/unit/infrastructure/RuntimeSanity/SanityChecks.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

201 lines
7.1 KiB
TypeScript

import { describe, it, expect } from 'vitest';
import { validateRuntimeSanity } from '@/infrastructure/RuntimeSanity/SanityChecks';
import { ISanityCheckOptions } from '@/infrastructure/RuntimeSanity/Common/ISanityCheckOptions';
import { SanityCheckOptionsStub } from '@tests/unit/shared/Stubs/SanityCheckOptionsStub';
import { ISanityValidator } from '@/infrastructure/RuntimeSanity/Common/ISanityValidator';
import { SanityValidatorStub } from '@tests/unit/shared/Stubs/SanityValidatorStub';
import { itEachAbsentCollectionValue, itEachAbsentObjectValue } from '@tests/unit/shared/TestCases/AbsentTests';
describe('SanityChecks', () => {
describe('validateRuntimeSanity', () => {
describe('parameter validation', () => {
describe('options', () => {
itEachAbsentObjectValue((absentValue) => {
// arrange
const expectedError = 'missing options';
const context = new TestContext()
.withOptions(absentValue);
// act
const act = () => context.validateRuntimeSanity();
// assert
expect(act).to.throw(expectedError);
});
});
describe('throws when validators are empty', () => {
itEachAbsentCollectionValue((absentCollection) => {
// arrange
const expectedError = 'missing validators';
const validators = absentCollection;
const context = new TestContext()
.withValidators(validators);
// act
const act = () => context.validateRuntimeSanity();
// assert
expect(act).to.throw(expectedError);
}, { excludeUndefined: true });
});
describe('throws when single validator is absent', () => {
itEachAbsentObjectValue((absentValue) => {
// arrange
const expectedError = 'missing validator in validators';
const absentValidator = absentValue;
const context = new TestContext()
.withValidators([new SanityValidatorStub(), absentValidator]);
// act
const act = () => context.validateRuntimeSanity();
// assert
expect(act).to.throw(expectedError);
});
});
});
describe('aggregates validators', () => {
it('does not throw if all validators pass', () => {
// arrange
const context = new TestContext()
.withValidators([
new SanityValidatorStub()
.withShouldValidateResult(false),
new SanityValidatorStub()
.withShouldValidateResult(false),
]);
// act
const act = () => context.validateRuntimeSanity();
// assert
expect(act).to.not.throw();
});
it('does not throw if a validator return errors but pass', () => {
// arrange
const context = new TestContext()
.withValidators([
new SanityValidatorStub()
.withErrorsResult(['should be ignored'])
.withShouldValidateResult(false),
]);
// act
const act = () => context.validateRuntimeSanity();
// assert
expect(act).to.not.throw();
});
it('does not throw if validators return no errors', () => {
// arrange
const context = new TestContext()
.withValidators([
new SanityValidatorStub()
.withShouldValidateResult(true)
.withErrorsResult([]),
new SanityValidatorStub()
.withShouldValidateResult(true)
.withErrorsResult([]),
]);
// act
const act = () => context.validateRuntimeSanity();
// assert
expect(act).to.not.throw();
});
it('throws if single validator has errors', () => {
// arrange
const firstError = 'first-error';
const secondError = 'second-error';
let actualError = '';
const context = new TestContext()
.withValidators([
new SanityValidatorStub()
.withShouldValidateResult(true)
.withErrorsResult([firstError, secondError]),
]);
// act
try {
context.validateRuntimeSanity();
} catch (err) {
actualError = err.toString();
}
// assert
expect(actualError).to.have.length.above(0);
expect(actualError).to.include(firstError);
expect(actualError).to.include(secondError);
});
it('throws with validators name', () => {
// arrange
const validatorWithErrors = 'validator-with-errors';
const validatorWithNoErrors = 'validator-with-no-errors';
let actualError = '';
const context = new TestContext()
.withValidators([
new SanityValidatorStub()
.withName(validatorWithErrors)
.withShouldValidateResult(true)
.withErrorsResult(['error']),
new SanityValidatorStub()
.withShouldValidateResult(true)
.withErrorsResult([]),
new SanityValidatorStub()
.withShouldValidateResult(true)
.withErrorsResult([]),
]);
// act
try {
context.validateRuntimeSanity();
} catch (err) {
actualError = err.toString();
}
// assert
expect(actualError).to.have.length.above(0);
expect(actualError).to.include(validatorWithErrors);
expect(actualError).to.not.include(validatorWithNoErrors);
});
it('accumulates error messages from validators', () => {
// arrange
const errorFromFirstValidator = 'first-error';
const errorFromSecondValidator = 'second-error';
let actualError = '';
const context = new TestContext()
.withValidators([
new SanityValidatorStub()
.withShouldValidateResult(true)
.withErrorsResult([errorFromFirstValidator]),
new SanityValidatorStub()
.withShouldValidateResult(true)
.withErrorsResult([errorFromSecondValidator]),
]);
// act
try {
context.validateRuntimeSanity();
} catch (err) {
actualError = err.toString();
}
// assert
expect(actualError).to.have.length.above(0);
expect(actualError).to.include(errorFromFirstValidator);
expect(actualError).to.include(errorFromSecondValidator);
});
});
});
});
class TestContext {
private options: ISanityCheckOptions = new SanityCheckOptionsStub();
private validators: ISanityValidator[] = [new SanityValidatorStub()];
public withOptionsSetup(
setup: (stub: SanityCheckOptionsStub) => SanityCheckOptionsStub,
) {
return this.withOptions(setup(new SanityCheckOptionsStub()));
}
public withOptions(options: ISanityCheckOptions): this {
this.options = options;
return this;
}
public withValidators(validators: ISanityValidator[]): this {
this.validators = validators;
return this;
}
public validateRuntimeSanity(): ReturnType<typeof validateRuntimeSanity> {
return validateRuntimeSanity(this.options, this.validators);
}
}