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.
63 lines
2.0 KiB
TypeScript
63 lines
2.0 KiB
TypeScript
import { isString } from '@/TypeHelpers';
|
|
|
|
// Because we cannot do "T extends enum" 😞 https://github.com/microsoft/TypeScript/issues/30611
|
|
export type EnumType = number | string;
|
|
export type EnumVariable<T extends EnumType, TEnumValue extends EnumType>
|
|
= { [key in T]: TEnumValue };
|
|
|
|
export interface IEnumParser<TEnum> {
|
|
parseEnum(value: string, propertyName: string): TEnum;
|
|
}
|
|
|
|
export function createEnumParser<T extends EnumType, TEnumValue extends EnumType>(
|
|
enumVariable: EnumVariable<T, TEnumValue>,
|
|
): IEnumParser<TEnumValue> {
|
|
return {
|
|
parseEnum: (value, propertyName) => parseEnumValue(value, propertyName, enumVariable),
|
|
};
|
|
}
|
|
|
|
function parseEnumValue<T extends EnumType, TEnumValue extends EnumType>(
|
|
value: string,
|
|
enumName: string,
|
|
enumVariable: EnumVariable<T, TEnumValue>,
|
|
): TEnumValue {
|
|
if (!value) {
|
|
throw new Error(`missing ${enumName}`);
|
|
}
|
|
if (!isString(value)) {
|
|
throw new Error(`unexpected type of ${enumName}: "${typeof value}"`);
|
|
}
|
|
const casedValue = getEnumNames(enumVariable)
|
|
.find((enumValue) => enumValue.toLowerCase() === value.toLowerCase());
|
|
if (!casedValue) {
|
|
throw new Error(`unknown ${enumName}: "${value}"`);
|
|
}
|
|
return enumVariable[casedValue as keyof typeof enumVariable];
|
|
}
|
|
|
|
export function getEnumNames
|
|
<T extends EnumType, TEnumValue extends EnumType>(
|
|
enumVariable: EnumVariable<T, TEnumValue>,
|
|
): string[] {
|
|
return Object
|
|
.values(enumVariable)
|
|
.filter((enumMember): enumMember is string => isString(enumMember));
|
|
}
|
|
|
|
export function getEnumValues<T extends EnumType, TEnumValue extends EnumType>(
|
|
enumVariable: EnumVariable<T, TEnumValue>,
|
|
): TEnumValue[] {
|
|
return getEnumNames(enumVariable)
|
|
.map((level) => enumVariable[level]) as TEnumValue[];
|
|
}
|
|
|
|
export function assertInRange<T extends EnumType, TEnumValue extends EnumType>(
|
|
value: TEnumValue,
|
|
enumVariable: EnumVariable<T, TEnumValue>,
|
|
) {
|
|
if (!(value in enumVariable)) {
|
|
throw new RangeError(`enum value "${value}" is out of range`);
|
|
}
|
|
}
|