Test a Google API key found in client code with one request per API to see if it is restricted

Leaked secrets and API keysExploitation

Keys in page source or JS bundles that start with AIza are often meant for Maps only, but the key is just an identifier. What decides impact is which APIs the project enabled for it and whether a restriction (HTTP referrer, IP, or API list) is set. Developers often skip the restriction because the key "is public anyway".

Send one plain request per API, with no Referer header, and read the JSON status:

code
curl -s 'https://maps.googleapis.com/maps/api/geocode/json?address=test&key=<key>'
curl -s 'https://maps.googleapis.com/maps/api/staticmap?center=0,0&zoom=1&size=100x100&key=<key>' -o /dev/null -w '%{http_code}\n'

"status": "OK" or a 200 image means the key is unrestricted for that API. REQUEST_DENIED with a message that the API is not authorized, or that referrer restrictions apply, means it is locked down. Repeat with -H 'Referer: https://<target-host>/' to tell a referrer-restricted key from an API-restricted one, since a Referer header is trivially spoofed outside a browser.

What to report: a key that works from curl for a billable API, which lets anyone run up the owner's quota and cost. A Maps-only key that is correctly restricted, or one that only loads map tiles on the site, is not worth reporting. Make one request per API only, never loop, and stay within the program's scope.

Follow #api-keys, #google-api-key, #maps or #secrets for new tips like this one, on your notifications page and, if you choose, in a daily or weekly email.

Comments

No comments yet.