AlinsRan opened a new pull request, #13781:
URL: https://github.com/apache/apisix/pull/13781
### Description
Hostnames are case-insensitive, and route matching runs against `$host`,
which nginx always lowercases (`ngx_http_validate_host()`). `apisix/router.lua`
normalizes this on the route object:
```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. `radixtree_host_uri` — the default router — builds its host buckets
itself, keyed 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()
```
A service host containing an uppercase character therefore ends up in a
bucket keyed on e.g. `moc.elpmaxe.esaCdexiM`, which the lowercase `$host` can
never reach. The route falls through to `only_uri_router`, and if nothing
host-agnostic matches, the request 404s — for every path and every casing of
the request `Host`, so the host is silently unroutable.
`radixtree_uri` is not affected, because it passes `hosts` to
lua-resty-radixtree, which lowercases on both the config side and the request
side.
Reproduced on 3.16.0 in standalone mode, with `service.hosts:
["MixedCase.example.com"]` and a route with no `hosts` of its own:
| request `Host` | `radixtree_host_uri` (default) | `radixtree_uri` | after
this patch |
| --- | --- | --- | --- |
| `MixedCase.example.com` | 404 | 200 | 200 |
| `mixedcase.example.com` | 404 | 200 | 200 |
| `MIXEDCASE.EXAMPLE.COM` | 404 | 200 | 200 |
This patch lowercases the hosts in the service `filter()`, mirroring what
`router.lua` already does for routes.
Found via apache/apisix-ingress-controller#2822: the controller carries the
host constraint on the service object, so an ingress route with an uppercase
hostname is unroutable.
#### Which issue(s) this PR fixes:
N/A
### Checklist
- [x] I have explained the need for this PR and the problem it solves
- [x] I have explained the changes or the new features added to this PR
- [x] I have added tests corresponding to this change
- [ ] I have updated the documentation to reflect this change
- [x] I have verified that this change is backward compatible (If not,
please discuss on the [APISIX mailing
list](https://github.com/apache/apisix/tree/master#community) first)
--
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]