Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3be45116ab | ||
|
|
788964d52c | ||
|
|
acca91d22b | ||
|
|
0323ce0a28 | ||
|
|
089ef732e8 | ||
|
|
f09f454f45 | ||
|
|
70cd4cbfb8 | ||
|
|
b7dcf782aa | ||
|
|
4135c911d3 | ||
|
|
0781716788 | ||
|
|
847b7328cf | ||
|
|
baad9eebe4 | ||
|
|
4692bb3330 | ||
|
|
af6a998066 | ||
|
|
7a66470385 | ||
|
|
90c6922905 | ||
|
|
3ded799bc7 | ||
|
|
d69acc3455 | ||
|
|
7d1e4d0e2b | ||
|
|
949228b606 | ||
|
|
5e8f7c536e | ||
|
|
18591f78ae | ||
|
|
44b197f924 | ||
|
|
610f873e91 | ||
|
|
59f528f0e5 | ||
|
|
c553d1138e | ||
|
|
930dd5357f | ||
|
|
084e540414 | ||
|
|
db689f3c3c | ||
|
|
f15ddbea7a | ||
|
|
68081ac414 | ||
|
|
bddeae5108 | ||
|
|
f78d137b42 | ||
|
|
1a3a96d36b | ||
|
|
c9764084b8 | ||
|
|
0f966e2b44 | ||
|
|
1851f1fcfd | ||
|
|
2b2a5d0f26 | ||
|
|
2a9252e4e6 | ||
|
|
7666a482b6 |
No files matched your search
+8
-1
@@ -314,7 +314,9 @@ diagnostics appear inside the report when `--full-output` is selected. Log
|
||||
truncation alone does not change the diagnostic health or exit status.
|
||||
|
||||
The default terminal format is TOON. Use `--json` when saving results for a
|
||||
JSON consumer. `--json` is indented on a terminal and compact when piped or
|
||||
JSON consumer. Valued output flags (`--format`, `--filter-output`,
|
||||
`--token-limit`, `--token-offset`) accept `--flag value` or `--flag=value`; a
|
||||
missing value fails with `MISSING_OUTPUT_VALUE` before the command runs. `--json` is indented on a terminal and compact when piped or
|
||||
redirected, which keeps large results smaller for agents and scripts; pipe
|
||||
through `jq .` for indented files. Output selection and formatting do not change
|
||||
operation status.
|
||||
@@ -352,6 +354,11 @@ command, objdump, Rizin and GDB. In these paths, a returned structured
|
||||
signal-derived status. This describes the current source routing; it is not a
|
||||
claim that every command or provider runtime has been independently exercised.
|
||||
|
||||
When the reader of piped output closes early, as `rea ... | head` does, the
|
||||
CLI stops writing, prints no error and exits `141` (`128 + 13`, the status of a
|
||||
SIGPIPE-terminated writer). The operation itself may have completed; only its
|
||||
output was declined.
|
||||
|
||||
`inspect-artifact` also returns a structured cancelled result, but its direct
|
||||
analysis cancellation path does not set a signal-derived exit status, so it
|
||||
exits `1`. Setup has its own contract: cancellation exits `0`. Treat the
|
||||
|
||||
@@ -541,6 +541,37 @@ search permissions (`0700`) on a directory you own before retrying.
|
||||
|
||||
On supported Linux distributions, approved setup verifies Hopper's official
|
||||
`.deb`, `.rpm`, or Arch package before invoking the native package manager.
|
||||
The official release feed selects the current public demo without a version
|
||||
pin. REA verifies the package's detached PGP signature against Hopper's stable
|
||||
primary fingerprint `7C2D73BAFAF5B9A710A69DA1A26DF815099B1BCD`, then checks
|
||||
that the signed package and installed launcher agree with `main_exe_hash`.
|
||||
Rotating signing subkeys remain bound to that primary identity.
|
||||
The feed row itself is not PGP-signed; doctor labels its display version as
|
||||
`reported_release_version`, separately from the authenticated launcher identity.
|
||||
|
||||
The approval plan includes the caller-selected cache location. Approved setup
|
||||
retains the signed archive, public key and release metadata under
|
||||
`${XDG_CACHE_HOME:-$HOME/.cache}/rea/hopper-releases/<launcher-sha256>`.
|
||||
An empty `XDG_CACHE_HOME` uses `$HOME/.cache`. Approved setup can repair a
|
||||
damaged caller-owned entry after authenticating a fresh package; the previous
|
||||
entry is preserved beside it as `<launcher-sha256>.backup-<id>`. Ordinary doctor
|
||||
and launches reject damaged evidence and never repair it implicitly.
|
||||
Approved publications of the same canonical cache entry share a process-backed
|
||||
lease. Concurrent setup waits without moving another writer's evidence; a
|
||||
failed replacement restores the previous directory when possible and reports
|
||||
any retained backup path. Unavailable reads or native inspection tools do not
|
||||
authorize replacing existing evidence.
|
||||
Doctor and launches authenticate this evidence for offline use; an editable
|
||||
digest receipt alone cannot authorize a launcher. Verification reuses at most
|
||||
eight immutable package proofs per process, keyed by the complete package,
|
||||
key, signature, package format and expected launcher digest. Cached file reads
|
||||
still check identity and content. Without retained evidence, inspecting an
|
||||
existing install requires the matching current official package and network
|
||||
access. Successful live inspection can be reused within that process after
|
||||
rehashing the launcher; it does not write a persistent cache or claim that a
|
||||
previously observed release is still the latest. Older uncached builds require
|
||||
an approved update while the feed exposes only its current release.
|
||||
|
||||
REA runs the supported demo build on a private Xvfb display and selects Hopper's
|
||||
offered demo mode for each analysis session; it does not require the user's
|
||||
desktop display. Unattended package-manager access requires
|
||||
|
||||
@@ -206,6 +206,9 @@ code unit, matching the original bytes identified by the artifact digest.
|
||||
|
||||
Plain `.ts` artifact sources use TypeScript syntax without JSX, including
|
||||
angle-bracket type assertions. `.tsx` and `.jsx` retain JSX parsing.
|
||||
JavaScript sources use JavaScript grammar with JSX, never TypeScript syntax,
|
||||
so valid JavaScript that TypeScript reads differently keeps its JavaScript
|
||||
meaning.
|
||||
|
||||
Relative TypeScript imports can name their emitted `.js`, `.mjs`, or `.cjs`
|
||||
counterparts while resolving to inventoried source files. This substitution
|
||||
@@ -220,7 +223,10 @@ Selected bounded text is then parsed as inert data to recover:
|
||||
- Webpack and Rspack chunk registrations and module factories represented as
|
||||
AST literals;
|
||||
- static imports, dynamic imports, CommonJS `require` calls, workers, and
|
||||
service workers;
|
||||
service workers (only `new Worker(new URL(path, import.meta.url))` resolves
|
||||
relative to the calling module; string worker and service-worker paths use
|
||||
the document base URL or Node's working directory, which static analysis
|
||||
does not establish, so they keep their expression and stay unresolved);
|
||||
- route, network endpoint, storage, and vendor-marker observations;
|
||||
- local source-map declarations and original
|
||||
source names and content digests;
|
||||
@@ -626,6 +632,10 @@ href are read without surrounding whitespace or embedded tabs and newlines, and
|
||||
their percent-encoded path bytes are decoded; encoded dot and separator bytes
|
||||
are still rejected. CommonJS module lookups retain extension and directory
|
||||
resolution.
|
||||
ESM, HTML, and URL references apply URL dot segments to empty path segments:
|
||||
`./assets//../app.js` selects `assets/app.js`, while a remaining empty segment
|
||||
such as `./assets//app.js` stays unavailable. CommonJS keeps filesystem path
|
||||
semantics and selects `app.js` for the same text.
|
||||
Unresolved HTML references retain their declaration, source range, and resolution
|
||||
reason in the renderer observations.
|
||||
HTML script source ranges follow the HTML parser across LF, CRLF, and bare CR
|
||||
|
||||
@@ -378,7 +378,12 @@ complete observation.
|
||||
property: `N` means `nonatomic`; a complete typed attribute string without
|
||||
`N` means the Objective-C default `atomic`;
|
||||
missing or incomplete attribute metadata leaves `atomicity` and any
|
||||
unestablished read-only value null. `is_meta_class`, `is_root_class`, and
|
||||
unestablished read-only value null. A type attribute whose quoted class name
|
||||
never closes is incomplete and marks `objc_properties_categories` partial
|
||||
with `property_attributes_incomplete`. A property record that cannot be
|
||||
decoded, such as one with an empty or unmapped name, is reported as
|
||||
`property_decode_failed` at its entry address while sibling properties are
|
||||
retained. `is_meta_class`, `is_root_class`, and
|
||||
`ivar_count` are also nullable when the selected provider exposes only symbol
|
||||
names. Class implementation methods have null `is_required` and `is_optional`
|
||||
fields because those facts apply to protocol declarations; decoded protocol
|
||||
@@ -570,6 +575,12 @@ Exact and ambiguous value counts describe emitted root/object value instances;
|
||||
a root and an object may contain the same value. Archive digests identify the
|
||||
original bytes.
|
||||
|
||||
Dates are reported as `{ "$plist_type": "date", "iso8601": "<timestamp>" }`. A
|
||||
date the decoder cannot represent, such as an XML date with a year beyond 9999
|
||||
or a negative year, is reported as `{ "$plist_type": "date", "iso8601": null }`
|
||||
with a counted limitation; the rest of the plist or archive is still decoded.
|
||||
`inspect_plist` and Interface Builder decoding use the same projection.
|
||||
|
||||
## Locate a byte signature
|
||||
|
||||
Use `search_bytes` after opening a target with Ghidra to locate a known constant
|
||||
|
||||
@@ -2,7 +2,7 @@ REA npm dependency inventory
|
||||
Prepared from the root package-lock.json; refresh this inventory when locked dependencies change.
|
||||
License identifiers are upstream lockfile declarations, not a legal assessment.
|
||||
Runtime includes transitive and optional platform packages; development includes build, test and website tooling.
|
||||
Locked package entries: 721; runtime: 177.
|
||||
Locked package entries: 722; runtime: 178.
|
||||
Full runtime copyright/license/NOTICE texts: npm-runtime-notices.txt.
|
||||
|
||||
@algolia/abtesting@1.25.0 | development | MIT
|
||||
@@ -2740,6 +2740,11 @@ oniguruma-to-es@3.1.1 | development | MIT
|
||||
Package: https://www.npmjs.com/package/oniguruma-to-es/v/3.1.1
|
||||
Archive: https://registry.npmjs.org/oniguruma-to-es/-/oniguruma-to-es-3.1.1.tgz
|
||||
Integrity: sha512-bUH8SDvPkH3ho3dvwJwfonjlQ4R80vjyvrU8YpxuROddv55vAEJrTuCuCVUhhsHbtlD9tGGbaNApGQckXhS8iQ==
|
||||
openpgp@6.3.2 | runtime | LGPL-3.0+
|
||||
Lockfile path: node_modules/openpgp
|
||||
Package: https://www.npmjs.com/package/openpgp/v/6.3.2
|
||||
Archive: https://registry.npmjs.org/openpgp/-/openpgp-6.3.2.tgz
|
||||
Integrity: sha512-wcZTzHz41LV8Y48zH/JlD1JT8YdNmpWOuMAjQ/podvvCwsjBEU37K3znJh4UQcj3hksE5z9TzvhbUbwzsGvlLQ==
|
||||
oxc-parser@0.150.0 | development | MIT
|
||||
Lockfile path: node_modules/oxc-parser
|
||||
Package: https://www.npmjs.com/package/oxc-parser/v/0.150.0
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
REA third-party npm runtime copyright, license and NOTICE texts
|
||||
Collected from integrity-verified locked npm archives. Release-specific supplements and upstream notice gaps are identified in their entries.
|
||||
External engines are separately obtained; see THIRD_PARTY_NOTICES.md.
|
||||
Runtime dependency fingerprint (SHA-256): 49b181923ab240c68c64d35ecdd8f93cb5cd516c765f2282d0e0eca0bcadd13e
|
||||
Runtime dependency fingerprint (SHA-256): 131100b2e1e6ad5e55b10a0baf4be2d7a0838fd724fb307b9bafd73446b0d3ec
|
||||
|
||||
===== @babel/helper-string-parser@8.0.6 =====
|
||||
Declared license: MIT
|
||||
@@ -5171,6 +5171,180 @@ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
|
||||
IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
|
||||
===== openpgp@6.3.2 =====
|
||||
Declared license: LGPL-3.0+
|
||||
Archive: https://registry.npmjs.org/openpgp/-/openpgp-6.3.2.tgz
|
||||
Integrity: sha512-wcZTzHz41LV8Y48zH/JlD1JT8YdNmpWOuMAjQ/podvvCwsjBEU37K3znJh4UQcj3hksE5z9TzvhbUbwzsGvlLQ==
|
||||
Scope: npm runtime dependency. GPLv3 terms incorporated by this LGPL text are preserved in licenses/GPL-3.0.txt.
|
||||
|
||||
--- package/LICENSE ---
|
||||
|
||||
GNU LESSER GENERAL PUBLIC LICENSE
|
||||
Version 3, 29 June 2007
|
||||
|
||||
Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
|
||||
Everyone is permitted to copy and distribute verbatim copies
|
||||
of this license document, but changing it is not allowed.
|
||||
|
||||
|
||||
This version of the GNU Lesser General Public License incorporates
|
||||
the terms and conditions of version 3 of the GNU General Public
|
||||
License, supplemented by the additional permissions listed below.
|
||||
|
||||
0. Additional Definitions.
|
||||
|
||||
As used herein, "this License" refers to version 3 of the GNU Lesser
|
||||
General Public License, and the "GNU GPL" refers to version 3 of the GNU
|
||||
General Public License.
|
||||
|
||||
"The Library" refers to a covered work governed by this License,
|
||||
other than an Application or a Combined Work as defined below.
|
||||
|
||||
An "Application" is any work that makes use of an interface provided
|
||||
by the Library, but which is not otherwise based on the Library.
|
||||
Defining a subclass of a class defined by the Library is deemed a mode
|
||||
of using an interface provided by the Library.
|
||||
|
||||
A "Combined Work" is a work produced by combining or linking an
|
||||
Application with the Library. The particular version of the Library
|
||||
with which the Combined Work was made is also called the "Linked
|
||||
Version".
|
||||
|
||||
The "Minimal Corresponding Source" for a Combined Work means the
|
||||
Corresponding Source for the Combined Work, excluding any source code
|
||||
for portions of the Combined Work that, considered in isolation, are
|
||||
based on the Application, and not on the Linked Version.
|
||||
|
||||
The "Corresponding Application Code" for a Combined Work means the
|
||||
object code and/or source code for the Application, including any data
|
||||
and utility programs needed for reproducing the Combined Work from the
|
||||
Application, but excluding the System Libraries of the Combined Work.
|
||||
|
||||
1. Exception to Section 3 of the GNU GPL.
|
||||
|
||||
You may convey a covered work under sections 3 and 4 of this License
|
||||
without being bound by section 3 of the GNU GPL.
|
||||
|
||||
2. Conveying Modified Versions.
|
||||
|
||||
If you modify a copy of the Library, and, in your modifications, a
|
||||
facility refers to a function or data to be supplied by an Application
|
||||
that uses the facility (other than as an argument passed when the
|
||||
facility is invoked), then you may convey a copy of the modified
|
||||
version:
|
||||
|
||||
a) under this License, provided that you make a good faith effort to
|
||||
ensure that, in the event an Application does not supply the
|
||||
function or data, the facility still operates, and performs
|
||||
whatever part of its purpose remains meaningful, or
|
||||
|
||||
b) under the GNU GPL, with none of the additional permissions of
|
||||
this License applicable to that copy.
|
||||
|
||||
3. Object Code Incorporating Material from Library Header Files.
|
||||
|
||||
The object code form of an Application may incorporate material from
|
||||
a header file that is part of the Library. You may convey such object
|
||||
code under terms of your choice, provided that, if the incorporated
|
||||
material is not limited to numerical parameters, data structure
|
||||
layouts and accessors, or small macros, inline functions and templates
|
||||
(ten or fewer lines in length), you do both of the following:
|
||||
|
||||
a) Give prominent notice with each copy of the object code that the
|
||||
Library is used in it and that the Library and its use are
|
||||
covered by this License.
|
||||
|
||||
b) Accompany the object code with a copy of the GNU GPL and this license
|
||||
document.
|
||||
|
||||
4. Combined Works.
|
||||
|
||||
You may convey a Combined Work under terms of your choice that,
|
||||
taken together, effectively do not restrict modification of the
|
||||
portions of the Library contained in the Combined Work and reverse
|
||||
engineering for debugging such modifications, if you also do each of
|
||||
the following:
|
||||
|
||||
a) Give prominent notice with each copy of the Combined Work that
|
||||
the Library is used in it and that the Library and its use are
|
||||
covered by this License.
|
||||
|
||||
b) Accompany the Combined Work with a copy of the GNU GPL and this license
|
||||
document.
|
||||
|
||||
c) For a Combined Work that displays copyright notices during
|
||||
execution, include the copyright notice for the Library among
|
||||
these notices, as well as a reference directing the user to the
|
||||
copies of the GNU GPL and this license document.
|
||||
|
||||
d) Do one of the following:
|
||||
|
||||
0) Convey the Minimal Corresponding Source under the terms of this
|
||||
License, and the Corresponding Application Code in a form
|
||||
suitable for, and under terms that permit, the user to
|
||||
recombine or relink the Application with a modified version of
|
||||
the Linked Version to produce a modified Combined Work, in the
|
||||
manner specified by section 6 of the GNU GPL for conveying
|
||||
Corresponding Source.
|
||||
|
||||
1) Use a suitable shared library mechanism for linking with the
|
||||
Library. A suitable mechanism is one that (a) uses at run time
|
||||
a copy of the Library already present on the user's computer
|
||||
system, and (b) will operate properly with a modified version
|
||||
of the Library that is interface-compatible with the Linked
|
||||
Version.
|
||||
|
||||
e) Provide Installation Information, but only if you would otherwise
|
||||
be required to provide such information under section 6 of the
|
||||
GNU GPL, and only to the extent that such information is
|
||||
necessary to install and execute a modified version of the
|
||||
Combined Work produced by recombining or relinking the
|
||||
Application with a modified version of the Linked Version. (If
|
||||
you use option 4d0, the Installation Information must accompany
|
||||
the Minimal Corresponding Source and Corresponding Application
|
||||
Code. If you use option 4d1, you must provide the Installation
|
||||
Information in the manner specified by section 6 of the GNU GPL
|
||||
for conveying Corresponding Source.)
|
||||
|
||||
5. Combined Libraries.
|
||||
|
||||
You may place library facilities that are a work based on the
|
||||
Library side by side in a single library together with other library
|
||||
facilities that are not Applications and are not covered by this
|
||||
License, and convey such a combined library under terms of your
|
||||
choice, if you do both of the following:
|
||||
|
||||
a) Accompany the combined library with a copy of the same work based
|
||||
on the Library, uncombined with any other library facilities,
|
||||
conveyed under the terms of this License.
|
||||
|
||||
b) Give prominent notice with the combined library that part of it
|
||||
is a work based on the Library, and explaining where to find the
|
||||
accompanying uncombined form of the same work.
|
||||
|
||||
6. Revised Versions of the GNU Lesser General Public License.
|
||||
|
||||
The Free Software Foundation may publish revised and/or new versions
|
||||
of the GNU Lesser General Public License from time to time. Such new
|
||||
versions will be similar in spirit to the present version, but may
|
||||
differ in detail to address new problems or concerns.
|
||||
|
||||
Each version is given a distinguishing version number. If the
|
||||
Library as you received it specifies that a certain numbered version
|
||||
of the GNU Lesser General Public License "or any later version"
|
||||
applies to it, you have the option of following the terms and
|
||||
conditions either of that published version or of any later version
|
||||
published by the Free Software Foundation. If the Library as you
|
||||
received it does not specify a version number of the GNU Lesser
|
||||
General Public License, you may choose any version of the GNU Lesser
|
||||
General Public License ever published by the Free Software Foundation.
|
||||
|
||||
If the Library as you received it specifies that a proxy can decide
|
||||
whether future versions of the GNU Lesser General Public License shall
|
||||
apply, that proxy's public statement of acceptance of any version is
|
||||
permanent authorization for you to choose that version for the
|
||||
Library.
|
||||
|
||||
===== pako@1.0.11 =====
|
||||
Declared license: (MIT AND Zlib)
|
||||
Archive: https://registry.npmjs.org/pako/-/pako-1.0.11.tgz
|
||||
|
||||
@@ -111,7 +111,9 @@ automatically. Alternatively, build it using the Linux/MinGW lane above.
|
||||
- Cleanup walks locked directory objects, removes files by handle, and unlinks
|
||||
reparse entries without traversing their targets. Failed removal retains the
|
||||
root and ancestor leases for retry; a partially removed runtime accepts only
|
||||
cleanup, and its registry entry remains until removal succeeds. Normal close, cancellation,
|
||||
cleanup, and its registry entry remains until removal succeeds. The owned root
|
||||
handle includes attribute-write access so cleanup can clear a read-only marker,
|
||||
matching the child cleanup handles. Normal close, cancellation,
|
||||
startup failure, and timeout release provider resources. Abrupt owner death
|
||||
kills the owned job. The Android provider additionally uses a detached helper
|
||||
that owns its private runtime directory and cleans it when the owner pipe
|
||||
@@ -156,8 +158,11 @@ npm run verify:windows-native
|
||||
npm run verify:ghidra:windows
|
||||
```
|
||||
|
||||
The native inspection probe retains its directory owner if deletion fails. A
|
||||
later availability query retries that cleanup before creating another probe;
|
||||
An unpublished runtime retains its directory and parent handles if validation,
|
||||
result publication, or rollback fails. Errors retain the original failure and
|
||||
any rollback failure. Later runtime creation or native inspection retries that
|
||||
exact owner before allocating another directory. The same owner covers the
|
||||
native inspection probe. A later availability query retries failed probe cleanup;
|
||||
successful admission and static artifact failures remain cached. Environment
|
||||
teardown makes one final cleanup attempt and reports a persistent failure to
|
||||
stderr rather than claiming the directory was removed.
|
||||
@@ -174,9 +179,11 @@ On Windows, after compiling the runtime:
|
||||
node scripts/verify-windows-native-inspection.mjs
|
||||
```
|
||||
|
||||
This artifact injects one access-denied disposition result while using a real
|
||||
NTFS directory. The verifier checks retained ownership, recovery on a later
|
||||
query, cached successful admission, and cleanup on environment teardown. It
|
||||
This artifact injects an access-denied disposition result while using real
|
||||
NTFS directories, and separately denies post-create validation and rollback.
|
||||
The verifier checks retained ownership after inspection and direct runtime
|
||||
creation failures, recovery on later admission, cached successful inspection,
|
||||
and cleanup on environment teardown. It
|
||||
renames the retained probe and recreates its old path, checking that cleanup
|
||||
removes the owned inode while preserving the replacement directory.
|
||||
It does not reproduce or identify the host-dependent cause reported in #1701.
|
||||
|
||||
+35
-15
@@ -60,6 +60,15 @@ napi_value wrap(napi_env env, std::unique_ptr<Resource> value) {
|
||||
value.release();
|
||||
return result;
|
||||
}
|
||||
void publishRuntimeHandle(napi_env env, napi_value observation, std::unique_ptr<Runtime>& value) {
|
||||
auto handle = object(env);
|
||||
check(napi_type_tag_object(env, handle, &RESOURCE_TAG));
|
||||
// Complete the result before transferring cleanup ownership. Any earlier
|
||||
// N-API failure leaves the unpublished runtime in its environment owner.
|
||||
set(env, observation, "handle", handle);
|
||||
check(napi_wrap(env, handle, value.get(), finalize, nullptr, nullptr));
|
||||
value.release();
|
||||
}
|
||||
Resource& resource(napi_env env, napi_value value, Kind kind) {
|
||||
bool tagged = false;
|
||||
check(napi_check_object_type_tag(env, value, &RESOURCE_TAG, &tagged));
|
||||
@@ -77,8 +86,15 @@ napi_value failureValue(napi_env env, const Failure& failure) {
|
||||
std::wstring detail = message == nullptr ? L"" : message;
|
||||
if (message != nullptr) LocalFree(message);
|
||||
const std::wstring constraint(failure.constraint.begin(), failure.constraint.end());
|
||||
auto text = string(env, constraint + (failure.path.empty() ? L"" : L" [" + failure.path + L"]") +
|
||||
L" (Win32 " + std::to_wstring(failure.win32) + L"): " + detail);
|
||||
std::wstring renderedMessage = constraint + (failure.path.empty() ? L"" : L" [" + failure.path + L"]") +
|
||||
L" (Win32 " + std::to_wstring(failure.win32) + L"): " + detail;
|
||||
if (!failure.cleanupConstraint.empty()) {
|
||||
const std::wstring cleanup(failure.cleanupConstraint.begin(), failure.cleanupConstraint.end());
|
||||
renderedMessage += L"; cleanup also failed: " + cleanup +
|
||||
(failure.cleanupPath.empty() ? L"" : L" [" + failure.cleanupPath + L"]") +
|
||||
L" (Win32 " + std::to_wstring(failure.cleanupWin32) + L")";
|
||||
}
|
||||
auto text = string(env, renderedMessage);
|
||||
napi_value error; napi_create_error(env, nullptr, text, &error);
|
||||
const char* code = failure.win32 == ERROR_FILE_NOT_FOUND || failure.win32 == ERROR_PATH_NOT_FOUND ? "ENOENT"
|
||||
: failure.win32 == ERROR_ACCESS_DENIED ? "EACCES" : "ERR_REA_WINDOWS_NATIVE";
|
||||
@@ -87,21 +103,24 @@ napi_value failureValue(napi_env env, const Failure& failure) {
|
||||
set(env, error, "requestedPath", string(env, failure.path));
|
||||
set(env, error, "win32Code", number(env, failure.win32));
|
||||
set(env, error, "win32Message", string(env, detail));
|
||||
if (!failure.cleanupConstraint.empty()) {
|
||||
set(env, error, "cleanupConstraint", string(env, failure.cleanupConstraint));
|
||||
set(env, error, "cleanupRequestedPath", string(env, failure.cleanupPath));
|
||||
set(env, error, "cleanupWin32Code", number(env, failure.cleanupWin32));
|
||||
}
|
||||
if (failure.runtimeDirectoryUnallocated)
|
||||
set(env, error, "runtimeAllocation", string(env, std::string("not-started")));
|
||||
return error;
|
||||
}
|
||||
|
||||
struct InspectionContext { std::unique_ptr<Runtime> probe; };
|
||||
|
||||
static void cleanupInspection(void* data) noexcept {
|
||||
std::unique_ptr<InspectionContext> context(static_cast<InspectionContext*>(data));
|
||||
if (!context->probe) return;
|
||||
try { closeRuntime(*context->probe); }
|
||||
static void cleanupRuntimeContext(void* data) noexcept {
|
||||
std::unique_ptr<RuntimeContext> context(static_cast<RuntimeContext*>(data));
|
||||
if (!context->pending) return;
|
||||
try { closeRuntime(*context->pending); }
|
||||
catch (const Failure& failure) {
|
||||
// Environment teardown cannot retain an owner or return an error. Report
|
||||
// terminal cleanup failure instead of claiming the directory was removed.
|
||||
std::fprintf(stderr, "REA Windows probe cleanup failed (Win32 %lu) path=\"",
|
||||
std::fprintf(stderr, "REA Windows runtime cleanup failed (Win32 %lu) path=\"",
|
||||
static_cast<unsigned long>(failure.win32));
|
||||
// Emit reversible UTF-16 escapes without allocation or CRT locale conversion.
|
||||
for (wchar_t unit : failure.path) {
|
||||
@@ -112,10 +131,10 @@ static void cleanupInspection(void* data) noexcept {
|
||||
std::fprintf(stderr, "\"\n");
|
||||
}
|
||||
catch (const std::exception& failure) {
|
||||
std::fprintf(stderr, "REA Windows probe cleanup failed: %s\n", failure.what());
|
||||
std::fprintf(stderr, "REA Windows runtime cleanup failed: %s\n", failure.what());
|
||||
}
|
||||
catch (...) {
|
||||
std::fprintf(stderr, "REA Windows probe cleanup failed: unknown native exception\n");
|
||||
std::fprintf(stderr, "REA Windows runtime cleanup failed: unknown native exception\n");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -128,12 +147,13 @@ static napi_value call(napi_env env, napi_callback_info info) {
|
||||
require(count == 2, "Native call requires operation and arguments", L"", ERROR_INVALID_PARAMETER);
|
||||
const auto operation = wide(env, args[0]);
|
||||
const auto values = array(env, args[1]);
|
||||
auto& context = *static_cast<RuntimeContext*>(data);
|
||||
if (operation == L"inspect") {
|
||||
require(values.empty(), "Inspect takes no arguments", L"", ERROR_INVALID_PARAMETER);
|
||||
return inspect(env, static_cast<InspectionContext*>(data)->probe);
|
||||
return inspect(env, context.pending);
|
||||
}
|
||||
if (operation.rfind(L"process_", 0) == 0) return processCall(env, operation, values);
|
||||
return filesystemCall(env, operation, values);
|
||||
return filesystemCall(env, operation, values, context);
|
||||
} catch (const Failure& failure) {
|
||||
auto error = failureValue(env, failure);
|
||||
napi_throw(env, error);
|
||||
@@ -145,8 +165,8 @@ static napi_value call(napi_env env, napi_callback_info info) {
|
||||
}
|
||||
|
||||
static napi_value initialize(napi_env env, napi_value exports) {
|
||||
auto context = std::make_unique<InspectionContext>();
|
||||
if (napi_add_env_cleanup_hook(env, cleanupInspection, context.get()) != napi_ok) return nullptr;
|
||||
auto context = std::make_unique<RuntimeContext>();
|
||||
if (napi_add_env_cleanup_hook(env, cleanupRuntimeContext, context.get()) != napi_ok) return nullptr;
|
||||
// The environment owns callback data before a function can expose it. Failed
|
||||
// initialization still leaves a valid context for the registered cleanup hook.
|
||||
auto* data = context.release();
|
||||
|
||||
@@ -33,6 +33,9 @@ struct Failure : std::runtime_error {
|
||||
DWORD win32;
|
||||
std::wstring path;
|
||||
std::string constraint;
|
||||
std::string cleanupConstraint;
|
||||
DWORD cleanupWin32 = ERROR_SUCCESS;
|
||||
std::wstring cleanupPath;
|
||||
// Affirmative proof for a failed runtime-parent admission; omission is unknown.
|
||||
bool runtimeDirectoryUnallocated = false;
|
||||
Failure(std::string constraint, std::wstring path = L"", DWORD code = GetLastError())
|
||||
@@ -96,6 +99,9 @@ struct Runtime : Resource {
|
||||
std::atomic_bool snapshotCancelled{false};
|
||||
Runtime() : Resource(Kind::Runtime) {}
|
||||
};
|
||||
// A failed, not-yet-published runtime remains owned by its N-API environment
|
||||
// until a later admission or environment teardown can retry cleanup by handle.
|
||||
struct RuntimeContext { std::unique_ptr<Runtime> pending; };
|
||||
struct Process : Resource {
|
||||
Handle job, process, stdoutRead, stderrRead, stdinWrite;
|
||||
// Present only for a job with a commit limit; receives the job's notifications.
|
||||
@@ -120,21 +126,24 @@ std::wstring wide(napi_env env, napi_value value);
|
||||
double numeric(napi_env env, napi_value value);
|
||||
std::vector<napi_value> array(napi_env env, napi_value value);
|
||||
napi_value wrap(napi_env env, std::unique_ptr<Resource> resource);
|
||||
void publishRuntimeHandle(napi_env env, napi_value observation, std::unique_ptr<Runtime>& runtime);
|
||||
Resource& resource(napi_env env, napi_value value, Kind kind);
|
||||
|
||||
std::unique_ptr<File> openFile(const std::wstring& path, DWORD access = GENERIC_READ,
|
||||
bool directory = false,
|
||||
DWORD finalSharing = FILE_SHARE_READ);
|
||||
napi_value identity(napi_env env, HANDLE handle, const std::wstring& path);
|
||||
std::unique_ptr<Runtime> createRuntime(const std::wstring& parent, const std::wstring& prefix);
|
||||
void createRuntime(const std::wstring& parent, const std::wstring& prefix,
|
||||
std::unique_ptr<Runtime>& owner);
|
||||
void cleanupFailedRuntime(std::unique_ptr<Runtime>& owner, Failure& primary);
|
||||
void closeRuntime(Runtime& runtime);
|
||||
napi_value filesystemCall(napi_env env, const std::wstring& operation,
|
||||
const std::vector<napi_value>& args);
|
||||
const std::vector<napi_value>& args, RuntimeContext& context);
|
||||
std::unique_ptr<Process> spawnProcess(const std::wstring& command, const std::wstring& commandLine,
|
||||
const std::wstring& cwd, const std::vector<std::wstring>& environment,
|
||||
bool protocolStdin = false, ULONGLONG memoryLimit = 0);
|
||||
napi_value processCall(napi_env env, const std::wstring& operation,
|
||||
const std::vector<napi_value>& args);
|
||||
napi_value inspect(napi_env env, std::unique_ptr<Runtime>& probe);
|
||||
napi_value inspect(napi_env env, std::unique_ptr<Runtime>& pending);
|
||||
|
||||
} // namespace rea
|
||||
@@ -184,6 +184,29 @@ static Handle createRelative(HANDLE parent, const std::wstring& name, DWORD acce
|
||||
return result;
|
||||
}
|
||||
|
||||
void cleanupFailedRuntime(std::unique_ptr<Runtime>& owner, Failure& primary) {
|
||||
if (!owner || !owner->directory.valid()) {
|
||||
owner.reset();
|
||||
return;
|
||||
}
|
||||
try {
|
||||
closeRuntime(*owner);
|
||||
owner.reset();
|
||||
} catch (const Failure& cleanup) {
|
||||
primary.cleanupConstraint = cleanup.constraint;
|
||||
primary.cleanupWin32 = cleanup.win32;
|
||||
primary.cleanupPath = cleanup.path;
|
||||
} catch (const std::exception& cleanup) {
|
||||
primary.cleanupConstraint = cleanup.what();
|
||||
primary.cleanupWin32 = ERROR_GEN_FAILURE;
|
||||
primary.cleanupPath = owner->path;
|
||||
} catch (...) {
|
||||
primary.cleanupConstraint = "Unknown runtime cleanup failure";
|
||||
primary.cleanupWin32 = ERROR_GEN_FAILURE;
|
||||
primary.cleanupPath = owner->path;
|
||||
}
|
||||
}
|
||||
|
||||
// Lookup relative to an already-owned directory; path is diagnostic only.
|
||||
static Handle openRelative(HANDLE parent, const std::wstring& name, DWORD access,
|
||||
DWORD sharing, ULONG kind, const std::wstring& path) {
|
||||
@@ -337,7 +360,11 @@ static napi_value readCutterBridgeDescriptor(napi_env env, const std::vector<nap
|
||||
return result;
|
||||
}
|
||||
|
||||
std::unique_ptr<Runtime> createRuntime(const std::wstring& parentPath, const std::wstring& prefix) {
|
||||
void createRuntime(const std::wstring& parentPath, const std::wstring& prefix,
|
||||
std::unique_ptr<Runtime>& owner) {
|
||||
// A previous failed admission retains the original root handle. Retry that
|
||||
// owner before allowing this environment to allocate another runtime.
|
||||
if (owner) { closeRuntime(*owner); owner.reset(); }
|
||||
std::unique_ptr<File> parent;
|
||||
try { parent = openFile(parentPath, 0, true); }
|
||||
catch (Failure& failure) {
|
||||
@@ -349,24 +376,29 @@ std::unique_ptr<Runtime> createRuntime(const std::wstring& parentPath, const std
|
||||
std::array<unsigned char, 16> random;
|
||||
require(BCryptGenRandom(nullptr, random.data(), random.size(), BCRYPT_USE_SYSTEM_PREFERRED_RNG) == 0,
|
||||
"Generate runtime identity failed", parentPath, ERROR_GEN_FAILURE);
|
||||
Security security;
|
||||
auto result = std::make_unique<Runtime>();
|
||||
const auto name = prefix + hex(random.data(), random.size());
|
||||
result->path = parent->path + (parent->path.back() == L'\\' ? L"" : L"\\") + name;
|
||||
Security security;
|
||||
result->directory = createRelative(parent->get(), name,
|
||||
DELETE | FILE_LIST_DIRECTORY | FILE_READ_ATTRIBUTES | READ_CONTROL,
|
||||
true, security, result->path);
|
||||
result->parents = std::move(parent->handles);
|
||||
owner = std::move(result);
|
||||
try {
|
||||
verifyPrivate(result->directory.get(), result->path, true);
|
||||
// The owned child pins the parent nonempty before write sharing is relaxed.
|
||||
parent->handles.back() = openComponent(parent->path, 0, true, FILE_SHARE_READ | FILE_SHARE_WRITE);
|
||||
result->parents = std::move(parent->handles);
|
||||
} catch (...) {
|
||||
FILE_DISPOSITION_INFO deletion{TRUE};
|
||||
SetFileInformationByHandle(result->directory.get(), FileDispositionInfo, &deletion, sizeof(deletion));
|
||||
owner->directory = createRelative(owner->parents.back().get(), name,
|
||||
DELETE | FILE_LIST_DIRECTORY | FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES | READ_CONTROL,
|
||||
true, security, owner->path);
|
||||
// FILE_CREATE proves ownership and pins the parent nonempty independently
|
||||
// of admission validation. A failed validation must not retain a strict
|
||||
// parent lease that prevents renaming the still-owned child before retry.
|
||||
owner->parents.back() = openComponent(parent->path, 0, true, FILE_SHARE_READ | FILE_SHARE_WRITE);
|
||||
verifyPrivate(owner->directory.get(), owner->path, true);
|
||||
} catch (Failure& primary) {
|
||||
cleanupFailedRuntime(owner, primary);
|
||||
throw;
|
||||
} catch (const std::exception& error) {
|
||||
Failure primary(error.what(), owner->path, ERROR_GEN_FAILURE);
|
||||
cleanupFailedRuntime(owner, primary);
|
||||
throw primary;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
static std::wstring relativePath(Runtime& root, const std::wstring& relative) {
|
||||
@@ -441,6 +473,9 @@ static void dispose(HANDLE handle, const std::wstring& path) {
|
||||
require(GetFileInformationByHandleEx(handle, FileBasicInfo, &info, sizeof(info)), "Read cleanup attributes failed", path);
|
||||
if ((info.FileAttributes & FILE_ATTRIBUTE_READONLY) != 0) {
|
||||
info.FileAttributes &= ~FILE_ATTRIBUTE_READONLY;
|
||||
// Zero means "leave attributes unchanged" to FileBasicInfo. A file whose
|
||||
// sole attribute was READONLY needs NORMAL to actually clear that marker.
|
||||
if (info.FileAttributes == 0) info.FileAttributes = FILE_ATTRIBUTE_NORMAL;
|
||||
require(SetFileInformationByHandle(handle, FileBasicInfo, &info, sizeof(info)), "Clear owned readonly attribute failed", path);
|
||||
}
|
||||
FILE_DISPOSITION_INFO deletion{TRUE};
|
||||
@@ -657,7 +692,8 @@ static napi_value snapshot(napi_env env, napi_value rootValue, Runtime& root,
|
||||
return promise;
|
||||
}
|
||||
|
||||
napi_value filesystemCall(napi_env env, const std::wstring& operation, const std::vector<napi_value>& args) {
|
||||
napi_value filesystemCall(napi_env env, const std::wstring& operation,
|
||||
const std::vector<napi_value>& args, RuntimeContext& context) {
|
||||
if (operation == L"cutter_bridge_verify_directory") return verifyCutterBridgeDirectory(env, args);
|
||||
if (operation == L"cutter_bridge_read_descriptor") return readCutterBridgeDescriptor(env, args);
|
||||
const auto expected = operation == L"open" || operation == L"close" || operation == L"runtime_close" || operation == L"runtime_snapshot_cancel" ? 1
|
||||
@@ -691,12 +727,21 @@ napi_value filesystemCall(napi_env env, const std::wstring& operation, const std
|
||||
return result;
|
||||
}
|
||||
if (operation == L"runtime_create") {
|
||||
auto root = createRuntime(wide(env, args[0]), wide(env, args[1]));
|
||||
auto result = identity(env, root->directory.get(), root->path);
|
||||
set(env, result, "path", string(env, root->path));
|
||||
set(env, result, "privateDacl", boolean(env, true));
|
||||
set(env, result, "handle", wrap(env, std::move(root)));
|
||||
return result;
|
||||
createRuntime(wide(env, args[0]), wide(env, args[1]), context.pending);
|
||||
try {
|
||||
auto result = identity(env, context.pending->directory.get(), context.pending->path);
|
||||
set(env, result, "path", string(env, context.pending->path));
|
||||
set(env, result, "privateDacl", boolean(env, true));
|
||||
publishRuntimeHandle(env, result, context.pending);
|
||||
return result;
|
||||
} catch (Failure& primary) {
|
||||
cleanupFailedRuntime(context.pending, primary);
|
||||
throw;
|
||||
} catch (const std::exception& error) {
|
||||
Failure primary(error.what(), context.pending ? context.pending->path : L"", ERROR_GEN_FAILURE);
|
||||
cleanupFailedRuntime(context.pending, primary);
|
||||
throw primary;
|
||||
}
|
||||
}
|
||||
auto& root = static_cast<Runtime&>(resource(env, args[0], Kind::Runtime));
|
||||
require(!root.closing || operation == L"runtime_close", "Runtime cleanup is pending", root.path, ERROR_BUSY);
|
||||
|
||||
@@ -454,17 +454,27 @@ napi_value processCall(napi_env env, const std::wstring& operation, const std::v
|
||||
throw Failure("Unknown native process operation", L"", ERROR_INVALID_PARAMETER);
|
||||
}
|
||||
|
||||
napi_value inspect(napi_env env, std::unique_ptr<Runtime>& probe) {
|
||||
napi_value inspect(napi_env env, std::unique_ptr<Runtime>& pending) {
|
||||
// Settle the previous owner before admitting another probe. Failed cleanup
|
||||
// retains its handles, including when identity construction itself failed.
|
||||
if (probe) { closeRuntime(*probe); probe.reset(); }
|
||||
if (pending) { closeRuntime(*pending); pending.reset(); }
|
||||
wchar_t temp[32768];
|
||||
const auto length = GetTempPathW(32768, temp);
|
||||
require(length > 0 && length < 32768, "Read temporary directory coordinate failed");
|
||||
probe = createRuntime(std::wstring(temp, length), L"rea-native-probe-");
|
||||
auto filesystem = identity(env, probe->directory.get(), probe->path);
|
||||
closeRuntime(*probe);
|
||||
probe.reset();
|
||||
createRuntime(std::wstring(temp, length), L"rea-native-probe-", pending);
|
||||
napi_value filesystem;
|
||||
try {
|
||||
filesystem = identity(env, pending->directory.get(), pending->path);
|
||||
} catch (Failure& primary) {
|
||||
cleanupFailedRuntime(pending, primary);
|
||||
throw;
|
||||
} catch (const std::exception& error) {
|
||||
Failure primary(error.what(), pending ? pending->path : L"", ERROR_GEN_FAILURE);
|
||||
cleanupFailedRuntime(pending, primary);
|
||||
throw primary;
|
||||
}
|
||||
closeRuntime(*pending);
|
||||
pending.reset();
|
||||
auto job = createJob();
|
||||
Attributes attributes(1);
|
||||
HANDLE raw = job.get();
|
||||
|
||||
Generated
+19
@@ -34,6 +34,7 @@
|
||||
"isomorphic-git": "1.38.7",
|
||||
"jsonc-parser": "3.3.1",
|
||||
"lossless-json": "4.3.1",
|
||||
"openpgp": "6.3.2",
|
||||
"parse5": "8.0.1",
|
||||
"pino": "10.4.0",
|
||||
"playwright-core": "1.58.2",
|
||||
@@ -7871,6 +7872,24 @@
|
||||
"regex-recursion": "^6.0.2"
|
||||
}
|
||||
},
|
||||
"node_modules/openpgp": {
|
||||
"version": "6.3.2",
|
||||
"resolved": "https://registry.npmjs.org/openpgp/-/openpgp-6.3.2.tgz",
|
||||
"integrity": "sha512-wcZTzHz41LV8Y48zH/JlD1JT8YdNmpWOuMAjQ/podvvCwsjBEU37K3znJh4UQcj3hksE5z9TzvhbUbwzsGvlLQ==",
|
||||
"license": "LGPL-3.0+",
|
||||
"engines": {
|
||||
"node": ">= 18.0.0",
|
||||
"typescript": ">= 5.0.0"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@openpgp/web-stream-tools": "~0.3.0"
|
||||
},
|
||||
"peerDependenciesMeta": {
|
||||
"@openpgp/web-stream-tools": {
|
||||
"optional": true
|
||||
}
|
||||
}
|
||||
},
|
||||
"node_modules/oxc-parser": {
|
||||
"version": "0.150.0",
|
||||
"resolved": "https://registry.npmjs.org/oxc-parser/-/oxc-parser-0.150.0.tgz",
|
||||
|
||||
@@ -337,6 +337,7 @@
|
||||
"isomorphic-git": "1.38.7",
|
||||
"jsonc-parser": "3.3.1",
|
||||
"lossless-json": "4.3.1",
|
||||
"openpgp": "6.3.2",
|
||||
"parse5": "8.0.1",
|
||||
"pino": "10.4.0",
|
||||
"playwright-core": "1.58.2",
|
||||
|
||||
+12
-10
@@ -1,5 +1,5 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Inspect or activate Hopper's version-pinned demo dialog on a private X display."""
|
||||
"""Inspect or activate Hopper's authenticated demo dialog on a private X display."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
@@ -8,6 +8,7 @@ import ctypes
|
||||
import hashlib
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
from pathlib import Path
|
||||
import secrets
|
||||
import select
|
||||
@@ -18,12 +19,6 @@ import threading
|
||||
import time
|
||||
|
||||
|
||||
# Keep this digest aligned with LinuxHopper.ts and rerun real-Hopper verification
|
||||
# before accepting another vendor build.
|
||||
SUPPORTED_HOPPER_SHA256 = {
|
||||
"0294ced141cc373468ee22d8343e7dac41980cb05a937994ca81c9f09afe7ded",
|
||||
"1339f9e58377442b0c6fcb0dfc3cec20d593cc557408521fad9a00dbc6b8da13",
|
||||
}
|
||||
EXPECTED_SCREEN = (1280, 1024)
|
||||
DIALOG_TITLES = {"Registration", "Registration — Hopper Disassembler"}
|
||||
HOPPER_WINDOW_TITLE = "Hopper Disassembler"
|
||||
@@ -121,6 +116,7 @@ def parse_args() -> argparse.Namespace:
|
||||
parser.add_argument("--probe", action="store_true")
|
||||
parser.add_argument("--owner-stdin", action="store_true")
|
||||
parser.add_argument("--hopper")
|
||||
parser.add_argument("--expected-launcher-sha256")
|
||||
parser.add_argument("--socket")
|
||||
parser.add_argument(
|
||||
"--strategy",
|
||||
@@ -661,7 +657,11 @@ def run_probe(args: argparse.Namespace) -> dict[str, object]:
|
||||
|
||||
|
||||
def run_launch(args: argparse.Namespace) -> int:
|
||||
if args.hopper is None or args.socket is None:
|
||||
if (
|
||||
args.hopper is None or args.socket is None
|
||||
or args.expected_launcher_sha256 is None
|
||||
or re.fullmatch(r"[a-f0-9]{64}", args.expected_launcher_sha256) is None
|
||||
):
|
||||
raise AdapterFailure(
|
||||
INVALID_LAUNCH_COMMAND,
|
||||
"invalid_launch_command",
|
||||
@@ -673,7 +673,9 @@ def run_launch(args: argparse.Namespace) -> int:
|
||||
hopper = Path(args.hopper)
|
||||
socket = Path(args.socket)
|
||||
try:
|
||||
supported = digest(hopper) in SUPPORTED_HOPPER_SHA256
|
||||
# The parent owns publisher/package interpretation. Recheck its admitted
|
||||
# launcher bytes immediately before execution, preserving all X11 guards.
|
||||
supported = digest(hopper) == args.expected_launcher_sha256
|
||||
except OSError as error:
|
||||
raise AdapterFailure(
|
||||
INVALID_LAUNCH_COMMAND,
|
||||
@@ -684,7 +686,7 @@ def run_launch(args: argparse.Namespace) -> int:
|
||||
raise AdapterFailure(
|
||||
UNSUPPORTED_HOPPER_BUILD,
|
||||
"unsupported_hopper_build",
|
||||
"unsupported Hopper binary",
|
||||
"Hopper binary differs from the authenticated release",
|
||||
)
|
||||
command = args.command[1:] if args.command[:1] == ["--"] else args.command
|
||||
if not command or Path(command[0]).resolve() != hopper.resolve():
|
||||
|
||||
+41
-8
@@ -14,6 +14,10 @@ process.env.REA_PACKAGE_VERSION = packageJson.version;
|
||||
const isMcpMode =
|
||||
args.length === 1 && (args[0] === "--mcp" || args[0] === "mcp");
|
||||
const isMcpDoctorMode = args[0] === "mcp" && args[1] === "doctor";
|
||||
/** Exit status of a writer terminated by SIGPIPE (128 + 13). */
|
||||
const SIGPIPE_EXIT_STATUS = 141;
|
||||
// Set once the reader of CLI output has closed the pipe.
|
||||
let stdoutClosed = false;
|
||||
const packageRoot = resolve(dirname(fileURLToPath(import.meta.url)), "..");
|
||||
|
||||
// Windows home lookup throws for an empty USERPROFILE. Dependencies resolve
|
||||
@@ -55,13 +59,14 @@ if (!(await compiledRuntimeExists(runtimeFiles))) {
|
||||
const { runEntrypoint } = await import("../dist/mcp/main.js");
|
||||
await runEntrypoint();
|
||||
} else if (isMcpDoctorMode) {
|
||||
guardClosedStdout();
|
||||
const { runProductionMcpDoctorCli } =
|
||||
await import("../dist/cli-runtime/mcpDoctor.js");
|
||||
const result = await runProductionMcpDoctorCli(args.slice(2), {
|
||||
dispatcherPath: fileURLToPath(import.meta.url),
|
||||
packageRoot,
|
||||
});
|
||||
process.stdout.write(result.output);
|
||||
writeStdout(result.output);
|
||||
process.exitCode = result.exitCode;
|
||||
} else {
|
||||
const { cliConfigurationError, createCli } =
|
||||
@@ -69,36 +74,64 @@ if (!(await compiledRuntimeExists(runtimeFiles))) {
|
||||
const { createCliResultOutput } =
|
||||
await import("../dist/cli-runtime/cliResultOutput.js");
|
||||
const {
|
||||
normalizeCliOutputArguments,
|
||||
renderCliOutputArgumentError,
|
||||
renderEmptyFilteredCliOutput,
|
||||
validateCliOutputArguments,
|
||||
} = await import("../dist/cli-runtime/cliOutput.js");
|
||||
const outputArguments = validateCliOutputArguments(args);
|
||||
guardClosedStdout();
|
||||
const cliArgs = normalizeCliOutputArguments(args);
|
||||
const outputArguments = validateCliOutputArguments(cliArgs);
|
||||
const configurationError = cliConfigurationError(process.env);
|
||||
if (configurationError !== undefined) {
|
||||
process.stderr.write(`${configurationError}\n`);
|
||||
process.exitCode = 1;
|
||||
} else if (!outputArguments.ok) {
|
||||
process.stdout.write(renderCliOutputArgumentError(outputArguments));
|
||||
writeStdout(renderCliOutputArgumentError(outputArguments));
|
||||
process.exitCode = 1;
|
||||
} else {
|
||||
let wroteOutput = false;
|
||||
const resultOutput = createCliResultOutput(args, process.stdout);
|
||||
await createCli(process.env, resultOutput).serve(args, {
|
||||
const resultOutput = createCliResultOutput(cliArgs, process.stdout);
|
||||
await createCli(process.env, resultOutput).serve(cliArgs, {
|
||||
stdout: (output) => {
|
||||
if (resultOutput?.handled) {
|
||||
if (resultOutput.failed) process.stderr.write(output);
|
||||
if (resultOutput.failed && !stdoutClosed)
|
||||
process.stderr.write(output);
|
||||
return;
|
||||
}
|
||||
if (output.length > 0) wroteOutput = true;
|
||||
process.stdout.write(output);
|
||||
writeStdout(output);
|
||||
},
|
||||
// A command failure caused by the closed pipe keeps the pipe's status.
|
||||
exit: (code) => process.exit(stdoutClosed ? SIGPIPE_EXIT_STATUS : code),
|
||||
});
|
||||
if (!wroteOutput && !resultOutput?.handled)
|
||||
process.stdout.write(renderEmptyFilteredCliOutput(args) ?? "");
|
||||
writeStdout(renderEmptyFilteredCliOutput(cliArgs) ?? "");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A reader that closes the pipe early, as `rea ... | head` does, has declined
|
||||
* the rest of the output. Stop writing without reporting a failure, and exit
|
||||
* with the status a SIGPIPE-terminated writer reports. MCP stdio keeps its own
|
||||
* client lifecycle.
|
||||
*/
|
||||
function guardClosedStdout() {
|
||||
process.stdout.on("error", (cause) => {
|
||||
if (cause?.code === "EPIPE") {
|
||||
stdoutClosed = true;
|
||||
process.exitCode = SIGPIPE_EXIT_STATUS;
|
||||
return;
|
||||
}
|
||||
// Other failures stay unhandled unless a pending JSON write reports them.
|
||||
if (process.stdout.listenerCount("error") === 1) throw cause;
|
||||
});
|
||||
}
|
||||
|
||||
function writeStdout(output) {
|
||||
if (!stdoutClosed) process.stdout.write(output);
|
||||
}
|
||||
|
||||
async function compiledRuntimeExists(paths) {
|
||||
for (const path of paths) {
|
||||
try {
|
||||
|
||||
@@ -13,6 +13,7 @@ import { promisify } from "node:util";
|
||||
|
||||
import { completeVerifierRun, createVerifierRun } from "./lib/verifier-run.mjs";
|
||||
import { assertCleanLinuxHopperRuntime } from "./lib/clean-linux-hopper-runtime.mjs";
|
||||
import { isUnsupportedHopperLaunchFailure } from "./verify-package-platform.mjs";
|
||||
|
||||
const execFileAsync = promisify(execFile);
|
||||
const verifierRun = createVerifierRun();
|
||||
@@ -89,16 +90,24 @@ try {
|
||||
await appendFile(modifiedHopper, new Uint8Array([0]));
|
||||
await chmod(modifiedHopper, 0o700);
|
||||
try {
|
||||
await execFileAsync(command, ["analyze", "/usr/bin/true"], {
|
||||
env: { ...process.env, HOPPER_LAUNCHER_PATH: modifiedHopper },
|
||||
timeout: 30_000,
|
||||
});
|
||||
await execFileAsync(
|
||||
command,
|
||||
["analyze", "/usr/bin/true", "--provider", "hopper", "--json"],
|
||||
{
|
||||
env: { ...process.env, HOPPER_LAUNCHER_PATH: modifiedHopper },
|
||||
timeout: 30_000,
|
||||
},
|
||||
);
|
||||
throw new Error("modified Hopper build was not rejected");
|
||||
} catch (error) {
|
||||
const output = `${error?.stdout ?? ""}\n${error?.stderr ?? ""}`;
|
||||
if (!output.includes("unsupported_hopper_build"))
|
||||
if (
|
||||
error?.code !== 1 ||
|
||||
typeof error.stdout !== "string" ||
|
||||
!isUnsupportedHopperLaunchFailure(JSON.parse(error.stdout))
|
||||
)
|
||||
throw new Error(
|
||||
"modified Hopper build did not return its stable failure code",
|
||||
{ cause: error },
|
||||
);
|
||||
}
|
||||
} finally {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { chmod, mkdir, symlink, writeFile } from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
import { pathToFileURL } from "node:url";
|
||||
import { supportedClients } from "../dist/application/SupportedClients.js";
|
||||
|
||||
/** Create an isolated prefix, fake PATH, client configs, and environment. */
|
||||
@@ -121,6 +122,14 @@ export const packageHopperEnvironment = (root, platform = process.platform) => {
|
||||
};
|
||||
return {
|
||||
HOPPER_LAUNCHER_PATH: "/bin/sh",
|
||||
// Feed availability must not turn this foreign-launcher rejection test into
|
||||
// an unrelated networking failure. Successful vendor verification is real E2E coverage.
|
||||
NODE_OPTIONS: [
|
||||
process.env.NODE_OPTIONS,
|
||||
`--import=${pathToFileURL(join(root, "tests/fixtures/hopperRejectionFeed.mjs")).href}`,
|
||||
]
|
||||
.filter(Boolean)
|
||||
.join(" "),
|
||||
HOPPER_LOADER_ARGS_JSON: JSON.stringify([
|
||||
"-c",
|
||||
'node_path=$1; shift; "$node_path" "$@"',
|
||||
|
||||
@@ -88,14 +88,29 @@ export async function verifyPackagePlatform({
|
||||
}
|
||||
}
|
||||
|
||||
/** Require the declared unsupported build or an exact missing-Xvfb diagnostic, never arbitrary provider failure. */
|
||||
/** One owner for unsupported-launch evidence across package and real-Hopper verification. */
|
||||
export const isUnsupportedHopperLaunchFailure = (failure) => {
|
||||
const details = failure?.details;
|
||||
const unsupportedBuild =
|
||||
details?.failure_code === "unsupported_hopper_build" &&
|
||||
details.exit_code === 72;
|
||||
const publisherRejection =
|
||||
details?.stage === "launch" &&
|
||||
details.launch_rejection?.boundary === "publisher_verification" &&
|
||||
details.launch_rejection.reason === "unsupported_hopper_build" &&
|
||||
details.exit_code === undefined &&
|
||||
details.launcher === undefined;
|
||||
return (
|
||||
failure?.code === "provider_unavailable" &&
|
||||
(publisherRejection || unsupportedBuild)
|
||||
);
|
||||
};
|
||||
|
||||
/** Require an exact pre-execution rejection, observed helper exit, or missing-Xvfb diagnostic. */
|
||||
export const assertLinuxPackageProviderFailure = (execution) => {
|
||||
const failure = json(execution.stdout);
|
||||
const details = failure?.details;
|
||||
const diagnostic = details?.diagnostics;
|
||||
const unsupportedBuild =
|
||||
details?.failure_code === "unsupported_hopper_build" &&
|
||||
details.exit_code === 72;
|
||||
const missingDisplayDependency =
|
||||
details?.failure_code === "runtime_dependency_unavailable" &&
|
||||
details.exit_code === 79 &&
|
||||
@@ -108,7 +123,7 @@ export const assertLinuxPackageProviderFailure = (execution) => {
|
||||
if (
|
||||
execution.status !== 1 ||
|
||||
failure?.code !== "provider_unavailable" ||
|
||||
(!unsupportedBuild && !missingDisplayDependency)
|
||||
(!isUnsupportedHopperLaunchFailure(failure) && !missingDisplayDependency)
|
||||
)
|
||||
throw new Error(
|
||||
`packaged Linux Hopper verification did not fail closed: ${JSON.stringify(failure)}`,
|
||||
|
||||
@@ -23,7 +23,7 @@ import {
|
||||
requireMcpOperationResult,
|
||||
} from "./lib/mcp-verifier-results.mjs";
|
||||
import { snapshotHopperRuntime } from "./lib/real-hopper-cleanup.mjs";
|
||||
import { HOPPER_TARGET_LEASE_DIRECTORY } from "../dist/hopper/HopperTargetLease.js";
|
||||
import { HOPPER_TARGET_LEASE_DIRECTORY } from "../dist/process/HopperLease.js";
|
||||
import { parseConfig } from "../dist/config/parseConfig.js";
|
||||
import {
|
||||
verifyHopperSourceBinding,
|
||||
|
||||
@@ -12,7 +12,7 @@ import {
|
||||
requirePseudocode,
|
||||
} from "../dist/application/RealHopperAssertions.js";
|
||||
import { HOPPER_PROVIDER_IDENTITY } from "../dist/hopper/HopperProviderCapabilities.js";
|
||||
import { HOPPER_TARGET_LEASE_DIRECTORY } from "../dist/hopper/HopperTargetLease.js";
|
||||
import { HOPPER_TARGET_LEASE_DIRECTORY } from "../dist/process/HopperLease.js";
|
||||
import { REA_WORKFLOW_PROVIDER } from "../dist/application/InvestigationProviders.js";
|
||||
import { loadRealHopperFixtureTargets } from "./lib/real-hopper-fixture.mjs";
|
||||
import { verifyHopperStringObjects } from "./lib/real-hopper-search.mjs";
|
||||
|
||||
@@ -55,14 +55,29 @@ try {
|
||||
join(workspace, "dist", "windows", "WindowsNativeLoader.js"),
|
||||
).href;
|
||||
const source = `import assert from 'node:assert/strict';
|
||||
import {createRequire} from 'node:module';
|
||||
import {existsSync,mkdirSync,readFileSync,readdirSync,renameSync,statSync,writeFileSync} from 'node:fs';
|
||||
import {join} from 'node:path';
|
||||
import {systemWindowsNativeAuthority} from ${JSON.stringify(loader)};
|
||||
const temp=process.env.TEMP;
|
||||
const first=systemWindowsNativeAuthority();
|
||||
assert.equal(first.available,false);
|
||||
assert.equal(first.retryable,true);
|
||||
assert.match(first.reason,/Delete owned object by handle failed.*Win32 5/s);
|
||||
const mode=process.argv[2];
|
||||
const creation=mode.startsWith('create-');
|
||||
const native=creation?createRequire(import.meta.url)(${JSON.stringify(join(workspace, "native", "windows", "build", "rea-windows-x64.node"))}):null;
|
||||
if(creation){
|
||||
let failure;
|
||||
try{native.call('runtime_create',[temp,'rea-native-created-'])}catch(error){failure=error}
|
||||
assert.ok(failure instanceof Error,'runtime_create unexpectedly passed injected post-create validation failure');
|
||||
assert.equal(failure.constraint,'Read private DACL by handle failed');
|
||||
assert.equal(failure.win32Code,5);
|
||||
assert.equal(failure.cleanupConstraint,'Delete owned object by handle failed');
|
||||
assert.equal(failure.cleanupWin32Code,5);
|
||||
assert.notEqual(failure.runtimeAllocation,'not-started');
|
||||
} else {
|
||||
const first=systemWindowsNativeAuthority();
|
||||
assert.equal(first.available,false);
|
||||
assert.equal(first.retryable,true);
|
||||
assert.match(first.reason,/Delete owned object by handle failed.*Win32 5/s);
|
||||
}
|
||||
const [name,...extra]=readdirSync(temp);assert.ok(name);assert.equal(extra.length,0);
|
||||
const path=join(temp,name);const identity=statSync(path,{bigint:true});
|
||||
// Delete sharing permits rename; ownership follows the retained NTFS handle.
|
||||
@@ -73,26 +88,40 @@ assert.equal(moved.dev,identity.dev);assert.equal(moved.ino,identity.ino);
|
||||
mkdirSync(path);const replacement=statSync(path,{bigint:true});
|
||||
assert.notEqual(replacement.ino,identity.ino);
|
||||
const sentinel=join(path,'unrelated.txt');writeFileSync(sentinel,'unrelated replacement');
|
||||
if(process.argv[2]!=='teardown'){
|
||||
if(mode==='retry'){
|
||||
const second=systemWindowsNativeAuthority();assert.equal(second.available,true);
|
||||
assert.equal(existsSync(renamed),false);
|
||||
assert.deepEqual(readdirSync(temp),[name]);
|
||||
assert.equal(readFileSync(sentinel,'utf8'),'unrelated replacement');
|
||||
assert.equal(statSync(path,{bigint:true}).ino,replacement.ino);
|
||||
assert.equal(systemWindowsNativeAuthority(),second);
|
||||
} else if(mode==='create-retry') {
|
||||
const second=native.call('runtime_create',[temp,'rea-native-created-retry-']);
|
||||
assert.equal(existsSync(renamed),false);
|
||||
assert.equal(readFileSync(sentinel,'utf8'),'unrelated replacement');
|
||||
native.call('runtime_close',[second.handle]);
|
||||
assert.equal(existsSync(second.path),false);
|
||||
assert.equal(statSync(path,{bigint:true}).ino,replacement.ino);
|
||||
}
|
||||
console.log(JSON.stringify({mode:process.argv[2],firstFailed:true,retainedOwner:true,name,renamed,device:identity.dev.toString(),inode:identity.ino.toString(),replacementInode:replacement.ino.toString()}));
|
||||
console.log(JSON.stringify({mode,firstFailed:true,retainedOwner:true,name,renamed,device:identity.dev.toString(),inode:identity.ino.toString(),replacementInode:replacement.ino.toString()}));
|
||||
`;
|
||||
const fixture = join(workspace, "probe.mjs");
|
||||
await writeFile(fixture, source);
|
||||
for (const mode of ["retry", "teardown"]) {
|
||||
for (const mode of ["retry", "teardown", "create-retry", "create-teardown"]) {
|
||||
const temp = join(workspace, mode);
|
||||
await mkdir(temp);
|
||||
const child = spawnSync(process.execPath, [fixture, mode], {
|
||||
encoding: "utf8",
|
||||
timeout: 20_000,
|
||||
windowsHide: true,
|
||||
env: { ...process.env, TEMP: temp, TMP: temp },
|
||||
env: {
|
||||
...process.env,
|
||||
TEMP: temp,
|
||||
TMP: temp,
|
||||
...(mode.startsWith("create-")
|
||||
? { REA_NATIVE_CREATION_FAILURE: "1" }
|
||||
: {}),
|
||||
},
|
||||
});
|
||||
if (child.error !== undefined) throw child.error;
|
||||
assert.equal(child.status, 0, child.stderr);
|
||||
@@ -119,7 +148,7 @@ console.log(JSON.stringify({mode:process.argv[2],firstFailed:true,retainedOwner:
|
||||
);
|
||||
}
|
||||
process.stdout.write(
|
||||
`${JSON.stringify({ ok: true, controlledDispositionFailure: true, retainedOwner: true, renamedOwnerCleansChildren: true, unrelatedReplacementPreserved: true, retryRecovers: true, successfulAdmissionCached: true, environmentTeardownCleansProbe: true, issue1701HostCauseReproduced: false })}\n`,
|
||||
`${JSON.stringify({ ok: true, inspectionPostCreateValidationFailure: true, runtimeCreatePostCreateValidationFailure: true, controlledRollbackDispositionFailure: true, retainedOwner: true, renamedOwnerCleansChildren: true, unrelatedReplacementPreserved: true, retryRecoversByInspectionAndCreation: true, successfulAdmissionCached: true, environmentTeardownCleansProbeAndCreation: true, issue1701HostCauseReproduced: false })}\n`,
|
||||
);
|
||||
} finally {
|
||||
await rm(workspace, { recursive: true, force: true });
|
||||
|
||||
@@ -48,6 +48,13 @@ const { PrivateRuntimeRootUnavailableError } = await import(
|
||||
)
|
||||
);
|
||||
const workspace = await mkdtemp(join(tmpdir(), "rea-native-conformance-"));
|
||||
// A pwsh caller can supply module paths incompatible with Windows PowerShell.
|
||||
// Each independent observer must discover its own module path.
|
||||
const windowsPowerShellEnvironment = Object.fromEntries(
|
||||
Object.entries(process.env).filter(
|
||||
([name]) => name.toLowerCase() !== "psmodulepath",
|
||||
),
|
||||
);
|
||||
const environment = Object.entries(process.env)
|
||||
.filter(([, value]) => value !== undefined)
|
||||
.map(([key, value]) => `${key}=${value}`);
|
||||
@@ -309,6 +316,7 @@ process.stdout.write(JSON.stringify({executable:basename(process.execPath),abiVe
|
||||
]);
|
||||
const sentinel = join(sourceDirectory, "sentinel");
|
||||
await writeFile(sentinel, "preserve");
|
||||
const runtimeVerificationErrors = [];
|
||||
try {
|
||||
const acl = JSON.parse(
|
||||
execFileSync(
|
||||
@@ -317,18 +325,13 @@ process.stdout.write(JSON.stringify({executable:basename(process.execPath),abiVe
|
||||
"-NoProfile",
|
||||
"-NonInteractive",
|
||||
"-Command",
|
||||
"$a=Get-Acl -LiteralPath $env:REA_TEST_PRIVATE_RUNTIME_PATH; $u=[Security.Principal.WindowsIdentity]::GetCurrent().User; @{protected=$a.AreAccessRulesProtected; ownerMatchesUser=($a.GetOwner([Security.Principal.SecurityIdentifier]).Value -eq $u.Value); trusteesOnlyUserAndSystem=(@($a.GetAccessRules($true,$true,[Security.Principal.SecurityIdentifier]) | Where-Object {$_.IdentityReference.Value -ne $u.Value -and $_.IdentityReference.Value -ne 'S-1-5-18'}).Count -eq 0); count=@($a.Access).Count} | ConvertTo-Json -Compress",
|
||||
"$ErrorActionPreference='Stop'; $p=$env:REA_TEST_PRIVATE_RUNTIME_PATH; $a=Get-Acl -LiteralPath $p; $u=[Security.Principal.WindowsIdentity]::GetCurrent().User; [IO.File]::SetAttributes($p,[IO.FileAttributes]([int][IO.File]::GetAttributes($p) -bor 1)); @{protected=$a.AreAccessRulesProtected; ownerMatchesUser=($a.GetOwner([Security.Principal.SecurityIdentifier]).Value -eq $u.Value); trusteesOnlyUserAndSystem=(@($a.GetAccessRules($true,$true,[Security.Principal.SecurityIdentifier]) | Where-Object {$_.IdentityReference.Value -ne $u.Value -and $_.IdentityReference.Value -ne 'S-1-5-18'}).Count -eq 0); count=@($a.Access).Count; rootReadonly=(([int][IO.File]::GetAttributes($p) -band 1) -ne 0)} | ConvertTo-Json -Compress",
|
||||
],
|
||||
{
|
||||
encoding: "utf8",
|
||||
// A pwsh caller can supply module paths incompatible with the
|
||||
// Windows PowerShell observer. Let that observer build its own path.
|
||||
timeout: 15_000,
|
||||
env: {
|
||||
...Object.fromEntries(
|
||||
Object.entries(process.env).filter(
|
||||
([name]) => name.toLowerCase() !== "psmodulepath",
|
||||
),
|
||||
),
|
||||
...windowsPowerShellEnvironment,
|
||||
REA_TEST_PRIVATE_RUNTIME_PATH: runtime.path,
|
||||
},
|
||||
},
|
||||
@@ -339,6 +342,7 @@ process.stdout.write(JSON.stringify({executable:basename(process.execPath),abiVe
|
||||
ownerMatchesUser: true,
|
||||
trusteesOnlyUserAndSystem: true,
|
||||
count: 2,
|
||||
rootReadonly: true,
|
||||
});
|
||||
native.call("runtime_mkdir", [runtime.handle, "inputs"]);
|
||||
native.call("runtime_write", [
|
||||
@@ -484,7 +488,10 @@ process.stdout.write(JSON.stringify({executable:basename(process.execPath),abiVe
|
||||
{
|
||||
encoding: "utf8",
|
||||
timeout: 15_000,
|
||||
env: { ...process.env, REA_SNAPSHOT_READER_PATH: snapshotPath },
|
||||
env: {
|
||||
...windowsPowerShellEnvironment,
|
||||
REA_SNAPSHOT_READER_PATH: snapshotPath,
|
||||
},
|
||||
},
|
||||
).trim();
|
||||
assert.equal(readerDigest, readbackSnapshot.sha256);
|
||||
@@ -574,12 +581,52 @@ process.stdout.write(JSON.stringify({executable:basename(process.execPath),abiVe
|
||||
asynchronousSnapshot: true,
|
||||
snapshotCancellation: true,
|
||||
};
|
||||
} finally {
|
||||
native.call("runtime_close", [runtime.handle]);
|
||||
const readonlyChildren = JSON.parse(
|
||||
execFileSync(
|
||||
"powershell.exe",
|
||||
[
|
||||
"-NoProfile",
|
||||
"-NonInteractive",
|
||||
"-Command",
|
||||
"$ErrorActionPreference='Stop'; $items=ConvertFrom-Json -InputObject $env:REA_TEST_READONLY_CHILDREN; @($items | ForEach-Object { $current=[int][IO.File]::GetAttributes($_); $next=if(($current -band 16) -ne 0){$current -bor 1}else{1}; [IO.File]::SetAttributes($_,[IO.FileAttributes]$next); $a=[int][IO.File]::GetAttributes($_); @{readonly=(($a -band 1) -ne 0); directory=(($a -band 16) -ne 0); onlyReadonly=($a -eq 1)} }) | ConvertTo-Json -Compress",
|
||||
],
|
||||
{
|
||||
encoding: "utf8",
|
||||
timeout: 15_000,
|
||||
env: {
|
||||
...windowsPowerShellEnvironment,
|
||||
REA_TEST_READONLY_CHILDREN: JSON.stringify([
|
||||
join(runtime.path, "inputs"),
|
||||
join(runtime.path, "inputs", "target.bin"),
|
||||
]),
|
||||
},
|
||||
},
|
||||
),
|
||||
);
|
||||
assert.deepEqual(readonlyChildren, [
|
||||
{ readonly: true, directory: true, onlyReadonly: false },
|
||||
{ readonly: true, directory: false, onlyReadonly: true },
|
||||
]);
|
||||
} catch (failure) {
|
||||
runtimeVerificationErrors.push(failure);
|
||||
}
|
||||
try {
|
||||
native.call("runtime_close", [runtime.handle]);
|
||||
} catch (cleanupFailure) {
|
||||
runtimeVerificationErrors.push(cleanupFailure);
|
||||
}
|
||||
if (runtimeVerificationErrors.length === 1)
|
||||
throw runtimeVerificationErrors[0];
|
||||
if (runtimeVerificationErrors.length > 1)
|
||||
throw new AggregateError(
|
||||
runtimeVerificationErrors,
|
||||
"Native runtime verification and cleanup failed",
|
||||
);
|
||||
assert.equal(await exists(runtime.path), false);
|
||||
assert.equal(await readFile(sentinel, "utf8"), "preserve");
|
||||
report.controls.cleanupDoesNotTraverseJunctions = true;
|
||||
report.controls.readonlyRootAndChildrenRemoved = true;
|
||||
report.controls.readonlyFileWithoutArchiveAttributeRemoved = true;
|
||||
const fixture = join(workspace, "tree.mjs");
|
||||
await writeFile(
|
||||
fixture,
|
||||
|
||||
@@ -709,7 +709,7 @@ it("projects package details from dumpsys package output", async () => {
|
||||
});
|
||||
expect(result).toMatchObject({
|
||||
version_name: "1.2.3",
|
||||
version_code: 45,
|
||||
version_code: "45",
|
||||
first_install_time: "2026-01-02 03:04:05",
|
||||
last_update_time: "2026-02-03 04:05:06",
|
||||
installer_package_name: "com.android.vending",
|
||||
@@ -719,6 +719,30 @@ it("projects package details from dumpsys package output", async () => {
|
||||
});
|
||||
});
|
||||
|
||||
// PackageInfo.getLongVersionCode() combines versionCodeMajor and versionCode
|
||||
// into a signed 64-bit long, which JSON numbers cannot hold exactly.
|
||||
it.each(["9007199254740993", "-4294967296"])(
|
||||
"keeps the long version code %s exactly as dumpsys prints it",
|
||||
async (versionCode) => {
|
||||
const { binary } = await writeAdbStub(
|
||||
shellStub({
|
||||
"dumpsys:package": `Packages:
|
||||
Package [com.example] (abcdef):
|
||||
versionCode=${versionCode} minSdk=24 targetSdk=34
|
||||
`,
|
||||
}),
|
||||
);
|
||||
const result = await evidenceResult(providerOver(binary), {
|
||||
operation: "inspect_adb_package",
|
||||
input: { serial: "emulator-5554", package: "com.example" },
|
||||
});
|
||||
expect(result).toMatchObject({
|
||||
version_code: versionCode,
|
||||
coverage: "complete",
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
// Android 10 and 11 append ": restricted=true" to hard- and soft-restricted
|
||||
// entries of the requested-permissions list (Settings.dumpPackageLPr).
|
||||
it("counts requested permissions that the device marks as restricted", async () => {
|
||||
|
||||
@@ -193,7 +193,8 @@ export const parseWindowFocusOutput = (
|
||||
|
||||
export interface AdbPackageDetails {
|
||||
readonly versionName: string | null;
|
||||
readonly versionCode: number | null;
|
||||
/** Signed 64-bit long version code exactly as Package Manager prints it. */
|
||||
readonly versionCode: string | null;
|
||||
readonly firstInstallTime: string | null;
|
||||
readonly lastUpdateTime: string | null;
|
||||
readonly installerPackageName: string | null;
|
||||
@@ -214,7 +215,7 @@ export const parseDumpsysPackageOutput = (
|
||||
): AdbPackageDetails => {
|
||||
const details: {
|
||||
versionName: string | null;
|
||||
versionCode: number | null;
|
||||
versionCode: string | null;
|
||||
firstInstallTime: string | null;
|
||||
lastUpdateTime: string | null;
|
||||
installerPackageName: string | null;
|
||||
@@ -252,8 +253,8 @@ export const parseDumpsysPackageOutput = (
|
||||
if (key === "versionName" && details.versionName === null)
|
||||
details.versionName = value === "" ? null : value;
|
||||
else if (key === "versionCode" && details.versionCode === null) {
|
||||
const parsed = /^(\d+)/u.exec(value);
|
||||
if (parsed !== null) details.versionCode = Number(parsed[1]!);
|
||||
const parsed = /^(-?\d+)/u.exec(value);
|
||||
if (parsed !== null) details.versionCode = parsed[1]!;
|
||||
} else if (key === "firstInstallTime" && details.firstInstallTime === null)
|
||||
details.firstInstallTime = value === "" ? null : value;
|
||||
else if (key === "lastUpdateTime" && details.lastUpdateTime === null)
|
||||
|
||||
@@ -8,10 +8,12 @@ import { snapshotEnvironment } from "../process/snapshotEnvironment.js";
|
||||
import { ABORTED, waitForAbortable } from "../process/AbortablePromise.js";
|
||||
import { ProviderStartupDeadline } from "../process/ProviderDeadline.js";
|
||||
|
||||
import { randomUUID } from "node:crypto";
|
||||
import {
|
||||
execFileOutput,
|
||||
execFileOutputFailure,
|
||||
} from "../process/ExecFileOutput.js";
|
||||
OwnedCommandFailure,
|
||||
runOwnedCommand,
|
||||
} from "../process/OwnedCommand.js";
|
||||
import type { ProviderProcessSupervisor } from "../process/ProviderProcess.js";
|
||||
import type { ProviderAvailability } from "../application/AnalysisProvider.js";
|
||||
import type { ProviderRejectionCode } from "../contracts/providerSelection.js";
|
||||
import type { JsonValue } from "../domain/jsonValue.js";
|
||||
@@ -152,14 +154,23 @@ export type ApktoolConfigurationFailureCode =
|
||||
| "version_unresolved"
|
||||
| "unsupported_version";
|
||||
|
||||
/** Unresolved cleanup of a launched probe; its owner was retained separately. */
|
||||
export interface ApktoolProbeCleanup {
|
||||
readonly reason: string;
|
||||
readonly resources: readonly string[];
|
||||
}
|
||||
|
||||
export class ApktoolConfigurationFailure extends Error {
|
||||
readonly cleanup: ApktoolProbeCleanup | undefined;
|
||||
|
||||
constructor(
|
||||
readonly code: ApktoolConfigurationFailureCode,
|
||||
message: string,
|
||||
readonly diagnostics: Readonly<Record<string, JsonValue>> = {},
|
||||
options?: ErrorOptions,
|
||||
options?: ErrorOptions & { readonly cleanup?: ApktoolProbeCleanup },
|
||||
) {
|
||||
super(message, options);
|
||||
this.cleanup = options?.cleanup;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -228,6 +239,7 @@ export const inspectApktoolClient = async (
|
||||
signal?: AbortSignal,
|
||||
environment: Readonly<Record<string, string | undefined>> = process.env,
|
||||
platform: NodeJS.Platform = process.platform,
|
||||
onCleanupOwner?: (owner: ProviderProcessSupervisor) => void,
|
||||
): Promise<ApktoolClientIdentity> => {
|
||||
const hostEnvironment = snapshotEnvironment(environment, platform);
|
||||
let invocation: ApktoolInvocation;
|
||||
@@ -254,23 +266,57 @@ export const inspectApktoolClient = async (
|
||||
let stdout: string;
|
||||
let stderr: string;
|
||||
try {
|
||||
({ stdout, stderr } = await execFileOutput(
|
||||
invocation.command,
|
||||
invocation.arguments,
|
||||
// The owned process group stops a shell launcher's children with it; a
|
||||
// child holding inherited output pipes cannot outlive the deadline.
|
||||
const output = await runOwnedCommand(
|
||||
{
|
||||
command: invocation.command,
|
||||
arguments: invocation.arguments,
|
||||
windowsVerbatimArguments: invocation.windowsVerbatimArguments,
|
||||
timeout: VERSION_TIMEOUT_MS,
|
||||
stopSignal: "SIGTERM",
|
||||
maxBuffer: VERSION_MAX_OUTPUT_BYTES,
|
||||
env: hostEnvironment,
|
||||
...(signal === undefined ? {} : { signal }),
|
||||
// Apktool launchers are commonly interpreter scripts, so ownership is
|
||||
// proved by the run token rather than the launcher's argv prefix.
|
||||
expectedCommand: null,
|
||||
runId: randomUUID(),
|
||||
hostEnvironment,
|
||||
},
|
||||
));
|
||||
{
|
||||
timeoutMs: VERSION_TIMEOUT_MS,
|
||||
diagnosticBytes: VERSION_MAX_OUTPUT_BYTES,
|
||||
},
|
||||
signal === undefined ? {} : { signal },
|
||||
);
|
||||
stdout = output.stdout.text;
|
||||
stderr = output.stderr.text;
|
||||
} catch (cause) {
|
||||
const failure = execFileOutputFailure(cause);
|
||||
if (signal?.aborted || failure?.code === "ABORT_ERR")
|
||||
throw new AnalysisCancelledError("inspect_apktool_client", { cause });
|
||||
if (failure?.code === "ENOENT")
|
||||
if (
|
||||
cause instanceof OwnedCommandFailure &&
|
||||
cause.cleanupOwner !== undefined
|
||||
)
|
||||
onCleanupOwner?.(cause.cleanupOwner);
|
||||
const cleanup =
|
||||
cause instanceof OwnedCommandFailure && cause.cleanupFailure !== null
|
||||
? { reason: cause.cleanupFailure, resources: cause.resources }
|
||||
: undefined;
|
||||
const cleanupDetails = cleanup === undefined ? {} : { cleanup };
|
||||
if (
|
||||
signal?.aborted === true ||
|
||||
(cause instanceof OwnedCommandFailure && cause.reason === "cancelled")
|
||||
)
|
||||
throw new AnalysisCancelledError("inspect_apktool_client", {
|
||||
cause,
|
||||
...cleanupDetails,
|
||||
});
|
||||
if (cause instanceof OwnedCommandFailure && cause.reason === "timeout")
|
||||
throw new AnalysisTimeoutError(
|
||||
"inspect_apktool_client",
|
||||
VERSION_TIMEOUT_MS,
|
||||
{ cause, ...cleanupDetails },
|
||||
);
|
||||
if (
|
||||
cause instanceof Error &&
|
||||
"code" in cause &&
|
||||
(cause.code === "ENOENT" || cause.code === "EACCES")
|
||||
)
|
||||
throw new ApktoolConfigurationFailure(
|
||||
"command_missing",
|
||||
`The selected apktool launcher was not found: ${selection.command}`,
|
||||
@@ -281,19 +327,25 @@ export const inspectApktoolClient = async (
|
||||
},
|
||||
{ cause },
|
||||
);
|
||||
const snapshot =
|
||||
cause instanceof OwnedCommandFailure ? cause.snapshot : null;
|
||||
throw new ApktoolConfigurationFailure(
|
||||
"version_unresolved",
|
||||
"The selected apktool launcher did not report a usable version",
|
||||
{
|
||||
selected_command: selection.command,
|
||||
...(failure === undefined
|
||||
...(snapshot === null
|
||||
? {}
|
||||
: {
|
||||
exit_code: failure.code,
|
||||
stderr_excerpt: failure.stderr.slice(0, 512),
|
||||
exit_code: snapshot.exitCode ?? null,
|
||||
stderr_excerpt: snapshot.stderr.text.slice(0, 512),
|
||||
}),
|
||||
...(cause instanceof OwnedCommandFailure &&
|
||||
cause.reason === "output-limit"
|
||||
? { max_output_bytes: VERSION_MAX_OUTPUT_BYTES }
|
||||
: {}),
|
||||
},
|
||||
{ cause },
|
||||
{ cause, ...cleanupDetails },
|
||||
);
|
||||
}
|
||||
const identity = parseApktoolVersionOutput(stdout);
|
||||
|
||||
@@ -56,6 +56,7 @@ import { WindowsBatchCommandError } from "../process/WindowsBatchCommand.js";
|
||||
import {
|
||||
APKTOOL_COMMAND_ENV,
|
||||
ApktoolConfigurationFailure,
|
||||
type ApktoolProbeCleanup,
|
||||
apktoolConfigurationAvailability,
|
||||
apktoolInvocation,
|
||||
inspectApktoolClient,
|
||||
@@ -148,6 +149,19 @@ interface DecodeLimits {
|
||||
readonly maxOutputBytes: number;
|
||||
}
|
||||
|
||||
/** A probe whose launched processes could not be confirmed stopped. */
|
||||
const probeCleanupFailure = (
|
||||
operation: string,
|
||||
failure: ApktoolConfigurationFailure,
|
||||
cleanup: ApktoolProbeCleanup,
|
||||
): ProviderCleanupError =>
|
||||
new ProviderCleanupError(
|
||||
APKTOOL_PROVIDER_IDENTITY.id,
|
||||
cleanup.resources,
|
||||
{ reason: cleanup.reason, probe_failure: failure.message },
|
||||
{ operation, cause: failure },
|
||||
);
|
||||
|
||||
const runApktool = async (
|
||||
operation: string,
|
||||
selection: ApktoolCommandSelection,
|
||||
@@ -180,6 +194,9 @@ const runApktool = async (
|
||||
command: invocation.command,
|
||||
arguments: invocation.arguments,
|
||||
windowsVerbatimArguments: invocation.windowsVerbatimArguments,
|
||||
// Apktool launchers are commonly interpreter scripts, so ownership is
|
||||
// proved by the run token rather than the launcher's argv prefix.
|
||||
expectedCommand: null,
|
||||
runId: randomUUID(),
|
||||
hostEnvironment: environment,
|
||||
},
|
||||
@@ -328,6 +345,10 @@ export class ApktoolProvider {
|
||||
private readonly workspaces = new Set<string>();
|
||||
private readonly removeWorkspace: (path: string) => Promise<void>;
|
||||
readonly #pendingCommandOwners = new Map<ProviderProcessSupervisor, string>();
|
||||
/** A version probe whose cleanup is unresolved is retried on close. */
|
||||
readonly #retainProbeOwner = (owner: ProviderProcessSupervisor): void => {
|
||||
this.#pendingCommandOwners.set(owner, "apktool version probe");
|
||||
};
|
||||
readonly #shutdown = new AbortController();
|
||||
readonly #active = new Set<Promise<void>>();
|
||||
#closing = false;
|
||||
@@ -378,7 +399,13 @@ export class ApktoolProvider {
|
||||
process.platform,
|
||||
signal,
|
||||
);
|
||||
await inspectApktoolClient(selection, signal, environment);
|
||||
await inspectApktoolClient(
|
||||
selection,
|
||||
signal,
|
||||
environment,
|
||||
process.platform,
|
||||
this.#retainProbeOwner,
|
||||
);
|
||||
throwIfCancelled("inspect_apktool_client", signal);
|
||||
return {
|
||||
status: "available",
|
||||
@@ -390,8 +417,15 @@ export class ApktoolProvider {
|
||||
},
|
||||
};
|
||||
} catch (cause) {
|
||||
if (cause instanceof ApktoolConfigurationFailure)
|
||||
if (cause instanceof ApktoolConfigurationFailure) {
|
||||
if (cause.cleanup !== undefined)
|
||||
throw probeCleanupFailure(
|
||||
"inspect_apktool_client",
|
||||
cause,
|
||||
cause.cleanup,
|
||||
);
|
||||
return apktoolConfigurationAvailability(cause);
|
||||
}
|
||||
throw cause;
|
||||
}
|
||||
}
|
||||
@@ -437,7 +471,13 @@ export class ApktoolProvider {
|
||||
process.platform,
|
||||
signal,
|
||||
);
|
||||
identity = await inspectApktoolClient(selection, signal, environment);
|
||||
identity = await inspectApktoolClient(
|
||||
selection,
|
||||
signal,
|
||||
environment,
|
||||
process.platform,
|
||||
this.#retainProbeOwner,
|
||||
);
|
||||
} catch (cause) {
|
||||
if (
|
||||
cause instanceof AnalysisCancelledError ||
|
||||
@@ -445,6 +485,10 @@ export class ApktoolProvider {
|
||||
)
|
||||
return err(cause);
|
||||
if (cause instanceof ApktoolConfigurationFailure) {
|
||||
if (cause.cleanup !== undefined)
|
||||
return err(
|
||||
probeCleanupFailure(request.operation, cause, cause.cleanup),
|
||||
);
|
||||
return err(
|
||||
providerConfigurationRefusal(
|
||||
APKTOOL_PROVIDER_IDENTITY.id,
|
||||
@@ -497,12 +541,14 @@ export class ApktoolProvider {
|
||||
async #drainAndClean(): Promise<void> {
|
||||
await Promise.all(this.#active);
|
||||
const failures: unknown[] = [];
|
||||
const retained: string[] = [];
|
||||
for (const [owner, root] of this.#pendingCommandOwners) {
|
||||
try {
|
||||
const stopped = await owner.stop();
|
||||
if (stopped.status === "incomplete") throw new Error(stopped.reason);
|
||||
this.#pendingCommandOwners.delete(owner);
|
||||
} catch (cause) {
|
||||
retained.push(root);
|
||||
failures.push(
|
||||
new Error(
|
||||
`apktool process cleanup remains incomplete for ${root}: ${cause instanceof Error ? cause.message : String(cause)}`,
|
||||
@@ -511,8 +557,23 @@ export class ApktoolProvider {
|
||||
);
|
||||
}
|
||||
}
|
||||
// Ownership stays retained for a later close; report it as typed cleanup.
|
||||
if (failures.length > 0)
|
||||
throw new AggregateError(failures, "Apktool process cleanup failed");
|
||||
throw new ProviderCleanupError(
|
||||
APKTOOL_PROVIDER_IDENTITY.id,
|
||||
retained,
|
||||
{
|
||||
reason: failures
|
||||
.map((failure) =>
|
||||
failure instanceof Error ? failure.message : String(failure),
|
||||
)
|
||||
.join("; "),
|
||||
},
|
||||
{
|
||||
operation: "close",
|
||||
cause: new AggregateError(failures, "Apktool process cleanup failed"),
|
||||
},
|
||||
);
|
||||
for (const path of this.workspaces) {
|
||||
try {
|
||||
await this.removeWorkspace(path);
|
||||
|
||||
@@ -0,0 +1,107 @@
|
||||
import { execFile } from "node:child_process";
|
||||
import { chmod, readFile, writeFile } from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
import { setTimeout as delay } from "node:timers/promises";
|
||||
import { promisify } from "node:util";
|
||||
import { expect, it } from "vitest";
|
||||
|
||||
import { AnalysisCancelledError } from "../domain/analysisErrorCore.js";
|
||||
import { createTestTempDirectory } from "../../tests/fixtures/temporaryDirectory.js";
|
||||
import { inspectApktoolClient } from "./ApktoolConfiguration.js";
|
||||
|
||||
/**
|
||||
* Only an exited process counts as stopped: it is gone, a zombie waiting to
|
||||
* be reaped, or absent from `ps`. A failed inspection is not proof of exit.
|
||||
*/
|
||||
const alive = async (pid: number): Promise<boolean> => {
|
||||
try {
|
||||
process.kill(pid, 0);
|
||||
} catch (cause: unknown) {
|
||||
if ((cause as NodeJS.ErrnoException).code === "ESRCH") return false;
|
||||
return true;
|
||||
}
|
||||
return promisify(execFile)("ps", ["-o", "stat=", "-p", `${pid}`]).then(
|
||||
({ stdout }) => {
|
||||
const state = stdout.trim();
|
||||
return state !== "" && !state.startsWith("Z");
|
||||
},
|
||||
// `ps` exits 1 when no process matches; any other failure is unknown.
|
||||
(cause: unknown) => (cause as { code?: unknown }).code !== 1,
|
||||
);
|
||||
};
|
||||
|
||||
const waitFor = async (check: () => Promise<boolean>, ms: number) => {
|
||||
const deadline = Date.now() + ms;
|
||||
while (Date.now() < deadline) {
|
||||
if (await check()) return true;
|
||||
await delay(25);
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
const readPids = async (path: string): Promise<readonly number[]> =>
|
||||
readFile(path, "utf8")
|
||||
.then((text) => JSON.parse(text) as number[])
|
||||
.catch(() => []);
|
||||
|
||||
// The interpreter is the test runtime itself, so its environment (and the
|
||||
// ownership run token) is readable on both Linux and macOS.
|
||||
it.skipIf(process.platform === "win32")(
|
||||
"stops an interpreter launcher and its pipe-holding child when the probe is cancelled",
|
||||
async () => {
|
||||
const root = await createTestTempDirectory("rea-apktool-probe-");
|
||||
const pids = join(root, "pids.json");
|
||||
const launcher = join(root, "apktool");
|
||||
await writeFile(
|
||||
launcher,
|
||||
[
|
||||
`#!${process.execPath}`,
|
||||
'const { spawn } = require("node:child_process");',
|
||||
'const child = spawn(process.execPath, ["-e", "setInterval(() => {}, 1000)"], { stdio: "inherit" });',
|
||||
`require("node:fs").writeFileSync(${JSON.stringify(pids)}, JSON.stringify([process.pid, child.pid]));`,
|
||||
"setInterval(() => {}, 1000);",
|
||||
].join("\n"),
|
||||
);
|
||||
await chmod(launcher, 0o755);
|
||||
const controller = new AbortController();
|
||||
const started = performance.now();
|
||||
const probe = inspectApktoolClient(
|
||||
{ command: launcher, commandSource: "environment" },
|
||||
controller.signal,
|
||||
process.env,
|
||||
).catch((cause: unknown) => cause);
|
||||
try {
|
||||
expect(
|
||||
await waitFor(async () => (await readPids(pids)).length === 2, 10_000),
|
||||
).toBe(true);
|
||||
const launched = await readPids(pids);
|
||||
controller.abort();
|
||||
// A regressed probe never settles; bound the wait so cleanup still runs.
|
||||
const outcome = await Promise.race([
|
||||
probe,
|
||||
delay(15_000).then(() => "still pending"),
|
||||
]);
|
||||
expect(outcome).toBeInstanceOf(AnalysisCancelledError);
|
||||
expect(performance.now() - started).toBeLessThan(15_000);
|
||||
expect(
|
||||
await waitFor(
|
||||
async () =>
|
||||
(await Promise.all(launched.map(alive))).every((live) => !live),
|
||||
5_000,
|
||||
),
|
||||
).toBe(true);
|
||||
} finally {
|
||||
// A failed assertion must not leave the fixture's processes running.
|
||||
controller.abort();
|
||||
await Promise.race([probe, delay(10_000)]);
|
||||
for (const pid of await readPids(pids))
|
||||
if (await alive(pid))
|
||||
try {
|
||||
process.kill(pid, "SIGKILL");
|
||||
} catch {
|
||||
// Already exited between the check and the signal.
|
||||
}
|
||||
}
|
||||
},
|
||||
30_000,
|
||||
);
|
||||
@@ -0,0 +1,147 @@
|
||||
import { afterEach, expect, it, vi } from "vitest";
|
||||
|
||||
import {
|
||||
AnalysisCancelledError,
|
||||
AnalysisTimeoutError,
|
||||
} from "../domain/analysisErrorCore.js";
|
||||
import { projectAnalysisError } from "../domain/analysisErrorProjection.js";
|
||||
import {
|
||||
OwnedCommandFailure,
|
||||
runOwnedCommand,
|
||||
} from "../process/OwnedCommand.js";
|
||||
import type { ProviderProcessSupervisor } from "../process/ProviderProcess.js";
|
||||
import { ProviderCleanupError } from "../domain/providerCleanupError.js";
|
||||
import {
|
||||
ApktoolConfigurationFailure,
|
||||
inspectApktoolClient,
|
||||
} from "./ApktoolConfiguration.js";
|
||||
import { ApktoolProvider } from "./ApktoolProvider.js";
|
||||
|
||||
vi.mock("../process/OwnedCommand.js", async (importOriginal) => ({
|
||||
...(await importOriginal<typeof import("../process/OwnedCommand.js")>()),
|
||||
runOwnedCommand: vi.fn(),
|
||||
}));
|
||||
afterEach(() => vi.mocked(runOwnedCommand).mockReset());
|
||||
|
||||
const selection = {
|
||||
command: "/opt/apktool/apktool",
|
||||
commandSource: "environment",
|
||||
} as const;
|
||||
const owner = {} as ProviderProcessSupervisor;
|
||||
|
||||
it("runs the version probe as an owned interpreter-script process group", async () => {
|
||||
vi.mocked(runOwnedCommand).mockResolvedValueOnce({
|
||||
stdout: { text: "2.9.3\n" },
|
||||
stderr: { text: "" },
|
||||
} as Awaited<ReturnType<typeof runOwnedCommand>>);
|
||||
await expect(
|
||||
inspectApktoolClient(selection, undefined, {}, "linux"),
|
||||
).resolves.toEqual({ version: "2.9.3" });
|
||||
expect(vi.mocked(runOwnedCommand).mock.lastCall?.[0]).toMatchObject({
|
||||
command: "/opt/apktool/apktool",
|
||||
arguments: ["--version"],
|
||||
expectedCommand: null,
|
||||
});
|
||||
expect(vi.mocked(runOwnedCommand).mock.lastCall?.[1]).toEqual({
|
||||
timeoutMs: 20_000,
|
||||
diagnosticBytes: 64 * 1024,
|
||||
});
|
||||
});
|
||||
|
||||
it("reports an expired version probe as a typed timeout", async () => {
|
||||
vi.mocked(runOwnedCommand).mockRejectedValueOnce(
|
||||
new OwnedCommandFailure("timeout", "Command deadline elapsed."),
|
||||
);
|
||||
const failure = await inspectApktoolClient(
|
||||
selection,
|
||||
undefined,
|
||||
{},
|
||||
"linux",
|
||||
).catch((cause: unknown) => cause);
|
||||
expect(failure).toBeInstanceOf(AnalysisTimeoutError);
|
||||
expect(projectAnalysisError(failure as AnalysisTimeoutError)).toMatchObject({
|
||||
code: "provider_timeout",
|
||||
category: "timeout",
|
||||
details: { timeout_ms: 20_000 },
|
||||
});
|
||||
});
|
||||
|
||||
it("retains an unresolved probe cleanup owner and reports the cleanup", async () => {
|
||||
vi.mocked(runOwnedCommand).mockRejectedValueOnce(
|
||||
new OwnedCommandFailure(
|
||||
"timeout",
|
||||
"Command deadline elapsed.",
|
||||
null,
|
||||
"cleanup incomplete",
|
||||
undefined,
|
||||
["pid:1"],
|
||||
owner,
|
||||
),
|
||||
);
|
||||
const retained: ProviderProcessSupervisor[] = [];
|
||||
const failure = await inspectApktoolClient(
|
||||
selection,
|
||||
undefined,
|
||||
{},
|
||||
"linux",
|
||||
(cleanupOwner) => retained.push(cleanupOwner),
|
||||
).catch((cause: unknown) => cause);
|
||||
expect(failure).toBeInstanceOf(AnalysisTimeoutError);
|
||||
expect(projectAnalysisError(failure as AnalysisTimeoutError)).toMatchObject({
|
||||
code: "cleanup_incomplete",
|
||||
});
|
||||
expect(retained).toEqual([owner]);
|
||||
});
|
||||
|
||||
it("reports a cancelled version probe as cancelled", async () => {
|
||||
vi.mocked(runOwnedCommand).mockRejectedValueOnce(
|
||||
new OwnedCommandFailure("cancelled", "Command cancelled."),
|
||||
);
|
||||
await expect(
|
||||
inspectApktoolClient(selection, undefined, {}, "linux"),
|
||||
).rejects.toBeInstanceOf(AnalysisCancelledError);
|
||||
});
|
||||
|
||||
it("keeps unresolved probe cleanup typed when the launcher overflows its output", async () => {
|
||||
const overflow = () =>
|
||||
new OwnedCommandFailure(
|
||||
"output-limit",
|
||||
"Command diagnostic output exceeded its complete-output budget.",
|
||||
null,
|
||||
"process group still running",
|
||||
undefined,
|
||||
["pid:7", "process-group:7"],
|
||||
owner,
|
||||
);
|
||||
vi.mocked(runOwnedCommand).mockRejectedValueOnce(overflow());
|
||||
const failure = await inspectApktoolClient(
|
||||
selection,
|
||||
undefined,
|
||||
{},
|
||||
"linux",
|
||||
).catch((cause: unknown) => cause);
|
||||
expect(failure).toBeInstanceOf(ApktoolConfigurationFailure);
|
||||
expect((failure as ApktoolConfigurationFailure).cleanup).toEqual({
|
||||
reason: "process group still running",
|
||||
resources: ["pid:7", "process-group:7"],
|
||||
});
|
||||
|
||||
// Both provider entry points report the unresolved processes as cleanup.
|
||||
const provider = new ApktoolProvider({
|
||||
environment: { REA_APKTOOL_COMMAND: "/opt/apktool/apktool" },
|
||||
});
|
||||
vi.mocked(runOwnedCommand).mockRejectedValueOnce(overflow());
|
||||
await expect(provider.inspectAvailability()).rejects.toBeInstanceOf(
|
||||
ProviderCleanupError,
|
||||
);
|
||||
vi.mocked(runOwnedCommand).mockRejectedValueOnce(overflow());
|
||||
const result = await provider.execute({
|
||||
operation: "decode_android_resources",
|
||||
input: {},
|
||||
} as never);
|
||||
expect(result.ok ? null : result.error).toBeInstanceOf(ProviderCleanupError);
|
||||
expect(result.ok ? null : projectAnalysisError(result.error)).toMatchObject({
|
||||
code: "cleanup_incomplete",
|
||||
details: { resources: ["pid:7", "process-group:7"] },
|
||||
});
|
||||
});
|
||||
@@ -1,5 +1,6 @@
|
||||
import { CATALOG_IDENTITY } from "../catalogIdentity.js";
|
||||
import { PRODUCT_IDENTITY } from "../identity.js";
|
||||
import { ok } from "../domain/result.js";
|
||||
import type { DoctorHost } from "./Doctor.js";
|
||||
|
||||
/** Healthy recording host for focused doctor and projection tests. */
|
||||
@@ -14,7 +15,16 @@ export const createDoctorHostFixture = (
|
||||
linuxDistribution: () => Promise.resolve(undefined),
|
||||
validTarget: (path) => Promise.resolve(path.includes("Hopper")),
|
||||
executable: (path) => Promise.resolve(path.includes("Hopper")),
|
||||
supportedLinuxHopper: () => Promise.resolve(true),
|
||||
inspectLinuxHopper: () =>
|
||||
Promise.resolve(
|
||||
ok({
|
||||
sha256: "0".repeat(64),
|
||||
version: "fixture",
|
||||
primaryFingerprint: "0".repeat(40),
|
||||
signingFingerprint: "1".repeat(40),
|
||||
source: "signed_package_cache" as const,
|
||||
}),
|
||||
),
|
||||
linuxDemoRuntimeCheck: () =>
|
||||
Promise.resolve({
|
||||
name: "hopper-demo-runtime",
|
||||
|
||||
@@ -4,6 +4,8 @@ import { SUPPORTED_NODE_VERSION_PROSE } from "../domain/runtimeVersion.js";
|
||||
import type { ClientRegistrationStatus } from "./ClientRegistrationStatus.js";
|
||||
import { createDoctorHostFixture as host } from "./Doctor.fixture.js";
|
||||
import { runDoctor } from "./Doctor.js";
|
||||
import { err } from "../domain/result.js";
|
||||
import { HopperReleaseError } from "./HopperReleaseFeed.js";
|
||||
|
||||
describe("doctor runtime check", () => {
|
||||
it("reports Node 25 as unsupported", async () => {
|
||||
@@ -617,12 +619,20 @@ describe("doctor Linux and Hopper discovery", () => {
|
||||
hopperSetupSupported: true,
|
||||
}),
|
||||
executable: (candidate) => Promise.resolve(candidate === path),
|
||||
supportedLinuxHopper: () => Promise.resolve(false),
|
||||
inspectLinuxHopper: () =>
|
||||
Promise.resolve(
|
||||
err(
|
||||
new HopperReleaseError(
|
||||
"unsupported_hopper_build",
|
||||
"Unverified fixture build",
|
||||
),
|
||||
),
|
||||
),
|
||||
}),
|
||||
);
|
||||
expect(
|
||||
result.checks.find(({ name }) => name === "hopper-version")?.remediation,
|
||||
).toContain("Unset or update HOPPER_LAUNCHER_PATH");
|
||||
).toContain("Check HOPPER_LAUNCHER_PATH");
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -19,11 +19,14 @@ import {
|
||||
macHopperRenamedLauncherCandidates,
|
||||
} from "./MacHopperPaths.js";
|
||||
import {
|
||||
linuxHopperBinarySupported,
|
||||
inspectLinuxHopperLauncher,
|
||||
linuxSharedLibrariesAvailable,
|
||||
readLinuxDistribution,
|
||||
type LinuxDistribution,
|
||||
} from "./LinuxHopper.js";
|
||||
import type { Result } from "../domain/result.js";
|
||||
import type { HopperReleaseError } from "./HopperReleaseFeed.js";
|
||||
import type { VerifiedLinuxHopperLauncher } from "./LinuxHopperPublisher.js";
|
||||
import {
|
||||
readInstalledSkillIdentity,
|
||||
type InstalledSkillIdentity,
|
||||
@@ -101,7 +104,9 @@ export interface DoctorHost {
|
||||
/** Route-specific target admission details, when supplied by the host. */
|
||||
inspectTarget?(path: string): Promise<DoctorCheck>;
|
||||
executable(path: string): Promise<boolean>;
|
||||
supportedLinuxHopper(path: string): Promise<boolean>;
|
||||
inspectLinuxHopper(
|
||||
path: string,
|
||||
): Promise<Result<VerifiedLinuxHopperLauncher, HopperReleaseError>>;
|
||||
linuxDemoRuntimeCheck(): Promise<DoctorCheck>;
|
||||
brewHopperPath(): Promise<string | undefined>;
|
||||
manualHopperPaths(): Promise<readonly string[]>;
|
||||
@@ -512,7 +517,7 @@ export const systemDoctorHost = (
|
||||
),
|
||||
linuxDemoRuntimeCheck:
|
||||
options.linuxDemoRuntimeCheck ?? uncomposedLinuxDemoRuntimeCheck,
|
||||
supportedLinuxHopper: linuxHopperBinarySupported,
|
||||
inspectLinuxHopper: (path) => inspectLinuxHopperLauncher(path, environment),
|
||||
async brewHopperPath() {
|
||||
return probeHomebrew(async (command) => {
|
||||
let caskroom: string;
|
||||
|
||||
@@ -209,17 +209,34 @@ const optionalHopperVersionCheck = async (
|
||||
hopperPath: string | undefined,
|
||||
): Promise<DoctorCheck | undefined> => {
|
||||
if (host.platform !== "linux" || hopperPath === undefined) return undefined;
|
||||
return check(
|
||||
"hopper-version",
|
||||
await host.supportedLinuxHopper(hopperPath),
|
||||
hopperPath,
|
||||
{
|
||||
remediation: unsupportedHopperRemediation(
|
||||
host.configuredHopperPath === hopperPath,
|
||||
),
|
||||
classification: "config_drift",
|
||||
},
|
||||
);
|
||||
const inspected = await host.inspectLinuxHopper(hopperPath);
|
||||
const buildCheck = check("hopper-version", inspected.ok, hopperPath, {
|
||||
remediation: unsupportedHopperRemediation(
|
||||
host.configuredHopperPath === hopperPath,
|
||||
),
|
||||
classification:
|
||||
!inspected.ok &&
|
||||
[
|
||||
"release_metadata",
|
||||
"download",
|
||||
"verification_unavailable",
|
||||
"cancelled",
|
||||
"cleanup_failed",
|
||||
].includes(inspected.error.reason)
|
||||
? "unknown"
|
||||
: "config_drift",
|
||||
});
|
||||
return {
|
||||
...buildCheck,
|
||||
details: inspected.ok
|
||||
? {
|
||||
reported_release_version: inspected.value.version,
|
||||
launcher_sha256: inspected.value.sha256,
|
||||
primary_fingerprint: inspected.value.primaryFingerprint,
|
||||
source: inspected.value.source,
|
||||
}
|
||||
: { reason: inspected.error.reason, message: inspected.error.message },
|
||||
};
|
||||
};
|
||||
|
||||
const optionalHopperDemoCheck = async (
|
||||
@@ -297,8 +314,8 @@ const providerDoctorChecks = (
|
||||
|
||||
const unsupportedHopperRemediation = (configured: boolean): string =>
|
||||
configured
|
||||
? "Unset or update HOPPER_LAUNCHER_PATH, install the Hopper build supported by this REA release, or update REA."
|
||||
: "Install the Hopper build supported by this REA release, or update REA for a newer Hopper build.";
|
||||
? "Check HOPPER_LAUNCHER_PATH and its publisher verification evidence. Run rea setup to install and retain an authenticated official Hopper package for offline use."
|
||||
: "Run rea setup to install and retain an authenticated official Hopper package for offline use.";
|
||||
|
||||
const parseMajor = (version: string | undefined): number =>
|
||||
Number.parseInt(version?.split(".")[0] ?? "0", 10);
|
||||
|
||||
@@ -4,9 +4,23 @@ export const HOPPER_DOWNLOAD_BYTES = 100_000_000;
|
||||
/** Bound observed response bytes before retaining them, including chunked bodies. */
|
||||
export const downloadHopperPackage = async (
|
||||
url: string,
|
||||
options: { readonly signal?: AbortSignal } = {},
|
||||
options: {
|
||||
readonly signal?: AbortSignal;
|
||||
readonly maxBytes?: number;
|
||||
readonly timeoutMs?: number;
|
||||
} = {},
|
||||
): Promise<{ readonly ok: boolean; readonly bytes: Uint8Array }> => {
|
||||
const deadline = AbortSignal.timeout(120_000);
|
||||
const timeoutMs = options.timeoutMs ?? 120_000;
|
||||
if (!Number.isSafeInteger(timeoutMs) || timeoutMs < 1 || timeoutMs > 120_000)
|
||||
throw new RangeError("Invalid Hopper response time budget");
|
||||
const deadline = AbortSignal.timeout(timeoutMs);
|
||||
const maxBytes = options.maxBytes ?? HOPPER_DOWNLOAD_BYTES;
|
||||
if (
|
||||
!Number.isSafeInteger(maxBytes) ||
|
||||
maxBytes < 1 ||
|
||||
maxBytes > HOPPER_DOWNLOAD_BYTES
|
||||
)
|
||||
throw new RangeError("Invalid Hopper response byte budget");
|
||||
const response = await fetch(url, {
|
||||
headers: { accept: "application/json", "user-agent": "rea-installer" },
|
||||
signal:
|
||||
@@ -17,7 +31,7 @@ export const downloadHopperPackage = async (
|
||||
const refused = { ok: false, bytes: new Uint8Array() };
|
||||
if (
|
||||
!response.ok ||
|
||||
Number(response.headers.get("content-length")) > HOPPER_DOWNLOAD_BYTES
|
||||
Number(response.headers.get("content-length")) > maxBytes
|
||||
) {
|
||||
await response.body?.cancel();
|
||||
return refused;
|
||||
@@ -30,7 +44,7 @@ export const downloadHopperPackage = async (
|
||||
while (true) {
|
||||
const chunk = await reader.read();
|
||||
if (chunk.done) return { ok: true, bytes: Buffer.concat(chunks, bytes) };
|
||||
if (chunk.value.byteLength > HOPPER_DOWNLOAD_BYTES - bytes) {
|
||||
if (chunk.value.byteLength > maxBytes - bytes) {
|
||||
await reader.cancel();
|
||||
return refused;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { parseLinuxHopperReleases } from "./HopperReleaseFeed.js";
|
||||
|
||||
const release = {
|
||||
version: "99.2.1",
|
||||
filename:
|
||||
"https://www.hopperapp.com:443/downloader/public/Hopper-99.2.1-Linux-demo.deb",
|
||||
file_length: "123",
|
||||
file_hash: "a".repeat(40),
|
||||
main_exe_hash: "b".repeat(64),
|
||||
signature: "AQ==",
|
||||
public: "1",
|
||||
beta: "0",
|
||||
published_at: "preserved vendor field",
|
||||
};
|
||||
|
||||
describe("Linux Hopper release feed boundary", () => {
|
||||
it("preserves future release identity and partial platform facts", () => {
|
||||
const parsed = parseLinuxHopperReleases({
|
||||
"Ubuntu / Mint": release,
|
||||
"OS X": { signature: "Sparkle DSA" },
|
||||
});
|
||||
expect(parsed).toMatchObject({
|
||||
ok: true,
|
||||
value: {
|
||||
deb: { ok: true, value: release },
|
||||
rpm: { ok: false },
|
||||
arch: { ok: false },
|
||||
},
|
||||
});
|
||||
if (!parsed.ok || !parsed.value.deb.ok) throw new Error("Missing release");
|
||||
expect(parsed.value.deb.value.filename).toBe(release.filename);
|
||||
});
|
||||
|
||||
it.each([
|
||||
{ signature: undefined },
|
||||
{ signature: "AQ==garbage" },
|
||||
{ main_exe_hash: "b".repeat(40) },
|
||||
{ file_length: "100000001" },
|
||||
{ file_length: "NaN" },
|
||||
{ public: "0" },
|
||||
{ beta: "1" },
|
||||
{
|
||||
filename:
|
||||
"https://other.example/downloader/public/Hopper-99.2.1-Linux-demo.deb",
|
||||
},
|
||||
{ filename: release.filename + "?token=caller" },
|
||||
{
|
||||
filename: release.filename.replace("https://", "https://user:password@"),
|
||||
},
|
||||
{ filename: release.filename.replace(".deb", ".rpm") },
|
||||
{ filename: release.filename.replace("demo.deb", "full.deb") },
|
||||
])(
|
||||
"rejects metadata outside the selected public package contract: %j",
|
||||
(changes) => {
|
||||
expect(
|
||||
parseLinuxHopperReleases({
|
||||
"Ubuntu / Mint": { ...release, ...changes },
|
||||
}),
|
||||
).toMatchObject({
|
||||
ok: true,
|
||||
value: { deb: { ok: false, error: { reason: "release_metadata" } } },
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
it.each([null, [], "html error page"])(
|
||||
"keeps malformed feed roots unknown",
|
||||
(raw) => {
|
||||
expect(parseLinuxHopperReleases(raw)).toMatchObject({
|
||||
ok: false,
|
||||
error: { reason: "release_metadata" },
|
||||
});
|
||||
},
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,125 @@
|
||||
import { z } from "zod";
|
||||
|
||||
import { err, ok, type Result } from "../domain/result.js";
|
||||
import { HOPPER_DOWNLOAD_BYTES } from "./HopperDownload.js";
|
||||
|
||||
/** Vendor endpoints, shared by installation and existing-build inspection. */
|
||||
export const HOPPER_RELEASES_URL =
|
||||
"https://www.hopperapp.com/include/files-api.php?request=releases&public=true";
|
||||
export const HOPPER_PUBLIC_KEY_URL =
|
||||
"https://www.hopperapp.com/hopper-public-key.asc";
|
||||
export const HOPPER_DOWNLOAD_PREFIX =
|
||||
"https://www.hopperapp.com:443/downloader/public/";
|
||||
|
||||
/** Stable publisher identity; signing subkeys are admitted through this primary. */
|
||||
export const HOPPER_LINUX_PRIMARY_FINGERPRINT =
|
||||
"7C2D73BAFAF5B9A710A69DA1A26DF815099B1BCD";
|
||||
|
||||
export type LinuxPackageFamily = "deb" | "rpm" | "arch";
|
||||
const platforms = {
|
||||
deb: "Ubuntu / Mint",
|
||||
rpm: "Fedora",
|
||||
arch: "Arch",
|
||||
} as const;
|
||||
const suffixes = { deb: ".deb", rpm: ".rpm", arch: ".pkg.tar.xz" } as const;
|
||||
|
||||
export class HopperReleaseError extends Error {
|
||||
readonly _tag = "HopperReleaseError";
|
||||
constructor(
|
||||
readonly reason:
|
||||
| "release_metadata"
|
||||
| "download"
|
||||
| "integrity"
|
||||
| "publisher_verification_failed"
|
||||
| "verification_unavailable"
|
||||
| "unsupported_hopper_build"
|
||||
| "cancelled"
|
||||
| "cleanup_failed"
|
||||
| "cache_missing",
|
||||
message: string,
|
||||
options?: ErrorOptions,
|
||||
) {
|
||||
super(message, options);
|
||||
this.name = this._tag;
|
||||
}
|
||||
}
|
||||
|
||||
const sha256 = z.string().regex(/^[a-f0-9]{64}$/u);
|
||||
const releaseSchema = z
|
||||
.object({
|
||||
version: z.string().min(1),
|
||||
filename: z.string().url(),
|
||||
file_length: z
|
||||
.string()
|
||||
.regex(/^[1-9]\d*$/u)
|
||||
.refine((text) => {
|
||||
const count = Number(text);
|
||||
return Number.isSafeInteger(count) && count <= HOPPER_DOWNLOAD_BYTES;
|
||||
}),
|
||||
file_hash: z.string().regex(/^[a-f0-9]{40}$/u),
|
||||
main_exe_hash: sha256,
|
||||
signature: z
|
||||
.string()
|
||||
.min(1)
|
||||
.refine(
|
||||
(text) => Buffer.from(text, "base64").toString("base64") === text,
|
||||
),
|
||||
public: z.literal("1"),
|
||||
beta: z.literal("0"),
|
||||
})
|
||||
.passthrough();
|
||||
|
||||
/** Parsed once at the feed boundary; preserve the vendor's complete release row. */
|
||||
export type LinuxHopperRelease = z.infer<typeof releaseSchema>;
|
||||
export type LinuxHopperReleaseFeed = Readonly<
|
||||
Record<LinuxPackageFamily, Result<LinuxHopperRelease, HopperReleaseError>>
|
||||
>;
|
||||
|
||||
export const parseLinuxHopperRelease = (
|
||||
raw: unknown,
|
||||
family: LinuxPackageFamily,
|
||||
): Result<LinuxHopperRelease, HopperReleaseError> => {
|
||||
const parsed = releaseSchema.safeParse(raw);
|
||||
if (!parsed.success)
|
||||
return err(
|
||||
new HopperReleaseError(
|
||||
"release_metadata",
|
||||
"The Hopper release omitted valid package identity or signature metadata.",
|
||||
),
|
||||
);
|
||||
const url = new URL(parsed.data.filename);
|
||||
if (
|
||||
url.origin !== "https://www.hopperapp.com" ||
|
||||
url.username !== "" ||
|
||||
url.password !== "" ||
|
||||
url.search !== "" ||
|
||||
url.hash !== "" ||
|
||||
!url.pathname.startsWith("/downloader/public/Hopper-") ||
|
||||
!url.pathname.endsWith(`-Linux-demo${suffixes[family]}`)
|
||||
)
|
||||
return err(
|
||||
new HopperReleaseError(
|
||||
"release_metadata",
|
||||
"The Hopper release is outside the official public Linux demo channel.",
|
||||
),
|
||||
);
|
||||
return ok(parsed.data);
|
||||
};
|
||||
|
||||
export const parseLinuxHopperReleases = (
|
||||
raw: unknown,
|
||||
): Result<LinuxHopperReleaseFeed, HopperReleaseError> => {
|
||||
const feed = z.record(z.string(), z.unknown()).safeParse(raw);
|
||||
if (!feed.success)
|
||||
return err(
|
||||
new HopperReleaseError(
|
||||
"release_metadata",
|
||||
"The Hopper releases response is not an object.",
|
||||
),
|
||||
);
|
||||
return ok({
|
||||
deb: parseLinuxHopperRelease(feed.data[platforms.deb], "deb"),
|
||||
rpm: parseLinuxHopperRelease(feed.data[platforms.rpm], "rpm"),
|
||||
arch: parseLinuxHopperRelease(feed.data[platforms.arch], "arch"),
|
||||
});
|
||||
};
|
||||
@@ -4,7 +4,6 @@ import { describe, expect, it } from "vitest";
|
||||
import {
|
||||
formatCommandForDisplay,
|
||||
installLinuxHopper,
|
||||
linuxHopperLauncherDigestSupported,
|
||||
linuxPackageManagerCommands,
|
||||
linuxSharedLibrariesAvailable,
|
||||
parseLinuxDistribution,
|
||||
@@ -18,6 +17,12 @@ import {
|
||||
type LinuxPackageInstallFailure,
|
||||
type LinuxPackageInstallOutcome,
|
||||
} from "./LinuxHopper.js";
|
||||
import { err, ok } from "../domain/result.js";
|
||||
import {
|
||||
HopperReleaseError,
|
||||
type LinuxHopperRelease,
|
||||
} from "./HopperReleaseFeed.js";
|
||||
import type { VerifiedLinuxHopperPackage } from "./LinuxHopperPublisher.js";
|
||||
|
||||
describe("Linux package-manager command display", () => {
|
||||
it("preserves argument boundaries for paths with spaces and quotes", () => {
|
||||
@@ -43,6 +48,9 @@ class RecordingLinuxHost implements LinuxHopperInstallHost {
|
||||
archiveOk = true;
|
||||
installSucceeds = true;
|
||||
cleanupFails = false;
|
||||
signatureValid = true;
|
||||
cacheWritable = true;
|
||||
verifiedBeforeInstall = false;
|
||||
packageFailure: LinuxPackageInstallFailure = {
|
||||
command: "pacman -U --noconfirm /tmp/rea test/hopper.arch",
|
||||
exitCode: 1,
|
||||
@@ -57,6 +65,8 @@ class RecordingLinuxHost implements LinuxHopperInstallHost {
|
||||
|
||||
distribution = (): Promise<LinuxDistribution | undefined> =>
|
||||
Promise.resolve(this.distributionValue);
|
||||
release = (family: LinuxPackageFamily) =>
|
||||
Promise.resolve(ok(releasesFor(new Uint8Array([1, 2, 3]))[family]));
|
||||
download = (url: string): Promise<LinuxHopperDownload> => {
|
||||
this.downloads.push(url);
|
||||
return Promise.resolve({ ok: this.archiveOk, bytes: this.archive });
|
||||
@@ -64,10 +74,52 @@ class RecordingLinuxHost implements LinuxHopperInstallHost {
|
||||
createTemporaryDirectory = (): Promise<string> =>
|
||||
Promise.resolve("/tmp/rea test");
|
||||
writeArchive = (): Promise<void> => Promise.resolve();
|
||||
verifyArchive = (family: LinuxPackageFamily, release: LinuxHopperRelease) => {
|
||||
if (
|
||||
createHash("sha1").update(this.archive).digest("hex") !==
|
||||
release.file_hash
|
||||
)
|
||||
return Promise.resolve(
|
||||
err(new HopperReleaseError("integrity", "Fixture byte mismatch")),
|
||||
);
|
||||
if (!this.signatureValid)
|
||||
return Promise.resolve(
|
||||
err(
|
||||
new HopperReleaseError(
|
||||
"publisher_verification_failed",
|
||||
"Fixture signature rejection",
|
||||
),
|
||||
),
|
||||
);
|
||||
this.verifiedBeforeInstall = true;
|
||||
return Promise.resolve(
|
||||
ok({
|
||||
family,
|
||||
release,
|
||||
packageSha256: createHash("sha256").update(this.archive).digest("hex"),
|
||||
publicKey: "fixture",
|
||||
primaryFingerprint: "0".repeat(40),
|
||||
signingFingerprint: "1".repeat(40),
|
||||
}),
|
||||
);
|
||||
};
|
||||
retainVerifiedArchive = (_verified: VerifiedLinuxHopperPackage) =>
|
||||
Promise.resolve(
|
||||
this.cacheWritable
|
||||
? ok(undefined)
|
||||
: err(
|
||||
new HopperReleaseError(
|
||||
"verification_unavailable",
|
||||
"Fixture cache permission rejection",
|
||||
),
|
||||
),
|
||||
);
|
||||
installPackage = (
|
||||
family: LinuxPackageFamily,
|
||||
archive: string,
|
||||
): Promise<LinuxPackageInstallOutcome> => {
|
||||
if (!this.verifiedBeforeInstall)
|
||||
throw new Error("Package manager ran before publisher verification");
|
||||
this.installed.push({ family, archive });
|
||||
const outcome: LinuxPackageInstallOutcome = this.installSucceeds
|
||||
? { status: "installed" }
|
||||
@@ -160,20 +212,6 @@ describe("Linux Hopper host classification", () => {
|
||||
});
|
||||
|
||||
describe("Linux Hopper installation", () => {
|
||||
it.each([
|
||||
[
|
||||
"Debian and Arch",
|
||||
"0294ced141cc373468ee22d8343e7dac41980cb05a937994ca81c9f09afe7ded",
|
||||
],
|
||||
["RPM", "1339f9e58377442b0c6fcb0dfc3cec20d593cc557408521fad9a00dbc6b8da13"],
|
||||
])("accepts the pinned %s launcher build", (_, digest) => {
|
||||
expect(linuxHopperLauncherDigestSupported(digest)).toBe(true);
|
||||
});
|
||||
|
||||
it("rejects an unverified launcher build", () => {
|
||||
expect(linuxHopperLauncherDigestSupported("0".repeat(64))).toBe(false);
|
||||
});
|
||||
|
||||
it.each([
|
||||
["deb", "apt-get", ["xvfb", "xauth", "python3", "libx11-6", "libxtst6"]],
|
||||
[
|
||||
@@ -239,6 +277,23 @@ describe("Linux Hopper package installation", () => {
|
||||
});
|
||||
|
||||
describe("Linux Hopper installation failure classification", () => {
|
||||
it.each([
|
||||
["signatureValid", "publisher_verification_failed"],
|
||||
["cacheWritable", "verification_unavailable"],
|
||||
] as const)(
|
||||
"stops before privileged installation when %s fails",
|
||||
async (field, reason) => {
|
||||
const host = new RecordingLinuxHost();
|
||||
host[field] = false;
|
||||
expect(await installFixture(host)).toMatchObject({
|
||||
status: "failed",
|
||||
reason,
|
||||
verificationFailure: { reason, message: expect.any(String) },
|
||||
});
|
||||
expect(host.installed).toEqual([]);
|
||||
expect(host.cleaned).toEqual(["/tmp/rea test"]);
|
||||
},
|
||||
);
|
||||
it.each([
|
||||
["unsupported host", "unsupported_host"],
|
||||
["package request failure", "download"],
|
||||
@@ -263,7 +318,7 @@ describe("Linux Hopper installation failure classification", () => {
|
||||
host.launcherStatusValue = "runtime_dependencies";
|
||||
if (scenario === "unsupported build")
|
||||
host.launcherStatusValue = "unsupported_hopper_build";
|
||||
expect(await installFixture(host)).toEqual({
|
||||
expect(await installFixture(host)).toMatchObject({
|
||||
status: "failed",
|
||||
reason,
|
||||
...(scenario === "package manager failure"
|
||||
@@ -363,14 +418,16 @@ describe("Linux Hopper temporary cleanup ownership", () => {
|
||||
|
||||
function releasesFor(
|
||||
bytes: Uint8Array,
|
||||
): Record<
|
||||
LinuxPackageFamily,
|
||||
{ filename: string; file_length: string; file_hash: string }
|
||||
> {
|
||||
): Record<LinuxPackageFamily, LinuxHopperRelease> {
|
||||
const release = (extension: string) => ({
|
||||
filename: `https://www.hopperapp.com:443/downloader/public/Hopper.${extension}`,
|
||||
version: "future-fixture",
|
||||
filename: `https://www.hopperapp.com:443/downloader/public/Hopper-future-fixture-Linux-demo.${extension}`,
|
||||
file_length: String(bytes.byteLength),
|
||||
file_hash: createHash("sha1").update(bytes).digest("hex"),
|
||||
main_exe_hash: "0".repeat(64),
|
||||
signature: "AQ==",
|
||||
public: "1" as const,
|
||||
beta: "0" as const,
|
||||
});
|
||||
return {
|
||||
deb: release("deb"),
|
||||
@@ -379,7 +436,4 @@ function releasesFor(
|
||||
};
|
||||
}
|
||||
|
||||
const installFixture = (host: RecordingLinuxHost) =>
|
||||
installLinuxHopper(host, {
|
||||
releases: releasesFor(new Uint8Array([1, 2, 3])),
|
||||
});
|
||||
const installFixture = (host: RecordingLinuxHost) => installLinuxHopper(host);
|
||||
+175
-102
@@ -1,55 +1,37 @@
|
||||
import { createHash } from "node:crypto";
|
||||
import { createReadStream } from "node:fs";
|
||||
import { access, mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
|
||||
import { execFileBoundedOutput } from "../process/ExecFileBoundedOutput.js";
|
||||
import { execFileOutput } from "../process/ExecFileOutput.js";
|
||||
import { downloadHopperPackage } from "./HopperDownload.js";
|
||||
import { hashStableFile } from "../filesystem/StableFileHash.js";
|
||||
import { err, type Result } from "../domain/result.js";
|
||||
import {
|
||||
downloadHopperPackage,
|
||||
HOPPER_DOWNLOAD_BYTES,
|
||||
} from "./HopperDownload.js";
|
||||
HOPPER_RELEASES_URL,
|
||||
HOPPER_PUBLIC_KEY_URL,
|
||||
HOPPER_DOWNLOAD_PREFIX,
|
||||
HOPPER_LINUX_PRIMARY_FINGERPRINT,
|
||||
HopperReleaseError,
|
||||
type LinuxHopperRelease,
|
||||
type LinuxPackageFamily,
|
||||
} from "./HopperReleaseFeed.js";
|
||||
import {
|
||||
LinuxHopperPublisher,
|
||||
retryLinuxHopperPublisherCleanup,
|
||||
type VerifiedLinuxHopperPackage,
|
||||
type VerifiedLinuxHopperLauncher,
|
||||
} from "./LinuxHopperPublisher.js";
|
||||
|
||||
export type { LinuxPackageFamily } from "./HopperReleaseFeed.js";
|
||||
|
||||
/** Package-manager output retained per stream without stopping the command. */
|
||||
const PACKAGE_MANAGER_OUTPUT_BYTES = 64 * 1024;
|
||||
const DOWNLOAD_PREFIX = "https://www.hopperapp.com:443/downloader/public/";
|
||||
const SUPPORTED_HOPPER_SHA256 = new Set([
|
||||
"0294ced141cc373468ee22d8343e7dac41980cb05a937994ca81c9f09afe7ded",
|
||||
"1339f9e58377442b0c6fcb0dfc3cec20d593cc557408521fad9a00dbc6b8da13",
|
||||
]);
|
||||
interface LinuxHopperRelease {
|
||||
readonly filename: string;
|
||||
readonly file_length: string;
|
||||
readonly file_hash: string;
|
||||
}
|
||||
const SUPPORTED_RELEASES: Readonly<
|
||||
Record<LinuxPackageFamily, LinuxHopperRelease>
|
||||
> = {
|
||||
deb: {
|
||||
filename: `${DOWNLOAD_PREFIX}Hopper-6.4.2-Linux-demo.deb`,
|
||||
file_length: "35755772",
|
||||
file_hash: "e4f79dff602648a8ff4a88b773875b6bfac0dc65",
|
||||
},
|
||||
rpm: {
|
||||
filename: `${DOWNLOAD_PREFIX}Hopper-6.4.2-Linux-demo.rpm`,
|
||||
file_length: "32264475",
|
||||
file_hash: "b9a65f2f583a386d440d4401d9140aef3bf305de",
|
||||
},
|
||||
arch: {
|
||||
filename: `${DOWNLOAD_PREFIX}Hopper-6.4.2-Linux-demo.pkg.tar.xz`,
|
||||
file_length: "29405968",
|
||||
file_hash: "92d780b61cab38eda95dc4112dee4fbeb0920f46",
|
||||
},
|
||||
};
|
||||
|
||||
export type LinuxPackageFamily = "deb" | "rpm" | "arch";
|
||||
|
||||
/** Exact stable external effects disclosed before a Linux Hopper installation. */
|
||||
export interface LinuxHopperInstallDisclosure {
|
||||
readonly downloadUrl: string;
|
||||
readonly expectedBytes: number;
|
||||
readonly expectedSha1: string;
|
||||
readonly networkOrigins: readonly string[];
|
||||
readonly integrity: string;
|
||||
readonly commands: readonly string[];
|
||||
}
|
||||
|
||||
@@ -58,11 +40,13 @@ export const linuxHopperInstallDisclosure = (
|
||||
family: LinuxPackageFamily,
|
||||
isRoot: boolean,
|
||||
): LinuxHopperInstallDisclosure => {
|
||||
const release = SUPPORTED_RELEASES[family];
|
||||
return {
|
||||
downloadUrl: release.filename,
|
||||
expectedBytes: Number(release.file_length),
|
||||
expectedSha1: release.file_hash,
|
||||
networkOrigins: [
|
||||
HOPPER_RELEASES_URL,
|
||||
HOPPER_PUBLIC_KEY_URL,
|
||||
HOPPER_DOWNLOAD_PREFIX,
|
||||
],
|
||||
integrity: `Detached PGP signature · primary ${HOPPER_LINUX_PRIMARY_FINGERPRINT} · package byte identity and installed launcher SHA-256 from the official feed`,
|
||||
commands: linuxPackageManagerCommands(
|
||||
family,
|
||||
`<verified-hopper.${family}>`,
|
||||
@@ -133,6 +117,10 @@ export type LinuxPackageInstallOutcome =
|
||||
/** External effects required to install the official Linux package. */
|
||||
export interface LinuxHopperInstallHost {
|
||||
distribution(): Promise<LinuxDistribution | undefined>;
|
||||
release(
|
||||
family: LinuxPackageFamily,
|
||||
signal?: AbortSignal,
|
||||
): Promise<Result<LinuxHopperRelease, HopperReleaseError>>;
|
||||
download(
|
||||
url: string,
|
||||
options: { readonly signal?: AbortSignal },
|
||||
@@ -143,8 +131,23 @@ export interface LinuxHopperInstallHost {
|
||||
family: LinuxPackageFamily,
|
||||
archive: string,
|
||||
): Promise<LinuxPackageInstallOutcome>;
|
||||
launcherStatus(path: string): Promise<LinuxHopperLauncherStatus>;
|
||||
verifyArchive(
|
||||
family: LinuxPackageFamily,
|
||||
release: LinuxHopperRelease,
|
||||
archive: string,
|
||||
signal?: AbortSignal,
|
||||
): Promise<Result<VerifiedLinuxHopperPackage, HopperReleaseError>>;
|
||||
retainVerifiedArchive(
|
||||
verified: VerifiedLinuxHopperPackage,
|
||||
archive: string,
|
||||
signal?: AbortSignal,
|
||||
): Promise<Result<void, HopperReleaseError>>;
|
||||
launcherStatus(
|
||||
path: string,
|
||||
expectedSha256: string,
|
||||
): Promise<LinuxHopperLauncherStatus>;
|
||||
cleanup(path: string): Promise<void>;
|
||||
close?(): Promise<void>;
|
||||
}
|
||||
|
||||
/** Structured outcome for every expected Linux installation failure. */
|
||||
@@ -163,6 +166,9 @@ export type LinuxHopperInstallResult =
|
||||
| "launcher_missing"
|
||||
| "runtime_dependencies"
|
||||
| "unsupported_hopper_build"
|
||||
| "publisher_verification_failed"
|
||||
| "verification_unavailable"
|
||||
| "cleanup_failed"
|
||||
| "cancelled";
|
||||
/**
|
||||
* The failing package-manager command and its output. Present only when
|
||||
@@ -170,6 +176,8 @@ export type LinuxHopperInstallResult =
|
||||
* package manager said instead of assuming an authorization problem.
|
||||
*/
|
||||
readonly packageFailure?: LinuxPackageInstallFailure;
|
||||
/** Publisher-owned failure, retained for the public setup diagnostic. */
|
||||
readonly verificationFailure?: HopperReleaseError;
|
||||
} & { readonly cleanupFailure?: LinuxHopperTemporaryCleanupFailure });
|
||||
|
||||
/** Failed removal of an owned temporary install directory. */
|
||||
@@ -270,9 +278,6 @@ export const installLinuxHopper = async (
|
||||
host: LinuxHopperInstallHost = systemLinuxHopperInstallHost(process.env),
|
||||
options: {
|
||||
readonly signal?: AbortSignal;
|
||||
readonly releases?: Readonly<
|
||||
Record<LinuxPackageFamily, LinuxHopperRelease>
|
||||
>;
|
||||
} = {},
|
||||
): Promise<LinuxHopperInstallResult> => {
|
||||
if (options.signal?.aborted === true)
|
||||
@@ -284,16 +289,56 @@ export const installLinuxHopper = async (
|
||||
let result: LinuxHopperInstallResult;
|
||||
try {
|
||||
result = await (async (): Promise<LinuxHopperInstallResult> => {
|
||||
const release = (options.releases ?? SUPPORTED_RELEASES)[
|
||||
distribution.packageFamily
|
||||
];
|
||||
const selected = await host.release(
|
||||
distribution.packageFamily,
|
||||
options.signal,
|
||||
);
|
||||
if (!selected.ok)
|
||||
return {
|
||||
status: "failed",
|
||||
verificationFailure: selected.error,
|
||||
reason:
|
||||
selected.error.reason === "cancelled"
|
||||
? "cancelled"
|
||||
: "release_metadata",
|
||||
};
|
||||
const release = selected.value;
|
||||
const archiveDownload = await host.download(release.filename, options);
|
||||
if (!archiveDownload.ok) return { status: "failed", reason: "download" };
|
||||
if (!packageIntegrityMatches(archiveDownload.bytes, release))
|
||||
return { status: "failed", reason: "integrity" };
|
||||
temporary = await host.createTemporaryDirectory();
|
||||
const archive = join(temporary, `hopper.${distribution.packageFamily}`);
|
||||
await host.writeArchive(archive, archiveDownload.bytes);
|
||||
const verified = await host.verifyArchive(
|
||||
distribution.packageFamily,
|
||||
release,
|
||||
archive,
|
||||
options.signal,
|
||||
);
|
||||
if (!verified.ok)
|
||||
return {
|
||||
status: "failed",
|
||||
verificationFailure: verified.error,
|
||||
reason:
|
||||
verified.error.reason === "cache_missing"
|
||||
? "verification_unavailable"
|
||||
: verified.error.reason,
|
||||
};
|
||||
const retained = await host.retainVerifiedArchive(
|
||||
verified.value,
|
||||
archive,
|
||||
options.signal,
|
||||
);
|
||||
if (!retained.ok)
|
||||
return {
|
||||
status: "failed",
|
||||
verificationFailure: retained.error,
|
||||
reason:
|
||||
retained.error.reason === "cache_missing"
|
||||
? "verification_unavailable"
|
||||
: retained.error.reason,
|
||||
};
|
||||
if (options.signal?.aborted === true)
|
||||
return { status: "failed", reason: "cancelled" };
|
||||
const install = await host.installPackage(
|
||||
distribution.packageFamily,
|
||||
archive,
|
||||
@@ -305,7 +350,10 @@ export const installLinuxHopper = async (
|
||||
packageFailure: install.failure,
|
||||
};
|
||||
const launcherPath = "/opt/hopper/bin/Hopper";
|
||||
const launcherStatus = await host.launcherStatus(launcherPath);
|
||||
const launcherStatus = await host.launcherStatus(
|
||||
launcherPath,
|
||||
release.main_exe_hash,
|
||||
);
|
||||
if (launcherStatus !== "ready")
|
||||
return {
|
||||
status: "failed",
|
||||
@@ -360,49 +408,82 @@ export const linuxHopperLauncherCandidates = (
|
||||
/** Compose package-manager and linker checks from the selected caller environment. */
|
||||
export const systemLinuxHopperInstallHost = (
|
||||
environment: Readonly<NodeJS.ProcessEnv>,
|
||||
): LinuxHopperInstallHost => ({
|
||||
distribution: readLinuxDistribution,
|
||||
download: downloadHopperPackage,
|
||||
createTemporaryDirectory: () => mkdtemp(join(tmpdir(), "rea-hopper-")),
|
||||
writeArchive: (path, bytes) => writeFile(path, bytes, { mode: 0o600 }),
|
||||
installPackage: (family, archive) =>
|
||||
installSystemPackage(family, archive, environment),
|
||||
async launcherStatus(path) {
|
||||
try {
|
||||
await access(path);
|
||||
const linked = await execFileOutput("ldd", [path], { env: environment });
|
||||
if (!linuxSharedLibrariesAvailable(`${linked.stdout}\n${linked.stderr}`))
|
||||
return "runtime_dependencies";
|
||||
return (await linuxHopperBinarySupported(path))
|
||||
? "ready"
|
||||
: "unsupported_hopper_build";
|
||||
} catch (cause: unknown) {
|
||||
// best-effort cleanup: optional launcher probing; unreadable means missing.
|
||||
void cause;
|
||||
return "missing";
|
||||
}
|
||||
},
|
||||
cleanup: (path) => rm(path, { recursive: true, force: true }),
|
||||
});
|
||||
|
||||
/** Match the Linux Hopper build validated by the packaged demo-dialog adapter. */
|
||||
export const linuxHopperBinarySupported = async (
|
||||
path: string,
|
||||
): Promise<boolean> => {
|
||||
try {
|
||||
const hash = createHash("sha256");
|
||||
for await (const chunk of createReadStream(path)) hash.update(chunk);
|
||||
return linuxHopperLauncherDigestSupported(hash.digest("hex"));
|
||||
} catch (cause: unknown) {
|
||||
// best-effort cleanup: optional build probing; unreadable means unsupported.
|
||||
void cause;
|
||||
return false;
|
||||
}
|
||||
): LinuxHopperInstallHost => {
|
||||
const publisher = systemLinuxHopperPublisher(environment);
|
||||
return {
|
||||
distribution: readLinuxDistribution,
|
||||
release: (family, signal) => publisher.currentRelease(family, signal),
|
||||
download: downloadHopperPackage,
|
||||
createTemporaryDirectory: () => mkdtemp(join(tmpdir(), "rea-hopper-")),
|
||||
writeArchive: (path, bytes) => writeFile(path, bytes, { mode: 0o600 }),
|
||||
installPackage: (family, archive) =>
|
||||
installSystemPackage(family, archive, environment),
|
||||
verifyArchive: (family, release, archive, signal) =>
|
||||
publisher.verifyArchive(family, release, archive, signal),
|
||||
retainVerifiedArchive: (verified, archive, signal) =>
|
||||
publisher.retainVerifiedArchive(verified, archive, signal),
|
||||
async launcherStatus(path, expectedSha256) {
|
||||
try {
|
||||
await access(path);
|
||||
const linked = await execFileOutput("ldd", [path], {
|
||||
env: environment,
|
||||
});
|
||||
if (
|
||||
!linuxSharedLibrariesAvailable(`${linked.stdout}\n${linked.stderr}`)
|
||||
)
|
||||
return "runtime_dependencies";
|
||||
return (await hashStableFile(path)).sha256 === expectedSha256
|
||||
? "ready"
|
||||
: "unsupported_hopper_build";
|
||||
} catch (cause: unknown) {
|
||||
// best-effort cleanup: optional launcher probing; unreadable means missing.
|
||||
void cause;
|
||||
return "missing";
|
||||
}
|
||||
},
|
||||
cleanup: (path) => rm(path, { recursive: true, force: true }),
|
||||
close: async () => {
|
||||
await publisher.close();
|
||||
await retryLinuxHopperPublisherCleanup();
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
/** Match a launcher extracted from one of the pinned official Linux packages. */
|
||||
export const linuxHopperLauncherDigestSupported = (digest: string): boolean =>
|
||||
SUPPORTED_HOPPER_SHA256.has(digest);
|
||||
export const systemLinuxHopperPublisher = (
|
||||
environment: Readonly<NodeJS.ProcessEnv>,
|
||||
): LinuxHopperPublisher =>
|
||||
new LinuxHopperPublisher(
|
||||
environment,
|
||||
async () => (await readLinuxDistribution())?.packageFamily,
|
||||
);
|
||||
|
||||
/** Read-only authentication; persistent package retention belongs to approved setup. */
|
||||
export const inspectLinuxHopperLauncher = async (
|
||||
path: string,
|
||||
environment: Readonly<NodeJS.ProcessEnv>,
|
||||
signal?: AbortSignal,
|
||||
): Promise<Result<VerifiedLinuxHopperLauncher, HopperReleaseError>> => {
|
||||
const publisher = systemLinuxHopperPublisher(environment);
|
||||
const result = await publisher.inspectLauncher(path, signal);
|
||||
try {
|
||||
await publisher.close();
|
||||
await retryLinuxHopperPublisherCleanup();
|
||||
return result;
|
||||
} catch (cause: unknown) {
|
||||
return err(
|
||||
new HopperReleaseError(
|
||||
"cleanup_failed",
|
||||
"Hopper publisher verification retains incomplete cleanup.",
|
||||
{
|
||||
cause: new AggregateError(
|
||||
result.ok ? [cause] : [result.error, cause],
|
||||
"Publisher verification outcomes",
|
||||
),
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
/** Interpret ldd output conservatively so a present but broken launcher is unhealthy. */
|
||||
export const linuxSharedLibrariesAvailable = (output: string): boolean =>
|
||||
@@ -492,14 +573,6 @@ export const linuxPackageManagerCommands = (
|
||||
});
|
||||
};
|
||||
|
||||
const packageIntegrityMatches = (
|
||||
bytes: Uint8Array,
|
||||
release: LinuxHopperRelease,
|
||||
): boolean =>
|
||||
bytes.byteLength === Number(release.file_length) &&
|
||||
bytes.byteLength <= HOPPER_DOWNLOAD_BYTES &&
|
||||
createHash("sha1").update(bytes).digest("hex") === release.file_hash;
|
||||
|
||||
const packageFamilyFor = (
|
||||
id: string,
|
||||
idLike: string | undefined,
|
||||
|
||||
@@ -0,0 +1,833 @@
|
||||
import { createHash, randomUUID } from "node:crypto";
|
||||
import { copyFile, lstat, mkdir, rename, writeFile } from "node:fs/promises";
|
||||
import { isAbsolute, join } from "node:path";
|
||||
|
||||
import { z } from "zod";
|
||||
import {
|
||||
ArtifactResourceScope,
|
||||
type ArtifactResourceOwner,
|
||||
} from "../artifacts/ArtifactResourceScope.js";
|
||||
import { ArtifactReaderFailure } from "../artifacts/ArtifactReader.js";
|
||||
import { homeDirectoryFromEnvironment } from "../config/homeDirectory.js";
|
||||
import { err, ok, type Result } from "../domain/result.js";
|
||||
import { hashStableFile } from "../filesystem/StableFileHash.js";
|
||||
import { readBoundedFileBytes } from "../process/BoundedFileBytes.js";
|
||||
import { verifyDetachedPgpSignature } from "../process/DetachedPgpSignature.js";
|
||||
import { execFileOutput } from "../process/ExecFileOutput.js";
|
||||
import { PrivateRuntimeRoot } from "../process/PrivateRuntimeRoot.js";
|
||||
import { waitForAbortableDelay } from "../process/ProviderDeadline.js";
|
||||
import {
|
||||
acquireHopperReleaseCacheLease,
|
||||
type HopperTargetLease,
|
||||
} from "../process/HopperLease.js";
|
||||
import {
|
||||
downloadHopperPackage,
|
||||
HOPPER_DOWNLOAD_BYTES,
|
||||
} from "./HopperDownload.js";
|
||||
import {
|
||||
HOPPER_LINUX_PRIMARY_FINGERPRINT,
|
||||
HOPPER_PUBLIC_KEY_URL,
|
||||
HOPPER_RELEASES_URL,
|
||||
HopperReleaseError,
|
||||
parseLinuxHopperRelease,
|
||||
parseLinuxHopperReleases,
|
||||
type LinuxHopperRelease,
|
||||
type LinuxPackageFamily,
|
||||
} from "./HopperReleaseFeed.js";
|
||||
import { withRegularFile } from "./RegularFileRead.js";
|
||||
|
||||
/** Metadata/key inspection budget, independent of the compressed package budget. */
|
||||
const RELEASE_METADATA_BYTES = 1024 * 1024;
|
||||
/** Small feed/key requests must not consume the full package-download deadline. */
|
||||
const RELEASE_METADATA_TIMEOUT_MS = 10_000;
|
||||
const cacheRecordSchema = z.object({
|
||||
format: z.literal("rea-signed-hopper-release-v1"),
|
||||
family: z.enum(["deb", "rpm", "arch"]),
|
||||
release: z.unknown(),
|
||||
});
|
||||
|
||||
export interface VerifiedLinuxHopperPackage {
|
||||
readonly release: LinuxHopperRelease;
|
||||
readonly family: LinuxPackageFamily;
|
||||
readonly packageSha256: string;
|
||||
readonly publicKey: string;
|
||||
readonly primaryFingerprint: string;
|
||||
readonly signingFingerprint: string;
|
||||
}
|
||||
|
||||
export interface VerifiedLinuxHopperLauncher {
|
||||
readonly sha256: string;
|
||||
/** Display value from release metadata; the package signature binds bytes, not this row. */
|
||||
readonly version: string;
|
||||
readonly primaryFingerprint: string;
|
||||
readonly signingFingerprint: string;
|
||||
readonly source:
|
||||
| "signed_package_cache"
|
||||
| "signed_vendor_release"
|
||||
| "verified_process_cache";
|
||||
}
|
||||
|
||||
/** Retain failed ephemeral cleanup independently of the caller's lifetime. */
|
||||
const pendingCleanup = new Set<ArtifactResourceScope>();
|
||||
/** Immutable cryptographic facts, bounded to eight packages for this process lifetime. */
|
||||
const packageProofs = new Map<
|
||||
string,
|
||||
Pick<VerifiedLinuxHopperPackage, "primaryFingerprint" | "signingFingerprint">
|
||||
>();
|
||||
const liveLauncherProofs = new Map<string, VerifiedLinuxHopperLauncher>();
|
||||
const retainProof = <T>(cache: Map<string, T>, key: string, proof: T): void => {
|
||||
cache.delete(key);
|
||||
cache.set(key, proof);
|
||||
const oldest = cache.keys().next().value;
|
||||
if (cache.size > 8 && oldest !== undefined) cache.delete(oldest);
|
||||
};
|
||||
|
||||
/** One owner for the selected cache location, shared with the approval plan. */
|
||||
export const linuxHopperReleaseCacheDirectory = (
|
||||
environment: Readonly<NodeJS.ProcessEnv>,
|
||||
): Result<string, HopperReleaseError> => {
|
||||
try {
|
||||
const supplied = environment.XDG_CACHE_HOME || undefined;
|
||||
if (supplied !== undefined && !isAbsolute(supplied))
|
||||
return err(
|
||||
new HopperReleaseError(
|
||||
"verification_unavailable",
|
||||
"XDG_CACHE_HOME must be an absolute filesystem path.",
|
||||
),
|
||||
);
|
||||
return ok(
|
||||
join(
|
||||
supplied ??
|
||||
join(homeDirectoryFromEnvironment(environment, "linux"), ".cache"),
|
||||
"rea",
|
||||
"hopper-releases",
|
||||
),
|
||||
);
|
||||
} catch (cause: unknown) {
|
||||
return err(
|
||||
new HopperReleaseError(
|
||||
"verification_unavailable",
|
||||
"The Hopper release cache location could not be resolved.",
|
||||
{ cause },
|
||||
),
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
/** Authenticated package facts shared by setup, doctor, and the launch boundary. */
|
||||
export class LinuxHopperPublisher {
|
||||
readonly #resources = new ArtifactResourceScope();
|
||||
#feed: ReturnType<typeof parseLinuxHopperReleases> | undefined;
|
||||
|
||||
constructor(
|
||||
readonly environment: Readonly<NodeJS.ProcessEnv>,
|
||||
readonly packageFamily: () => Promise<LinuxPackageFamily | undefined>,
|
||||
) {}
|
||||
|
||||
get cacheRoot(): string {
|
||||
const location = linuxHopperReleaseCacheDirectory(this.environment);
|
||||
if (!location.ok) throw location.error;
|
||||
return location.value;
|
||||
}
|
||||
|
||||
async currentRelease(
|
||||
family: LinuxPackageFamily,
|
||||
signal?: AbortSignal,
|
||||
): Promise<Result<LinuxHopperRelease, HopperReleaseError>> {
|
||||
try {
|
||||
signal?.throwIfAborted();
|
||||
if (this.#feed === undefined) {
|
||||
const response = await downloadHopperPackage(HOPPER_RELEASES_URL, {
|
||||
...(signal === undefined ? {} : { signal }),
|
||||
maxBytes: RELEASE_METADATA_BYTES,
|
||||
timeoutMs: RELEASE_METADATA_TIMEOUT_MS,
|
||||
});
|
||||
if (!response.ok)
|
||||
throw new HopperReleaseError(
|
||||
"release_metadata",
|
||||
"The official Hopper release feed is unavailable.",
|
||||
);
|
||||
this.#feed = parseLinuxHopperReleases(
|
||||
JSON.parse(decodeUtf8(response.bytes)),
|
||||
);
|
||||
}
|
||||
if (!this.#feed.ok) return this.#feed;
|
||||
return this.#feed.value[family];
|
||||
} catch (cause: unknown) {
|
||||
return err(releaseFailure(cause, signal, "release_metadata"));
|
||||
}
|
||||
}
|
||||
|
||||
async verifyArchive(
|
||||
family: LinuxPackageFamily,
|
||||
release: LinuxHopperRelease,
|
||||
archive: string,
|
||||
signal?: AbortSignal,
|
||||
suppliedPublicKey?: string,
|
||||
): Promise<Result<VerifiedLinuxHopperPackage, HopperReleaseError>> {
|
||||
try {
|
||||
const bytes = await this.#read(archive, HOPPER_DOWNLOAD_BYTES, signal);
|
||||
if (
|
||||
bytes.length !== Number(release.file_length) ||
|
||||
createHash("sha1").update(bytes).digest("hex") !== release.file_hash
|
||||
)
|
||||
return err(
|
||||
new HopperReleaseError(
|
||||
"integrity",
|
||||
"The Hopper package does not match the selected release bytes.",
|
||||
),
|
||||
);
|
||||
let publicKey = suppliedPublicKey;
|
||||
if (publicKey === undefined) {
|
||||
const response = await downloadHopperPackage(HOPPER_PUBLIC_KEY_URL, {
|
||||
...(signal === undefined ? {} : { signal }),
|
||||
maxBytes: RELEASE_METADATA_BYTES,
|
||||
timeoutMs: RELEASE_METADATA_TIMEOUT_MS,
|
||||
});
|
||||
if (!response.ok)
|
||||
throw new HopperReleaseError(
|
||||
"verification_unavailable",
|
||||
"The official Hopper public key is unavailable.",
|
||||
);
|
||||
publicKey = decodeUtf8(response.bytes);
|
||||
}
|
||||
const packageSha256 = createHash("sha256").update(bytes).digest("hex");
|
||||
// Every read still validates file identity, byte bounds and selected metadata.
|
||||
// Reuse only proof of the exact signed bytes/key/signature/payload interpretation.
|
||||
const proofKey = createHash("sha256")
|
||||
.update(
|
||||
JSON.stringify([
|
||||
family,
|
||||
packageSha256,
|
||||
publicKey,
|
||||
release.signature,
|
||||
release.main_exe_hash,
|
||||
]),
|
||||
)
|
||||
.digest("hex");
|
||||
signal?.throwIfAborted();
|
||||
const existingProof = packageProofs.get(proofKey);
|
||||
if (existingProof !== undefined)
|
||||
return ok({
|
||||
release,
|
||||
family,
|
||||
packageSha256,
|
||||
publicKey,
|
||||
...existingProof,
|
||||
});
|
||||
const verified = await verifyDetachedPgpSignature(
|
||||
bytes,
|
||||
Buffer.from(release.signature, "base64"),
|
||||
publicKey,
|
||||
HOPPER_LINUX_PRIMARY_FINGERPRINT,
|
||||
signal,
|
||||
);
|
||||
if (!verified.ok)
|
||||
return err(
|
||||
new HopperReleaseError(
|
||||
verified.error.reason === "cancelled"
|
||||
? "cancelled"
|
||||
: "publisher_verification_failed",
|
||||
verified.error.message,
|
||||
{ cause: verified.error },
|
||||
),
|
||||
);
|
||||
const mainExeHash = await this.#withTemporary(async (root) => {
|
||||
// Extract only after authentication, from this operation's private byte snapshot.
|
||||
const snapshot = join(root, "package");
|
||||
await writeFile(snapshot, bytes, { flag: "wx", mode: 0o600 });
|
||||
return this.#packageLauncherHash(family, snapshot, root, signal);
|
||||
});
|
||||
if (mainExeHash !== release.main_exe_hash)
|
||||
return err(
|
||||
new HopperReleaseError(
|
||||
"integrity",
|
||||
`The signed ${family} package launcher SHA-256 ${mainExeHash} differs from the release main_exe_hash ${release.main_exe_hash}.`,
|
||||
),
|
||||
);
|
||||
retainProof(packageProofs, proofKey, verified.value);
|
||||
return ok({
|
||||
release,
|
||||
family,
|
||||
packageSha256,
|
||||
publicKey,
|
||||
...verified.value,
|
||||
});
|
||||
} catch (cause: unknown) {
|
||||
return err(releaseFailure(cause, signal, "verification_unavailable"));
|
||||
}
|
||||
}
|
||||
|
||||
/** Durable evidence contains the signed bytes; an editable digest receipt cannot authorize a build. */
|
||||
async retainVerifiedArchive(
|
||||
verified: VerifiedLinuxHopperPackage,
|
||||
archive: string,
|
||||
signal?: AbortSignal,
|
||||
): Promise<Result<void, HopperReleaseError>> {
|
||||
try {
|
||||
signal?.throwIfAborted();
|
||||
await mkdir(this.cacheRoot, { recursive: true, mode: 0o700 });
|
||||
const parent = await lstat(this.cacheRoot);
|
||||
if (
|
||||
!parent.isDirectory() ||
|
||||
parent.isSymbolicLink() ||
|
||||
parent.uid !== process.getuid?.()
|
||||
)
|
||||
throw new HopperReleaseError(
|
||||
"verification_unavailable",
|
||||
"The Hopper release cache is not a caller-owned directory.",
|
||||
);
|
||||
const target = join(this.cacheRoot, verified.release.main_exe_hash);
|
||||
const lease = await this.#acquireCacheLease(
|
||||
verified.release.main_exe_hash,
|
||||
signal,
|
||||
);
|
||||
await this.#withOwnedResource(
|
||||
{
|
||||
kind: "lease",
|
||||
resource: lease.socketPath ?? "hopper-release-cache-lease",
|
||||
lease,
|
||||
},
|
||||
async () => {
|
||||
signal?.throwIfAborted();
|
||||
await this.#withTemporary(async (stage) => {
|
||||
await copyFile(archive, join(stage, "archive"));
|
||||
if (
|
||||
(await hashStableFile(join(stage, "archive"), signal)).sha256 !==
|
||||
verified.packageSha256
|
||||
)
|
||||
throw new HopperReleaseError(
|
||||
"integrity",
|
||||
"The verified package changed before cache retention.",
|
||||
);
|
||||
await writeFile(join(stage, "public-key.asc"), verified.publicKey, {
|
||||
flag: "wx",
|
||||
mode: 0o600,
|
||||
});
|
||||
await writeFile(
|
||||
join(stage, "release.json"),
|
||||
JSON.stringify({
|
||||
format: "rea-signed-hopper-release-v1",
|
||||
family: verified.family,
|
||||
release: verified.release,
|
||||
}) + "\n",
|
||||
{ flag: "wx", mode: 0o600 },
|
||||
);
|
||||
await this.#publishCacheStage(
|
||||
stage,
|
||||
target,
|
||||
verified.release.main_exe_hash,
|
||||
signal,
|
||||
);
|
||||
}, this.cacheRoot);
|
||||
},
|
||||
);
|
||||
return ok(undefined);
|
||||
} catch (cause: unknown) {
|
||||
return err(releaseFailure(cause, signal, "verification_unavailable"));
|
||||
}
|
||||
}
|
||||
|
||||
async #publishCacheStage(
|
||||
stage: string,
|
||||
target: string,
|
||||
digest: string,
|
||||
signal?: AbortSignal,
|
||||
): Promise<void> {
|
||||
const existing = await this.#cachedPackage(digest, signal);
|
||||
if (existing.ok) return;
|
||||
if (existing.error.reason === "cache_missing") {
|
||||
await rename(stage, target);
|
||||
return;
|
||||
}
|
||||
if (
|
||||
![
|
||||
"integrity",
|
||||
"release_metadata",
|
||||
"publisher_verification_failed",
|
||||
].includes(existing.error.reason)
|
||||
)
|
||||
throw existing.error;
|
||||
signal?.throwIfAborted();
|
||||
const previous = await lstat(target);
|
||||
if (
|
||||
!previous.isDirectory() ||
|
||||
previous.isSymbolicLink() ||
|
||||
previous.uid !== process.getuid?.()
|
||||
)
|
||||
throw existing.error;
|
||||
// Only approved retention under the publication lease may repair an entry.
|
||||
// Preserve malformed evidence without following links or deleting content.
|
||||
const backup = `${target}.backup-${randomUUID()}`;
|
||||
await rename(target, backup);
|
||||
try {
|
||||
const moved = await lstat(backup);
|
||||
if (moved.dev !== previous.dev || moved.ino !== previous.ino)
|
||||
throw new Error("The Hopper cache entry changed during repair.");
|
||||
await rename(stage, target);
|
||||
} catch (cause: unknown) {
|
||||
const replacement = await this.#cachedPackage(digest, signal);
|
||||
if (replacement.ok) return;
|
||||
await this.#failedCachePublication(
|
||||
target,
|
||||
backup,
|
||||
[cause, replacement.error],
|
||||
replacement.error.reason === "cleanup_failed",
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async #acquireCacheLease(
|
||||
digest: string,
|
||||
signal?: AbortSignal,
|
||||
): Promise<HopperTargetLease> {
|
||||
const deadline = performance.now() + 120_000;
|
||||
const runId = randomUUID();
|
||||
let delay = 25;
|
||||
while (true) {
|
||||
signal?.throwIfAborted();
|
||||
const acquired = await acquireHopperReleaseCacheLease({
|
||||
cacheDirectory: this.cacheRoot,
|
||||
launcherSha256: digest,
|
||||
runId,
|
||||
});
|
||||
if (acquired.acquired) return acquired.lease;
|
||||
const remaining = deadline - performance.now();
|
||||
if (remaining <= 0)
|
||||
throw new HopperReleaseError(
|
||||
"verification_unavailable",
|
||||
"Another approved Hopper setup still owns this release cache. Retry after it finishes.",
|
||||
);
|
||||
await waitForAbortableDelay(Math.min(delay, remaining), signal);
|
||||
delay = Math.min(delay * 2, 200);
|
||||
}
|
||||
}
|
||||
|
||||
async #failedCachePublication(
|
||||
target: string,
|
||||
backup: string,
|
||||
causes: unknown[],
|
||||
cleanupFailed = false,
|
||||
): Promise<never> {
|
||||
let preserved = backup;
|
||||
try {
|
||||
// Under the publication lease, restore the previous directory on failure.
|
||||
// POSIX rename cannot replace another nonempty evidence directory.
|
||||
await rename(backup, target);
|
||||
preserved = target;
|
||||
} catch (cause: unknown) {
|
||||
causes.push(cause);
|
||||
}
|
||||
throw new HopperReleaseError(
|
||||
cleanupFailed ? "cleanup_failed" : "verification_unavailable",
|
||||
`The Hopper cache repair did not finish; previous evidence is preserved at ${preserved}. Retry approved setup.`,
|
||||
{
|
||||
cause: new AggregateError(
|
||||
causes,
|
||||
"Hopper cache publication and recovery outcomes",
|
||||
),
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
async inspectLauncher(
|
||||
path: string,
|
||||
signal?: AbortSignal,
|
||||
): Promise<Result<VerifiedLinuxHopperLauncher, HopperReleaseError>> {
|
||||
try {
|
||||
const observed = await hashStableFile(path, signal);
|
||||
const cached = await this.#cachedPackage(observed.sha256, signal);
|
||||
if (cached.ok) {
|
||||
if ((await hashStableFile(path, signal)).sha256 !== observed.sha256)
|
||||
return err(
|
||||
new HopperReleaseError(
|
||||
"integrity",
|
||||
"The Hopper launcher changed during publisher verification.",
|
||||
),
|
||||
);
|
||||
return ok(launcherIdentity(cached.value, "signed_package_cache"));
|
||||
}
|
||||
if (cached.error.reason !== "cache_missing") return cached;
|
||||
signal?.throwIfAborted();
|
||||
const processProof = liveLauncherProofs.get(observed.sha256);
|
||||
if (processProof !== undefined)
|
||||
return ok({ ...processProof, source: "verified_process_cache" });
|
||||
const family = await this.packageFamily();
|
||||
if (family === undefined)
|
||||
return err(
|
||||
new HopperReleaseError(
|
||||
"verification_unavailable",
|
||||
"No native Linux package family is available for Hopper publisher verification.",
|
||||
),
|
||||
);
|
||||
const selected = await this.currentRelease(family, signal);
|
||||
if (!selected.ok) return selected;
|
||||
if (selected.value.main_exe_hash !== observed.sha256)
|
||||
return err(
|
||||
new HopperReleaseError(
|
||||
"unsupported_hopper_build",
|
||||
"The selected Hopper launcher does not match an authenticated cached release or the official current Linux demo.",
|
||||
),
|
||||
);
|
||||
const verified = await this.#withTemporary(async (root) => {
|
||||
const response = await downloadHopperPackage(
|
||||
selected.value.filename,
|
||||
signal === undefined ? {} : { signal },
|
||||
);
|
||||
if (!response.ok)
|
||||
throw new HopperReleaseError(
|
||||
"download",
|
||||
"The official Hopper package is unavailable for publisher verification.",
|
||||
);
|
||||
const archive = join(root, "archive");
|
||||
await writeFile(archive, response.bytes, { flag: "wx", mode: 0o600 });
|
||||
const verified = await this.verifyArchive(
|
||||
family,
|
||||
selected.value,
|
||||
archive,
|
||||
signal,
|
||||
);
|
||||
if (!verified.ok) throw verified.error;
|
||||
return verified.value;
|
||||
});
|
||||
if ((await hashStableFile(path, signal)).sha256 !== observed.sha256)
|
||||
return err(
|
||||
new HopperReleaseError(
|
||||
"integrity",
|
||||
"The Hopper launcher changed during publisher verification.",
|
||||
),
|
||||
);
|
||||
const identity = launcherIdentity(verified, "signed_vendor_release");
|
||||
retainProof(liveLauncherProofs, observed.sha256, identity);
|
||||
return ok(identity);
|
||||
} catch (cause: unknown) {
|
||||
return err(releaseFailure(cause, signal, "verification_unavailable"));
|
||||
}
|
||||
}
|
||||
|
||||
async close(): Promise<void> {
|
||||
try {
|
||||
await this.#resources.close();
|
||||
pendingCleanup.delete(this.#resources);
|
||||
} catch (cause: unknown) {
|
||||
pendingCleanup.add(this.#resources);
|
||||
throw cause;
|
||||
}
|
||||
}
|
||||
|
||||
async #cachedPackage(
|
||||
digest: string,
|
||||
signal?: AbortSignal,
|
||||
): Promise<Result<VerifiedLinuxHopperPackage, HopperReleaseError>> {
|
||||
try {
|
||||
const root = join(this.cacheRoot, digest);
|
||||
try {
|
||||
const directory = await lstat(root);
|
||||
if (!directory.isDirectory() || directory.isSymbolicLink())
|
||||
throw new HopperReleaseError(
|
||||
"integrity",
|
||||
"The retained Hopper cache entry is not a regular directory.",
|
||||
);
|
||||
} catch (cause: unknown) {
|
||||
if (cause instanceof Error && Reflect.get(cause, "code") === "ENOENT") {
|
||||
return err(
|
||||
new HopperReleaseError(
|
||||
"cache_missing",
|
||||
"No retained signed Hopper package identifies this launcher.",
|
||||
{ cause },
|
||||
),
|
||||
);
|
||||
}
|
||||
throw cause;
|
||||
}
|
||||
// Distinguish proven malformed/missing evidence from unavailable reads or
|
||||
// inspection tools. Only the former authorize approved cache replacement.
|
||||
for (const name of ["release.json", "public-key.asc", "archive"])
|
||||
await this.#assertCacheFile(join(root, name));
|
||||
const metadata = await this.#read(
|
||||
join(root, "release.json"),
|
||||
RELEASE_METADATA_BYTES,
|
||||
signal,
|
||||
);
|
||||
let value: unknown;
|
||||
try {
|
||||
value = JSON.parse(decodeUtf8(metadata));
|
||||
} catch (cause: unknown) {
|
||||
throw new HopperReleaseError(
|
||||
"release_metadata",
|
||||
"The retained Hopper package metadata is malformed.",
|
||||
{ cause },
|
||||
);
|
||||
}
|
||||
const parsed = cacheRecordSchema.safeParse(value);
|
||||
if (!parsed.success)
|
||||
throw new HopperReleaseError(
|
||||
"release_metadata",
|
||||
"The retained Hopper package metadata is malformed.",
|
||||
);
|
||||
const release = parseLinuxHopperRelease(
|
||||
parsed.data.release,
|
||||
parsed.data.family,
|
||||
);
|
||||
if (!release.ok) return release;
|
||||
if (release.value.main_exe_hash !== digest)
|
||||
throw new HopperReleaseError(
|
||||
"integrity",
|
||||
"The retained Hopper metadata does not identify this launcher.",
|
||||
);
|
||||
const keyBytes = await this.#read(
|
||||
join(root, "public-key.asc"),
|
||||
RELEASE_METADATA_BYTES,
|
||||
signal,
|
||||
);
|
||||
let key: string;
|
||||
try {
|
||||
key = decodeUtf8(keyBytes);
|
||||
} catch (cause: unknown) {
|
||||
throw new HopperReleaseError(
|
||||
"publisher_verification_failed",
|
||||
"The retained Hopper public key is malformed.",
|
||||
{ cause },
|
||||
);
|
||||
}
|
||||
return this.verifyArchive(
|
||||
parsed.data.family,
|
||||
release.value,
|
||||
join(root, "archive"),
|
||||
signal,
|
||||
key,
|
||||
);
|
||||
} catch (cause: unknown) {
|
||||
return err(releaseFailure(cause, signal, "verification_unavailable"));
|
||||
}
|
||||
}
|
||||
|
||||
async #assertCacheFile(path: string): Promise<void> {
|
||||
try {
|
||||
const stats = await lstat(path);
|
||||
if (!stats.isFile() || stats.isSymbolicLink())
|
||||
throw new HopperReleaseError(
|
||||
"integrity",
|
||||
`Required Hopper evidence is not a regular file: ${path}.`,
|
||||
);
|
||||
} catch (cause: unknown) {
|
||||
if (cause instanceof Error && Reflect.get(cause, "code") === "ENOENT")
|
||||
throw new HopperReleaseError(
|
||||
"integrity",
|
||||
`Required Hopper evidence is missing: ${path}.`,
|
||||
{ cause },
|
||||
);
|
||||
throw cause;
|
||||
}
|
||||
}
|
||||
|
||||
async #packageLauncherHash(
|
||||
family: LinuxPackageFamily,
|
||||
archive: string,
|
||||
root: string,
|
||||
signal?: AbortSignal,
|
||||
): Promise<string> {
|
||||
const commandOptions = {
|
||||
env: this.environment,
|
||||
signal,
|
||||
timeout: 120_000,
|
||||
stopSignal: "SIGKILL" as const,
|
||||
maxBuffer: RELEASE_METADATA_BYTES,
|
||||
};
|
||||
if (family === "rpm") {
|
||||
const output = await execFileOutput(
|
||||
"rpm",
|
||||
[
|
||||
"-qp",
|
||||
"--nosignature",
|
||||
"--nodigest",
|
||||
"--qf",
|
||||
"%{FILEDIGESTALGO}\n[%{FILENAMES}\t%{FILEDIGESTS}\n]",
|
||||
archive,
|
||||
],
|
||||
commandOptions,
|
||||
);
|
||||
const rows = output.stdout.split("\n");
|
||||
if (rows.shift() !== "8")
|
||||
throw new HopperReleaseError(
|
||||
"integrity",
|
||||
"The signed RPM header does not use SHA-256 file digests.",
|
||||
);
|
||||
const matches = rows.filter((row) =>
|
||||
row.startsWith("/opt/hopper/bin/Hopper\t"),
|
||||
);
|
||||
const digest = matches[0]?.split("\t")[1];
|
||||
if (
|
||||
matches.length !== 1 ||
|
||||
digest === undefined ||
|
||||
!/^[a-f0-9]{64}$/u.test(digest)
|
||||
)
|
||||
throw new HopperReleaseError(
|
||||
"integrity",
|
||||
"The signed RPM header has no unique Hopper launcher digest.",
|
||||
);
|
||||
return digest;
|
||||
}
|
||||
const extracted = join(root, "extracted");
|
||||
await mkdir(extracted, { mode: 0o700 });
|
||||
if (family === "deb")
|
||||
await execFileOutput(
|
||||
"dpkg-deb",
|
||||
["--extract", archive, extracted],
|
||||
commandOptions,
|
||||
);
|
||||
else
|
||||
await execFileOutput(
|
||||
"tar",
|
||||
["-xf", archive, "-C", extracted, "--", "opt/hopper/bin/Hopper"],
|
||||
commandOptions,
|
||||
);
|
||||
return (
|
||||
await hashStableFile(
|
||||
join(extracted, "opt", "hopper", "bin", "Hopper"),
|
||||
signal,
|
||||
)
|
||||
).sha256;
|
||||
}
|
||||
|
||||
#read(path: string, limit: number, signal?: AbortSignal): Promise<Buffer> {
|
||||
return withRegularFile(
|
||||
path,
|
||||
async (handle, stats) => {
|
||||
if (stats.size > limit)
|
||||
throw new HopperReleaseError(
|
||||
"integrity",
|
||||
"Retained Hopper evidence exceeds its resource byte budget.",
|
||||
);
|
||||
const bytes = await readBoundedFileBytes(handle, stats.size, signal);
|
||||
if (bytes === undefined)
|
||||
throw new HopperReleaseError(
|
||||
"integrity",
|
||||
"Retained Hopper evidence changed during inspection.",
|
||||
);
|
||||
return bytes;
|
||||
},
|
||||
{ signal, resources: this.#resources, symlinks: "reject" },
|
||||
);
|
||||
}
|
||||
|
||||
async #withTemporary<T>(
|
||||
operation: (path: string) => Promise<T>,
|
||||
parent?: string,
|
||||
): Promise<T> {
|
||||
const root = await PrivateRuntimeRoot.create({
|
||||
prefix: "rea-hopper-publisher-",
|
||||
...(parent === undefined ? {} : { parent }),
|
||||
});
|
||||
return this.#withOwnedResource(
|
||||
{
|
||||
kind: "private-runtime-root",
|
||||
resource: root.path,
|
||||
root,
|
||||
},
|
||||
() => operation(root.path),
|
||||
);
|
||||
}
|
||||
|
||||
async #withOwnedResource<T>(
|
||||
owner: ArtifactResourceOwner,
|
||||
operation: () => Promise<T>,
|
||||
): Promise<T> {
|
||||
let outcome:
|
||||
| { readonly ok: true; readonly value: T }
|
||||
| { readonly ok: false; readonly cause: unknown };
|
||||
try {
|
||||
outcome = { ok: true, value: await operation() };
|
||||
} catch (cause: unknown) {
|
||||
outcome = { ok: false, cause };
|
||||
}
|
||||
const cleanup = await this.#resources.release(owner);
|
||||
if (cleanup.kind === "failed") {
|
||||
pendingCleanup.add(this.#resources);
|
||||
const causes = outcome.ok
|
||||
? [cleanup.cause]
|
||||
: [outcome.cause, cleanup.cause];
|
||||
throw new HopperReleaseError(
|
||||
"cleanup_failed",
|
||||
`Hopper publisher verification retained cleanup ownership for ${owner.resource}.`,
|
||||
{
|
||||
cause: new AggregateError(
|
||||
causes,
|
||||
"Publisher inspection and cleanup outcomes",
|
||||
),
|
||||
},
|
||||
);
|
||||
}
|
||||
if (!outcome.ok) throw outcome.cause;
|
||||
return outcome.value;
|
||||
}
|
||||
}
|
||||
|
||||
/** Retry only resources retained by this producer after failed cleanup. */
|
||||
export const retryLinuxHopperPublisherCleanup = async (): Promise<void> => {
|
||||
const causes: unknown[] = [];
|
||||
const observations = [];
|
||||
for (const scope of pendingCleanup) {
|
||||
try {
|
||||
await scope.close();
|
||||
pendingCleanup.delete(scope);
|
||||
} catch (cause: unknown) {
|
||||
causes.push(cause);
|
||||
observations.push(
|
||||
ArtifactReaderFailure.cleanupObservation(
|
||||
cause,
|
||||
"hopper-publisher-verification",
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
if (causes.length > 0) {
|
||||
const cleanup = {
|
||||
reason: observations.map(({ reason }) => reason).join("; "),
|
||||
resources: [
|
||||
...new Set(observations.flatMap(({ resources }) => resources)),
|
||||
],
|
||||
};
|
||||
throw new ArtifactReaderFailure(
|
||||
"unavailable",
|
||||
`Hopper publisher cleanup remains incomplete: ${cleanup.resources.join(", ")}`,
|
||||
{
|
||||
cause: new AggregateError(causes, "Hopper publisher cleanup failures"),
|
||||
cleanup,
|
||||
},
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
const decodeUtf8 = (bytes: Uint8Array): string =>
|
||||
new TextDecoder("utf-8", { fatal: true }).decode(bytes);
|
||||
const releaseFailure = (
|
||||
cause: unknown,
|
||||
signal: AbortSignal | undefined,
|
||||
fallback: HopperReleaseError["reason"],
|
||||
): HopperReleaseError =>
|
||||
cause instanceof HopperReleaseError && cause.reason === "cleanup_failed"
|
||||
? cause
|
||||
: signal?.aborted === true
|
||||
? new HopperReleaseError(
|
||||
"cancelled",
|
||||
"Hopper publisher verification was cancelled.",
|
||||
{ cause },
|
||||
)
|
||||
: cause instanceof HopperReleaseError
|
||||
? cause
|
||||
: new HopperReleaseError(
|
||||
fallback,
|
||||
"Hopper publisher verification could not complete.",
|
||||
{ cause },
|
||||
);
|
||||
const launcherIdentity = (
|
||||
verified: VerifiedLinuxHopperPackage,
|
||||
source: VerifiedLinuxHopperLauncher["source"],
|
||||
): VerifiedLinuxHopperLauncher => ({
|
||||
sha256: verified.release.main_exe_hash,
|
||||
version: verified.release.version,
|
||||
primaryFingerprint: verified.primaryFingerprint,
|
||||
signingFingerprint: verified.signingFingerprint,
|
||||
source,
|
||||
});
|
||||
@@ -413,7 +413,22 @@ const addCandidate = (
|
||||
...current.authority_references,
|
||||
...candidate.authority_references,
|
||||
]),
|
||||
definitionCount: current.definitionCount + candidate.definitionCount,
|
||||
// Each observation keeps its own unresolved authority and next steps.
|
||||
unavailable_authority: uniqueBy(
|
||||
[...current.unavailable_authority, ...candidate.unavailable_authority],
|
||||
(item) => item,
|
||||
),
|
||||
required_next_evidence: uniqueBy(
|
||||
[...current.required_next_evidence, ...candidate.required_next_evidence],
|
||||
(item) => item,
|
||||
),
|
||||
// A generated ID digests its derivation key, so two generated candidates
|
||||
// with one ID are the same claim seen by several records. Only another
|
||||
// reviewed definition can introduce a competing definition.
|
||||
definitionCount:
|
||||
current.origin === "generated" && candidate.origin === "generated"
|
||||
? current.definitionCount
|
||||
: current.definitionCount + candidate.definitionCount,
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
@@ -12,6 +12,62 @@ import {
|
||||
import { deriveReconstructionObligationCandidates } from "./ReconstructionObligationCandidates.js";
|
||||
import { evaluateReconstructionObligationLedger } from "./ReconstructionObligationLedgerEvaluation.js";
|
||||
|
||||
/** Build a request whose packaged-process obligation closes as ready. */
|
||||
const readyProcessLedgerRequest = () => {
|
||||
const capture = processEvidence();
|
||||
const initial = build(request([capture]));
|
||||
const obligation = initial.obligations[0];
|
||||
if (obligation === undefined) throw new Error("Expected process obligation");
|
||||
const fixtureIds = obligation.required_case_kinds.map(
|
||||
(caseKind) => `packaged.${caseKind}`,
|
||||
);
|
||||
const proof = boundProofEvidence("packaged", {
|
||||
obligation_ids: [obligation.obligation_id],
|
||||
fixture_ids: fixtureIds,
|
||||
case_kinds: obligation.required_case_kinds,
|
||||
verifier_ids: ["verifier.packaged"],
|
||||
claim_ids: ["claim.process"],
|
||||
});
|
||||
const ledgerRequest = request([capture, proof], {
|
||||
manifest: {
|
||||
bindings: [
|
||||
{
|
||||
obligation_id: obligation.obligation_id,
|
||||
owner: {
|
||||
module_path: "src/processOwner.ts",
|
||||
symbol: "runProcess",
|
||||
owner_sha256: "2".repeat(64),
|
||||
},
|
||||
parser_type: null,
|
||||
original_cases: originalCases(obligation, capture.evidence_id),
|
||||
fixtures: obligation.required_case_kinds.map((caseKind) => ({
|
||||
fixture_id: `packaged.${caseKind}`,
|
||||
case_kind: caseKind,
|
||||
authority: "packaged-process",
|
||||
evidence_ids: [proof.evidence_id],
|
||||
})),
|
||||
verifier: {
|
||||
verifier_id: "verifier.packaged",
|
||||
claim_id: "claim.process",
|
||||
command: "npm run verify:package -- process",
|
||||
authority: "packaged-process",
|
||||
status: "pass",
|
||||
result_evidence_id: proof.evidence_id,
|
||||
enumerated_obligation_ids: [obligation.obligation_id],
|
||||
nondeterminism: {
|
||||
mode: "partial-order",
|
||||
specification:
|
||||
"Startup precedes teardown; worker completion is unordered.",
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
contradictions: [],
|
||||
},
|
||||
});
|
||||
return ledgerRequest;
|
||||
};
|
||||
|
||||
describe("reconstruction obligation ledger", () => {
|
||||
it("emits the complete deterministic ledger and closure identity", () => {
|
||||
const evidence = proofEvidence("review");
|
||||
@@ -31,6 +87,49 @@ describe("reconstruction obligation ledger", () => {
|
||||
expect(first.status).toBe("open");
|
||||
});
|
||||
|
||||
it("merges repeated captures of one process scenario into one definition", () => {
|
||||
const first = processEvidence();
|
||||
const second = processEvidence({
|
||||
residual_unknowns: [{ scope: "network", reason: "Second run only." }],
|
||||
});
|
||||
expect(second.evidence_id).not.toBe(first.evidence_id);
|
||||
const single = build(request([first]));
|
||||
const repeated = build(request([first, second]));
|
||||
|
||||
const obligation = repeated.obligations[0];
|
||||
expect(repeated.obligations).toHaveLength(1);
|
||||
expect(obligation?.obligation_id).toBe(
|
||||
single.obligations[0]?.obligation_id,
|
||||
);
|
||||
expect(obligation?.diagnostics.map(({ code }) => code)).not.toContain(
|
||||
"duplicate-definition",
|
||||
);
|
||||
expect(
|
||||
obligation?.authority_references.map(({ evidence_id: id }) => id).sort(),
|
||||
).toEqual([first.evidence_id, second.evidence_id].sort());
|
||||
// The second capture's own uncertainty stays visible after the merge.
|
||||
expect(obligation?.unavailable_authority).toContain(
|
||||
"network: Second run only.",
|
||||
);
|
||||
});
|
||||
|
||||
it("still reports a reviewed definition that reuses a generated ID", () => {
|
||||
const capture = processEvidence();
|
||||
const generated = build(request([capture])).obligations[0];
|
||||
if (generated === undefined) throw new Error("Expected process obligation");
|
||||
const ledger = build(
|
||||
request([capture], {
|
||||
reviewed_obligations: [
|
||||
reviewedObligation(generated.obligation_id, capture.evidence_id),
|
||||
],
|
||||
}),
|
||||
);
|
||||
|
||||
expect(
|
||||
ledger.obligations[0]?.diagnostics.map(({ code }) => code),
|
||||
).toContain("duplicate-definition");
|
||||
});
|
||||
|
||||
it("does not let green unit evidence close a packaged-process obligation", () => {
|
||||
const capture = processEvidence();
|
||||
const proof = proofEvidence("green-unit");
|
||||
@@ -181,58 +280,7 @@ describe("reconstruction obligation ledger case closure", () => {
|
||||
});
|
||||
|
||||
it("closes only when every required case and verifier has comparable authority", () => {
|
||||
const capture = processEvidence();
|
||||
const initial = build(request([capture]));
|
||||
const obligation = initial.obligations[0];
|
||||
if (obligation === undefined)
|
||||
throw new Error("Expected process obligation");
|
||||
const fixtureIds = obligation.required_case_kinds.map(
|
||||
(caseKind) => `packaged.${caseKind}`,
|
||||
);
|
||||
const proof = boundProofEvidence("packaged", {
|
||||
obligation_ids: [obligation.obligation_id],
|
||||
fixture_ids: fixtureIds,
|
||||
case_kinds: obligation.required_case_kinds,
|
||||
verifier_ids: ["verifier.packaged"],
|
||||
claim_ids: ["claim.process"],
|
||||
});
|
||||
const ledgerRequest = request([capture, proof], {
|
||||
manifest: {
|
||||
bindings: [
|
||||
{
|
||||
obligation_id: obligation.obligation_id,
|
||||
owner: {
|
||||
module_path: "src/processOwner.ts",
|
||||
symbol: "runProcess",
|
||||
owner_sha256: "2".repeat(64),
|
||||
},
|
||||
parser_type: null,
|
||||
original_cases: originalCases(obligation, capture.evidence_id),
|
||||
fixtures: obligation.required_case_kinds.map((caseKind) => ({
|
||||
fixture_id: `packaged.${caseKind}`,
|
||||
case_kind: caseKind,
|
||||
authority: "packaged-process",
|
||||
evidence_ids: [proof.evidence_id],
|
||||
})),
|
||||
verifier: {
|
||||
verifier_id: "verifier.packaged",
|
||||
claim_id: "claim.process",
|
||||
command: "npm run verify:package -- process",
|
||||
authority: "packaged-process",
|
||||
status: "pass",
|
||||
result_evidence_id: proof.evidence_id,
|
||||
enumerated_obligation_ids: [obligation.obligation_id],
|
||||
nondeterminism: {
|
||||
mode: "partial-order",
|
||||
specification:
|
||||
"Startup precedes teardown; worker completion is unordered.",
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
contradictions: [],
|
||||
},
|
||||
});
|
||||
const ledgerRequest = readyProcessLedgerRequest();
|
||||
const result = build(ledgerRequest);
|
||||
|
||||
expect(result.obligations[0]).toMatchObject({
|
||||
@@ -257,3 +305,54 @@ describe("reconstruction obligation ledger case closure", () => {
|
||||
expect(limited.coverage.status).toBe("partial");
|
||||
});
|
||||
});
|
||||
|
||||
describe("reconstruction obligation manifest references", () => {
|
||||
it("withholds readiness when a manifest entry names absent Evidence", () => {
|
||||
const ledgerRequest = readyProcessLedgerRequest();
|
||||
// A valid entry for the same case kind must not hide an absent reference.
|
||||
const absent = `ev_${"a".repeat(64)}`;
|
||||
const binding = ledgerRequest.manifest.bindings[0];
|
||||
if (binding === undefined) throw new Error("Expected binding");
|
||||
const variants: {
|
||||
readonly code: string;
|
||||
readonly binding: typeof binding;
|
||||
}[] = [
|
||||
{
|
||||
code: "missing-original-case",
|
||||
binding: {
|
||||
...binding,
|
||||
original_cases: [
|
||||
...binding.original_cases,
|
||||
{ kind: "positive", evidence_id: absent, location: "/old" },
|
||||
],
|
||||
},
|
||||
},
|
||||
{
|
||||
code: "missing-case",
|
||||
binding: {
|
||||
...binding,
|
||||
fixtures: [
|
||||
...binding.fixtures,
|
||||
{
|
||||
fixture_id: "packaged.old",
|
||||
case_kind: "positive",
|
||||
authority: "packaged-process",
|
||||
evidence_ids: [absent],
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
];
|
||||
for (const { code, binding: dangling } of variants) {
|
||||
const open = build({
|
||||
...ledgerRequest,
|
||||
manifest: { ...ledgerRequest.manifest, bindings: [dangling] },
|
||||
});
|
||||
expect(open.status).not.toBe("ready");
|
||||
expect(open.obligations[0]?.status).toBe("implemented");
|
||||
expect(open.obligations[0]?.diagnostics).toEqual([
|
||||
{ code, detail: expect.stringContaining(absent) },
|
||||
]);
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -14,7 +14,10 @@ import {
|
||||
type ReviewedReconstructionObligation,
|
||||
} from "../domain/reconstructionObligationLedgerSchemas.js";
|
||||
|
||||
import { processCaptureSchema } from "../domain/process/processCapture.js";
|
||||
import {
|
||||
processCaptureSchema,
|
||||
type UnverifiedProcessCapture,
|
||||
} from "../domain/process/processCapture.js";
|
||||
/** Build controlled fixture Evidence used by reconstruction-ledger scenarios. */
|
||||
export const proofEvidence = (id: string) =>
|
||||
createEvidence(
|
||||
@@ -31,13 +34,18 @@ export const proofEvidence = (id: string) =>
|
||||
);
|
||||
|
||||
/** Use the canonical empty process capture as a controlled Evidence example. */
|
||||
export const processEvidence = () =>
|
||||
export const processEvidence = (
|
||||
overrides: Partial<UnverifiedProcessCapture> = {},
|
||||
) =>
|
||||
createEvidence(undefined, PROCESS_PROVIDER, {
|
||||
predicateType: "rea.process-capture",
|
||||
operation: "capture_process_scenario",
|
||||
parameters: {},
|
||||
result: jsonValueSchema.parse(
|
||||
processCaptureSchema.parse(EMPTY_PROCESS_CAPTURE_EXAMPLE),
|
||||
processCaptureSchema.parse({
|
||||
...EMPTY_PROCESS_CAPTURE_EXAMPLE,
|
||||
...overrides,
|
||||
}),
|
||||
),
|
||||
confidence: "observed",
|
||||
authority: "controlled-replay",
|
||||
|
||||
@@ -268,6 +268,14 @@ const evaluateBinding = (
|
||||
);
|
||||
continue;
|
||||
}
|
||||
for (const fixture of fixtures)
|
||||
for (const evidenceId of fixture.evidence_ids)
|
||||
if (!context.evidence.has(evidenceId))
|
||||
addDiagnostic(
|
||||
diagnostics,
|
||||
"missing-case",
|
||||
`${caseKind} fixture ${fixture.fixture_id} references Evidence ${evidenceId}, which is absent from the bundle.`,
|
||||
);
|
||||
if (
|
||||
!fixtures.some(
|
||||
(fixture) =>
|
||||
|
||||
@@ -45,6 +45,14 @@ export const originalCaseDiagnostics = (
|
||||
});
|
||||
continue;
|
||||
}
|
||||
// A valid entry for this case kind does not resolve another entry's
|
||||
// reference; every referenced Evidence ID must be in the bundle.
|
||||
for (const { evidence_id: evidenceId } of cases)
|
||||
if (!evidenceById.has(evidenceId))
|
||||
diagnostics.push({
|
||||
code: "missing-original-case",
|
||||
detail: `Original ${caseKind} case references Evidence ${evidenceId}, which is absent from the bundle.`,
|
||||
});
|
||||
if (
|
||||
!cases.some(({ evidence_id: evidenceId }) => {
|
||||
const evidence = evidenceById.get(evidenceId);
|
||||
|
||||
@@ -110,9 +110,11 @@ const resolveInternalSpecifier = (
|
||||
const directoryReference =
|
||||
specifier.endsWith("/") || [".", ".."].includes(posix.basename(specifier));
|
||||
const suffixes = ["", ".ts", ".js", ".mjs", ".cjs", ".tsx", ".jsx"];
|
||||
// A directory resolves only to an index with an extension; neither Node nor
|
||||
// TypeScript selects an extensionless `index` file.
|
||||
const candidates = suffixes.flatMap((suffix) => [
|
||||
...(directoryReference ? [] : [`${normalized}${suffix}`]),
|
||||
posix.join(normalized, `index${suffix}`),
|
||||
...(suffix === "" ? [] : [posix.join(normalized, `index${suffix}`)]),
|
||||
]);
|
||||
// Retain present runtime files first; substitute only a missing counterpart.
|
||||
// Counterparts cover explicit filenames, not directory/index resolution.
|
||||
|
||||
@@ -5,6 +5,7 @@ import type { SetupProgressEvent } from "./SetupTypes.js";
|
||||
import { FakeSetupHost, options } from "./Setup.fixture.js";
|
||||
import { SetupSkillInstallError } from "./SetupSkill.js";
|
||||
import { err, ok } from "../domain/result.js";
|
||||
import { HopperReleaseError } from "./HopperReleaseFeed.js";
|
||||
|
||||
const linkedSkillFailure = () =>
|
||||
new SetupSkillInstallError({
|
||||
@@ -16,6 +17,44 @@ const linkedSkillFailure = () =>
|
||||
cause: undefined,
|
||||
});
|
||||
|
||||
describe("Linux publisher cache approval", () => {
|
||||
it.each([true, false])(
|
||||
"discloses the selected cache or blocks an unresolved path before approval: %s",
|
||||
async (valid) => {
|
||||
const host = Object.assign(new FakeSetupHost("linux"), {
|
||||
linuxHopperReleaseCache: valid
|
||||
? ok("/caller/cache/rea/hopper-releases")
|
||||
: err(
|
||||
new HopperReleaseError(
|
||||
"verification_unavailable",
|
||||
"Invalid selected cache path",
|
||||
),
|
||||
),
|
||||
});
|
||||
host.distribution = {
|
||||
id: "ubuntu",
|
||||
versionId: "24.04",
|
||||
packageFamily: "deb",
|
||||
hopperSetupSupported: true,
|
||||
};
|
||||
const result = await runSetup(options(false, true), host);
|
||||
if (valid)
|
||||
expect(
|
||||
result.plannedActions.find(({ kind }) => kind === "install_hopper")
|
||||
?.detail,
|
||||
).toContain("/caller/cache/rea/hopper-releases/<launcher-sha256>");
|
||||
else
|
||||
expect(result).toMatchObject({
|
||||
status: "needs_human",
|
||||
plannedActions: [],
|
||||
});
|
||||
expect(host.hopperInstalls).toBe(0);
|
||||
expect(host.configurations).toBe(0);
|
||||
expect(host.skillInstalls).toBe(0);
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
describe("selected skill admission", () => {
|
||||
it.each(["codex", "shared"] as const)(
|
||||
"keeps %s choices when an unselected client is linked",
|
||||
|
||||
@@ -20,8 +20,10 @@ import {
|
||||
LinuxHopperTemporaryCleanupOwner,
|
||||
readLinuxDistribution,
|
||||
systemLinuxHopperInstallHost,
|
||||
type LinuxHopperInstallHost,
|
||||
} from "./LinuxHopper.js";
|
||||
import { installMacHopper, systemMacHopperInstallHost } from "./MacHopper.js";
|
||||
import { linuxHopperReleaseCacheDirectory } from "./LinuxHopperPublisher.js";
|
||||
import {
|
||||
clientEvidencePaths,
|
||||
supportedClients,
|
||||
@@ -156,6 +158,12 @@ export const systemSetupHost = (
|
||||
return {
|
||||
platform,
|
||||
homeDirectory,
|
||||
...(platform === "linux"
|
||||
? {
|
||||
linuxHopperReleaseCache:
|
||||
linuxHopperReleaseCacheDirectory(environment),
|
||||
}
|
||||
: {}),
|
||||
skillDestinations: (clientIds) =>
|
||||
skillDestinations(homeDirectory, clientIds, environment, platform),
|
||||
...setupRegistration(platform, homeDirectory, environment),
|
||||
@@ -164,7 +172,7 @@ export const systemSetupHost = (
|
||||
linuxDistribution: readLinuxDistribution,
|
||||
close: () =>
|
||||
closeSetupResources(resources, macHopperHost, doctorHost, () =>
|
||||
pendingLinuxHopperCleanup.retry(linuxHopperHost.cleanup),
|
||||
closeLinuxHopperInstallHost(linuxHopperHost, pendingLinuxHopperCleanup),
|
||||
),
|
||||
initialSetupState: async (
|
||||
scope?: DoctorScope,
|
||||
@@ -206,9 +214,19 @@ export const systemSetupHost = (
|
||||
const result = await installLinuxHopper(linuxHopperHost);
|
||||
if (result.cleanupFailure !== undefined)
|
||||
pendingLinuxHopperCleanup.retain(result.cleanupFailure);
|
||||
return result.status === "installed"
|
||||
? { status: "installed", launcherPath: result.launcherPath }
|
||||
: setupInstallFailure(result.reason, result.packageFailure);
|
||||
if (result.status === "installed")
|
||||
return { status: "installed", launcherPath: result.launcherPath };
|
||||
const failure = setupInstallFailure(
|
||||
result.reason,
|
||||
result.packageFailure,
|
||||
"linux",
|
||||
);
|
||||
return result.verificationFailure !== undefined
|
||||
? {
|
||||
...failure,
|
||||
remediation: `${result.verificationFailure.message} ${failure.remediation}`,
|
||||
}
|
||||
: failure;
|
||||
}
|
||||
if (macHopperHost === undefined)
|
||||
return setupInstallFailure("unsupported_host");
|
||||
@@ -425,6 +443,23 @@ const configurationCleanupError = (
|
||||
const errorMessage = (cause: unknown): string =>
|
||||
cause instanceof Error ? cause.message : String(cause);
|
||||
|
||||
const closeLinuxHopperInstallHost = async (
|
||||
host: LinuxHopperInstallHost,
|
||||
temporaryOwner: LinuxHopperTemporaryCleanupOwner,
|
||||
): Promise<string | undefined> => {
|
||||
const reasons: string[] = [];
|
||||
const temporary = await temporaryOwner.retry(host.cleanup);
|
||||
if (temporary !== undefined) reasons.push(temporary);
|
||||
try {
|
||||
await host.close?.();
|
||||
} catch (cause: unknown) {
|
||||
reasons.push(
|
||||
`Linux Hopper publisher cleanup failed: ${errorMessage(cause)}`,
|
||||
);
|
||||
}
|
||||
return reasons.length === 0 ? undefined : reasons.join("; ");
|
||||
};
|
||||
|
||||
/** Detect supported agents from their config files or stable installation markers. */
|
||||
export const detectClients = async (
|
||||
home: string,
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { HOPPER_PUBLISHER } from "../domain/hopperPublisher.js";
|
||||
import { HOPPER_LINUX_PRIMARY_FINGERPRINT } from "./HopperReleaseFeed.js";
|
||||
import type { LinuxPackageInstallFailure } from "./LinuxHopper.js";
|
||||
|
||||
export type SetupFailureCode =
|
||||
@@ -50,6 +51,8 @@ export type HopperInstallFailureReason =
|
||||
| "mount"
|
||||
| "bundle"
|
||||
| "publisher_verification_failed"
|
||||
| "verification_unavailable"
|
||||
| "cleanup_failed"
|
||||
| "copy";
|
||||
|
||||
const normalizePackageOutput = (output: string): string =>
|
||||
@@ -99,6 +102,7 @@ const packageManagerRemediation = (
|
||||
export const setupInstallFailure = (
|
||||
reason: HopperInstallFailureReason,
|
||||
packageFailure?: LinuxPackageInstallFailure,
|
||||
platform: NodeJS.Platform = "darwin",
|
||||
): SetupHopperInstallFailure => {
|
||||
switch (reason) {
|
||||
case "unsupported_host":
|
||||
@@ -139,7 +143,7 @@ export const setupInstallFailure = (
|
||||
status: "failed",
|
||||
code: "unsupported_hopper_build",
|
||||
remediation:
|
||||
"The installed Hopper launcher does not match the build supported by this REA release. Unset HOPPER_LAUNCHER_PATH, reinstall through rea setup, or update REA.",
|
||||
"The installed Hopper launcher does not match the authenticated vendor package. Check HOPPER_LAUNCHER_PATH and reinstall the official Hopper demo through rea setup.",
|
||||
};
|
||||
case "cancelled":
|
||||
return {
|
||||
@@ -173,7 +177,24 @@ export const setupInstallFailure = (
|
||||
return {
|
||||
status: "failed",
|
||||
code: "publisher_verification_failed",
|
||||
remediation: `The mounted Hopper application is not signed by the publisher REA pins (${HOPPER_PUBLISHER.bundleIdentifier}, Apple team ${HOPPER_PUBLISHER.teamIdentifier}); nothing was copied or launched. Install Hopper yourself and set HOPPER_LAUNCHER_PATH to its launcher, then rerun rea setup.`,
|
||||
remediation:
|
||||
platform === "linux"
|
||||
? `The Hopper package signature did not verify under the pinned primary PGP fingerprint ${HOPPER_LINUX_PRIMARY_FINGERPRINT}; the package manager was not run. Download the official package and retry setup.`
|
||||
: `The mounted Hopper application is not signed by the publisher REA pins (${HOPPER_PUBLISHER.bundleIdentifier}, Apple team ${HOPPER_PUBLISHER.teamIdentifier}); nothing was copied or launched. Install Hopper yourself and set HOPPER_LAUNCHER_PATH to its launcher, then rerun rea setup.`,
|
||||
};
|
||||
case "verification_unavailable":
|
||||
return {
|
||||
status: "failed",
|
||||
code: "publisher_verification_failed",
|
||||
remediation:
|
||||
"REA could not authenticate or retain the signed Hopper package. Check vendor access, native package inspection tools and caller-owned cache permissions, then retry setup.",
|
||||
};
|
||||
case "cleanup_failed":
|
||||
return {
|
||||
status: "failed",
|
||||
code: "cleanup_failed",
|
||||
remediation:
|
||||
"Hopper publisher verification retained temporary cleanup ownership. Retry cleanup before running setup again.",
|
||||
};
|
||||
case "copy":
|
||||
return {
|
||||
|
||||
@@ -161,6 +161,14 @@ export const planSetupActions = async (input: {
|
||||
const client = byId.get(id);
|
||||
return client === undefined ? [] : [client];
|
||||
});
|
||||
const hopperCache = input.host.linuxHopperReleaseCache;
|
||||
if (input.discovery.installHopper && hopperCache?.ok === false)
|
||||
return {
|
||||
plannedActions: [],
|
||||
selectedClients,
|
||||
clientsToConfigure: [],
|
||||
blocker: hopperCache.error.message,
|
||||
};
|
||||
if (
|
||||
selectedClients.length > 0 &&
|
||||
input.host.registrationCommandBlocker !== undefined
|
||||
@@ -199,6 +207,9 @@ export const planSetupActions = async (input: {
|
||||
const plannedActions = setupPlan({
|
||||
platform: input.host.platform,
|
||||
homeDirectory: input.host.homeDirectory,
|
||||
...(hopperCache?.ok === true
|
||||
? { hopperCacheDirectory: hopperCache.value }
|
||||
: {}),
|
||||
installHopper: input.discovery.installHopper,
|
||||
installSkill: input.installSkill,
|
||||
skillClientIds: input.skillClientIds,
|
||||
@@ -264,6 +275,7 @@ const plannedClientWrites = (
|
||||
const setupPlan = (input: {
|
||||
readonly platform: NodeJS.Platform;
|
||||
readonly homeDirectory: string;
|
||||
readonly hopperCacheDirectory?: string;
|
||||
readonly installHopper: boolean;
|
||||
readonly installSkill: boolean;
|
||||
readonly skillClientIds: readonly string[];
|
||||
@@ -289,7 +301,7 @@ const setupPlan = (input: {
|
||||
: "system package manager",
|
||||
detail:
|
||||
input.platform === "linux"
|
||||
? "Download, verify, and install Hopper plus its Xvfb demo-session dependencies. For the supported demo build, REA uses a private display and selects Hopper's offered demo mode for each analysis session."
|
||||
? `Download the current official Hopper demo, verify its PGP publisher and launcher bytes, and retain archive, public-key.asc and release.json under ${input.hopperCacheDirectory ?? `${input.homeDirectory}/.cache/rea/hopper-releases`}/<launcher-sha256> for offline verification. Preserve replaced damaged evidence beside it as <launcher-sha256>.backup-<id>. Install Hopper plus its Xvfb demo-session dependencies. REA uses a private display and selects Hopper's offered demo mode for each analysis session.`
|
||||
: "Download the official Hopper package, verify it, and install it. Hopper may show its demo or license prompt when first opened.",
|
||||
external: true,
|
||||
operation: "install" as const,
|
||||
@@ -346,9 +358,9 @@ const linuxDisclosure = (family: LinuxPackageFamily) => {
|
||||
process.getuid?.() === 0,
|
||||
);
|
||||
return {
|
||||
networkOrigins: [disclosure.downloadUrl],
|
||||
networkOrigins: disclosure.networkOrigins,
|
||||
commands: disclosure.commands,
|
||||
integrity: `${String(disclosure.expectedBytes)} bytes · SHA-1 ${disclosure.expectedSha1}`,
|
||||
integrity: disclosure.integrity,
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import type { DoctorReport, DoctorScope, runDoctor } from "./Doctor.js";
|
||||
import type { LinuxDistribution } from "./LinuxHopper.js";
|
||||
import type { HopperReleaseError } from "./HopperReleaseFeed.js";
|
||||
import type {
|
||||
SkillDestination,
|
||||
SkillInstallResult,
|
||||
@@ -67,6 +68,7 @@ export type ClientConfigurationInspection =
|
||||
export interface SetupHost {
|
||||
readonly platform: NodeJS.Platform;
|
||||
readonly homeDirectory: string;
|
||||
readonly linuxHopperReleaseCache?: Result<string, HopperReleaseError>;
|
||||
skillDestinations(clientIds: readonly string[]): readonly SkillDestination[];
|
||||
readonly registrationCommand: readonly string[];
|
||||
/** Why client registration cannot proceed from this host. */
|
||||
|
||||
@@ -5,10 +5,7 @@ import { setImmediate } from "node:timers/promises";
|
||||
|
||||
import { resolveJavaScriptSourceMapReferences } from "../../domain/javascript/javascriptSourceMapPaths.js";
|
||||
import { analyzeParsedJavaScriptStaticSourceSteps } from "../../domain/javascript/javascriptStaticAnalysis.js";
|
||||
import {
|
||||
analyzeParsedJavaScriptSemanticsSteps,
|
||||
classifyParsedJavaScriptOpenReceiversSteps,
|
||||
} from "../../domain/javascript/javascriptSemanticAnalysis.js";
|
||||
import { analyzeParsedJavaScriptSemanticsSteps } from "../../domain/javascript/javascriptSemanticAnalysis.js";
|
||||
import type { JavaScriptSemanticIr } from "../../domain/javascript/javascriptSemanticIr.js";
|
||||
import { projectJavaScriptModuleSemantics } from "../../domain/javascript/javascriptModuleSemanticIr.js";
|
||||
import { parseJavaScriptSource } from "../../domain/javascript/javascriptSourceParser.js";
|
||||
@@ -382,12 +379,9 @@ function* analyzeArtifactFileSteps<
|
||||
state.parseFailures += 1;
|
||||
return;
|
||||
}
|
||||
const openReceiverFacts =
|
||||
yield* classifyParsedJavaScriptOpenReceiversSteps(parsed);
|
||||
const analysis = yield* analyzeParsedJavaScriptStaticSourceSteps(
|
||||
file.text.value,
|
||||
parsed,
|
||||
openReceiverFacts,
|
||||
);
|
||||
const staticFindings = findingCount(analysis);
|
||||
yield;
|
||||
|
||||
@@ -2495,20 +2495,61 @@ describe("url-reference directory and empty-segment semantics", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it.each(["./nested//worker.js", "./nested//../worker.js"])(
|
||||
"does not collapse empty URL path segments: %s",
|
||||
(declaredPath) => {
|
||||
it.each([
|
||||
["./nested//worker.js", null],
|
||||
["./nested//../worker.js", "dist/nested/worker.js"],
|
||||
])(
|
||||
"keeps empty URL path segments significant: %s",
|
||||
(declaredPath, resolvedPath) => {
|
||||
// A dot segment removes the preceding empty segment, never `nested`.
|
||||
expect(new URL(declaredPath, "file:///root/dist/app.mjs").pathname).toBe(
|
||||
resolvedPath === null
|
||||
? "/root/dist/nested//worker.js"
|
||||
: `/root/${resolvedPath}`,
|
||||
);
|
||||
for (const context of ["url-reference", "html-reference"] as const)
|
||||
expect(
|
||||
resolve({
|
||||
declaredPath,
|
||||
sourcePath: "dist/app.mjs",
|
||||
context,
|
||||
files: fileMap([
|
||||
file("dist/worker.js", "root"),
|
||||
file("dist/nested/worker.js", "root"),
|
||||
]),
|
||||
}),
|
||||
context,
|
||||
).toMatchObject({ resolved_path: resolvedPath });
|
||||
expect(
|
||||
resolve({
|
||||
declaredPath,
|
||||
sourcePath: "dist/app.mjs",
|
||||
context: "url-reference",
|
||||
context: "module-specifier",
|
||||
moduleKind: "import",
|
||||
files: fileMap([
|
||||
file("dist/worker.js", "root"),
|
||||
file("dist/nested/worker.js", "root"),
|
||||
]),
|
||||
}),
|
||||
).toMatchObject({ resolved_path: null });
|
||||
).toMatchObject({ resolved_path: resolvedPath });
|
||||
// CommonJS keeps filesystem path semantics.
|
||||
expect(
|
||||
resolve({
|
||||
declaredPath,
|
||||
sourcePath: "dist/app.mjs",
|
||||
context: "module-specifier",
|
||||
moduleKind: "require",
|
||||
files: fileMap([
|
||||
file("dist/worker.js", "root"),
|
||||
file("dist/nested/worker.js", "root"),
|
||||
]),
|
||||
}),
|
||||
).toMatchObject({
|
||||
resolved_path:
|
||||
declaredPath === "./nested//worker.js"
|
||||
? "dist/nested/worker.js"
|
||||
: "dist/worker.js",
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
|
||||
@@ -551,22 +551,24 @@ const contextualCandidate = (
|
||||
return unresolvedCandidate("rejected", [
|
||||
"The URL reference path percent-decodes to NUL or to bytes that are not UTF-8.",
|
||||
]);
|
||||
// Empty URL segments are significant; canonical filesystem paths cannot
|
||||
// preserve them, including their interaction with following dot segments.
|
||||
if (decoded.includes("//"))
|
||||
return unresolvedCandidate("unavailable", [
|
||||
"Repeated URL path separators cannot be mapped to an exact canonical artifact file.",
|
||||
]);
|
||||
return exactUrlPathCandidate(
|
||||
const candidate = urlRelativeCandidate(
|
||||
posix.dirname(input.sourcePath),
|
||||
decoded,
|
||||
relativeCandidate(input.sourcePath, decoded),
|
||||
"URL",
|
||||
);
|
||||
return typeof candidate === "string"
|
||||
? exactUrlPathCandidate(decoded, candidate, "URL")
|
||||
: candidate;
|
||||
}
|
||||
const candidate = relativeCandidate(input.sourcePath, declared);
|
||||
return context === "module-specifier" && input.moduleKind === "import"
|
||||
? exactUrlPathCandidate(declared, candidate, "module URL")
|
||||
: candidate;
|
||||
if (context === "module-specifier" && input.moduleKind === "import") {
|
||||
const candidate = urlRelativeCandidate(
|
||||
posix.dirname(input.sourcePath),
|
||||
declared,
|
||||
);
|
||||
return typeof candidate === "string"
|
||||
? exactUrlPathCandidate(declared, candidate, "module URL")
|
||||
: candidate;
|
||||
}
|
||||
return relativeCandidate(input.sourcePath, declared);
|
||||
};
|
||||
|
||||
const decodeModuleUrlPath = (
|
||||
@@ -593,6 +595,78 @@ const relativeCandidate = (sourcePath: string, declared: string): string =>
|
||||
? declared.slice(1)
|
||||
: posix.join(posix.dirname(sourcePath), declared);
|
||||
|
||||
/**
|
||||
* A URL path during reference resolution. Above-root `..` markers are kept
|
||||
* apart from segments, so a later `..` cannot cancel an escape, while empty
|
||||
* segments stay removable until the final path is formed.
|
||||
*/
|
||||
interface UrlPathState {
|
||||
readonly escapes: number;
|
||||
readonly segments: readonly string[];
|
||||
}
|
||||
|
||||
const urlDirectoryState = (directory: string): UrlPathState => ({
|
||||
escapes: 0,
|
||||
segments: directory === "" || directory === "." ? [] : directory.split("/"),
|
||||
});
|
||||
|
||||
/**
|
||||
* Apply URL dot segments: `..` removes the previous segment even when it is
|
||||
* empty, so `assets//../app.js` names `assets/app.js`. A final `.` or `..`
|
||||
* leaves directory form, as the URL parser does.
|
||||
*/
|
||||
const applyUrlReference = (
|
||||
base: UrlPathState,
|
||||
reference: string,
|
||||
): UrlPathState => {
|
||||
const absolute = reference.startsWith("/");
|
||||
const segments = absolute ? [] : [...base.segments];
|
||||
let escapes = absolute ? 0 : base.escapes;
|
||||
const parts = (absolute ? reference.slice(1) : reference).split("/");
|
||||
for (const [index, part] of parts.entries()) {
|
||||
if (part !== "." && part !== "..") {
|
||||
segments.push(part);
|
||||
continue;
|
||||
}
|
||||
if (part === "..")
|
||||
if (segments.length > 0) segments.pop();
|
||||
else escapes += 1;
|
||||
if (index === parts.length - 1) segments.push("");
|
||||
}
|
||||
return { escapes, segments };
|
||||
};
|
||||
|
||||
/** A relative reference replaces the base URL's final path segment. */
|
||||
const urlBaseDirectory = (base: UrlPathState): UrlPathState => ({
|
||||
escapes: base.escapes,
|
||||
segments: base.segments.slice(0, -1),
|
||||
});
|
||||
|
||||
/**
|
||||
* Form the artifact candidate. A remaining empty segment cannot name a
|
||||
* canonical artifact file; a trailing one is directory form. Confinement
|
||||
* rejects a candidate that keeps an above-root marker.
|
||||
*/
|
||||
const urlPathCandidate = (path: UrlPathState): string | CandidateResolution => {
|
||||
const segments =
|
||||
path.segments.at(-1) === "" ? path.segments.slice(0, -1) : path.segments;
|
||||
if (segments.includes(""))
|
||||
return unresolvedCandidate("unavailable", [
|
||||
"Repeated URL path separators cannot be mapped to an exact canonical artifact file.",
|
||||
]);
|
||||
return [
|
||||
...Array.from({ length: path.escapes }, () => ".."),
|
||||
...segments,
|
||||
].join("/");
|
||||
};
|
||||
|
||||
/** Resolve a URL path reference against a canonical directory. */
|
||||
const urlRelativeCandidate = (
|
||||
directory: string,
|
||||
reference: string,
|
||||
): string | CandidateResolution =>
|
||||
urlPathCandidate(applyUrlReference(urlDirectoryState(directory), reference));
|
||||
|
||||
const bareModuleCandidate = (
|
||||
input: ResolveArtifactPathInput,
|
||||
): CandidateResolution => {
|
||||
@@ -1066,11 +1140,12 @@ const htmlCandidate = (
|
||||
return unresolvedCandidate("rejected", [
|
||||
"The HTML reference path percent-decodes to NUL or to bytes that are not UTF-8.",
|
||||
]);
|
||||
if (declaredPath.startsWith("/")) return declaredPath.slice(1);
|
||||
if (declaredPath.startsWith("/"))
|
||||
return urlRelativeCandidate("", declaredPath);
|
||||
if (base === undefined || base === null || base === "")
|
||||
return declaredPath === ""
|
||||
? input.sourcePath
|
||||
: posix.join(posix.dirname(input.sourcePath), declaredPath);
|
||||
: urlRelativeCandidate(posix.dirname(input.sourcePath), declaredPath);
|
||||
// A local base href is a second untrusted path input; apply the same
|
||||
// admission rules a declared path gets so it cannot smuggle traversal or
|
||||
// separator syntax past canonicalization.
|
||||
@@ -1083,12 +1158,21 @@ const htmlCandidate = (
|
||||
return unresolvedCandidate("rejected", [
|
||||
"The document base href path percent-decodes to NUL or to bytes that are not UTF-8.",
|
||||
]);
|
||||
const basePath = decodedBase.startsWith("/")
|
||||
? decodedBase.slice(1)
|
||||
: posix.join(posix.dirname(input.sourcePath), decodedBase);
|
||||
if (declaredPath === "")
|
||||
return exactUrlPathCandidate(decodedBase, basePath, "HTML");
|
||||
return posix.join(htmlBaseDirectory(decodedBase, basePath), declaredPath);
|
||||
// Keep the base's empty segments and escapes until the reference is
|
||||
// applied; only the final path must name a canonical artifact file.
|
||||
const baseState = applyUrlReference(
|
||||
urlDirectoryState(posix.dirname(input.sourcePath)),
|
||||
decodedBase,
|
||||
);
|
||||
if (declaredPath === "") {
|
||||
const basePath = urlPathCandidate(baseState);
|
||||
return typeof basePath === "string"
|
||||
? exactUrlPathCandidate(decodedBase, basePath, "HTML")
|
||||
: basePath;
|
||||
}
|
||||
return urlPathCandidate(
|
||||
applyUrlReference(urlBaseDirectory(baseState), declaredPath),
|
||||
);
|
||||
};
|
||||
|
||||
const exactUrlPathCandidate = (
|
||||
@@ -1112,9 +1196,6 @@ const urlPathIsDirectory = (path: string): boolean =>
|
||||
path === "." ||
|
||||
path === "..";
|
||||
|
||||
const htmlBaseDirectory = (base: string, basePath: string): string =>
|
||||
urlPathIsDirectory(base) ? basePath : posix.dirname(basePath);
|
||||
|
||||
const confineCandidate = (candidate: string): string | CandidateResolution => {
|
||||
const normalized = normalizeJoinedLogicalPath(candidate);
|
||||
if (logicalPathEscapesRoot(normalized))
|
||||
|
||||
@@ -6,6 +6,7 @@ const paths = [
|
||||
"assets/app.js",
|
||||
"renderer/app.js",
|
||||
"renderer/a/app.js",
|
||||
"renderer/assets/app.js",
|
||||
"app.js",
|
||||
];
|
||||
const files = new Map<string, JavaScriptArtifactFile>(
|
||||
@@ -105,6 +106,26 @@ describe("HTML base href dot segments", () => {
|
||||
},
|
||||
);
|
||||
});
|
||||
describe("HTML base href empty segments", () => {
|
||||
it("lets the script reference remove an empty segment from the base", () => {
|
||||
// URL resolution keeps the base's empty segment until `..` removes it.
|
||||
expect(
|
||||
new URL("../app.js", new URL("./assets//", documentUrl)).pathname,
|
||||
).toBe("/renderer/assets/app.js");
|
||||
expect(resolve("../app.js", "./assets//")).toMatchObject({
|
||||
resolution_status: "resolved",
|
||||
resolved_path: "renderer/assets/app.js",
|
||||
});
|
||||
});
|
||||
it("keeps an above-root base escape when the reference also climbs", () => {
|
||||
expect(resolve("../app.js", "../../")).toMatchObject({
|
||||
resolution_status: "rejected",
|
||||
limitations: [
|
||||
"The resolved candidate escapes the canonical artifact root.",
|
||||
],
|
||||
});
|
||||
});
|
||||
});
|
||||
describe("HTML base href path syntax", () => {
|
||||
it.each(["%2e%2e/secret/", "/a/%2e%2e/", "/a\\b/", "a\0b"])(
|
||||
"rejects base href %j with the same admission rules a declared path gets",
|
||||
|
||||
@@ -8,8 +8,13 @@ import { ArtifactReaderFailure } from "./ArtifactReader.js";
|
||||
import type { SafeOutputTree } from "./SafeOutputTree.js";
|
||||
import type { PrivateRuntimeRoot } from "../process/PrivateRuntimeRoot.js";
|
||||
|
||||
/** One artifact resource and its format-specific cleanup owner. */
|
||||
/** One artifact resource and the capability that owns its cleanup. */
|
||||
export type ArtifactResourceOwner =
|
||||
| {
|
||||
readonly kind: "lease";
|
||||
readonly resource: string;
|
||||
readonly lease: { release(): Promise<void> };
|
||||
}
|
||||
| {
|
||||
readonly kind: "javascript-source-analysis";
|
||||
readonly resource: string;
|
||||
@@ -156,6 +161,9 @@ export class ArtifactResourceScope {
|
||||
|
||||
const cleanup = async (owner: ArtifactResourceOwner): Promise<void> => {
|
||||
switch (owner.kind) {
|
||||
case "lease":
|
||||
await owner.lease.release();
|
||||
return;
|
||||
case "javascript-source-analysis":
|
||||
await owner.analysis.close();
|
||||
return;
|
||||
|
||||
@@ -7,7 +7,10 @@ import {
|
||||
isUIKitRuntimeConnectionClass,
|
||||
} from "../../domain/apple/interfaceBuilderKeyedArchive.js";
|
||||
import { DirectoryArtifactReader } from "../DirectoryArtifactReader.js";
|
||||
import { projectPlistValue } from "../../domain/apple/plistValue.js";
|
||||
import {
|
||||
projectPlistValue,
|
||||
unknownDateLimitation,
|
||||
} from "../../domain/apple/plistValue.js";
|
||||
import {
|
||||
omittedPrototypeKeysLimitation,
|
||||
parseXmlPropertyList,
|
||||
@@ -58,6 +61,7 @@ export const analyzeInterfaceBuilderBundle = async (input: {
|
||||
const incompleteHierarchies = new Map<string, number>();
|
||||
const unresolvedConnections = new Map<string, number>();
|
||||
const prototypeKeyOmissions = new Map<string, number | null>();
|
||||
const unknownDates = new Map<string, number>();
|
||||
let omitted = 0;
|
||||
let attempted = 0;
|
||||
let aggregateInputBytes = 0;
|
||||
@@ -131,16 +135,21 @@ export const analyzeInterfaceBuilderBundle = async (input: {
|
||||
binaryPrototypeKeyObserved =
|
||||
decodedReservation.binaryPrototypeKeyObserved;
|
||||
}
|
||||
const { value: raw, omittedPrototypeKeys } =
|
||||
nib === null
|
||||
? decodePlist(bytes, xmlText, binaryPrototypeKeyObserved)
|
||||
: { value: nib.raw, omittedPrototypeKeys: 0 };
|
||||
const {
|
||||
value: raw,
|
||||
omittedPrototypeKeys,
|
||||
unknownDateCount,
|
||||
} = nib === null
|
||||
? decodePlist(bytes, xmlText, binaryPrototypeKeyObserved)
|
||||
: { value: nib.raw, omittedPrototypeKeys: 0, unknownDateCount: 0 };
|
||||
if (nib !== null && nib.omitted > 0)
|
||||
incompleteHierarchies.set(entry.path, nib.omitted);
|
||||
if (nib !== null && nib.unresolvedConnections > 0)
|
||||
unresolvedConnections.set(entry.path, nib.unresolvedConnections);
|
||||
if (omittedPrototypeKeys !== 0)
|
||||
prototypeKeyOmissions.set(entry.path, omittedPrototypeKeys);
|
||||
if (unknownDateCount !== 0)
|
||||
unknownDates.set(entry.path, unknownDateCount);
|
||||
const documentHash = createHash("sha256").update(bytes).digest("hex");
|
||||
documents.push({
|
||||
relativePath: entry.path,
|
||||
@@ -178,6 +187,7 @@ export const analyzeInterfaceBuilderBundle = async (input: {
|
||||
incompleteHierarchies,
|
||||
unresolvedConnections,
|
||||
prototypeKeyOmissions,
|
||||
unknownDates,
|
||||
invalid,
|
||||
omittedArchives,
|
||||
unreportedOmittedArchives,
|
||||
@@ -246,6 +256,7 @@ const finalizeHierarchyCoverage = (
|
||||
incompleteHierarchies: ReadonlyMap<string, number>;
|
||||
unresolvedConnections: ReadonlyMap<string, number>;
|
||||
prototypeKeyOmissions: ReadonlyMap<string, number | null>;
|
||||
unknownDates: ReadonlyMap<string, number>;
|
||||
invalid: readonly string[];
|
||||
omittedArchives: readonly { path: string; reason: string }[];
|
||||
unreportedOmittedArchives: number;
|
||||
@@ -257,6 +268,7 @@ const finalizeHierarchyCoverage = (
|
||||
incompleteHierarchies,
|
||||
unresolvedConnections,
|
||||
prototypeKeyOmissions,
|
||||
unknownDates,
|
||||
invalid,
|
||||
omittedArchives,
|
||||
unreportedOmittedArchives,
|
||||
@@ -342,6 +354,9 @@ const finalizeHierarchyCoverage = (
|
||||
...[...prototypeKeyOmissions].map(
|
||||
([path, count]) => `${path}: ${omittedPrototypeKeysLimitation(count)}`,
|
||||
),
|
||||
...[...unknownDates].map(
|
||||
([path, count]) => `${path}: ${unknownDateLimitation(count)}`,
|
||||
),
|
||||
...omittedArchives.map(
|
||||
({ path, reason }) =>
|
||||
`${path}: omitted because ${reason.replaceAll("_", " ")}.`,
|
||||
@@ -693,6 +708,7 @@ const decodePlist = (
|
||||
): {
|
||||
readonly value: JsonValue;
|
||||
readonly omittedPrototypeKeys: number | null;
|
||||
readonly unknownDateCount: number;
|
||||
} => {
|
||||
const binary = hasBinaryPlistSignature(bytes);
|
||||
const { value, omittedPrototypeKeys } = binary
|
||||
@@ -701,5 +717,10 @@ const decodePlist = (
|
||||
omittedPrototypeKeys: binaryPrototypeKeyObserved ? null : 0,
|
||||
}
|
||||
: parseXmlPropertyList(xmlText ?? decodeXmlPlistText(bytes));
|
||||
return { value: projectPlistValue(value).value, omittedPrototypeKeys };
|
||||
const projected = projectPlistValue(value);
|
||||
return {
|
||||
value: projected.value,
|
||||
omittedPrototypeKeys,
|
||||
unknownDateCount: projected.unknownDateCount,
|
||||
};
|
||||
};
|
||||
@@ -6,7 +6,10 @@ import {
|
||||
AnalysisInputError,
|
||||
AnalysisUnsupportedTargetError,
|
||||
} from "../../domain/analysisErrorCore.js";
|
||||
import { projectPlistValue } from "../../domain/apple/plistValue.js";
|
||||
import {
|
||||
projectPlistValue,
|
||||
unknownDateLimitation,
|
||||
} from "../../domain/apple/plistValue.js";
|
||||
import { createPlistNumberProjection } from "../../domain/apple/plistNumbers.js";
|
||||
import {
|
||||
binaryPlistNumberLiterals,
|
||||
@@ -75,9 +78,11 @@ export const decodeKeyedArchiveBytes = (
|
||||
: 0,
|
||||
}
|
||||
: parseXmlPropertyList(xmlText ?? "");
|
||||
const { value: plistValue, unknownRealCount } = projectPlistValue(
|
||||
parsed.value,
|
||||
);
|
||||
const {
|
||||
value: plistValue,
|
||||
unknownRealCount,
|
||||
unknownDateCount,
|
||||
} = projectPlistValue(parsed.value);
|
||||
const graph = projectKeyedArchive(plistValue, selection);
|
||||
const numbers = createPlistNumberProjection(
|
||||
binary
|
||||
@@ -112,6 +117,9 @@ export const decodeKeyedArchiveBytes = (
|
||||
: [
|
||||
`${String(unknownRealCount)} non-finite real value(s) are reported as { "$plist_type": "real", "value": null } because JSON evidence cannot preserve NaN vs infinity.`,
|
||||
]),
|
||||
...(unknownDateCount === 0
|
||||
? []
|
||||
: [unknownDateLimitation(unknownDateCount)]),
|
||||
],
|
||||
};
|
||||
};
|
||||
|
||||
@@ -1,5 +1,10 @@
|
||||
import { afterEach, expect, it, vi } from "vitest";
|
||||
import type { Browser } from "playwright-core";
|
||||
import type {
|
||||
Browser,
|
||||
BrowserContext,
|
||||
CDPSession,
|
||||
Page,
|
||||
} from "playwright-core";
|
||||
|
||||
import { browserScenarioSchema } from "../domain/browserScenario.js";
|
||||
import {
|
||||
@@ -157,11 +162,12 @@ it("keeps attached-target ownership after the cleanup response timeout", async (
|
||||
|
||||
it("does not configure a late attached connection after cancellation", async () => {
|
||||
const connection =
|
||||
deferred<Pick<Browser, "contexts" | "close" | "version">>();
|
||||
deferred<Pick<Browser, "contexts" | "close" | "isConnected" | "version">>();
|
||||
const connectionStarted = deferred<void>();
|
||||
const connectedBrowser = {
|
||||
close: vi.fn(async () => undefined),
|
||||
contexts: vi.fn(() => []),
|
||||
isConnected: vi.fn(() => true),
|
||||
version: () => "fixture",
|
||||
};
|
||||
const controller = new AbortController();
|
||||
@@ -211,6 +217,7 @@ it("closes an attached connection when cancellation starts target discovery", as
|
||||
controller.abort();
|
||||
return [];
|
||||
}),
|
||||
isConnected: vi.fn(() => true),
|
||||
version: () => "fixture",
|
||||
};
|
||||
const launcher = {
|
||||
@@ -245,6 +252,265 @@ it("closes an attached connection when cancellation starts target discovery", as
|
||||
expect(connectedBrowser.close).toHaveBeenCalledOnce();
|
||||
});
|
||||
|
||||
const attachedBrowserFixture = (
|
||||
callbacks: {
|
||||
readonly send?: (method: string, parameters?: unknown) => Promise<unknown>;
|
||||
readonly emulateMedia?: (
|
||||
options: Parameters<Page["emulateMedia"]>[0],
|
||||
) => Promise<void>;
|
||||
readonly detach?: () => Promise<void>;
|
||||
readonly closeBrowser?: () => Promise<void>;
|
||||
readonly isTargetClosed?: () => boolean;
|
||||
readonly isBrowserConnected?: () => boolean;
|
||||
} = {},
|
||||
) => {
|
||||
const discoverySession = {
|
||||
send: vi.fn(async () => ({ targetInfo: { targetId: "page-1" } })),
|
||||
detach: vi.fn(async () => undefined),
|
||||
} as unknown as CDPSession;
|
||||
const environmentSession = {
|
||||
send: vi.fn(callbacks.send ?? (async () => undefined)),
|
||||
detach: vi.fn(callbacks.detach ?? (async () => undefined)),
|
||||
} as unknown as CDPSession;
|
||||
const page = {
|
||||
emulateMedia: vi.fn(callbacks.emulateMedia ?? (async () => undefined)),
|
||||
isClosed: vi.fn(callbacks.isTargetClosed ?? (() => false)),
|
||||
} as unknown as Page;
|
||||
const context = {
|
||||
pages: () => [page],
|
||||
newCDPSession: vi
|
||||
.fn()
|
||||
.mockResolvedValueOnce(discoverySession)
|
||||
.mockResolvedValueOnce(environmentSession),
|
||||
} as unknown as BrowserContext;
|
||||
const browser = {
|
||||
close: vi.fn(callbacks.closeBrowser ?? (async () => undefined)),
|
||||
contexts: () => [context],
|
||||
isConnected: vi.fn(callbacks.isBrowserConnected ?? (() => true)),
|
||||
version: () => "fixture",
|
||||
} as unknown as Pick<
|
||||
Browser,
|
||||
"contexts" | "close" | "isConnected" | "version"
|
||||
>;
|
||||
const scenario = browserScenarioSchema.parse({
|
||||
browser: {
|
||||
mode: "connect",
|
||||
cdp_endpoint: "http://127.0.0.1:9222",
|
||||
target_id: "page-1",
|
||||
},
|
||||
start_url: { url: "https://example.test" },
|
||||
actions: [{ step_id: "wait", action: "wait_for_timeout", duration_ms: 1 }],
|
||||
});
|
||||
return {
|
||||
browser,
|
||||
environmentSession,
|
||||
page,
|
||||
open: (signal?: AbortSignal) =>
|
||||
openPlaywrightScenarioBrowser(
|
||||
scenario,
|
||||
{},
|
||||
{
|
||||
launcher: {
|
||||
connectOverCDP: async () => browser,
|
||||
launchPersistentContext: async () => {
|
||||
throw new Error("Unexpected launch");
|
||||
},
|
||||
},
|
||||
signal,
|
||||
},
|
||||
),
|
||||
};
|
||||
};
|
||||
|
||||
it("restores its environment overrides before releasing an attached browser", async () => {
|
||||
const cleanupOrder: string[] = [];
|
||||
const fixture = attachedBrowserFixture({
|
||||
send: async (method, parameters) => {
|
||||
if (
|
||||
method === "Emulation.setTimezoneOverride" &&
|
||||
(parameters as { timezoneId?: string }).timezoneId === ""
|
||||
)
|
||||
cleanupOrder.push("timezone");
|
||||
if (
|
||||
method === "Emulation.setLocaleOverride" &&
|
||||
(parameters as { locale?: string }).locale === ""
|
||||
)
|
||||
cleanupOrder.push("locale");
|
||||
if (method === "Emulation.clearDeviceMetricsOverride")
|
||||
cleanupOrder.push("device-metrics");
|
||||
},
|
||||
emulateMedia: async (options) => {
|
||||
if (options?.colorScheme === null) cleanupOrder.push("media");
|
||||
},
|
||||
detach: async () => {
|
||||
cleanupOrder.push("session");
|
||||
},
|
||||
closeBrowser: async () => {
|
||||
cleanupOrder.push("browser");
|
||||
},
|
||||
});
|
||||
const opened = await fixture.open();
|
||||
await opened.cleanup.close();
|
||||
|
||||
expect(fixture.environmentSession.send).toHaveBeenCalledWith(
|
||||
"Emulation.setLocaleOverride",
|
||||
{ locale: "en-US" },
|
||||
);
|
||||
expect(fixture.environmentSession.send).toHaveBeenCalledWith(
|
||||
"Emulation.setLocaleOverride",
|
||||
{ locale: "" },
|
||||
);
|
||||
expect(fixture.environmentSession.send).toHaveBeenCalledWith(
|
||||
"Emulation.setTimezoneOverride",
|
||||
{ timezoneId: "" },
|
||||
);
|
||||
expect(fixture.environmentSession.send).toHaveBeenCalledWith(
|
||||
"Emulation.clearDeviceMetricsOverride",
|
||||
);
|
||||
expect(fixture.page.emulateMedia).toHaveBeenLastCalledWith({
|
||||
colorScheme: null,
|
||||
reducedMotion: null,
|
||||
});
|
||||
expect(fixture.environmentSession.detach).toHaveBeenCalledOnce();
|
||||
expect(fixture.browser.close).toHaveBeenCalledOnce();
|
||||
expect(cleanupOrder).toEqual([
|
||||
"timezone",
|
||||
"locale",
|
||||
"device-metrics",
|
||||
"media",
|
||||
"session",
|
||||
"browser",
|
||||
]);
|
||||
});
|
||||
|
||||
it("retains attached environment ownership until a failed reset can be retried", async () => {
|
||||
let localeResetAttempts = 0;
|
||||
const fixture = attachedBrowserFixture({
|
||||
send: async (method, parameters) => {
|
||||
if (
|
||||
method === "Emulation.setLocaleOverride" &&
|
||||
(parameters as { locale?: string }).locale === ""
|
||||
) {
|
||||
localeResetAttempts += 1;
|
||||
if (localeResetAttempts === 1) throw new Error("reset interrupted");
|
||||
}
|
||||
},
|
||||
});
|
||||
const opened = await fixture.open();
|
||||
|
||||
await expect(opened.cleanup.close()).rejects.toMatchObject({
|
||||
reason: "cleanup_failed",
|
||||
cleanup: { reason: "reset interrupted" },
|
||||
});
|
||||
expect(fixture.browser.close).not.toHaveBeenCalled();
|
||||
expect(fixture.environmentSession.detach).not.toHaveBeenCalled();
|
||||
|
||||
await opened.cleanup.close();
|
||||
expect(localeResetAttempts).toBe(2);
|
||||
expect(fixture.environmentSession.detach).toHaveBeenCalledOnce();
|
||||
expect(fixture.browser.close).toHaveBeenCalledOnce();
|
||||
});
|
||||
|
||||
it("does not clear an attached environment override it failed to acquire", async () => {
|
||||
const fixture = attachedBrowserFixture({
|
||||
send: async (method, parameters) => {
|
||||
if (
|
||||
method === "Emulation.setLocaleOverride" &&
|
||||
(parameters as { locale?: string }).locale === "en-US"
|
||||
)
|
||||
throw new Error("caller already owns locale");
|
||||
},
|
||||
});
|
||||
|
||||
await expect(fixture.open()).rejects.toThrow("caller already owns locale");
|
||||
expect(fixture.environmentSession.send).not.toHaveBeenCalledWith(
|
||||
"Emulation.setLocaleOverride",
|
||||
{ locale: "" },
|
||||
);
|
||||
expect(fixture.environmentSession.send).toHaveBeenCalledWith(
|
||||
"Emulation.clearDeviceMetricsOverride",
|
||||
);
|
||||
expect(fixture.environmentSession.detach).toHaveBeenCalledOnce();
|
||||
expect(fixture.browser.close).toHaveBeenCalledOnce();
|
||||
});
|
||||
|
||||
it("waits for a cancelled environment mutation before restoring it", async () => {
|
||||
const localeStarted = deferred<void>();
|
||||
const localeResult = deferred<void>();
|
||||
const fixture = attachedBrowserFixture({
|
||||
send: async (method, parameters) => {
|
||||
if (
|
||||
method === "Emulation.setLocaleOverride" &&
|
||||
(parameters as { locale?: string }).locale === "en-US"
|
||||
) {
|
||||
localeStarted.resolve(undefined);
|
||||
await localeResult.promise;
|
||||
}
|
||||
},
|
||||
});
|
||||
const controller = new AbortController();
|
||||
const opening = fixture.open(controller.signal);
|
||||
await localeStarted.promise;
|
||||
const cancelled = expect(opening).rejects.toMatchObject({
|
||||
_tag: "AnalysisCancelledError",
|
||||
});
|
||||
controller.abort();
|
||||
await new Promise((resolve) => setImmediate(resolve));
|
||||
expect(fixture.environmentSession.send).not.toHaveBeenCalledWith(
|
||||
"Emulation.setLocaleOverride",
|
||||
{ locale: "" },
|
||||
);
|
||||
|
||||
localeResult.resolve(undefined);
|
||||
await cancelled;
|
||||
expect(fixture.environmentSession.send).toHaveBeenCalledWith(
|
||||
"Emulation.setLocaleOverride",
|
||||
{ locale: "" },
|
||||
);
|
||||
expect(fixture.environmentSession.detach).toHaveBeenCalledOnce();
|
||||
expect(fixture.browser.close).toHaveBeenCalledOnce();
|
||||
});
|
||||
|
||||
it("releases cleanup ownership when the attached target is destroyed", async () => {
|
||||
let targetClosed = false;
|
||||
const fixture = attachedBrowserFixture({
|
||||
send: async (method, parameters) => {
|
||||
if (
|
||||
targetClosed &&
|
||||
method === "Emulation.setTimezoneOverride" &&
|
||||
(parameters as { timezoneId?: string }).timezoneId === ""
|
||||
)
|
||||
throw new Error("Target closed");
|
||||
},
|
||||
isTargetClosed: () => targetClosed,
|
||||
});
|
||||
const opened = await fixture.open();
|
||||
targetClosed = true;
|
||||
|
||||
await opened.cleanup.close();
|
||||
expect(fixture.browser.close).toHaveBeenCalledOnce();
|
||||
});
|
||||
|
||||
it("releases cleanup ownership when the attached browser disconnects", async () => {
|
||||
let connected = true;
|
||||
const fixture = attachedBrowserFixture({
|
||||
send: async (method, parameters) => {
|
||||
if (
|
||||
!connected &&
|
||||
method === "Emulation.setTimezoneOverride" &&
|
||||
(parameters as { timezoneId?: string }).timezoneId === ""
|
||||
)
|
||||
throw new Error("Browser disconnected");
|
||||
},
|
||||
isBrowserConnected: () => connected,
|
||||
});
|
||||
const opened = await fixture.open();
|
||||
connected = false;
|
||||
|
||||
await opened.cleanup.close();
|
||||
expect(fixture.browser.close).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("cancels same-target queued connection before acquiring a browser", async () => {
|
||||
const makeScenario = (targetId: string) =>
|
||||
browserScenarioSchema.parse({
|
||||
@@ -350,7 +616,7 @@ it("serializes concurrent attach across loopback spellings of one browser", asyn
|
||||
],
|
||||
});
|
||||
const firstConnection =
|
||||
deferred<Pick<Browser, "contexts" | "close" | "version">>();
|
||||
deferred<Pick<Browser, "contexts" | "close" | "isConnected" | "version">>();
|
||||
let rejectFirstConnection: ((cause: Error) => void) | undefined;
|
||||
const failingConnection = new Promise<never>((_resolve, reject) => {
|
||||
rejectFirstConnection = reject;
|
||||
@@ -399,6 +665,7 @@ it("serializes concurrent attach across loopback spellings of one browser", asyn
|
||||
firstConnection.resolve({
|
||||
close: vi.fn(async () => undefined),
|
||||
contexts: vi.fn(() => []),
|
||||
isConnected: vi.fn(() => true),
|
||||
version: () => "fixture",
|
||||
});
|
||||
await expect(first).rejects.toThrow();
|
||||
|
||||
@@ -2,7 +2,13 @@ import { chmod, mkdtemp, rm } from "node:fs/promises";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join, resolve } from "node:path";
|
||||
|
||||
import type { Browser, BrowserContext, Page, chromium } from "playwright-core";
|
||||
import type {
|
||||
Browser,
|
||||
BrowserContext,
|
||||
CDPSession,
|
||||
Page,
|
||||
chromium,
|
||||
} from "playwright-core";
|
||||
|
||||
import type { BrowserScenario } from "../domain/browserScenario.js";
|
||||
import { AnalysisError } from "../domain/analysisErrorBase.js";
|
||||
@@ -314,39 +320,148 @@ const findConnectedPage = async (
|
||||
throw new BrowserObservationError(OPERATION, "target_not_found");
|
||||
};
|
||||
|
||||
const configureAttachedEnvironment = async (
|
||||
context: BrowserContext,
|
||||
page: Page,
|
||||
environment: BrowserScenario["environment"],
|
||||
signal: AbortSignal | undefined,
|
||||
): Promise<void> => {
|
||||
// setViewportSize also resizes the external browser window. The scenario's
|
||||
// CDP metrics override below supplies the viewport and expires on disconnect.
|
||||
await page.emulateMedia({
|
||||
colorScheme: environment.color_scheme,
|
||||
reducedMotion: environment.reduced_motion,
|
||||
});
|
||||
throwIfScenarioCancelled(signal);
|
||||
const session = await context.newCDPSession(page);
|
||||
throwIfScenarioCancelled(signal);
|
||||
// Chromium resets these overrides when the CDP session detaches. Keep it on
|
||||
// the scenario transport until cleanup disconnects the attached browser.
|
||||
await session.send("Emulation.setDeviceMetricsOverride", {
|
||||
width: environment.viewport.width,
|
||||
height: environment.viewport.height,
|
||||
deviceScaleFactor: environment.viewport.device_scale_factor,
|
||||
mobile: false,
|
||||
dontSetVisibleSize: true,
|
||||
});
|
||||
throwIfScenarioCancelled(signal);
|
||||
await session.send("Emulation.setLocaleOverride", {
|
||||
locale: environment.locale,
|
||||
});
|
||||
throwIfScenarioCancelled(signal);
|
||||
await session.send("Emulation.setTimezoneOverride", {
|
||||
timezoneId: environment.timezone,
|
||||
});
|
||||
};
|
||||
/** Owns the target state changed while one scenario uses an attached page. */
|
||||
class AttachedScenarioEnvironmentOwner {
|
||||
#mediaOverride = false;
|
||||
#deviceMetricsOverride = false;
|
||||
#localeOverride = false;
|
||||
#timezoneOverride = false;
|
||||
#detached = false;
|
||||
#application: Promise<void> | undefined;
|
||||
|
||||
private constructor(
|
||||
private readonly page: Page,
|
||||
private readonly session: CDPSession,
|
||||
) {}
|
||||
|
||||
static async create(
|
||||
context: BrowserContext,
|
||||
page: Page,
|
||||
): Promise<AttachedScenarioEnvironmentOwner> {
|
||||
return new AttachedScenarioEnvironmentOwner(
|
||||
page,
|
||||
await context.newCDPSession(page),
|
||||
);
|
||||
}
|
||||
|
||||
get restored(): boolean {
|
||||
return (
|
||||
!this.#mediaOverride &&
|
||||
!this.#deviceMetricsOverride &&
|
||||
!this.#localeOverride &&
|
||||
!this.#timezoneOverride
|
||||
);
|
||||
}
|
||||
|
||||
get targetClosed(): boolean {
|
||||
return this.page.isClosed();
|
||||
}
|
||||
|
||||
apply(
|
||||
environment: BrowserScenario["environment"],
|
||||
signal: AbortSignal | undefined,
|
||||
): Promise<void> {
|
||||
const application =
|
||||
this.#application ?? this.#applyEnvironment(environment, signal);
|
||||
this.#application = application;
|
||||
return application;
|
||||
}
|
||||
|
||||
async #applyEnvironment(
|
||||
environment: BrowserScenario["environment"],
|
||||
signal: AbortSignal | undefined,
|
||||
): Promise<void> {
|
||||
await this.page.emulateMedia({
|
||||
colorScheme: environment.color_scheme,
|
||||
reducedMotion: environment.reduced_motion,
|
||||
});
|
||||
this.#mediaOverride = true;
|
||||
throwIfScenarioCancelled(signal);
|
||||
|
||||
// setViewportSize also resizes the external browser window. CDP metrics
|
||||
// provide the scenario viewport without changing that caller-owned window.
|
||||
await this.session.send("Emulation.setDeviceMetricsOverride", {
|
||||
width: environment.viewport.width,
|
||||
height: environment.viewport.height,
|
||||
deviceScaleFactor: environment.viewport.device_scale_factor,
|
||||
mobile: false,
|
||||
dontSetVisibleSize: true,
|
||||
});
|
||||
this.#deviceMetricsOverride = true;
|
||||
throwIfScenarioCancelled(signal);
|
||||
|
||||
await this.session.send("Emulation.setLocaleOverride", {
|
||||
locale: environment.locale,
|
||||
});
|
||||
this.#localeOverride = true;
|
||||
throwIfScenarioCancelled(signal);
|
||||
|
||||
await this.session.send("Emulation.setTimezoneOverride", {
|
||||
timezoneId: environment.timezone,
|
||||
});
|
||||
this.#timezoneOverride = true;
|
||||
throwIfScenarioCancelled(signal);
|
||||
}
|
||||
|
||||
async close(): Promise<void> {
|
||||
if (this.#detached) return;
|
||||
// The execution boundary can report cancellation before a pending CDP
|
||||
// command settles. Wait for that producer so every successful mutation is
|
||||
// recorded before deciding which state this owner must restore.
|
||||
try {
|
||||
await this.#application;
|
||||
} catch {
|
||||
// The operation path reports application failures. Cleanup still owns
|
||||
// and restores every mutation that completed before the failure.
|
||||
}
|
||||
const failures: unknown[] = [];
|
||||
if (this.#timezoneOverride) {
|
||||
try {
|
||||
await this.session.send("Emulation.setTimezoneOverride", {
|
||||
timezoneId: "",
|
||||
});
|
||||
this.#timezoneOverride = false;
|
||||
} catch (cause: unknown) {
|
||||
failures.push(cause);
|
||||
}
|
||||
}
|
||||
if (this.#localeOverride) {
|
||||
try {
|
||||
await this.session.send("Emulation.setLocaleOverride", { locale: "" });
|
||||
this.#localeOverride = false;
|
||||
} catch (cause: unknown) {
|
||||
failures.push(cause);
|
||||
}
|
||||
}
|
||||
if (this.#deviceMetricsOverride) {
|
||||
try {
|
||||
await this.session.send("Emulation.clearDeviceMetricsOverride");
|
||||
this.#deviceMetricsOverride = false;
|
||||
} catch (cause: unknown) {
|
||||
failures.push(cause);
|
||||
}
|
||||
}
|
||||
if (this.#mediaOverride) {
|
||||
try {
|
||||
await this.page.emulateMedia({
|
||||
colorScheme: null,
|
||||
reducedMotion: null,
|
||||
});
|
||||
this.#mediaOverride = false;
|
||||
} catch (cause: unknown) {
|
||||
failures.push(cause);
|
||||
}
|
||||
}
|
||||
if (failures.length === 1) throw failures[0];
|
||||
if (failures.length > 1)
|
||||
throw new AggregateError(
|
||||
failures,
|
||||
"Attached browser environment restoration failed",
|
||||
);
|
||||
await this.session.detach();
|
||||
this.#detached = true;
|
||||
}
|
||||
}
|
||||
|
||||
/** Preserve an operation failure while reporting any incomplete cleanup. */
|
||||
export const failBrowserScenarioOperation = async (
|
||||
@@ -389,7 +504,7 @@ type ScenarioBrowserLauncher = Pick<
|
||||
> & {
|
||||
readonly connectOverCDP: (
|
||||
endpoint: string,
|
||||
) => Promise<Pick<Browser, "contexts" | "close" | "version">>;
|
||||
) => Promise<Pick<Browser, "contexts" | "close" | "isConnected" | "version">>;
|
||||
};
|
||||
|
||||
type AttachedBrowserRequest = Extract<
|
||||
@@ -406,6 +521,7 @@ const openAttachedScenarioBrowser = async (
|
||||
): Promise<OpenedScenarioBrowser> => {
|
||||
const lease = await acquireAttachedScenario(browserRequest, signal);
|
||||
let cleanupOwner: PlaywrightScenarioBrowserCleanupOwner | undefined;
|
||||
let environmentOwner: AttachedScenarioEnvironmentOwner | undefined;
|
||||
try {
|
||||
const priorOwner = lease.admission.cleanupOwner;
|
||||
if (priorOwner !== undefined) await priorOwner.close();
|
||||
@@ -414,7 +530,43 @@ const openAttachedScenarioBrowser = async (
|
||||
const browser = await launcher.connectOverCDP(browserRequest.cdp_endpoint);
|
||||
const cleanup: PlaywrightScenarioBrowserCleanupOwner =
|
||||
new PlaywrightScenarioBrowserCleanupOwner({
|
||||
closeBrowser: () => browser.close(),
|
||||
closeBrowser: async () => {
|
||||
let environmentFailure: unknown;
|
||||
const ownedEnvironment = environmentOwner;
|
||||
if (ownedEnvironment !== undefined) {
|
||||
try {
|
||||
await ownedEnvironment.close();
|
||||
environmentOwner = undefined;
|
||||
} catch (cause: unknown) {
|
||||
environmentFailure = cause;
|
||||
const browserConnected = browser.isConnected();
|
||||
if (ownedEnvironment.targetClosed || !browserConnected) {
|
||||
// A destroyed target has no remaining emulation state. A lost
|
||||
// transport cannot be retained or retried; release its local
|
||||
// owner so later discovery can establish current target state.
|
||||
environmentOwner = undefined;
|
||||
if (!browserConnected) return;
|
||||
} else if (!ownedEnvironment.restored) {
|
||||
// Keep the live transport and target session for a cleanup
|
||||
// retry while any target override remains.
|
||||
throw cause;
|
||||
}
|
||||
}
|
||||
}
|
||||
try {
|
||||
await browser.close();
|
||||
// Once target state is restored, disconnecting the transport is a
|
||||
// definitive fallback for an explicit session detach failure.
|
||||
environmentOwner = undefined;
|
||||
} catch (browserFailure: unknown) {
|
||||
if (environmentFailure !== undefined)
|
||||
throw new AggregateError(
|
||||
[environmentFailure, browserFailure],
|
||||
"Attached browser environment cleanup failed",
|
||||
);
|
||||
throw browserFailure;
|
||||
}
|
||||
},
|
||||
onSettled: (browserClosed) => {
|
||||
if (browserClosed && lease.admission.cleanupOwner === cleanup)
|
||||
lease.admission.cleanupOwner = undefined;
|
||||
@@ -431,14 +583,15 @@ const openAttachedScenarioBrowser = async (
|
||||
signal,
|
||||
);
|
||||
throwIfScenarioCancelled(signal);
|
||||
await withPlaywrightExecutionBoundary(
|
||||
const configuredEnvironment = await withPlaywrightExecutionBoundary(
|
||||
() =>
|
||||
configureAttachedEnvironment(
|
||||
target.context,
|
||||
target.page,
|
||||
environment,
|
||||
signal,
|
||||
),
|
||||
AttachedScenarioEnvironmentOwner.create(target.context, target.page),
|
||||
undefined,
|
||||
signal,
|
||||
);
|
||||
environmentOwner = configuredEnvironment;
|
||||
await withPlaywrightExecutionBoundary(
|
||||
() => configuredEnvironment.apply(environment, signal),
|
||||
undefined,
|
||||
signal,
|
||||
);
|
||||
|
||||
+47
-7
@@ -3,13 +3,40 @@ const UNSUPPORTED_OUTPUT_COMBINATION_MESSAGE =
|
||||
|
||||
type StructuredOutputFormat = "json" | "jsonl" | "yaml";
|
||||
const INCUR_OUTPUT_FORMATS = new Set(["toon", "json", "yaml", "md", "jsonl"]);
|
||||
/** Global output flags that take a value. */
|
||||
const VALUED_OUTPUT_FLAGS = new Set([
|
||||
"--format",
|
||||
"--filter-output",
|
||||
"--token-limit",
|
||||
"--token-offset",
|
||||
]);
|
||||
|
||||
/**
|
||||
* Split `--flag=value` forms of the valued output flags. Incur reads these
|
||||
* flags only in spaced form, so `--format=json` otherwise reaches the command
|
||||
* parser and fails as an unknown flag. Command options accept both forms.
|
||||
*/
|
||||
export const normalizeCliOutputArguments = (
|
||||
arguments_: readonly string[],
|
||||
): string[] =>
|
||||
arguments_.flatMap((argument) => {
|
||||
const separator = argument.indexOf("=");
|
||||
if (separator < 0) return [argument];
|
||||
const flag = argument.slice(0, separator);
|
||||
return VALUED_OUTPUT_FLAGS.has(flag)
|
||||
? [flag, argument.slice(separator + 1)]
|
||||
: [argument];
|
||||
});
|
||||
|
||||
export type CliOutputArgumentValidation =
|
||||
| { readonly ok: true }
|
||||
| {
|
||||
readonly ok: false;
|
||||
readonly format: StructuredOutputFormat;
|
||||
readonly code: "UNSUPPORTED_OUTPUT_COMBINATION" | "INVALID_OUTPUT_FILTER";
|
||||
readonly code:
|
||||
| "UNSUPPORTED_OUTPUT_COMBINATION"
|
||||
| "INVALID_OUTPUT_FILTER"
|
||||
| "MISSING_OUTPUT_VALUE";
|
||||
readonly message: string;
|
||||
};
|
||||
|
||||
@@ -17,6 +44,8 @@ interface ParsedCliOutputArguments {
|
||||
readonly filterOutput: string | undefined;
|
||||
readonly format: string;
|
||||
readonly fullOutput: boolean;
|
||||
/** First valued output flag that Incur skips because it has no value. */
|
||||
readonly missingValue: string | undefined;
|
||||
readonly parseError: boolean;
|
||||
readonly tokenCount: boolean;
|
||||
readonly tokenWindow: boolean;
|
||||
@@ -31,6 +60,7 @@ export const parseCliOutputArguments = (
|
||||
let format: string = "toon";
|
||||
let filterOutput: string | undefined;
|
||||
let fullOutput = false;
|
||||
let missingValue: string | undefined;
|
||||
let tokenCount = false;
|
||||
let tokenLimit: number | undefined;
|
||||
let tokenOffset: number | undefined;
|
||||
@@ -38,6 +68,7 @@ export const parseCliOutputArguments = (
|
||||
filterOutput,
|
||||
format,
|
||||
fullOutput,
|
||||
missingValue,
|
||||
parseError,
|
||||
tokenCount,
|
||||
tokenWindow:
|
||||
@@ -47,8 +78,8 @@ export const parseCliOutputArguments = (
|
||||
});
|
||||
for (let index = 0; index < arguments_.length; index += 1) {
|
||||
const argument = arguments_[index];
|
||||
// Incur scans builtins across `--` and accepts only spaced value forms.
|
||||
// Mirror those effective values at the executable output boundary.
|
||||
// Incur scans builtins across `--` and reads spaced value forms, which
|
||||
// normalizeCliOutputArguments has produced. Mirror those effective values.
|
||||
if (argument === "--json") {
|
||||
format = "json";
|
||||
continue;
|
||||
@@ -61,14 +92,14 @@ export const parseCliOutputArguments = (
|
||||
if (!INCUR_OUTPUT_FORMATS.has(value)) return parsed(true);
|
||||
format = value;
|
||||
index += 1;
|
||||
}
|
||||
} else missingValue ??= argument;
|
||||
continue;
|
||||
}
|
||||
if (argument === "--filter-output") {
|
||||
if (arguments_[index + 1]) {
|
||||
filterOutput = arguments_[index + 1];
|
||||
index += 1;
|
||||
}
|
||||
} else missingValue ??= argument;
|
||||
continue;
|
||||
}
|
||||
if (argument === "--token-limit" || argument === "--token-offset") {
|
||||
@@ -80,7 +111,7 @@ export const parseCliOutputArguments = (
|
||||
if (argument === "--token-limit") tokenLimit = numericValue;
|
||||
else tokenOffset = numericValue;
|
||||
index += 1;
|
||||
}
|
||||
} else missingValue ??= argument;
|
||||
continue;
|
||||
}
|
||||
if (argument === "--full-output") fullOutput = true;
|
||||
@@ -111,7 +142,7 @@ const hasUnclosedFilterSlice = (expression: string): boolean => {
|
||||
export const validateCliOutputArguments = (
|
||||
arguments_: readonly string[],
|
||||
): CliOutputArgumentValidation => {
|
||||
const { filterOutput, format, parseError, tokenWindow } =
|
||||
const { filterOutput, format, missingValue, parseError, tokenWindow } =
|
||||
parseCliOutputArguments(arguments_);
|
||||
if (parseError) return { ok: true };
|
||||
if (filterOutput !== undefined && hasUnclosedFilterSlice(filterOutput))
|
||||
@@ -132,6 +163,15 @@ export const validateCliOutputArguments = (
|
||||
code: "UNSUPPORTED_OUTPUT_COMBINATION",
|
||||
message: UNSUPPORTED_OUTPUT_COMBINATION_MESSAGE,
|
||||
};
|
||||
// Incur skips a valued flag without a value and then reports the flag
|
||||
// itself as unknown. Name the missing value instead.
|
||||
if (missingValue !== undefined)
|
||||
return {
|
||||
ok: false,
|
||||
format: format === "yaml" || format === "jsonl" ? format : "json",
|
||||
code: "MISSING_OUTPUT_VALUE",
|
||||
message: `${missingValue} requires a value, as in ${missingValue} <value> or ${missingValue}=<value>.`,
|
||||
};
|
||||
return { ok: true };
|
||||
};
|
||||
|
||||
|
||||
@@ -1,10 +1,7 @@
|
||||
import { moduleResolutionModeForJavaScriptReference } from "../domain/javascript/javascriptModuleResolutionMode.js";
|
||||
import { parseJavaScriptSource } from "../domain/javascript/javascriptSourceParser.js";
|
||||
import { analyzeParsedJavaScriptStaticSourceSteps } from "../domain/javascript/javascriptStaticAnalysis.js";
|
||||
import {
|
||||
analyzeParsedJavaScriptSemanticsSteps,
|
||||
classifyParsedJavaScriptOpenReceiversSteps,
|
||||
} from "../domain/javascript/javascriptSemanticAnalysis.js";
|
||||
import { analyzeParsedJavaScriptSemanticsSteps } from "../domain/javascript/javascriptSemanticAnalysis.js";
|
||||
import { failedJavaScriptStaticAnalysis } from "../domain/javascript/javascriptStaticAnalysisHelpers.js";
|
||||
import { projectJavaScriptModuleSemantics } from "../domain/javascript/javascriptModuleSemanticIr.js";
|
||||
import {
|
||||
@@ -33,15 +30,8 @@ const analyze: JavaScriptSourceWorkerAnalyzer = async (
|
||||
return null;
|
||||
}
|
||||
await callbacks.phase("static");
|
||||
const receivers = await completeJavaScriptAnalysisSteps(
|
||||
classifyParsedJavaScriptOpenReceiversSteps(parsed),
|
||||
);
|
||||
const facts = await completeJavaScriptAnalysisSteps(
|
||||
analyzeParsedJavaScriptStaticSourceSteps(
|
||||
file.text.value,
|
||||
parsed,
|
||||
receivers,
|
||||
),
|
||||
analyzeParsedJavaScriptStaticSourceSteps(file.text.value, parsed),
|
||||
);
|
||||
const resultBudgetBytes = await callbacks.checkpoint(facts);
|
||||
if (facts.parse_status === "failed") return null;
|
||||
|
||||
@@ -527,7 +527,13 @@ export const adbResultSchemas = {
|
||||
serial: z.string().min(1),
|
||||
package_name: packageNameSchema,
|
||||
version_name: z.string().nullable(),
|
||||
version_code: z.number().int().nullable(),
|
||||
version_code: z
|
||||
.string()
|
||||
.regex(/^-?\d+$/u)
|
||||
.nullable()
|
||||
.describe(
|
||||
"Long version code exactly as dumpsys package prints it; may exceed the JSON safe-integer range",
|
||||
),
|
||||
first_install_time: z.string().nullable(),
|
||||
last_update_time: z.string().nullable(),
|
||||
installer_package_name: z.string().nullable(),
|
||||
|
||||
@@ -422,6 +422,9 @@ const providerErrorDetails = (
|
||||
return {
|
||||
stage: "launch",
|
||||
provider_state: "unknown",
|
||||
...(error.launchRejection === undefined
|
||||
? {}
|
||||
: { launch_rejection: { ...error.launchRejection } }),
|
||||
retry_action: error.ownerRunId === undefined ? "retry" : "unknown",
|
||||
...(error.ownerRunId === undefined
|
||||
? {}
|
||||
|
||||
@@ -2,6 +2,20 @@ import { expect, it } from "vitest";
|
||||
|
||||
import { projectPlistValue } from "./plistValue.js";
|
||||
|
||||
// Binary decoders build dates from seconds; beyond the Date range they are invalid.
|
||||
it("projects an unrepresentable date as unknown and counts it", () => {
|
||||
expect(
|
||||
projectPlistValue({ far: new Date(Number.NaN), near: new Date(0) }),
|
||||
).toEqual({
|
||||
value: {
|
||||
far: { $plist_type: "date", iso8601: null },
|
||||
near: { $plist_type: "date", iso8601: "1970-01-01T00:00:00.000Z" },
|
||||
},
|
||||
unknownRealCount: 0,
|
||||
unknownDateCount: 1,
|
||||
});
|
||||
});
|
||||
|
||||
it("rejects excessive plist nesting without exhausting the stack", () => {
|
||||
const deep: unknown[] = [];
|
||||
let cursor = deep;
|
||||
|
||||
@@ -14,8 +14,14 @@ export interface ProjectedPlistValue {
|
||||
readonly value: JsonValue;
|
||||
/** Reals decoded as NaN, which the decoder also uses for ±infinity. */
|
||||
readonly unknownRealCount: number;
|
||||
/** Dates the decoder could not represent, reported without a timestamp. */
|
||||
readonly unknownDateCount: number;
|
||||
}
|
||||
|
||||
/** Limitation for results whose plist held dates the decoder could not represent. */
|
||||
export const unknownDateLimitation = (count: number): string =>
|
||||
`${String(count)} date value(s) are reported as { "$plist_type": "date", "iso8601": null } because the decoder could not represent them, such as XML dates with years beyond 9999 or negative years, or dates outside the JavaScript date range.`;
|
||||
|
||||
/**
|
||||
* Project a decoded plist into JSON. Data and dates become explicit
|
||||
* `$plist_type` objects; a non-finite real becomes an explicit unknown
|
||||
@@ -23,6 +29,7 @@ export interface ProjectedPlistValue {
|
||||
*/
|
||||
export const projectPlistValue = (value: unknown): ProjectedPlistValue => {
|
||||
let unknownRealCount = 0;
|
||||
let unknownDateCount = 0;
|
||||
const project = (item: unknown, depth: number): JsonValue => {
|
||||
if (depth > MAX_PLIST_DEPTH)
|
||||
throw new RangeError(
|
||||
@@ -33,8 +40,12 @@ export const projectPlistValue = (value: unknown): ProjectedPlistValue => {
|
||||
$plist_type: "data",
|
||||
base64: Buffer.from(item).toString("base64"),
|
||||
};
|
||||
if (item instanceof Date)
|
||||
return { $plist_type: "date", iso8601: item.toISOString() };
|
||||
if (item instanceof Date) {
|
||||
if (!Number.isNaN(item.getTime()))
|
||||
return { $plist_type: "date", iso8601: item.toISOString() };
|
||||
unknownDateCount += 1;
|
||||
return { $plist_type: "date", iso8601: null };
|
||||
}
|
||||
if (typeof item === "number" && !Number.isFinite(item)) {
|
||||
unknownRealCount += 1;
|
||||
return { $plist_type: "real", value: null };
|
||||
@@ -51,5 +62,5 @@ export const projectPlistValue = (value: unknown): ProjectedPlistValue => {
|
||||
return z.union([z.string(), z.number(), z.boolean(), z.null()]).parse(item);
|
||||
};
|
||||
const projected = project(value, 0);
|
||||
return { value: projected, unknownRealCount };
|
||||
return { value: projected, unknownRealCount, unknownDateCount };
|
||||
};
|
||||
@@ -599,11 +599,20 @@ describe("function comparison ordering controls", () => {
|
||||
makeEvidence("e", rightWithoutNativeApi),
|
||||
);
|
||||
|
||||
expect(comparison.status).toBe("unchanged");
|
||||
for (const dimension of ["calls", "references", "strings_names"] as const)
|
||||
for (const dimension of ["calls", "strings_names"] as const)
|
||||
expect(
|
||||
comparison.dimensions.find((item) => item.dimension === dimension),
|
||||
).toMatchObject({ status: "unchanged" });
|
||||
// Unnamed endpoints in different artifacts have no stable identity.
|
||||
expect(
|
||||
comparison.dimensions.find((item) => item.dimension === "references"),
|
||||
).toMatchObject({
|
||||
status: "unknown",
|
||||
limitations: expect.arrayContaining([
|
||||
expect.stringContaining("Unnamed reference endpoints"),
|
||||
]),
|
||||
});
|
||||
expect(comparison.status).toBe("unknown");
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1274,3 +1283,48 @@ it.each([
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
describe("function comparison unnamed reference endpoints", () => {
|
||||
it("distinguishes unnamed reference targets by address within one artifact", () => {
|
||||
const unnamedTarget = (target: string) =>
|
||||
functionDossierSchema.parse({
|
||||
...dossierWithReference("0x1000", "call"),
|
||||
outgoing_references: [
|
||||
{
|
||||
...dossierWithReference("0x1000", "call").outgoing_references[0],
|
||||
target_address: target,
|
||||
target_procedure: null,
|
||||
},
|
||||
],
|
||||
});
|
||||
const reference = (left: string, right: string, digits = ["a", "a"]) =>
|
||||
compareFunctions(
|
||||
observe(digits[0] ?? "a", unnamedTarget(left)),
|
||||
observe(digits[1] ?? "a", unnamedTarget(right)),
|
||||
).dimensions.find(({ dimension }) => dimension === "references");
|
||||
expect(reference("0x1010", "0x1020")).toMatchObject({ status: "changed" });
|
||||
expect(reference("0x1010", "0x1010")).toMatchObject({
|
||||
status: "unchanged",
|
||||
});
|
||||
// Across artifacts the same address does not identify the same target.
|
||||
expect(reference("0x1010", "0x1010", ["a", "b"])).toMatchObject({
|
||||
status: "unknown",
|
||||
});
|
||||
});
|
||||
|
||||
it("keeps known reference-kind changes beside unnamed endpoints", () => {
|
||||
const unnamed = (kind: "call" | "data") => {
|
||||
const edge = dossierWithReference("0x1000", kind).outgoing_references[0];
|
||||
return functionDossierSchema.parse({
|
||||
...dossierWithReference("0x1000", kind),
|
||||
outgoing_references: [{ ...edge, target_procedure: null }],
|
||||
});
|
||||
};
|
||||
expect(
|
||||
compareFunctions(
|
||||
observe("a", unnamed("call")),
|
||||
observe("b", unnamed("data")),
|
||||
).dimensions.find(({ dimension }) => dimension === "references"),
|
||||
).toMatchObject({ status: "changed" });
|
||||
});
|
||||
});
|
||||
@@ -253,13 +253,23 @@ const swiftLiteralCoverage = (
|
||||
? "absent"
|
||||
: "complete";
|
||||
|
||||
const hasUnnamedEndpoint = (snapshot: FunctionSnapshot): boolean =>
|
||||
[
|
||||
...snapshot.dossier.incoming_references,
|
||||
...snapshot.dossier.outgoing_references,
|
||||
].some(
|
||||
(edge) => edge.source_procedure === null || edge.target_procedure === null,
|
||||
);
|
||||
|
||||
const compareReferences = (
|
||||
left: FunctionSnapshot,
|
||||
right: FunctionSnapshot,
|
||||
context: ComparisonContext,
|
||||
): FunctionDimension => {
|
||||
const leftProjection = referenceProjection(left);
|
||||
const rightProjection = referenceProjection(right);
|
||||
const sameArtifact =
|
||||
left.subject.digest.sha256 === right.subject.digest.sha256;
|
||||
const leftProjection = referenceProjection(left, sameArtifact);
|
||||
const rightProjection = referenceProjection(right, sameArtifact);
|
||||
if (leftProjection.length === 0 && rightProjection.length === 0)
|
||||
return compareValues(
|
||||
"references",
|
||||
@@ -284,9 +294,28 @@ const compareReferences = (
|
||||
"Reference endpoints differ independently of provider reference-kind metadata.",
|
||||
],
|
||||
});
|
||||
const leftKinds = referenceKindProjection(left);
|
||||
const rightKinds = referenceKindProjection(right);
|
||||
if (leftKinds !== null && rightKinds !== null)
|
||||
const leftKinds = referenceKindProjection(left, sameArtifact);
|
||||
const rightKinds = referenceKindProjection(right, sameArtifact);
|
||||
const kindsKnown = leftKinds !== null && rightKinds !== null;
|
||||
// Differing known edge kinds are a change whatever the endpoint identity.
|
||||
if (
|
||||
kindsKnown &&
|
||||
canonicalJson(leftKinds, "Function comparison") !==
|
||||
canonicalJson(rightKinds, "Function comparison")
|
||||
)
|
||||
return compareValues("references", leftKinds, rightKinds, context);
|
||||
// Equal projections with unknown endpoints do not establish equal targets.
|
||||
if (!sameArtifact && (hasUnnamedEndpoint(left) || hasUnnamedEndpoint(right)))
|
||||
return unresolvedDimension({
|
||||
dimension: "references",
|
||||
links: context.links,
|
||||
leftCount: leftProjection.length,
|
||||
rightCount: rightProjection.length,
|
||||
limitations: [
|
||||
"Unnamed reference endpoints have no stable identity across different artifacts, so equal named endpoints do not establish equal references.",
|
||||
],
|
||||
});
|
||||
if (kindsKnown)
|
||||
return compareValues("references", leftKinds, rightKinds, context);
|
||||
return unresolvedDimension({
|
||||
dimension: "references",
|
||||
|
||||
@@ -72,22 +72,52 @@ export const normalizeCfg = (
|
||||
return graph;
|
||||
};
|
||||
|
||||
export const referenceProjection = (snapshot: FunctionSnapshot) =>
|
||||
sorted([
|
||||
...snapshot.dossier.incoming_references.map((item) => ({
|
||||
direction: "in",
|
||||
source: item.source_procedure?.name ?? null,
|
||||
target: item.target_procedure?.name ?? null,
|
||||
type ReferenceEdge = FunctionSnapshot["dossier"]["outgoing_references"][number];
|
||||
|
||||
/**
|
||||
* A named endpoint survives relocation. An unnamed endpoint is identified
|
||||
* only by its address, which names one location only within one artifact;
|
||||
* across artifacts it stays unknown (null).
|
||||
*/
|
||||
const referenceEndpoint = (
|
||||
procedure: ReferenceEdge["source_procedure"],
|
||||
address: string,
|
||||
sameArtifact: boolean,
|
||||
): string | { readonly address: string } | null =>
|
||||
procedure !== null ? procedure.name : sameArtifact ? { address } : null;
|
||||
|
||||
export const referenceProjection = (
|
||||
snapshot: FunctionSnapshot,
|
||||
sameArtifact: boolean,
|
||||
) =>
|
||||
sorted(
|
||||
[
|
||||
...snapshot.dossier.incoming_references.map((item) => ({
|
||||
direction: "in",
|
||||
item,
|
||||
})),
|
||||
...snapshot.dossier.outgoing_references.map((item) => ({
|
||||
direction: "out",
|
||||
item,
|
||||
})),
|
||||
].map(({ direction, item }) => ({
|
||||
direction,
|
||||
source: referenceEndpoint(
|
||||
item.source_procedure,
|
||||
item.source_address,
|
||||
sameArtifact,
|
||||
),
|
||||
target: referenceEndpoint(
|
||||
item.target_procedure,
|
||||
item.target_address,
|
||||
sameArtifact,
|
||||
),
|
||||
})),
|
||||
...snapshot.dossier.outgoing_references.map((item) => ({
|
||||
direction: "out",
|
||||
source: item.source_procedure?.name ?? null,
|
||||
target: item.target_procedure?.name ?? null,
|
||||
})),
|
||||
]);
|
||||
);
|
||||
|
||||
export const referenceKindProjection = (
|
||||
snapshot: FunctionSnapshot,
|
||||
sameArtifact: boolean,
|
||||
): readonly unknown[] | null => {
|
||||
const edges = [
|
||||
...snapshot.dossier.incoming_references.map((edge) => ({
|
||||
@@ -108,8 +138,16 @@ export const referenceKindProjection = (
|
||||
);
|
||||
return {
|
||||
direction,
|
||||
source: edge.source_procedure?.name ?? null,
|
||||
target: edge.target_procedure?.name ?? null,
|
||||
source: referenceEndpoint(
|
||||
edge.source_procedure,
|
||||
edge.source_address,
|
||||
sameArtifact,
|
||||
),
|
||||
target: referenceEndpoint(
|
||||
edge.target_procedure,
|
||||
edge.target_address,
|
||||
sameArtifact,
|
||||
),
|
||||
type: edge.kind.type,
|
||||
flow: edge.kind.flow,
|
||||
call: edge.kind.call,
|
||||
|
||||
@@ -148,23 +148,34 @@ export class HopperProcessError extends HopperError {
|
||||
}
|
||||
}
|
||||
|
||||
/** Parsed facts about rejecting an application before execution. */
|
||||
export interface HopperLaunchRejection {
|
||||
/** The selected application was rejected before it was executed. */
|
||||
readonly boundary: "publisher_verification";
|
||||
/** Parsed failure reason supplied by the verification owner. */
|
||||
readonly reason: string;
|
||||
}
|
||||
|
||||
/** Hopper or the repository bridge could not be started. */
|
||||
export class HopperStartError extends HopperError {
|
||||
readonly _tag = "HopperStartError";
|
||||
override readonly userMessage: string | undefined;
|
||||
readonly ownerRunId: string | undefined;
|
||||
readonly launcherFailure: HopperLauncherOutcome | undefined;
|
||||
readonly launchRejection: HopperLaunchRejection | undefined;
|
||||
|
||||
constructor(
|
||||
options?: ErrorOptions & {
|
||||
readonly userMessage?: string;
|
||||
readonly ownerRunId?: string;
|
||||
readonly launcherFailure?: HopperLauncherOutcome;
|
||||
readonly launchRejection?: HopperLaunchRejection;
|
||||
},
|
||||
) {
|
||||
super("Hopper application bridge could not be started", options);
|
||||
this.userMessage = options?.userMessage;
|
||||
this.ownerRunId = options?.ownerRunId;
|
||||
this.launcherFailure = options?.launcherFailure;
|
||||
this.launchRejection = options?.launchRejection;
|
||||
}
|
||||
}
|
||||
@@ -353,6 +353,22 @@ it("retains distinct wrapper invocation ranges for one IPC API source", () => {
|
||||
findings[1]?.invocation_location,
|
||||
);
|
||||
});
|
||||
it("keeps one wrapper registration when one invocation reaches it twice", () => {
|
||||
const result = analyzeJavaScriptStaticSource(`
|
||||
const e=require("electron");
|
||||
const copy=event=>event.sender.copy();
|
||||
function register(channel,handler){e.ipcMain.on(channel,handler);}
|
||||
function twice(channel){register(channel,copy);register(channel,copy);}
|
||||
twice("same:invocation");
|
||||
`);
|
||||
expect(
|
||||
result.electron.ipc.filter(
|
||||
({ resolution_basis, channel }) =>
|
||||
resolution_basis === "wrapper-substitution" &&
|
||||
channel === "same:invocation",
|
||||
),
|
||||
).toHaveLength(1);
|
||||
});
|
||||
it("keeps a directly observed renderer operation singular when tracing its exposed callback", () => {
|
||||
const result = analyzeJavaScriptStaticSource(`
|
||||
const {contextBridge,ipcRenderer}=require("electron");
|
||||
|
||||
@@ -43,6 +43,35 @@ const callLabel = (node: t.Node): string =>
|
||||
? callLabel(node.expressions.at(-1) ?? node)
|
||||
: calleeName(node) || "local call";
|
||||
|
||||
const ipcEndpointKey = (value: ElectronIpcFinding): string =>
|
||||
JSON.stringify([value.side, value.operation, value.channel, value.location]);
|
||||
|
||||
/** Duplicate evidence already recorded for one endpoint and call site. */
|
||||
interface IpcEndpointFacts {
|
||||
// A directly observed transmission stays one fact however it is reached.
|
||||
observedTransmission: boolean;
|
||||
readonly invocations: Set<string>;
|
||||
}
|
||||
|
||||
const ipcInvocationKey = (location: unknown): string =>
|
||||
String(JSON.stringify(location));
|
||||
|
||||
const indexIpcFinding = (
|
||||
index: Map<string, IpcEndpointFacts>,
|
||||
value: ElectronIpcFinding,
|
||||
): void => {
|
||||
const key = ipcEndpointKey(value);
|
||||
let facts = index.get(key);
|
||||
if (facts === undefined) {
|
||||
facts = { observedTransmission: false, invocations: new Set() };
|
||||
index.set(key, facts);
|
||||
}
|
||||
facts.observedTransmission ||=
|
||||
value.resolution_basis === undefined &&
|
||||
(value.mode === "send" || value.mode === "invoke");
|
||||
facts.invocations.add(ipcInvocationKey(value.invocation_location));
|
||||
};
|
||||
|
||||
interface PathContext {
|
||||
readonly entryNode: t.Node;
|
||||
readonly entry: string;
|
||||
@@ -149,6 +178,16 @@ export const collectElectronWrappedCallPaths = (
|
||||
) ?? context
|
||||
);
|
||||
};
|
||||
// Duplicate checks consult only facts at the same endpoint and call site.
|
||||
// Scanning every prior fact made bundles with many registrations quadratic.
|
||||
let ipcByEndpoint: Map<string, IpcEndpointFacts> | undefined;
|
||||
const ipcAtEndpoint = (): Map<string, IpcEndpointFacts> => {
|
||||
if (ipcByEndpoint !== undefined) return ipcByEndpoint;
|
||||
ipcByEndpoint = new Map();
|
||||
for (const { value } of accumulator.ipc)
|
||||
indexIpcFinding(ipcByEndpoint, value);
|
||||
return ipcByEndpoint;
|
||||
};
|
||||
const hasConditions = (value: SymbolicValue): boolean =>
|
||||
symbolicVariants(value).some((variant) => variant.conditions?.length);
|
||||
function* bridgeMemberFunctions(
|
||||
@@ -351,24 +390,20 @@ export const collectElectronWrappedCallPaths = (
|
||||
};
|
||||
// A fully observed transmission stays one source fact when its callback is
|
||||
// traced. Registrations retain invocation identity because handlers compete.
|
||||
const duplicate = accumulator.ipc.some(
|
||||
({ value }) =>
|
||||
value.side === finding.side &&
|
||||
value.operation === finding.operation &&
|
||||
value.channel === channel &&
|
||||
JSON.stringify(value.location) === JSON.stringify(location) &&
|
||||
((value.resolution_basis === undefined &&
|
||||
(value.mode === "send" || value.mode === "invoke")) ||
|
||||
JSON.stringify(value.invocation_location) ===
|
||||
JSON.stringify(invocationLocation)),
|
||||
);
|
||||
const recorded = ipcAtEndpoint().get(ipcEndpointKey(finding));
|
||||
const duplicate =
|
||||
recorded !== undefined &&
|
||||
(recorded.observedTransmission ||
|
||||
recorded.invocations.has(ipcInvocationKey(invocationLocation)));
|
||||
const findingGuarded =
|
||||
context.conditions.length > 0 || hasConditions(first) || guardedDispatch;
|
||||
if (!duplicate && !findingGuarded) {
|
||||
accumulator.ipc.push({
|
||||
offset: node.start ?? -1,
|
||||
value: { ...finding, resolution_basis: "wrapper-substitution" },
|
||||
});
|
||||
const value: ElectronIpcFinding = {
|
||||
...finding,
|
||||
resolution_basis: "wrapper-substitution",
|
||||
};
|
||||
accumulator.ipc.push({ offset: node.start ?? -1, value });
|
||||
indexIpcFinding(ipcAtEndpoint(), value);
|
||||
if (descriptor.mode !== "send" && descriptor.mode !== "invoke" && !fn)
|
||||
accumulator.unknownFindings += 1;
|
||||
} else if (findingGuarded) accumulator.unknownFindings += 1;
|
||||
|
||||
@@ -14,6 +14,7 @@ import {
|
||||
resolveSemanticBindingState,
|
||||
semanticStableInitializer,
|
||||
type JavaScriptSemanticAnalysisState,
|
||||
type JavaScriptSemanticMemberEffect,
|
||||
} from "./javascriptSemanticState.js";
|
||||
import { isImportMetaUrl } from "./javascriptStaticAnalysisHelpers.js";
|
||||
import { semanticMutationInitializers } from "./javascriptSemanticMutationInitializers.js";
|
||||
@@ -289,29 +290,99 @@ export function* indexJavaScriptAmbientWritesSteps(
|
||||
}
|
||||
|
||||
/**
|
||||
* Recognize an ambient name or direct global-object property without a visible
|
||||
* source-local overwrite. This is a syntax-local identity fact, not proof that
|
||||
* the runtime's globals have never been patched by other code.
|
||||
* How a global reference relates to visible source-local writes. A write under
|
||||
* an unknown key may or may not select the name, so it leaves the built-in
|
||||
* possible rather than replaced.
|
||||
*/
|
||||
type SemanticGlobalReferenceState =
|
||||
| "not-global"
|
||||
| "unmodified"
|
||||
| "possibly-replaced"
|
||||
| "replaced";
|
||||
|
||||
/** Exact written paths, indexed once per collected ambient write list. */
|
||||
const exactAmbientWrites = new WeakMap<
|
||||
readonly JavaScriptSemanticMemberEffect[],
|
||||
{ readonly length: number; readonly paths: ReadonlySet<string> }
|
||||
>();
|
||||
|
||||
const exactAmbientWritePaths = (
|
||||
state: JavaScriptSemanticAnalysisState,
|
||||
): ReadonlySet<string> => {
|
||||
const effects = state.ambientWriteEffects;
|
||||
const cached = exactAmbientWrites.get(effects);
|
||||
if (cached?.length === effects.length) return cached.paths;
|
||||
const paths = new Set<string>();
|
||||
for (const { path } of effects)
|
||||
if (path.every((key) => typeof key === "string" || typeof key === "number"))
|
||||
paths.add(JSON.stringify(path.map(String)));
|
||||
exactAmbientWrites.set(effects, { length: effects.length, paths });
|
||||
return paths;
|
||||
};
|
||||
|
||||
/** Coverage is the prefix index; only an all-exact written prefix replaces. */
|
||||
const ambientPathWrite = (
|
||||
state: JavaScriptSemanticAnalysisState,
|
||||
path: readonly string[],
|
||||
): "none" | "possible" | "exact" => {
|
||||
if (!state.ambientWrites.covers(path)) return "none";
|
||||
const exact = exactAmbientWritePaths(state);
|
||||
for (let length = path.length; length > 0; length -= 1)
|
||||
if (exact.has(JSON.stringify(path.slice(0, length)))) return "exact";
|
||||
return "possible";
|
||||
};
|
||||
|
||||
/**
|
||||
* Classify an ambient name, direct global-object property, or member path of
|
||||
* one, such as `JSON.parse`, against visible source-local overwrites. This is a
|
||||
* syntax-local identity fact, not proof that the runtime's globals have never
|
||||
* been patched by other code.
|
||||
*/
|
||||
const semanticGlobalReferenceState = (
|
||||
callee: t.Node,
|
||||
state: JavaScriptSemanticAnalysisState,
|
||||
name: string,
|
||||
members: readonly string[] = [],
|
||||
): SemanticGlobalReferenceState => {
|
||||
const node = unwrapJavaScriptExpression(callee).node;
|
||||
const paths = [["global", name, ...members]];
|
||||
if (!isUnshadowedGlobal(node, state, name)) {
|
||||
if (
|
||||
!t.isMemberExpression(node) ||
|
||||
semanticStaticPropertyKey(node.property, node.computed) !== name
|
||||
)
|
||||
return "not-global";
|
||||
const root = unwrapJavaScriptExpression(node.object).node;
|
||||
const namespace = globalObjectName(root, state);
|
||||
if (namespace === undefined) return "not-global";
|
||||
paths.push(["global", namespace]);
|
||||
}
|
||||
const writes = paths.map((path) => ambientPathWrite(state, path));
|
||||
if (writes.includes("exact")) return "replaced";
|
||||
return writes.includes("possible") ? "possibly-replaced" : "unmodified";
|
||||
};
|
||||
|
||||
/** Recognize a global reference without any visible source-local overwrite. */
|
||||
export const isUnmodifiedGlobalReference = (
|
||||
callee: t.Node,
|
||||
state: JavaScriptSemanticAnalysisState,
|
||||
name: string,
|
||||
): boolean => {
|
||||
const node = unwrapJavaScriptExpression(callee).node;
|
||||
if (state.ambientWrites.covers(["global", name])) return false;
|
||||
if (isUnshadowedGlobal(node, state, name)) return true;
|
||||
if (
|
||||
!t.isMemberExpression(node) ||
|
||||
semanticStaticPropertyKey(node.property, node.computed) !== name
|
||||
)
|
||||
return false;
|
||||
const root = unwrapJavaScriptExpression(node.object).node;
|
||||
const namespace = globalObjectName(root, state);
|
||||
return (
|
||||
namespace !== undefined &&
|
||||
!state.ambientWrites.covers(["global", namespace])
|
||||
);
|
||||
): boolean =>
|
||||
semanticGlobalReferenceState(callee, state, name) === "unmodified";
|
||||
|
||||
/**
|
||||
* Resolve a built-in effect's identity: complete when unmodified, partial when
|
||||
* an unknown-key write may have replaced it, and null when not the built-in.
|
||||
*/
|
||||
export const semanticBuiltinGlobalResolution = (
|
||||
callee: t.Node,
|
||||
state: JavaScriptSemanticAnalysisState,
|
||||
name: string,
|
||||
members: readonly string[] = [],
|
||||
): "complete" | "partial" | null => {
|
||||
const reference = semanticGlobalReferenceState(callee, state, name, members);
|
||||
if (reference === "unmodified") return "complete";
|
||||
return reference === "possibly-replaced" ? "partial" : null;
|
||||
};
|
||||
|
||||
/** A module URL base is known only while its property has no visible write. */
|
||||
|
||||
@@ -0,0 +1,108 @@
|
||||
import { expect, it } from "vitest";
|
||||
|
||||
import { analyzeJavaScriptSemantics } from "./javascriptSemanticAnalysis.js";
|
||||
|
||||
const returned = (source: string) => {
|
||||
const ir = analyzeJavaScriptSemantics(source);
|
||||
const callable = ir.callables.find(({ name }) => name === "observe");
|
||||
return callable?.returnSites[0]?.value;
|
||||
};
|
||||
|
||||
it("keeps members unknown after a write through a local setter", () => {
|
||||
// The setter runs on assignment and may replace any member of `this`.
|
||||
expect(
|
||||
returned(`
|
||||
export function observe() {
|
||||
const state = { value: 1, set update(next) { this.value = next; } };
|
||||
state.update = 2;
|
||||
return state;
|
||||
}
|
||||
`),
|
||||
).toMatchObject({ status: "unknown" });
|
||||
});
|
||||
|
||||
it("keeps sibling literals when the written key is an ordinary property", () => {
|
||||
expect(
|
||||
returned(`
|
||||
export function observe() {
|
||||
const state = { value: 1, update: 0 };
|
||||
state.update = 2;
|
||||
return state;
|
||||
}
|
||||
`),
|
||||
).toMatchObject({
|
||||
status: "object",
|
||||
properties: expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
name: "value",
|
||||
value: { status: "literal", value: 1 },
|
||||
}),
|
||||
]),
|
||||
});
|
||||
});
|
||||
|
||||
it("keeps nested members unknown after a write through a nested setter", () => {
|
||||
const value = returned(`
|
||||
export function observe() {
|
||||
const state = {
|
||||
label: "kept",
|
||||
nested: { value: 1, set update(next) { this.value = next; } },
|
||||
};
|
||||
state.nested.update = 2;
|
||||
return state;
|
||||
}
|
||||
`);
|
||||
expect(value).toMatchObject({
|
||||
status: "object",
|
||||
properties: expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
name: "label",
|
||||
value: { status: "literal", value: "kept" },
|
||||
}),
|
||||
expect.objectContaining({
|
||||
name: "nested",
|
||||
value: expect.objectContaining({ status: "unknown" }),
|
||||
}),
|
||||
]),
|
||||
});
|
||||
});
|
||||
|
||||
it("keeps sibling literals when an accessor is deleted rather than assigned", () => {
|
||||
expect(
|
||||
returned(`
|
||||
export function observe() {
|
||||
const state = { value: 1, set update(next) {} };
|
||||
delete state.update;
|
||||
return state;
|
||||
}
|
||||
`),
|
||||
).toMatchObject({
|
||||
status: "object",
|
||||
properties: expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
name: "value",
|
||||
value: { status: "literal", value: 1 },
|
||||
}),
|
||||
]),
|
||||
});
|
||||
});
|
||||
|
||||
it("keeps sibling literals when a later data property replaces the setter", () => {
|
||||
expect(
|
||||
returned(`
|
||||
export function observe() {
|
||||
const state = { value: 1, set update(next) {}, update: 0 };
|
||||
state.update = 2;
|
||||
return state;
|
||||
}
|
||||
`),
|
||||
).toMatchObject({
|
||||
status: "object",
|
||||
properties: expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
name: "value",
|
||||
value: { status: "literal", value: 1 },
|
||||
}),
|
||||
]),
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,38 @@
|
||||
import { expect, it } from "vitest";
|
||||
|
||||
import { analyzeJavaScriptSemantics } from "./javascriptSemanticAnalysis.js";
|
||||
|
||||
const effects = (call: (value: string) => string, value: string) => {
|
||||
const ir = analyzeJavaScriptSemantics(`
|
||||
const { spawn } = require("child_process");
|
||||
const { EventEmitter } = require("events");
|
||||
${call(value)}
|
||||
`);
|
||||
return {
|
||||
commands: ir.childProcessSpawns.map(({ command }) => command),
|
||||
endpoints: ir.requestOperations.map(({ endpoint }) => endpoint),
|
||||
events: ir.eventOperations.map(({ eventName }) => eventName),
|
||||
};
|
||||
};
|
||||
|
||||
const calls = (value: string) =>
|
||||
[
|
||||
`spawn(${value});`,
|
||||
`fetch(${value});`,
|
||||
`new EventEmitter().on(${value}, () => 1);`,
|
||||
].join("\n");
|
||||
|
||||
it("keeps recovered templates without a cooked value dynamic", () => {
|
||||
// `\\unicode` is an invalid escape: Babel recovers it with `cooked: null`.
|
||||
expect(effects(calls, "`\\unicode`")).toEqual({
|
||||
commands: [null],
|
||||
endpoints: [null],
|
||||
events: [null],
|
||||
});
|
||||
// Valid templates keep their cooked value, including escapes.
|
||||
expect(effects(calls, "`ready\\n`")).toEqual({
|
||||
commands: ["ready\n"],
|
||||
endpoints: ["ready\n"],
|
||||
events: ["ready\n"],
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,57 @@
|
||||
import { expect, it } from "vitest";
|
||||
|
||||
import { analyzeJavaScriptSemantics } from "./javascriptSemanticAnalysis.js";
|
||||
|
||||
const effects = (source: string) => {
|
||||
const ir = analyzeJavaScriptSemantics(source);
|
||||
return {
|
||||
timers: ir.timerOperations.map(
|
||||
({ method, resolution }) => `${method}:${resolution}`,
|
||||
),
|
||||
requests: ir.requestOperations
|
||||
.filter(({ kind }) => kind === "request")
|
||||
.map(({ method, resolution }) => `${method}:${resolution}`),
|
||||
parses: ir.boundaryOperations
|
||||
.filter(({ kind }) => kind === "parse")
|
||||
.map(({ method, resolution }) => `${method}:${resolution}`),
|
||||
};
|
||||
};
|
||||
|
||||
const calls = `
|
||||
function work() {}
|
||||
setTimeout(work, 100);
|
||||
fetch("https://example.invalid");
|
||||
JSON.parse("null");
|
||||
`;
|
||||
|
||||
it("does not classify calls to replaced globals as built-in effects", () => {
|
||||
expect(
|
||||
effects(`
|
||||
setTimeout = () => null;
|
||||
fetch = () => null;
|
||||
JSON.parse = () => null;
|
||||
${calls}
|
||||
`),
|
||||
).toEqual({ timers: [], requests: [], parses: ["parse:partial"] });
|
||||
});
|
||||
|
||||
it("keeps built-in effects partial after a write under an unknown key", () => {
|
||||
expect(
|
||||
effects(`
|
||||
globalThis[Symbol.for("registry")] = new Map();
|
||||
${calls}
|
||||
`),
|
||||
).toEqual({
|
||||
timers: ["setTimeout:partial"],
|
||||
requests: ["fetch:partial"],
|
||||
parses: ["JSON.parse:partial"],
|
||||
});
|
||||
});
|
||||
|
||||
it("keeps built-in effects for unmodified globals", () => {
|
||||
expect(effects(calls)).toEqual({
|
||||
timers: ["setTimeout:complete"],
|
||||
requests: ["fetch:complete"],
|
||||
parses: ["JSON.parse:complete"],
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,59 @@
|
||||
import { expect, it } from "vitest";
|
||||
|
||||
import { analyzeJavaScriptSemantics } from "./javascriptSemanticAnalysis.js";
|
||||
|
||||
const responses = (source: string) =>
|
||||
analyzeJavaScriptSemantics(source)
|
||||
.requestOperations.filter(({ kind }) => kind === "response-consumer")
|
||||
.map(({ method, endpoint, resolution }) => ({
|
||||
method,
|
||||
endpoint,
|
||||
resolution,
|
||||
}));
|
||||
|
||||
it("keeps the endpoint of a uniquely consumed request response", () => {
|
||||
expect(
|
||||
responses(`
|
||||
async function load() {
|
||||
const a = await fetch("https://example.test/a");
|
||||
const b = await fetch("https://example.test/b");
|
||||
return [await a.json(), await b.text()];
|
||||
}
|
||||
`),
|
||||
).toEqual([
|
||||
{
|
||||
method: "json",
|
||||
endpoint: "https://example.test/a",
|
||||
resolution: "complete",
|
||||
},
|
||||
{
|
||||
method: "text",
|
||||
endpoint: "https://example.test/b",
|
||||
resolution: "complete",
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it("leaves an ambiguous response consumer without an endpoint", () => {
|
||||
expect(
|
||||
responses(`
|
||||
async function load(flag) {
|
||||
let response = await fetch("https://example.test/a");
|
||||
if (flag) response = await fetch("https://example.test/b");
|
||||
return response.json();
|
||||
}
|
||||
`),
|
||||
).toEqual([{ method: "json", endpoint: null, resolution: "partial" }]);
|
||||
});
|
||||
|
||||
it("leaves a response replaced by a non-request value without an endpoint", () => {
|
||||
expect(
|
||||
responses(`
|
||||
async function load(cachedResponse) {
|
||||
let response = await fetch("https://example.test/a");
|
||||
response = cachedResponse;
|
||||
return response.json();
|
||||
}
|
||||
`),
|
||||
).toEqual([expect.objectContaining({ method: "json", endpoint: null })]);
|
||||
});
|
||||
@@ -223,37 +223,6 @@ export function* analyzeParsedJavaScriptSemanticsSteps(
|
||||
};
|
||||
}
|
||||
|
||||
/** Resolve tRPC client roots by lexical identity, excluding overwritten bindings. */
|
||||
export function* classifyParsedJavaScriptRpcClientRootsSteps(
|
||||
file: ParsedJavaScriptSource,
|
||||
): Generator<void, ReadonlySet<t.Node>> {
|
||||
const state = createState(file.program);
|
||||
yield* collectDefinitionsSteps(file.program, state);
|
||||
const roots = new Set<t.Node>();
|
||||
const clients = new Map<string, boolean>();
|
||||
yield* traverseJavaScriptAstSteps(file.program, {
|
||||
enter: (node) => {
|
||||
if (!t.isMemberExpression(node) && !t.isOptionalMemberExpression(node))
|
||||
return;
|
||||
const root = node.object;
|
||||
if (!t.isIdentifier(root)) return;
|
||||
const binding = resolveSemanticBindingState(state, root, root.name);
|
||||
if (binding === undefined) return;
|
||||
let client = clients.get(binding.bindingId);
|
||||
if (client === undefined) {
|
||||
const initializer = semanticStableInitializer(binding, state);
|
||||
client =
|
||||
initializer !== undefined &&
|
||||
initializer.projection.length === 0 &&
|
||||
isJavaScriptRpcClientFactory(initializer.node);
|
||||
clients.set(binding.bindingId, client);
|
||||
}
|
||||
if (client) roots.add(root);
|
||||
},
|
||||
});
|
||||
return roots;
|
||||
}
|
||||
|
||||
/**
|
||||
* Receiver identity facts for the overloaded `.open` syntax, plus
|
||||
* `configuration` for `.get` reads of a `getConfiguration(...)` result.
|
||||
@@ -268,111 +237,169 @@ export type JavaScriptOpenReceiverFact =
|
||||
| "configuration"
|
||||
| "extension-state";
|
||||
|
||||
/**
|
||||
* Resolve only the lexical receiver identity needed to distinguish browser
|
||||
* `window.open` from XHR and global storage `.open` calls, and configuration
|
||||
* `.get` reads from HTTP client calls. This deliberately builds definitions
|
||||
* without running semantic value or provenance analysis.
|
||||
*/
|
||||
export const classifyParsedJavaScriptOpenReceivers = (
|
||||
file: ParsedJavaScriptSource,
|
||||
): ReadonlyMap<number, JavaScriptOpenReceiverFact> =>
|
||||
completeSemanticSteps(classifyParsedJavaScriptOpenReceiversSteps(file));
|
||||
/** Lexical receiver identities that select static endpoint interpretations. */
|
||||
export interface JavaScriptReceiverFacts {
|
||||
readonly openReceivers: ReadonlyMap<number, JavaScriptOpenReceiverFact>;
|
||||
readonly rpcClientRoots: ReadonlySet<t.Node>;
|
||||
}
|
||||
|
||||
/** Classify `.open` receivers while yielding during lexical and AST scans. */
|
||||
export function* classifyParsedJavaScriptOpenReceiversSteps(
|
||||
/**
|
||||
* Resolve only the lexical receiver identities needed to distinguish browser
|
||||
* `window.open` from XHR and global storage `.open` calls, configuration
|
||||
* `.get` reads from HTTP client calls, and tRPC client roots (excluding
|
||||
* overwritten bindings). This deliberately builds definitions without running
|
||||
* semantic value or provenance analysis.
|
||||
*/
|
||||
export const classifyParsedJavaScriptReceivers = (
|
||||
file: ParsedJavaScriptSource,
|
||||
): Generator<void, ReadonlyMap<number, JavaScriptOpenReceiverFact>> {
|
||||
): JavaScriptReceiverFacts =>
|
||||
completeSemanticSteps(classifyParsedJavaScriptReceiversSteps(file));
|
||||
|
||||
/** Classify receivers while yielding during lexical and AST scans. */
|
||||
export function* classifyParsedJavaScriptReceiversSteps(
|
||||
file: ParsedJavaScriptSource,
|
||||
): Generator<void, JavaScriptReceiverFacts> {
|
||||
const state = createState(file.program);
|
||||
yield* collectDefinitionsSteps(file.program, state);
|
||||
return yield* classifyDefinitionReceiversSteps(file.program, state);
|
||||
}
|
||||
|
||||
/**
|
||||
* Index bindings and classify receivers from one definitions pass. Receiver
|
||||
* facts read the definitions-only phase, so they are classified before the
|
||||
* same state is extended with ambient writes.
|
||||
*/
|
||||
export function* indexJavaScriptBindingsWithReceiversSteps(
|
||||
program: t.Program,
|
||||
): Generator<
|
||||
void,
|
||||
{
|
||||
readonly bindings: JavaScriptSemanticAnalysisState;
|
||||
readonly receivers: JavaScriptReceiverFacts;
|
||||
}
|
||||
> {
|
||||
const state = createState(program);
|
||||
yield* collectDefinitionsSteps(program, state);
|
||||
const receivers = yield* classifyDefinitionReceiversSteps(program, state);
|
||||
yield* indexJavaScriptAmbientWritesSteps(program, state);
|
||||
return { bindings: state, receivers };
|
||||
}
|
||||
|
||||
function* classifyDefinitionReceiversSteps(
|
||||
program: t.Program,
|
||||
state: JavaScriptSemanticAnalysisState,
|
||||
): Generator<void, JavaScriptReceiverFacts> {
|
||||
const facts = new Map<number, JavaScriptOpenReceiverFact>();
|
||||
const extensionStateReads: number[] = [];
|
||||
yield* traverseJavaScriptAstSteps(file.program, {
|
||||
enter: (node) => {
|
||||
if (
|
||||
(!t.isCallExpression(node) && !t.isOptionalCallExpression(node)) ||
|
||||
(!t.isMemberExpression(node.callee) &&
|
||||
!t.isOptionalMemberExpression(node.callee))
|
||||
)
|
||||
return;
|
||||
const method = semanticStaticPropertyKey(
|
||||
node.callee.property,
|
||||
node.callee.computed,
|
||||
const roots = new Set<t.Node>();
|
||||
const clients = new Map<string, boolean>();
|
||||
const classifyRpcRoot = (node: t.Node): void => {
|
||||
if (!t.isMemberExpression(node) && !t.isOptionalMemberExpression(node))
|
||||
return;
|
||||
const root = node.object;
|
||||
if (!t.isIdentifier(root)) return;
|
||||
const binding = resolveSemanticBindingState(state, root, root.name);
|
||||
if (binding === undefined) return;
|
||||
let client = clients.get(binding.bindingId);
|
||||
if (client === undefined) {
|
||||
const initializer = semanticStableInitializer(binding, state);
|
||||
client =
|
||||
initializer !== undefined &&
|
||||
initializer.projection.length === 0 &&
|
||||
isJavaScriptRpcClientFactory(initializer.node);
|
||||
clients.set(binding.bindingId, client);
|
||||
}
|
||||
if (client) roots.add(root);
|
||||
};
|
||||
const classifyOpenReceiver = (node: t.Node): void => {
|
||||
if (
|
||||
(!t.isCallExpression(node) && !t.isOptionalCallExpression(node)) ||
|
||||
(!t.isMemberExpression(node.callee) &&
|
||||
!t.isOptionalMemberExpression(node.callee))
|
||||
)
|
||||
return;
|
||||
const method = semanticStaticPropertyKey(
|
||||
node.callee.property,
|
||||
node.callee.computed,
|
||||
);
|
||||
if (method === "get") {
|
||||
if (isConfigurationReceiver(state, node.callee.object))
|
||||
facts.set(node.start ?? -1, "configuration");
|
||||
else if (isExtensionStateMember(node.callee.object))
|
||||
extensionStateReads.push(node.start ?? -1);
|
||||
return;
|
||||
}
|
||||
if (method !== "open" || !t.isIdentifier(node.callee.object)) return;
|
||||
const receiver = node.callee.object.name;
|
||||
const binding = resolveSemanticBindingState(state, node, receiver);
|
||||
if (binding === undefined) {
|
||||
if (semanticResolutionBlocked(state, node, receiver)) return;
|
||||
const fact = openGlobalFact(receiver);
|
||||
if (fact !== undefined) facts.set(node.start ?? -1, fact);
|
||||
return;
|
||||
}
|
||||
// A lexically bound receiver overrides ambient browser globals. Mark an
|
||||
// otherwise unresolved local as local so name-based window.open
|
||||
// heuristics do not hide its XHR-like overloads.
|
||||
facts.set(node.start ?? -1, "local");
|
||||
if (receiver === "indexedDB" || receiver === "caches") {
|
||||
facts.set(
|
||||
node.start ?? -1,
|
||||
receiver === "indexedDB" ? "local-indexed-db" : "local-cache-storage",
|
||||
);
|
||||
if (method === "get") {
|
||||
if (isConfigurationReceiver(state, node.callee.object))
|
||||
facts.set(node.start ?? -1, "configuration");
|
||||
else if (isExtensionStateMember(node.callee.object))
|
||||
extensionStateReads.push(node.start ?? -1);
|
||||
return;
|
||||
}
|
||||
if (method !== "open" || !t.isIdentifier(node.callee.object)) return;
|
||||
const receiver = node.callee.object.name;
|
||||
const binding = resolveSemanticBindingState(state, node, receiver);
|
||||
if (binding === undefined) {
|
||||
if (semanticResolutionBlocked(state, node, receiver)) return;
|
||||
const fact = openGlobalFact(receiver);
|
||||
if (fact !== undefined) facts.set(node.start ?? -1, fact);
|
||||
return;
|
||||
}
|
||||
// A lexically bound receiver overrides ambient browser globals. Mark an
|
||||
// otherwise unresolved local as local so name-based window.open
|
||||
// heuristics do not hide its XHR-like overloads.
|
||||
facts.set(node.start ?? -1, "local");
|
||||
if (receiver === "indexedDB" || receiver === "caches") {
|
||||
facts.set(
|
||||
node.start ?? -1,
|
||||
receiver === "indexedDB" ? "local-indexed-db" : "local-cache-storage",
|
||||
);
|
||||
return;
|
||||
}
|
||||
if (binding.mutable || binding.initializers.length !== 1) return;
|
||||
const initializer = binding.initializers[0]?.node;
|
||||
if (!t.isIdentifier(initializer)) return;
|
||||
const initializerBinding = resolveSemanticBindingState(
|
||||
state,
|
||||
initializer,
|
||||
initializer.name,
|
||||
);
|
||||
if (initializerBinding !== undefined) {
|
||||
if (initializer.name === "indexedDB" || initializer.name === "caches") {
|
||||
const localFact =
|
||||
initializer.name === "indexedDB"
|
||||
? "local-indexed-db"
|
||||
: "local-cache-storage";
|
||||
facts.set(node.start ?? -1, localFact);
|
||||
if (
|
||||
initializerBinding.mutable ||
|
||||
initializerBinding.initializers.length !== 1
|
||||
)
|
||||
return;
|
||||
const nested = initializerBinding.initializers[0]?.node;
|
||||
if (t.isIdentifier(nested)) {
|
||||
const nestedFact = resolveGlobalAliasFact(
|
||||
state,
|
||||
initializer,
|
||||
nested.name,
|
||||
);
|
||||
if (nestedFact !== undefined)
|
||||
facts.set(node.start ?? -1, nestedFact);
|
||||
}
|
||||
} else if (initializer.name === "window") {
|
||||
facts.set(node.start ?? -1, "local");
|
||||
return;
|
||||
}
|
||||
if (binding.mutable || binding.initializers.length !== 1) return;
|
||||
const initializer = binding.initializers[0]?.node;
|
||||
if (!t.isIdentifier(initializer)) return;
|
||||
const initializerBinding = resolveSemanticBindingState(
|
||||
state,
|
||||
initializer,
|
||||
initializer.name,
|
||||
);
|
||||
if (initializerBinding !== undefined) {
|
||||
if (initializer.name === "indexedDB" || initializer.name === "caches") {
|
||||
const localFact =
|
||||
initializer.name === "indexedDB"
|
||||
? "local-indexed-db"
|
||||
: "local-cache-storage";
|
||||
facts.set(node.start ?? -1, localFact);
|
||||
if (
|
||||
initializerBinding.mutable ||
|
||||
initializerBinding.initializers.length !== 1
|
||||
)
|
||||
return;
|
||||
const nested = initializerBinding.initializers[0]?.node;
|
||||
if (t.isIdentifier(nested)) {
|
||||
const nestedFact = resolveGlobalAliasFact(
|
||||
state,
|
||||
initializer,
|
||||
nested.name,
|
||||
);
|
||||
if (nestedFact !== undefined) facts.set(node.start ?? -1, nestedFact);
|
||||
}
|
||||
return;
|
||||
} else if (initializer.name === "window") {
|
||||
facts.set(node.start ?? -1, "local");
|
||||
}
|
||||
const aliasFact = resolveGlobalAliasFact(
|
||||
state,
|
||||
initializer,
|
||||
initializer.name,
|
||||
);
|
||||
if (aliasFact !== undefined) facts.set(node.start ?? -1, aliasFact);
|
||||
return;
|
||||
}
|
||||
const aliasFact = resolveGlobalAliasFact(
|
||||
state,
|
||||
initializer,
|
||||
initializer.name,
|
||||
);
|
||||
if (aliasFact !== undefined) facts.set(node.start ?? -1, aliasFact);
|
||||
};
|
||||
yield* traverseJavaScriptAstSteps(program, {
|
||||
enter: (node) => {
|
||||
classifyRpcRoot(node);
|
||||
classifyOpenReceiver(node);
|
||||
},
|
||||
});
|
||||
if (extensionStateReads.length > 0 && importsVsCodeModule(state))
|
||||
for (const start of extensionStateReads)
|
||||
facts.set(start, "extension-state");
|
||||
return facts;
|
||||
return { openReceivers: facts, rpcClientRoots: roots };
|
||||
}
|
||||
|
||||
/** VS Code passes extension state through ExtensionContext, not an import. */
|
||||
|
||||
@@ -5,8 +5,8 @@ import {
|
||||
analyzeParsedJavaScriptSemanticsSteps,
|
||||
classifyParsedJavaScriptElectronBindings,
|
||||
classifyParsedJavaScriptElectronBindingsSteps,
|
||||
classifyParsedJavaScriptOpenReceivers,
|
||||
classifyParsedJavaScriptOpenReceiversSteps,
|
||||
classifyParsedJavaScriptReceivers,
|
||||
classifyParsedJavaScriptReceiversSteps,
|
||||
} from "./javascriptSemanticAnalysis.js";
|
||||
import { parseJavaScriptSource } from "./javascriptSourceParser.js";
|
||||
import {
|
||||
@@ -110,15 +110,15 @@ new NativeWindow();
|
||||
`;
|
||||
const parsed = parseJavaScriptSource(text, "receivers.js");
|
||||
if (parsed === null) throw new Error("Expected parsed source");
|
||||
const openSteps = classifyParsedJavaScriptOpenReceiversSteps(parsed);
|
||||
const openSteps = classifyParsedJavaScriptReceiversSteps(parsed);
|
||||
let openStep = openSteps.next();
|
||||
expect(openStep.done).toBe(false);
|
||||
while (openStep.done !== true) openStep = openSteps.next();
|
||||
expect([...openStep.value]).toEqual([
|
||||
expect([...openStep.value.openReceivers]).toEqual([
|
||||
[text.indexOf('ambient.open("/local")'), "local"],
|
||||
[text.indexOf('ambient.open("/global")'), "window"],
|
||||
]);
|
||||
expect(openStep.value).toEqual(classifyParsedJavaScriptOpenReceivers(parsed));
|
||||
expect(openStep.value).toEqual(classifyParsedJavaScriptReceivers(parsed));
|
||||
|
||||
const electronSteps = classifyParsedJavaScriptElectronBindingsSteps(parsed);
|
||||
let electronStep = electronSteps.next();
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import * as t from "@babel/types";
|
||||
|
||||
import { semanticBuiltinGlobalResolution } from "./javascriptSemanticAmbientWrites.js";
|
||||
import type {
|
||||
JavaScriptSemanticCallable,
|
||||
JavaScriptSemanticEventOperation,
|
||||
@@ -68,6 +69,8 @@ interface TimerCandidate {
|
||||
readonly node: t.CallExpression | t.OptionalCallExpression;
|
||||
readonly method: JavaScriptSemanticTimerOperation["method"];
|
||||
readonly kind: JavaScriptSemanticTimerOperation["kind"];
|
||||
/** Partial when an unknown-key global write may have replaced the timer. */
|
||||
readonly identity: "complete" | "partial";
|
||||
readonly ownerCallableId: string | null;
|
||||
readonly handleBindingId: string | null;
|
||||
readonly projectedResultBindings: readonly {
|
||||
@@ -209,8 +212,9 @@ const emitterIdentity = (
|
||||
|
||||
const collectTimer = (input: TimerCollectionInput): void => {
|
||||
const { node, ownerCallableId, state, output, ancestors } = input;
|
||||
const method = timerMethod(node.callee, state);
|
||||
if (method === null) return;
|
||||
const timer = timerMethod(node.callee, state);
|
||||
if (timer === null) return;
|
||||
const { method, identity } = timer;
|
||||
const kind = TIMER_SCHEDULE_METHODS.some((candidate) => candidate === method)
|
||||
? "schedule"
|
||||
: "cancel";
|
||||
@@ -223,6 +227,7 @@ const collectTimer = (input: TimerCollectionInput): void => {
|
||||
node,
|
||||
method,
|
||||
kind,
|
||||
identity,
|
||||
ownerCallableId,
|
||||
handleBindingId:
|
||||
kind === "schedule"
|
||||
@@ -275,38 +280,53 @@ const resolveTimers = (
|
||||
? timerId(linked[0])
|
||||
: null,
|
||||
delayMilliseconds: timerDelay(candidate),
|
||||
resolution:
|
||||
candidate.kind === "schedule"
|
||||
? "complete"
|
||||
: linked.length === 1 &&
|
||||
!linked[0]?.projectedResultBindings.some(
|
||||
({ bindingId }) => bindingId === candidate.handleBindingId,
|
||||
)
|
||||
? "complete"
|
||||
: linked.length > 1
|
||||
? "partial"
|
||||
: "unresolved",
|
||||
resolution: timerResolution(candidate, linked),
|
||||
};
|
||||
});
|
||||
};
|
||||
|
||||
const timerResolution = (
|
||||
candidate: TimerCandidate,
|
||||
linked: readonly TimerCandidate[],
|
||||
): JavaScriptSemanticTimerOperation["resolution"] => {
|
||||
const resolution =
|
||||
candidate.kind === "schedule"
|
||||
? "complete"
|
||||
: linked.length === 1 &&
|
||||
!linked[0]?.projectedResultBindings.some(
|
||||
({ bindingId }) => bindingId === candidate.handleBindingId,
|
||||
)
|
||||
? "complete"
|
||||
: linked.length > 1
|
||||
? "partial"
|
||||
: "unresolved";
|
||||
return resolution === "complete" && candidate.identity === "partial"
|
||||
? "partial"
|
||||
: resolution;
|
||||
};
|
||||
|
||||
const timerMethod = (
|
||||
callee: t.Node,
|
||||
state: JavaScriptSemanticAnalysisState,
|
||||
): JavaScriptSemanticTimerOperation["method"] | null => {
|
||||
): Pick<TimerCandidate, "method" | "identity"> | null => {
|
||||
if (t.isIdentifier(callee)) {
|
||||
const binding = resolveSemanticBindingState(state, callee, callee.name);
|
||||
if (binding === undefined) {
|
||||
const method = admittedTimerMethod(callee.name);
|
||||
if (method === null) return null;
|
||||
return semanticResolutionBlocked(state, callee, callee.name)
|
||||
? null
|
||||
: method;
|
||||
// A visibly replaced global no longer schedules a Node timer.
|
||||
const identity = semanticBuiltinGlobalResolution(
|
||||
callee,
|
||||
state,
|
||||
callee.name,
|
||||
);
|
||||
return identity === null ? null : { method, identity };
|
||||
}
|
||||
}
|
||||
return admittedTimerMethod(
|
||||
const method = admittedTimerMethod(
|
||||
builtinDataEffectMethod(callee, state, ["timers", "node:timers"]) ?? "",
|
||||
);
|
||||
return method === null ? null : { method, identity: "complete" };
|
||||
};
|
||||
const admittedTimerMethod = (
|
||||
value: string,
|
||||
|
||||
@@ -924,7 +924,11 @@ export const dataEffectPrimitive = (
|
||||
return null;
|
||||
};
|
||||
|
||||
/** Read one static string or expression-free template. */
|
||||
/**
|
||||
* Read one static string or expression-free template. A template recovered
|
||||
* with an invalid escape has no cooked value; its raw spelling is not a
|
||||
* JavaScript string, so it stays dynamic.
|
||||
*/
|
||||
export const dataEffectLiteralString = (
|
||||
node:
|
||||
| t.Expression
|
||||
@@ -935,9 +939,8 @@ export const dataEffectLiteralString = (
|
||||
): string | null => {
|
||||
if (node === undefined) return null;
|
||||
const literal = readExactJavaScriptLiteral(node);
|
||||
if (literal.found && typeof literal.value === "string") return literal.value;
|
||||
return t.isTemplateLiteral(node) && node.expressions.length === 0
|
||||
? (node.quasis[0]?.value.raw ?? null)
|
||||
return literal.found && typeof literal.value === "string"
|
||||
? literal.value
|
||||
: null;
|
||||
};
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import * as t from "@babel/types";
|
||||
|
||||
import { semanticBuiltinGlobalResolution } from "./javascriptSemanticAmbientWrites.js";
|
||||
import type {
|
||||
JavaScriptSemanticBoundaryOperation,
|
||||
JavaScriptSemanticCallable,
|
||||
@@ -14,7 +15,6 @@ import {
|
||||
} from "./javascriptAstValues.js";
|
||||
import {
|
||||
resolveSemanticBindingState,
|
||||
semanticResolutionBlocked,
|
||||
type JavaScriptSemanticAnalysisState,
|
||||
} from "./javascriptSemanticState.js";
|
||||
import {
|
||||
@@ -202,8 +202,9 @@ const collectRequests = (
|
||||
!t.isNewExpression(node)
|
||||
)
|
||||
return;
|
||||
const method = requestMethod(node, context.state);
|
||||
if (method === null) return;
|
||||
const request = requestMethod(node, context.state);
|
||||
if (request === null) return;
|
||||
const { method, identity } = request;
|
||||
const endpoint = requestEndpoint(node);
|
||||
output.push({
|
||||
node,
|
||||
@@ -224,7 +225,8 @@ const collectRequests = (
|
||||
linkedRequestIds: [],
|
||||
endpoint,
|
||||
fields: requestFields(node, method, context.state),
|
||||
resolution: endpoint === null ? "partial" : "complete",
|
||||
resolution:
|
||||
endpoint === null || identity === "partial" ? "partial" : "complete",
|
||||
},
|
||||
});
|
||||
});
|
||||
@@ -270,6 +272,18 @@ const collectResponseConsumers = (
|
||||
? []
|
||||
: (requestsByBinding.get(binding.bindingId) ?? []);
|
||||
if (linked.length === 0) return;
|
||||
// A consumer of exactly one request through its own result binding
|
||||
// reads that request's response, so it keeps the request's endpoint.
|
||||
const uniqueRequest =
|
||||
linked.length === 1 &&
|
||||
!linked[0]?.operation.projectedResultBindings.some(
|
||||
({ bindingId }) => bindingId === binding?.bindingId,
|
||||
)
|
||||
? linked[0]
|
||||
: undefined;
|
||||
// Any other write (assignment, update, loop or destructuring target) can
|
||||
// replace the response before this read, so its endpoint stays unknown.
|
||||
const soleWrite = binding?.initializers.length === 1;
|
||||
output.push({
|
||||
requestId: `response:${String(node.start ?? -1)}:${String(node.end ?? -1)}`,
|
||||
kind: "response-consumer",
|
||||
@@ -287,15 +301,9 @@ const collectResponseConsumers = (
|
||||
linkedRequestIds: linked
|
||||
.map(({ operation }) => operation.requestId)
|
||||
.sort(compareUnicodeCodePoints),
|
||||
endpoint: null,
|
||||
endpoint: soleWrite ? (uniqueRequest?.operation.endpoint ?? null) : null,
|
||||
fields: [],
|
||||
resolution:
|
||||
linked.length === 1 &&
|
||||
!linked[0]?.operation.projectedResultBindings.some(
|
||||
({ bindingId }) => bindingId === binding?.bindingId,
|
||||
)
|
||||
? "complete"
|
||||
: "partial",
|
||||
resolution: uniqueRequest === undefined ? "partial" : "complete",
|
||||
});
|
||||
});
|
||||
return output;
|
||||
@@ -334,25 +342,27 @@ const boundaryDetails = (
|
||||
} | null => {
|
||||
if (t.isIdentifier(node.callee)) {
|
||||
const name = node.callee.name;
|
||||
const unbound =
|
||||
resolveSemanticBindingState(state, node.callee, name) === undefined &&
|
||||
!semanticResolutionBlocked(state, node.callee, name);
|
||||
if (
|
||||
unbound &&
|
||||
["String", "Number", "Boolean", "parseInt", "parseFloat"].includes(name)
|
||||
)
|
||||
return { kind: "coerce", method: name, resolution: "complete" };
|
||||
const resolution = [
|
||||
"String",
|
||||
"Number",
|
||||
"Boolean",
|
||||
"parseInt",
|
||||
"parseFloat",
|
||||
].includes(name)
|
||||
? semanticBuiltinGlobalResolution(node.callee, state, name)
|
||||
: null;
|
||||
if (resolution !== null)
|
||||
return { kind: "coerce", method: name, resolution };
|
||||
}
|
||||
const member = memberCallee(node);
|
||||
if (member === null) return null;
|
||||
const method = semanticStaticPropertyName(member.property, member.computed);
|
||||
if (
|
||||
method === "parse" &&
|
||||
t.isIdentifier(member.object, { name: "JSON" }) &&
|
||||
resolveSemanticBindingState(state, member.object, "JSON") === undefined &&
|
||||
!semanticResolutionBlocked(state, member.object, "JSON")
|
||||
)
|
||||
return { kind: "parse", method: "JSON.parse", resolution: "complete" };
|
||||
const jsonParse =
|
||||
method === "parse" && t.isIdentifier(member.object, { name: "JSON" })
|
||||
? semanticBuiltinGlobalResolution(member.object, state, "JSON", ["parse"])
|
||||
: null;
|
||||
if (jsonParse !== null)
|
||||
return { kind: "parse", method: "JSON.parse", resolution: jsonParse };
|
||||
if (method === "parse" || method === "safeParse")
|
||||
return { kind: "parse", method, resolution: "partial" };
|
||||
if (method === "validate" || method === "assert" || method === "is")
|
||||
@@ -363,15 +373,19 @@ const boundaryDetails = (
|
||||
const requestMethod = (
|
||||
node: t.CallExpression | t.OptionalCallExpression | t.NewExpression,
|
||||
state: JavaScriptSemanticAnalysisState,
|
||||
): JavaScriptSemanticRequestOperation["method"] | null => {
|
||||
): {
|
||||
readonly method: JavaScriptSemanticRequestOperation["method"];
|
||||
readonly identity: "complete" | "partial";
|
||||
} | null => {
|
||||
if (t.isIdentifier(node.callee)) {
|
||||
const name = node.callee.name;
|
||||
const unbound =
|
||||
resolveSemanticBindingState(state, node.callee, name) === undefined &&
|
||||
!semanticResolutionBlocked(state, node.callee, name);
|
||||
if (!t.isNewExpression(node) && unbound && name === "fetch") return "fetch";
|
||||
if (t.isNewExpression(node) && unbound && name === "WebSocket")
|
||||
return "WebSocket";
|
||||
const global = t.isNewExpression(node) ? "WebSocket" : "fetch";
|
||||
// A visibly replaced global is no longer the built-in request API.
|
||||
const identity =
|
||||
name === global
|
||||
? semanticBuiltinGlobalResolution(node.callee, state, name)
|
||||
: null;
|
||||
if (identity !== null) return { method: global, identity };
|
||||
}
|
||||
const method = builtinMethod(node.callee, state, [
|
||||
"http",
|
||||
@@ -379,7 +393,9 @@ const requestMethod = (
|
||||
"node:http",
|
||||
"node:https",
|
||||
]);
|
||||
return method === "request" || method === "get" ? method : null;
|
||||
return method === "request" || method === "get"
|
||||
? { method, identity: "complete" }
|
||||
: null;
|
||||
};
|
||||
|
||||
const requestFields = (
|
||||
|
||||
@@ -53,6 +53,82 @@ type PropertyPath = JavaScriptSemanticPropertyPath;
|
||||
type ValueEffect = "write" | "escape";
|
||||
|
||||
/** Preserve uncertainty from property writes and references exposed to calls, yielding during traversal and fixpoint iteration. */
|
||||
|
||||
/** Setter keys of one object literal; a computed setter key may match any key. */
|
||||
interface SetterKeys {
|
||||
readonly names: ReadonlySet<string>;
|
||||
readonly computed: boolean;
|
||||
}
|
||||
|
||||
// Writes to one literal repeat; scan its properties once, including the
|
||||
// common result that it defines no setter.
|
||||
const setterKeysByObject = new WeakMap<t.ObjectExpression, SetterKeys | null>();
|
||||
|
||||
const setterKeys = (object: t.ObjectExpression): SetterKeys | null => {
|
||||
const cached = setterKeysByObject.get(object);
|
||||
if (cached !== undefined) return cached;
|
||||
const names = new Set<string>();
|
||||
let computed = false;
|
||||
// Follow definition order: a later data property or method with the same
|
||||
// static name replaces the accessor, so its setter no longer runs. Spreads
|
||||
// and unknown computed keys may or may not replace it and keep it.
|
||||
for (const property of object.properties) {
|
||||
if (t.isSpreadElement(property)) continue;
|
||||
const name = semanticStaticPropertyKey(property.key, property.computed);
|
||||
if (t.isObjectMethod(property) && property.kind === "set") {
|
||||
if (name === null) computed = true;
|
||||
else names.add(name);
|
||||
} else if (
|
||||
name !== null &&
|
||||
!(t.isObjectMethod(property) && property.kind === "get")
|
||||
)
|
||||
names.delete(name);
|
||||
}
|
||||
const keys = names.size === 0 && !computed ? null : { names, computed };
|
||||
setterKeysByObject.set(object, keys);
|
||||
return keys;
|
||||
};
|
||||
|
||||
/**
|
||||
* The receiver path whose own object-literal setter an assignment to `path`
|
||||
* runs, following nested literal members from the binding's initializer.
|
||||
*/
|
||||
const localSetterReceiver = (
|
||||
initializers: readonly {
|
||||
readonly node: t.Node;
|
||||
readonly projection: readonly unknown[];
|
||||
}[],
|
||||
path: readonly unknown[],
|
||||
): readonly string[] | undefined => {
|
||||
const key = path.at(-1);
|
||||
if (typeof key !== "string") return undefined;
|
||||
const receiver = path.slice(0, -1);
|
||||
if (!receiver.every((segment) => typeof segment === "string"))
|
||||
return undefined;
|
||||
for (const { node, projection } of initializers) {
|
||||
if (projection.length !== 0) continue;
|
||||
let object: t.Node | undefined = unwrapJavaScriptExpression(node).node;
|
||||
for (const segment of receiver) {
|
||||
if (!t.isObjectExpression(object)) break;
|
||||
const member: t.ObjectExpression["properties"][number] | undefined =
|
||||
object.properties.findLast(
|
||||
(property) =>
|
||||
t.isObjectProperty(property) &&
|
||||
semanticStaticPropertyKey(property.key, property.computed) ===
|
||||
segment,
|
||||
);
|
||||
object = t.isObjectProperty(member)
|
||||
? unwrapJavaScriptExpression(member.value).node
|
||||
: undefined;
|
||||
}
|
||||
if (!t.isObjectExpression(object)) continue;
|
||||
const keys = setterKeys(object);
|
||||
if (keys !== null && (keys.computed || keys.names.has(key)))
|
||||
return receiver as readonly string[];
|
||||
}
|
||||
return undefined;
|
||||
};
|
||||
|
||||
export function* collectSemanticMemberMutationsSteps(
|
||||
program: t.Program,
|
||||
state: JavaScriptSemanticAnalysisState,
|
||||
@@ -377,6 +453,16 @@ export function* collectSemanticMemberMutationsSteps(
|
||||
current.originAt,
|
||||
parents,
|
||||
);
|
||||
// A setter on the binding's own literal runs on assignment and may
|
||||
// replace any member of its receiver, so the written key is unknown.
|
||||
// Deleting the accessor never calls it.
|
||||
if (
|
||||
effect === "write" &&
|
||||
!t.isUnaryExpression(mutation, { operator: "delete" })
|
||||
) {
|
||||
const receiver = localSetterReceiver(origins.initializers, path);
|
||||
if (receiver !== undefined) path = [...receiver, null];
|
||||
}
|
||||
// Keep an antichain of exact paths. Widening distinct member keys would
|
||||
// also invalidate unrelated siblings that never reach this effect.
|
||||
// This map is local to the file scan. Escaping the binding id keeps a
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
import * as t from "@babel/types";
|
||||
import { expect, it } from "vitest";
|
||||
|
||||
import { indexJavaScriptBindingsSteps } from "./javascriptSemanticAnalysis.js";
|
||||
import { semanticObjectPropertyReferences } from "./javascriptSemanticObjectReferences.js";
|
||||
import { completeSemanticSteps } from "./javascriptSemanticTraversal.js";
|
||||
import { parseJavaScriptSource } from "./javascriptSourceParser.js";
|
||||
|
||||
const objectAndState = (source: string) => {
|
||||
const parsed = parseJavaScriptSource(source, "object.js");
|
||||
if (parsed === null) throw new Error("Expected a parsed fixture");
|
||||
const declaration = parsed.program.body[0];
|
||||
if (!t.isVariableDeclaration(declaration))
|
||||
throw new Error("Expected a declaration");
|
||||
const init = declaration.declarations[0]?.init;
|
||||
if (!t.isObjectExpression(init)) throw new Error("Expected an object");
|
||||
return {
|
||||
object: init,
|
||||
source,
|
||||
state: completeSemanticSteps(indexJavaScriptBindingsSteps(parsed.program)),
|
||||
};
|
||||
};
|
||||
|
||||
const spelled = (
|
||||
fixture: ReturnType<typeof objectAndState>,
|
||||
key: string | number | null,
|
||||
) =>
|
||||
semanticObjectPropertyReferences(fixture.object, [key], fixture.state).map(
|
||||
({ node, path }) => [
|
||||
fixture.source.slice(node.start ?? 0, node.end ?? 0),
|
||||
JSON.stringify(path),
|
||||
],
|
||||
);
|
||||
|
||||
it("resolves exact keys with source-order overrides, spreads and accessors", () => {
|
||||
const fixture = objectAndState(
|
||||
`const o = { a: 1, ...base, b: 2, a: 3, [dynamic]: 4, __proto__: proto, ...{ b: 5 }, get c() { return 6; }, c: 7 };`,
|
||||
);
|
||||
// Expectations match the full source-order scan these exact keys replace.
|
||||
expect(spelled(fixture, "a")).toEqual([
|
||||
["{ b: 5 }", '["a"]'],
|
||||
["4", "[]"],
|
||||
["3", "[]"],
|
||||
]);
|
||||
expect(spelled(fixture, "b")).toEqual([
|
||||
["{ b: 5 }", '["b"]'],
|
||||
["4", "[]"],
|
||||
]);
|
||||
expect(spelled(fixture, "c")).toEqual([
|
||||
["7", "[]"],
|
||||
["4", "[]"],
|
||||
]);
|
||||
expect(spelled(fixture, "missing")).toEqual([
|
||||
["{ b: 5 }", '["missing"]'],
|
||||
["proto", '["missing"]'],
|
||||
["4", "[]"],
|
||||
["base", '["missing"]'],
|
||||
]);
|
||||
// A wildcard key still scans every property.
|
||||
expect(spelled(fixture, null)).toEqual([
|
||||
["7", "[]"],
|
||||
["{ b: 5 }", '[{"excludedKeys":["c"]}]'],
|
||||
["proto", "[null]"],
|
||||
["4", "[]"],
|
||||
["3", "[]"],
|
||||
["2", "[]"],
|
||||
["base", '[{"excludedKeys":["a","b","c"]}]'],
|
||||
]);
|
||||
// Numeric keys use the same exact spelling as string keys.
|
||||
expect(
|
||||
spelled(objectAndState(`const o = { 1: "one", 2: "two" };`), 1),
|
||||
).toEqual([['"one"', "[]"]]);
|
||||
});
|
||||
@@ -739,6 +739,75 @@ const getterResultReferences = (
|
||||
return references;
|
||||
};
|
||||
|
||||
type ObjectLiteralMember = t.ObjectExpression["properties"][number];
|
||||
|
||||
/** Property positions grouped by whether an exact key can reach them. */
|
||||
interface ObjectPropertyIndex {
|
||||
/** Spreads, computed keys and `__proto__` data entries reach every key. */
|
||||
readonly keyIndependent: readonly number[];
|
||||
readonly byName: ReadonlyMap<string, readonly number[]>;
|
||||
}
|
||||
|
||||
// Mutation tracking resolves one key of the same literal per write, so the
|
||||
// index removes the per-write scan of every property.
|
||||
const propertyIndexes = new WeakMap<t.ObjectExpression, ObjectPropertyIndex>();
|
||||
|
||||
const objectPropertyIndex = (node: t.ObjectExpression): ObjectPropertyIndex => {
|
||||
const cached = propertyIndexes.get(node);
|
||||
if (cached !== undefined) return cached;
|
||||
const keyIndependent: number[] = [];
|
||||
const byName = new Map<string, number[]>();
|
||||
node.properties.forEach((property, index) => {
|
||||
const name = t.isSpreadElement(property)
|
||||
? null
|
||||
: semanticStaticPropertyKey(property.key, property.computed);
|
||||
if (
|
||||
name === null ||
|
||||
(t.isObjectProperty(property) &&
|
||||
!property.computed &&
|
||||
!property.shorthand &&
|
||||
name === "__proto__")
|
||||
) {
|
||||
keyIndependent.push(index);
|
||||
return;
|
||||
}
|
||||
const positions = byName.get(name);
|
||||
if (positions === undefined) byName.set(name, [index]);
|
||||
else positions.push(index);
|
||||
});
|
||||
const index = { keyIndependent, byName };
|
||||
propertyIndexes.set(node, index);
|
||||
return index;
|
||||
};
|
||||
|
||||
/**
|
||||
* Properties in reverse source order that can affect `key`. For an exact key,
|
||||
* another static name only updates bookkeeping for that other name, which
|
||||
* this key never reads, so only same-name and key-independent entries remain.
|
||||
*/
|
||||
const relevantPropertiesInReverse = (
|
||||
node: t.ObjectExpression,
|
||||
key: PropertyPath[number] | undefined,
|
||||
): readonly ObjectLiteralMember[] => {
|
||||
if (typeof key !== "string" && typeof key !== "number")
|
||||
return [...node.properties].reverse();
|
||||
const { keyIndependent, byName } = objectPropertyIndex(node);
|
||||
const named = byName.get(String(key)) ?? [];
|
||||
const relevant: ObjectLiteralMember[] = [];
|
||||
let left = keyIndependent.length - 1;
|
||||
let right = named.length - 1;
|
||||
while (left >= 0 || right >= 0) {
|
||||
const fromLeft =
|
||||
right < 0 ||
|
||||
(left >= 0 && (keyIndependent[left] ?? -1) > (named[right] ?? -1));
|
||||
const position = fromLeft ? keyIndependent[left--] : named[right--];
|
||||
const property =
|
||||
position === undefined ? undefined : node.properties[position];
|
||||
if (property !== undefined) relevant.push(property);
|
||||
}
|
||||
return relevant;
|
||||
};
|
||||
|
||||
export const semanticObjectPropertyReferences = (
|
||||
node: t.ObjectExpression,
|
||||
path: PropertyPath,
|
||||
@@ -751,7 +820,7 @@ export const semanticObjectPropertyReferences = (
|
||||
const accessorNames = new Set<string>();
|
||||
const getterNames = new Set<string>();
|
||||
const setterNames = new Set<string>();
|
||||
for (const property of [...node.properties].reverse()) {
|
||||
for (const property of relevantPropertiesInReverse(node, key)) {
|
||||
if (t.isSpreadElement(property)) {
|
||||
const selected = key ?? null;
|
||||
if (typeof selected !== "object" && overwritten.has(String(selected)))
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
import { expect, it } from "vitest";
|
||||
|
||||
import { parseJavaScriptSource } from "./javascriptSourceParser.js";
|
||||
import { analyzeParsedJavaScriptStaticSource } from "./javascriptStaticAnalysis.js";
|
||||
|
||||
const analyze = (source: string, path: string) => {
|
||||
const parsed = parseJavaScriptSource(source, path);
|
||||
return parsed === null
|
||||
? null
|
||||
: {
|
||||
errors: parsed.errors.length,
|
||||
facts: analyzeParsedJavaScriptStaticSource(source, parsed),
|
||||
};
|
||||
};
|
||||
|
||||
it("parses conditional arrow bodies in JavaScript sources with JavaScript grammar", () => {
|
||||
// Bundlers emit this shape; TypeScript reads `):H=>` as a return type.
|
||||
const source = `const {ipcMain}=require("electron");
|
||||
const pick=n===0?H=>({start:H.startY}):H=>({start:H.startX});
|
||||
ipcMain.handle("layout:ready",()=>pick);`;
|
||||
for (const path of ["main.js", "main.cjs", "main.mjs", "main.jsx"]) {
|
||||
const result = analyze(source, path);
|
||||
expect(result?.errors).toBe(0);
|
||||
expect(result?.facts.electron.ipc).toContainEqual(
|
||||
expect.objectContaining({ channel: "layout:ready", operation: "handle" }),
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
it("keeps JavaScript comparison meaning for angle brackets in JavaScript sources", () => {
|
||||
const source = `const a=fetch<limit>("/api/compare");`;
|
||||
expect(analyze(source, "main.js")?.facts.endpoints).toEqual([]);
|
||||
expect(analyze(source, "main.ts")?.facts.endpoints).toContainEqual(
|
||||
expect.objectContaining({ value: "/api/compare" }),
|
||||
);
|
||||
});
|
||||
|
||||
it("keeps TypeScript syntax available for TypeScript and unnamed sources", () => {
|
||||
const source = "let count: number = 1; export type Id = string;";
|
||||
expect(parseJavaScriptSource(source, "value.ts")?.errors).toEqual([]);
|
||||
expect(parseJavaScriptSource(source)?.errors).toEqual([]);
|
||||
});
|
||||
@@ -9,17 +9,21 @@ export type ParsedJavaScriptSource = ReturnType<typeof parse>;
|
||||
*
|
||||
* - `dts:true` only for `.d.ts/.d.mts/.d.cts` ambient declarations;
|
||||
* ordinary `.ts` keeps the missing-initializer diagnostic.
|
||||
* - JSX enabled for `.tsx`, `.jsx`, `.js`, and unknown paths; disabled for
|
||||
* plain `.ts/.cts/.mts` (angle-bracket assertions, not JSX) and for all
|
||||
* - `.js/.jsx/.mjs/.cjs` use JavaScript grammar with JSX. TypeScript reads
|
||||
* some valid JavaScript differently (`c?x=>(a):y=>(b)` as a return type,
|
||||
* `a<b>(c)` as a generic call), so it must not select their meaning.
|
||||
* - JSX enabled for `.tsx` and unknown paths; disabled for plain
|
||||
* `.ts/.cts/.mts` (angle-bracket assertions, not JSX) and for all
|
||||
* declaration files. `.mts` (including `.d.mts`) sets
|
||||
* `disallowAmbiguousJSXLike`, matching tsc's `.mts` mode.
|
||||
* - `decorators-legacy` always present: without it decorated sources fail
|
||||
* hard instead of reporting recovered syntax, losing every derived fact.
|
||||
* - Unknown paths retain the existing JSX-capable contract (best-effort
|
||||
* parse; failures surface via `errorRecovery`, never silent misparse).
|
||||
* - Unknown paths retain the existing JSX- and TypeScript-capable contract
|
||||
* (best-effort parse; failures surface via `errorRecovery`).
|
||||
*/
|
||||
export const parserPluginsForPath = (sourcePath?: string): ParserPlugin[] => {
|
||||
const path = sourcePath ?? "";
|
||||
if (/\.(?:js|jsx|mjs|cjs)$/iu.test(path)) return ["decorators-legacy", "jsx"];
|
||||
const dts = /\.d\.(?:ts|mts|cts)$/iu.test(path);
|
||||
const isTsLike = /\.(?:ts|cts|mts)$/iu.test(path);
|
||||
const isTsx = /\.tsx$/iu.test(path);
|
||||
|
||||
@@ -42,10 +42,7 @@ import type { JavaScriptStaticAnalysis } from "./javascriptStaticAnalysisTypes.j
|
||||
import type { JavaScriptSemanticAnalysisState } from "./javascriptSemanticState.js";
|
||||
import {
|
||||
indexJavaScriptElectronIdentitySteps,
|
||||
indexJavaScriptBindingsSteps,
|
||||
classifyParsedJavaScriptRpcClientRootsSteps,
|
||||
classifyParsedJavaScriptOpenReceivers,
|
||||
type JavaScriptOpenReceiverFact,
|
||||
indexJavaScriptBindingsWithReceiversSteps,
|
||||
} from "./javascriptSemanticAnalysis.js";
|
||||
import {
|
||||
parseJavaScriptSource,
|
||||
@@ -67,45 +64,38 @@ export const analyzeJavaScriptStaticSource = (
|
||||
const file = parseJavaScriptSource(source);
|
||||
return file === null
|
||||
? failedJavaScriptStaticAnalysis()
|
||||
: analyzeParsedJavaScriptStaticSource(
|
||||
source,
|
||||
file,
|
||||
classifyParsedJavaScriptOpenReceivers(file),
|
||||
);
|
||||
: analyzeParsedJavaScriptStaticSource(source, file);
|
||||
};
|
||||
|
||||
/** Recover static structure from an already parsed JavaScript artifact. */
|
||||
export const analyzeParsedJavaScriptStaticSource = (
|
||||
source: string,
|
||||
file: ParsedJavaScriptSource,
|
||||
openReceiverFacts?: ReadonlyMap<number, JavaScriptOpenReceiverFact>,
|
||||
): JavaScriptStaticAnalysis =>
|
||||
completeSemanticSteps(
|
||||
analyzeParsedJavaScriptStaticSourceSteps(source, file, openReceiverFacts),
|
||||
);
|
||||
completeSemanticSteps(analyzeParsedJavaScriptStaticSourceSteps(source, file));
|
||||
|
||||
/** Static analysis that yields during its source traversal; results are unchanged. */
|
||||
export function* analyzeParsedJavaScriptStaticSourceSteps(
|
||||
source: string,
|
||||
file: ParsedJavaScriptSource,
|
||||
openReceiverFacts?: ReadonlyMap<number, JavaScriptOpenReceiverFact>,
|
||||
): Generator<void, JavaScriptStaticAnalysis> {
|
||||
const accumulator = createJavaScriptAnalysisAccumulator(source.length);
|
||||
const bindings = yield* indexJavaScriptBindingsSteps(file.program);
|
||||
// One definitions pass serves receiver classification and binding lookup.
|
||||
const { bindings, receivers } =
|
||||
yield* indexJavaScriptBindingsWithReceiversSteps(file.program);
|
||||
const electronIdentity = yield* indexJavaScriptElectronIdentitySteps(
|
||||
file.program,
|
||||
bindings,
|
||||
);
|
||||
const clientRoots = yield* classifyParsedJavaScriptRpcClientRootsSteps(file);
|
||||
yield* traverseStaticSourceSteps(
|
||||
file,
|
||||
{
|
||||
source,
|
||||
accumulator,
|
||||
vitePreloadDependencyTable: indexVitePreloadDependencyTable(file.program),
|
||||
...(openReceiverFacts === undefined ? {} : { openReceiverFacts }),
|
||||
openReceiverFacts: receivers.openReceivers,
|
||||
electronBindings: electronIdentity.bindings,
|
||||
rpcScan: createJavaScriptRpcScan(source, clientRoots),
|
||||
rpcScan: createJavaScriptRpcScan(source, receivers.rpcClientRoots),
|
||||
},
|
||||
bindings,
|
||||
);
|
||||
|
||||
@@ -27,11 +27,12 @@ describe("static callee boundary matching", () => {
|
||||
["./bare.html", "call:loadFile"],
|
||||
["https://optional.example.test", "call:win.loadURL"],
|
||||
]);
|
||||
// The document base URL is unknown, so the script path stays unresolved.
|
||||
expect(
|
||||
analysis.references
|
||||
.filter(({ kind }) => kind === "service-worker")
|
||||
.map(({ specifier }) => specifier),
|
||||
).toEqual(["./worker.js"]);
|
||||
.map(({ specifier, expression }) => [specifier, expression]),
|
||||
).toEqual([[null, 'navigator.serviceWorker.register("./worker.js")']]);
|
||||
expect(
|
||||
analysis.endpoints
|
||||
.filter(({ mechanism }) => mechanism.endsWith("loadURL"))
|
||||
|
||||
@@ -95,19 +95,25 @@ export const inspectCall = (
|
||||
addReference(context, {
|
||||
node,
|
||||
kind: "worker",
|
||||
// Node string paths use the process cwd or filesystem root, neither of
|
||||
// which is established by the analyzed source's artifact location.
|
||||
// A string path is relative to the document base URL in a browser and
|
||||
// to the process cwd in Node; the analyzed source's artifact location
|
||||
// establishes neither. Only `new URL(path, import.meta.url)` names a
|
||||
// location relative to this module.
|
||||
specifier:
|
||||
(workerConstructor === "node" ? undefined : first) ??
|
||||
(workerConstructor === undefined || worker === undefined
|
||||
workerConstructor === undefined || worker === undefined
|
||||
? undefined
|
||||
: workerUrlSpecifier(worker, bindings)),
|
||||
: workerUrlSpecifier(worker, bindings),
|
||||
});
|
||||
// Service worker script URLs are also relative to the document base URL.
|
||||
if (
|
||||
name === "serviceWorker.register" ||
|
||||
name.endsWith(".serviceWorker.register")
|
||||
)
|
||||
addReference(context, { node, kind: "service-worker", specifier: first });
|
||||
addReference(context, {
|
||||
node,
|
||||
kind: "service-worker",
|
||||
specifier: undefined,
|
||||
});
|
||||
inspectEndpointCall(node, name, first, context);
|
||||
inspectStorageCall(node, name, first, context);
|
||||
if (first === "audio" || first === "video")
|
||||
|
||||
@@ -1,5 +1,11 @@
|
||||
import * as t from "@babel/types";
|
||||
|
||||
import {
|
||||
semanticStaticPropertyKey,
|
||||
semanticWriteSiteTarget,
|
||||
semanticWriteTargets,
|
||||
unwrapJavaScriptExpression,
|
||||
} from "./javascript/javascriptAstValues.js";
|
||||
import { analyzeParsedJavaScriptReferences } from "./javascript/javascriptSemanticAnalysis.js";
|
||||
import {
|
||||
semanticBindingId,
|
||||
@@ -49,6 +55,22 @@ export const unshadowedReferenceSourceRequireCalls = (
|
||||
(reference.resolution === "unbound" ||
|
||||
reference.bindingId === programRequireBinding),
|
||||
);
|
||||
const isLoaderReference = (identifier: t.Identifier): boolean => {
|
||||
const start = identifier.loc?.start;
|
||||
const reference =
|
||||
start === undefined
|
||||
? undefined
|
||||
: references.get(`${start.line}:${start.column}`);
|
||||
return (
|
||||
(allowsUnboundRequire && reference?.resolution === "unbound") ||
|
||||
(reference?.resolution === "resolved" &&
|
||||
reference.bindingId === programRequireBinding)
|
||||
);
|
||||
};
|
||||
const loaderMembersIntact = requireLoaderMembersIntact(
|
||||
file.program,
|
||||
isLoaderReference,
|
||||
);
|
||||
const calls = new Set<t.CallExpression>();
|
||||
|
||||
traverseJavaScriptAst(file.program, {
|
||||
@@ -64,6 +86,9 @@ export const unshadowedReferenceSourceRequireCalls = (
|
||||
)
|
||||
)
|
||||
return;
|
||||
// `require.resolve` and `require.main` denote Node's members only
|
||||
// while no visible write or escape can have replaced them.
|
||||
if (!t.isIdentifier(node.callee) && !loaderMembersIntact) return;
|
||||
const reference = references.get(`${start.line}:${start.column}`);
|
||||
if (
|
||||
(allowsUnboundRequire &&
|
||||
@@ -80,6 +105,68 @@ export const unshadowedReferenceSourceRequireCalls = (
|
||||
return calls;
|
||||
};
|
||||
|
||||
/**
|
||||
* Whether the loader's `resolve` and `main` members keep Node's identity. A
|
||||
* write to either member or to a computed member, or `require` used as a
|
||||
* value other than a call or member read (where a mutator or alias can
|
||||
* replace members), leaves that identity unknown.
|
||||
*/
|
||||
const requireLoaderMembersIntact = (
|
||||
program: t.Program,
|
||||
isLoaderReference: (identifier: t.Identifier) => boolean,
|
||||
): boolean => {
|
||||
let intact = true;
|
||||
const isLoaderMemberTarget = (target: t.Node): boolean => {
|
||||
if (!t.isMemberExpression(target) && !t.isOptionalMemberExpression(target))
|
||||
return false;
|
||||
const object = unwrapJavaScriptExpression(target.object).node;
|
||||
if (!t.isIdentifier(object, { name: "require" })) return false;
|
||||
if (!isLoaderReference(object)) return false;
|
||||
const key = semanticStaticPropertyKey(target.property, target.computed);
|
||||
return key === null || key === "resolve" || key === "main";
|
||||
};
|
||||
traverseJavaScriptAst(program, {
|
||||
enter: (node, parent, readAncestors) => {
|
||||
if (!intact) return;
|
||||
const site = semanticWriteSiteTarget(node);
|
||||
if (site !== null) {
|
||||
for (const target of semanticWriteTargets(site))
|
||||
if (isLoaderMemberTarget(target)) intact = false;
|
||||
return;
|
||||
}
|
||||
if (
|
||||
!t.isIdentifier(node, { name: "require" }) ||
|
||||
parent === null ||
|
||||
!isLoaderReference(node)
|
||||
)
|
||||
return;
|
||||
// Inert parentheses and TypeScript wrappers do not change how the
|
||||
// loader value is used.
|
||||
let used: t.Node = node;
|
||||
let user: t.Node | undefined = parent;
|
||||
const read = readAncestors();
|
||||
const ancestors = read.at(-1) === parent ? read : [...read, parent];
|
||||
for (let index = ancestors.length - 1; index >= 0; index -= 1) {
|
||||
user = ancestors[index];
|
||||
if (
|
||||
user === undefined ||
|
||||
unwrapJavaScriptExpression(user).node !== node
|
||||
)
|
||||
break;
|
||||
used = user;
|
||||
}
|
||||
const usedAsLoader =
|
||||
((t.isCallExpression(user) || t.isOptionalCallExpression(user)) &&
|
||||
user.callee === used) ||
|
||||
((t.isMemberExpression(user) || t.isOptionalMemberExpression(user)) &&
|
||||
user.object === used) ||
|
||||
t.isUnaryExpression(user, { operator: "typeof" });
|
||||
if (!usedAsLoader) intact = false;
|
||||
},
|
||||
});
|
||||
return intact;
|
||||
};
|
||||
|
||||
const contains = (container: t.Node, node: t.Node): boolean =>
|
||||
typeof container.start === "number" &&
|
||||
typeof container.end === "number" &&
|
||||
|
||||
@@ -4,14 +4,8 @@ import { sanitizeEndpointCandidate } from "./browserObservation.js";
|
||||
import { compositeKey } from "./unicodeCodePointOrder.js";
|
||||
import type { WebPageInspection } from "./browserObservationSchemas.js";
|
||||
import type { WebBundleAnalysis } from "./webBundleAnalysis.js";
|
||||
import {
|
||||
completeSemanticSteps,
|
||||
traverseJavaScriptAst,
|
||||
} from "./javascript/javascriptSemanticTraversal.js";
|
||||
import {
|
||||
classifyParsedJavaScriptOpenReceivers,
|
||||
classifyParsedJavaScriptRpcClientRootsSteps,
|
||||
} from "./javascript/javascriptSemanticAnalysis.js";
|
||||
import { traverseJavaScriptAst } from "./javascript/javascriptSemanticTraversal.js";
|
||||
import { classifyParsedJavaScriptReceivers } from "./javascript/javascriptSemanticAnalysis.js";
|
||||
import type { JavaScriptOpenReceiverFact } from "./javascript/javascriptSemanticAnalysis.js";
|
||||
import { semanticStaticPropertyName } from "./javascript/javascriptAstValues.js";
|
||||
import {
|
||||
@@ -67,10 +61,11 @@ export const analyzeScript = (
|
||||
if ((file.errors?.length ?? 0) > 0)
|
||||
accumulator.recoveredScripts.push(script.script_key);
|
||||
detectVendorFingerprints(script, accumulator);
|
||||
const openReceiverFacts = classifyParsedJavaScriptOpenReceivers(file);
|
||||
const receivers = classifyParsedJavaScriptReceivers(file);
|
||||
const openReceiverFacts = receivers.openReceivers;
|
||||
const rpc = createJavaScriptRpcScan(
|
||||
script.source.artifact.text,
|
||||
completeSemanticSteps(classifyParsedJavaScriptRpcClientRootsSteps(file)),
|
||||
receivers.rpcClientRoots,
|
||||
);
|
||||
traverseJavaScriptAst(file, {
|
||||
enter: (node) => {
|
||||
|
||||
@@ -10,6 +10,7 @@ import {
|
||||
} from "../domain/managed/managedArtifact.js";
|
||||
import {
|
||||
managedCli32BitRequirement,
|
||||
managedCliIsMixedClrNative,
|
||||
readManagedPeLayout,
|
||||
type ManagedCliHeader,
|
||||
type ManagedPeLayout,
|
||||
@@ -164,7 +165,7 @@ const runtimeFamilyFromCliHeader = (
|
||||
): ManagedArtifactInspection["classification"]["runtime_family"] =>
|
||||
cli?.readyToRunSignature === true
|
||||
? "modern-dotnet"
|
||||
: cli !== null && (cli.flags & 1) === 0
|
||||
: cli !== null && managedCliIsMixedClrNative(cli)
|
||||
? "mixed-clr-native"
|
||||
: "unknown";
|
||||
|
||||
@@ -190,8 +191,6 @@ const runtimeFamilyFor = (
|
||||
);
|
||||
let runtimeFamily: ManagedArtifactInspection["classification"]["runtime_family"] =
|
||||
"unknown";
|
||||
// crossgen2 clears ILONLY on ReadyToRun output, so only a clear flag
|
||||
// without the ReadyToRun signature indicates mixed CLR/native code.
|
||||
if (runtimeFamilyFromCliHeader(cli) === "mixed-clr-native") {
|
||||
runtimeFamily = "mixed-clr-native";
|
||||
evidence.push({
|
||||
|
||||
@@ -35,6 +35,35 @@ describe("managed projection admission", () => {
|
||||
ManagedReaderFailure,
|
||||
);
|
||||
});
|
||||
|
||||
it("charges an admitted record once inside its containing projection", () => {
|
||||
const budget = new ManagedDecodeBudget();
|
||||
const record = admitManagedProjection(
|
||||
{ name: "a".repeat(40 * 1024 * 1024) },
|
||||
budget,
|
||||
);
|
||||
|
||||
expect(() =>
|
||||
admitManagedProjection({ records: [record] }, budget),
|
||||
).not.toThrow();
|
||||
// The containing projection spent the credit, so later reuse pays again.
|
||||
expect(() =>
|
||||
admitManagedProjection({ records: [record, record] }, budget),
|
||||
).toThrow(ManagedReaderFailure);
|
||||
});
|
||||
|
||||
it("charges a repeated occurrence of an admitted record again", () => {
|
||||
const budget = new ManagedDecodeBudget();
|
||||
const record = admitManagedProjection(
|
||||
{ name: "a".repeat(40 * 1024 * 1024) },
|
||||
budget,
|
||||
);
|
||||
|
||||
// The first occurrence spends the credit; the second serializes again.
|
||||
expect(() =>
|
||||
admitManagedProjection({ records: [record, record] }, budget),
|
||||
).toThrow(ManagedReaderFailure);
|
||||
});
|
||||
});
|
||||
|
||||
describe("managed decoding bounds", () => {
|
||||
|
||||
@@ -18,6 +18,7 @@ export class ManagedDecodeBudget {
|
||||
readonly #rows = new Map<number, Set<number>>();
|
||||
readonly #strings = new Map<number, CachedString>();
|
||||
readonly #digests = new WeakMap<ArrayBufferLike, Map<string, string>>();
|
||||
readonly #prepaid = new WeakSet<object>();
|
||||
|
||||
reserve(bytes: number, offset: number | null): void {
|
||||
this.#admit(bytes, offset);
|
||||
@@ -82,6 +83,16 @@ export class ManagedDecodeBudget {
|
||||
return digest;
|
||||
}
|
||||
|
||||
/** Record an admitted projection; its next serialized occurrence is already charged. */
|
||||
prepay(value: object): void {
|
||||
this.#prepaid.add(value);
|
||||
}
|
||||
|
||||
/** Spend an admitted projection's charge once; repeated occurrences pay again. */
|
||||
consumePrepaid(value: object): boolean {
|
||||
return this.#prepaid.delete(value);
|
||||
}
|
||||
|
||||
cachedString(index: number): CachedString | undefined {
|
||||
return this.#strings.get(index);
|
||||
}
|
||||
@@ -141,12 +152,23 @@ const jsonBytes = (item: unknown): number =>
|
||||
? managedJsonStringBytes(item)
|
||||
: Buffer.byteLength(JSON.stringify(item) ?? "null");
|
||||
|
||||
/** Admit repeated values too: shared heap strings still expand at the JSON boundary. */
|
||||
/**
|
||||
* Admit repeated values too: shared heap strings still expand at the JSON
|
||||
* boundary. An object admitted earlier was charged for one occurrence, so a
|
||||
* containing projection spends that charge instead of counting it again.
|
||||
* Admitted values must stay unmodified.
|
||||
*/
|
||||
export const admitManagedProjection = <Value>(
|
||||
value: Value,
|
||||
budget: ManagedDecodeBudget = new ManagedDecodeBudget(),
|
||||
): Value => {
|
||||
const visit = (item: unknown): void => {
|
||||
if (
|
||||
item !== null &&
|
||||
typeof item === "object" &&
|
||||
budget.consumePrepaid(item)
|
||||
)
|
||||
return;
|
||||
if (Array.isArray(item)) {
|
||||
budget.reserve(item.length + 2, null);
|
||||
for (const child of item) visit(child);
|
||||
@@ -161,5 +183,6 @@ export const admitManagedProjection = <Value>(
|
||||
}
|
||||
};
|
||||
visit(value);
|
||||
if (value !== null && typeof value === "object") budget.prepay(value);
|
||||
return value;
|
||||
};
|
||||
@@ -153,7 +153,8 @@ describe("CLR ExtraData index widths and boundaries", () => {
|
||||
|
||||
it("reports a truncated row count before interpreting ExtraData", () => {
|
||||
const bytes = buildManagedPeFixture({ metadataExtraData: 0x1234_5678 });
|
||||
const { streamHeader, extraDataOffset } = fixtureTablesStream(bytes);
|
||||
const { streamHeader, tablesOffset, extraDataOffset } =
|
||||
fixtureTablesStream(bytes);
|
||||
bytes.writeUInt32LE(extraDataOffset - 1, streamHeader + 4);
|
||||
const result = inspectManagedMembersBytes(
|
||||
bytes,
|
||||
@@ -166,6 +167,8 @@ describe("CLR ExtraData index widths and boundaries", () => {
|
||||
expect.objectContaining({
|
||||
code: "invalid-metadata-root",
|
||||
scope: "metadata.table-row-count",
|
||||
// The last row count's artifact file offset, not its stream offset.
|
||||
offset: tablesOffset + extraDataOffset - 4,
|
||||
}),
|
||||
],
|
||||
});
|
||||
@@ -173,6 +176,24 @@ describe("CLR ExtraData index widths and boundaries", () => {
|
||||
expect(result.methods).toEqual([]);
|
||||
});
|
||||
|
||||
it("reports a stream extent beyond the metadata root at its file offset", () => {
|
||||
const bytes = buildManagedPeFixture();
|
||||
const { streamHeader, tablesOffset } = fixtureTablesStream(bytes);
|
||||
bytes.writeUInt32LE(0xffff_ffff, streamHeader + 4);
|
||||
const result = inspectManagedMembersBytes(
|
||||
bytes,
|
||||
managedPeFixtureTarget(bytes),
|
||||
);
|
||||
|
||||
expect(result.coverage.issues).toEqual([
|
||||
expect.objectContaining({
|
||||
code: "invalid-metadata-root",
|
||||
scope: "metadata.#~",
|
||||
offset: tablesOffset,
|
||||
}),
|
||||
]);
|
||||
});
|
||||
|
||||
it.each([0, 1, 3])(
|
||||
"rejects an ExtraData word with only %i declared bytes",
|
||||
(availableBytes) => {
|
||||
|
||||
@@ -116,8 +116,10 @@ const CODED_INDEXES = {
|
||||
|
||||
const SUPPORTED_TABLE_MASK = (1n << BigInt(METADATA_TABLE_NAMES.length)) - 1n;
|
||||
|
||||
/** Check a range within `bytes`, which start at artifact file offset `base`. */
|
||||
const requireRange = (
|
||||
bytes: Buffer,
|
||||
base: number,
|
||||
offset: number,
|
||||
length: number,
|
||||
scope: string,
|
||||
@@ -133,7 +135,7 @@ const requireRange = (
|
||||
"invalid-metadata-root",
|
||||
scope,
|
||||
`${scope} leaves the metadata byte range`,
|
||||
Number.isSafeInteger(offset) && offset >= 0 ? offset : null,
|
||||
Number.isSafeInteger(base + offset) && offset >= 0 ? base + offset : null,
|
||||
);
|
||||
};
|
||||
|
||||
@@ -224,7 +226,7 @@ const readMetadataStreams = (
|
||||
rootOffset,
|
||||
);
|
||||
const versionLength = bytes.readUInt32LE(12);
|
||||
requireRange(bytes, 16, versionLength, "metadata.version");
|
||||
requireRange(bytes, rootOffset, 16, versionLength, "metadata.version");
|
||||
const versionBytes = bytes.subarray(16, 16 + versionLength);
|
||||
const zero = versionBytes.indexOf(0);
|
||||
const decodedVersionLength = zero < 0 ? versionBytes.length : zero;
|
||||
@@ -233,7 +235,7 @@ const readMetadataStreams = (
|
||||
.subarray(0, decodedVersionLength)
|
||||
.toString("utf8");
|
||||
let cursor = align4(16 + versionLength);
|
||||
requireRange(bytes, cursor, 4, "metadata.stream-count");
|
||||
requireRange(bytes, rootOffset, cursor, 4, "metadata.stream-count");
|
||||
const streamCount = bytes.readUInt16LE(cursor + 2);
|
||||
if (streamCount === 0 || streamCount > 64)
|
||||
throw managedFailure(
|
||||
@@ -245,7 +247,7 @@ const readMetadataStreams = (
|
||||
cursor += 4;
|
||||
const streams = new Map<string, MetadataStream>();
|
||||
for (let index = 0; index < streamCount; index += 1) {
|
||||
requireRange(bytes, cursor, 8, "metadata.stream-header");
|
||||
requireRange(bytes, rootOffset, cursor, 8, "metadata.stream-header");
|
||||
const relativeOffset = bytes.readUInt32LE(cursor);
|
||||
const streamSize = bytes.readUInt32LE(cursor + 4);
|
||||
const named = readStreamName(bytes, cursor + 8);
|
||||
@@ -256,7 +258,13 @@ const readMetadataStreams = (
|
||||
`Duplicate metadata stream ${named.name}`,
|
||||
rootOffset + cursor,
|
||||
);
|
||||
requireRange(bytes, relativeOffset, streamSize, `metadata.${named.name}`);
|
||||
requireRange(
|
||||
bytes,
|
||||
rootOffset,
|
||||
relativeOffset,
|
||||
streamSize,
|
||||
`metadata.${named.name}`,
|
||||
);
|
||||
streams.set(named.name, {
|
||||
name: named.name,
|
||||
offset: rootOffset + relativeOffset,
|
||||
@@ -295,7 +303,13 @@ const readTablesHeader = (
|
||||
tablesStream.offset,
|
||||
tablesStream.offset + tablesStream.size,
|
||||
);
|
||||
requireRange(tableBytes, 0, 24, "metadata.tables-header");
|
||||
requireRange(
|
||||
tableBytes,
|
||||
tablesStream.offset,
|
||||
0,
|
||||
24,
|
||||
"metadata.tables-header",
|
||||
);
|
||||
const heapSizes = tableBytes[6] ?? 0;
|
||||
const heaps = {
|
||||
string: (heapSizes & 0x01) === 0 ? (2 as const) : (4 as const),
|
||||
@@ -315,13 +329,20 @@ const readTablesHeader = (
|
||||
|
||||
const readRowCounts = (
|
||||
tableBytes: Buffer,
|
||||
tablesOffset: number,
|
||||
valid: bigint,
|
||||
): readonly number[] => {
|
||||
const rowCounts = Array<number>(METADATA_TABLE_NAMES.length).fill(0);
|
||||
let tableCursor = 24;
|
||||
for (let index = 0; index < METADATA_TABLE_NAMES.length; index += 1) {
|
||||
if ((valid & (1n << BigInt(index))) === 0n) continue;
|
||||
requireRange(tableBytes, tableCursor, 4, "metadata.table-row-count");
|
||||
requireRange(
|
||||
tableBytes,
|
||||
tablesOffset,
|
||||
tableCursor,
|
||||
4,
|
||||
"metadata.table-row-count",
|
||||
);
|
||||
const count = tableBytes.readUInt32LE(tableCursor);
|
||||
rowCounts[index] = count;
|
||||
tableCursor += 4;
|
||||
@@ -384,7 +405,13 @@ const buildMetadataTables = ({
|
||||
"Metadata table byte size overflowed",
|
||||
tablesOffset + tableCursor,
|
||||
);
|
||||
requireRange(tableBytes, tableCursor, byteLength, "metadata.table-data");
|
||||
requireRange(
|
||||
tableBytes,
|
||||
tablesOffset,
|
||||
tableCursor,
|
||||
byteLength,
|
||||
"metadata.table-data",
|
||||
);
|
||||
tables.set(index, {
|
||||
index,
|
||||
name: METADATA_TABLE_NAMES[index] ?? `Table${String(index)}`,
|
||||
@@ -417,7 +444,7 @@ export const readManagedMetadataLayout = (
|
||||
artifact,
|
||||
streams,
|
||||
);
|
||||
const rowCounts = readRowCounts(tableBytes, valid);
|
||||
const rowCounts = readRowCounts(tableBytes, tablesOffset, valid);
|
||||
const tables = buildMetadataTables({
|
||||
tableBytes,
|
||||
rowCounts,
|
||||
|
||||
@@ -20,6 +20,8 @@ import { managedTableRowCounts } from "./ManagedMetadataInventory.js";
|
||||
import type { ManagedMetadataInventory } from "./ManagedMetadataInventory.js";
|
||||
import {
|
||||
managedCli32BitRequirement,
|
||||
managedCliIsMixedClrNative,
|
||||
type ManagedCliHeader,
|
||||
type ManagedPeLayout,
|
||||
} from "./ManagedPeReader.js";
|
||||
import { readManagedValue } from "./ManagedReaderFailure.js";
|
||||
@@ -378,36 +380,37 @@ const boundaryKind = (
|
||||
};
|
||||
|
||||
export const cliNative = (
|
||||
pe: ManagedPeLayout,
|
||||
cli: ManagedCliHeader | null,
|
||||
): ManagedNativeBoundaryInspection["cli_native"] => {
|
||||
if (pe.cli === null) return null;
|
||||
if (cli === null) return null;
|
||||
return {
|
||||
il_only: (pe.cli.flags & 0x0000_0001) !== 0,
|
||||
requires_32bit: managedCli32BitRequirement(pe.cli.flags) === "required",
|
||||
strong_name_signed: (pe.cli.flags & 0x0000_0008) !== 0,
|
||||
native_entry_point: (pe.cli.flags & 0x0000_0010) !== 0,
|
||||
ready_to_run_signature: pe.cli.readyToRunSignature,
|
||||
managed_native_header_rva: pe.cli.managedNativeHeader.rva,
|
||||
managed_native_header_size: pe.cli.managedNativeHeader.size,
|
||||
il_only: (cli.flags & 0x0000_0001) !== 0,
|
||||
requires_32bit: managedCli32BitRequirement(cli.flags) === "required",
|
||||
strong_name_signed: (cli.flags & 0x0000_0008) !== 0,
|
||||
native_entry_point: (cli.flags & 0x0000_0010) !== 0,
|
||||
ready_to_run_signature: cli.readyToRunSignature,
|
||||
managed_native_header_rva: cli.managedNativeHeader.rva,
|
||||
managed_native_header_size: cli.managedNativeHeader.size,
|
||||
};
|
||||
};
|
||||
|
||||
/** Summarize declaration counts with the native facets of the CLI header. */
|
||||
export const nativeBoundarySummary = (
|
||||
native: ManagedNativeBoundaryInspection["cli_native"],
|
||||
cli: ManagedCliHeader | null,
|
||||
counts: Pick<
|
||||
ManagedNativeBoundaryInspection["summary"],
|
||||
"module_ref_count" | "pinvoke_import_count" | "native_implementation_count"
|
||||
>,
|
||||
): ManagedNativeBoundaryInspection["summary"] => ({
|
||||
...counts,
|
||||
ready_to_run: native?.ready_to_run_signature ?? null,
|
||||
ready_to_run: cli?.readyToRunSignature ?? null,
|
||||
mixed_mode_or_native_header:
|
||||
native === null
|
||||
cli === null
|
||||
? null
|
||||
: native.managed_native_header_rva !== 0 ||
|
||||
native.managed_native_header_size !== 0 ||
|
||||
native.native_entry_point,
|
||||
: managedCliIsMixedClrNative(cli) ||
|
||||
cli.managedNativeHeader.rva !== 0 ||
|
||||
cli.managedNativeHeader.size !== 0 ||
|
||||
(cli.flags & 0x0000_0010) !== 0,
|
||||
});
|
||||
|
||||
interface BoundaryInspectionContext {
|
||||
@@ -419,19 +422,18 @@ interface BoundaryInspectionContext {
|
||||
readonly moduleRefs: readonly ModuleRef[];
|
||||
readonly imports: readonly NativeImport[];
|
||||
readonly implementations: readonly NativeImplementation[];
|
||||
readonly native: ManagedNativeBoundaryInspection["cli_native"];
|
||||
readonly issues: readonly ManagedParseIssue[];
|
||||
}
|
||||
|
||||
export const buildNativeBoundaryInspection = ({
|
||||
target,
|
||||
bytes,
|
||||
pe,
|
||||
layout,
|
||||
inventory,
|
||||
moduleRefs,
|
||||
imports,
|
||||
implementations,
|
||||
native,
|
||||
issues,
|
||||
}: BoundaryInspectionContext): ManagedNativeBoundaryInspection =>
|
||||
managedNativeBoundaryInspectionSchema.parse(
|
||||
@@ -454,11 +456,11 @@ export const buildNativeBoundaryInspection = ({
|
||||
requires_artifact_sha256: target.sha256,
|
||||
requires_mvid: inventory.module?.mvid ?? null,
|
||||
},
|
||||
cli_native: native,
|
||||
cli_native: cliNative(pe.cli),
|
||||
module_refs: moduleRefs,
|
||||
pinvoke_imports: imports,
|
||||
native_implementations: implementations,
|
||||
summary: nativeBoundarySummary(native, {
|
||||
summary: nativeBoundarySummary(pe.cli, {
|
||||
module_ref_count: moduleRefs.length,
|
||||
pinvoke_import_count: imports.length,
|
||||
native_implementation_count: implementations.length,
|
||||
|
||||
@@ -76,6 +76,35 @@ describe("managed native boundaries", () => {
|
||||
"P/Invoke rows prove managed import declarations only; this inspection does not verify that a native library, export, thunk, or provider-qualified function exists.",
|
||||
);
|
||||
});
|
||||
|
||||
it.each([
|
||||
{
|
||||
name: "C++/CLI with a native method body",
|
||||
options: {
|
||||
cliFlags: 0,
|
||||
methods: [
|
||||
{ name: "NativeMethod", body: Buffer.from([0xc3]), implFlags: 1 },
|
||||
],
|
||||
},
|
||||
mixed: true,
|
||||
},
|
||||
{
|
||||
name: "ILONLY clear with unreadable metadata",
|
||||
options: { cliFlags: 0, corruptMetadataSignature: true },
|
||||
mixed: true,
|
||||
},
|
||||
{ name: "pure IL", options: {}, mixed: false },
|
||||
{ name: "ReadyToRun", options: { readyToRun: true }, mixed: true },
|
||||
])("agrees with artifact classification for $name", ({ options, mixed }) => {
|
||||
const bytes = buildManagedPeFixture(options);
|
||||
const target = managedPeFixtureTarget(bytes);
|
||||
const artifact = inspectManagedArtifactBytes(bytes, target);
|
||||
const boundaries = inspectManagedNativeBoundariesBytes(bytes, target);
|
||||
|
||||
expect(boundaries.summary.mixed_mode_or_native_header).toBe(mixed);
|
||||
if (artifact.classification.runtime_family === "mixed-clr-native")
|
||||
expect(boundaries.summary.mixed_mode_or_native_header).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("managed native boundary coded indexes", () => {
|
||||
|
||||
@@ -16,6 +16,7 @@ import {
|
||||
import { readTypeMemberListFacts } from "./ManagedTypeMemberLists.js";
|
||||
import {
|
||||
readManagedPeLayout,
|
||||
type ManagedCliHeader,
|
||||
type ManagedPeLayout,
|
||||
} from "./ManagedPeReader.js";
|
||||
import { ManagedReaderFailure } from "./ManagedReaderFailure.js";
|
||||
@@ -33,8 +34,8 @@ import {
|
||||
type Inventory = ReturnType<typeof readManagedMetadataInventory>;
|
||||
|
||||
/**
|
||||
* Report a PE whose CLI metadata is absent or cannot be admitted. `native`
|
||||
* holds the CLI header facts when the header itself was admitted; otherwise
|
||||
* Report a PE whose CLI metadata is absent or cannot be admitted. `cli`
|
||||
* holds the CLI header when the header itself was admitted; otherwise
|
||||
* the header is absent (`not-managed`) or unreadable (`malformed`).
|
||||
*/
|
||||
const emptyInspection = (
|
||||
@@ -42,10 +43,10 @@ const emptyInspection = (
|
||||
bytes: Buffer,
|
||||
classification: "not-managed" | "malformed",
|
||||
{
|
||||
native = null,
|
||||
cli = null,
|
||||
issues = [],
|
||||
}: {
|
||||
readonly native?: ManagedNativeBoundaryInspection["cli_native"] | null;
|
||||
readonly cli?: ManagedCliHeader | null;
|
||||
readonly issues?: readonly ManagedParseIssue[];
|
||||
} = {},
|
||||
): ManagedNativeBoundaryInspection => {
|
||||
@@ -72,11 +73,11 @@ const emptyInspection = (
|
||||
requires_artifact_sha256: target.sha256,
|
||||
requires_mvid: null,
|
||||
},
|
||||
cli_native: native,
|
||||
cli_native: cliNative(cli),
|
||||
module_refs: [],
|
||||
pinvoke_imports: [],
|
||||
native_implementations: [],
|
||||
summary: nativeBoundarySummary(native, {
|
||||
summary: nativeBoundarySummary(cli, {
|
||||
module_ref_count: 0,
|
||||
pinvoke_import_count: 0,
|
||||
native_implementation_count: 0,
|
||||
@@ -141,7 +142,7 @@ export const inspectManagedNativeBoundariesBytes = (
|
||||
} catch (cause: unknown) {
|
||||
if (!(cause instanceof ManagedReaderFailure)) throw cause;
|
||||
return emptyInspection(target, bytes, "malformed", {
|
||||
native: cliNative(pe),
|
||||
cli: pe.cli,
|
||||
issues: [cause.issue],
|
||||
});
|
||||
}
|
||||
@@ -179,13 +180,12 @@ export const inspectManagedNativeBoundariesBytes = (
|
||||
moduleRefs,
|
||||
imports,
|
||||
implementations,
|
||||
native: cliNative(pe),
|
||||
issues,
|
||||
});
|
||||
} catch (cause: unknown) {
|
||||
if (!(cause instanceof ManagedReaderFailure)) throw cause;
|
||||
return emptyInspection(target, bytes, "malformed", {
|
||||
native: cliNative(pe),
|
||||
cli: pe.cli,
|
||||
issues: [...issues, cause.issue],
|
||||
});
|
||||
}
|
||||
|
||||
@@ -45,6 +45,13 @@ export const managedCli32BitRequirement = (
|
||||
return pair === 0x0002_0002 ? "preferred" : "none";
|
||||
};
|
||||
|
||||
/**
|
||||
* crossgen2 clears ILONLY on ReadyToRun output, so only a clear ILONLY flag
|
||||
* without the ReadyToRun signature marks mixed CLR/native code.
|
||||
*/
|
||||
export const managedCliIsMixedClrNative = (cli: ManagedCliHeader): boolean =>
|
||||
(cli.flags & 0x0000_0001) === 0 && !cli.readyToRunSignature;
|
||||
|
||||
export interface ManagedPeLayout {
|
||||
readonly machine: number;
|
||||
readonly architecture: "x86" | "x86_64" | "arm" | "arm64";
|
||||
|
||||
@@ -69,7 +69,6 @@ const doctorHost = (
|
||||
validTarget: () => Promise.resolve(true),
|
||||
executable: (path) =>
|
||||
Promise.resolve(hopperAvailable && path.includes("Hopper")),
|
||||
supportedLinuxHopper: () => Promise.resolve(true),
|
||||
linuxDemoRuntimeCheck: () =>
|
||||
Promise.resolve({
|
||||
name: "hopper-demo-runtime",
|
||||
|
||||
Loaded 100 of 149 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user