The eval viewer's safeJoin allows a sibling directory sharing the runs prefix

Open Beginner friendly
#3,761 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
72/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
javascript, nodejs
Domain
backend, security

Research direction

The bug is in safeJoin in packages/hdx-eval/scripts/viewer/server.js (around line 39), which uses a bare startsWith check on the resolved root. Reproduce by creating a sibling runs-other directory and requesting /api/batches/..%2Fruns-other/runs/a/b/c/1 against the run route near line 176. Done when the check compares against the root with a path-separator boundary (or uses path.relative) and the sibling request returns an error instead of the file.

Written by the indexing model from the issue text.

Description

external

The eval viewer's safeJoin allows a sibling directory sharing the runs prefix

Summary

The eval viewer's safeJoin checks only startsWith(path.resolve(root)). A sibling directory whose name begins with the runs root name passes the check. A crafted request can therefore read a JSON run artifact from outside the configured runs directory.

Location

  • Repository: hyperdxio/hyperdx
  • File: packages/hdx-eval/scripts/viewer/server.js
  • Line: 39 (safeJoin; the run route is at 176)
  • Revision: 1773c11942a7

Affected code

function safeJoin(root, ...parts) {
  const p = path.resolve(root, ...parts);
  if (!p.startsWith(path.resolve(root))) throw new Error('path escapes root');
  return p;
}

The prefix check has no path-separator boundary.

Expected behavior

A request may only read files under the configured runs directory. A path resolving to a sibling such as runs-other/... must be rejected.

Actual behavior

Create a sibling runs-other/a/b/c/1.json next to runs, then run the viewer and request:

/api/batches/..%2Fruns-other/runs/a/b/c/1

The server returns the sibling file:

200 {"trajectory":{"secret":"outside"},"grade":null}

The file is outside packages/hdx-eval/runs.

Root cause

path.resolve(root, '../runs-other/...') produces a path that still starts with the root string (.../runs-other starts with .../runs). startsWith therefore accepts it.

Suggested fix

Append a path separator to the root before comparing, or use path.relative and reject results starting with .. or absolute paths:

const base = path.resolve(root);
const p = path.resolve(base, ...parts);
const rel = path.relative(base, p);
if (rel.startsWith('..') || path.isAbsolute(rel)) throw new Error('path escapes root');
return p;
Dominant language
TypeScript
Stars
10.1k
Forks
527
Avg merge
1d 20h
Merged PRs (30d)
186

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from ClickHouse/ClickStack

All issues in ClickHouse/ClickStack

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.