Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v18.18.2 -> v18.19.0, v20.5.1 -> v20.6.0 regression: pathToFileURL trims whitespace. #51167

Open
jgerigmeyer opened this issue Dec 15, 2023 · 3 comments
Labels
confirmed-bug Issues with confirmed bugs. url Issues and PRs related to the legacy built-in url module. whatwg-url Issues and PRs related to the WHATWG URL implementation.

Comments

@jgerigmeyer
Copy link

jgerigmeyer commented Dec 15, 2023

Version

v18.19.0, v20.6.0

Platform

Darwin Kernel Version 23.2.0: Wed Nov 15 21:53:18 PST 2023; root:xnu-10002.61.3~2/RELEASE_ARM64_T6000 arm64

Subsystem

url

What steps will reproduce the bug?

import {pathToFileURL} from 'url';

const filename = `test-${String.fromCharCode(0)}`;
console.log(pathToFileURL(filename).toString()); // file:///[...]/test-

How often does it reproduce? Is there a required condition?

always

What is the expected behavior? Why is that the expected behavior?

In Node v18.18.2 and v20.5.1:

import {pathToFileURL} from 'url';

const filename = `test-${String.fromCharCode(0)}`;
console.log(pathToFileURL(filename).toString()); // file:///[...]/test-%00

What do you see instead?

Where prior versions retained the trailing whitespace and returned a pathname ending in /test-%00, starting in v18.19.0 and v20.6.0 the trailing whitespace is removed and results in a pathname ending in /test-.

Additional information

This regression was introduced by optimizations in #48709, specifically the switch from:

const outURL = new URL('file://');
outURL.pathname = encodePathChars(resolved);
return outURL;

To:

resolved = encodePathChars(resolved);
return new URL(`file://${resolved}`);

While these seem equivalent, new URL uses parse, which trims leading and trailing whitespace from the path. In contrast, the earlier method of setting pathname directly does not trim whitespace.

I'm not sure which way is actually preferred, but regardless this was an unexpected (and undocumented) breaking change.

@anonrig
Copy link
Member

anonrig commented Dec 17, 2023

cc @nodejs/url

@anonrig anonrig added confirmed-bug Issues with confirmed bugs. whatwg-url Issues and PRs related to the WHATWG URL implementation. url Issues and PRs related to the legacy built-in url module. labels Dec 19, 2023
@EarlyRiser42
Copy link
Contributor

Hello,
I am interested in contributing to Node.js and would like to work on my first issue. However, I am a bit confused because I thought that most file systems and operating systems do not allow null characters in file names. I have configured that this issue still occurs in Node.js version 22.5.1. If this is indeed a problem that needs to be addressed, may I work on it?

@avivkeller
Copy link
Member

You can work on any issue you'd like. If you have a suggested improvement to any part of the code base, feel free to submit a PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug Issues with confirmed bugs. url Issues and PRs related to the legacy built-in url module. whatwg-url Issues and PRs related to the WHATWG URL implementation.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants