← Blog

Gemini File MIME Types Need Validation at Upload and Inference

Parminder Singh
Parminder Singh··7 min read
Summarize with AI

Gemini file requests carry media type claims during upload and again when an uploaded URI enters a model request. A secure route should compare those claims with the stored File resource, verify that the route supports the media family, and hold any mismatch before inference. Byte parsing, malware checks, and safe decoding remain application-owned controls.

AI Security Solutionsai-securityllm-securitypolicy-enforcementzero-trustarchitectureaudit
Gemini File MIME Types Need Validation at Upload and Inference

A Gemini file crosses two typed HTTP boundaries. The upload request declares a content type, then the model request pairs the returned file URI with another MIME type. Google's examples carry the uploaded File resource's mimeType into the inference call. Production policy should preserve that relationship instead of trusting a filename or caller-supplied string, including a browser declaration, at either boundary.

I want a mismatch held before model processing. The audit record should show the original claims, the stored Gemini metadata, the route's permitted media family, and the decision that followed.

TL;DR

  • Compare the inference MIME type with the output-only mimeType on the Gemini File resource.
  • Validate upload declarations against application-side byte inspection and the route's approved media families.
  • Re-fetch metadata before use when a cached file record is stale or missing, or when it belongs to another upload context.
  • Keep safe decoding and malware analysis with the application. Use the gateway for typed HTTP policy and evidence.

MIME type appears at two separate decisions

Google's Files API guide shows the complete relationship. Its JavaScript upload example supplies audio/mpeg as upload configuration. The returned File object then provides myfile.mimeType, which the example sends beside myfile.uri in the model input. The REST version declares the media type in X-Goog-Upload-Header-Content-Type and later places that value in file_data.mime_type.

Those fields describe one uploaded object at different moments. Treat them as linked evidence. The upload declaration says what the client claims it is sending. The File resource reports the MIME type stored by the provider. The inference part says how the caller wants Gemini to interpret the referenced object.

A route should record all three values without collapsing them into one normalized field. Lowercasing a type token for comparison is reasonable, while the raw strings still belong in protected evidence. Parameters such as a caller-added charset need an explicit parser and policy rather than ad hoc string splitting.

The Files API reference labels mimeType as output-only. That makes the stored value useful provider evidence. It remains one input to an application decision rather than proof that the bytes are safe.

The validation chain starts before upload

The application receives the file first, so it owns the initial content check. Use a bounded parser or trusted file-type detector on the bytes. Compare that result with the browser or SDK declaration and the expected task. A route built for recorded interviews should have an explicit audio allowlist. A PDF review route should accept the document formats it has fixtures for and reject an image renamed with a .pdf suffix.

Then create an upload intent containing the authenticated principal, route, expected media family, byte length, protected content digest, declared MIME type, and policy version. That intent gives the upload gateway a stable contract. If the caller changes the type after approval, the start request fails.

Google's documentation uses file -b --mime-type in its REST example to populate the upload header. That is an illustrative local mechanism, not a universal security verdict. Polyglot files and malformed containers require format-specific handling. Active document content does too. The application should quarantine suspicious bytes before they enter the Gemini upload route.

My blunt opinion is that a filename extension has no place in a production authorization decision. It is display metadata dressed up as a type system.

Stored File metadata becomes the inference baseline

After upload, retrieve the File resource and bind its returned properties to the upload intent. The current API schema exposes name, mimeType, sizeBytes, sha256Hash, uri, processing state, and lifecycle timestamps. Each field answers a separate release question.

The stored MIME type should match the approved type under a documented comparison rule. Size should fit the upload intent. Where a digest is available, compare it with the application record. The file state must be ACTIVE before inference; Google defines PROCESSING as unavailable for inference and FAILED as a processing failure. Unknown future state values belong in a hold outcome rather than an assumed success path.

A useful operator view resembles a luggage tag attached to one object. Put the route and principal at the top. The next lines can show the caller's declaration and byte detector result, followed by provider metadata, byte count, digest status, and File state. Color only the final disposition. A red media row should remain visible even when every other row is green.

Store the provider File name and URI separately. The resource name supports metadata operations, while the URI enters model content. Conflating them makes later reconciliation harder and encourages string-based guesses about identity.

Inference requests need a fresh equality check

Inspect the final serialized model request. SDK helper calls are intermediate state; policy applies to the bytes dispatched over HTTP. For every file reference, extract the URI and MIME type, then locate the approved upload record. Compare the request value with the provider metadata captured for that exact resource.

