fix(ui): decode room id from URL and encode it when writing back - #254
Merged
Merged
Conversation
The room id was read from the query string with a hand-rolled parser that
never decoded it, while the id was written back into the URL without any
encoding:
getFromURL = (key, search) =>
search.slice(1).split('&')
.find((param) => param.startsWith(`${key}=`))?.split('=')[1];
window.history.pushState({roomId: id}, '', id ? `?room=${id}` : '?');
Browsers percent-encode non-ASCII characters when a URL is serialized, so a
room named "不辞" is shared as `?room=%E4%B8%8D%E8%BE%9E`, but the client then
joins the literal `%E4%B8%8D%E8%BE%9E` room, which does not exist:
-> {"type":"join","payload":{"id":"%E4%B8%8D%E8%BE%9E"}}
<- close 1000 "room with id %E4%B8%8D%E8%BE%9E does not exist"
The same asymmetry also broke ids containing `&`, `=`, `+`, `#` or `%`, and
made ids that already contain percent escapes gain another encoding level on
every write, producing broken (double encoded) share links.
Use the platform URL API, which decodes on read and encodes on write exactly
once:
new URLSearchParams(search).get(key) ?? undefined
const params = new URLSearchParams();
params.set('room', id);
window.history.pushState({roomId: id}, '', `?${params.toString()}`);
No server change is needed: the room id stays the raw, human-readable string
that the ws `create`/`join` payload and the server-side room map already use,
and existing share links keep working.
Verified: URL round-trip for 不辞 / "my room" / "a+b" / "a&b=c" / "100%",
protocol-level e2e against a live server (encoded id -> "does not exist",
decoded id -> joins, cleanup on owner leave), `yarn build` (tsc + vite) and
`prettier --check src/useRoomID.ts`.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
fix(ui): decode the room id from the URL and encode it when writing back
Fixes #253
Problem
ui/src/useRoomID.tsreads and writes theroomquery parameter by hand — no decode on read, noencode on write:
Browsers percent-encode non-ASCII names when serializing a URL, so a room named
不辞is shared as?room=%E4%B8%8D%E8%BE%9E, but the client then joins the literal%E4%B8%8D%E8%BE%9Eroom and theserver closes the connection with
room with id %E4%B8%8D%E8%BE%9E does not exist. Ids containing&,=,#,+or%break in the same way, and writing the escaped value back adds anotherescaping level on every share.
Changes
Only
ui/src/useRoomID.ts: the query string now goes throughURLSearchParams, which decodes onceon read and encodes once on write.
No server change: the room id stays the raw string used by the
create/joinpayload, and existing?room=%E4%B8%8D%E8%BE%9Elinks work again without migration.Verification
cd ui && yarn build(tsc + vite) passesprettier --check src/useRoomID.tspasses不辞⇄?room=%E4%B8%8D%E8%BE%9Eround-trips stably (repeated writes do not re-escape);my room,a+b,a&b=cand100%round-trip correctly as wellroom with id %E4%B8%8D%E8%BE%9E ... does not exist), joining with the decoded id succeeds, and the test room is removed after its owner leaves