Scrivio Interview library
ScenarioIntermediate6 min read · updated 12 November 2026

A team enables gzip compression on all responses, including those containing CSRF tokens and user data over HTTPS. A security reviewer raises a concern. What is it, and what would you decide?

The concern is a BREACH-style attack: when a response compresses a secret, such as a CSRF token or session data, together with input the attacker controls, the compressed size changes depending on how much of the attacker's guess matches the secret, and an observer can recover the secret from response lengths even through TLS. I would limit compression to static or non-sensitive content types, mask tokens per response, add rate limiting, and measure the bandwidth gain, because small JSON responses gain little.

Why this question is asked

Compression looks like a pure performance setting, so it is easy to turn on for everything. The security review is right to stop it, because compression and encryption together can leak information that encryption alone would hide. Interviewers ask this to see whether we understand the attack well enough to make a proportionate decision, rather than either ignoring it or banning compression completely. In this answer, we will recap how compression works, walk through how a BREACH-style attack recovers a token, look at the conditions it needs, and then decide what to compress and which mitigations to add.

1A Quick Recap of How Gzip Shrinks Text

Gzip uses an algorithm called DEFLATE, which finds repeated pieces of text and replaces the second copy with a short reference back to the first. A JSON body that repeats "customerId" 500 times shrinks a lot, because only the first copy is stored in full.

Here is the key property: the more a response repeats itself, the smaller it gets. TLS encrypts the compressed bytes but does not hide their length. An observer on the network, such as someone on the same Wi-Fi, can see roughly how long each encrypted response is.

2How a BREACH-Style Attack Recovers a Secret

Suppose a page contains a CSRF token, a secret value that proves a form came from our site, and also reflects a search term from the URL. The attacker cannot read the page, but can make the victim's browser send requests, for example from a malicious site, and can watch the encrypted response sizes.

XML
<!-- response body, compressed then encrypted -->
<input type="hidden" name="_csrf" value="csrf=7f3a9c...">
<p>Results for: csrf=7a</p> <!-- attacker's guess, reflected from ?q=csrf=7a -->
// Guess Compressed size
// csrf=7a 1,204 bytes
// csrf=7b 1,204 bytes
// csrf=7f 1,203 bytes // one byte smaller: 'csrf=7f' repeats the real token's start
// next round guesses csrf=7f0 ... csrf=7ff, and so on
Step throughRecovering a token from response sizes
Attacker siteVictim browserOur serviceNetwork observer
make a request with ?q=csrf=7a
GET /search?q=csrf=7a (with cookies)
page with token and guess, gzipped, TLS
note encrypted length: 1,204
try csrf=7f
response is one byte shorter
keep 7f, guess the next character

7 messages in order. Play them, or step through them one at a time.

TLS hides the bytes but not the length, and compression makes the length depend on the content.

When the guess matches the start of the real token, DEFLATE can reuse more of the earlier text, so the response gets a little smaller. The attacker keeps the guess that produced the smallest response and extends it one character at a time. With a few thousand requests, the whole token can be recovered, even though every byte was encrypted. Real attacks need care to remove noise, but the principle is that simple.

3The Conditions the Attack Needs

Three things must be true at once. The response is compressed over TLS. The response contains a secret that stays the same across many requests, such as a CSRF token or a user's account number. And the same response reflects input the attacker can choose, such as a query parameter or a form field echoed back. If any one is missing, the attack fails.

So the review concern is valid for exactly the responses the team described: user pages and API responses that contain tokens or personal data and may echo input. It is not a concern for a static JavaScript bundle, which contains no secret, or for public product data.

4What I Would Decide

I would not turn compression off everywhere, and I would not leave it on everywhere. Compression stays on for static and non-sensitive content types, such as CSS, JavaScript and public catalogue JSON, ideally at the CDN. Responses that carry tokens or user data are not compressed unless we have also removed one of the attack's conditions.

