question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

inconsistent application of `encodeUri`

See original GitHub issue

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Fastify version

3.21.1

Plugin version

4.2.3

Node.js version

14.17.6

Operating system

macOS

Operating system version (i.e. 20.04, 11.3, 10)

11.6

Description

I needed to disable the wildcard setup in favor of explicit static routes so I could use the catch all route for handling non-static pages.

This broke some unrelated route handling later on and I’ve tracked it back to this change. I have files that need to be served from the /page-data/app/[...]/page-data.json route. This works fine when wildcard: true. However, when wildcard: false the code uses encodeUri on all the file names meaning the server is now listening for /page-data/app/%5B...%5D/page-data.json and breaks my app.

Steps to Reproduce

Try serving a file with fastify-static and wildcard: false. If the file has characters modified by encodeUri then the file will 404.

Expected Behavior

I’d expect that setting wildcard: false would still allow /page-data/app/[...]/page-data.json to be served at it’s correct path.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:13 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
mav-rikcommented, Oct 3, 2021

@moonmeister according to rfc the [ and ] characters must always be encoded (stackoverflow). But it looks like browsers (like chrome) and node-fetch just ignore that rule and send [ and ] as is. When you set wildcard: false the framework lists all the files in root directory and creates handlers for each file doing encodeURI for files paths which is the right thing to do. But node-fetch or chrome send un-escaped [ ] so the path does not match to any file. When you set wildcard: true obviously the framework creates a single handler for root + '*' path. Then no matter if browser encodes the path or not it gets to the handler with asterisk. In your case I would suggest to always encode path when requesting the file (which won’t work in browser):

const res = await fetch(encodeURI(`http://localhost:3000${path}`));

Another solution would be to PR the code and add un-encoded (decoded) routes along with encoded ones. But I am not sure how bad this solution is.

0reactions
stale[bot]commented, May 2, 2022

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Resolve decoding inconsistency in `url_utility.queryToObject`
In applyQuery we set the url params with setUrlParams , and internally this function uses URLSearchParams (see app/assets/javascripts/lib/utils/ ...
Read more >
encodeURI() - JavaScript - MDN Web Docs
The encodeURI() function encodes a URI by replacing each instance of certain characters by one, two, three, or four escape sequences ...
Read more >
inconsistent window.history.pushState uri encoding
I have considered encodeURIComponent('www.somesite.com/@user1') for the pushstate url, but this also encodes the '/', and what I am hoping is ...
Read more >
EncodeURI incompatible with PI Web API? - PI Square
I used to use encodeURI for the individual calls going into the PI Web API: var getDataStreamURL = encodeURI(PIWebAPIURL + "streams/" + datastream....
Read more >
URL-Encoding of "inconsistent" - Online
Encode inconsistent to URL-encoded format with various advanced options. Our site has an easy to use online tool to convert your data.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found