Skip to content

Commit ee73141

Browse files
authored
Bound retained metadata when listing Node SEA programs (R3.12) (#1377)
The code path, asset names and exec arguments kept from a Node single-executable program now share an 8 MiB limit on decoded text, and the listing says when metadata was left out instead of keeping all of it.
1 parent 5b1bf92 commit ee73141

4 files changed

Lines changed: 198 additions & 11 deletions

File tree

‎crates/formats/src/node_sea.rs‎

Lines changed: 77 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ const RESOURCE: &str = "NODE_SEA_BLOB";
1616
const FUSE: &[u8] = b"NODE_SEA_FUSE_fce680ab2cc467b6e072b8b5df1996b2:";
1717
/// How many items a count in the blob may claim.
1818
const MAX_COUNT: u64 = 1 << 20;
19+
/// Total decoded bytes retained for the main path, asset keys and execution arguments.
20+
const MAX_TEXT_BYTES: usize = 8 * 1024 * 1024;
1921

2022
const FLAG_NO_WARNING: u32 = 1;
2123
const FLAG_SNAPSHOT: u32 = 1 << 1;
@@ -74,6 +76,28 @@ impl Cursor<'_> {
7476
Some(Part { start: self.base.checked_add(at)?, len })
7577
}
7678

79+
/// Keep whole text fields only: shortening an asset key would invent an extraction path.
80+
fn retain_text(&self, part: Part, remaining: &mut usize) -> bool {
81+
if part.len > *remaining {
82+
return false;
83+
}
84+
let Some(start) = part.start.checked_sub(self.base) else {
85+
return false;
86+
};
87+
let Some(bytes) = self.blob.get(start..start.saturating_add(part.len)) else {
88+
return false;
89+
};
90+
let mut len = 0usize;
91+
for chunk in bytes.utf8_chunks() {
92+
len += chunk.valid().len() + if chunk.invalid().is_empty() { 0 } else { 3 };
93+
if len > *remaining {
94+
return false;
95+
}
96+
}
97+
*remaining -= len;
98+
true
99+
}
100+
77101
fn count(&mut self) -> Option<usize> {
78102
let n = self.number()?;
79103
if n > MAX_COUNT {
@@ -93,7 +117,10 @@ struct Parsed {
93117
code: Part,
94118
code_cache: Option<Part>,
95119
assets: Vec<(Part, Part)>,
120+
asset_count: usize,
96121
exec_argv: Vec<Part>,
122+
exec_argv_count: usize,
123+
metadata_truncated: bool,
97124
trailing_zeros: usize,
98125
}
99126

@@ -110,27 +137,61 @@ fn parse_with(blob: &[u8], base: usize, flags: u32, layout: Layout) -> Option<Pa
110137
extension = Some(e);
111138
c.pos += 1;
112139
}
113-
let code_path = if layout.code_path { Some(c.part()?) } else { None };
140+
let mut remaining = MAX_TEXT_BYTES;
141+
let mut metadata_truncated = false;
142+
let code_path = if layout.code_path {
143+
let path = c.part()?;
144+
if c.retain_text(path, &mut remaining) {
145+
Some(path)
146+
} else {
147+
metadata_truncated = true;
148+
None
149+
}
150+
} else {
151+
None
152+
};
114153
let code = c.part()?;
115154
let code_cache = if flags & FLAG_CODE_CACHE != 0 { Some(c.part()?) } else { None };
155+
let asset_count = if flags & FLAG_ASSETS != 0 { c.count()? } else { 0 };
156+
let asset_limit = MAX_ENTRIES.saturating_sub(1 + usize::from(code_cache.is_some()));
116157
let mut assets = Vec::new();
117-
if flags & FLAG_ASSETS != 0 {
118-
for _ in 0..c.count()? {
119-
let key = c.part()?;
120-
assets.push((key, c.part()?));
158+
for _ in 0..asset_count {
159+
let key = c.part()?;
160+
let value = c.part()?;
161+
if assets.len() < asset_limit && c.retain_text(key, &mut remaining) {
162+
assets.push((key, value));
163+
} else {
164+
metadata_truncated = true;
121165
}
122166
}
167+
let exec_argv_count = if flags & FLAG_EXEC_ARGV != 0 { c.count()? } else { 0 };
123168
let mut exec_argv = Vec::new();
124-
if flags & FLAG_EXEC_ARGV != 0 {
125-
for _ in 0..c.count()? {
126-
exec_argv.push(c.part()?);
169+
for _ in 0..exec_argv_count {
170+
let argument = c.part()?;
171+
if exec_argv.len() < MAX_ENTRIES && c.retain_text(argument, &mut remaining) {
172+
exec_argv.push(argument);
173+
} else {
174+
metadata_truncated = true;
127175
}
128176
}
129177
let rest = blob.get(c.pos..)?;
130178
if rest.iter().any(|b| *b != 0) {
131179
return None;
132180
}
133-
Some(Parsed { layout, flags, extension, code_path, code, code_cache, assets, exec_argv, trailing_zeros: rest.len() })
181+
Some(Parsed {
182+
layout,
183+
flags,
184+
extension,
185+
code_path,
186+
code,
187+
code_cache,
188+
assets,
189+
asset_count,
190+
exec_argv,
191+
exec_argv_count,
192+
metadata_truncated,
193+
trailing_zeros: rest.len(),
194+
})
134195
}
135196

136197
fn parse_blob(blob: &[u8], base: usize) -> Result<Parsed, FormatError> {
@@ -223,6 +284,9 @@ impl PackedFormat for NodeSea {
223284
}
224285
let exec_argv: Vec<String> = p.exec_argv.iter().map(|a| text(data, *a)).collect();
225286
let mut notes = Vec::new();
287+
if p.metadata_truncated {
288+
notes.push("Some paths or arguments were omitted because the metadata listing reached its entry or decoded text byte limit.".to_string());
289+
}
226290
if snapshot {
227291
notes.push("The main program is a V8 startup snapshot, not source text; it's taken out as it is and isn't decoded.".to_string());
228292
}
@@ -247,11 +311,13 @@ impl PackedFormat for NodeSea {
247311
"uses_code_cache": p.code_cache.is_some(),
248312
"exec_argv_extension": p.extension,
249313
"code_path": code_path,
250-
"assets": p.assets.len(),
314+
"assets": p.asset_count,
315+
"assets_listed": p.assets.len().min(room),
316+
"exec_argv_count": p.exec_argv_count,
251317
"exec_argv": exec_argv,
252318
"fuse_says_injected": fuse_injected,
253319
});
254-
Ok(PackedListing { facts, truncated: p.assets.len() > room, entries, notes })
320+
Ok(PackedListing { facts, truncated: p.metadata_truncated || p.assets.len() > room, entries, notes })
255321
}
256322
}
257323

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
use craft_anything_formats::FormatError;
2+
use craft_anything_formats::node_sea::NodeSea;
3+
use craft_anything_formats::packed::{MAX_ENTRIES, PackedFormat};
4+
5+
const TEXT_BUDGET: usize = 8 * 1024 * 1024;
6+
7+
fn part(blob: &mut Vec<u8>, bytes: &[u8]) {
8+
blob.extend((bytes.len() as u64).to_le_bytes());
9+
blob.extend(bytes);
10+
}
11+
12+
fn blob(path: &[u8], assets: &[(&[u8], &[u8])], arguments: &[&[u8]]) -> Vec<u8> {
13+
let mut bytes = vec![0x20, 0xda, 0x43, 0x01, 0x18, 0, 0, 0, 1];
14+
part(&mut bytes, path);
15+
part(&mut bytes, b"code");
16+
bytes.extend((assets.len() as u64).to_le_bytes());
17+
for (key, value) in assets {
18+
part(&mut bytes, key);
19+
part(&mut bytes, value);
20+
}
21+
bytes.extend((arguments.len() as u64).to_le_bytes());
22+
for argument in arguments {
23+
part(&mut bytes, argument);
24+
}
25+
bytes
26+
}
27+
28+
#[test]
29+
fn asset_retention_includes_the_main_entry_in_its_limit() {
30+
let assets = vec![(b"a".as_slice(), b"v".as_slice()); MAX_ENTRIES];
31+
let bytes = blob(b"main.js", &assets, &[b"--ok"]);
32+
let listing = NodeSea.list(&bytes).unwrap();
33+
assert_eq!(listing.entries.len(), MAX_ENTRIES);
34+
assert_eq!(listing.facts["assets"], MAX_ENTRIES);
35+
assert_eq!(listing.facts["assets_listed"], MAX_ENTRIES - 1);
36+
assert_eq!(listing.facts["exec_argv"][0], "--ok");
37+
assert!(listing.truncated);
38+
assert!(listing.notes.iter().any(|note| note.contains("metadata listing")));
39+
}
40+
41+
#[test]
42+
fn argument_retention_is_bounded_even_for_empty_strings() {
43+
let mut arguments = vec![b"".as_slice(); MAX_ENTRIES + 1];
44+
let ambiguous = blob(b"main.js", &[], &arguments);
45+
assert!(matches!(NodeSea.list(&ambiguous), Err(FormatError::Unsupported(message)) if message.contains("more than one")));
46+
// A nonempty final argument keeps legacy layouts from accepting the empty records as padding.
47+
arguments[MAX_ENTRIES] = b"--end";
48+
let bytes = blob(b"main.js", &[], &arguments);
49+
let listing = NodeSea.list(&bytes).unwrap();
50+
assert_eq!(listing.facts["exec_argv"].as_array().unwrap().len(), MAX_ENTRIES);
51+
assert_eq!(listing.facts["exec_argv_count"], MAX_ENTRIES + 1);
52+
assert!(listing.truncated);
53+
}
54+
55+
#[test]
56+
fn skipped_fields_still_have_to_fit_the_complete_blob() {
57+
let arguments = vec![b"x".as_slice(); MAX_ENTRIES + 1];
58+
let mut bytes = blob(b"main.js", &[], &arguments);
59+
bytes.pop();
60+
assert!(NodeSea.list(&bytes).is_err());
61+
62+
let assets = vec![(b"a".as_slice(), b"v".as_slice()); MAX_ENTRIES];
63+
let mut bytes = blob(b"main.js", &assets, &[]);
64+
bytes.push(1);
65+
assert!(NodeSea.list(&bytes).is_err());
66+
}
67+
68+
#[test]
69+
fn oversized_keys_are_omitted_whole_and_do_not_hide_later_assets() {
70+
let key = vec![b'x'; TEXT_BUDGET];
71+
let bytes = blob(b"main.js", &[(&key, b"large"), (b"ok", b"small")], &[b"--ok"]);
72+
let listing = NodeSea.list(&bytes).unwrap();
73+
assert_eq!(listing.entries.len(), 2);
74+
assert_eq!(listing.entries[1].path, "assets/ok");
75+
assert_eq!(NodeSea.read(&bytes, &listing.entries[1], 100).unwrap(), b"small");
76+
assert_eq!(listing.facts["assets"], 2);
77+
assert_eq!(listing.facts["assets_listed"], 1);
78+
assert_eq!(listing.facts["exec_argv"][0], "--ok");
79+
assert!(listing.truncated);
80+
}
81+
82+
#[test]
83+
fn replacement_characters_spend_the_decoded_byte_budget() {
84+
let key = vec![0xff; TEXT_BUDGET / 3 + 1];
85+
let bytes = blob(b"main.js", &[(&key, b"v")], &[b"--ok"]);
86+
let listing = NodeSea.list(&bytes).unwrap();
87+
assert_eq!(listing.entries.len(), 1);
88+
assert_eq!(listing.facts["assets"], 1);
89+
assert_eq!(listing.facts["assets_listed"], 0);
90+
assert_eq!(listing.facts["exec_argv"][0], "--ok");
91+
assert!(listing.truncated);
92+
}
93+
94+
#[test]
95+
fn oversized_main_paths_use_the_existing_fallback_name() {
96+
let path = vec![b'x'; TEXT_BUDGET + 1];
97+
let bytes = blob(&path, &[(b"ok", b"v")], &[b"--ok"]);
98+
let listing = NodeSea.list(&bytes).unwrap();
99+
assert_eq!(listing.entries[0].path, "main/main.js");
100+
assert!(listing.facts["code_path"].is_null());
101+
assert_eq!(listing.entries[1].path, "assets/ok");
102+
assert!(listing.truncated);
103+
}
104+
105+
#[test]
106+
fn ordinary_metadata_is_preserved_without_truncation() {
107+
let bytes = blob(b"/work/main.js", &[(b"name", b"value")], &[b"--no-warnings", b"--stack-size=512"]);
108+
let listing = NodeSea.list(&bytes).unwrap();
109+
assert_eq!(listing.entries[0].path, "main/main.js");
110+
assert_eq!(listing.entries[1].path, "assets/name");
111+
assert_eq!(listing.facts["code_path"], "/work/main.js");
112+
assert_eq!(listing.facts["assets"], 1);
113+
assert_eq!(listing.facts["assets_listed"], 1);
114+
assert_eq!(listing.facts["exec_argv_count"], 2);
115+
assert_eq!(listing.facts["exec_argv"][1], "--stack-size=512");
116+
assert!(!listing.truncated);
117+
}

‎docs/ROADMAP.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1374,6 +1374,9 @@ Owner: open · Status: in progress
13741374
— claimed: Afloat16, branch bugfix/r3.12-asar-limit-bounds-20261107
13751375
- [x] Keep .NET bundle member reads before the manifest.
13761376
— claimed: Afloat16, branch bugfix/r3.12-dotnet-data-bounds-20261107
1377+
- [x] Bound retained Node SEA asset and argument metadata while still validating the complete blob layout.
1378+
— claimed: Afloat16, branch bugfix/r3.12-node-sea-retained-budget-20261109
1379+
Source `ce34c2446499`; [format checks](https://github.com/Afloat16/claude-mem/actions/runs/42642329977/job/129246378958) and [compatibility gates](https://github.com/Afloat16/claude-mem/actions/runs/42642329977/job/129246378958) passed.
13771380
- [x] Check the decoded byte count for packed files whose manifest declares their original size.
13781381
— claimed: Afloat16, branch bugfix/r3.12-packed-member-size
13791382
- [x] Decode all frames of concatenated Zstandard members and account for their combined output.

‎docs/STATUS.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ workspace clippy, layers, fmt and the secret scan of all history were clean. Chi
126126
| JVM method descriptor class names | Parameter and return types keep legal parentheses inside internal class names. | Class-file parser regression and an independent OpenJDK 17 MethodType check. |
127127
| Java class usage | `java_class_users_find` includes the component class of multidimensional object arrays; primitive-array descriptors do not match an unrelated class with the same descriptor letter | Synthetic class files parsed by the built-in reader: one- and two-dimensional allocations, class names beginning with `L`, primitive arrays, hit limits and method filters; formats tests. This correction was not checked with javac output or a JVM |
128128
| Keyed-archive reference graphs | Object rows expose edges from arrays and UID aliases. Projection follows inline dictionaries, arrays and aliases, including cycles. Repeated scalar references spend a shared byte budget. | Binary/XML fixture and nested object, alias and cycle regressions. |
129+
| Node SEA metadata budgets | Asset and argument metadata obey entry caps and a shared 8 MiB decoded-text budget. Declared counts and truncation remain visible, and omitted fields still undergo complete layout validation. | Synthetic `node_sea_metadata_budget` regressions (7 tests): Entry limits including empty arguments, oversized paths/keys, invalid UTF-8 expansion, malformed bytes after the retained prefix and ordinary metadata. [Format checks](https://github.com/Afloat16/claude-mem/actions/runs/42642329977/job/129246378958) and [compatibility gates](https://github.com/Afloat16/claude-mem/actions/runs/42642329977/job/129246378958) passed at `ce34c2446499`. |
129130
| Single-file apps and game files | `core_packed_files_list` and `core_packed_files_unpack` read Bun and Node.js single-file programs, .NET single-file apps, PyInstaller and Godot packs through one seam (`formats::packed`); `formats_bytecode_show` reads Python bytecode, `game_unity_code_names_show` Unity IL2CPP metadata, `formats_ai_weights_show` GGUF and safetensors headers. `app_folder_explain` says what an install folder is made with and what to run next | Unit tests with damaged files. Real files: a 279 MB Bun program, Node blobs, PyInstaller programs (Python 3.9, 3.14), .NET 8 single-file apps (macOS, Linux, Windows, compressed), Godot 3.6, 4.3 and 4.5 packs, 214 Python modules against CPython's own reader, a GGUF and a safetensors file, about 25 installed Mac apps. IL2CPP on synthetic files only |
130131
| IL2CPP method owners | Names and type membership are shown only when method records agree with the types that claim them. Conflicting ownership falls back to header information. | Declaring-type and overlapping-range fixtures across each supported layout. |
131132
| Source map lookup on long lines | Binary-search sorted section starts and segment columns. A lookup examines only the matching positions, while preserving duplicates and unmapped boundaries. | Regression cases added: A repeated lookup fixture and equal-column and unmapped boundary cases. |

0 commit comments

Comments
 (0)