mirror of
https://github.com/morluto/rea.git
synced 2026-11-22 22:45:47 +00:00
fix(apple): bound bundle Info.plist reads before decoding (#12279)
* fix(apple): bound bundle Info.plist reads before decoding * refactor(apple): report plist input limits through shared failures * fix(apple): retain bundle plist descriptors through target cleanup * test(apple): verify Hopper retries bundle plist cleanup * fix(apple): preserve typed cancellation through bundle plist cleanup * fix(apple): preserve plist read failure through late cancellation * fix(apple): preserve cancellation during plist close retry * docs(apple): document bundle plist admission policy --------- Co-authored-by: ShuaixinHuang <197200901+ShuaixinHuang@users.noreply.github.com> Co-authored-by: morluto <76467478+morluto@users.noreply.github.com>
This commit is contained in:
9 files changed
+512
-62
No files matched your search
@@ -1,11 +1,18 @@
|
||||
import { lstat, opendir, readFile, realpath, stat } from "node:fs/promises";
|
||||
import { lstat, opendir, realpath, stat } from "node:fs/promises";
|
||||
import { extname, join, sep } from "node:path";
|
||||
|
||||
import { z } from "zod";
|
||||
|
||||
import {
|
||||
AnalysisCancelledError,
|
||||
cancelledBy,
|
||||
carriedReason,
|
||||
} from "../domain/analysisErrorCore.js";
|
||||
import { analysisErrorWithCancellation } from "../domain/analysisErrorCancellation.js";
|
||||
import { isPlistDecodeBudgetFailure } from "../domain/apple/propertyListDecodeBudget.js";
|
||||
import { decodeInfoPlistValue } from "../domain/apple/infoPlist.js";
|
||||
import { BinaryTargetError } from "../domain/configurationErrors.js";
|
||||
import { decodeLimit, ParseFailureError } from "../domain/parseFailure.js";
|
||||
import { isPathWithinRoot } from "../domain/localPath.js";
|
||||
import type {
|
||||
BundleResourceRoot,
|
||||
@@ -17,6 +24,46 @@ import {
|
||||
} from "../domain/apple/appleBundleTypes.js";
|
||||
import { err, ok, type Result } from "../domain/result.js";
|
||||
import { absoluteCallerPath } from "../filesystem/callerPath.js";
|
||||
import { regularFileCancellation } from "../filesystem/OwnedRegularFile.js";
|
||||
import type { ArtifactResourceScope } from "../resources/index.js";
|
||||
import { errorMessage } from "../kernel/errorMessage.js";
|
||||
|
||||
import {
|
||||
bindRegularFileCleanup,
|
||||
readRegularFile,
|
||||
RegularFileReadFailure,
|
||||
RetainedRegularFileCleanupFailure,
|
||||
retryRegularFileCleanup,
|
||||
} from "../process/RegularFileRead.js";
|
||||
|
||||
/** Match the native plist inspector's input allowance, independent of decoding. */
|
||||
const BUNDLE_INFO_PLIST_INPUT_BYTES = 64 * 1024 * 1024;
|
||||
|
||||
const bundlePlistCancellation = (reason: unknown): AnalysisCancelledError =>
|
||||
cancelledBy("resolve_binary_target", carriedReason(reason));
|
||||
|
||||
export const readBundleInfoPlist = (
|
||||
path: string,
|
||||
signal?: AbortSignal,
|
||||
): Promise<Buffer> =>
|
||||
readRegularFile(path, {
|
||||
symlinks: "follow",
|
||||
...(signal === undefined
|
||||
? {}
|
||||
: { signal, cancellationFailure: bundlePlistCancellation }),
|
||||
admit: ({ size }) => {
|
||||
if (size > BUNDLE_INFO_PLIST_INPUT_BYTES)
|
||||
throw decodeLimit(
|
||||
"plist",
|
||||
`Info.plist exceeds the ${String(BUNDLE_INFO_PLIST_INPUT_BYTES)}-byte input allowance: ${path} (${String(size)} bytes)`,
|
||||
{
|
||||
name: "bundle Info.plist input bytes",
|
||||
maximum: BUNDLE_INFO_PLIST_INPUT_BYTES,
|
||||
required: size,
|
||||
},
|
||||
);
|
||||
},
|
||||
});
|
||||
|
||||
/** Where one app bundle layout keeps its Info.plist and program file. */
|
||||
interface BundleLayout {
|
||||
@@ -81,7 +128,11 @@ interface AppBundleFileSystem {
|
||||
readonly realpath: (path: string) => Promise<string>;
|
||||
}
|
||||
|
||||
const defaultFileSystem: AppBundleFileSystem = { readFile, realpath };
|
||||
interface AppBundleResolutionOptions {
|
||||
readonly fileSystem?: AppBundleFileSystem;
|
||||
readonly resources?: ArtifactResourceScope;
|
||||
readonly signal?: AbortSignal | undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve an app bundle directory to its declared program file. macOS and
|
||||
@@ -91,12 +142,13 @@ const defaultFileSystem: AppBundleFileSystem = { readFile, realpath };
|
||||
*/
|
||||
const resolveAppBundleExecutable = async (
|
||||
path: string,
|
||||
fileSystem: AppBundleFileSystem = defaultFileSystem,
|
||||
fileSystem: AppBundleFileSystem,
|
||||
options: AppBundleResolutionOptions,
|
||||
): Promise<Result<ResolvedAppBundle, BinaryTargetError>> => {
|
||||
try {
|
||||
const layout = await presentLayout(path);
|
||||
if (layout !== undefined)
|
||||
return await resolveLayoutExecutable(path, layout, fileSystem);
|
||||
return await resolveLayoutExecutable(path, layout, fileSystem, options);
|
||||
const wrapped = await wrappedBundle(path);
|
||||
if (wrapped !== undefined) {
|
||||
const wrappedLayout = await presentLayout(wrapped);
|
||||
@@ -105,9 +157,11 @@ const resolveAppBundleExecutable = async (
|
||||
wrapped,
|
||||
wrappedLayout,
|
||||
fileSystem,
|
||||
options,
|
||||
);
|
||||
}
|
||||
} catch (cause: unknown) {
|
||||
if (cause instanceof AnalysisCancelledError) throw cause;
|
||||
if (cause instanceof BinaryTargetError) return err(cause);
|
||||
return err(
|
||||
new BinaryTargetError(path, errorReason(cause, path), { cause }),
|
||||
@@ -136,12 +190,17 @@ const resolveAppBundleExecutable = async (
|
||||
*/
|
||||
export const resolveAppleBundleExecutable = async (
|
||||
path: string,
|
||||
fileSystem: AppBundleFileSystem = defaultFileSystem,
|
||||
options: AppBundleResolutionOptions = {},
|
||||
): Promise<Result<ResolvedAppBundle, BinaryTargetError>> => {
|
||||
const fileSystem = options.fileSystem ?? {
|
||||
readFile: (plistPath: string) =>
|
||||
readBundleInfoPlist(plistPath, options.signal),
|
||||
realpath,
|
||||
};
|
||||
if (extname(path).toLowerCase() === ".app")
|
||||
return resolveAppBundleExecutable(path, fileSystem);
|
||||
return resolveAppBundleExecutable(path, fileSystem, options);
|
||||
try {
|
||||
const layout = await declaredBundleLayout(path, fileSystem);
|
||||
const layout = await declaredBundleLayout(path, fileSystem.realpath);
|
||||
if (!layout.ok) return layout;
|
||||
if (layout.value === undefined)
|
||||
return err(
|
||||
@@ -156,8 +215,14 @@ export const resolveAppleBundleExecutable = async (
|
||||
},
|
||||
),
|
||||
);
|
||||
return await resolveLayoutExecutable(path, layout.value, fileSystem);
|
||||
return await resolveLayoutExecutable(
|
||||
path,
|
||||
layout.value,
|
||||
fileSystem,
|
||||
options,
|
||||
);
|
||||
} catch (cause: unknown) {
|
||||
if (cause instanceof AnalysisCancelledError) throw cause;
|
||||
if (cause instanceof BinaryTargetError) return err(cause);
|
||||
return err(
|
||||
new BinaryTargetError(path, errorReason(cause, path), { cause }),
|
||||
@@ -182,7 +247,7 @@ const bundleLayouts = async (
|
||||
*/
|
||||
const declaredBundleLayout = async (
|
||||
path: string,
|
||||
fileSystem: AppBundleFileSystem,
|
||||
canonicalize: AppBundleFileSystem["realpath"],
|
||||
): Promise<Result<BundleLayout | undefined, BinaryTargetError>> => {
|
||||
const layouts = await bundleLayouts(path);
|
||||
if (layouts === VERSIONED_FRAMEWORK_LAYOUTS) {
|
||||
@@ -220,10 +285,7 @@ const declaredBundleLayout = async (
|
||||
);
|
||||
const plistPath = join(path, ...layout.plist);
|
||||
if (
|
||||
!isPathWithinRoot(
|
||||
await fileSystem.realpath(path),
|
||||
await fileSystem.realpath(plistPath),
|
||||
)
|
||||
!isPathWithinRoot(await canonicalize(path), await canonicalize(plistPath))
|
||||
)
|
||||
return err(
|
||||
new BinaryTargetError(path, "bundle Info.plist leaves the bundle", {
|
||||
@@ -258,10 +320,7 @@ export const findAppBundleResourceRoot = async (
|
||||
extension !== ".app" &&
|
||||
openableAppleBundleExtension(extension) !== undefined
|
||||
) {
|
||||
const declared = await declaredBundleLayout(
|
||||
canonicalInput,
|
||||
defaultFileSystem,
|
||||
);
|
||||
const declared = await declaredBundleLayout(canonicalInput, realpath);
|
||||
if (!declared.ok) return declared;
|
||||
// An admitted bundle type without Info.plist is a native admission
|
||||
// failure, not an application directory for other routes.
|
||||
@@ -412,34 +471,124 @@ const wrappedBundle = async (bundle: string): Promise<string | undefined> => {
|
||||
return app;
|
||||
};
|
||||
|
||||
const plistReadFailure = (
|
||||
plistPath: string,
|
||||
noun: string,
|
||||
failure: unknown,
|
||||
signal?: AbortSignal,
|
||||
): BinaryTargetError => {
|
||||
const cause =
|
||||
failure instanceof RegularFileReadFailure
|
||||
? failure.operationCause
|
||||
: failure;
|
||||
const error = new BinaryTargetError(
|
||||
plistPath,
|
||||
cause instanceof ParseFailureError && cause.kind === "limit"
|
||||
? cause.message
|
||||
: isPermissionDenied(cause)
|
||||
? `permission denied reading ${noun} Info.plist ${plistPath}`
|
||||
: isAbsence(cause)
|
||||
? `${noun} Info.plist is missing: ${plistPath}`
|
||||
: `could not read ${noun} Info.plist ${plistPath}${codeDescription(cause)}`,
|
||||
{ cause },
|
||||
);
|
||||
if (signal?.aborted === true)
|
||||
analysisErrorWithCancellation(
|
||||
error,
|
||||
bundlePlistCancellation(signal.reason),
|
||||
);
|
||||
return error;
|
||||
};
|
||||
|
||||
const readLayoutPlist = async (
|
||||
path: string,
|
||||
noun: string,
|
||||
read: AppBundleFileSystem["readFile"],
|
||||
{ resources, signal }: AppBundleResolutionOptions,
|
||||
): Promise<Result<Buffer, BinaryTargetError>> => {
|
||||
try {
|
||||
return ok(await read(path));
|
||||
} catch (cause: unknown) {
|
||||
if (cause instanceof AnalysisCancelledError) throw cause;
|
||||
if (!(cause instanceof RetainedRegularFileCleanupFailure))
|
||||
return err(plistReadFailure(path, noun, cause, signal));
|
||||
|
||||
// The target resolver already tracks this operation. Retry only the owned
|
||||
// descriptor, since re-entering scope admission could wait on this read.
|
||||
const failure = cause as RetainedRegularFileCleanupFailure<Buffer>;
|
||||
const retry =
|
||||
resources === undefined
|
||||
? undefined
|
||||
: await retryRegularFileCleanup(failure, resources, signal);
|
||||
const { outcome } = failure;
|
||||
const cancellation =
|
||||
regularFileCancellation(failure) ??
|
||||
(outcome.kind === "completed" && signal?.aborted === true
|
||||
? { reason: signal.reason }
|
||||
: undefined);
|
||||
if (retry !== undefined && retry.failure === undefined) {
|
||||
if (cancellation !== undefined)
|
||||
throw bundlePlistCancellation(cancellation.reason);
|
||||
return outcome.kind === "completed"
|
||||
? ok(outcome.value)
|
||||
: err(
|
||||
plistReadFailure(
|
||||
path,
|
||||
noun,
|
||||
failure.operationFailure ?? outcome.cause,
|
||||
signal,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
const primary =
|
||||
failure.operationFailure !== undefined
|
||||
? plistReadFailure(path, noun, failure.operationFailure, signal)
|
||||
: cancellation !== undefined
|
||||
? bundlePlistCancellation(cancellation.reason)
|
||||
: outcome.kind === "failed"
|
||||
? plistReadFailure(path, noun, outcome.cause, signal)
|
||||
: undefined;
|
||||
const retained = retry?.failure ?? failure;
|
||||
const error = new BinaryTargetError(
|
||||
path,
|
||||
"The Info.plist file handle could not be closed",
|
||||
{
|
||||
cause: retained,
|
||||
cleanupPrimary: primary,
|
||||
cleanup: retry?.cleanup ?? {
|
||||
reason: errorMessage(failure.cleanupCause),
|
||||
resources: [failure.path],
|
||||
},
|
||||
},
|
||||
);
|
||||
bindRegularFileCleanup(retained, error);
|
||||
return err(error);
|
||||
}
|
||||
};
|
||||
|
||||
const resolveLayoutExecutable = async (
|
||||
bundle: string,
|
||||
layout: BundleLayout,
|
||||
fileSystem: AppBundleFileSystem,
|
||||
options: AppBundleResolutionOptions,
|
||||
): Promise<Result<ResolvedAppBundle, BinaryTargetError>> => {
|
||||
const plistPath = join(bundle, ...layout.plist);
|
||||
// Reasons for an app keep their established wording.
|
||||
const noun = extname(bundle).toLowerCase() === ".app" ? "app" : "bundle";
|
||||
let plist: Buffer;
|
||||
try {
|
||||
plist = await fileSystem.readFile(plistPath);
|
||||
} catch (cause: unknown) {
|
||||
return err(
|
||||
new BinaryTargetError(
|
||||
plistPath,
|
||||
isPermissionDenied(cause)
|
||||
? `permission denied reading ${noun} Info.plist ${plistPath}`
|
||||
: isAbsence(cause)
|
||||
? `${noun} Info.plist is missing: ${plistPath}`
|
||||
: `could not read ${noun} Info.plist ${plistPath}${codeDescription(cause)}`,
|
||||
{ cause },
|
||||
),
|
||||
);
|
||||
}
|
||||
const plist = await readLayoutPlist(
|
||||
plistPath,
|
||||
noun,
|
||||
fileSystem.readFile,
|
||||
options,
|
||||
);
|
||||
if (!plist.ok) return plist;
|
||||
|
||||
let name: string;
|
||||
try {
|
||||
name = executableName(decodeInfoPlistValue(plist, ["CFBundleExecutable"]));
|
||||
name = executableName(
|
||||
decodeInfoPlistValue(plist.value, ["CFBundleExecutable"]),
|
||||
);
|
||||
} catch (cause: unknown) {
|
||||
if (isPlistDecodeBudgetFailure(cause))
|
||||
return err(
|
||||
|
||||
Reference in new issue
Block a user