Skip to content

Encode the id as a URL path segment, not a form value - #477

Merged
rafaelfranca merged 1 commit into
rails:mainfrom
flavorjones:card-392-id-path-encoding
Sep 9, 2026
Merged

Encode the id as a URL path segment, not a form value#477
rafaelfranca merged 1 commit into
rails:mainfrom
flavorjones:card-392-id-path-encoding

Conversation

@flavorjones

@flavorjones flavorjones commented Sep 8, 2026

Copy link
Copy Markdown
Member

Base.element_path and CustomMethods#custom_method_element_url
escaped id with URI.encode_www_form_component, an
application/x-www-form-urlencoded encoder. A space became +, so
Person.find("ann mary") requested /people/ann+mary.json and a
backend reading the path as a path saw the id as ann+mary. The
custom-method path had the same corruption.

Escape with ERB::Util.url_encode at both sites, matching the prefix
half. These were the only places in lib/ where a runtime value was
form-encoded into a path.

Breaking: an id containing a space now goes on the wire as %20
rather than +, and a literal + as %2B. Nothing raises; the
request is simply made to a different URL. Backends that form-decode
the path are unaffected. Applications that compensated for the old
encoding must remove the workaround.

`Base.element_path` and `CustomMethods#custom_method_element_url`
escaped `id` with `URI.encode_www_form_component`, an
`application/x-www-form-urlencoded` encoder. A space became `+`, so
`Person.find("ann mary")` requested `/people/ann+mary.json` and a
backend reading the path as a path saw the id as `ann+mary`. The
custom-method path had the same corruption.

Escape with `ERB::Util.url_encode` at both sites, matching the prefix
half. These were the only places in `lib/` where a runtime value was
form-encoded into a path.

Breaking: an id containing a space now goes on the wire as `%20`
rather than `+`, and a literal `+` as `%2B`. Nothing raises; the
request is simply made to a different URL. Backends that form-decode
the path are unaffected. Applications that compensated for the old
encoding must remove the workaround.
@rafaelfranca
rafaelfranca merged commit 890703e into rails:main Sep 9, 2026
18 checks passed
@flavorjones
flavorjones deleted the card-392-id-path-encoding branch September 9, 2026 01:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants