TL;DR: JWTs let JavaScript apps pass identity and authorization data between systems without shared state, but the article stresses that safe use depends on verifying signatures, enforcing issuer and audience checks, and managing JWKS-based rotation correctly. The security boundary is validation logic, not token structure, and weak checks turn bearer tokens into replayable access.
At a glance
What this is: This is a practical guide to creating, sending, decoding, and validating JWTs in JavaScript, with a strong focus on signature verification, claim enforcement, and key rotation.
Why it matters: It matters because JWT handling sits on the boundary between human, workload, and service identity, and weak validation can turn a routine auth pattern into a standing access risk across IAM, NHI, and application security programmes.
By the numbers:
- Only 44% of developers are reported to follow security best practices for secrets management, exposing a significant developer behaviour gap.
- When AWS credentials are exposed publicly, attackers attempt access within an average of 17 minutes, and as quickly as 9 minutes in some cases.
👉 Read WorkOS's guide to JWT handling and validation in JavaScript
Context
JWTs are compact bearer credentials that move identity and claims between systems without a database lookup, which makes them useful in distributed JavaScript applications but unforgiving when validation is loose. The primary governance issue is not token generation itself, but whether the application treats decoded data as trusted identity evidence.
For identity teams, JWTs sit at the intersection of application authentication, API trust, and workload access. A token that is signed but weakly checked can behave like a reusable access pass, which is why issuer, audience, expiration, algorithm, and key management need to be treated as identity controls, not implementation details.
Key questions
Q: How should security teams validate JWTs in JavaScript applications?
A: Use a cryptographic verification step, not simple decoding, before any token claims are trusted. Require the expected signing algorithm, verify the issuer and audience, and reject expired or malformed tokens. In practice, the verification layer should sit in shared middleware so every protected route enforces the same identity check.
Q: Why do JWTs create risk when used as bearer tokens?
A: Bearer tokens are reusable by anyone who holds them, so leakage immediately becomes replay risk. That means transport security, short expiration, and careful storage matter as much as signature validation. If a token can be copied from logs, browser storage, or a URL, it should be treated as an exposed credential.
Q: What do teams get wrong about JWT claim validation?
A: They often treat a successfully decoded token as proof of identity, even though decoding only exposes data and does not prove trust. Claims such as issuer, audience, and expiry must be checked after signature verification, otherwise a valid-looking token can be accepted in the wrong application or context.
Q: Should organisations use JWKS for JWT key rotation?
A: Yes, when tokens are verified across multiple services or environments. JWKS centralises public key distribution, uses kid to identify the right key, and makes rotation safer than manual PEM distribution. The key requirement is overlap, so old keys stay available until all issued tokens signed with them have expired.
Technical breakdown
JWT signature verification and algorithm enforcement
A JWT is only trustworthy if the verifier confirms the signature with the expected key and the expected algorithm. In JavaScript, that means using a library such as jose to validate the token cryptographically rather than simply decoding it. Algorithm confusion appears when code accepts whatever the header says instead of constraining verification to the algorithm the system is designed to trust. That creates room for forged or misrouted tokens to be accepted as valid identity assertions.
Practical implication: enforce a fixed allowed algorithm set and reject any token that does not match the expected signing method.
Claims validation for issuer, audience, and expiry
Standard claims such as iss, aud, exp, nbf, and iat define where a token came from, who may accept it, and how long it should remain valid. Validation failures matter because a token can be syntactically correct yet still be outside the trust boundary of the API that receives it. In practice, claim checks are what stop a valid token from one context being reused in another context where it was never intended to apply.
Practical implication: validate critical claims on every protected endpoint and keep token lifetimes short enough to limit replay risk.
JWKS, kid, and key rotation in distributed JavaScript systems
JWKS lets services fetch current public keys from a central endpoint and use kid values to select the right key for verification. That avoids manual key distribution and reduces the chance that different services drift onto different trust material. Rotation only works safely when new keys are published before signing begins and retired keys remain available until all valid tokens signed with them expire. Without that overlap, verification failures can become a service availability issue as well as a security issue.
Practical implication: use JWKS with controlled overlap windows and test key retirement before rotation reaches production.
Breaches seen in the wild
- MongoBleed breach — MongoBleed exposed secrets across 87K MongoDB servers.
- Shai Hulud npm malware campaign — Shai Hulud campaign: npm malware exposed secrets on GitHub.
Read our 52 NHI Breaches Analysis report for a comprehensive view of breaches impacting Non-Human Identities including AI Agents.
NHI Mgmt Group analysis
JWT validation is an identity control, not a coding convenience. The article shows that the real security decision happens at verification time, where signature, algorithm, and claim checks determine whether a token represents trusted identity or just decoded data. That makes JWT handling part of the access-control boundary for both human and machine-authenticated workflows. Practitioners should treat every verifier as an identity enforcement point, not a parsing utility.
Bearer-token design makes JWTs sensitive to replay, leakage, and misplaced trust. A valid JWT can be reused anywhere the receiving system accepts it, which means transport, storage, and expiry are identity governance decisions as much as application decisions. This is especially relevant where service-to-service access and user sessions coexist, because the same token model can span workload identity and human IAM. The implication is that token scope must be narrow enough that reuse does not become durable access.
JWKS centralises trust material, but it also makes rotation discipline visible. The article’s emphasis on kid and published public keys shows that distributed verification depends on consistent lifecycle handling across services. That aligns with OWASP-NHI and NIST CSF thinking: key exposure, stale keys, and weak retirement processes become governance failures, not merely implementation bugs. Practitioners should treat key rotation as a standing control, not an emergency task.
JWT misuse often begins where teams confuse encoding with confidentiality. The payload is readable to anyone holding the token, so sensitive claims inside JWTs turn a credential into a data exposure vehicle. That is a named governance problem, not a formatting issue, because it expands the blast radius of any token leak. The practical conclusion is that JWT payload design must be constrained to non-sensitive claims that can safely travel with the bearer credential.
From our research:
- The average estimated time to remediate a leaked secret is 27 days, despite 75% of organisations expressing strong confidence in their secrets management capabilities, according to The State of Secrets in AppSec.
- Only 44% of developers are reported to follow security best practices for secrets management, exposing a significant developer behaviour gap.
- See also Guide to the Secret Sprawl Challenge for how secret exposure and fragmentation change remediation discipline.
What this signals
JWT governance is converging with broader secrets discipline. If teams cannot reliably validate, rotate, and retire token material, they are managing bearer credentials with the same fragility that drives secret sprawl. The lesson for programmes is to treat token issuance and verification as a lifecycle problem, not just an API implementation choice.
The operational signal is that identity teams need shared control points between application security and IAM. A JWT that is accepted by multiple services without uniform verification creates inconsistent trust boundaries, which is why validation logic, key rotation, and claim policy need central oversight rather than ad hoc implementation.
With 43% of security professionals concerned about AI systems learning and reproducing sensitive information patterns from codebases, token design has to stay conservative about what it carries. That aligns with the broader NHI posture: minimise credential surface, keep payloads non-sensitive, and anchor the trust model in verifiable identity rather than encoded convenience.
For practitioners
- Enforce verification before authorization Require jwtVerify or equivalent signature validation before any claim is used for access decisions. Treat decode-only flows as debugging only, and block routes that accept unsigned, expired, or issuer-mismatched tokens.
- Lock down allowed algorithms and claim checks Configure each verifier to accept only the algorithm you issue, then require strict issuer, audience, expiration, not-before, and issued-at checks on every request path.
- Adopt JWKS-based key distribution Publish public keys through a JWKS endpoint, use kid to select active keys, and keep retired keys available until all tokens they signed have expired.
- Keep sensitive data out of token payloads Limit claims to non-sensitive identity and authorization context. Do not place secrets, passwords, or high-risk personal data in the payload because it is only encoded, not encrypted.
- Test failure paths with malformed tokens Add tests for wrong iss, wrong aud, expired tokens, tampered payloads, missing claims, and the none-style algorithm edge case so the verifier fails closed under pressure.
Key takeaways
- JWTs are safe only when verification, not decoding, is treated as the trust boundary.
- Short-lived claims, strict algorithm enforcement, and JWKS-based rotation are the controls that keep bearer tokens from becoming durable access.
- Token payloads should stay free of sensitive data because encoded claims expand the blast radius of any credential leak.
Standards & Framework Alignment
This section maps relevant standards and security frameworks to the operational risks and controls described in this guidance.
OWASP Non-Human Identity Top 10 address the attack and risk surface, while NIST CSF 2.0 and NIST Zero Trust (SP 800-207) set the governance and control requirements practitioners need to meet.
| Framework | Control / Reference | Relevance |
|---|---|---|
| OWASP Non-Human Identity Top 10 | NHI-03 | JWTs rely on secure key rotation and credential handling across services. |
| NIST CSF 2.0 | PR.AC-1 | JWT verification governs which identities can be authenticated and trusted. |
| NIST Zero Trust (SP 800-207) | SC-12 | Distributed JWT trust depends on controlled key distribution and verification. |
Apply strict access validation rules at every verifier and reject tokens that fail issuer or audience checks.
Key terms
- Json Web Token: A compact, signed token that carries identity and claim data between systems. In practice, its security comes from verification of the signature and claims, not from the token being difficult to read, because the payload is usually encoded rather than encrypted.
- Jwks: A JSON Web Key Set is a published collection of public keys used to verify signed tokens. It lets distributed services fetch the current verification key centrally, which simplifies rotation and reduces the risk that different systems drift onto different trust material.
- Bearer Token: A credential that grants access to whoever presents it. Because possession is enough, bearer tokens must be protected in transit and stored carefully, and they should have short lifetimes so a leak does not become long-lived access.
- Algorithm Confusion: A validation failure where software trusts the algorithm named in the token header instead of enforcing the expected signing method. This matters because an attacker may be able to exploit loose verification logic to get an invalid or forged token accepted as authentic.
Deepen your knowledge
JWT validation, claim enforcement, and key rotation are core topics in our NHI Foundation Level course, the industry's only accredited NHI security programme. If you are building application identity controls for JavaScript services, it is worth exploring.
This post draws on content published by WorkOS: How to handle JWT in JavaScript. Read the original.
Published by the NHIMG editorial team on 2026-01-14.
NHI Mgmt Group — the independent authority on Non-Human Identity, IAM, and Agentic AI security. nhimg.org