Why does ASP.NET Core's response compression middleware leave HTTPS responses uncompressed by default, and when would you enable EnableForHttps or compress at a proxy instead?
answer
- CRIME and BREACH
- secret plus attacker-reflected input
- IHttpsCompressionFeature and HttpsCompressionMode
- StaticFileOptions.HttpsCompression defaults to Compress
- IsHttps behind a TLS-terminating proxy
basics
~20 sCompressing HTTPS responses that mix a secret with attacker-influenced input lets an attacker infer the secret from response sizes (CRIME, BREACH). Enable EnableForHttps only for responses without that mix, opt individual responses in or out, or let a proxy compress.
solid answer
~50 s`EnableForHttps` defaults to `false` because compression ratios leak information: if a response contains a secret, such as an antiforgery token or personal data, and also reflects input the attacker controls, an attacker who can trigger many requests and watch encrypted sizes can recover the secret piece by piece (BREACH; CRIME did the same at the TLS layer). For a public catalogue that carries no secrets, turning it on is reasonable. You can also decide per response through `IHttpsCompressionFeature.Mode`: `Compress` opts a response in even when the option is off, `DoNotCompress` opts it out even when it is on. Static files already default to `HttpsCompressionMode.Compress`. The docs prefer server-level compression in IIS, Apache or Nginx when you have one, and keep the middleware for Kestrel or HTTP.sys facing clients directly. Behind a TLS-terminating proxy, `Request.IsHttps` is false unless forwarded headers are applied, so the HTTPS gate quietly stops protecting you.
code
csharp · 15 linesusing Microsoft.AspNetCore.Http.Features;
builder.Services.AddResponseCompression(o => o.EnableForHttps = true);
var app = builder.Build();
app.UseResponseCompression();
// Account page renders an antiforgery token next to the user's search text
app.MapGet("/account/orders", (HttpContext context, string? search) =>
{
if (context.Features.Get<IHttpsCompressionFeature>() is { } feature)
{
feature.Mode = HttpsCompressionMode.DoNotCompress;
}
return Results.Content(RenderOrdersPage(search), "text/html");
});go deeper
Know that HTTPS responses are not compressed by default and that the reason is a security attack on compressed encrypted traffic.
Explain the BREACH conditions and the three HttpsCompressionMode values, and why static files are compressed regardless.
Set a compression policy per endpoint, exempt secret-bearing pages, and catch the IsHttps blind spot behind TLS-terminating proxies.
Own the decision of which tier compresses for the estate, balancing CPU, latency and side-channel exposure across apps and proxies.
## What the default protects against Compression shrinks repeated text. If an attacker can put a guess into a response that also contains a secret, the compressed response gets slightly **smaller when the guess matches part of the secret**. Over HTTPS the attacker cannot read the bytes, but can often observe their length on the network. Repeating the request with different guesses recovers the secret one character at a time. That is the idea behind **CRIME** (against TLS-level compression) and **BREACH** (against HTTP response compression). An attack needs all of these at once: 1. The response is **compressed and sent over HTTPS**. 2. The body contains a **secret**: an antiforgery token, a session-bound value, personal data. 3. The same body **reflects attacker-controlled input**, such as a search term or query parameter. 4. The attacker can **make the victim's browser send many requests** and **observe response sizes**. Because the middleware cannot know whether a dynamic page meets these conditions, `ResponseCompressionOptions.EnableForHttps` defaults to `false`. ## How the HTTPS gate actually decides For an HTTPS request the provider looks at two inputs, the option and a per-response feature, `IHttpsCompressionFeature`, whose `Mode` is an `HttpsCompressionMode`: | `Mode` on the response | `EnableForHttps = false` | `EnableForHttps = true` | |---|---|---| | `Default` | not compressed | compressed | | `Compress` | compressed | compressed | | `DoNotCompress` | not compressed | not compressed | The middleware installs the feature when the request carries `Accept-Encoding`, so code downstream can set it: ```csharp if (context.Features.Get<IHttpsCompressionFeature>() is { } feature) { feature.Mode = HttpsCompressionMode.DoNotCompress; } ``` The static file middleware sets it for you: `StaticFileOptions.HttpsCompression` defaults to `HttpsCompressionMode.Compress`, so files served after `UseResponseCompression` are compressed over HTTPS even with `EnableForHttps` off. Static files carry no per-user secrets, which is why that default is safe. ## When enabling it is reasonable - **Public, secret-free payloads.** A wholesaler's product catalogue or price list that is the same for everyone has nothing to extract. - **APIs that do not reflect input next to secrets.** A JSON API authenticated by a bearer header, whose bodies never contain tokens, is a weaker target than an HTML page with a form token and a search box. - **With exceptions carved out.** Turn it on, then set `DoNotCompress` on endpoints that render tokens or personal data next to user input. The ASP.NET Core docs point to antiforgery tokens and the BREACH mitigations list when you do enable it; length-hiding and separating secrets from reflected input are the general ideas. ## Compressing at a proxy instead The docs recommend server-based compression (IIS dynamic compression, Apache `mod_deflate`, Nginx) when it is available, because those modules usually outperform the middleware, and reserve the middleware for apps served directly by Kestrel or HTTP.sys, which have no compression of their own. Points to check when a proxy is involved: - **The proxy may strip `Accept-Encoding`.** The docs note that Nginx does when proxying, so the middleware never compresses behind it. - **IIS or App Service can gzip on their own**, even with `EnableForHttps` off; check the `Server` header before blaming the app. - **No double compression.** The middleware skips responses that already have `Content-Encoding`. - **The scheme the app sees.** When the proxy terminates TLS and forwards plain HTTP, `Request.IsHttps` is `false` unless forwarded headers are processed. The middleware then treats the request as HTTP and compresses it, so the BREACH protection silently disappears. ## A policy for the wholesaler API 1. Keep `EnableForHttps = false` as the default while the app still renders HTML pages with antiforgery tokens. 2. Opt the anonymous catalogue and price-list endpoints in with `HttpsCompressionMode.Compress` from a small endpoint filter or middleware, since their bodies are the same for everyone. 3. Leave account pages, order history and anything echoing search text at `Default`, or set `DoNotCompress` explicitly once the option is turned on. 4. Make sure forwarded headers are processed, so `Request.IsHttps` tells the truth behind the load balancer. 5. Review the list whenever a new endpoint mixes user input with personal data. ## What interviewers listen for - The four conditions of BREACH, not just the acronym. - `EnableForHttps` versus the per-response `HttpsCompressionMode`, including static files' `Compress` default. - A policy: compress public payloads, exempt secret-bearing pages. - The proxy pitfalls: stripped headers, server-level gzip, and `IsHttps` behind TLS termination.
- Why are static files compressed over HTTPS even though EnableForHttps is false?`StaticFileOptions.HttpsCompression` defaults to `HttpsCompressionMode.Compress`, and the static file middleware writes that into `IHttpsCompressionFeature` before sending the file. A `Compress` mode overrides the `EnableForHttps = false` default. Set the option to `DoNotCompress` if a static folder ever holds sensitive per-user files.
- The app sits behind a proxy that terminates TLS. What happens to the HTTPS gate?Kestrel receives plain HTTP, so `Request.IsHttps` is `false` unless the forwarded headers middleware rewrites the scheme from `X-Forwarded-Proto`. The compression provider then treats every request as HTTP and compresses it whatever `EnableForHttps` says, so secret-bearing pages lose the protection without any warning.
Compression over HTTPS is like mailing sealed envelopes whose thickness depends on how much the letter repeats itself. Nobody can open them, but if a stranger can slip a sentence into your letter and weigh the envelope, a thinner envelope tells them the sentence matched something already written inside.
- HTTPS encryption makes compression side channels impossible, so EnableForHttps is always safe.
- Setting EnableForHttps to false also stops static files from being compressed over HTTPS.
- HttpsCompressionMode.Compress has no effect while EnableForHttps is false.
- Behind a TLS-terminating proxy, the middleware still knows the client used HTTPS.
- BREACH needs the attacker to decrypt the TLS traffic first.