الانتقال إلى المحتوى
تطبيقات الموبايلApp Architecture· 6 دقائق للقراءة

Your Mobile App Can't Keep a Secret: API Keys, Obfuscation and the Backend

Anything you ship in an APK or IPA belongs to whoever downloads it. Secret keys stay on your server; the app gets restricted keys and user tokens.

ReadIn

مقال كل يوم يفرق معاك

21 نوفمبر 2026
  • كاتب موثّق
محتويات المقال

A small team adds an AI summary feature to its Flutter app. The vendor's secret key isn't in the repository: CI passes it with --dart-define, and the release build uses --obfuscate. A few weeks later the vendor's invoice shows far more usage than the app has users, from requests that never went through the app at all. Someone pulled the key out of the APK and is using it for their own product, on the team's account.

The common misconception

The belief: an API key compiled into a release build is hidden from users, especially when the build is obfuscated. The key isn't in the source, it's turned into machine code, and nobody sees it on screen.

It sounds reasonable because each part is partly true. Keeping keys out of the repository is good practice. Release builds of Flutter apps use , so there's no readable Dart source in the package. Obfuscation does make reverse engineering slower. And most users never look inside an app. But "most users" isn't the threat model: it takes one curious person with an unzip tool.

What actually happens

The package belongs to whoever downloads it

OWASP's mobile weakness catalog describes this exact case: sensitive data "embedded in the app package (APK/IPA) and shipped with the app, where anyone who downloads the package can recover it". Android's security guidance says the same about keys: if your app's source code includes them, "it's possible for an attacker to decompile the app and find these resources."

It doesn't matter how the key got in. A --dart-define, a .env file bundled as an asset, a Gradle property or an Info.plist entry all end up inside the package. And even if you hid the bytes perfectly, the app has to send the key with each request, so anyone in control of their own phone can watch it leave.

Obfuscation renames symbols, it doesn't hide strings

Flutter's docs are blunt. Obfuscation "hides function and class names in your compiled Dart code", but it "does not encrypt resources nor does it protect against reverse engineering. It only renames symbols with more obscure names." The same page states: "It is a poor security practice to store secrets in an app."

We checked. A tiny Flutter app read a demo key from String.fromEnvironment and kept it in a PaymentsClient class. We built two release APKs, unzipped them and ran a plain text search on lib/arm64-v8a/libapp.so, the compiled Dart code. Without --obfuscate, the search found both the class name and the key. With it, the class name was gone, and the key was still there, character for character.

OWASP's advice on hardcoded secrets puts last, under "Harden Only as a Last Resort": such techniques "deter but do not prevent extraction and must not replace" the real fixes.

Not every key is a secret

Some keys are meant to be public. Firebase's docs say API keys for Firebase services "only identify your Firebase project and app" and are "OK to include in code", because access is controlled by Security Rules and App Check, not by the key. A Maps key restricted to your app is similar. But Google is clear about how far app restrictions go: for keys restricted to Android or iOS apps, "Bypassing this restriction is straightforward," so you should "also add API restrictions and monitor usage carefully."

A key that authorizes paid or sensitive work is different. A payment provider's secret key, an AI vendor's key or an admin token is a : whoever holds it can use it, and nothing about the request says it came from your app. Firebase itself warns: "never include the Gemini Developer API in the allowlist for a publicly accessible API key."

Moving the secret to the server

Dart summary_api.dart
// Built with --obfuscate --dart-define=AI_KEY=...const aiKey = String.fromEnvironment('AI_KEY');Future<String> summarize(Http http, String text) {  return http.post(    Uri.parse('https://api.ai-vendor.example/v1/summarize'),    // Ends up in libapp.so and in every request    headers: {'Authorization': 'Bearer $aiKey'},    body: jsonEncode({'text': text}),  );}

The app now sends the user's own access token to your backend. The backend checks it, applies per-user limits, and only then calls the vendor with the secret it keeps. OWASP lists this first among its fixes: a proxy or gateway that keeps "the static secret server-side rather than in the client". An extracted user token is still a problem, but it belongs to one user, it expires, and you can revoke it without shipping a new app.

Kind of keyExampleIn the app?What actually protects it
Project identifierFirebase API keyYesSecurity Rules and App Check
Restricted platform keyMaps key limited to your appYes, restrictedAPI restrictions, quotas, monitoring
Authorizing secretPayment secret key, AI vendor keyNeverStays on your server
User credentialThe signed-in user's access tokenYes, per userExpiry, revocation, server-side checks

How to apply it in your project

  1. List every key the app containsCheck --dart-define flags in CI, bundled .env and JSON assets, google-services.json, Info.plist, native code and third-party SDK setup calls.
  2. Ask what the key can do on its ownIf someone copies it, can they spend your money, read user data or act as an admin? Then it's a secret. If it only identifies your project and the real checks happen elsewhere, it can ship.
  3. Put secrets behind your APIAdd an endpoint that authenticates the user, enforces limits, and calls the third party with a key stored only on the server.
  4. Restrict the keys that shipLimit each public key to the APIs it needs, add app restrictions and quotas, and watch usage. Use separate keys per app and per environment, as Android's guidance recommends.
  5. Keep obfuscation for what it's good atTurn on --obfuscate to make your code harder to read, and back up the symbol files from --split-debug-info: Flutter's docs say you need them to de-obfuscate stack traces. Just don't count it as key protection.

Key takeaways

  • Everything inside an APK or IPA can be recovered by anyone who downloads it.
  • Flutter's obfuscation renames symbols; strings such as API keys stay readable.
  • Identifying keys can ship if they're restricted; authorizing secrets never should.
  • Put third-party secrets behind your backend and give the app short-lived, per-user tokens.
  • If a secret ever shipped, rotate it; a new release doesn't recall the old one.

📚 المصادر

  1. Obfuscate Dart code — Flutter docs
  2. Security best practices: API key management — Android Developers
  3. MASWE-0004: Sensitive Data Hardcoded in the App Package — OWASP MAS
  4. Manage API keys: add restrictions — Google Cloud
  5. Learn about using and managing API keys for Firebase — Firebase

التعليقات (0)