Skip to content

Commit

Permalink
Support resolution of unencrypted code from plaintext asar file (closes
Browse files Browse the repository at this point in the history
  • Loading branch information
sleeyax committed Apr 19, 2024
1 parent 14b98e6 commit 78c5584
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 67 deletions.
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,13 +169,12 @@ Don't forget to update `package.json` as well:
- "main": "./dist/main/main.js",
```

4. Load any hooks at the start of the [main process](https://github.com/sleeyax/asarmor/blob/master/example/electron/src/main/main.ts) file (optional):
4. **Optional**: load configuration hooks at the start of the [main process](https://github.com/sleeyax/asarmor/blob/master/example/electron/src/main/main.ts) file:
```ts
// main.ts
import { hookNodeModulesAsar } from 'asarmor/encryption/hooks';
import { allowUnencrypted } from 'asarmor';

// load hooks at the start of the file
hookNodeModulesAsar(); // enables resolution of non-encrypted dependencies from node_modules.asar
allowUnencrypted(['node_modules']); // enables resolution of non-encrypted dependencies from `node_modules.asar`
```

5. Update your `BrowserWindow.webPreferences` configuration settings:
Expand Down
6 changes: 3 additions & 3 deletions example/electron/src/main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ import { autoUpdater } from 'electron-updater';
import log from 'electron-log';
import MenuBuilder from './menu';
import { resolveHtmlPath } from './util';
import { hookNodeModulesAsar } from '../../../../build/src/encryption/hooks';
import { allowUnencrypted } from '../../../../build/src';

allowUnencrypted(['node_modules']);

export default class AppUpdater {
constructor() {
Expand All @@ -24,8 +26,6 @@ export default class AppUpdater {
}
}

hookNodeModulesAsar();

let mainWindow: BrowserWindow | null = null;

ipcMain.on('ipc-example', async (event, arg) => {
Expand Down
61 changes: 61 additions & 0 deletions src/encryption/allowlist.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/* eslint-disable no-plusplus */
/* eslint-disable func-names */
/* eslint-disable no-underscore-dangle */
/* eslint-disable @typescript-eslint/ban-ts-comment */
import path from 'path';
import Module from 'module';

// @ts-ignore
const originalResolveLookupPaths = Module._resolveLookupPaths;

/**
* Hooks into `Module._resolveLookupPaths` so it can resolve unencrypted source files.
* @example
* allowUnencrypted(['node_modules']); // This will allow resolution of unencrypted code from `node_modules.asar`.
*/
export function allowUnencrypted(allowedPaths: string[]) {
function override2args(request: unknown, parent: unknown) {
// @ts-ignore
const result = originalResolveLookupPaths.call(this, request, parent);

if (!result) return result;

for (let i = 0; i < result.length; i++) {
if (allowedPaths.includes(path.basename(result[i]))) {
result.splice(i + 1, 0, `${result[i]}.asar`);
i++;
}
}

return result;
}

function override3args(
request: unknown,
parent: unknown,
newReturn: unknown
) {
const result = originalResolveLookupPaths.call(
// @ts-ignore
this,
request,
parent,
newReturn
);

const paths = newReturn ? result : result[1];

for (let i = 0; i < paths.length; i++) {
if (allowedPaths.includes(path.basename(paths[i]))) {
paths.splice(i + 1, 0, `${paths[i]}.asar`);
i++;
}
}

return result;
}

// @ts-ignore
Module._resolveLookupPaths =
originalResolveLookupPaths.length === 2 ? override2args : override3args;
}
1 change: 0 additions & 1 deletion src/encryption/hooks/index.ts

This file was deleted.

58 changes: 0 additions & 58 deletions src/encryption/hooks/node-modules.ts

This file was deleted.

3 changes: 2 additions & 1 deletion src/encryption/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { encrypt, EncryptionOptions } from './encryption';
export * from './encryption';
export * from './allowlist';

0 comments on commit 78c5584

Please sign in to comment.