Mobile EngineeringVol. 22 · Mobile
0 / 0

Shelves / Mobile Engineering

MB·08

Mobile Security

Securing apps that run on devices you don't control: why the client is untrusted and anything shipped in it is public, secure storage with the Keychain and Keystore, sign-in for native apps with the system browser and PKCE, biometrics, TLS, network security configuration and when certificate pinning is worth its risks, deep links and exported components, WebViews, app attestation and tamper resistance, privacy and permissions, and the OWASP Mobile Application Security Verification Standard; with a lab in which an interceptor trusted by the device reads a payment request until the app pins its server's key, and an API secret is pulled out of an app bundle in a fifth of a millisecond.

17 topics · ~8 min read · 16-question quiz
Updated Suggest a correction

Start here: the attacker has your app

A server's code runs on machines you control. A mobile app's code runs on millions of devices you don't: some rooted or jailbroken, some with debugging proxies installed, some running modified copies of your app. Anyone can download your app, unpack it, read its code and strings, watch its traffic and call your API directly. So:

  • Anything in the app is public: API keys, endpoints, business logic, feature flags.
  • The server must enforce everything: authentication, authorization, prices, limits, cut-offs.
  • The app protects the user's data on the device and in transit: secure storage, TLS, careful use of platform features.

Measured

Experiment Result
an app sends a payment confirmation through an interceptor whose certificate authority the device trusts request succeeds; the interceptor reads the OTP and address
the same app with the server's public key pinned the connection is refused; nothing leaked
searching a 116 KB minified app bundle for an embedded secret key found in 0.2 ms

Trusting the device's certificate store is right for most traffic, but a user (or malware, or an employer) can add certificate authorities to it. And a secret compiled into an app is a secret for about as long as it takes to run grep.

How deep to go

Volume 17 defines six mastery levels. For mobile security:

Level What you can do
L1 Explain why the client is untrusted and what must stay on the server
L2 Use secure storage, system-browser sign-in and HTTPS correctly
L3 Explain Keystore/Keychain, PKCE, pinning, deep-link verification and attestation
L4 Find leaked secrets, insecure storage, exported components and interceptable traffic in an app
L5 Design an app's security architecture against MASVS for its risk level
L6 Run it: pin rotation, incident response, abuse detection, security reviews per release

The untrusted client

  • Server-side enforcement of everything that matters (Volume 10): authorization per object, prices and totals computed on the server, rate limits per account and device, idempotency keys.
  • No provider secrets in the app: the app calls TiffinBox's API; TiffinBox's server calls the payment provider, maps service and model providers with its own keys.
  • Assume the API is called by scripts: the lab's interceptor shows how easily requests are read and replayed; design APIs that are safe to call directly.

Storage on the device

Data Where
refresh and access tokens encrypted with a Keystore key (Android) / Keychain item (iOS), accessible only when unlocked
personal data cached for offline use app-private database; encrypted if sensitive; wiped on sign-out
non-sensitive settings DataStore / UserDefaults
never logs, clipboard, screenshots of sensitive screens (use FLAG_SECURE / hide content in the app switcher), external storage

Keys can require user authentication (biometric or device credential) to unlock, binding sensitive actions (showing saved addresses, approving a refund on the staff app) to the user's presence: BiometricPrompt with a CryptoObject, or Keychain access control with LocalAuthentication.

Authentication for native apps

  • OAuth 2.0 / OIDC authorization code flow with PKCE in the system browser (Android Custom Tabs, iOS ASWebAuthenticationSession), redirecting back to the app via a verified App Link / Universal Link (RFC 8252).
  • Passkeys (platform credentials) where supported: phishing-resistant and convenient.
  • Short-lived access tokens; rotating refresh tokens bound to the install where possible; revocation on sign-out and when the server detects reuse.

Network security

  • HTTPS only: Android's Network Security Configuration disallows cleartext by default for modern targets; iOS's App Transport Security requires TLS.
  • Certificate pinning (the lab): pin the SPKI hash of your keys, include backup keys, ship pins that expire or can be updated via a safe channel, and monitor pinning failures. Many apps choose not to pin and rely on Certificate Transparency and platform trust instead; pin when you handle high-value transactions and can operate rotation reliably.
  • Android apps targeting recent SDKs don't trust user-installed CAs by default (an interceptor needs a rooted device or a debug build that opts in); iOS users can install and trust profiles. Either way, pinning closes the gap for your own domains.

Platform attack surface

  • Deep links: use verified Android App Links and iOS Universal Links (domain-verified), and treat every deep link's parameters as untrusted input (don't let tiffinbox://refund?amount=… do anything without server checks).
  • Exported components (Android): activities, services, receivers and providers are private unless explicitly exported; exported ones validate callers and inputs.
  • WebViews: load only your content; disable JavaScript if not needed; never expose a JavaScript interface to untrusted pages; prefer Custom Tabs for external content.
  • Intents and pasteboard: avoid sending sensitive data through implicit intents or the clipboard.