Java
# Compress only types that carry no secrets; HTML pages with forms are left out
server.compression.enabled=true
server.compression.mime-types=text/css,text/javascript,application/javascript
server.compression.min-response-size=2KB
# For public JSON from one endpoint, compress at the proxy for that path only,
# rather than adding application/json for every endpoint
Step throughShould this response be compressed?
  1. Contains a secret or user data?
    YesCheck reflected inputNoCompress it
  2. Reflects input the client controls?
    YesDo not compressNoCompress with care
  3. Add per-response token maskingdefence in depth
  4. Rate limit repeated requestsslows guessing

4 steps in order. Play them, or step through them one at a time.

Notice that mime-types works per content type, not per endpoint. If our authenticated API and our public catalogue both return application/json, the application setting cannot tell them apart. That is a good reason to move the decision to the proxy, where we can enable compression per path, for example only for /catalogue/**.

5Mitigations and Measuring the Gain

Masking tokens per response removes the constant secret. Instead of sending the same CSRF token every time, the server sends it combined with a fresh random value, so each response contains different bytes and the attacker has nothing stable to guess. Spring Security 6 does this by default for CSRF tokens with its XOR-based request attribute handler (check your version). Rate limiting slows the thousands of requests the attack needs, and adding random padding to sensitive responses blurs the size signal.

Finally, measure. A 1.5 KB JSON response compresses to perhaps 600 bytes, a saving smaller than one TCP packet, and below the default 2KB minimum it would not be compressed anyway. If most of the API's responses are small, the gain is tiny and the risk is not worth it. If a few responses are 200 KB reports, we can compress those specific public ones at the proxy. The decision should come from measured bytes saved, not from a blanket setting.

6Follow-up questions

Is this the same as the CRIME attack?

They are related. CRIME used compression at the TLS level, which browsers and servers then disabled. BREACH applies the same idea to HTTP body compression, which is still widely used, so the defence has to happen in the application or proxy.

Does HTTP/2 header compression have the same problem?

HTTP/2 uses HPACK, which was designed to resist this kind of attack, for example by letting sensitive headers such as cookies be marked never to be indexed. So the concern here is body compression, not HTTP/2 itself.

Would removing reflected input be enough?

It removes one condition, and in a small application that can be enough, but reflection is easy to reintroduce: an error message that echoes a parameter is enough. Token masking is more robust because it does not depend on every developer remembering the rule.

7Conclusion

In this answer, we saw that compressing a secret together with attacker-controlled input over TLS lets response lengths reveal the secret, a BREACH-style attack, which needs compression, a stable secret and reflected input together. The decision is to compress static and non-sensitive content, ideally per path at the proxy, to mask tokens per response and rate limit as defence in depth, and to measure the bandwidth gain, which for small JSON responses is small.

What to say in the interviewAbout 80 seconds aloud

Open with

The reviewer is worried about a BREACH-style attack. TLS hides the bytes of a response but not its length, and gzip makes the length depend on content, because it replaces repeated text with short references.

Then, if they want more

If a response contains a secret, like a CSRF token, and also reflects input the attacker chooses, the attacker can make the victim's browser send guesses and watch the encrypted sizes. When a guess matches the start of the token, the response gets slightly smaller, so they extend the guess one character at a time and recover the token.

It needs three conditions together: compression over TLS, a secret that stays the same, and reflected input. So I would not compress everything.

I would keep compression for static and non-sensitive types like CSS and JavaScript, ideally at the CDN or proxy, where I can also enable it per path for public JSON, and leave out pages and API responses that carry tokens or user data.

As defence in depth, tokens should be masked per response, which Spring Security does by default for CSRF in recent versions, and repeated requests rate limited. And I would measure the gain: small JSON responses save almost nothing, so the risk is rarely worth it.

About this answer

Last updated
12 November 2026
Versions it names
Spring Security 6
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
Spring Boot reference
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "A team enables gzip compression on all responses, including those containing CSRF tokens...": https://scrivio-interview-library.netlify.app/spring-boot/embedded-servers/78978fae75/