views:

759

answers:

6

Currently Google requires you to create an API Key that is specific to the domain of where the map will be served from. How does Google enforce this? I want to do the same thing.

I expose an API for my service but want to allow clients to embed calls to the API via javascript and not just from the server. I could secure it with just a random token but of course this could be easily spoofed by anyone looking at the code on the client machine.

I always understood this concept to not be possible but somehow Google does a good job at enforcing it.

Edit - It sounds like Google really hasn't done anything amazing after all. Their API is most likely just for tracking and not really to guarantee that their API is used by the person with the key.

A: 

The API key is associated with your domain. If they don't match, an error is given. Obviously this is enforced through Google's database of such information.

wallyk
+6  A: 

I'm quite certain they use the REFERER URL to determine where the call is coming from. If the domain doesn't match what's assigned to the key, it's an invalid request.

For a practical example, using PHP you can check the domain using $_SERVER['HTTP_REFERER'] to check the referer. If the domain matches, return a valid response. If it doesn't, you can return a 401 Unauthorized or other response.

Trevor
So if a client were to make an AJAX call to our API via JavaScript I can depend on the Referer Domain to be accurate and non-spoofable?
Vyrotek
Nope. It is spoofable. I think Google more likely relies on the IP Address (can't be spoofed easily), and a DNS lookup.
Chacha102
Ah, so they take the domain URL and look up it's IP and lookup the DNS to make sure its a match. Although, you say this is technically spoofable as well?
Vyrotek
The referer sent in the request can be spoofed (http://en.wikipedia.org/wiki/Referrer_spoofing).
Trevor
It might be. Not 'completely' sure about its spoof-ability. But my guess is its more likely that Google us using the IP Address than using the REFERER.
Chacha102
The API address for an Ajax call is the IP address of the user agent of the client, that parses the document and executes the javascript. As such, the IP address of the Ajax request is unrelated to the document originating domain. The only information in the request that relates to the document domain is the referrer.Of course, the referrer can be spoofed. However, in the case of the Google API, it would be the website that wants to spoof the referrer; however, the actual spoofing can happen only on the client side in the browser. Thus, spoofing is not that big of a threat.
Franci Penov
The check is made by the GMaps Javascript code, **at the client side**. No need to check/spoof the referrer in this case...
Wim
@Wim - this is interesting. So to clarify, when you embed the GMaps javascript on your site, it is *custom* JS just for your site? That would mean Google returns a modified version of the JS for each website? Or do you think the js is doing 2 calls: one to figure out what site it should be on and one to send the data?
Brian Armstrong
Wim
Very cool, thanks...
Brian Armstrong
A: 

The reason it works is that you cannot make API calls with javascript. Browser security prevents javascript from making requests anywhere except to the domain that the javascript originated from. Because of this, any API calls from javascript need to be bounced through your server where the API key is stored (the api key is never seen by javascript).

Jeffrey Aylesworth
There are ways around this though (JSONP). I don't think its that you can't make the call, you just normally can't process the return.
Ryan Elkins
Looking at a few examples and especially http://econym.org.uk/gmap/example_map12.htm (listed as a good tutorial) it appears that typical user expose key when they script src maps api. The sourced js overwrites the page (map is a set of img). Markers are placed by downloading json data using GDownloadUrl() - this just makes a XMLHttpRequest and so is back to his server. JSONP requires support from google server, right?
mar
+2  A: 

As my comment says:

The REFERER is spoofable, so it is probably unlikely that Google will use it as a means of verification. See this wikipedia entry.

My guess is that Google probably uses the IP address of the caller along with a DNS lookup. DNS is not really spoofable, as your DNS entries have to be correct for the website to even get to you.

But, even that has its problems, because if a server uses a Round-Robin IP Address DNS setup, Google will be redirected to a different IP address when doing a DNS lookup.

From the FAQ

Note that a key for http://www.mygooglemapssite.com/ will only be accepted when the site is accessed using this address. It will not be accepted if the site is accessed by IP address (eg. http://10.1.2.3/) or by a hostname that is aliased to www.mygooglemapssite.com using a DNS CNAME record.

My guess is that it might be using the Host header that is sent when requesting the page, which would work as normally Google asks you to include it's API script directly into the page. Then that script has access to the headers for the current page and can use that to check.

My guess is backed up with the fact that it does not work for IP addresses or Aliases, which means it isn't doing a DNS check.

THIS method cannot be spoofed, as it must be the correct header to access the page. However, this means that any aliases to the domain will not work.

However, this also means that you MUST provide a Javascript library to access the code, as you can't check this server side, I believe.

Chacha102
The 'Host' header may contain the address when requesting the page, but how would I guarantee that value is also passed into the new AJAX request made by that page to my API? I thought this is basically what 'Referrer' contains which we know can be spoofed.
Vyrotek
Well, because YOU are writing the AJAX call (because you provided the Javascript library), YOU can make sure it gets sent.
Chacha102
The Reason Google can require an API Key is because they provide the Javascript library, and can then run Javascript on the page.
Chacha102
Right, but anyone can see the code loaded onto the client and make modifications to send any domain url they want. So its still not secure.
Vyrotek
+3  A: 

The API key itself is most probably a one way hash of the domain the key is associated with and a secret only the Google API server knows about. It may contain some other pieces of well-known (to Google of course) information. When you make a request from that domain, the API server takes the domain the request comes from and makes that same one way hash calculation and compares the two values.

For Ajax calls, they most probably use the referrer to get the domain of the document host. While the referrer can be spoofed, ultimately in order to use the API, you need to get Google javascript to execute in the document. At this point, this javascript can verify that indeed the document that invoked the Ajax API call originated from the target server. This is also spoofable of course, provided you have your own DOM implementation or on the fly modification of the script. However, this spoofing needs to happen on the client side and the chances that the website that wants to use Google API will be able to spoof the client software are quite small.

Note that since the API is essentially free, they could've offered anonymous access to their API as well. Apparently Google's intent is not to protect unauthorized access to it, but to ensure that they can gather as much data as possible about that data usage and be able to associate that usage with other data they've collected about the target domain. As such, I wouldn't expect the API key verification to be much more complex than what I described above - the ROI on more advanced approach is too low.

And of course there's also the concern of possible XSS attacks through their API. But I don't believe their API key is tied too much into any anti-XSS code they have.

Franci Penov
Unfortunately this sounds like the most reasonable answer. Thanks for your input.
Vyrotek
A: 

I agree with all the points that Franci Penov has listed.I would like to elaborate a little bit on using someone else's API key. Let us assume you register key1 with http://mysite.com.

1) First attempt - If anothersite.com has script src=http://www.google.com/jsapi?key=key1, google could check referrer (hash scheme mentioned) and in this case there is a mismatch. How does evil attacker overcome this - a lot of people have mentioned that referrer can be spoofed. This does not really apply here. Sure you could send arbitrary headers if you make the request but how does evil hacker spoof referrer for users on anothersite.com - this is in general not easy. There have been old versions of flash on IE 6 that allowed attacker to set arbitrary headers when making cross domain requests but in general this is not workable for script src. I am not sure if the included js does any validation of document.location to prevent this (probably not).

2) Second attempt - evil attacker copies google javascript for the API key from mysite.com page source and then embeds modified javascript on anothersite.com. Now google can't check anything (the remote IP will be the user's computer and there isn't a whole lot you or google can do).

So, if you want to for some reason keep your API key secret (one reason, malicious person can get your key blacklisted/blocked), then don't embed key in client and proxy requests via your server (your application code now has the key).

mar