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.
This commit is contained in:
47
src/infrastructure/CodeRunner/TemporaryFileCodeRunner.ts
Normal file
47
src/infrastructure/CodeRunner/TemporaryFileCodeRunner.ts
Normal file
@@ -0,0 +1,47 @@
|
||||
import { OperatingSystem } from '@/domain/OperatingSystem';
|
||||
import { CodeRunner } from '@/application/CodeRunner';
|
||||
import { SystemOperations } from './SystemOperations/SystemOperations';
|
||||
import { createNodeSystemOperations } from './SystemOperations/NodeSystemOperations';
|
||||
|
||||
export class TemporaryFileCodeRunner implements CodeRunner {
|
||||
constructor(
|
||||
private readonly system: SystemOperations = createNodeSystemOperations(),
|
||||
) { }
|
||||
|
||||
public async runCode(
|
||||
code: string,
|
||||
folderName: string,
|
||||
fileExtension: string,
|
||||
os: OperatingSystem,
|
||||
): Promise<void> {
|
||||
const dir = this.system.location.combinePaths(
|
||||
this.system.operatingSystem.getTempDirectory(),
|
||||
folderName,
|
||||
);
|
||||
await this.system.fileSystem.createDirectory(dir, true);
|
||||
const filePath = this.system.location.combinePaths(dir, `run.${fileExtension}`);
|
||||
await this.system.fileSystem.writeToFile(filePath, code);
|
||||
await this.system.fileSystem.setFilePermissions(filePath, '755');
|
||||
const command = getExecuteCommand(filePath, os);
|
||||
await this.system.command.execute(command);
|
||||
}
|
||||
}
|
||||
|
||||
function getExecuteCommand(
|
||||
scriptPath: string,
|
||||
currentOperatingSystem: OperatingSystem,
|
||||
): string {
|
||||
switch (currentOperatingSystem) {
|
||||
case OperatingSystem.Linux:
|
||||
return `x-terminal-emulator -e '${scriptPath}'`;
|
||||
case OperatingSystem.macOS:
|
||||
return `open -a Terminal.app ${scriptPath}`;
|
||||
// Another option with graphical sudo would be
|
||||
// `osascript -e "do shell script \\"${scriptPath}\\" with administrator privileges"`
|
||||
// However it runs in background
|
||||
case OperatingSystem.Windows:
|
||||
return scriptPath;
|
||||
default:
|
||||
throw Error(`unsupported os: ${OperatingSystem[currentOperatingSystem]}`);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user