Summary
A SQL injection vulnerability in Ory Hydra's admin APIs (listOAuth2Clients, listOAuth2ConsentSessions, and listTrustedOAuth2JwtGrantIssuers) allows attackers who know the pagination secret to forge pagination tokens that inject arbitrary SQL into backend queries.
When these admin endpoints are exposed directly or indirectly, and the pagination or system secret is known, crafted tokens can be used to manipulate database queries and potentially compromise stored OAuth2 and OpenID Connect data.
Affected context
This vulnerability affects deployments of Ory Hydra where:
- The admin APIs
listOAuth2Clients,listOAuth2ConsentSessions, orlistTrustedOAuth2JwtGrantIssuersare reachable (directly or via intermediary services), and - Pagination tokens are accepted and processed for listing results, and
- The encryption key for these tokens (
secrets.pagination, orsecrets.systemas a fallback) is either predictable, reused across environments, shared with other components, or otherwise known to an attacker.
In such configurations, forged pagination tokens become an input channel into the SQL layer backing these admin list operations.
Exploit preconditions
- Attack surface: Ory Hydra admin APIs:
GET /admin/clientsGET /admin/oauth2/auth/sessions/consentGET /admin/trust/grants/jwt- Required access:
- Network access to a service that can call these admin endpoints (e.g., direct admin API exposure, admin panel, proxy, or internal service delegating pagination tokens).
- Knowledge of the encryption secret used for pagination tokens (
secrets.pagination, orsecrets.systemwhen pagination secret is unset). - Trigger point:
- Submission of a crafted pagination token as a request parameter to one of the affected admin APIs.
- Why exploitation is possible:
- The pagination implementation embeds unvalidated token contents into SQL queries in a manner that allows attacker-controlled data to influence query structure.
- The design assumes that possession of the encryption secret implies trustworthiness, but when the secret is known or leaked, pagination tokens become a manipulable channel for SQL injection.
Why this happens
The core issue is the combination of:
- Treating encrypted pagination tokens as trusted internal data due to their cryptographic protection, and
- Building SQL statements in a way that lets token-derived values affect the query structure rather than being strictly bound as typed parameters.
Once an attacker knows the token encryption secret, they can generate syntactically correct tokens whose decrypted content includes malicious SQL fragments. Because the application's pagination logic does not strictly separate query structure from data, these fragments are incorporated into the executed SQL. This maps to CWE-89: SQL Injection, where untrusted data is incorporated into SQL commands without proper neutralization or strict parameterization, even though the data is wrapped in a cryptographic layer.
Potential impact
- Reading, modifying, or deleting OAuth2 client metadata stored in the database
- Tampering with consent session records, potentially altering or invalidating existing user consents
- Manipulating trusted JWT grant issuers, impacting token validation and trust relationships
- Escalating further into the backing database, including pivoting to other tables depending on schema and permissions
- Denial of service against Hydra by issuing expensive or malformed SQL queries
General mitigation guidance
Effective mitigation focuses on both query construction safety and secret handling:
- Enforce strict parameterization: Ensure that all pagination-related values, including offsets, cursors, and sort keys, are bound as parameters rather than concatenated into SQL strings. Avoid dynamic query fragments derived from token content wherever possible.
- Constrain pagination data: Represent pagination state using simple, strongly-typed primitives (e.g., numeric offsets, UUIDs, or opaque cursor IDs) that map directly to columns, and validate them against a strict schema before use.
- Treat encrypted tokens as untrusted: Even if data is encrypted or signed, handle it as potentially attacker-controlled unless derived entirely within a trusted boundary. Re-validate and sanitize token contents after decryption.
- Harden secret management:
- Use cryptographically strong, random values for
secrets.paginationthat are unique per environment. - Avoid reusing
secrets.systemas a general-purpose secret; restrict its scope and rotate secrets on suspicion of exposure. - Limit who and what components can access these secrets, following least privilege.
- Limit admin API exposure: Place admin endpoints behind strong authentication, authorization, and network controls (e.g., dedicated admin networks, service-to-service auth) so that only trusted operators or services can call them.
How teams detect issues like this
Organizations can approach detection on several layers:
- Static analysis and code review:
- Flag locations where SQL queries are built from concatenated strings or where decrypted token contents influence query fragments.
- Look for pagination logic that decodes or decrypts tokens and then injects their fields directly into ORDER BY, WHERE, or LIMIT/OFFSET clauses.
- Verify that all database access patterns, especially in admin handlers, use prepared statements or query builders with strict parameter binding.
- Configuration and policy checks:
- Automatically scan Hydra configuration for weak, default, or reused secrets, especially
secrets.paginationandsecrets.system. - Enforce internal policies requiring unique, random secrets per environment and disallowing shared secrets across multiple services.
- Runtime monitoring:
- Monitor database logs for anomalous or unusually complex queries originating from admin list endpoints, particularly those containing unexpected SQL keywords or patterns in pagination-related parameters.
- Implement application-level logging around pagination token parsing and validation to detect malformed or suspicious tokens.
- Automated tooling:
- Integrate security scanners or linters into CI pipelines to enforce safe query construction practices.
- Use API gateways or WAFs with rules to detect and block obviously malicious SQL patterns in parameters, while recognizing that encrypted tokens may limit their visibility.

