How to validate JWTs so your API rejects tokens intended for another recipient
A valid JWT signature does not mean the token is suitable for your API. The recipient must verify the token’s integrity and claims: who issued it, who it is intended for, and whether it is currently valid. We explain how to define these rules and test the central scenario: a token signed with a trusted key but issued for another recipient. All names, data and examples below are entirely fictional; the checks are intended only for your own test environment.
Define the trust boundary first
The developer’s task is to explicitly define which tokens a particular API accepts. The trust boundary is the point at which the API validates a presented JWT and decides whether it can be used for access. The signature confirms integrity, but the consumer must also validate the token’s claims. A rule that accepts any token with a valid signature is therefore insufficient.
For the fictional reports-api, define these acceptance conditions: an allowed algorithm, a trusted key, the expected issuer iss, the intended audience aud=reports-api, and a valid lifetime. By default, access control should require and validate iss, aud and exp. These conditions form the basis of the configuration and the criteria for checking the result.
Fix the algorithm and enforce integrity verification
A JWT must have integrity protection through a signature or MAC. Unsecured tokens with alg=none must not be accepted. Set the verification algorithm through the API’s own configuration or logic: the value in the presented JWT’s header must not determine which algorithm the API uses to verify it.
The practical priority is to check the settings of the component that accepts JWTs: which algorithm is allowed and which trusted key is used to verify integrity. Then separately confirm that claim validation is mandatory. Fixing signature verification alone does not address the central issue: a correctly signed token may be intended for another recipient.
Validate the issuer and recipient separately
Validating iss requires establishing whether the issuer is trusted and is the expected owner of the signing key. Validating aud answers a different question: is the receiving API part of the token’s intended audience? Both checks are necessary; the expected issuer does not replace the expected recipient.
In our fictional integration, one test issuer issues tokens for reports-api and billing-api. Configure reports-api to expect the audience reports-api. A token with aud=billing-api must be rejected even if it has a valid signature made with the same trusted key and the expected iss. The specific fix is to require aud validation against the recipient configured for reports-api.
Define time-based acceptance conditions
The exp field must be required and validated: the current time must be before the token’s expiration time. If nbf is present, the current time must be at or after the specified start time. The nbf field itself is mandatory if the selected token profile requires it.
Document these conditions alongside the algorithm, iss and aud so the complete acceptance rule can be tested. Use controlled time and predefined exp and nbf values for testing. This makes it clear which specific time condition should cause rejection.
Compare an allowed request with a token containing a different aud
The following is an original editorial test, not an official OWASP procedure. In your own test environment, create a synthetic report and a protected operation that changes its test marker. Save the initial state. Issue your own test JWT with the allowed algorithm, a valid signature, the expected iss, aud=reports-api, and a valid lifetime.
The positive control should demonstrate the allowed result: the expected synthetic data is accessible, and the test marker actually changes after the operation. Then restore the initial state and issue a token using the same key, changing only aud to billing-api. Repeat the request. The rejection criteria are that the response contains no protected data and the marker remains at its initial value. Record the actual responses and state: an HTTP status alone does not prove that an operation was performed or rejected.
Test the remaining reasons for rejection
The following set also consists of original editorial tests. Starting from the successful positive control, separately test an invalid signature, an algorithm outside the allowed configuration, alg=none, an unexpected iss, and the absence of each mandatory field: iss, aud or exp. Use only your own test tokens and synthetic data.
Separately test an expired exp and an nbf in the future. If the profile requires nbf, add a case in which it is missing. For every variant, expect rejection with no protected data returned and no change to the test marker. Change one condition at a time, restore the initial state, and save the comparison with the positive control.
Set priorities and state the limitations
First fix the algorithm and enforce integrity verification, then require iss, aud and exp and apply the nbf conditions. The main criterion for this work is that reports-api accepts a suitable test token and rejects a correctly signed token for billing-api without producing a protected result. Successful checks confirm the observed behavior of the test environment, but do not guarantee the security of the entire API.
If you need an external assessment of your implementation, you can start with a free audit of your website or the web part of your product from CheckMyApp.