How We Detect Broken PDF Redactions in the Browser Using Canvas RGBA Luminance Sampling
A surprising number of "redacted" PDFs shared in legal discovery, real estate packets, HR files, and government disclosures still contain the original text underneath a black rectangle. Anyone can press Ctrl+A, copy, and
A surprising number of "redacted" PDFs shared in legal discovery, real estate packets, HR files, and government disclosures still contain the original text underneath a black rectangle. Anyone can press Ctrl+A, copy, and paste the hidden Social Security numbers, bank account digits, or contract terms straight into Notepad.
This happens because drawing a black shape or highlight annotation in Preview, Word, or basic PDF editors adds a graphics operator on top of the existing PDF content stream without removing the underlying text glyphs. Even worse, incremental PDF saves leave prior revisions intact between earlier %%EOF markers, and document /Info dictionaries still carry the original title, author, and subject strings.
While building PDFFields, I wanted a client-side diagnostic tool where anyone could drop a sensitive PDF into a browser tab, verify zero bytes leave their machine (inspectable in DevTools Network), and catch broken redactions in seconds. Here is the exact detection pipeline we shipped at pdffields.com/pdf-redaction-checker.
Why inspecting PDF vector operators alone is brittle
My first instinct was to parse raw PDF content streams (re, f, rg) and intersect every filled black rectangle with every Tj/TJ text operator. That breaks quickly on real-world PDFs:
- Rotated or nested Form XObjects (
Do) carry compound current transformation matrices (CTMs). - Redaction shapes are often drawn inside PDF annotations (
/Annot) rather than the page content stream. - People frequently cover text with opaque whiteout rectangles, dark navy boxes, or raster stamps rather than pure
#000000.
Rendering the page once to an offscreen HTML5 <canvas> with pdf.js solves all of those coordinate, clipping, z-order, and annotation-flattening edge cases automatically. We already have the exact final pixels a human sees, alongside the exact text items returned by page.getTextContent().
Step 1: Sampling rendered RGBA pixels behind every text bounding box
Normal readable text on paper has high luminance contrast: dark ink strokes mixed with bright background pixels inside the glyph bounding box (typically stdDev between 25 and 110, with a dark pixel fraction between 0.08 and 0.35).
When intact text sits underneath an opaque black rectangle, almost every pixel inside its bounding box is uniformly dark (darkFraction >= 0.85, very low luminance standard deviation). When intact text sits underneath an opaque whiteout or solid-color cover, the entire bounding box collapses to near-zero luminance variance (stdDev < 3.5).
Here is the core pixel sampler from our TypeScript module (redactionAudit.ts):
export interface PixelSurface {
data: Uint8ClampedArray;
width: number;
height: number;
}
function sampleBoxPixels(surface: PixelSurface, rect: NormalizedRect) {
const { data, width, height } = surface;
const rawX0 = Math.floor(rect.x * width);
const rawY0 = Math.floor(rect.y * height);
const rawX1 = Math.ceil((rect.x + rect.w) * width);
const rawY1 = Math.ceil((rect.y + rect.h) * height);
// Inset by 1px on boxes >= 6x5 to ignore anti-aliased border halos
const insetX = rawX1 - rawX0 >= 6 ? 1 : 0;
const insetY = rawY1 - rawY0 >= 5 ? 1 : 0;
const x0 = Math.max(0, Math.min(width - 1, rawX0 + insetX));
const y0 = Math.max(0, Math.min(height - 1, rawY0 + insetY));
const x1 = Math.max(x0 + 1, Math.min(width, rawX1 - insetX));
const y1 = Math.max(y0 + 1, Math.min(height, rawY1 - insetY));
const totalPixels = (x1 - x0) * (y1 - y0);
const stride = totalPixels <= 6000 ? 1 : Math.ceil(Math.sqrt(totalPixels / 6000));
let count = 0;
let darkCount = 0;
let sumLum = 0;
let sumLumSq = 0;
for (let y = y0; y < y1; y += stride) {
for (let x = x0; x < x1; x += stride) {
const idx = (y * width + x) * 4;
const r = data[idx] ?? 255;
const g = data[idx + 1] ?? 255;
const b = data[idx + 2] ?? 255;
const lum = 0.299 * r + 0.587 * g + 0.114 * b;
const spread = Math.max(r, g, b) - Math.min(r, g, b);
if (lum <= 48 && spread <= 30) {
darkCount++;
}
sumLum += lum;
sumLumSq += lum * lum;
count++;
}
}
const mean = sumLum / count;
const variance = Math.max(0, sumLumSq / count - mean * mean);
return {
sampleCount: count,
darkFraction: darkCount / count,
stdDev: Math.sqrt(variance)
};
}
A few small details mattered in practice to eliminate false positives:
-
1px inset: Anti-aliased edges around a black rectangle introduce bright fringe pixels on thin 10pt text boxes. Insetting by 1 pixel when the box is at least
6x5keeps the sample strictly inside the covered region. -
Neutral dark check (
spread <= 30): Requiringmax(r,g,b) - min(r,g,b) <= 30alongsidelum <= 48separates true neutral black boxes from saturated dark-blue table headers with white text (which also have highstdDevbecause of the white glyph strokes). -
White-on-black intentional headers: Because real readable white text on a black banner mixes luminance
~0and~255, itsstdDevstays high (> 45) even thoughdarkFractionis elevated. Checking bothdarkFractionand lowstdDevtogether cleanly separates readable inverse headers from fake black redactions.
Step 2: Catching unapplied /Redact annotations and multi-revision %%EOF leaks
Visual pixel cover is only one failure mode. Two structural PDF leaks are just as common:
-
Unapplied
/Redactannotations: Users mark areas in Acrobat with the Redact tool and save the file without clicking "Apply". The/Subtype /Redactannotation sits inpage.getAnnotations(), visibly outlined or shaded, while the text stream underneath is completely untouched. -
Incremental revision history (
%%EOF): PDF supports append-only incremental saves. If someone saves a redaction incrementally, earlier pre-redaction objects can survive earlier in the byte stream. Scanning raw bytes for%%EOFmarkers bounded by whitespace or EOF immediately flags files withrevisionCount > 1:
export function countPdfRevisions(bytes: Uint8Array): number {
if (!bytes || bytes.length < 5) return 0;
let count = 0;
const end = bytes.length - 4;
for (let i = 0; i < end; i++) {
if (
bytes[i] === 0x25 && // %
bytes[i + 1] === 0x25 && // %
bytes[i + 2] === 0x45 && // E
bytes[i + 3] === 0x4f && // O
bytes[i + 4] === 0x46 // F
) {
const next = bytes[i + 5];
const nextIsBoundary =
next === undefined || next === 0x0a || next === 0x0d || next === 0x20 || next === 0x09;
if (nextIsBoundary) {
count++;
i += 4;
}
}
}
return count;
}
-
/Infometadata leaks: ReadingpdfDoc.getTitle(),getAuthor(),getSubject(),getKeywords(),getCreator(), andgetProducer()viapdf-libfrequently turns up client names or internal matter numbers that were never cleared.
Step 3: Repairing leaked pages permanently (not just hiding them)
When leaks are detected, the only way to guarantee zero recoverable characters on affected pages inside a browser-only pipeline is to rasterize the affected pages at 200 DPI with solid black redaction boxes baked into the pixels, rebuild a fresh single-revision PDF via pdf-lib, wipe all /Info fields, and run a post-export text-layer verification confirming 0 remaining characters inside every redacted bounding box.
We also publish an explicit Audit Scope disclosure right in the UI: the checker flags covered text, unapplied /Redact annotations, /Info metadata, and multi-revision %%EOF trailers, and explicitly notes that embedded file attachments, raw XMP XML packets, or hidden bitmap layers underneath vector shapes should also be checked when handling high-stakes legal filings.
Try it on a broken sample or your own PDF
You can test the full client-side checker (including a built-in broken-redaction sample PDF with hidden SSNs and account numbers underneath black rectangles) with no account required at pdffields.com/pdf-redaction-checker. Open your browser's Network tab while scanning to confirm zero document bytes ever leave your machine.
If you need to draw new permanent redactions from scratch (with local SSN, email, phone, and account-number pattern detection), the full studio is at pdffields.com/redact-pdf.
Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes β full credit and traffic to the original publisher.