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

esm: identify parent importing a url with invalid host #49736

Conversation

JakobJingleheimer
Copy link
Member

@JakobJingleheimer JakobJingleheimer commented Sep 20, 2023

New output:

TypeError [ERR_INVALID_FILE_URL_HOST]: File URL host must be "localhost" or empty on darwin
    at new NodeError (node:internal/errors:406:5)
    at getPathFromURLPosix (node:internal/url:1385:11)
    at fileURLToPath (node:internal/url:1408:50)
    at finalizeResolution (node:internal/modules/esm/resolve:210:16)
    …
  code: 'ERR_INVALID_FILE_URL_HOST',
  input: 'file://hmm.js/',
  module: 'file:///…/test.mjs'

fixes #49571

@nodejs-github-bot
Copy link
Collaborator

Review requested:

  • @nodejs/loaders
  • @nodejs/url

@nodejs-github-bot nodejs-github-bot added errors Issues and PRs related to JavaScript errors originated in Node.js core. esm Issues and PRs related to the ECMAScript Modules implementation. needs-ci PRs that need a full CI run. whatwg-url Issues and PRs related to the WHATWG URL implementation. labels Sep 20, 2023
lib/internal/url.js Outdated Show resolved Hide resolved
E('ERR_INVALID_FILE_URL_HOST',
'File URL host must be "localhost" or empty on %s', TypeError);
E('ERR_INVALID_FILE_URL_HOST', (url, platform, base) => {
let msg = `File URL host in ${url}`;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not pass the base and url to this just like in err_invalid_url?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at the comment within err_invalid_url that's probably a good idea!

lib/internal/errors.js Outdated Show resolved Hide resolved
Copy link
Member

@GeoffreyBooth GeoffreyBooth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we please add a test?

TypeError [ERR_INVALID_FILE_URL_HOST]: File URL host must be "localhost" or empty on darwin

On darwin? You mean the rules differ by platform?

@JakobJingleheimer
Copy link
Member Author

On darwin? You mean the rules differ by platform?

Yep. MacOS + Linux vs Windows.

@GeoffreyBooth
Copy link
Member

On darwin? You mean the rules differ by platform?

Yep. MacOS + Linux vs Windows.

So we should have tests for both versions of the error message. CI can validate the Windows one. We should also make the platform name friendly, like translate “darwin” to “macOS” and “win32” to “Windows”, and maybe everything else gets “POSIX platforms”. What is the rule for Windows?

@JakobJingleheimer
Copy link
Member Author

That sounds rather out of scope for the issue this is fixing. There is already a test for the underlying util that handles this. All I'm doing here is appending some properties to the error, not changing its message.

@GeoffreyBooth
Copy link
Member

That sounds rather out of scope for the issue this is fixing. There is already a test for the underlying util that handles this. All I’m doing here is appending some properties to the error, not changing its message.

Oh, I thought the message itself was the new part. I totally didn’t notice these extra properties. Why not put them into the message itself, in addition to where they are now?

@JakobJingleheimer
Copy link
Member Author

I did that at first, but it was rather dirty: The info was not available (so it involved a lot of daisy-chaining), and this augmentation isn't applicable to any other uses of the underlying util.

Also embedding the url in the message can leak data in logging.

@ljharb
Copy link
Member

ljharb commented Sep 21, 2023

Can there be a test to cover this change?

@JakobJingleheimer
Copy link
Member Author

JakobJingleheimer commented Sep 21, 2023

Can there be a test to cover this change?

Ordinarily I would've included one already, but this applies to only POSIX platforms, and I don't know how to mark a test-case as being applicable to only a certain platform.

@GeoffreyBooth
Copy link
Member

I don’t know how to mark a test-case as being applicable to only a certain platform.

Just put if (process.platform !== 'win32'), or whatever the comparison should be, within the test. Then it’s a no-op on unsupported platforms. There are examples of tests like this in the codebase already. https://nodejs.org/api/process.html#processplatform

@JakobJingleheimer JakobJingleheimer added request-ci Add this label to start a Jenkins CI on a PR. commit-queue Add this label to land a pull request using GitHub Actions. commit-queue-squash Add this label to instruct the Commit Queue to squash all the PR commits into the first one. labels Sep 22, 2023
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Sep 22, 2023
@nodejs-github-bot
Copy link
Collaborator

@nodejs-github-bot nodejs-github-bot added commit-queue-failed An error occurred while landing this pull request using GitHub Actions. and removed commit-queue Add this label to land a pull request using GitHub Actions. labels Sep 22, 2023
@nodejs-github-bot
Copy link
Collaborator

Commit Queue failed
- Loading data for nodejs/node/pull/49736
✔  Done loading data for nodejs/node/pull/49736
----------------------------------- PR info ------------------------------------
Title      esm: identify parent importing a url with invalid host (#49736)
   ⚠  Could not retrieve the email or name of the PR author's from user's GitHub profile!
Branch     JakobJingleheimer:fix/esm-specifier-invalid-url-host -> nodejs:main
Labels     errors, whatwg-url, esm, needs-ci, commit-queue-squash
Commits    3
 - esm: identify parent importing a url with invalid host
 - test: add case for invalid posix host
 - fixup!: de-lint
Committers 1
 - Jacob Smith <[email protected]>
PR-URL: https://github.com/nodejs/node/pull/49736
Reviewed-By: Antoine du Hamel 
Reviewed-By: Geoffrey Booth 
------------------------------ Generated metadata ------------------------------
PR-URL: https://github.com/nodejs/node/pull/49736
Reviewed-By: Antoine du Hamel 
Reviewed-By: Geoffrey Booth 
--------------------------------------------------------------------------------
   ⚠  Commits were pushed since the last approving review:
   ⚠  - test: add case for invalid posix host
   ⚠  - fixup!: de-lint
   ℹ  This PR was created on Wed, 20 Sep 2023 15:08:30 GMT
   ✔  Approvals: 2
   ✔  - Antoine du Hamel (@aduh95) (TSC): https://github.com/nodejs/node/pull/49736#pullrequestreview-1637644110
   ✔  - Geoffrey Booth (@GeoffreyBooth) (TSC): https://github.com/nodejs/node/pull/49736#pullrequestreview-1638329347
   ✘  Last GitHub CI failed
   ℹ  Last Full PR CI on 2023-09-22T18:42:45Z: https://ci.nodejs.org/job/node-test-pull-request/54154/
- Querying data for job/node-test-pull-request/54154/
   ✔  Last Jenkins CI successful
--------------------------------------------------------------------------------
   ✔  Aborted `git node land` session in /home/runner/work/node/node/.ncu
https://github.com/nodejs/node/actions/runs/6278312662

@nodejs-github-bot
Copy link
Collaborator

@JakobJingleheimer JakobJingleheimer added commit-queue Add this label to land a pull request using GitHub Actions. request-ci Add this label to start a Jenkins CI on a PR. and removed commit-queue-failed An error occurred while landing this pull request using GitHub Actions. labels Sep 22, 2023
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Sep 22, 2023
@nodejs-github-bot
Copy link
Collaborator

@aduh95 aduh95 removed the commit-queue Add this label to land a pull request using GitHub Actions. label Sep 22, 2023
@nodejs-github-bot
Copy link
Collaborator

@nodejs-github-bot
Copy link
Collaborator

@GeoffreyBooth GeoffreyBooth added the commit-queue Add this label to land a pull request using GitHub Actions. label Sep 23, 2023
@nodejs-github-bot nodejs-github-bot removed the commit-queue Add this label to land a pull request using GitHub Actions. label Sep 23, 2023
@nodejs-github-bot nodejs-github-bot merged commit 645b788 into nodejs:main Sep 23, 2023
48 checks passed
@nodejs-github-bot
Copy link
Collaborator

Landed in 645b788

ruyadorno pushed a commit that referenced this pull request Sep 28, 2023
PR-URL: #49736
Reviewed-By: Antoine du Hamel <[email protected]>
Reviewed-By: Geoffrey Booth <[email protected]>
This was referenced Sep 28, 2023
alexfernandez pushed a commit to alexfernandez/node that referenced this pull request Nov 1, 2023
PR-URL: nodejs#49736
Reviewed-By: Antoine du Hamel <[email protected]>
Reviewed-By: Geoffrey Booth <[email protected]>
targos pushed a commit to targos/node that referenced this pull request Nov 11, 2023
PR-URL: nodejs#49736
Reviewed-By: Antoine du Hamel <[email protected]>
Reviewed-By: Geoffrey Booth <[email protected]>
@targos targos added the backported-to-v18.x PRs backported to the v18.x-staging branch. label Nov 23, 2023
targos pushed a commit that referenced this pull request Nov 23, 2023
PR-URL: #49736
Backport-PR-URL: #50669
Reviewed-By: Antoine du Hamel <[email protected]>
Reviewed-By: Geoffrey Booth <[email protected]>
sercher added a commit to sercher/graaljs that referenced this pull request Apr 25, 2024
PR-URL: nodejs/node#49736
Backport-PR-URL: nodejs/node#50669
Reviewed-By: Antoine du Hamel <[email protected]>
Reviewed-By: Geoffrey Booth <[email protected]>
sercher added a commit to sercher/graaljs that referenced this pull request Apr 25, 2024
PR-URL: nodejs/node#49736
Backport-PR-URL: nodejs/node#50669
Reviewed-By: Antoine du Hamel <[email protected]>
Reviewed-By: Geoffrey Booth <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backported-to-v18.x PRs backported to the v18.x-staging branch. commit-queue-squash Add this label to instruct the Commit Queue to squash all the PR commits into the first one. errors Issues and PRs related to JavaScript errors originated in Node.js core. esm Issues and PRs related to the ECMAScript Modules implementation. needs-ci PRs that need a full CI run. whatwg-url Issues and PRs related to the WHATWG URL implementation.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Invalid URLs in static imports do not produce a helpful error message
8 participants