Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript

fix(desktop): correct read-budget citation and cap zip entry names (review)

M313c46466a61
diff --git a/desktop-app/ui/src/lib/__tests__/gfsFolderZip.test.ts b/desktop-app/ui/src/lib/__tests__/gfsFolderZip.test.ts
index 73bd9bcd7..8c4cde36f 100644
--- a/desktop-app/ui/src/lib/__tests__/gfsFolderZip.test.ts
+++ b/desktop-app/ui/src/lib/__tests__/gfsFolderZip.test.ts
@@ -438,6 +438,54 @@ describe('createGfsFolderZip', () => {
expect(result.fileName).toBe('Weird_Name_.zip')
})
+ it('caps entry-name length so the 16-bit ZIP name fields cannot truncate (L4)', async () => {
+ // 70 000 ASCII bytes would overflow ZIP's uint16 name length and corrupt
+ // the archive; the segment must be cut under the documented ceiling.
+ const overlong = `${'a'.repeat(70_000)}.txt`
+ const deps = depsFor({
+ root: [
+ {
+ items: [folder({ resourceId: 'huge', name: overlong })],
+ nextCursor: null,
+ },
+ ],
+ })
+ const result = await createGfsFolderZip(
+ { resourceId: 'root', drive: 'main', name: 'Long' },
+ { deps }
+ )
+
+ expect(result.fileCount).toBe(1)
+ const names = zipEntryNames(result.bytes)
+ expect(names).toHaveLength(1)
+ const written = names[0]!
+ // The archive parses back (zipEntryNames reads the real central
+ // directory); the written name is well inside the uint16 field and the
+ // SEGMENT was cut under the documented ceiling.
+ expect(written.startsWith('Long/')).toBe(true)
+ expect(new TextEncoder().encode(written).length).toBeLessThan(65536)
+ expect(new TextEncoder().encode(written.slice('Long/'.length)).length).toBeLessThanOrEqual(1024)
+ // Truncation keeps whole code points: a multi-byte character near the cut
+ // is never split.
+ const emojiDeps = depsFor({
+ root: [
+ {
+ items: [folder({ resourceId: 'emoji', name: `${'πŸ˜€'.repeat(600)}x` })],
+ nextCursor: null,
+ },
+ ],
+ })
+ const emojiResult = await createGfsFolderZip(
+ { resourceId: 'root', drive: 'main', name: 'Emoji' },
+ { deps: emojiDeps }
+ )
+ const emojiName = zipEntryNames(emojiResult.bytes)[0]!
+ expect(emojiName.endsWith('\uFFFD')).toBe(false)
+ expect(new TextEncoder().encode(emojiName.slice('Emoji/'.length)).length).toBeLessThanOrEqual(
+ 1024
+ )
+ })
+
it('treats a missing byte count as zero instead of NaN-poisoning the size guard (M1)', async () => {
const deps = depsFor({
root: [
diff --git a/desktop-app/ui/src/lib/gfsFolderZip.ts b/desktop-app/ui/src/lib/gfsFolderZip.ts
index 3b30d2190..0bf2e5f64 100644
--- a/desktop-app/ui/src/lib/gfsFolderZip.ts
+++ b/desktop-app/ui/src/lib/gfsFolderZip.ts
@@ -2,10 +2,14 @@
* BUG-175 β€” recursive "Download as zip" for a GFS folder, assembled fully in
* the renderer from the existing `window.clerum.gfs` read plane.
*
- * Budget contract: the server meters GFS reads per actor (~120 requests/min
- * shared with the browsing surface), so every listing and download below goes
- * through `createGfsReadThrottle`, which spaces requests to that budget, and a
- * 429 answer backs off once by the server-provided `retryAfterSeconds` before
+ * Budget contract: the producer meters this walk's request classes per actor β€”
+ * resource reads (children, affordances, resolve) and proxy reads (downloads)
+ * β€” at 480/min each by default (control-api `externalGfsResourceReadRlPerMin`
+ * / `externalGfsProxyReadRlPerMin`). Every listing and download below goes
+ * through `createGfsReadThrottle`, deliberately spaced at a CONSERVATIVE
+ * fraction of those ceilings (see `GFS_ZIP_READS_PER_MINUTE`) so one walk
+ * leaves room for the browsing surfaces sharing the same classes, and a 429
+ * answer backs off once by the server-provided `retryAfterSeconds` before
* failing. Byte and entry ceilings bound the walk before anything large is
* buffered, and entries the caller cannot read (a listing row marked
* unreadable, or a 403 on the download itself) are skipped and reported back
@@ -15,7 +19,12 @@
import { isRateLimited, parseHttpStatus, parseRetryAfterSeconds } from '@lib/gfsGrantErrors'
import { createZipWriter } from '@lib/zipWriter'
-/** Server-side GFS read budget the walk must respect (requests per minute). */
+/**
+ * Client-side pacing for the walk, a deliberate conservative margin: the
+ * producer's default ceilings for the classes used here are 480/min each
+ * (resource reads, proxy reads); spacing at 120/min keeps one walk well under
+ * them while the Files browsing surface shares the same classes.
+ */
export const GFS_ZIP_READS_PER_MINUTE = 120
/** Refuse before buffering: total uncompressed bytes across the folder. */
export const GFS_ZIP_MAX_TOTAL_BYTES = 1024 * 1024 * 1024
@@ -148,18 +157,46 @@ function isAccessDenied(message: string): boolean {
return parseHttpStatus(message) === 403 || message.toLowerCase().includes('forbidden')
}
+/**
+ * Zip entry-name length ceiling per segment. ZIP name fields are 16-bit byte
+ * counts; a name whose UTF-8 encoding reaches 65 536 bytes would silently
+ * truncate there and corrupt the archive, so segments are cut well under it.
+ */
+export const GFS_ZIP_MAX_SEGMENT_NAME_BYTES = 1024
+
+const segmentNameEncoder = new TextEncoder()
+
+/** Truncates by code point so the UTF-8 encoding stays `maxBytes` or less. */
+function truncateToUtf8Bytes(name: string, maxBytes: number): string {
+ if (segmentNameEncoder.encode(name).length <= maxBytes) return name
+ let kept = ''
+ let used = 0
+ for (const character of name) {
+ const size = segmentNameEncoder.encode(character).length
+ if (used + size > maxBytes) break
+ kept += character
+ used += size
+ }
+ return kept
+}
+
/**
* Zip-safe path segment. Separators, NUL and the characters Windows forbids
* (`: * ? " < > |`) map to `_`; trailing dots/spaces (also invalid on Windows)
* are stripped; only the EXACT `.`/`..` segments are dropped, so dotfiles like
- * `.env` keep their name. Traversal is impossible regardless: separators are
- * gone before the segment is used.
+ * `.env` keep their name; and the UTF-8 encoding is capped at
+ * `GFS_ZIP_MAX_SEGMENT_NAME_BYTES` so the 16-bit ZIP name fields can never
+ * truncate. Traversal is impossible regardless: separators are gone before the
+ * segment is used.
*/
function sanitizeZipSegment(name: string): string {
- const cleaned = name
- .replace(/[/\\:*?"<>|\u0000]/g, '_')
- .replace(/[\s.]+$/, '')
- .trim()
+ const cleaned = truncateToUtf8Bytes(
+ name
+ .replace(/[/\\:*?"<>|\u0000]/g, '_')
+ .replace(/[\s.]+$/, '')
+ .trim(),
+ GFS_ZIP_MAX_SEGMENT_NAME_BYTES
+ )
if (!cleaned || cleaned === '.' || cleaned === '..') return 'unnamed'
return cleaned
}