JWT authentication code can look deceptively simple.
A request arrives. The application extracts a token, verifies its signature, attaches the decoded claims to the request, and continues processing.
At first glance, the security review seems equally simple:
Is the JWT properly verified?
That is an important question, but it is only the beginning.
A stronger application security review asks a much broader set of questions:
- Where does untrusted data enter the application?
- At what point does the application begin trusting it?
- What does cryptographic verification actually guarantee?
- What assumptions are being made about the surrounding architecture?
- What happens if an attacker obtains a legitimate token?
- Where is authorization actually enforced?
Those questions turn a review of a few lines of authentication code into an examination of the application's trust model.
Start With What the Code Actually Says
Consider a common JWT authentication function:
const jwt = require("jsonwebtoken");
function authenticate(req, res, next) {
const auth = req.headers.authorization;
if (!auth) {
return res.status(401).send("Missing token");
}
const token = auth.replace("Bearer ", "");
try {
const decoded = jwt.verify(token, process.env.JWT_SECRET);
req.user = decoded;
next();
} catch {
return res.status(401).send("Invalid token");
}
}Before looking for vulnerabilities, first establish what the code actually does.
The function retrieves the authorization header from the request. If the header is absent, it returns a 401 response.
It then removes the Bearer prefix from the header value and passes the resulting token to:
jwt.verify(token, process.env.JWT_SECRET)If verification succeeds, the returned payload is assigned to req.user, and next() is called. If verification fails and an exception is thrown, the function returns another 401.
That is what we can observe.
Notice what we have not established.
We have not established which routes use this function.
We have not established whether every sensitive route uses it.
We have not established whether it is registered globally.
We have not even established from this snippet alone that it is actually used anywhere.
Its req, res, and next parameters and its call to next() strongly resemble a familiar middleware pattern. That supports an inference about its intended purpose.
But inference is not evidence.
This distinction matters far beyond JWTs:
Separate observations from inferences, and separate both from conclusions.
A security reviewer should be comfortable saying, "I cannot conclude that from the code I have."
That is not uncertainty caused by weak technical knowledge. It is disciplined analysis.
Find the Trust Boundary
One of the most useful questions when reviewing authentication code is:
Where does trust change?
Initially, the Authorization header is attacker-controlled input.
Anyone capable of sending a request can provide something like:
Authorization: Bearer eyJhbGciOi...
The mere existence of a token provides no reason to trust its contents.
The important operation is:
const decoded = jwt.verify(token, process.env.JWT_SECRET);Successful verification changes the application's security posture toward the token.
Before verification:
JWT
↓
Untrusted input
After successful verification:
JWT
↓
Signature verification
↓
Verified claims
The application now has cryptographic evidence concerning the integrity and origin of those claims.
That does not mean every security question has been answered.
It means one particular security control succeeded.
That distinction is crucial.
Decode and Verify Are Not the Same Operation
Variable names can create surprisingly dangerous shortcuts during code review.
Consider:
const decoded = jwt.verify(token, process.env.JWT_SECRET);It is easy to see the variable name decoded and mentally jump to "the application decodes the token."
But the important operation is not the name of the variable.
It is the function being called.
A useful code-review habit is to read the expression from the operation outward:
jwt.verify(...)
↓
return value
↓
decoded
The variable could just as easily have been named:
const banana = jwt.verify(token, process.env.JWT_SECRET);The security behavior would not change.
This sounds trivial, but code review relies heavily on pattern recognition, and pattern recognition can betray us. Familiar variable names, frameworks, architectures, and function signatures encourage the brain to autocomplete what it expects to see.
Security review requires resisting that impulse.
What Does a JWT Signature Actually Guarantee?
The statement:
"JWTs are signed, so users can't change anything."
contains an important truth.
It is also dangerously incomplete.
A properly implemented signature provides integrity. An attacker should not be able to modify the signed contents without causing verification to fail.
If a token contains:
{
"sub": "123",
"role": "user"
}changing it to:
{
"sub": "123",
"role": "admin"
}should invalidate the signature.
The signature can also provide confidence that the token was signed by the expected trusted signer when the verification configuration is correct.
But a signature does not provide every security property an application needs.
| Security property | Provided by the JWT signature? |
|---|---|
| Integrity | Yes |
| Authenticity of the signer | Yes |
| Confidentiality | No |
| Proof that the current holder is the legitimate user | No |
That final distinction is where threat modeling becomes interesting.
Bearer Means Bearer
Imagine an attacker steals a legitimate user's valid JWT.
The attacker does not necessarily need to forge anything.
They already possess a valid credential.
They may simply present it:
Authorization: Bearer <stolen-token>
If the application accepts bearer tokens without an additional mechanism tying them to their legitimate holder, it may have no way to distinguish:
Legitimate user
↓
presents valid JWT
from:
Attacker
↓
presents stolen valid JWT
The cryptography can work perfectly in both cases.
The signature is valid.
The token has not been modified.
The claims are exactly what the trusted issuer signed.
And the application can still be dealing with an attacker.
This reveals an important security principle:
Integrity of a credential is not proof of legitimate possession.
A stolen bearer token can generally allow the attacker to impersonate the identity represented by that token for as long as the application continues accepting it.
Token Theft Is the Beginning of the Threat Model
Once we assume an attacker possesses a valid token, asking whether they can forge a JWT becomes less interesting.
They do not need to.
The more useful question becomes:
What can this identity reach?
Suppose the stolen token belongs to User 123.
The attacker may now be able to perform whatever actions User 123 is authorized to perform.
But consider an endpoint such as:
GET /users/456/profile
Authorization: Bearer <valid-token-for-user-123>
The JWT can be completely valid.
Authentication can work exactly as designed.
If the application fails to verify that User 123 is permitted to access User 456's profile, the application may still expose the resource.
The cryptography did not fail.
Authorization failed.
This is why perfect token verification cannot compensate for broken object-level authorization.
The practical impact of a stolen token therefore depends on several factors:
- Which identity the token represents
- The privileges associated with that identity
- Token expiration
- Revocation mechanisms
- Whether refresh tokens were also compromised
- Whether tokens can be replayed
- Which application routes accept the token
- Whether authorization is correctly enforced
- Whether horizontal access to other users' resources is possible
- Whether vertical privilege escalation is possible
The threat model becomes:
Stolen JWT
↓
Impersonation of token owner
↓
Authorization determines reachable resources
↓
Token lifecycle determines duration
That is considerably more useful than stopping at "the signature is valid."
Claims Are Not Permissions
Another important distinction is between claims and permissions.
A JWT might contain:
{
"sub": "123",
"role": "admin",
"scope": ["orders:read"]
}Those values are claims.
They are assertions contained in the token and signed by its issuer.
They are not authorization enforcement.
The application might take a verified claim and evaluate it:
if (req.user.role === "admin") {
// allow administrative action
}Now application logic is making an authorization decision.
A useful mental model is:
A claim is data. A permission is a rule the application enforces.
The JWT may provide information used by the authorization system, but it does not itself decide whether a particular operation should be permitted.
Authentication Gets You to the Door
This leads to one of the most important distinctions in application security.
Authentication establishes identity.
Authorization determines allowed actions.
Authentication might tell the application:
This request represents User 123.
Authorization must still answer:
May User 123 perform this operation?
And sometimes:
May User 123 perform this operation on this specific resource?
A useful analogy is a building.
Authentication determines who is standing at the entrance.
Authorization determines which doors that person is allowed to open.
Passing the first doorway does not grant unrestricted access to every room in the building.
So Where Should Authorization Live?
The answer is not simply "inside the JWT" or "inside the endpoint."
A robust architecture generally requires multiple layers.
The JWT may carry identity and authorization-related context such as roles or scopes.
A centralized middleware or policy layer may enforce broad requirements:
User must be authenticated.
User must have an administrative role.
Token must contain the required scope.
Then endpoint or service-level logic can enforce resource-specific rules:
Does this order belong to this user?
Can this user modify this particular record?
Is this account permitted to perform this operation?
That separation matters because broad role checks cannot necessarily enforce object ownership.
For example:
JWT claim:
User 123 is a customer.
Shared policy:
Authenticated customers may access /orders.
Resource authorization:
Order 456 must belong to User 123.
That final check is what prevents one authenticated customer from simply changing an identifier and requesting another customer's order.
Authorization is therefore not something authentication finishes.
It remains an ongoing security decision throughout the request path.
Review the Assumptions Around the Control
Once the authentication function itself is understood, the AppSec review should expand outward.
The code assumes or raises questions about several things that are not established by the snippet.
Header Parsing
The function performs:
auth.replace("Bearer ", "")That raises questions about how strictly the expected authentication scheme is validated.
What happens with malformed input?
Authorization: Bearer
What about:
Authorization: Basic something
Or simply:
Authorization: abc
The reviewer should determine whether the application requires the intended Bearer <token> structure rather than assuming the input follows it.
Verification Configuration
The presence of jwt.verify() is encouraging, but it is not the end of the review.
Relevant questions include:
- Which signing algorithms are accepted?
- Is the expected algorithm explicitly restricted?
- Are issuer and audience validated where appropriate?
- Is expiration enforced as intended?
- Which JWT library and version are being used?
Secret Management
The code references:
process.env.JWT_SECRETThat leads to another investigation path.
How strong is the secret?
Where is it stored?
Is the same secret shared across environments?
How is it rotated?
Who or what can access it?
Token Lifecycle
Even a perfectly signed token creates risk if its lifecycle is poorly controlled.
A reviewer should investigate:
- How long are tokens valid?
- What happens after logout?
- Can tokens be revoked?
- What happens when an account is disabled?
- What happens when a user's privileges change?
- Are refresh tokens used?
- What happens if a refresh token is stolen?
Route Coverage
Perhaps the most important architectural question is also one of the easiest to overlook:
Where is this authentication function actually used?
A beautifully implemented authentication control protects nothing if a sensitive route never invokes it.
The review therefore needs to locate its call sites and determine:
- Which routes are protected?
- Which routes are intentionally public?
- Are sensitive routes missing authentication?
- Is enforcement centralized or individually configured?
- Can routing or execution order bypass the expected control?
This brings the review back to where it started.
Do not assume the security architecture from a familiar-looking function.
Verify it.
Cryptography and Application Security Solve Different Problems
JWT implementations demonstrate a broader lesson that applies well beyond JWTs:
You can have perfect cryptography and still have a vulnerable application.
Cryptography may tell you that signed data has not been altered and came from the expected signing authority.
Application logic must decide what that identity is actually permitted to do.
A request can therefore contain:
- A legitimate token
- A valid signature
- Authentic signed claims
- An unexpired credential
and still be an unauthorized request.
That is not a contradiction.
It means the cryptographic control and the authorization control answer different security questions.
The Bigger Code-Review Lesson
JWT review is ultimately an exercise in reasoning about trust.
A strong reviewer does not merely search for insecure functions or known vulnerability patterns.
They trace the system:
Where does input enter?
↓
What do I actually know about it?
↓
Where is it validated or verified?
↓
Where does the application begin trusting it?
↓
What exactly has been established?
↓
What security decisions happen next?
↓
Where is authorization enforced?
↓
What assumptions still need verification?
And throughout that process, they distinguish:
Observation
↓
Inference
↓
Verified conclusion
That discipline prevents a surprisingly common security-review failure: analyzing the application you expect to exist instead of the application the evidence actually shows you.
Final Takeaway
The interesting question is not:
"Does this application use JWTs correctly?"
It is:
"What security properties does this JWT implementation establish, where does the application begin relying on those properties, and what security decisions remain afterward?"
A signature can establish integrity.
Verification can establish confidence in signed claims.
Claims can provide authorization context.
None of those things eliminate the need for application-level authorization.
And none of them prove that the person holding a bearer token is the person who was originally given it.
That is why strong AppSec reviews do not stop when the cryptography succeeds.
That is where the next set of questions begins.