AlinsRan opened a new pull request, #2837:
URL: https://github.com/apache/apisix-ingress-controller/pull/2837

   <!-- Please answer these questions before submitting a pull request -->
   
   ### Type of change:
   
   - [x] Bugfix
   
   ### What this PR does / why we need it:
   
   Fixes #2822.
   
   An `ApisixRoute` whose `match.hosts` contains an uppercase character is 
silently unroutable: every request for that host returns 404, on every path and 
for every casing of the request `Host` header.
   
   Route matching runs against `$host`, which nginx always lowercases 
(`ngx_http_validate_host()`). APISIX normalizes the route object to match, in 
`apisix/router.lua`:
   
   ```lua
   if route.value.host then
       route.value.host = str_lower(route.value.host)
   elseif route.value.hosts then
       for i, v in ipairs(route.value.hosts) do
           route.value.hosts[i] = str_lower(v)
       end
   end
   ```
   
   `apisix/http/service.lua` has no equivalent, so `service.hosts` is stored 
verbatim, and the default `radixtree_host_uri` router keys its host buckets on 
the raw reversed host string:
   
   ```lua
   -- apisix/http/router/radixtree_host_uri.lua
   for i, host in ipairs(hosts) do
       local host_rev = host:reverse()
   ```
   
   An uppercase host therefore lands in a bucket keyed on e.g. 
`moc.elpmaxe.esaCdexiM`, which the lowercase `$host` can never reach.
   
   Before #2743 the constraint was written to both the route and the service, 
so it passed through `router.lua`'s normalization and casing never mattered. 
Since #2743 it travels on the service only, where nothing normalizes it — which 
is why this reproduces on 2.1.0 but not on 2.0.1.
   
   Verified against APISIX 3.16.0 in standalone mode, `service.hosts: 
["MixedCase.example.com"]` with a route that carries no `hosts`:
   
   | request `Host` | `radixtree_host_uri` (default) | `radixtree_uri` |
   | --- | --- | --- |
   | `MixedCase.example.com` | 404 | 200 |
   | `mixedcase.example.com` | 404 | 200 |
   | `lowercase.example.com` (control) | 200 | 200 |
   
   `radixtree_uri` is unaffected because lua-resty-radixtree lowercases hosts 
itself, on both the config and the request side.
   
   This PR lowercases the hosts in the translator. Hostnames are 
case-insensitive, so it is a no-op for every host that already works. Only 
`ApisixRoute` needs it: Ingress hosts are validated as DNS-1123 subdomains and 
Gateway API `Hostname` has a lowercase-only pattern, so neither can carry 
uppercase. ApisixTls SNIs are also fine — `apisix/ssl/router/radixtree_sni.lua` 
lowercases both the configured SNIs and the one from the handshake.
   
   The asymmetry in APISIX is fixed separately in apache/apisix#13781; this 
change also keeps existing APISIX releases working.
   
   ### Pre-submission checklist:
   
   - [x] Did you explain what problem does this PR solve? Or what new features 
have been added?
   - [x] Have you added corresponding test cases?
   - [ ] Have you modified the corresponding document?
   - [x] Is this PR backward compatible?
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to