Skip to content

High Cardinality Risk via Unsanitized Host Header in Caddy per_host Metrics #7208

@W0n9

Description

@W0n9

When per_host is enabled in Caddy's metrics configuration, the Host header (from r.Host) is used directly as a label in Prometheus metrics without any validation or length restriction. This allows users to generate high cardinality metrics by sending requests with extremely long Host headers (I faced 25k+ strings), causing excessive memory consumption in Prometheus instances.

I think the problem caused by this

if h.metrics.PerHost {
    labels["host"] = strings.ToLower(r.Host)
    statusLabels["host"] = strings.ToLower(r.Host) // without length restriction
}

Maybe it should be limited in length?

Reproduce Caddyfile

{
	metrics {
		per_host
	}
}

// For regulatory reasons, I need to avoid returning 308 redirects to non-whitelisted domains
:80 {
	respond "MUST Match Host" 403 {
		close
	}
}

https://example.com {
    ...
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions