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:
@@ -9,3 +9,7 @@ export type PropertyKeys<T> = {
|
||||
|
||||
export type ConstructorArguments<T> =
|
||||
T extends new (...args: infer U) => unknown ? U : never;
|
||||
|
||||
export type FunctionKeys<T> = {
|
||||
[K in keyof T]: T[K] extends (...args: unknown[]) => unknown ? K : never;
|
||||
}[keyof T];
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import { IApplicationContext } from '@/application/Context/IApplicationContext';
|
||||
import { OperatingSystem } from '@/domain/OperatingSystem';
|
||||
import { IApplication } from '@/domain/IApplication';
|
||||
import { Environment } from '../Environment/Environment';
|
||||
import { IEnvironment } from '../Environment/IEnvironment';
|
||||
import { Environment } from '@/infrastructure/Environment/Environment';
|
||||
import { IEnvironment } from '@/infrastructure/Environment/IEnvironment';
|
||||
import { IApplicationFactory } from '../IApplicationFactory';
|
||||
import { ApplicationFactory } from '../ApplicationFactory';
|
||||
import { ApplicationContext } from './ApplicationContext';
|
||||
|
||||
@@ -1,89 +0,0 @@
|
||||
import { OperatingSystem } from '@/domain/OperatingSystem';
|
||||
import { BrowserOsDetector } from './BrowserOs/BrowserOsDetector';
|
||||
import { IBrowserOsDetector } from './BrowserOs/IBrowserOsDetector';
|
||||
import { IEnvironment } from './IEnvironment';
|
||||
|
||||
export interface IEnvironmentVariables {
|
||||
readonly window: Window & typeof globalThis;
|
||||
readonly process: NodeJS.Process;
|
||||
readonly navigator: Navigator;
|
||||
}
|
||||
|
||||
export class Environment implements IEnvironment {
|
||||
public static readonly CurrentEnvironment: IEnvironment = new Environment({
|
||||
window,
|
||||
process: typeof process !== 'undefined' ? process /* electron only */ : undefined,
|
||||
navigator,
|
||||
});
|
||||
|
||||
public readonly isDesktop: boolean;
|
||||
|
||||
public readonly os: OperatingSystem;
|
||||
|
||||
protected constructor(
|
||||
variables: IEnvironmentVariables,
|
||||
browserOsDetector: IBrowserOsDetector = new BrowserOsDetector(),
|
||||
) {
|
||||
if (!variables) {
|
||||
throw new Error('variables is null or empty');
|
||||
}
|
||||
this.isDesktop = isDesktop(variables);
|
||||
if (this.isDesktop) {
|
||||
this.os = getDesktopOsType(getProcessPlatform(variables));
|
||||
} else {
|
||||
const userAgent = getUserAgent(variables);
|
||||
this.os = !userAgent ? undefined : browserOsDetector.detect(userAgent);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function getUserAgent(variables: IEnvironmentVariables): string {
|
||||
if (!variables.window || !variables.window.navigator) {
|
||||
return undefined;
|
||||
}
|
||||
return variables.window.navigator.userAgent;
|
||||
}
|
||||
|
||||
function getProcessPlatform(variables: IEnvironmentVariables): string {
|
||||
if (!variables.process || !variables.process.platform) {
|
||||
return undefined;
|
||||
}
|
||||
return variables.process.platform;
|
||||
}
|
||||
|
||||
function getDesktopOsType(processPlatform: string): OperatingSystem | undefined {
|
||||
// https://nodejs.org/api/process.html#process_process_platform
|
||||
switch (processPlatform) {
|
||||
case 'darwin':
|
||||
return OperatingSystem.macOS;
|
||||
case 'win32':
|
||||
return OperatingSystem.Windows;
|
||||
case 'linux':
|
||||
return OperatingSystem.Linux;
|
||||
default:
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
function isDesktop(variables: IEnvironmentVariables): boolean {
|
||||
// More: https://github.com/electron/electron/issues/2288
|
||||
// Renderer process
|
||||
if (variables.window
|
||||
&& variables.window.process
|
||||
&& variables.window.process.type === 'renderer') {
|
||||
return true;
|
||||
}
|
||||
// Main process
|
||||
if (variables.process
|
||||
&& variables.process.versions
|
||||
&& Boolean(variables.process.versions.electron)) {
|
||||
return true;
|
||||
}
|
||||
// Detect the user agent when the `nodeIntegration` option is set to true
|
||||
if (variables.navigator
|
||||
&& variables.navigator.userAgent
|
||||
&& variables.navigator.userAgent.includes('Electron')) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
import { OperatingSystem } from '@/domain/OperatingSystem';
|
||||
|
||||
export interface IEnvironment {
|
||||
readonly isDesktop: boolean;
|
||||
readonly os: OperatingSystem;
|
||||
}
|
||||
@@ -14,7 +14,7 @@ import { parseCategoryCollection } from './CategoryCollectionParser';
|
||||
export function parseApplication(
|
||||
categoryParser = parseCategoryCollection,
|
||||
informationParser = parseProjectInformation,
|
||||
metadata: IAppMetadata = AppMetadataFactory.Current,
|
||||
metadata: IAppMetadata = AppMetadataFactory.Current.instance,
|
||||
collectionsData = PreParsedCollections,
|
||||
): IApplication {
|
||||
validateCollectionsData(collectionsData);
|
||||
|
||||
@@ -7,7 +7,7 @@ import { ConstructorArguments } from '@/TypeHelpers';
|
||||
|
||||
export function
|
||||
parseProjectInformation(
|
||||
metadata: IAppMetadata = AppMetadataFactory.Current,
|
||||
metadata: IAppMetadata = AppMetadataFactory.Current.instance,
|
||||
createProjectInformation: ProjectInformationFactory = (
|
||||
...args
|
||||
) => new ProjectInformation(...args),
|
||||
|
||||
@@ -1,25 +1,27 @@
|
||||
import os from 'os';
|
||||
import path from 'path';
|
||||
import fs from 'fs';
|
||||
import child_process from 'child_process';
|
||||
import { Environment } from '@/application/Environment/Environment';
|
||||
import { Environment } from '@/infrastructure/Environment/Environment';
|
||||
import { OperatingSystem } from '@/domain/OperatingSystem';
|
||||
|
||||
export class CodeRunner {
|
||||
constructor(
|
||||
private readonly node = getNodeJs(),
|
||||
private readonly environment = Environment.CurrentEnvironment,
|
||||
) {
|
||||
if (!environment.system) {
|
||||
throw new Error('missing system operations');
|
||||
}
|
||||
}
|
||||
|
||||
public async runCode(code: string, folderName: string, fileExtension: string): Promise<void> {
|
||||
const dir = this.node.path.join(this.node.os.tmpdir(), folderName);
|
||||
await this.node.fs.promises.mkdir(dir, { recursive: true });
|
||||
const filePath = this.node.path.join(dir, `run.${fileExtension}`);
|
||||
await this.node.fs.promises.writeFile(filePath, code);
|
||||
await this.node.fs.promises.chmod(filePath, '755');
|
||||
const { system } = this.environment;
|
||||
const dir = system.location.combinePaths(
|
||||
system.operatingSystem.getTempDirectory(),
|
||||
folderName,
|
||||
);
|
||||
await system.fileSystem.createDirectory(dir, true);
|
||||
const filePath = system.location.combinePaths(dir, `run.${fileExtension}`);
|
||||
await system.fileSystem.writeToFile(filePath, code);
|
||||
await system.fileSystem.setFilePermissions(filePath, '755');
|
||||
const command = getExecuteCommand(filePath, this.environment);
|
||||
this.node.child_process.exec(command);
|
||||
system.command.execute(command);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,43 +40,3 @@ function getExecuteCommand(scriptPath: string, environment: Environment): string
|
||||
throw Error(`unsupported os: ${OperatingSystem[environment.os]}`);
|
||||
}
|
||||
}
|
||||
|
||||
function getNodeJs(): INodeJs {
|
||||
return {
|
||||
os, path, fs, child_process,
|
||||
};
|
||||
}
|
||||
|
||||
export interface INodeJs {
|
||||
os: INodeOs;
|
||||
path: INodePath;
|
||||
fs: INodeFs;
|
||||
// eslint-disable-next-line camelcase
|
||||
child_process: INodeChildProcess;
|
||||
}
|
||||
|
||||
export interface INodeOs {
|
||||
tmpdir(): string;
|
||||
}
|
||||
|
||||
export interface INodePath {
|
||||
join(...paths: string[]): string;
|
||||
}
|
||||
|
||||
export interface INodeChildProcess {
|
||||
exec(command: string): void;
|
||||
}
|
||||
|
||||
export interface INodeFs {
|
||||
readonly promises: INodeFsPromises;
|
||||
}
|
||||
|
||||
interface INodeFsPromisesMakeDirectoryOptions {
|
||||
recursive?: boolean;
|
||||
}
|
||||
|
||||
interface INodeFsPromises { // https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/node/v13/fs.d.ts
|
||||
chmod(path: string, mode: string | number): Promise<void>;
|
||||
mkdir(path: string, options: INodeFsPromisesMakeDirectoryOptions): Promise<string>;
|
||||
writeFile(path: string, data: string): Promise<void>;
|
||||
}
|
||||
|
||||
49
src/infrastructure/Environment/Environment.ts
Normal file
49
src/infrastructure/Environment/Environment.ts
Normal file
@@ -0,0 +1,49 @@
|
||||
import { OperatingSystem } from '@/domain/OperatingSystem';
|
||||
import { ISystemOperations } from '@/infrastructure/Environment/SystemOperations/ISystemOperations';
|
||||
import { BrowserOsDetector } from './BrowserOs/BrowserOsDetector';
|
||||
import { IBrowserOsDetector } from './BrowserOs/IBrowserOsDetector';
|
||||
import { IEnvironment } from './IEnvironment';
|
||||
import { WindowVariables } from './WindowVariables';
|
||||
import { validateWindowVariables } from './WindowVariablesValidator';
|
||||
|
||||
export class Environment implements IEnvironment {
|
||||
public static readonly CurrentEnvironment: IEnvironment = new Environment(window);
|
||||
|
||||
public readonly isDesktop: boolean;
|
||||
|
||||
public readonly os: OperatingSystem | undefined;
|
||||
|
||||
public readonly system: ISystemOperations | undefined;
|
||||
|
||||
protected constructor(
|
||||
window: Partial<Window>,
|
||||
browserOsDetector: IBrowserOsDetector = new BrowserOsDetector(),
|
||||
windowValidator: WindowValidator = validateWindowVariables,
|
||||
) {
|
||||
if (!window) {
|
||||
throw new Error('missing window');
|
||||
}
|
||||
windowValidator(window);
|
||||
this.isDesktop = isDesktop(window);
|
||||
if (this.isDesktop) {
|
||||
this.os = window?.os;
|
||||
} else {
|
||||
this.os = undefined;
|
||||
const userAgent = getUserAgent(window);
|
||||
if (userAgent) {
|
||||
this.os = browserOsDetector.detect(userAgent);
|
||||
}
|
||||
}
|
||||
this.system = window?.system;
|
||||
}
|
||||
}
|
||||
|
||||
function getUserAgent(window: Partial<Window>): string {
|
||||
return window?.navigator?.userAgent;
|
||||
}
|
||||
|
||||
function isDesktop(window: Partial<WindowVariables>): boolean {
|
||||
return window?.isDesktop === true;
|
||||
}
|
||||
|
||||
export type WindowValidator = typeof validateWindowVariables;
|
||||
8
src/infrastructure/Environment/IEnvironment.ts
Normal file
8
src/infrastructure/Environment/IEnvironment.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import { OperatingSystem } from '@/domain/OperatingSystem';
|
||||
import { ISystemOperations } from '@/infrastructure/Environment/SystemOperations/ISystemOperations';
|
||||
|
||||
export interface IEnvironment {
|
||||
readonly isDesktop: boolean;
|
||||
readonly os: OperatingSystem | undefined;
|
||||
readonly system: ISystemOperations | undefined;
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
export interface ISystemOperations {
|
||||
readonly operatingSystem: IOperatingSystemOps;
|
||||
readonly location: ILocationOps;
|
||||
readonly fileSystem: IFileSystemOps;
|
||||
readonly command: ICommandOps;
|
||||
}
|
||||
|
||||
export interface IOperatingSystemOps {
|
||||
getTempDirectory(): string;
|
||||
}
|
||||
|
||||
export interface ILocationOps {
|
||||
combinePaths(...pathSegments: string[]): string;
|
||||
}
|
||||
|
||||
export interface ICommandOps {
|
||||
execute(command: string): void;
|
||||
}
|
||||
|
||||
export interface IFileSystemOps {
|
||||
setFilePermissions(filePath: string, mode: string | number): Promise<void>;
|
||||
createDirectory(directoryPath: string, isRecursive?: boolean): Promise<string>;
|
||||
writeToFile(filePath: string, data: string): Promise<void>;
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
import { tmpdir } from 'os';
|
||||
import { join } from 'path';
|
||||
import { chmod, mkdir, writeFile } from 'fs/promises';
|
||||
import { exec } from 'child_process';
|
||||
import { ISystemOperations } from './ISystemOperations';
|
||||
|
||||
export function createNodeSystemOperations(): ISystemOperations {
|
||||
return {
|
||||
operatingSystem: {
|
||||
getTempDirectory: () => tmpdir(),
|
||||
},
|
||||
location: {
|
||||
combinePaths: (...pathSegments) => join(...pathSegments),
|
||||
},
|
||||
fileSystem: {
|
||||
setFilePermissions: (
|
||||
filePath: string,
|
||||
mode: string | number,
|
||||
) => chmod(filePath, mode),
|
||||
createDirectory: (
|
||||
directoryPath: string,
|
||||
isRecursive?: boolean,
|
||||
) => mkdir(directoryPath, { recursive: isRecursive }),
|
||||
writeToFile: (
|
||||
filePath: string,
|
||||
data: string,
|
||||
) => writeFile(filePath, data),
|
||||
},
|
||||
command: {
|
||||
execute: (command) => exec(command),
|
||||
},
|
||||
};
|
||||
}
|
||||
13
src/infrastructure/Environment/WindowVariables.ts
Normal file
13
src/infrastructure/Environment/WindowVariables.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import { OperatingSystem } from '@/domain/OperatingSystem';
|
||||
import { ISystemOperations } from './SystemOperations/ISystemOperations';
|
||||
|
||||
export type WindowVariables = {
|
||||
system: ISystemOperations;
|
||||
isDesktop: boolean;
|
||||
os: OperatingSystem;
|
||||
};
|
||||
|
||||
declare global {
|
||||
// eslint-disable-next-line @typescript-eslint/no-empty-interface
|
||||
interface Window extends WindowVariables { }
|
||||
}
|
||||
76
src/infrastructure/Environment/WindowVariablesValidator.ts
Normal file
76
src/infrastructure/Environment/WindowVariablesValidator.ts
Normal file
@@ -0,0 +1,76 @@
|
||||
import { OperatingSystem } from '@/domain/OperatingSystem';
|
||||
import { PropertyKeys } from '@/TypeHelpers';
|
||||
import { WindowVariables } from './WindowVariables';
|
||||
|
||||
/**
|
||||
* Checks for consistency in runtime environment properties injected by Electron preloader.
|
||||
*/
|
||||
export function validateWindowVariables(variables: Partial<WindowVariables>) {
|
||||
if (!variables) {
|
||||
throw new Error('missing variables');
|
||||
}
|
||||
if (!isObject(variables)) {
|
||||
throw new Error(`window is not an object but ${typeof variables}`);
|
||||
}
|
||||
const errors = [...testEveryProperty(variables)];
|
||||
if (errors.length > 0) {
|
||||
throw new Error(errors.join('\n'));
|
||||
}
|
||||
}
|
||||
|
||||
function* testEveryProperty(variables: Partial<WindowVariables>): Iterable<string> {
|
||||
const tests: {
|
||||
[K in PropertyKeys<WindowVariables>]: boolean;
|
||||
} = {
|
||||
os: testOperatingSystem(variables.os),
|
||||
isDesktop: testIsDesktop(variables.isDesktop),
|
||||
system: testSystem(variables),
|
||||
};
|
||||
|
||||
for (const [propertyName, testResult] of Object.entries(tests)) {
|
||||
if (!testResult) {
|
||||
const propertyValue = variables[propertyName as keyof WindowVariables];
|
||||
yield `Unexpected ${propertyName} (${typeof propertyValue})`;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function testOperatingSystem(os: unknown): boolean {
|
||||
if (os === undefined) {
|
||||
return true;
|
||||
}
|
||||
if (!isNumber(os)) {
|
||||
return false;
|
||||
}
|
||||
return Object
|
||||
.values(OperatingSystem)
|
||||
.includes(os);
|
||||
}
|
||||
|
||||
function testSystem(variables: Partial<WindowVariables>): boolean {
|
||||
if (!variables.isDesktop) {
|
||||
return true;
|
||||
}
|
||||
return variables.system !== undefined && isObject(variables.system);
|
||||
}
|
||||
|
||||
function testIsDesktop(isDesktop: unknown): boolean {
|
||||
if (isDesktop === undefined) {
|
||||
return true;
|
||||
}
|
||||
return isBoolean(isDesktop);
|
||||
}
|
||||
|
||||
function isNumber(variable: unknown): variable is number {
|
||||
return typeof variable === 'number';
|
||||
}
|
||||
|
||||
function isBoolean(variable: unknown): variable is boolean {
|
||||
return typeof variable === 'boolean';
|
||||
}
|
||||
|
||||
function isObject(variable: unknown): variable is object {
|
||||
return typeof variable === 'object'
|
||||
&& variable !== null // the data type of null is an object
|
||||
&& !Array.isArray(variable);
|
||||
}
|
||||
@@ -1,16 +1,18 @@
|
||||
import { IAppMetadata } from './IAppMetadata';
|
||||
import { IAppMetadataFactory } from './IAppMetadataFactory';
|
||||
import { validateMetadata } from './MetadataValidator';
|
||||
import { ViteAppMetadata } from './Vite/ViteAppMetadata';
|
||||
|
||||
export class AppMetadataFactory {
|
||||
public static get Current(): IAppMetadata {
|
||||
if (!this.instance) {
|
||||
this.instance = new ViteAppMetadata();
|
||||
}
|
||||
return this.instance;
|
||||
export class AppMetadataFactory implements IAppMetadataFactory {
|
||||
public static readonly Current = new AppMetadataFactory();
|
||||
|
||||
public readonly instance: IAppMetadata;
|
||||
|
||||
protected constructor(validator: MetadataValidator = validateMetadata) {
|
||||
const metadata = new ViteAppMetadata();
|
||||
validator(metadata);
|
||||
this.instance = metadata;
|
||||
}
|
||||
|
||||
private static instance: IAppMetadata;
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
||||
private constructor() {}
|
||||
}
|
||||
|
||||
export type MetadataValidator = typeof validateMetadata;
|
||||
|
||||
5
src/infrastructure/Metadata/IAppMetadataFactory.ts
Normal file
5
src/infrastructure/Metadata/IAppMetadataFactory.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
import { IAppMetadata } from './IAppMetadata';
|
||||
|
||||
export interface IAppMetadataFactory {
|
||||
readonly instance: IAppMetadata;
|
||||
}
|
||||
50
src/infrastructure/Metadata/MetadataValidator.ts
Normal file
50
src/infrastructure/Metadata/MetadataValidator.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
import { IAppMetadata } from '@/infrastructure/Metadata/IAppMetadata';
|
||||
|
||||
/* Validation is externalized to keep the environment objects simple */
|
||||
export function validateMetadata(metadata: IAppMetadata): void {
|
||||
if (!metadata) {
|
||||
throw new Error('missing metadata');
|
||||
}
|
||||
const keyValues = capturePropertyValues(metadata);
|
||||
if (!Object.keys(keyValues).length) {
|
||||
throw new Error('Unable to capture metadata key/value pairs');
|
||||
}
|
||||
const keysMissingValue = getMissingMetadataKeys(keyValues);
|
||||
if (keysMissingValue.length > 0) {
|
||||
throw new Error(`Metadata keys missing: ${keysMissingValue.join(', ')}`);
|
||||
}
|
||||
}
|
||||
|
||||
function getMissingMetadataKeys(keyValuePairs: Record<string, unknown>): string[] {
|
||||
return Object.entries(keyValuePairs)
|
||||
.reduce((acc, [key, value]) => {
|
||||
if (!value) {
|
||||
acc.push(key);
|
||||
}
|
||||
return acc;
|
||||
}, new Array<string>());
|
||||
}
|
||||
|
||||
/**
|
||||
* Captures values of properties and getters from the provided instance.
|
||||
* Necessary because code transformations can make class getters non-enumerable during bundling.
|
||||
* This ensures that even if getters are non-enumerable, their values are still captured and used.
|
||||
*/
|
||||
function capturePropertyValues(instance: unknown): Record<string, unknown> {
|
||||
const obj: Record<string, unknown> = {};
|
||||
const descriptors = Object.getOwnPropertyDescriptors(instance.constructor.prototype);
|
||||
|
||||
// Capture regular properties from the instance
|
||||
for (const [key, value] of Object.entries(instance)) {
|
||||
obj[key] = value;
|
||||
}
|
||||
|
||||
// Capture getter properties from the instance's prototype
|
||||
for (const [key, descriptor] of Object.entries(descriptors)) {
|
||||
if (typeof descriptor.get === 'function') {
|
||||
obj[key] = descriptor.get.call(instance);
|
||||
}
|
||||
}
|
||||
|
||||
return obj;
|
||||
}
|
||||
31
src/infrastructure/RuntimeSanity/Common/FactoryValidator.ts
Normal file
31
src/infrastructure/RuntimeSanity/Common/FactoryValidator.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
import { ISanityCheckOptions } from './ISanityCheckOptions';
|
||||
import { ISanityValidator } from './ISanityValidator';
|
||||
|
||||
export type FactoryFunction<T> = () => T;
|
||||
|
||||
export abstract class FactoryValidator<T> implements ISanityValidator {
|
||||
private readonly factory: FactoryFunction<T>;
|
||||
|
||||
protected constructor(factory: FactoryFunction<T>) {
|
||||
if (!factory) {
|
||||
throw new Error('missing factory');
|
||||
}
|
||||
this.factory = factory;
|
||||
}
|
||||
|
||||
public abstract shouldValidate(options: ISanityCheckOptions): boolean;
|
||||
|
||||
public abstract name: string;
|
||||
|
||||
public* collectErrors(): Iterable<string> {
|
||||
try {
|
||||
const value = this.factory();
|
||||
if (!value) {
|
||||
// Do not remove this check, it ensures that the factory call is not optimized away.
|
||||
yield 'Factory resulted in a falsy value';
|
||||
}
|
||||
} catch (error) {
|
||||
yield `Error in factory creation: ${error.message}`;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
export interface ISanityCheckOptions {
|
||||
readonly validateMetadata: boolean;
|
||||
readonly validateEnvironment: boolean;
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
import { ISanityCheckOptions } from './ISanityCheckOptions';
|
||||
|
||||
export interface ISanityValidator {
|
||||
readonly name: string;
|
||||
shouldValidate(options: ISanityCheckOptions): boolean;
|
||||
collectErrors(): Iterable<string>;
|
||||
}
|
||||
@@ -1,24 +1,23 @@
|
||||
import { ISanityCheckOptions } from './ISanityCheckOptions';
|
||||
import { ISanityValidator } from './ISanityValidator';
|
||||
import { ISanityCheckOptions } from './Common/ISanityCheckOptions';
|
||||
import { ISanityValidator } from './Common/ISanityValidator';
|
||||
import { MetadataValidator } from './Validators/MetadataValidator';
|
||||
|
||||
const SanityValidators: ISanityValidator[] = [
|
||||
const DefaultSanityValidators: ISanityValidator[] = [
|
||||
new MetadataValidator(),
|
||||
];
|
||||
|
||||
/* Helps to fail-fast on errors */
|
||||
export function validateRuntimeSanity(
|
||||
options: ISanityCheckOptions,
|
||||
validators: readonly ISanityValidator[] = SanityValidators,
|
||||
validators: readonly ISanityValidator[] = DefaultSanityValidators,
|
||||
): void {
|
||||
if (!options) {
|
||||
throw new Error('missing options');
|
||||
}
|
||||
if (!validators?.length) {
|
||||
throw new Error('missing validators');
|
||||
}
|
||||
validateContext(options, validators);
|
||||
const errorMessages = validators.reduce((errors, validator) => {
|
||||
if (validator.shouldValidate(options)) {
|
||||
errors.push(...validator.collectErrors());
|
||||
const errorMessage = getErrorMessage(validator);
|
||||
if (errorMessage) {
|
||||
errors.push(errorMessage);
|
||||
}
|
||||
}
|
||||
return errors;
|
||||
}, new Array<string>());
|
||||
@@ -26,3 +25,26 @@ export function validateRuntimeSanity(
|
||||
throw new Error(`Sanity check failed.\n${errorMessages.join('\n---\n')}`);
|
||||
}
|
||||
}
|
||||
|
||||
function validateContext(
|
||||
options: ISanityCheckOptions,
|
||||
validators: readonly ISanityValidator[],
|
||||
) {
|
||||
if (!options) {
|
||||
throw new Error('missing options');
|
||||
}
|
||||
if (!validators?.length) {
|
||||
throw new Error('missing validators');
|
||||
}
|
||||
if (validators.some((validator) => !validator)) {
|
||||
throw new Error('missing validator in validators');
|
||||
}
|
||||
}
|
||||
|
||||
function getErrorMessage(validator: ISanityValidator): string | undefined {
|
||||
const errorMessages = [...validator.collectErrors()];
|
||||
if (!errorMessages.length) {
|
||||
return undefined;
|
||||
}
|
||||
return `${validator.name}:\n${errorMessages.join('\n')}`;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
import { Environment } from '@/infrastructure/Environment/Environment';
|
||||
import { IEnvironment } from '@/infrastructure/Environment/IEnvironment';
|
||||
import { ISanityCheckOptions } from '../Common/ISanityCheckOptions';
|
||||
import { FactoryValidator, FactoryFunction } from '../Common/FactoryValidator';
|
||||
|
||||
export class EnvironmentValidator extends FactoryValidator<IEnvironment> {
|
||||
constructor(factory: FactoryFunction<IEnvironment> = () => Environment.CurrentEnvironment) {
|
||||
super(factory);
|
||||
}
|
||||
|
||||
public override name = 'environment';
|
||||
|
||||
public override shouldValidate(options: ISanityCheckOptions): boolean {
|
||||
return options.validateEnvironment;
|
||||
}
|
||||
}
|
||||
@@ -1,66 +1,16 @@
|
||||
import { IAppMetadata } from '@/infrastructure/Metadata/IAppMetadata';
|
||||
import { AppMetadataFactory } from '@/infrastructure/Metadata/AppMetadataFactory';
|
||||
import { ISanityCheckOptions } from '../ISanityCheckOptions';
|
||||
import { ISanityValidator } from '../ISanityValidator';
|
||||
import { ISanityCheckOptions } from '../Common/ISanityCheckOptions';
|
||||
import { FactoryValidator, FactoryFunction } from '../Common/FactoryValidator';
|
||||
|
||||
export class MetadataValidator implements ISanityValidator {
|
||||
private readonly metadata: IAppMetadata;
|
||||
|
||||
constructor(metadataFactory: () => IAppMetadata = () => AppMetadataFactory.Current) {
|
||||
this.metadata = metadataFactory();
|
||||
export class MetadataValidator extends FactoryValidator<IAppMetadata> {
|
||||
constructor(factory: FactoryFunction<IAppMetadata> = () => AppMetadataFactory.Current.instance) {
|
||||
super(factory);
|
||||
}
|
||||
|
||||
public shouldValidate(options: ISanityCheckOptions): boolean {
|
||||
public override name = 'metadata';
|
||||
|
||||
public override shouldValidate(options: ISanityCheckOptions): boolean {
|
||||
return options.validateMetadata;
|
||||
}
|
||||
|
||||
public* collectErrors(): Iterable<string> {
|
||||
if (!this.metadata) {
|
||||
yield 'missing metadata';
|
||||
return;
|
||||
}
|
||||
const keyValues = capturePropertyValues(this.metadata);
|
||||
if (!Object.keys(keyValues).length) {
|
||||
yield 'Unable to capture metadata key/value pairs';
|
||||
return;
|
||||
}
|
||||
const keysMissingValue = getMissingMetadataKeys(keyValues);
|
||||
if (keysMissingValue.length > 0) {
|
||||
yield `Metadata keys missing: ${keysMissingValue.join(', ')}`;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function getMissingMetadataKeys(keyValuePairs: Record<string, unknown>): string[] {
|
||||
return Object.entries(keyValuePairs)
|
||||
.reduce((acc, [key, value]) => {
|
||||
if (!value) {
|
||||
acc.push(key);
|
||||
}
|
||||
return acc;
|
||||
}, new Array<string>());
|
||||
}
|
||||
|
||||
/**
|
||||
* Captures values of properties and getters from the provided instance.
|
||||
* Necessary because code transformations can make class getters non-enumerable during bundling.
|
||||
* This ensures that even if getters are non-enumerable, their values are still captured and used.
|
||||
*/
|
||||
function capturePropertyValues(instance: unknown): Record<string, unknown> {
|
||||
const obj: Record<string, unknown> = {};
|
||||
const descriptors = Object.getOwnPropertyDescriptors(instance.constructor.prototype);
|
||||
|
||||
// Capture regular properties from the instance
|
||||
for (const [key, value] of Object.entries(instance)) {
|
||||
obj[key] = value;
|
||||
}
|
||||
|
||||
// Capture getter properties from the instance's prototype
|
||||
for (const [key, descriptor] of Object.entries(descriptors)) {
|
||||
if (typeof descriptor.get === 'function') {
|
||||
obj[key] = descriptor.get.call(instance);
|
||||
}
|
||||
}
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ import {
|
||||
useCollectionStateKey, useApplicationKey, useEnvironmentKey,
|
||||
} from '@/presentation/injectionSymbols';
|
||||
import { IApplicationContext } from '@/application/Context/IApplicationContext';
|
||||
import { Environment } from '@/application/Environment/Environment';
|
||||
import { Environment } from '@/infrastructure/Environment/Environment';
|
||||
|
||||
export function provideDependencies(context: IApplicationContext) {
|
||||
registerSingleton(useApplicationKey, useApplication(context.app));
|
||||
|
||||
@@ -35,6 +35,7 @@ export default defineComponent({
|
||||
provideDependencies(singletonAppContext); // In Vue 3.0 we can move it to main.ts
|
||||
validateRuntimeSanity({
|
||||
validateMetadata: true,
|
||||
validateEnvironment: true,
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
@@ -33,7 +33,7 @@ import { useCollectionStateKey, useEnvironmentKey } from '@/presentation/injecti
|
||||
import { SaveFileDialog, FileType } from '@/infrastructure/SaveFileDialog';
|
||||
import { Clipboard } from '@/infrastructure/Clipboard';
|
||||
import ModalDialog from '@/presentation/components/Shared/Modal/ModalDialog.vue';
|
||||
import { Environment } from '@/application/Environment/Environment';
|
||||
import { Environment } from '@/infrastructure/Environment/Environment';
|
||||
import { IReadOnlyCategoryCollectionState } from '@/application/Context/State/ICategoryCollectionState';
|
||||
import { ScriptingLanguage } from '@/domain/ScriptingLanguage';
|
||||
import { IApplicationCode } from '@/application/Context/State/Code/IApplicationCode';
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { IEnvironment } from '@/application/Environment/IEnvironment';
|
||||
import { IEnvironment } from '@/infrastructure/Environment/IEnvironment';
|
||||
|
||||
export function useEnvironment(environment: IEnvironment) {
|
||||
if (!environment) {
|
||||
|
||||
@@ -24,7 +24,12 @@ protocol.registerSchemesAsPrivileged([
|
||||
|
||||
setupLogger();
|
||||
validateRuntimeSanity({
|
||||
// Metadata is used by manual updates.
|
||||
validateMetadata: true,
|
||||
|
||||
// Environment is populated by the preload script and is in the renderer's context;
|
||||
// it's not directly accessible from the main process.
|
||||
validateEnvironment: false,
|
||||
});
|
||||
|
||||
function createWindow() {
|
||||
@@ -34,8 +39,8 @@ function createWindow() {
|
||||
width: size.width,
|
||||
height: size.height,
|
||||
webPreferences: {
|
||||
nodeIntegration: true,
|
||||
contextIsolation: false,
|
||||
nodeIntegration: true, // disabling does not work with electron-vite, https://electron-vite.org/guide/dev.html#nodeintegration
|
||||
contextIsolation: true,
|
||||
preload: PRELOADER_SCRIPT_PATH,
|
||||
},
|
||||
icon: APP_ICON_PATH,
|
||||
|
||||
14
src/presentation/electron/preload/NodeOsMapper.ts
Normal file
14
src/presentation/electron/preload/NodeOsMapper.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import { OperatingSystem } from '@/domain/OperatingSystem';
|
||||
|
||||
export function convertPlatformToOs(platform: NodeJS.Platform): OperatingSystem | undefined {
|
||||
switch (platform) {
|
||||
case 'darwin':
|
||||
return OperatingSystem.macOS;
|
||||
case 'win32':
|
||||
return OperatingSystem.Windows;
|
||||
case 'linux':
|
||||
return OperatingSystem.Linux;
|
||||
default:
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
14
src/presentation/electron/preload/WindowVariablesProvider.ts
Normal file
14
src/presentation/electron/preload/WindowVariablesProvider.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import { createNodeSystemOperations } from '@/infrastructure/Environment/SystemOperations/NodeSystemOperations';
|
||||
import { WindowVariables } from '@/infrastructure/Environment/WindowVariables';
|
||||
import { convertPlatformToOs } from './NodeOsMapper';
|
||||
|
||||
export function provideWindowVariables(
|
||||
createSystem = createNodeSystemOperations,
|
||||
convertToOs = convertPlatformToOs,
|
||||
): WindowVariables {
|
||||
return {
|
||||
system: createSystem(),
|
||||
isDesktop: true,
|
||||
os: convertToOs(process.platform),
|
||||
};
|
||||
}
|
||||
@@ -1,10 +1,22 @@
|
||||
// This preload script serves as a placeholder to securely expose Electron APIs to the application.
|
||||
// As of now, the application does not utilize any specific Electron APIs through this script.
|
||||
// This file is used to securely expose Electron APIs to the application.
|
||||
|
||||
import { contextBridge } from 'electron';
|
||||
import log from 'electron-log';
|
||||
import { validateRuntimeSanity } from '@/infrastructure/RuntimeSanity/SanityChecks';
|
||||
import { provideWindowVariables } from './WindowVariablesProvider';
|
||||
|
||||
validateRuntimeSanity({
|
||||
// Validate metadata as a preventive measure for fail-fast,
|
||||
// even if it's not currently used in the preload script.
|
||||
validateMetadata: true,
|
||||
|
||||
// The preload script cannot access variables on the window object.
|
||||
validateEnvironment: false,
|
||||
});
|
||||
|
||||
const windowVariables = provideWindowVariables();
|
||||
Object.entries(windowVariables).forEach(([key, value]) => {
|
||||
contextBridge.exposeInMainWorld(key, value);
|
||||
});
|
||||
|
||||
// Do not remove [PRELOAD_INIT]; it's a marker used in tests.
|
||||
|
||||
Reference in New Issue
Block a user