Integrity and tamper resistance

  • Attestation: Google Play Integrity API and Apple App Attest / DeviceCheck let the server verify requests come from a genuine app on a device passing integrity checks; use for high-value actions (account creation, payouts to riders, promotions) and abuse prevention, as one signal among many.
  • Obfuscation (R8) and root/jailbreak or tamper detection raise effort for attackers; they're speed bumps, not walls.

Privacy

  • Collect only what features need; ask permissions in context (chapter 01); declare data use accurately in Google Play's Data safety section and Apple's privacy labels and privacy manifests.
  • Third-party SDKs (analytics, ads) collect data under your name: inventory them and their data flows.
  • Respect laws such as India's DPDP Act and GDPR (Volume 21, chapter 09): consent, purpose, retention, deletion.

The standard

OWASP's MASVS groups requirements into storage, cryptography, authentication, network, platform interaction, code quality, resilience and privacy; the MASTG (testing guide) shows how to test each. Pick the level that matches TiffinBox's risk (payments and personal data: the standard controls plus resilience for the rider payout flow), and run its checks in every release.

Common traps

  • API keys in the app for third-party services.
  • Tokens in plain storage or logs.
  • WebView sign-in.
  • Unverified deep links that perform actions.
  • Pinning without backup keys or a rotation plan.
  • Trusting client-side checks for prices or permissions.

Lab: an interceptor, a pinned key and a leaked secret

Setup. Python on the lab machine with the cryptography library and the standard ssl module.

  • Two certificate authorities: a "public" CA that issued the real server's certificate, and a "rogue" CA, standing in for a corporate inspection proxy, malware, or a user-installed profile. The "device" trusts both (as a device with an extra CA installed would).
  • A real HTTPS server (api.tiffinbox.test as localhost) accepting POST /pay.
  • An interceptor presenting a certificate from the rogue CA: it terminates TLS, records the plaintext request, and forwards it to the real server.
  • A client sending {"card_last4":"4242","otp":"731905","address":"Flat 4B, Kondapur"}, with or without pinning (comparing the SHA-256 of the server certificate's public key, its SPKI, with the expected value).

Separately, a 116 KB "minified app bundle" containing sk_live_… among thousands of lines of code was searched with a regular expression.

Results

Path Pinning Outcome Interceptor read the body?
direct to the server no 200 OK no
through the interceptor no 200 OK YES (OTP and address)
direct to the server yes 200 OK no
through the interceptor yes refused: key doesn't match the pin no
Search Result
regex sk_live_[A-Za-z0-9]{10,} over the bundle found sk_live_51TfnBx9Q3kLzR8pYw2v in 0.20 ms

What it shows

  • TLS protects against strangers, not against whoever the device trusts. With an extra trusted CA, the interception was invisible to the app and the server.
  • Pinning makes your app trust only your keys for your domains. The cost is operational: if the server's key changes without a matching pin in installed apps, they stop working until users update (chapter 09's adoption curve), so pin public keys, ship backups, and monitor.
  • Shipping a secret is publishing it. Minification and obfuscation don't hide string constants; an attacker needs one search. Keep provider keys on the server.
  • This lab simplified TLS on a single machine; real interception tools (mitmproxy, Charles) do the same thing with real devices in minutes.

Try it yourself

  1. Add a backup pin and "rotate" the server to a new key covered by it; confirm the client still connects.
  2. Configure an Android debug build's Network Security Configuration to trust user CAs, intercept it with mitmproxy, then add a <pin-set> and watch the failure.
  3. Build a release APK with R8 and search the DEX for your strings with strings or jadx.
  4. Replace the embedded key with a backend proxy endpoint and rate-limit it per signed-in user.

Revision sheet: mobile security

Key ideas

  • The client is untrusted; anything in the app is public; the server enforces everything.
  • Tokens in Keystore/Keychain-protected storage; sensitive caches private, encrypted if needed, wiped on sign-out; nothing sensitive in logs, clipboard or screenshots.
  • Sign-in via the system browser with authorization code + PKCE; passkeys; rotating refresh tokens.
  • HTTPS only; pin SPKI with backups when the risk justifies it; know platform CA trust rules.
  • Verified App Links/Universal Links; untrusted deep-link input; private components; careful WebViews.
  • Attestation (Play Integrity, App Attest) as an abuse signal; obfuscation as a speed bump.
  • Privacy: minimal data, honest declarations, SDK inventory; MASVS/MASTG as the checklist.

Numbers to remember

  • Lab: interceptor read OTP and address without pinning; pinned client refused; embedded key found in 0.2 ms.

Glossary

  • SPKI pin: a hash of a server's public key the app accepts.
  • PKCE: proof key for code exchange, protecting the authorization code.
  • App Links / Universal Links: domain-verified deep links.
  • Attestation: a platform-signed statement about an app's and device's integrity.
  • MASVS: OWASP Mobile Application Security Verification Standard.