Ask
26
@token_leak_tomas ·

How do I make sure my API is only called by my own app, and not by someone with a copy of the requests?

Public API behind a mobile app and a web front end. I want to ensure only my clients can call it.

So far I have tried: a secret key compiled into the app, a custom header the server checks, and checking the user-agent. Someone on my team pointed out that all three are visible to anyone who looks at the traffic, which I now understand is true.

What I would like to know:

  1. Is there a way to actually do this, or is the goal itself wrong?
  2. If a secret cannot live in a client, what is the point of API keys at all?
  3. What should I be doing instead to stop abuse, which is the real thing I care about?
4 answers Share
Report

Answering anonymously — a moderator will review it first.

  • @chargeback_charu · 5h ago

    Question 2 is a good one because API keys are widely misunderstood.

    A key in a server-to-server context is a real secret: it lives on a machine you control, and nobody else sees it. That is the case they were designed for and there they work.

    A key in a client is not a secret and was never treated as one by anyone who thought about it. What it is instead is an identifier: it tells you which application the traffic came from, so you can meter it, revoke it, and see when one integration starts behaving oddly. That is genuinely useful — it just is not authentication.

    The failure comes from treating the second one as though it were the first. A public key with a rate limit attached is fine. A public key that grants privileged access is a credential you have published.

    24
    Share
    Reply

    Answering anonymously — a moderator will review it first.

    Report
  • @cache_bust_cato · 13h ago · 2 replies

    The goal is wrong, and it is worth being blunt about it because a lot of effort gets spent here.

    Anything shipped to a client — an app binary, JavaScript, a mobile package — is in the hands of the person running it. They can read it, decompile it, or simply watch the network traffic their own device produces. There is no secret you can put in that package that stays secret, and no header you can send that cannot be replayed. Obfuscation raises the time cost from minutes to hours and changes nothing structural.

    So "only my app can call this" is not achievable. What you can achieve is "only an authenticated user can do things on their own behalf, at a rate I allow", and that is almost always what people actually needed.

    The reframe matters because the impossible version leads to weeks of work on attestation schemes that a determined person bypasses, while the achievable version is standard and effective.

    29
    Share
    Reply

    Answering anonymously — a moderator will review it first.

    Report
    • @token_leak_tomas · yesterday

      "Only an authenticated user, on their own behalf, at a rate I allow" is genuinely what I needed. I had framed it as a client identity problem when it was an authorisation and abuse problem.

      13
      Share
      Reply

      Answering anonymously — a moderator will review it first.

      Report
  • @form_field_faruk · 53m ago

    For question 3, the things that actually work, roughly in order of value per hour spent:

    Authenticate the user, authorise every request. Each request should be checked against what that user is allowed to do, server-side, every time. This is the one that matters. Most real incidents are not "someone used our API" but "someone changed an id in a request and got another customer's data".

    Rate limit per user and per address, with something stricter for unauthenticated endpoints.

    Keep the expensive and dangerous operations behind your own server. If the client never has the ability to do the costly thing directly, no amount of replaying its traffic gets there.

    Monitor for shape, not identity. Abuse looks different from use — bursts, sequential ids, odd hours, one account from many places. That is what catches the real cases.

    Platform attestation services exist and do raise the cost for casual copying. They are worth considering after the above, never instead of it.

    1
    Share
    Reply

    Answering anonymously — a moderator will review it first.

    Report