-
-
Notifications
You must be signed in to change notification settings - Fork 174
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
1 parent
62f8bfa
commit e9e0001
Showing
83 changed files
with
1,843 additions
and
766 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
24 changes: 24 additions & 0 deletions
24
src/infrastructure/Environment/SystemOperations/ISystemOperations.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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>; | ||
} |
33 changes: 33 additions & 0 deletions
33
src/infrastructure/Environment/SystemOperations/NodeSystemOperations.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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), | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 { } | ||
} |
Oops, something went wrong.