Inspecting Base64-Encoded PDF and Image Blocks Sent to Claude and Gemini for Sensitive Data
To inspect base64 PDF and image blocks for sensitive data, intercept the request before it reaches the provider, find the encoded blocks in the provider-specific JSON structure, decode them, extract text from the text layer or by OCR, run detection on the result, and apply an allow, redact or block decision. Record what was inspected and decided. Plain-text filters never see these blocks, so the inspection has to run in the request path.
The inspection pipeline: decode, extract, detect, decide
- 1
1. Locate
Parse the JSON body using the provider schema and collect every base64 field, including blocks nested in multi-turn message history and tool results.
- 2
2. Decode
Decode the string, verify the bytes match the declared media type, and reject mismatches, truncated data and files over your configured size ceiling.
- 3
3. Extract
For PDFs, read the text layer and enumerate embedded images, attachments and metadata. For images and scanned pages, run OCR. Read file metadata as well, since geotags, timestamps and device information can carry sensitive data separately from visible content.
- 4
4. Detect
Run the same detectors you apply to plain-text prompts against the extracted text and metadata, and record which detectors fired and where.
- 5
5. Decide
Map detector results and file condition to allow, redact or block under a written policy, with a defined action for files that cannot be parsed.
- 6
6. Record
Write an evidence entry for the decision without storing the sensitive content itself.
Where base64 payloads appear in Claude and Gemini requests
Applications and agents commonly attach PDFs and images to model calls by embedding the file as a base64 string inside the JSON body. In Claude requests, the file sits in a content block whose source object carries the base64 data and a media type. In Gemini requests, it sits in a part that uses an inline_data field with a MIME type and the encoded data. The two structures differ, so a single path-based rule will not cover both. Each provider needs its own parser that knows which fields hold encoded data and which hold instructions or plain text.
Base64 changes how the input is represented. A pattern matcher looking for account numbers or national identifiers in text fields sees only an opaque string. NIST's adversarial machine learning work notes that base64 is used to alter input representation and can render data unrecognizable to standard recognition methods. The same property applies to ordinary traffic: the content is intact for the model and invisible to a filter that stops at plain text.
Size and format limits differ by provider and change over time, so treat them as configuration rather than constants in your code. Both providers also offer ways to reference a file without inlining it. A request that carries only a file reference has no base64 to decode, so inspection must either cover the upload step or apply policy to the reference. Cover both paths, or the inline block becomes the only one you watch.
Trade-offs: text layers, OCR, size and false positives
Text-layer extraction is fast and exact when a PDF contains real text. It is also incomplete. A scanned form, a photographed document or a screenshot of a console holds its text only as pixels, so a text-layer reader returns nothing and the file looks clean. OCR closes that gap but adds compute and latency, and recognition errors can break a pattern that would otherwise match, such as a digit misread inside an identifier. Measure both extraction paths on your own document mix before setting thresholds, because accuracy and latency depend heavily on scan quality, layout and page count.
Latency is a real design constraint because inspection sits in a synchronous call. Common options are to run the cheap text-layer pass first and invoke OCR only for pages with little or no extractable text, to set per-file time budgets, and to decide in advance what happens when a budget is exceeded. Blocking on timeout is safer for regulated data. Allowing on timeout is easier on users. Either is defensible if it is a stated policy.
False positives need the same attention as misses. OCR noise and long numeric strings in tables can trip identifier detectors, and a policy that blocks too often pushes teams toward unmonitored workarounds. Tune detectors with context rules and confidence thresholds, and review blocked samples regularly.
Some files will not cooperate. Encrypted PDFs cannot be read without a key, malformed files may parse differently in your extractor than in the provider's, and embedded objects can hide content from a shallow pass. Decide whether each case is blocked, allowed or routed for human review, and apply that rule consistently.
Allow, redact or block: what each decision means for binary files
Allow is straightforward: nothing sensitive was found and the file was fully readable. Block is also simple to implement, because the whole request is stopped and the caller receives a policy error. Block is the right default for credentials found in a screenshot, or for any file that could not be fully inspected in a high-risk workflow.
Redact is harder than it sounds. In plain text, you replace a span. In a PDF or image, you must change the binary content so the sensitive data is actually gone. Drawing a black box over text in a PDF can leave the underlying text layer intact, and the original characters remain extractable. Effective redaction removes the content, then re-encodes the file and updates the request. For images, it means altering the pixels at the regions OCR identified, which depends on OCR having located the data correctly. Metadata must be stripped as well, or the file still carries what the visible content no longer shows.
Because of this, many teams start with allow or block for files and reserve redaction for cases where a reliable rewrite is available. Whichever you choose, run extraction again on the redacted output before forwarding it, so the file that leaves is the file you verified.
Why enforcement belongs in the execution path
NIST SP 800-228 distinguishes basic API protections, which operate at the connection or request-metadata level, from deeper inspection of request and response data. It lists non-signature payload scanning for generative AI APIs as a recommended control, meaning analysis for sensitive information that is not a literal attack signature. A base64 PDF is exactly that kind of payload. NIST SP 800-53 Rev. 5 also names content checking, security policy filters and data type identifiers as flow-control mechanisms, which gives you reference terms for the pipeline described above.
Transport encryption does not substitute for this. TLS protects the connection to the provider, but it does not evaluate what the payload contains. The FTC has also noted that customers may disclose sensitive or confidential information, including internal documents and their own users' data, when using AI models. Once the request has left, the data has been disclosed, and a review the next day can only report that it happened. A decision made before the call can prevent it.
Evidence should be a product of that decision, not a reconstruction. NIST's minimum telemetry fields are timestamp, client ID, request and response status, and endpoint identifier. For file inspection, add fields such as the decision taken, detectors that fired, file type and size, whether OCR was used, and a hash of the decoded file. Store those, not the content. Retention periods should be set deliberately and match your data residency requirements.
Enforcing policy and producing evidence with Trussed
Trussed enforces policy in the execution path, so decisions on individual interactions are enforced at runtime, not reviewed after the fact. Available actions are allow, redact, block, rate limit and escalate for human approval. The same policy applies across models, agents, tools, MCP servers, AI sites and coding assistants, with more than 100 models under one policy plane.
Each enforcement decision generates audit-ready evidence, mapped to HIPAA, NAIC, NYDFS, EU AI Act and NIST requirements. Trussed can be deployed self-managed in the customer cloud, with configurable retention and data residency, which lets a regulated enterprise keep evidence within its own boundary. Scope is individual interactions and tool calls.
Inspection across the request lifecycle
Locate
Find base64 blocks in Claude and Gemini request JSON.
Decode and extract
Recover the file, then read the text layer or run OCR.
Detect
Evaluate extracted text and metadata against policy.
Decide
Allow, redact or block before the call leaves.
Record
Keep evidence of what was inspected and decided.
Review your multimodal request path
Talk through where PDF and image payloads leave your environment, which decisions you need to enforce, and what evidence each one should produce.
Talk to an ExpertHow does your AI governance program compare?
See where your program has gaps in less than 2 minutes.