I painted over a 4-digit code and read it back from the pixels outside the box
Can you unredact an image? From lossy WebP, yes: I got a hidden 4-digit code back from pixels 96 px outside the box. JPEG with a 16 px margin leaked nothing.
By Meet ModiI drew a 4-digit code on an image, saved it as lossy WebP, decoded it, and painted a solid black box over the digits. Then I made the box 96 pixels bigger on every side. Comparing only what was left outside the box against 99 other codes, the real one matched exactly. Five codes out of five.
So can you unredact an image? Not by lifting the box: the pixels under it are gone. But if the image was lossy before you redacted it, the hidden content changed how the rest of the image was compressed, and that change is still there. With JPEG the effect stays inside 8 by 8 blocks, so a margin fixes it. With lossy WebP it spreads across the whole picture. Thomas Karpiniec showed the WebP half on November 29 with a YES and a NO. I wanted to know how far it goes, why, and whether it is enough to actually guess what was hidden.
Why redacting a JPEG needs an 8 px margin
JPEG cuts an image into 8 by 8 pixel blocks and compresses each one on its own. Quantization throws away detail inside a block, which is why sharp text in a JPEG gets a faint halo of noise around it. That halo is made from the text. If your black box covers the text but not the whole block, the halo in the uncovered part of the block still depends on what the text was.
The rule people use is to make the box at least 8 px bigger than the content. Then every block that held any of the text is covered, and the blocks outside it never saw the text at all. My test agreed:
# 800x400 textured image, "YES" vs "NO" drawn on it, encoded, decoded,
# box painted 2 px past the text. Share of pixels that differ, by distance from the box.
JPEG q50: 1-8: 54.1% max 28 | 9-16: 4.8% max 9 | 17-32: 0.0% | 33-64: 0.0% | 65-128: 0.0% | 129+: 0.0%
WEBP q50: 1-8: 91.4% max 17 | 9-16: 79.6% max 18 | 17-32: 75.0% max 16 | 33-64: 70.1% max 21 | 65-128: 66.7% max 20 | 129+: 64.9% max 19
WEBP q80: 1-8: 86.8% max 18 | 9-16: 74.9% max 16 | 17-32: 73.9% max 16 | 33-64: 65.1% max 18 | 65-128: 56.2% max 19 | 129+: 57.6% max 25
WEBP q95: 1-8: 68.0% max 8 | 9-16: 46.2% max 7 | 17-32: 45.4% max 6 | 33-64: 41.3% max 7 | 65-128: 33.6% max 7 | 129+: 35.2% max 8JPEG's leak stops within 16 px, because the box was not aligned to the 8 px grid. WebP's does not stop. Even at quality 95, a third of the pixels more than 129 px away from the box differ depending on whether the hidden word was YES or NO. The differences are small, up to 25 levels out of 255 and mostly less, and invisible to the eye. A script does not need to see them.
Is WebP lossy or lossless?
Both, and the two are different formats in one container. Lossy WebP is VP8, the video codec's keyframe format. Lossless WebP is a separate codec. In my test, lossless WebP leaked nothing, as you would expect, and near-lossless at level 60 leaked a little near the box and almost nothing far away:
# cwebp 1.6.0 from Google's release tarball, same YES/NO test, q50 unless noted
-q 50 near(1-16px) 85.3% far(>64px) 65.7% max 20
-q 50 -segments 1 near(1-16px) 64.3% far(>64px) 32.0% max 15
-q 50 -f 0 near(1-16px) 85.5% far(>64px) 67.4% max 24
-q 50 -segments 1 -f 0 -sns 0 near(1-16px) 61.8% far(>64px) 33.4% max 20
-lossless near(1-16px) 0.0% far(>64px) 0.0% max 0
-near_lossless 60 near(1-16px) 45.1% far(>64px) 0.6% max 4You cannot tell which kind a .webp file is from its name. webpinfo from the same libwebp tarball prints the chunk type: VP8 for lossy, VP8L for lossless.
Why lossy WebP leaks across the whole image
Two things, as far as I can tell from poking at it. The first is prediction. VP8 does not compress each 16 by 16 macroblock alone. It predicts each one from the already-decoded pixels above it and to its left, and stores only the difference (RFC 6386 describes the scheme). If the text changes how one macroblock comes out, the next macroblock is predicted from different pixels, and so on, right and down, in the order the image is coded.
The second thing is that the encoder makes some decisions from the whole image at once. With cwebp's default settings, the rows above the box changed too, which prediction alone cannot do. With -segments 1 they did not:
# share of pixels that differ between the YES and NO images, by region
-q 50 | above box: 57.4% | left: 56.8% | right: 77.1% | below: 73.7%
-q 50 -segments 1 | above box: 0.0% | left: 15.1% | right: 60.9% | below: 60.2%With one segment, the leak runs right and down, exactly the direction prediction runs. With the default four, my guess is that the hidden content nudges how the encoder splits the image into segments, each with its own quantizer, and that reaches everywhere. Turning off the deblocking filter (-f 0) changed nothing, so it is not the filter. One caveat on my own reading: cwebp's documentation says -segments has no effect at method 3 and up unless -low_memory is set, and the default method is 4. It clearly had an effect here. I am reporting what the numbers did, not claiming to know which line of libwebp did it.
Can you unredact an image? I tried
A pixel diff between YES and NO proves information leaks. It does not prove anyone can use it. So I played the attacker. I drew a random 4-digit code on the same textured background, saved it as WebP, decoded it, and painted the box. Then, for 200 candidate codes including the right one, I did the same thing and summed the absolute difference against the target over every pixel outside the box. The lowest score wins.
WEBP q50 box+0px: picked the hidden code 5/5 times out of 200 candidates each (1 candidate tied with the right one)
WEBP q80 box+0px: picked the hidden code 5/5 times out of 200 candidates each (1 candidate tied with the right one)
WEBP q80 box+32px: picked the hidden code 5/5 times out of 200 candidates each (1 candidate tied with the right one)
JPEG q50 box+0px: picked the hidden code 5/5 times out of 200 candidates each (1 candidate tied with the right one)
JPEG q50 box+16px: picked the hidden code 5/5 times out of 200 candidates each (200 candidates tied with the right one)
target q80, attacker assumes q80, box+96px: 5/5 out of 100 candidates"1 candidate tied" means the right code was the only one with its score. The last JPEG line is the margin working: with the box 16 px past the text, all 200 candidates produced identical pixels outside it, so the "win" was just the first in the list. WebP with a box grown by 96 px on every side still gave the code away every time.
The attacker does need the quality setting. When I guessed q75 or q85 for a q80 image, it got 0 out of 5. That is less comforting than it sounds. The right code at the right quality reproduces the outside pixels exactly, a difference of 0, so you can search both at once:
hidden 6707 at q80 | best (difference, code, quality) over 40 codes x 21 qualities: (0, '6707', 80)How to properly redact a screenshot
- Redact the original. Screenshot tools usually save PNG, which is lossless, but check yours. Paint the box on that, before anything compresses it.
- Use a solid fill. Blur and pixelation have their own, older, well-known ways of being reversed. A black rectangle has none.
- Then compress if you need to. Once the secret's pixels are gone, the encoder never sees them, and lossy WebP is fine.
- If all you have is a lossy WebP, do not redact it and share it. Take a new capture if you can. If you cannot, crop the secret's whole region out, and remember that the rest of the image may still carry traces of it.
- For JPEG, a box 8 px past the content on every side covers every block that touched it. In my test 16 px past the text was enough.
This matters for web apps because a lot of pipelines produce lossy WebP without asking. Next.js's image optimizer, for one, defaults to formats: ['image/webp'] at quality 75 (from the defaults in next 16.1.6's image config). If a user uploads a screenshot, your app turns it into WebP, and someone later screenshots and redacts that rendering, the redaction happened on a lossy copy. That is the same class of surprise as accept="image/*" letting an SVG through: the format does something nobody at the upload step was thinking about.
The catch: what this does not show
- My attacker knew everything: the exact background, font, size, position, encoder version and the quality range. That is the best case for them. With a real photo and an unknown layout, they would first have to rebuild all of that.
- Four digits is 10,000 guesses. A 24-character API key is not something this method brute-forces. On a flat, settings-page style screenshot with a fake key, lossy WebP still changed about 9% of pixels more than 64 px from the box at q50 and about 2% at q80, so information leaks, but I did not show a key can be recovered from it.
- Flat UI screenshots leak less. In my first test, on a plain grey background with small text, nothing outside the box changed at all. Texture makes it worse, so photos of documents and screens are the riskier case.
- Everything here used libwebp 1.6.0, through Pillow 13.0.0 and through Google's cwebp. Other encoders may behave differently, in either direction.
Can you unredact an image?
Not by removing the box, because the covered pixels are replaced. But if the image was lossy before it was redacted, traces of the hidden content can survive outside the box. In my test on lossy WebP, comparing only the pixels outside a black box picked the right 4-digit code out of 100 candidates every time, even with the box grown by 96 px.
Is WebP lossy or lossless?
WebP can be either. Lossy WebP uses the VP8 keyframe format, and lossless WebP uses a separate codec, both in the same .webp container, so the file name does not tell you which. Running webpinfo from Google's libwebp tools shows a VP8 chunk for lossy and VP8L for lossless. In my redaction test, lossless WebP leaked nothing.
How do you properly redact a screenshot?
Paint a solid box over the original lossless capture, usually the PNG your screenshot tool saved, and only then convert or compress it. Avoid blur and pixelation, which can be reversed. Do not redact a lossy WebP or JPEG copy and assume it is clean. For JPEG, cover at least 8 px past the content on every side.
Why is redacting a WebP image unsafe?
Lossy WebP predicts each 16x16 block from the pixels above and to the left, and the encoder also makes some choices from the whole image. So content in one place changes how the rest of the image is compressed. In my test, about 65% of pixels more than 64 px from the box differed depending on the hidden word.
How big should a redaction box be on a JPEG?
At least 8 px past the content on every side, so every 8x8 JPEG block that held any of it is covered. In my test, a box 2 px past the text leaked enough to pick a 4-digit code out of 200, and a box 16 px past it left every candidate identical outside the box. Redacting the original before compressing is still safer.
Does converting a redacted image to PNG remove the leak?
No. Saving to PNG keeps every pixel exactly, including the traces the earlier lossy compression left outside the box. In my tests the redacted image was compared pixel for pixel, which is what a PNG preserves. The leak comes from compression that happened before the redaction, so only redacting an uncompressed original avoids it.
- Take care redacting WebP images, Serious Computer Business, Thomas Karpiniec
- RFC 6386: VP8 Data Format and Decoding Guide, IETF
- cwebp documentation, Google for Developers
- libwebp downloads, WebM Project
- Image component, formats and qualities, Next.js
More Posts
Next.js 16.5 compiles 4.2x faster. This blog got 1.35x
Next.js 16.5 on a 92-page static site with 2 cores: cold Turbopack compile went from 7.0 s to 5.2 s with 21% less memory. Not 4x. Warm builds didn't move.
I made a post-quantum SSH key. GitHub never answered it
A post-quantum SSH key is one ssh-keygen command in OpenSSH 10.6. GitHub, GitLab, Bitbucket, Codeberg and sourcehut can't use it, and GitHub just hangs.
I reran Laravel Rust's 18x benchmark. Most of the gap is Eloquent
Laravel Rust served 1,243 req/s to Laravel PHP's 86 on one core in my rerun. Swap Eloquent for the query builder and PHP reaches 543. Here is the gap.