#2311·R2R

[Security] Hard-coded JWT fallback allows impersonation when authentication is enabled

Author: 28HusCreated Sep 26, 2026Updated Sep 26, 2026

Hard-coded JWT fallback allows impersonation when authentication is enabled

Hello R2R maintainers — could you please review this authentication issue?

Summary. R2R ships the same hard-coded HS256 signing key in both crypto providers. If an authentication-enabled deployment leaves R2R_SECRET_KEY unset or empty, an unauthenticated remote attacker can forge an access token for an existing account, including the configured administrator, and act with that database user's permissions.

Severity: High when the conditions below are met. CWE-321. This is a conditional, source-confirmed finding; no live instance was tested.

Affected configuration

Verified at 9c5a94d (main, 2025-11-06).

ImageImage

The key is wNFbczH3QhUVcPALwtWZCPi0lrDlGV3P1DPRVEQCPbM. It appears in nacl.py:18 and bcrypt.py:17. Each provider selects config.secret_key or os.getenv("R2R_SECRET_KEY") or DEFAULT_*_SECRET_KEY (NaCl, bcrypt). The checked-in Docker env file leaves R2R_SECRET_KEY empty (r2r.env:8). Both providers sign and verify HS256 JWTs with the selected key (NaCl, bcrypt).

Impact and conditions

An attacker needs network access to an R2R API configured with require_authentication = true, and the deployment must use this fallback rather than a private key. The repository includes an auth-enabled config with require_authentication = true and the default administrator email [email protected] (r2r_with_auth.toml).

R2RAuthProvider.decode_token() checks the signature, expiry, blacklist, and required claims, then resolves sub to a database user. It does not verify that the token was issued through login or bind it to server-side session state (r2r_auth.py:106-145). The auth wrapper returns that database user (auth.py:122-160); the /v3/users/me route is one protected example (users_router.py:785-833). A forged sub for an existing account therefore impersonates that account. If it resolves to the seeded administrator, the request inherits the administrator permissions stored in the database.

The database lookup is a real constraint: a token cannot create a nonexistent user, and changing token claims does not independently grant a role. The default py/r2r/r2r.toml sets require_authentication = false and seeds [email protected]; it also selects bcrypt (lines 58–59). That configuration's unauthenticated-default-user behavior is separate from this finding and is already discussed in issue #2295. This report concerns deployments that enable authentication but retain the public fallback key.

Proof of concept

This offline PoC creates a token for an existing account. It was not sent to a running service.

python
import time
import jwt

key = "wNFbczH3QhUVcPALwtWZCPi0lrDlGV3P1DPRVEQCPbM"
now = int(time.time())
token = jwt.encode(
    {
        "sub": "[email protected]",
        "token_type": "access",
        "iat": now,
        "nbf": now,
        "exp": now + 3600,
    },
    key,
    algorithm="HS256",
)
print(token)

Against an authorized local auth-enabled instance, the token can be supplied to a protected endpoint such as:

bash
curl -i http://127.0.0.1:7272/v3/users/me \
  -H "Authorization: Bearer <forged-token>"

Source-based expected result: the forged signature passes verification and the request is resolved as the existing user. No runtime reproduction was performed.

Remediation

Remove the hard-coded fallback from both providers and fail startup when no R2R_SECRET_KEY is configured. Require a unique, high-entropy secret per deployment. Rotate the exposed key and invalidate tokens signed with it. Add a regression test proving startup fails when the signing key is absent.

Public reporting history

No published R2R security advisory or exact public report for this shared fallback key was found in the checked GitHub security page, issues, pull requests, and public search results. Open issue #2295 reports other authentication problems (including the unauthenticated default-admin behavior and token-type handling); it does not identify this signing-key fallback. Issue #2297 asks maintainers for a private channel for a different authorization issue. Private reports are not searchable, so this check cannot establish that no prior private submission exists.

The repository's security policy requests a confidential vulnerability report and lists [email protected]. No report has been submitted as part of this review.

Related works

These cases document related risks involving exposed defaults, forged tokens, or session integrity; they are not claims that the implementations are identical.

This report is part of my ongoing security research. If you have any questions, please feel free to reach out or tag me. I would be glad to help, and I appreciate the opportunity to make a small contribution to improving R2R's security.