Skip to content

Fix: make artifact filename rooting host-OS independent in FileArtifactService - #760

Open
AmaadMartin wants to merge 3 commits into
mainfrom
fix/artifact-filename-host-independent-rooting
Open

Fix: make artifact filename rooting host-OS independent in FileArtifactService#760
AmaadMartin wants to merge 3 commits into
mainfrom
fix/artifact-filename-host-independent-rooting

Conversation

@AmaadMartin

@AmaadMartin AmaadMartin commented Aug 7, 2026

Copy link
Copy Markdown
Owner

Please ensure you have read the contribution guide before creating a pull request.

Link to Issue or Description of Change

  1. Link to an existing issue (if applicable):
    N/A
  2. Or, if no issue exists, describe the change:
    Problem: getArtifactDir rejects absolute artifact filenames with path.isAbsolute, which follows the host platform. On POSIX it reads C:\evil.txt and \evil.txt as ordinary relative names, so one filename is an error on Windows and a valid artifact on Linux. It also leaves backslashes intact, so sub\file.txt becomes a single directory whose name contains a backslash instead of nesting under sub/. An artifact store written on one operating system does not read back the same way on another.

Solution: Parse the filename with path.win32, which reports a non-empty root for a drive, the drive-relative C:, a UNC share, a leading backslash and a leading / on every platform. Rewrite backslashes to / before resolving, so sub\file.txt nests exactly like sub/file.txt and sub\..\..\escape.txt reaches the existing containment check. This mirrors PureWindowsPath(...).drive or .root in adk-python. The public API, the two error messages and the containment check are unchanged.

Notes:

Testing Plan

Please describe the tests that you ran to verify your changes. This is required for all PRs that are not small documentation or typo fixes.

Unit Tests:
[x] I have added or updated unit tests for my change.
[x] All unit tests pass locally.

Six tests were added to core/test/artifacts/file_artifact_service_test.ts. No existing test was changed.

npx vitest run --project unit:core core/test/artifacts/file_artifact_service_test.ts
  Test Files  1 passed (1)
       Tests  48 passed (48)
npm run lint          # exit 0
npx prettier --check core/src/artifacts/file_artifact_service.ts core/test/artifacts/file_artifact_service_test.ts   # clean
npm run ts:check      # 280 errors, all pre-existing; identical count on main, none in these two files

New lines reach 100% line and branch coverage, measured with
--coverage.include='core/src/artifacts/file_artifact_service.ts'.

Proof the tests can fail. I ran each new test against the unfixed code.

  1. Restore if (path.isAbsolute(cleanFilename)) — 3 tests fail. The two drive
    forms report promise resolved "+0" instead of rejecting. The
    \evil.txt case reports expected [Function] to throw error including 'Absolute artifact filename \evil.txt …' but got 'Artifact filename \evil.txt escapes s…'.
  2. Delete cleanFilename = cleanFilename.replaceAll('\\', '/'); — 2 tests fail.
    nests a filename that uses Windows separators reports promise rejected "Error: ENOENT: no such file or directory…" instead of resolving.
    rejects nested traversal expressed with Windows separators reports
    promise resolved "+0" instead of rejecting.
  3. rejects traversal expressed with Windows separators (..\escape.txt)
    survives both mutations. path.relative returns the literal name
    ..\escape.txt, which starts with .., so today the containment check
    rejects it by coincidence. I kept the test as a regression pin, because the
    coincidence disappears if the containment check becomes segment-wise.

CI passes on ubuntu-latest, windows-latest and macos-latest. The first two
runs failed on the known app_loader discovery timeout on macos-latest, which
#664 explains and fixes; fail-fast then cancelled the Windows leg. A rerun of
the same commit is green on all three.

Manual End-to-End (E2E) Tests:
Please provide instructions on how to manually test your changes, including any necessary setup or configuration.

Run this on Linux or macOS after npm install && npm run build:

import {FileArtifactService} from '@google/adk';
import * as fs from 'node:fs/promises';
import * as os from 'node:os';
import * as path from 'node:path';

const root = await fs.mkdtemp(path.join(os.tmpdir(), 'adk-e2e-'));
const service = new FileArtifactService(root);
const base = {appName: 'app', userId: 'u', sessionId: 's'};

// Before: succeeds on POSIX. After: throws.
await service.saveArtifact({
  ...base,
  filename: 'C:\\evil.txt',
  artifact: {text: 'x'},
});

// Before: one directory named "sub\file.txt". After: nests under sub/.
await service.saveArtifact({
  ...base,
  filename: 'sub\\file.txt',
  artifact: {text: 'hello'},
});

Observed on Node v22.22.2:

Absolute artifact filename C:\evil.txt is not permitted.
<root>/users/u/sessions/s/artifacts/sub/file.txt/versions/0/file.txt
<root>/users/u/sessions/s/artifacts/sub/file.txt/versions/0/metadata.json
loadArtifact('sub\file.txt') -> {text: 'hello'}
entries containing a backslash: 0

Checklist

[x] I have read the CONTRIBUTING.md document.
[x] I have performed a self-review of my own code.
[x] I have commented my code, particularly in hard-to-understand areas.
[x] I have added tests that prove my fix is effective or that my feature works.
[x] New and existing unit tests pass locally with my changes.

Amaad Martin added 3 commits August 7, 2026 00:29
getArtifactDir guarded absolute filenames with path.isAbsolute, which
follows the host platform. On POSIX it read 'C:\evil.txt' and
'\evil.txt' as ordinary relative names, and it left backslash
separators intact, so 'sub\file.txt' became one directory whose name
contains a backslash instead of nesting.

Parse the filename with path.win32 instead, which reports a root for a
drive, a UNC share, a leading backslash and a leading slash on every
platform, and rewrite backslashes to '/' before resolving. This matches
PureWindowsPath handling in adk-python.
Cover the Windows drive, drive-relative and root-relative forms, the two
traversal forms written with backslashes, and the nesting and round-trip
of 'sub\file.txt'.
replaceAll('\\', '/') is the idiom the repo already uses for this in
dev/src/conformance, and ES2022 is in the lib list, so the one-line
helper and its doc comment are not needed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant