Skip to content

Fix directory-index links using native path.join instead of path.posix.join - #856

Open
Tech Guy (lukiod) wants to merge 1 commit into
microsoft:mainfrom
lukiod:fix-directory-index-backslash-encoding
Open

Fix directory-index links using native path.join instead of path.posix.join#856
Tech Guy (lukiod) wants to merge 1 commit into
microsoft:mainfrom
lukiod:fix-directory-index-backslash-encoding

Conversation

@lukiod

Copy link
Copy Markdown

Fixes #855 (and the same root cause as #762, #795).

The bug

createIndexPage() builds each directory-listing link with:

const relativeFileWithChild = path.join(relativePath, childFile);

path.join uses the host OS's native path separator. relativePath here is always a URL path (it comes from the decoded request URL, not a filesystem path), but on Windows path.join still returns \-separated output — e.g. path.join('/apps', 'folder')\apps\folder. That string is then passed through encodeURI(), which percent-encodes the backslashes, producing exactly the broken links reported: %5Capps%5Cfolder.

The fix

Use path.posix.join for that one join — it always returns /-separated output regardless of host OS, which is what a URL path segment actually needs. The sibling absolutePath join on the next line is untouched, since that one is a real filesystem path and correctly needs native separators.

Verification

Exact reproduction of the reported bug, using Node's own platform-specific path modules directly (no Windows machine needed — path.win32/path.posix are both available cross-platform for exactly this):

$ node -e "
const path = require('path');
console.log(encodeURI(path.win32.join('/apps', 'folder')));   // what runs on Windows today
console.log(encodeURI(path.posix.join('/apps', 'folder')));   // the fix
"
%5Capps%5Cfolder
/apps/folder

The first line matches the issue's reported output byte-for-byte.

Added a regression test (src/test/suite/contentLoader.test.ts) following this repo's own existing pattern exactly (ContentLoader construction + fsReadDir/PathUtil.FileExistsStat stubbing, same as manager.test.ts/preview.test.ts), asserting the generated index page contains working /apps/folder/-style links and no %5C.

What I could and couldn't run in my environment: npm run compile (tsc -p ./) succeeds clean, and eslint on both changed files is clean. I could not run the full suite (xvfb-run -a npm test, per your CI config) — no display server available in my environment and no way to install one without root. I want to be upfront about that rather than claim a green test run I didn't get, but wanted to flag that the standalone reproduction above uses the exact same path module logic the fix and test exercise, so I'm confident in the fix's correctness even without the full Electron-hosted suite run.

createIndexPage() builds each entry's href with
path.join(relativePath, childFile), then encodeURI()'s the result.
path.join uses the host OS's native separator, so on Windows it
returns e.g. "\apps\folder" instead of "/apps/folder" -- encodeURI
then turns the backslashes into %5C, producing broken links like
%5Capps%5Cfolder (microsoft#855, microsoft#762, microsoft#795).

relativePath is always a URL path (it comes from the decoded request
URL, not a filesystem path), so it should use posix separators
regardless of host OS. Switched that one join to path.posix.join;
the sibling absolutePath join on the next line is unchanged since
that one is a real filesystem path and needs native separators.

Added a regression test following the existing suite's own pattern
(ContentLoader + fsReadDir/PathUtil stubbing, as in manager.test.ts).
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.

Live Preview generates incorrect directory index URLs using encoded backslashes (%5Cfolder%5Cpath)

1 participant