corazawaf / coraza-caddy

OWASP Coraza middleware for Caddy. It provides Web Application Firewall capabilities
https://www.coraza.io/
Apache License 2.0
284 stars 35 forks source link

Response headers leakage during block in phase 4 #147

Open romko11l opened 2 months ago

romko11l commented 2 months ago

Coraza module for Caddy pass reponse headers, even if it should not give the response to the user.

Example of a protected backend:

package main

import (
    "fmt"
    "log"
    "net/http"
)

func example(w http.ResponseWriter, req *http.Request) {
    res := `
#! /usr/bin/python3

a = 2
b = 3
c = a + b
print(c)
`
    w.Header().Add("X-romko11l", "123")
    log.Println(123)
    fmt.Fprintf(w, res)
}

func main() {
    http.HandleFunc("/", example)
    http.ListenAndServe(":8090", nil)
}

Caddyfile:

{
    order coraza_waf first
}

:8080 {
    coraza_waf {
        load_owasp_crs
        directives `
          SecRule RESPONSE_BODY "@rx ^#\!\s?/"  "id:1,phase:4,deny,log,t:none"
          SecResponseBodyMimeType text/plain
          SecResponseBodyAccess On
          SecRuleEngine On
        `
    }

    reverse_proxy 0.0.0.0:8090
}

Coraza-caddy do not pass response body, but pass response headers:

curl -vvv http://127.0.0.1:8080
*   Trying 127.0.0.1:8080...
* TCP_NODELAY set
* Connected to 127.0.0.1 (127.0.0.1) port 8080 (#0)
> GET / HTTP/1.1
> Host: 127.0.0.1:8080
> User-Agent: curl/7.68.0
> Accept: */*
> 
* Mark bundle as not supporting multiuse
< HTTP/1.1 403 Forbidden
< Content-Type: text/plain; charset=utf-8
< Date: Mon, 06 May 2024 10:40:51 GMT
< Server: Caddy
< X-Romko11l: 123 <------- leaked header
< Content-Length: 0
< 
* Connection #0 to host 127.0.0.1 left intact

Steps to reproduce:

  1. Start protected backend.
  2. Start Caddy server with the provided Caddyfile configuration.
  3. Send a request:
    curl -vvv http://127.0.0.1:8080
  4. Check the reponse header :)

Expected Behavior: Caddy server should not pass response headers from backend.

Actual Behavior: Caddy server pass response headers from backend.

Additional Information:

M4tteoP commented 2 months ago

Hi @romko11l, thanks a lot for the detailed report. I managed to reproduce it and propose an initial fix: https://github.com/corazawaf/coraza/pull/1062. Any feedback is welcomed. After being merged, we will have to port the fix also to coraza-caddy.