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:
@@ -1,133 +1,7 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { describe } from 'vitest';
|
||||
import { MetadataValidator } from '@/infrastructure/RuntimeSanity/Validators/MetadataValidator';
|
||||
import { SanityCheckOptionsStub } from '@tests/unit/shared/Stubs/SanityCheckOptionsStub';
|
||||
import { itEachAbsentObjectValue } from '@tests/unit/shared/TestCases/AbsentTests';
|
||||
import { AppMetadataStub } from '@tests/unit/shared/Stubs/AppMetadataStub';
|
||||
import { IAppMetadata } from '@/infrastructure/Metadata/IAppMetadata';
|
||||
import { itNoErrorsOnCurrentEnvironment } from './ValidatorTestRunner';
|
||||
|
||||
describe('MetadataValidator', () => {
|
||||
describe('shouldValidate', () => {
|
||||
it('returns true when validateMetadata is true', () => {
|
||||
// arrange
|
||||
const expectedValue = true;
|
||||
const options = new SanityCheckOptionsStub()
|
||||
.withValidateMetadata(true);
|
||||
const validator = new TestContext()
|
||||
.createSut();
|
||||
// act
|
||||
const actualValue = validator.shouldValidate(options);
|
||||
// assert
|
||||
expect(actualValue).to.equal(expectedValue);
|
||||
});
|
||||
|
||||
it('returns false when validateMetadata is false', () => {
|
||||
// arrange
|
||||
const expectedValue = false;
|
||||
const options = new SanityCheckOptionsStub()
|
||||
.withValidateMetadata(false);
|
||||
const validator = new TestContext()
|
||||
.createSut();
|
||||
// act
|
||||
const actualValue = validator.shouldValidate(options);
|
||||
// assert
|
||||
expect(actualValue).to.equal(expectedValue);
|
||||
});
|
||||
});
|
||||
describe('collectErrors', () => {
|
||||
describe('yields "missing metadata" if metadata is not provided', () => {
|
||||
itEachAbsentObjectValue((absentValue) => {
|
||||
// arrange
|
||||
const expectedError = 'missing metadata';
|
||||
const validator = new TestContext()
|
||||
.withMetadata(absentValue)
|
||||
.createSut();
|
||||
// act
|
||||
const errors = [...validator.collectErrors()];
|
||||
// assert
|
||||
expect(errors).to.have.lengthOf(1);
|
||||
expect(errors[0]).to.equal(expectedError);
|
||||
});
|
||||
});
|
||||
it('yields missing keys if metadata has keys without values', () => {
|
||||
// arrange
|
||||
const expectedError = 'Metadata keys missing: name, homepageUrl';
|
||||
const metadata = new AppMetadataStub()
|
||||
.witName(undefined)
|
||||
.withHomepageUrl(undefined);
|
||||
const validator = new TestContext()
|
||||
.withMetadata(metadata)
|
||||
.createSut();
|
||||
// act
|
||||
const errors = [...validator.collectErrors()];
|
||||
// assert
|
||||
expect(errors).to.have.lengthOf(1);
|
||||
expect(errors[0]).to.equal(expectedError);
|
||||
});
|
||||
it('yields missing keys if metadata has getters instead of properties', () => {
|
||||
/*
|
||||
This test may behave differently in unit testing vs. production due to how code
|
||||
is transformed, especially around class getters and their enumerability during bundling.
|
||||
*/
|
||||
// arrange
|
||||
const expectedError = 'Metadata keys missing: name, homepageUrl';
|
||||
const stubWithGetters: Partial<IAppMetadata> = {
|
||||
get name() {
|
||||
return undefined;
|
||||
},
|
||||
get homepageUrl() {
|
||||
return undefined;
|
||||
},
|
||||
};
|
||||
const stub: IAppMetadata = {
|
||||
...new AppMetadataStub(),
|
||||
...stubWithGetters,
|
||||
};
|
||||
const validator = new TestContext()
|
||||
.withMetadata(stub)
|
||||
.createSut();
|
||||
// act
|
||||
const errors = [...validator.collectErrors()];
|
||||
// assert
|
||||
expect(errors).to.have.lengthOf(1);
|
||||
expect(errors[0]).to.equal(expectedError);
|
||||
});
|
||||
it('yields unable to capture metadata if metadata has no getter values', () => {
|
||||
// arrange
|
||||
const expectedError = 'Unable to capture metadata key/value pairs';
|
||||
const stub = {} as IAppMetadata;
|
||||
const validator = new TestContext()
|
||||
.withMetadata(stub)
|
||||
.createSut();
|
||||
// act
|
||||
const errors = [...validator.collectErrors()];
|
||||
// assert
|
||||
expect(errors).to.have.lengthOf(1);
|
||||
expect(errors[0]).to.equal(expectedError);
|
||||
});
|
||||
it('does not yield errors if all metadata keys have values', () => {
|
||||
// arrange
|
||||
const metadata = new AppMetadataStub();
|
||||
const validator = new TestContext()
|
||||
.withMetadata(metadata)
|
||||
.createSut();
|
||||
// act
|
||||
const errors = [...validator.collectErrors()];
|
||||
// assert
|
||||
expect(errors).to.have.lengthOf(0);
|
||||
});
|
||||
});
|
||||
itNoErrorsOnCurrentEnvironment(() => new MetadataValidator());
|
||||
});
|
||||
|
||||
class TestContext {
|
||||
public metadata: IAppMetadata = new AppMetadataStub();
|
||||
|
||||
public withMetadata(metadata: IAppMetadata): this {
|
||||
this.metadata = metadata;
|
||||
return this;
|
||||
}
|
||||
|
||||
public createSut(): MetadataValidator {
|
||||
const mockFactory = () => this.metadata;
|
||||
return new MetadataValidator(mockFactory);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user