Skip to content

Choosing a folder by file:// link doubles the Windows drive letter and throws on a stray percent #816

Closed

Description

What happens

src/server/routes/folder-resolve.ts expands the path the client sends to POST /folder/resolve/validate:

function expandPath(input: unknown): string {
  const raw = String(input ?? "").trim()
  if (!raw) return ""
  const withoutFileUrl = raw.startsWith("file://") ? decodeURIComponent(new URL(raw).pathname) : raw
  if (withoutFileUrl === "~") return HOME
  if (withoutFileUrl.startsWith("~/")) return path.join(HOME, withoutFileUrl.slice(2))
  return path.resolve(withoutFileUrl)
}

Two defects, both on the file:// branch.

1. The drive letter is doubled on Windows. Per the URL spec, pathname for a drive URL keeps the leading slash:

new URL("file:///C:/Users/me/Desktop").pathname  ->  "/C:/Users/me/Desktop"

path.resolve("/C:/Users/me/Desktop") on Windows then prepends the current drive, producing C:\C:\Users\me\Desktop — a path that cannot exist. The route then answers path not found for a folder that is really there.

2. A stray % throws. decodeURIComponent throws URIError on a malformed escape such as a%zz, and nothing catches it, so the request fails instead of reporting a path that does not exist.

Why it is wrong

Measured on this Windows machine with the original expression:

"file:///C:/Users/me/Desktop"  -> "C:\\C:\\Users\\me\\Desktop"     doubled drive
"file:///home/me/data"         -> "C:\\home\\me\\data"             POSIX path treated as relative
"C:/Users/me/Desktop"          -> "C:\\Users\\me\\Desktop"         correct (plain path)
"file:///C:/work/100%25"       -> "C:\\C:\\work\\100%"             doubled drive, decoded
"file:///C:/work/a%zz"         -> THROWS URIError: URI error

A plain path works and a file:// link does not, which is the confusing part: the same folder is selectable one way and not the other. This is the route behind "choose a folder", so it is the path a user takes when the browser hands over a file:// link.

The POSIX line is the same bug in mirror image: on a POSIX host file:///home/me/data is correct, so this only ever surfaced on Windows.

Repro

expandPath("file:///C:/Users/me/Desktop")
// "C:\C:\Users\me\Desktop", expected "C:\Users\me\Desktop"
expandPath("file:///C:/work/a%zz")
// throws URIError

expandPath was module-private and had no test, so this exports it.

Suggested fix

function fromFileUrl(raw: string): string {
  const pathname = new URL(raw).pathname
  const decoded = pathname.replace(/%[0-9a-f]{2}/gi, (escape) => {
    try { return decodeURIComponent(escape) } catch { return escape }
  })
  return /^\/[a-z]:[\\/]/i.test(decoded) ? decoded.slice(1) : decoded
}

Bun 1.3.14 has no URL.filePath (verified: it is undefined), so the drive slash is stripped explicitly. Decoding escape-by-escape rather than in one decodeURIComponent call means one malformed percent cannot discard the rest of an otherwise valid path.

Note on the SDK

AGENTS.md asks for ./tooling/repo/generate.ts after a src/server change. It was run here: it touched 31 files but git diff --stat reports 0 lines for openapi.json and the diff is empty — only line endings. No route, parameter, or response field changed, so the generated client is unchanged and was reverted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions