API key restrictions
A web page that shows a map or reads from Firebase in the browser ships the service's API key in code that every visitor downloads. Such a key cannot stay secret, so the provider checks more than the key on each request: which of its APIs the key may call, and whether the request comes from a website, server or app that the owner listed. These checks are the key's restrictions. A Google Cloud key that has none can be used from anywhere, against every API that is switched on in its project.12
Two kinds of restriction
On Google Cloud, an API restriction lists the services the key may call. The console and the gcloud command-line tool refuse to create a key without at least one entry, while a key created through the REST API has no restriction unless the request carries an apiTargets list.1 An application restriction says where requests may come from: named websites, IP addresses, Android apps or iOS apps. A key holds one type of application restriction at a time, so a project with a website and a mobile app needs a key for each.13 Google advises setting both.1
| Provider | What the key may do | Where it may be used from |
|---|---|---|
| Google Cloud | API restrictions: a list of APIs enabled in the project | Websites (by Referer), IP addresses, Android package and certificate, iOS bundle ID |
| Firebase | Keys it creates are limited to Firebase-related APIs | Google Cloud console, APIs & Services > Credentials |
| Stripe | Restricted key rk_live_: Read, Write or None per resource | Stable IP addresses; blocks by ASN, country or threat category |
| Mapbox | Scopes; secret scopes only on sk tokens | URL restrictions, up to 100 per token |
Website restrictions
A website restriction is a list of allowed referrers, compared with the referer header of each request. The browser fills that header with the address of the page that made the call, and page scripts are not allowed to set it.4 On Google Cloud an entry of https://example.com admits calls from that site only. An asterisk may replace a subdomain or a path, as in *.example.com, but not part of a name.1
Some browsers send only the origin on cross-origin requests, so their users cannot use a key restricted to one page's address.1 Mapbox answers a restricted token with 403: forbidden when the header is blank, which a strict Content Security Policy can cause by stripping it.5
The header is still a value that the client sends. Truffle Security calls referrer allow-listing bypassable, and Mapbox describes its URL restrictions as best-effort protection against use in other applications.25
App restrictions
An Android restriction lists package names, each with the 20-byte SHA-1 fingerprint of the app's signing certificate. An iOS restriction lists bundle IDs. The app sends these values with each request in headers: X-Android-Package and X-Android-Cert, or X-Ios-Bundle-Identifier.1 Google's documentation calls bypassing either restriction straightforward and tells developers who rely on them to add an API restriction and watch the key's usage closely.1
IP restrictions
A key that only a server uses can be tied to that server's addresses. Google accepts single IPv4 or IPv6 addresses and CIDR ranges such as 198.51.100.0/24, but not internal addresses or localhost.1 Stripe offers the same for secret and restricted keys when a service sends from fixed addresses, such as a range reserved with its host, and can also refuse requests by country, network (ASN) or threat category.6
Permissions on the key
Some providers limit what a key may do inside the account. A Stripe secret key can do anything; a restricted key starts with rk_live_ or rk_test_ and carries a Read, Write or None setting for each resource. A call outside those settings fails with an invalid request error.7 A stolen key that can only read dispute data cannot create charges or reach customer payment methods.76 Mapbox divides tokens into public pk tokens and secret sk tokens. A public token cannot be given a secret scope, and secret tokens are for server-side requests only.5 Both apply Principle of least privilege (see Over-privileged access token).
Keys that gain access later
Without an API restriction, a Google key follows its project: an API enabled later is open to the key at once. In February 2026 Truffle Security reported that turning on the Generative Language API (Gemini) in a project let the keys already in it call Gemini endpoints, without warning. Among them were Maps keys that Google had told developers to paste into HTML.2 In the November 2025 Common Crawl, a public archive of scraped web pages, the researchers found 2,863 live keys of this kind, some on Google's own websites. A key copied from page source got a 200 response instead of a 403 from the Gemini files endpoint, which exposed uploaded files and cached data and let the caller run up usage charges.2
Google first judged the behaviour intended, reclassified the report as a bug on 2 December 2025, and on 12 December shared a plan that included blocking exposed keys from the Gemini API.2 The Firebase documentation says to keep the Gemini Developer API off the allowlist of any public key and to give Maps or Places a separate key.8 The billing side is covered in Denial of wallet, LLMjacking and Spending limits and budget alerts.
Setting restrictions
In the Google Cloud console, open the Credentials page and select the key. Under API restrictions, choose Restrict key, tick the APIs the key is for and select Save. Under Application restrictions, choose the type and add each website, address or app.1 The same changes with gcloud:
gcloud services api-keys update KEY_ID \
--allowed-referrers="www.example.com/*"
gcloud services api-keys update KEY_ID \
--allowed-ips="198.51.100.0/24"The referrers given replace the key's existing list, so every allowed referrer goes into one command.1 Securing a Firebase application and Securing Stripe payments give the steps for those two providers.
The data behind a key needs its own controls. Firebase keys restricted to Firebase services need not be treated as secrets, because Security Rules and App Check guard the data.8 Google also advises sending a key in the x-goog-api-key header, since a ?key= parameter exposes it to anything that scans URLs, and passing calls that need a real secret through the server.9 See Personal data in URLs and Privileged key in client-side code.
In SecFlash
SecFlash reports a Google or Firebase key found in google-services.json, GoogleService-Info.plist or the app config as SF-SEC-729 on the Security page. Keys meant to be public that appear in a web page, such as a Firebase config, Google Maps or Mapbox tokens and Stripe publishable keys, show as SF-EXP-306, and the same kinds of key in a mobile build as SF-EXP-613, both on the Exposure page.
See also
- Abuse of leaked API keys
- Denial of wallet
- Privileged key in client-side code
- Over-privileged access token
- Spending limits and budget alerts
- Rotating API keys at each provider
- Secrets management
- Category: Secrets
References
- a b c d e f g h i j k Google Cloud. "Manage API keys". Authentication documentation.
- a b c d e Leon, Joe (25 February 2026). "Google API Keys Weren't Secrets. But then Gemini Changed the Rules.". Truffle Security.
- back Yankulin, Leonid (22 May 2026). "Securing Your Gemini and Google API Keys". Google Cloud Blog. Google.
- back MDN contributors (n.d., read 22 November 2026). "Referer header". MDN Web Docs.
- a b c Mapbox (n.d., read 22 November 2026). "Access tokens". Mapbox Accounts documentation.
- a b Stripe (n.d.). "Best practices for managing secret API keys". Stripe Docs.
- a b Stripe (n.d.). "Restricted API keys". Stripe Docs.
- a b Google (n.d.). "Learn about using and managing API keys for Firebase". Firebase documentation.
- back "Best practices for managing API keys". Google Cloud Documentation. Google.
External links
- Best practices for managing API keys, Google Cloud documentation