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.
67 lines
2.1 KiB
TypeScript
67 lines
2.1 KiB
TypeScript
import { it, describe, expect } from 'vitest';
|
|
import { provideWindowVariables } from '@/presentation/electron/preload/ContextBridging/RendererApiProvider';
|
|
import {
|
|
isArray, isBoolean, isFunction, isNullOrUndefined, isNumber, isPlainObject, isString,
|
|
} from '@/TypeHelpers';
|
|
|
|
describe('RendererApiProvider', () => {
|
|
describe('provideWindowVariables', () => {
|
|
describe('conforms to Electron\'s context bridging requirements', () => {
|
|
// https://www.electronjs.org/docs/latest/api/context-bridge
|
|
const variables = provideWindowVariables();
|
|
Object.entries(variables).forEach(([key, value]) => {
|
|
it(`\`${key}\` conforms to allowed types for context bridging`, () => {
|
|
// act
|
|
const act = () => checkAllowedType(value);
|
|
// assert
|
|
expect(act).to.not.throw();
|
|
});
|
|
});
|
|
});
|
|
});
|
|
});
|
|
|
|
function checkAllowedType(value: unknown): void {
|
|
if (isBasicType(value)) {
|
|
return;
|
|
}
|
|
if (isArray(value)) {
|
|
checkArrayElements(value);
|
|
return;
|
|
}
|
|
if (!isPlainObject(value)) {
|
|
throw new Error(`Type error: Expected a valid object, array, or primitive type, but received type '${typeof value}'.`);
|
|
}
|
|
if (isNullOrUndefined(value)) {
|
|
throw new Error('Type error: Value is null or undefined, which is not allowed.');
|
|
}
|
|
checkObjectProperties(value);
|
|
}
|
|
|
|
function isBasicType(value: unknown): boolean {
|
|
return isString(value) || isNumber(value) || isBoolean(value) || isFunction(value);
|
|
}
|
|
|
|
function checkArrayElements(array: unknown[]): void {
|
|
array.forEach((item, index) => {
|
|
try {
|
|
checkAllowedType(item);
|
|
} catch (error) {
|
|
throw new Error(`Invalid array element at index ${index}: ${error.message}`);
|
|
}
|
|
});
|
|
}
|
|
|
|
function checkObjectProperties(obj: NonNullable<object>): void {
|
|
if (Object.keys(obj).some((key) => !isString(key))) {
|
|
throw new Error('Type error: At least one object key is not a string, which violates the allowed types.');
|
|
}
|
|
Object.entries(obj).forEach(([key, memberValue]) => {
|
|
try {
|
|
checkAllowedType(memberValue);
|
|
} catch (error) {
|
|
throw new Error(`Invalid object property '${key}': ${error.message}`);
|
|
}
|
|
});
|
|
}
|