mirror of
https://github.com/morluto/rea.git
synced 2026-12-04 05:51:06 +00:00
* fix(desktop): recognize unpacked resources/app Electron applications Electron loads an application from resources/app.asar, resources/app or resources/default_app.asar. The desktop path convention recognized only the ASAR names, so VS Code and GitHub Desktop, which ship an unpacked resources/app folder, got no Electron candidate and only .NET probes for the managed helpers they bundle. The convention owner now reports the Electron application layout and its depth for both forms. inspect_desktop_application emits an electron-app-directory-convention fact for resources/app/package.json, adds it to the Electron candidate and offers analyze_javascript_application on the folder. Electron application probes are now application priority, so bundled managed or native helpers no longer lead next_probes. Release exposure reads the layout folder from the same convention, so an unpacked build names its fuse host like an ASAR build. * fix(desktop): derive Electron probes before bundled managed helpers A bundled managed helper with its own .runtimeconfig.json, such as Git Credential Manager in GitHub Desktop, is application priority too. Its probe was derived first, so the stable priority sort still put it ahead of the Electron application. Derive the Electron candidate and probes first.
546 lines
18 KiB
TypeScript
546 lines
18 KiB
TypeScript
import { desktopPathConventions } from "../domain/nativeConvention.js";
|
||
import { basename, isAbsolute } from "node:path";
|
||
import {
|
||
ArtifactReaderFailure,
|
||
artifactFileCompletion,
|
||
ArtifactResourceScope,
|
||
type ArtifactResourceOwner,
|
||
abortIfNeeded,
|
||
DirectoryArtifactReader,
|
||
DirectoryArtifactRootFailure,
|
||
DirectoryArtifactTraversalLimit,
|
||
type DirectoryArtifactEntry,
|
||
translateArtifactFailure,
|
||
} from "../resources/index.js";
|
||
import { declaredArtifactEntries } from "./ArtifactTraversalObservation.js";
|
||
import {
|
||
inspectDesktopFile,
|
||
RETAINED_FACT_BYTES,
|
||
retainedFactReferenceBytes,
|
||
retainedTextBytes,
|
||
type DesktopReadBudget,
|
||
} from "./DesktopFileInspection.js";
|
||
import type { DesktopApplicationPort } from "../ports/artifacts/DesktopApplicationPort.js";
|
||
import type { ExecutionOptions } from "../ports/AnalysisProvider.js";
|
||
import type {
|
||
DesktopApplication,
|
||
DesktopFact,
|
||
DesktopFile,
|
||
DesktopOmission,
|
||
InspectDesktopApplicationInput,
|
||
} from "../domain/desktopApplication.js";
|
||
import { projectDesktopApplication } from "../domain/desktopApplicationProjection.js";
|
||
import { digestCanonicalValue } from "../domain/canonicalDigest.js";
|
||
import { err, ok } from "../domain/result.js";
|
||
import { createDesktopApplicationEvidence } from "../domain/desktopApplicationEvidence.js";
|
||
import { ARTIFACT_GRAPH_PROVIDER } from "../domain/investigationProviders.js";
|
||
import {
|
||
AnalysisResourceConstraintError,
|
||
AnalysisTargetFailureError,
|
||
} from "../domain/analysisErrorCore.js";
|
||
import type {
|
||
AnalysisError,
|
||
AnalysisPartialObservation,
|
||
} from "../domain/analysisErrorBase.js";
|
||
import { BinaryTargetError } from "../domain/configurationErrors.js";
|
||
|
||
const RETAINED_METADATA_BYTES = 32 * 1024 * 1024;
|
||
/**
|
||
* Upper bound for one file row besides its texts, facts and ranges: the row
|
||
* and identity objects, file_id, identity strings, arrays, candidate and
|
||
* component basis IDs and short constant issues. Whole rows, paths included,
|
||
* measured 800–940 heap and 490–640 JSON bytes on real app bundles (64-bit
|
||
* Node; #8104).
|
||
*/
|
||
const RETAINED_ROW_BYTES = 1024;
|
||
const RETAINED_OMISSION_BYTES = 512;
|
||
const RETAINED_ISSUE_BYTES = 64;
|
||
|
||
/** Stop traversal without discarding the preceding bounded observations. */
|
||
class DesktopRepresentationLimit extends ArtifactReaderFailure {
|
||
constructor() {
|
||
super(
|
||
"limit",
|
||
"Directory metadata reached the 32 MiB representation budget; remaining entry and omission facts are unavailable.",
|
||
{ boundary: "memory" },
|
||
);
|
||
}
|
||
}
|
||
|
||
/** Existing artifact ownership with bounded metadata, serial probes and no payload cache. */
|
||
export class DesktopApplicationReader implements DesktopApplicationPort {
|
||
readonly #resources = new ArtifactResourceScope();
|
||
#running = false;
|
||
constructor(
|
||
private readonly directoryReaderFor: (
|
||
path: string,
|
||
) => DirectoryArtifactReader = (path) => new DirectoryArtifactReader(path),
|
||
) {}
|
||
|
||
async inspect(
|
||
input: InspectDesktopApplicationInput,
|
||
options?: ExecutionOptions,
|
||
) {
|
||
if (this.#running)
|
||
return err(
|
||
new AnalysisResourceConstraintError(
|
||
"inspect_desktop_application",
|
||
"capacity",
|
||
"This artifact reader already owns a directory inspection",
|
||
{ max_concurrent_inspections: 1 },
|
||
{
|
||
retryable: true,
|
||
// An occupied admission: the slot frees when the inspection ends.
|
||
measuredExhaustion: true,
|
||
remediationAction:
|
||
"Wait for this directory inspection to finish before submitting another.",
|
||
},
|
||
),
|
||
);
|
||
this.#running = true;
|
||
try {
|
||
return ok(
|
||
await this.#resources.run(() => this.#inspect(input, options?.signal)),
|
||
);
|
||
} catch (cause: unknown) {
|
||
const error = translateArtifactFailure(
|
||
"inspect_desktop_application",
|
||
input.path,
|
||
cause,
|
||
);
|
||
return err(error);
|
||
} finally {
|
||
this.#running = false;
|
||
}
|
||
}
|
||
|
||
close(): Promise<void> {
|
||
return this.#resources.close();
|
||
}
|
||
|
||
async #inspect(
|
||
input: InspectDesktopApplicationInput,
|
||
signal?: AbortSignal,
|
||
): Promise<DesktopApplication> {
|
||
if (!isAbsolute(input.path))
|
||
throw new ArtifactReaderFailure(
|
||
"path",
|
||
"Expected an absolute directory path",
|
||
);
|
||
const reader = this.directoryReaderFor(input.path);
|
||
const owner: ArtifactResourceOwner = {
|
||
kind: "reader",
|
||
resource: input.path,
|
||
reader,
|
||
};
|
||
let outcome:
|
||
| { kind: "completed"; value: DesktopApplication }
|
||
| { kind: "failed"; cause: unknown };
|
||
let report: DesktopApplication | undefined;
|
||
try {
|
||
const scanned = await scanDirectory(reader, input, signal);
|
||
report = scanned.report;
|
||
outcome =
|
||
scanned.failure === undefined
|
||
? { kind: "completed", value: report }
|
||
: { kind: "failed", cause: scanned.failure.cause };
|
||
} catch (cause: unknown) {
|
||
outcome = { kind: "failed", cause };
|
||
}
|
||
const closed = await this.#resources.release(owner);
|
||
const partial =
|
||
report === undefined ||
|
||
(closed.kind !== "failed" &&
|
||
outcome.kind !== "failed" &&
|
||
signal?.aborted !== true)
|
||
? undefined
|
||
: createDesktopApplicationEvidence(
|
||
input,
|
||
report,
|
||
ARTIFACT_GRAPH_PROVIDER,
|
||
);
|
||
if (closed.kind === "failed")
|
||
throw this.#resources.cleanupFailure(outcome, [closed], partial);
|
||
if (outcome.kind === "failed") {
|
||
const cause = outcome.cause;
|
||
if (cause instanceof DirectoryArtifactRootFailure)
|
||
throw selectedRootFailure(input.path, cause, partial);
|
||
if (!(cause instanceof ArtifactReaderFailure) || partial === undefined)
|
||
throw cause;
|
||
throw new ArtifactReaderFailure(
|
||
cause.reason,
|
||
cause.message,
|
||
{
|
||
cause,
|
||
cancellation: cause.cancellation,
|
||
cleanup: cause.cleanup,
|
||
cleanupLeases: cause.cleanupLeases,
|
||
partialObservation: partial,
|
||
...cause.primaryFacts(),
|
||
},
|
||
cause.details,
|
||
);
|
||
}
|
||
if (signal?.aborted)
|
||
throw new ArtifactReaderFailure(
|
||
"cancelled",
|
||
"Directory inspection cancelled during final cleanup",
|
||
{ cause: signal.reason, partialObservation: partial },
|
||
);
|
||
return outcome.value;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* The root is the caller's selection: a path that is not a directory is a
|
||
* selection failure, and host failures keep the shared
|
||
* target-unavailable/access-denied classification instead of becoming
|
||
* artifact I/O faults.
|
||
*/
|
||
const selectedRootFailure = (
|
||
path: string,
|
||
failure: DirectoryArtifactRootFailure,
|
||
partialObservation: AnalysisPartialObservation | undefined,
|
||
): AnalysisError =>
|
||
failure.reason === "path"
|
||
? new AnalysisTargetFailureError(
|
||
"inspect_desktop_application",
|
||
path,
|
||
{
|
||
kind: "selection",
|
||
format: "desktop-application",
|
||
detail: `${failure.message}. Select the application's local installation directory, such as a macOS .app bundle or the directory containing its executable.`,
|
||
},
|
||
{ cause: failure, partialObservation },
|
||
)
|
||
: new BinaryTargetError(path, failure.message, {
|
||
cause: failure.cause,
|
||
partialObservation,
|
||
});
|
||
|
||
const scanDirectory = async (
|
||
reader: DirectoryArtifactReader,
|
||
input: InspectDesktopApplicationInput,
|
||
signal?: AbortSignal,
|
||
): Promise<{
|
||
readonly report: DesktopApplication;
|
||
readonly failure?: { readonly cause: unknown };
|
||
}> => {
|
||
const admittedRoot = await reader.rootObservation();
|
||
const root = {
|
||
canonical_path: admittedRoot.canonicalPath,
|
||
identity: {
|
||
device: admittedRoot.identity.dev.toString(),
|
||
inode: admittedRoot.identity.ino.toString(),
|
||
},
|
||
};
|
||
const namespaceId = `dsr_${digestCanonicalValue(root, "Admitted desktop directory namespace")}`;
|
||
const files: DesktopFile[] = [];
|
||
const omissions: DesktopOmission[] = [];
|
||
const unityWitnessRoots = new Set<string>();
|
||
const rootName = basename(root.canonical_path);
|
||
const budget: DesktopReadBudget = {
|
||
bytes: 0,
|
||
maximum: input.max_probe_bytes,
|
||
retainedBytes:
|
||
RETAINED_ROW_BYTES +
|
||
retainedTextBytes(input.path) +
|
||
retainedTextBytes(root.canonical_path),
|
||
maximumRetainedBytes: RETAINED_METADATA_BYTES,
|
||
};
|
||
const fits = (bytes: number): boolean => {
|
||
if (bytes > budget.maximumRetainedBytes - budget.retainedBytes)
|
||
return false;
|
||
budget.retainedBytes += bytes;
|
||
return true;
|
||
};
|
||
const reserve = (bytes: number) => {
|
||
if (!fits(bytes)) throw new DesktopRepresentationLimit();
|
||
};
|
||
let entries = 0;
|
||
let probed = 0;
|
||
let symlinks = 0;
|
||
let special = 0;
|
||
let partial = false;
|
||
let failure: { readonly cause: unknown } | undefined;
|
||
const limitations = [
|
||
"Runtime candidates and component markers do not establish loaded code, execution, or build correspondence.",
|
||
"The directory SHA-256 commits only to retained paths, omission kinds, available filesystem metadata and read-range digests, not to every file's contents. Unread bytes are unknown.",
|
||
"Special-entry payloads are not read. Their paths and kinds are retained; other special-entry metadata is unavailable in this report.",
|
||
"Child symlinks are not followed. Executable section-embedded PCKs without a trailer and encrypted contents remain unexamined.",
|
||
"This is a sequence of stable entry observations, not an atomic snapshot of the entire directory.",
|
||
"Unity reference decoding stops after a witness in each Data/Managed directory; other assemblies' references are unexamined. Header facts remain per-file.",
|
||
];
|
||
try {
|
||
for await (const entry of declaredArtifactEntries(reader, signal, {
|
||
maximumEntries: input.max_entries,
|
||
onOmission: (omission) => {
|
||
if (omission.kind !== "directory-descendants") {
|
||
special++;
|
||
entries++;
|
||
}
|
||
partial = true;
|
||
reserve(RETAINED_OMISSION_BYTES + retainedTextBytes(omission.path));
|
||
const semantic = {
|
||
root_namespace_id: namespaceId,
|
||
path: omission.path,
|
||
kind: omission.kind,
|
||
};
|
||
omissions.push({
|
||
omission_id: `dso_${digestCanonicalValue(semantic, "Desktop traversal omission")}`,
|
||
path: omission.path,
|
||
kind: omission.kind,
|
||
});
|
||
},
|
||
onCoverage: (coverage) => {
|
||
if (coverage.examinedEntries !== undefined)
|
||
entries = coverage.examinedEntries;
|
||
if (coverage.status !== "complete") partial = true;
|
||
limitations.push(...coverage.limitations);
|
||
},
|
||
})) {
|
||
entries++;
|
||
abortIfNeeded(signal);
|
||
const conventions =
|
||
entry.kind === "file"
|
||
? pathConventions(entry.path, rootName)
|
||
: undefined;
|
||
// Reserve the row and every repeated path before retaining them: each
|
||
// convention fact copies at most the path and its probes copy the
|
||
// local path.
|
||
reserve(
|
||
RETAINED_ROW_BYTES +
|
||
retainedTextBytes(entry.path) +
|
||
retainedTextBytes(entry.adapterKey) +
|
||
(conventions?.facts.length ?? 0) *
|
||
(RETAINED_FACT_BYTES +
|
||
retainedTextBytes(entry.path) +
|
||
retainedFactReferenceBytes(entry.adapterKey)),
|
||
);
|
||
const row = fileRow(entry, namespaceId);
|
||
files.push(row);
|
||
if (entry.kind === "symlink") {
|
||
symlinks++;
|
||
continue;
|
||
}
|
||
if (conventions === undefined) continue;
|
||
row.facts.push(...conventions.facts);
|
||
const managedRoot = conventions.unityManagedDirectory;
|
||
if (!probeSelected(entry)) continue;
|
||
if (budget.bytes >= budget.maximum) {
|
||
row.read_status = "unavailable";
|
||
row.issues.push(
|
||
"Probe byte budget exhausted; bytes were not inspected.",
|
||
);
|
||
partial = true;
|
||
continue;
|
||
}
|
||
probed++;
|
||
try {
|
||
await reader.inspectFile(
|
||
entry,
|
||
(file) =>
|
||
inspectDesktopFile(
|
||
file,
|
||
row,
|
||
budget,
|
||
signal,
|
||
managedRoot !== null && !unityWitnessRoots.has(managedRoot)
|
||
? managedRoot
|
||
: null,
|
||
),
|
||
signal,
|
||
);
|
||
row.read_status = row.issues.length === 0 ? "examined" : "partial";
|
||
if (
|
||
managedRoot !== null &&
|
||
row.facts.some((fact) => fact.kind === "unity-assembly-reference")
|
||
)
|
||
unityWitnessRoots.add(managedRoot);
|
||
if (row.issues.length > 0) partial = true;
|
||
} catch (cause: unknown) {
|
||
if (!(cause instanceof ArtifactReaderFailure)) throw cause;
|
||
const probeIssueCount = row.issues.length;
|
||
row.read_status = "unavailable";
|
||
// Host failures name the absolute path; charge that copy too.
|
||
const issueRetained = fits(
|
||
RETAINED_ISSUE_BYTES + retainedTextBytes(cause.message),
|
||
);
|
||
if (issueRetained) row.issues.push(cause.message);
|
||
partial = true;
|
||
const completedRead = artifactFileCompletion(cause) !== undefined;
|
||
if (completedRead)
|
||
row.read_status = probeIssueCount === 0 ? "examined" : "partial";
|
||
if (
|
||
!completedRead &&
|
||
(cause.reason === "integrity" ||
|
||
cause.reason === "io" ||
|
||
cause.reason === "cancelled")
|
||
) {
|
||
row.facts = row.facts.filter((fact) => fact.offset === null);
|
||
row.ranges = [];
|
||
}
|
||
if (cause.cleanup !== undefined || cause.reason === "cancelled")
|
||
throw cause;
|
||
if (!issueRetained) throw new DesktopRepresentationLimit();
|
||
}
|
||
}
|
||
} catch (cause: unknown) {
|
||
partial = true;
|
||
if (cause instanceof DirectoryArtifactTraversalLimit) {
|
||
entries = cause.examinedEntries;
|
||
limitations.push(
|
||
"Directory traversal reached max_entries; unvisited paths and candidate families remain unknown.",
|
||
);
|
||
} else if (cause instanceof DesktopRepresentationLimit) {
|
||
limitations.push(cause.message);
|
||
} else {
|
||
failure = { cause };
|
||
limitations.push(
|
||
"Inspection was interrupted; only preceding stable entry observations are retained.",
|
||
);
|
||
}
|
||
}
|
||
files.sort((a, b) => (a.path < b.path ? -1 : a.path > b.path ? 1 : 0));
|
||
omissions.sort((a, b) =>
|
||
a.path < b.path
|
||
? -1
|
||
: a.path > b.path
|
||
? 1
|
||
: a.kind < b.kind
|
||
? -1
|
||
: a.kind > b.kind
|
||
? 1
|
||
: 0,
|
||
);
|
||
const coverage: DesktopApplication["coverage"] = {
|
||
status: partial ? "partial" : "complete-within-scan",
|
||
entries_examined: entries,
|
||
files_probed: probed,
|
||
bytes_read: budget.bytes,
|
||
symlinks_not_followed: symlinks,
|
||
special_entries_omitted: special,
|
||
limits: {
|
||
max_entries: input.max_entries,
|
||
max_probe_bytes: input.max_probe_bytes,
|
||
},
|
||
};
|
||
// Compose identities already owned by admitted root/entry observations and
|
||
// retained reads. Caller policy, interpretations and cleanup diagnostics stay
|
||
// in Evidence without renaming the same examined source.
|
||
const snapshot = digestCanonicalValue(
|
||
{
|
||
root_namespace_id: namespaceId,
|
||
files: files.map(({ file_id, bytes, ranges }) => ({
|
||
file_id,
|
||
bytes,
|
||
ranges,
|
||
})),
|
||
omissions: omissions.map(({ omission_id }) => omission_id),
|
||
},
|
||
"Examined desktop directory facts",
|
||
);
|
||
const report: DesktopApplication = {
|
||
root: {
|
||
path: input.path,
|
||
...root,
|
||
namespace_id: namespaceId,
|
||
snapshot_sha256: snapshot,
|
||
identity_scope: "examined-metadata-and-read-ranges",
|
||
},
|
||
files,
|
||
omissions,
|
||
...projectDesktopApplication(files),
|
||
coverage,
|
||
limitations,
|
||
};
|
||
return { report, ...(failure === undefined ? {} : { failure }) };
|
||
};
|
||
|
||
const fileRow = (
|
||
entry: DirectoryArtifactEntry,
|
||
rootNamespaceId: string,
|
||
): DesktopFile => {
|
||
const source = entry.sourceIdentity;
|
||
const identity =
|
||
source === undefined
|
||
? null
|
||
: {
|
||
device: source.device.toString(),
|
||
inode: source.inode.toString(),
|
||
size: source.size.toString(),
|
||
mode: source.mode.toString(),
|
||
mtime_ns: source.mtimeNs.toString(),
|
||
ctime_ns: source.ctimeNs.toString(),
|
||
};
|
||
const semantic = {
|
||
root_namespace_id: rootNamespaceId,
|
||
path: entry.path,
|
||
kind: entry.kind,
|
||
identity,
|
||
};
|
||
return {
|
||
file_id: `dsf_${digestCanonicalValue(semantic, "Desktop directory entry")}`,
|
||
path: entry.path,
|
||
local_path: entry.adapterKey,
|
||
kind: entry.kind,
|
||
bytes: entry.declaredSize,
|
||
identity,
|
||
read_status: "not-selected",
|
||
ranges: [],
|
||
facts: [],
|
||
issues: [],
|
||
};
|
||
};
|
||
|
||
/** Path-convention facts of one file; each value repeats at most the path. */
|
||
const pathConventions = (path: string, rootName: string) => {
|
||
const conventions = desktopPathConventions(path, rootName);
|
||
const facts: DesktopFact[] = [];
|
||
if (
|
||
conventions.unityIl2cppBackupAssembly &&
|
||
conventions.unityIl2cppBackupDirectory !== null
|
||
)
|
||
facts.push({
|
||
kind: "unity-il2cpp-backup-convention",
|
||
offset: null,
|
||
value: { backup_directory: conventions.unityIl2cppBackupDirectory },
|
||
});
|
||
if (conventions.electronApplication !== null)
|
||
facts.push({
|
||
kind:
|
||
conventions.electronApplication.layout === "asar"
|
||
? "electron-asar-convention"
|
||
: "electron-app-directory-convention",
|
||
offset: null,
|
||
value: path,
|
||
});
|
||
if (conventions.unrealContainer)
|
||
facts.push({
|
||
kind: "unreal-container-convention",
|
||
offset: null,
|
||
value: path,
|
||
});
|
||
if (conventions.il2cppLibrary)
|
||
facts.push({
|
||
kind: "il2cpp-library-convention",
|
||
offset: null,
|
||
value: path,
|
||
});
|
||
return {
|
||
facts,
|
||
unityManagedDirectory: conventions.unityManagedDirectory,
|
||
};
|
||
};
|
||
|
||
/**
|
||
* Files whose headers are read. Besides host executable marking, which
|
||
* Windows cannot apply to ELF or Mach-O files, a bundle's `Contents/MacOS/`
|
||
* entries and extensionless files at the root name conventional Unix mains.
|
||
*/
|
||
const probeSelected = (entry: DirectoryArtifactEntry): boolean =>
|
||
entry.executable ||
|
||
/(?:\.(?:exe|dll|so|dylib|pck|utoc|bundle|ab|unity3d)|(?:^|\/)(?:global-metadata\.dat|data\.win|game\.(?:unx|ios))|(?:^|\/)Contents\/MacOS\/[^/]+|^[^/.]+)$/iu.test(
|
||
entry.path,
|
||
);
|