-
-
01 - Step 1: Private document kept strictly in browser RAM
-
02 - Step 2: Local scrubbing (ONLY_ON_DEVICE) with Gemini Nano & AES-256
-
03 - Step 3: Blind Cloud Gemini review (PREFER_IN_CLOUD) with 0 PII
-
04 - Step 3: Verbatim outbound HTTPS prompt sent to Google Cloud
-
05 - Step 4: Sub-millisecond local RAM restoration & JSON receipt
-
06 - Zero-knowledge Cloud Firestore audit log & live telemetry
Inspiration
Whenever someone works with sensitive documents (a legal contract, a hospital discharge note, a wire transfer request, or a production crash log full of .env keys), using cloud AI is risky. Cloud models are great at spotting subtle legal traps, drug interactions, or security bugs, but pasting real client names, IBAN numbers, or live API keys into an external API violates privacy rules like GDPR, HIPAA, and basic NDA obligations.
On the other hand, running a small local model in the browser keeps everything private, though a compact on-device model cannot match a larger cloud model on complex legal or medical analysis.
I built ZeroLeak AI to combine both sides practically: let the local model strip out private details right inside browser RAM, send only anonymized text to the cloud for deep analysis, and put the real names back locally once the answer comes back.
What it does
ZeroLeak AI is a 4-step web workspace built with the Firebase AI Logic Hybrid SDK (firebase/ai):
- Step 1 - Load a Private Document (Local RAM only): Paste or upload a contract, medical note,
.envstack trace, or wire transfer email. I also added a quick "Test With My Name" button so anyone testing the app can inject their own name and email in 5 seconds to see how the masking works live. - Step 2 - Local Scrubbing (
InferenceMode.ONLY_ON_DEVICE): Chrome's built-in Gemini Nano works alongside a local pattern matcher to catch names, IBANs, SSNs, medical IDs, amounts, emails, phone numbers, and secret keys. Each original value is encrypted in a temporary AES-256-GCM RAM vault (Web Crypto API) and swapped with clean placeholders like[[PERSON_01]],[[IBAN_FINANCE_01]], or[[SECRET_KEY_01]]. Before unlocking Step 3, a local check verifies that none of the original values remain in the text and generates aSHA-256hash of the anonymized payload. - Step 3 - Blind Cloud Review (
InferenceMode.PREFER_IN_CLOUD): Cloud Gemini receives only the masked text. Using structured JSON output (Schema), it flags legal, clinical, financial, or security risks and drafts a revised version while keeping placeholders like[[PERSON_01]]intact. You can inspect the exact raw prompt sent to Google Cloud right on screen. If you lose internet or switch on Offline Mode,firebase/aifalls back to on-device inference so the review still runs locally. - Step 4 - Local Restoration & Zero-Knowledge Audit Log: As soon as the cloud response arrives, the browser swaps
[[PERSON_01]]and[[IBAN_FINANCE_01]]back to their real values in memory, with zero extra network calls. You can toggle between the restored text and the blind cloud output to compare them side by side, download a.jsonverification receipt, and check the Cloud Firestore compliance log (which only stores theSHA-256hash and risk score, never the private text).
How I built it
[Private Document in Browser RAM]
|
v Stage 1: ONLY_ON_DEVICE (firebase/ai + Gemini Nano)
[Web Crypto AES-256-GCM RAM Vault] -> Leak Check + SHA-256 Hash
|
| Only [[PERSON_01]] & [[IBAN_FINANCE_01]] tokens sent
v Stage 2: PREFER_IN_CLOUD (Cloud Gemini + JSON Schema)
[Blind Cloud Review + Offline On-Device Fallback]
|
+--> Stage 3A: Local RAM Restoration (0 network bytes)
+--> Stage 3B: Firestore Audit Log (firestore.rules)
- Firebase AI Logic (
firebase/ai):- Initialized with
getAI(app, { backend: new GoogleAIBackend() }). - Stage 1 (
InferenceMode.ONLY_ON_DEVICE): UsesonDeviceModelwith Chrome's local Prompt API (Gemini Nano) so raw text never falls back to the cloud during entity detection. - Stage 2 (
InferenceMode.PREFER_IN_CLOUD/PREFER_ON_DEVICE): Sends the masked document to Cloud Gemini (gemini-3.8-flash/gemini-3.5-flash-lite, with an option forGemini 4) usingSchema.object(...)for typed JSON responses, and readsresponse.inferenceSource(ON_DEVICEvsIN_CLOUD) to display where the inference actually ran.
- Initialized with
- Web Crypto API (
crypto.subtle): Generates a non-extractable 256-bitAES-GCMkey in volatile browser memory (extractable: false) for the session vault, plusSHA-256hashes. - Cloud Firestore (
firebase/firestore): Configured withpersistentLocalCachefor offline support and customfirestore.rulesthat reject any log entry unlesszeroLeakVerified == trueand a valid 64-charactersha256SanitizedHashis provided. - Frontend & Hosting: Built with React 19, TypeScript, Tailwind CSS, Lucide Icons, and Vite, deployed on Firebase Hosting.
Challenges I ran into
- Overlapping patterns breaking placeholders: At first, my phone-number regex sometimes matched the last digits inside a European IBAN (like
GB29 NWBK 6016 1331 9268 19), which broke the placeholder mapping when restoring text later. I fixed this incryptoVault.tsby sorting matches by priority and span length so longer structured tokens (IBANs, API keys, emails) are masked before generic numbers. - Keeping placeholders intact during cloud rewrites: Cloud models occasionally reformat brackets (turning
[[PERSON_01]]into[Person 1]) when drafting a revised email or contract clause. Tightening the system instructions and enforcingfirebase/aistructuredSchemaoutput kept the exact[[CATEGORY_XX]]tokens intact for clean local restoration. - Handling Chrome local AI availability cleanly: Not every browser has Gemini Nano downloaded yet, and model names evolve quickly. I made sure the app checks local model readiness clearly and works smoothly on the free Firebase Spark tier out of the box so judges can test it right away.
Accomplishments that I'm proud of
- Verifiable privacy before anything hits the network: Step 2 checks the masked text against every item in the local vault before unlocking the Cloud button, and Step 3 lets you inspect the exact prompt sent to the cloud.
- 5-second live test: Clicking "Test With My Name" lets anyone put in their own name, see Cloud Gemini review the document using only
[[PERSON_01]], and watch their real name come back locally in Step 4. - Clean single-screen workflow: All 4 steps fit cleanly in one viewport without endless scrolling, making it easy to follow what happens locally vs. in the cloud.
What I learned
Working with firebase/ai showed me how practical it is to chain two different inference modes in a single workflow. Using ONLY_ON_DEVICE as a strict local privacy filter and PREFER_IN_CLOUD for heavy reasoning solves a real problem that neither local AI nor cloud AI solves well on its own.
What's next for ZeroLeak AI
- Image and PDF redaction (
#prompt-api-multimodal-input): Masking names and IDs on scanned documents and medical images locally before sending them to cloud vision models. - Mobile support (Android & iOS): Bringing the same local-masking + blind-cloud review flow to mobile with Firebase AI Logic.
- Custom domain presets: Let users add custom wordlists (internal codenames, client IDs, project names) to always mask locally.
Built With
- chrome-built-in-ai
- cloud-firestore
- firebase
- firebase-ai-logic
- firebase-hosting
- gemini
- gemini-nano
- react
- tailwindcss
- typescript
- vite
- web-crypto-api