Skip to content

URI malformed when receiving traffic from bittorrent clients #19848

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

Closed
7 tasks done
itschasa opened this issue Apr 11, 2025 · 1 comment · Fixed by #19853
Closed
7 tasks done

URI malformed when receiving traffic from bittorrent clients #19848

itschasa opened this issue Apr 11, 2025 · 1 comment · Fixed by #19853

Comments

@itschasa
Copy link
Contributor

itschasa commented Apr 11, 2025

Describe the bug

Whenever the following path is sent to a vite app:

/?info_hash=%20%19KE%B7Fj%C4%18%C4%E69%CAK%8D%25%A3-%8AY&peer_id=-qB4450-hbn4UiL5QKA_&port=17002&uploaded=0&downloaded=1851392&left=617611264&corrupt=0&key=17919F10&event=stopped&numwant=0&compact=1&no_peer_id=1&supportcrypto=1&redundant=46784

It will return

[vite] Internal server error: URI malformed
      at decodeURI (<anonymous>)
      at viteTransformMiddleware (file:///***/node_modules/vite/dist/node/chunks/dep-ByPKlqZ5.js:42708:13)
      at call (file:///***/node_modules/vite/dist/node/chunks/dep-ByPKlqZ5.js:22743:7)
      at next (file:///***/node_modules/vite/dist/node/chunks/dep-ByPKlqZ5.js:22687:5)
      at viteHMRPingMiddleware (file:///***/node_modules/vite/dist/node/chunks/dep-ByPKlqZ5.js:44204:7)

Trying to decode the URI using decodeURI() in js results in the same error, however new URL() is able to handle it fine.

Unfortunately, I cannot change how these clients send these arguments, as it's a fully aged protocol implemented across dozens of clients.

Reproduction

https://stackblitz.com/edit/vitejs-vite-aknq4gqx?file=index.html&terminal=dev

Steps to reproduce

  1. Go to any vite app
  2. Go to the path provided above.
  3. Error occurs.

System Info

Is this necessary?

Used Package Manager

npm

Logs

No response

Validations

@itschasa
Copy link
Contributor Author

After some more investigation, it seems that the info_hash parameter is causing the malformed URL. The spec describes the info_hash as a "urlencoded 20-byte SHA1 hash".

As decodeURI expects all encoded parts prefixed with a % to be a valid UTF-8 character (source) and not any byte, it causes the error.

One way to fix this would to implement a custom decodeURI function to handle other bytes. Currently, decodeURI('%00') returns \u0000, maybe something similar could be implemented so other bytes are converted. Or maybe just ignore those characters and leave as is.

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

Successfully merging a pull request may close this issue.

1 participant