From 16c7e5e08d9a8aa4ba9b5f68374072c731d07462 Mon Sep 17 00:00:00 2001 From: zerone0x <39543393+zerone0x@users.noreply.github.com> Date: Wed, 18 Nov 2026 20:13:54 +0000 Subject: [PATCH] fix(remote): drop a remote artifact whose name cannot be a file name A daemon response naming a non-script artifact with a NUL byte in its fileName (or localPath) crashed the client in the download's first fs call. The client now drops that artifact before any filesystem work, records a typed droppedArtifacts entry (reason artifact_name_invalid) and a warning, and keeps the rest of the response, including the action's own outcome. Fixes #4661 --- .../daemon-artifacts-capture.test.ts | 53 ++++++++++ packages/remote/src/daemon-artifacts.ts | 99 ++++++++++++++----- 2 files changed, 129 insertions(+), 23 deletions(-) diff --git a/packages/remote/src/__tests__/daemon-artifacts-capture.test.ts b/packages/remote/src/__tests__/daemon-artifacts-capture.test.ts index e8971e6356..87168de24b 100644 --- a/packages/remote/src/__tests__/daemon-artifacts-capture.test.ts +++ b/packages/remote/src/__tests__/daemon-artifacts-capture.test.ts @@ -172,3 +172,56 @@ test.for([ await closeLoopbackServer(server); } }); + +test('a remote artifact named with a NUL byte is dropped and the action result is kept', async (t) => { + if (await skipWhenLoopbackUnavailable(t, 'remote artifact download')) return; + const root = mkdtempForTestSync('agent-device-remote-artifact-nul-'); + const cwd = path.join(root, 'cwd'); + fs.mkdirSync(cwd); + const requested: string[] = []; + const server = http.createServer((req, res) => { + requested.push(req.url ?? ''); + res.end('artifact-bytes'); + }); + try { + const port = await listenOnLoopback(server); + const response = await materializeRemoteArtifacts( + { baseUrl: `http://127.0.0.1:${port}`, token: 'secret' }, + { token: 'secret', command: 'snapshot', session: 'capture', positionals: [], meta: { cwd } }, + { + ok: true, + data: { + nodes: 3, + path: '/daemon/x', + artifacts: [ + { + artifactId: 'nul', + field: 'path', + artifactType: 'screenshot', + path: '/daemon/x', + fileName: 'shot\0.png', + }, + ], + }, + }, + createTestHostEnvironment(), + path.join(root, 'state'), + ); + expect(response).toEqual({ + ok: true, + data: { + nodes: 3, + path: '/daemon/x', + artifacts: [], + droppedArtifacts: [{ field: 'path', reason: 'artifact_name_invalid' }], + warnings: [ + 'The remote artifact for "path" was not downloaded: its name cannot be a file name.', + ], + }, + }); + expect(requested).toEqual([]); + expect(fs.readdirSync(cwd)).toEqual([]); + } finally { + await closeLoopbackServer(server); + } +}); diff --git a/packages/remote/src/daemon-artifacts.ts b/packages/remote/src/daemon-artifacts.ts index 50a60d8629..5bd362de3f 100644 --- a/packages/remote/src/daemon-artifacts.ts +++ b/packages/remote/src/daemon-artifacts.ts @@ -530,6 +530,7 @@ async function downloadResponseArtifacts( if (artifacts.length === 0) return data === response.data ? response : { ok: true, data }; const nextData: Record = { ...(data ?? {}) }; const nextArtifacts: DaemonArtifact[] = []; + const dropped: DroppedArtifact[] = []; for (const artifact of artifacts) { if (isSessionScriptArtifact(artifact)) { await scripts?.publish(artifact, nextData, nextArtifacts); @@ -539,32 +540,52 @@ async function downloadResponseArtifacts( nextArtifacts.push(artifact); continue; } - if (typeof artifact.field !== 'string' || isClientOwnedResponseField(artifact.field)) continue; - const downloadPath = resolveMaterializedArtifactPath(artifact, req, host); - const materializedPath = await downloadRemoteArtifact({ - ...endpoint, - artifactId: artifact.artifactId, - destinationPath: downloadPath, - requestScope: req.meta, - // #2246: `test-artifacts` is the one directory-shaped artifact type today — known from the - // response, before any bytes arrive, so cleanup-on-error can be decided up front instead of - // racing the response headers (a directory destination must never be `rm`'d wholesale; see - // `downloadRemoteArtifact`). - isDirectory: artifact.artifactType === 'test-artifacts', - }); - // Directory artifacts download into a caller-owned root, then atomically publish their one - // top-level entry beneath it. Use the path the download actually published rather than the - // root hint sent over the wire; otherwise `artifactsDir` loses its suite invocation segment. - nextData[artifact.field] = materializedPath; - nextArtifacts.push({ - ...artifact, - localPath: materializedPath, - }); + await downloadOneArtifact(endpoint, req, host, artifact, { nextData, nextArtifacts, dropped }); } nextData.artifacts = nextArtifacts; + if (dropped.length > 0) discloseDroppedArtifacts(nextData, dropped); return { ok: true, data: nextData }; } +/** Downloads one non-script response artifact into the response being built, or drops it. */ +async function downloadOneArtifact( + endpoint: Required, + req: DaemonWireRequest, + host: HostEnvironment, + artifact: DaemonArtifact & { artifactId: string }, + into: { + nextData: Record; + nextArtifacts: DaemonArtifact[]; + dropped: DroppedArtifact[]; + }, +): Promise { + if (typeof artifact.field !== 'string' || isClientOwnedResponseField(artifact.field)) return; + const downloadPath = resolveMaterializedArtifactPath(artifact, req, host); + if (downloadPath === undefined) { + into.dropped.push({ field: artifact.field, reason: 'artifact_name_invalid' }); + return; + } + const materializedPath = await downloadRemoteArtifact({ + ...endpoint, + artifactId: artifact.artifactId, + destinationPath: downloadPath, + requestScope: req.meta, + // #2246: `test-artifacts` is the one directory-shaped artifact type today — known from the + // response, before any bytes arrive, so cleanup-on-error can be decided up front instead of + // racing the response headers (a directory destination must never be `rm`'d wholesale; see + // `downloadRemoteArtifact`). + isDirectory: artifact.artifactType === 'test-artifacts', + }); + // Directory artifacts download into a caller-owned root, then atomically publish their one + // top-level entry beneath it. Use the path the download actually published rather than the + // root hint sent over the wire; otherwise `artifactsDir` loses its suite invocation segment. + into.nextData[artifact.field] = materializedPath; + into.nextArtifacts.push({ + ...artifact, + localPath: materializedPath, + }); +} + function hasDownloadableArtifactId( artifact: DaemonArtifact | undefined, ): artifact is DaemonArtifact & { artifactId: string } { @@ -585,10 +606,29 @@ function isClientOwnedResponseField(field: string): boolean { field === 'artifacts' || field === 'savedScript' || field === 'scriptDeliveryFailure' || + field === 'droppedArtifacts' || field === 'warnings' ); } +/** A response artifact the client did not download, and why. */ +type DroppedArtifact = { field: string; reason: 'artifact_name_invalid' }; + +/** + * Adds the artifacts this client dropped, and one warning for each, to a response body. The action + * keeps its own outcome; only the dropped artifacts are missing from `artifacts`. + */ +function discloseDroppedArtifacts(data: Record, dropped: DroppedArtifact[]): void { + data.droppedArtifacts = dropped; + data.warnings = [ + ...(Array.isArray(data.warnings) ? data.warnings : []), + ...dropped.map( + ({ field }) => + `The remote artifact for ${JSON.stringify(field)} was not downloaded: its name cannot be a file name.`, + ), + ]; +} + // Only a successfully published version can name a caller's savedScript destination. function withoutScriptFieldOwner( data: DaemonResponseData | undefined, @@ -621,18 +661,31 @@ function finishesEarlierClientCapture(req: DaemonWireRequest): boolean { * Where this client writes one downloaded artifact. The client chooses: a path it registered in * this request wins, and a response `localPath` is accepted only on a stop or report that echoes an * earlier request's destination. Anything else lands in cwd under the response file name's basename. + * A destination that cannot be a file path (a NUL byte, a file name that is not a string) answers + * `undefined`, so it never reaches a filesystem call. */ function resolveMaterializedArtifactPath( artifact: DaemonArtifact, req: DaemonWireRequest, host: HostEnvironment, -): string { +): string | undefined { + const destination = chooseMaterializedArtifactPath(artifact, req, host); + return destination === undefined || destination.includes('\0') ? undefined : destination; +} + +function chooseMaterializedArtifactPath( + artifact: DaemonArtifact, + req: DaemonWireRequest, + host: HostEnvironment, +): string | undefined { const requestedPath = req.meta?.clientArtifactPaths?.[artifact.field]; if (hasNonEmptyString(requestedPath)) return requestedPath; if (hasNonEmptyString(artifact.localPath) && finishesEarlierClientCapture(req)) { return artifact.localPath; } - const responseName = path.basename(artifact.fileName?.trim() ?? ''); + const rawName: unknown = artifact.fileName ?? ''; + if (typeof rawName !== 'string') return undefined; + const responseName = path.basename(rawName.trim()); const fileName = responseName && responseName !== '.' && responseName !== '..' ? responseName