DEV Community

kozhevniko
kozhevniko

Posted on

HAProxy request rules execute in declaration order

HAProxy request rules execute in declaration order

An edge proxy often performs several jobs before it selects a backend: reject forbidden paths, normalize headers, apply rate limits, and choose a certificate or upstream. HAProxy's http-request rules are evaluated in declaration order. Rules inherited from defaults run before rules in the associated proxy. A security decision placed after a header mutation can therefore see different input than the administrator expected.

Order security before routing

A minimal policy should make the intended deny path visible and keep later rules from changing the decision.

frontend public_https
    bind :443 ssl crt /etc/haproxy/certs/
    http-request deny if { path_beg /internal }
    http-request set-header X-Forwarded-Proto https
    default_backend application
Enter fullscreen mode Exit fullscreen mode

The example is only a starting point. A real configuration should also remove client-supplied forwarding headers that the application trusts, enforce host and method policy, and decide whether an authentication subrequest is required. A deny rule that appears after an allow may never run, while a rule whose condition becomes false after an earlier header rewrite may also be skipped.

HAProxy documents that rules are evaluated in declaration order when their conditions are met. Review the generated configuration from the running version, because imports, defaults, and reusable blocks can change the visible order.

Treat header changes as policy changes

set-header and del-header are not cosmetic. They can make a request appear to come from a trusted proxy, a different scheme, or a different client address. If the application uses such a header for authorization, a client that can reach HAProxy directly may be able to bypass the intended trust boundary.

Remove incoming identity and forwarding headers at the first trusted edge. Add new values only after the proxy has established them. Keep the application listener private and test direct access from the same network that a compromised client could use.

Test the rule graph

Create requests that combine overlapping conditions: an allowed path with a blocked method, a trusted source with a forbidden host, a header that tries to override the client address, a path that becomes blocked after decoding, and a request with no matching backend. Check both the response and the backend request that was actually delivered.

Log the frontend, matched rule, backend, source, and a redacted request identifier. Avoid logging cookies or authorization values. For a production change, compare the old and new rule traces for the same corpus of synthetic requests.

HAProxy's explicit rule model makes order part of the access-control design. Treat a moved line as a policy edit and review it with the conditions that will run before and after it.

References

Top comments (0)