Require the media family expected by the request part. An audio content item should reference an approved audio object. A document item should satisfy the document route contract. A caller that pairs an audio URI with application/pdf creates an explicit mismatch outcome. The gateway should hold the request before Gemini receives it and preserve the conflicting values.

Nested payload traversal matters when one prompt contains several files. Validate each item by ordinal and retain a separate result for it. One valid PDF should never lend its approval to the image beside it. The LLM API parameter allowlisting guide applies here at the content-item level, with MIME type and URI evaluated as a pair.

Re-fetch metadata when the local record has passed its policy freshness window or the provider state was previously PROCESSING. Re-fetch it too when the record lacks a required property. Cache hits should include the project and credential context used for the metadata lookup. That context prevents a bare URI from becoming portable authority inside the application.

Supported format and safe content are different gates

A supported MIME type means the route and Gemini integration know how to submit that media family. It says little about the content's safety or business authorization. The application still owns archive expansion, decompression limits, document sanitization, malware checks, encrypted-file handling, and format-specific parsing.

Keep those outcomes distinct in the audit event. mime_type_match can pass while safe_decode fails. A file may also pass both checks and fail authorization because the authenticated user lacks permission to send the underlying record to Gemini. Combining the outcomes into one valid_file boolean destroys useful evidence.

The Gemini file input guide distinguishes inline data from the Files API. It also covers registered Google Cloud Storage URIs. Each path needs its own validator. Inline data has no prior File resource, so the application must bind the declared MIME type directly to inspected bytes. Uploaded files can use stored File metadata. Registered objects also need the cloud-object authorization and registration context supplied by the application.

The per-route AI policy pattern gives each method a named contract. Avoid one account-wide list that silently permits every supported format on every task.

Failure outcomes should identify the broken link

Stable reason codes make incidents and tests readable. Use one outcome when the upload declaration disagrees with byte inspection. Provider metadata outside the approved upload intent needs another. An inference MIME type that differs from the File resource also needs a distinct outcome. Add route-family mismatch and unsupported-type outcomes rather than mapping every failure to bad_file.

Release fixtures should include an ordinary PDF, valid audio, an extension mismatch, a polyglot fixture accepted only by one weak detector, a request with an altered inference type, and a prompt containing several file references. Exercise PROCESSING, ACTIVE, FAILED, plus an unknown state string in the metadata adapter. A retry after processing completes should produce a new decision tied to the same File resource.

The AI audit log schema can carry principal, route, policy version, request digest, and disposition. Extend the evidence with upload-intent ID, File name, protected URI digest, raw and normalized MIME values, metadata retrieval time, File state, and validation reason. Raw file bytes belong in a separately governed store when retention is required.

DeepInspect

DeepInspect can inspect authenticated user or agent HTTP traffic deliberately routed through its proxy to Gemini upload and model endpoints. It can enforce approved upload content-type headers, compare inference MIME values with registered File metadata, hold unknown or conflicting types, and write a signed per-decision audit record before provider dispatch.

The application owns byte inspection, safe decoding, malware analysis, file authorization, upload-intent creation, and the registry that links a principal to a Gemini File resource. Google owns provider-side storage, File metadata, processing state, and model handling after receipt. Direct uploads, local file operations, STDIO traffic, and model calls that bypass the proxy remain outside DeepInspect's enforcement and evidence.

Book a technical deep dive at deepinspect.ai.

Frequently asked questions

Should the application trust the MIME type returned by Gemini?

Use the returned File.mimeType as provider metadata and compare it with the approved upload intent. Safe decoding and content scanning still depend on application-side controls. Business authorization does too because a media label cannot establish those properties.

Should the inference MIME type exactly match the File resource?

A production route should require a match under its documented normalization rule. Preserve the raw values and normalize only valid media-type syntax. Hold conflicting types before inference. Avoid guessing that two different labels describe equivalent bytes unless the route contains an explicit tested alias.

Can the file extension determine the type?

Extensions help with display and workflow hints. Byte inspection and format-specific parsing supply stronger application evidence. The route should record an extension mismatch and rely on its approved detector plus the stored provider metadata for the release decision.

What happens when a prompt contains several uploaded files?

Validate every URI and MIME pair independently after final serialization. Record the content ordinal and result, then apply the route's aggregate policy. A strict route can hold the whole request when any item fails, preventing partial inspection from creating an unreviewed path.

Does MIME validation stop malicious documents?

MIME validation limits type confusion at the HTTP boundary. The application remains responsible for safe parsers, malware analysis, archive controls, document sanitization, and authorization. Those controls should produce separate evidence that can block the upload before the provider receives the bytes.