Is there a way to secure an API key on a frontend page?



My service allow any HTML documents to be converted to PDF using a POST request.
It is mostly used on the backend of my client’s server and thus, the API key used for the communication is kept private.

Now, I’m thinking of a way to let my client’s visitors be able to call my service on behalf of my client API key, without exposing this secure API Key.

My main issue here is security. If my client add an XHR POST requests that contains the API key, someone can take that API key and use it for their own purpose and abusing my client’s account.

I could filter by domain, but this is easily spoofed so it’s not possible.

I was wondering if there was a way to call a private service and be identified without risking its identity to be stolen, from the client (‘s client) side?


If you’re providing this sublet for authenticated users, then it’s fairly trivial to give them unique keys (something that hashes their user ID or session against the API key and an initial timestamp, and checks it / logs it / looks for brutes before accessing the API). If you’re doing it on the open web, without any kind of user authentication, then rate limiting gets very tricky indeed. Generally you’d want to use a combination of session hashes, IP address, operating system and browser data to create an anonymous profile that gets a temporary key on the frontend. One fairly solid way to do this is to force users through a CAPTCHA before serving a temporary key that allows them a limited number of uses of the permanent key. Any user whose ip/browser/session matches the existing attributes of a known client key is shunted to that one (and gets to skip the CAPTCHA); anyone who doesn’t match an existing profile gets the CAPTCHA. That makes you a less attractive target for spoofing. On top of that, you should always rate-limit the entire thing, within a reasonable number of hits per day based on what kind of traffic you expect (or can afford), just so you don’t have any surprises. This is the minimal security you’d want if your client’s money is on the line every time their API key is used. It will require a simple database to store these “profiles”, track usage, check for brutes and maintain the currently valid client keys. Client keys should always be expired regularly – either with a time diff against when they were created, or a regular cron process, or a maximum number of uses, etc.

One other thing I frequently do is rate-limit based on a curve. If I think 5 uses per minute is reasonable, for example, then after 5 uses in a minute from a session, each usage adds a delay of a fraction of a second * the number of uses in the last minute, squared, before the data is served.

The best answer would be to put this all behind a login system and secure that.

Answered By – joshstrike

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More