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:
undergroundwires
2023-08-25 14:31:30 +02:00
parent 62f8bfac2f
commit e9e0001ef8
83 changed files with 1846 additions and 769 deletions

View File

@@ -3,16 +3,48 @@ import { EnvironmentStub } from '@tests/unit/shared/Stubs/EnvironmentStub';
import { OperatingSystem } from '@/domain/OperatingSystem';
import { CodeRunner } from '@/infrastructure/CodeRunner';
import { expectThrowsAsync } from '@tests/unit/shared/Assertions/ExpectThrowsAsync';
import { SystemOperationsStub } from '@tests/unit/shared/Stubs/SystemOperationsStub';
import { OperatingSystemOpsStub } from '@tests/unit/shared/Stubs/OperatingSystemOpsStub';
import { LocationOpsStub } from '@tests/unit/shared/Stubs/LocationOpsStub';
import { FileSystemOpsStub } from '@tests/unit/shared/Stubs/FileSystemOpsStub';
import { CommandOpsStub } from '@tests/unit/shared/Stubs/CommandOpsStub';
import { IFileSystemOps, ISystemOperations } from '@/infrastructure/Environment/SystemOperations/ISystemOperations';
import { FunctionKeys } from '@/TypeHelpers';
import { itEachAbsentObjectValue } from '@tests/unit/shared/TestCases/AbsentTests';
describe('CodeRunner', () => {
describe('ctor throws if system is missing', () => {
itEachAbsentObjectValue((absentValue) => {
// arrange
const expectedError = 'missing system operations';
const environment = new EnvironmentStub()
.withSystemOperations(absentValue);
// act
const act = () => new CodeRunner(environment);
// assert
expect(act).to.throw(expectedError);
});
});
describe('runCode', () => {
it('creates temporary directory recursively', async () => {
// arrange
const expectedDir = 'expected-dir';
const expectedIsRecursive = true;
const folderName = 'privacy.sexy';
const context = new TestContext();
context.mocks.os.setupTmpdir('tmp');
context.mocks.path.setupJoin(expectedDir, 'tmp', folderName);
const temporaryDirName = 'tmp';
const filesystem = new FileSystemOpsStub();
const context = new TestContext()
.withSystemOperationsStub((ops) => ops
.withOperatingSystem(
new OperatingSystemOpsStub()
.withTemporaryDirectoryResult(temporaryDirName),
)
.withLocation(
new LocationOpsStub()
.withJoinResult(expectedDir, temporaryDirName, folderName),
)
.withFileSystem(filesystem));
// act
await context
@@ -20,22 +52,34 @@ describe('CodeRunner', () => {
.runCode();
// assert
expect(context.mocks.fs.mkdirHistory.length).to.equal(1);
expect(context.mocks.fs.mkdirHistory[0].isRecursive).to.equal(true);
expect(context.mocks.fs.mkdirHistory[0].path).to.equal(expectedDir);
const calls = filesystem.callHistory.filter((call) => call.methodName === 'createDirectory');
expect(calls.length).to.equal(1);
const [actualPath, actualIsRecursive] = calls[0].args;
expect(actualPath).to.equal(expectedDir);
expect(actualIsRecursive).to.equal(expectedIsRecursive);
});
it('creates a file with expected code and path', async () => {
// arrange
const expectedCode = 'expected-code';
const expectedFilePath = 'expected-file-path';
const filesystem = new FileSystemOpsStub();
const extension = '.sh';
const expectedName = `run.${extension}`;
const folderName = 'privacy.sexy';
const context = new TestContext();
context.mocks.os.setupTmpdir('tmp');
context.mocks.path.setupJoin('folder', 'tmp', folderName);
context.mocks.path.setupJoin(expectedFilePath, 'folder', expectedName);
const temporaryDirName = 'tmp';
const context = new TestContext()
.withSystemOperationsStub((ops) => ops
.withOperatingSystem(
new OperatingSystemOpsStub()
.withTemporaryDirectoryResult(temporaryDirName),
)
.withLocation(
new LocationOpsStub()
.withJoinResult('folder', temporaryDirName, folderName)
.withJoinResult(expectedFilePath, 'folder', expectedName),
)
.withFileSystem(filesystem));
// act
await context
@@ -45,22 +89,34 @@ describe('CodeRunner', () => {
.runCode();
// assert
expect(context.mocks.fs.writeFileHistory.length).to.equal(1);
expect(context.mocks.fs.writeFileHistory[0].data).to.equal(expectedCode);
expect(context.mocks.fs.writeFileHistory[0].path).to.equal(expectedFilePath);
const calls = filesystem.callHistory.filter((call) => call.methodName === 'writeToFile');
expect(calls.length).to.equal(1);
const [actualFilePath, actualData] = calls[0].args;
expect(actualFilePath).to.equal(expectedFilePath);
expect(actualData).to.equal(expectedCode);
});
it('set file permissions as expected', async () => {
// arrange
const expectedMode = '755';
const expectedFilePath = 'expected-file-path';
const filesystem = new FileSystemOpsStub();
const extension = '.sh';
const expectedName = `run.${extension}`;
const folderName = 'privacy.sexy';
const context = new TestContext();
context.mocks.os.setupTmpdir('tmp');
context.mocks.path.setupJoin('folder', 'tmp', folderName);
context.mocks.path.setupJoin(expectedFilePath, 'folder', expectedName);
const temporaryDirName = 'tmp';
const context = new TestContext()
.withSystemOperationsStub((ops) => ops
.withOperatingSystem(
new OperatingSystemOpsStub()
.withTemporaryDirectoryResult(temporaryDirName),
)
.withLocation(
new LocationOpsStub()
.withJoinResult('folder', temporaryDirName, folderName)
.withJoinResult(expectedFilePath, 'folder', expectedName),
)
.withFileSystem(filesystem));
// act
await context
@@ -69,57 +125,74 @@ describe('CodeRunner', () => {
.runCode();
// assert
expect(context.mocks.fs.chmodCallHistory.length).to.equal(1);
expect(context.mocks.fs.chmodCallHistory[0].mode).to.equal(expectedMode);
expect(context.mocks.fs.chmodCallHistory[0].path).to.equal(expectedFilePath);
const calls = filesystem.callHistory.filter((call) => call.methodName === 'setFilePermissions');
expect(calls.length).to.equal(1);
const [actualFilePath, actualMode] = calls[0].args;
expect(actualFilePath).to.equal(expectedFilePath);
expect(actualMode).to.equal(expectedMode);
});
describe('executes as expected', () => {
// arrange
const filePath = 'expected-file-path';
const testData = [
interface IExecutionTestCase {
readonly givenOs: OperatingSystem;
readonly expectedCommand: string;
}
const testData: readonly IExecutionTestCase[] = [
{
os: OperatingSystem.Windows,
expected: filePath,
givenOs: OperatingSystem.Windows,
expectedCommand: filePath,
},
{
os: OperatingSystem.macOS,
expected: `open -a Terminal.app ${filePath}`,
givenOs: OperatingSystem.macOS,
expectedCommand: `open -a Terminal.app ${filePath}`,
},
{
os: OperatingSystem.Linux,
expected: `x-terminal-emulator -e '${filePath}'`,
givenOs: OperatingSystem.Linux,
expectedCommand: `x-terminal-emulator -e '${filePath}'`,
},
];
for (const data of testData) {
it(`returns ${data.expected} on ${OperatingSystem[data.os]}`, async () => {
const context = new TestContext();
context.mocks.os.setupTmpdir('non-important-temp-dir-name');
context.mocks.path.setupJoinSequence('non-important-folder-name', filePath);
context.withOs(data.os);
for (const { givenOs, expectedCommand } of testData) {
it(`returns ${expectedCommand} on ${OperatingSystem[givenOs]}`, async () => {
const command = new CommandOpsStub();
const context = new TestContext()
.withSystemOperationsStub((ops) => ops
.withLocation(
new LocationOpsStub()
.withJoinResultSequence('non-important-folder-name', filePath),
)
.withCommand(command));
// act
await context
.withOs(data.os)
.withOs(givenOs)
.runCode();
// assert
expect(context.mocks.child_process.executionHistory.length).to.equal(1);
expect(context.mocks.child_process.executionHistory[0]).to.equal(data.expected);
const calls = command.callHistory.filter((c) => c.methodName === 'execute');
expect(calls.length).to.equal(1);
const [actualCommand] = calls[0].args;
expect(actualCommand).to.equal(expectedCommand);
});
}
});
it('runs in expected order', async () => {
// arrange
const expectedOrder = [NodeJsCommand.mkdir, NodeJsCommand.writeFile, NodeJsCommand.chmod];
const context = new TestContext();
context.mocks.os.setupTmpdir('non-important-temp-dir-name');
context.mocks.path.setupJoinSequence('non-important-folder-name1', 'non-important-folder-name2');
it('runs in expected order', async () => { // verifies correct `async`, `await` usage.
const expectedOrder: readonly FunctionKeys<IFileSystemOps>[] = [
'createDirectory',
'writeToFile',
'setFilePermissions',
];
const fileSystem = new FileSystemOpsStub();
const context = new TestContext()
.withSystemOperationsStub((ops) => ops
.withFileSystem(fileSystem));
// act
await context.runCode();
// assert
const actualOrder = context.mocks.commandHistory
const actualOrder = fileSystem.callHistory
.map((c) => c.methodName)
.filter((command) => expectedOrder.includes(command));
expect(expectedOrder).to.deep.equal(actualOrder);
});
@@ -138,23 +211,40 @@ describe('CodeRunner', () => {
});
class TestContext {
public mocks = getNodeJsMocks();
private code = 'code';
private folderName = 'folderName';
private fileExtension = 'fileExtension';
private env = mockEnvironment(OperatingSystem.Windows);
private os = OperatingSystem.Windows;
private systemOperations: ISystemOperations = new SystemOperationsStub();
public async runCode(): Promise<void> {
const runner = new CodeRunner(this.mocks, this.env);
const environment = new EnvironmentStub()
.withOs(this.os)
.withSystemOperations(this.systemOperations);
const runner = new CodeRunner(environment);
await runner.runCode(this.code, this.folderName, this.fileExtension);
}
public withSystemOperations(
systemOperations: ISystemOperations,
): this {
this.systemOperations = systemOperations;
return this;
}
public withSystemOperationsStub(
setup: (stub: SystemOperationsStub) => SystemOperationsStub,
): this {
const stub = setup(new SystemOperationsStub());
return this.withSystemOperations(stub);
}
public withOs(os: OperatingSystem) {
this.env = mockEnvironment(os);
this.os = os;
return this;
}
@@ -173,104 +263,3 @@ class TestContext {
return this;
}
}
function mockEnvironment(os: OperatingSystem) {
return new EnvironmentStub().withOs(os);
}
const enum NodeJsCommand { tmpdir, join, exec, mkdir, writeFile, chmod }
function getNodeJsMocks() {
const commandHistory = new Array<NodeJsCommand>();
return {
os: mockOs(commandHistory),
path: mockPath(commandHistory),
fs: mockNodeFs(commandHistory),
child_process: mockChildProcess(commandHistory),
commandHistory,
};
}
function mockOs(commandHistory: NodeJsCommand[]) {
let tmpDir = '/stub-temp-dir/';
return {
setupTmpdir: (value: string): void => {
tmpDir = value;
},
tmpdir: (): string => {
if (!tmpDir) {
throw new Error('tmpdir not set up');
}
commandHistory.push(NodeJsCommand.tmpdir);
return tmpDir;
},
};
}
function mockPath(commandHistory: NodeJsCommand[]) {
const sequence = new Array<string>();
const scenarios = new Map<string, string>();
const getScenarioKey = (paths: string[]) => paths.join('|');
return {
setupJoin: (returnValue: string, ...paths: string[]): void => {
scenarios.set(getScenarioKey(paths), returnValue);
},
setupJoinSequence: (...valuesToReturn: string[]): void => {
sequence.push(...valuesToReturn);
sequence.reverse();
},
join: (...paths: string[]): string => {
commandHistory.push(NodeJsCommand.join);
if (sequence.length > 0) {
return sequence.pop();
}
const key = getScenarioKey(paths);
if (!scenarios.has(key)) {
return paths.join('/');
}
return scenarios.get(key);
},
};
}
function mockChildProcess(commandHistory: NodeJsCommand[]) {
const executionHistory = new Array<string>();
return {
exec: (command: string): void => {
commandHistory.push(NodeJsCommand.exec);
executionHistory.push(command);
},
executionHistory,
};
}
function mockNodeFs(commandHistory: NodeJsCommand[]) {
interface IMkdirCall { path: string; isRecursive: boolean; }
interface IWriteFileCall { path: string; data: string; }
interface IChmodCall { path: string; mode: string | number; }
const mkdirHistory = new Array<IMkdirCall>();
const writeFileHistory = new Array<IWriteFileCall>();
const chmodCallHistory = new Array<IChmodCall>();
return {
promises: {
mkdir: (path, options) => {
commandHistory.push(NodeJsCommand.mkdir);
mkdirHistory.push({ path, isRecursive: options && options.recursive });
return Promise.resolve(path);
},
writeFile: (path, data) => {
commandHistory.push(NodeJsCommand.writeFile);
writeFileHistory.push({ path, data });
return Promise.resolve();
},
chmod: (path, mode) => {
commandHistory.push(NodeJsCommand.chmod);
chmodCallHistory.push({ path, mode });
return Promise.resolve();
},
},
mkdirHistory,
writeFileHistory,
chmodCallHistory,
};
}