medialize / URI.js

Javascript URL mutation library
http://medialize.github.io/URI.js/
MIT License
6.26k stars 476 forks source link

URI malformed exception is thrown when overriding path/pathname #419

Closed zaidoonhad closed 1 year ago

zaidoonhad commented 1 year ago

Repro

const url = new URI('https://example.com/something/%E3%82%BF%E3%82%B0/%E6%88%A6%E')
const newUrl = url.path(`/hello${url.path()}`)

path/pathname doesn't like the encoding here but URI(xxx) was successfully able to parse the url . So I would expect for it work when trying to override part of the path as well.

rodneyrehm commented 1 year ago

Hey there,

new URI does not parse the URL beyond figuring out the distinct parts (path, query, fragment, …) and does not mutate the URL itself.

.path mutates the URL and therefore takes a much closer look at the value. it first decodes the input, then encodes it to make sure we're producing a valid URL.

url.path()
// '/something/%E3%82%BF%E3%82%B0/%E6%88%A6%E'

url.segment()
// ['something', '%E3%82%BF%E3%82%B0', '%E6%88%A6%E']

decodeURIComponent('%E3%82%BF%E3%82%B0')
// 'タグ'

decodeURIComponent('%E6%88%A6%E')
// Uncaught URIError: URI malformed

The problem is the trailing %E, which is not a valid percent-encoding-sequence - it's missing a character.