Skip to content

Commit

Permalink
fix: ensure app load is limited to real asar files when appropriate
Browse files Browse the repository at this point in the history
Co-authored-by: Samuel Attard <marshallofsound@electronjs.org>
  • Loading branch information
trop[bot] and MarshallOfSound committed Sep 11, 2023
1 parent eda2ced commit 6476bcf
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/asar/fs-wrapper.ts
Expand Up @@ -27,7 +27,7 @@ const cachedArchives = new Map<string, NodeJS.AsarArchive>();
const getOrCreateArchive = (archivePath: string) => {
const isCached = cachedArchives.has(archivePath);
if (isCached) {
return cachedArchives.get(archivePath);
return cachedArchives.get(archivePath)!;
}

try {
Expand All @@ -39,6 +39,8 @@ const getOrCreateArchive = (archivePath: string) => {
}
};

process._getOrCreateArchive = getOrCreateArchive;

const asarRe = /\.asar/i;

// Separate asar package's path from full path.
Expand Down
9 changes: 9 additions & 0 deletions lib/browser/init.ts
Expand Up @@ -84,11 +84,20 @@ const v8Util = process._linkedBinding('electron_common_v8_util');
let packagePath = null;
let packageJson = null;
const searchPaths: string[] = v8Util.getHiddenValue(global, 'appSearchPaths');
const searchPathsOnlyLoadASAR: boolean = v8Util.getHiddenValue(global, 'appSearchPathsOnlyLoadASAR');
// Borrow the _getOrCreateArchive asar helper
const getOrCreateArchive = process._getOrCreateArchive;
delete process._getOrCreateArchive;

if (process.resourcesPath) {
for (packagePath of searchPaths) {
try {
packagePath = path.join(process.resourcesPath, packagePath);
if (searchPathsOnlyLoadASAR) {
if (!getOrCreateArchive?.(packagePath)) {
continue;
}
}
packageJson = Module._load(path.join(packagePath, 'package.json'));
break;
} catch {
Expand Down
7 changes: 7 additions & 0 deletions shell/common/node_bindings.cc
Expand Up @@ -512,6 +512,13 @@ node::Environment* NodeBindings::CreateEnvironment(
electron::fuses::IsOnlyLoadAppFromAsarEnabled()
? app_asar_search_paths
: search_paths));
context->Global()->SetPrivate(
context,
v8::Private::ForApi(
isolate, gin::ConvertToV8(isolate, "appSearchPathsOnlyLoadASAR")
.As<v8::String>()),
gin::ConvertToV8(isolate,
electron::fuses::IsOnlyLoadAppFromAsarEnabled()));
}

base::FilePath resources_path = GetResourcesPath();
Expand Down
1 change: 1 addition & 0 deletions typings/internal-ambient.d.ts
Expand Up @@ -247,6 +247,7 @@ declare namespace NodeJS {
// Additional properties
_firstFileName?: string;
_serviceStartupScript: string;
_getOrCreateArchive?: (path: string) => NodeJS.AsarArchive | null;

helperExecPath: string;
mainModule?: NodeJS.Module | undefined;
Expand Down

0 comments on commit 6476bcf

Please sign in to comment.