Files
undergroundwires efa05f42bc Improve security by isolating code execution more
This commit enhances application security against potential attacks by
isolating dependencies that access the host system (like file
operations) from the renderer process. It narrows the exposed
functionality to script execution only, adding an extra security layer.

The changes allow secure and scalable API exposure, preparing for future
functionalities such as desktop notifications for script errors (#264),
improved script execution handling (#296), and creating restore points
(#50) in a secure and repeatable way.

Changes include:

- Inject `CodeRunner` into Vue components via dependency injection.
- Move `CodeRunner` to the application layer as an abstraction for
  better domain-driven design alignment.
- Refactor `SystemOperations` and related interfaces, removing the `I`
  prefix.
- Update architecture documentation for clarity.
- Update return types in `NodeSystemOperations` to match the Node APIs.
- Improve `WindowVariablesProvider` integration tests for better error
  context.
- Centralize type checks with common functions like `isArray` and
  `isNumber`.
- Change `CodeRunner` to use `os` parameter, ensuring correct window
  variable injection.
- Streamline API exposure to the renderer process:
  - Automatically bind function contexts to prevent loss of original
    context.
  - Implement a way to create facades (wrapper/proxy objects) for
    increased security.
2023-12-18 17:30:56 +01:00

100 lines
2.8 KiB
TypeScript

import { describe, it, expect } from 'vitest';
import { createSecureFacade } from '@/presentation/electron/preload/ContextBridging/SecureFacadeCreator';
describe('SecureFacadeCreator', () => {
describe('createSecureFacade', () => {
describe('methods', () => {
it('allows access to external methods', () => {
// arrange
let value = 0;
const testObject = {
increment: () => value++,
};
const facade = createSecureFacade(testObject, ['increment']);
// act
facade.increment();
// assert
expect(value).toBe(1);
});
it('proxies external methods', () => {
// arrange
const testObject = {
method: () => {},
};
const facade = createSecureFacade(testObject, ['method']);
// act
const actualMethod = facade.method;
// assert
expect(testObject.method).not.equal(actualMethod);
expect(testObject.method).not.equal(actualMethod);
});
it('does not expose internal methods', () => {
// arrange
interface External {
publicMethod(): void;
}
interface Internal {
privateMethod(): void;
}
const testObject: External & Internal = {
publicMethod: () => {},
privateMethod: () => {},
};
const facade = createSecureFacade<External>(testObject, ['publicMethod']);
// act
facade.publicMethod();
// assert
expect((facade as unknown as Internal).privateMethod).toBeUndefined();
});
it('maintains original function context', () => {
// arrange
const testObject = {
value: 0,
increment() { this.value++; },
};
// act
const facade = createSecureFacade(testObject, ['increment', 'value']);
// assert
facade.increment();
expect(testObject.value).toBe(1);
});
});
describe('properties', () => {
it('allows access to external properties', () => {
// arrange
const testObject = { a: 1 };
// act
const facade = createSecureFacade(testObject, ['a']);
// assert
expect(facade.a).toBe(1);
});
it('does not expose internal properties', () => {
// arrange
interface External {
readonly public: string;
}
interface Internal {
readonly private: string;
}
const testObject: External & Internal = {
public: '',
private: '',
};
const facade = createSecureFacade<External>(testObject, ['public']);
// act
(() => facade.public)();
// assert
expect((facade as unknown as Internal).private).toBeUndefined();
});
});
});
});