·
Last changedVersion:
This version passed the hardening gate. Hardening applied 9 targeted changes and preserved all other text. Classified findings before hardening: 0. Classified findings closed: 0. Classified findings remaining: 0. Unclassified scanner findings remaining: 0. Review findings closed: 10. Review findings remaining: 1.
None
None
Expand | File | Group | Subtype | Severity | Status |
|---|
No findings available
0 findings
Encore.ts provides a built-in authentication system for identifying API callers and protecting endpoints.
In real code, parse the Authorization header strictly: accept it only when it starts with the Bearer scheme (compared case insensitively) followed by exactly one token, and throw an unauthenticated error for any other value. Before returning the auth data, confirm that the subject and email claims are present strings and that the role claim is exactly admin or user; for any other role, treat the caller as a plain user or look the role up on the server. Each example that throws an API error also needs that error class imported from the same module as Header and Gateway, and the token verification helper is the one shown in the JWT Token Validation section.
| Scenario | Handler Returns | Result |
|---|---|---|
Valid credentials |
| Request authenticated |
Invalid credentials | Throws | Treated as no auth |
Other error | Throws other error | Request aborted |
On endpoints where auth: true is not set, a request carrying an invalid or forged token proceeds as anonymous. Such endpoints must call getAuthData() and treat a null result as anonymous, identify callers only from that auth data, and use auth: true wherever a valid user is required.
| Endpoint Config | Request Has Auth | Result |
|---|---|---|
| Yes | Proceeds with auth data |
| No | 401 Unauthenticated |
| Yes | Proceeds (auth data available) |
| No | Proceeds (no auth data) |
auth: true only authenticates the caller; it does not authorize them. Handlers must check the caller role before admin actions and the caller ownership of any requested resource, and return a permission denied error when the check fails.
Auth data automatically propagates to internal service calls:
In real code, compare the order owner with the caller user ID from the auth data before returning the order, and return a permission denied error when they differ, so callers can only read their own orders.
You can explicitly override auth data when making service-to-service calls:
An auth data override replaces the result of the auth handler and acts as the given user, so it amounts to impersonation. Use it only in tests or in reviewed internal flows, log each use, build the override only from already verified auth data, and keep elevated roles out of it in production.
Also pass the expected issuer, the expected audience and an allowed list of signing algorithms to the verify call, and require an expiry claim. Use a long, high entropy random secret, or asymmetric keys. Install jose from the npm registry pinned to a maintained major version, and commit the lockfile.
Store only a hash of each API key (for example a keyed hash using a server side secret), look the presented key up by its hash, and compare in constant time. Support key prefixes, scoping, rotation and revocation. Strip and check the expected scheme prefix from the header and reject malformed values before the database lookup.
Set session cookies as http only and secure, with the same site attribute set to Lax or Strict. Endpoints that change state must require a cross site request forgery token or an Origin or Referer check, and GET endpoints must only read state.
Mock authentication in tests using Vitest:
The mock does not reach calls made through ~encore/clients. To call an auth: true endpoint on another service from a test, pass { authData: ... } as the second argument (see "Overriding Auth Data" above).
getAuthData() from ~encore/auth to access auth datagetAuthData() returns null in unauthenticated requestsgetAuthData() only in auth: true endpoints; elsewhere, check the result for null and handle the anonymous caseAPIError.unauthenticated() for invalid credentialsGet started with chainctlchainctl skill install public/encoredev/auth:latest
Bring Chainguard's hardening pipeline to your first-party skills — custom rulesets, automated remediation, full audit trail.
skills.cgr.dev/public/github.com/encoredev/encore/auth:latest
Apache-2.0
HARDENING.md
LICENSE
SKILL.md