Summary
SciTokens' KeyCache class previously constructed SQLite queries using Python's str.format() with user-controlled values like issuer and key_id, leading to classic SQL injection.
In environments where an attacker can influence these fields, they may execute arbitrary SQL commands against the local SciTokens SQLite database, potentially altering or exfiltrating cached key material and related metadata.
Affected context
This vulnerability appears in deployments using the SciTokens reference library where:
- The
KeyCacheclass is enabled to store and retrieve token-related keys via SQLite. - Values such as
issuerandkey_idcan be influenced, directly or indirectly, by external input (for example from token contents, configuration, or upstream services). - SQL statements are constructed dynamically rather than via parameterized APIs.
The issue is limited to the local SQLite database used by SciTokens, but its impact depends on how the cached data is used in the broader authorization and trust pipeline.
Exploit preconditions
- Attack surface: Code paths that call
KeyCachemethods which construct SQL queries usingissuer,key_id, or other externally influenced parameters. - Required access: Ability to supply or manipulate values that flow into these parameters (e.g., controlling token contents or configuration fields that become query inputs).
- Trigger point: Invocation of a KeyCache read/write/lookup operation that interpolates untrusted values into an SQL string using
str.format(). - Why exploitation is possible: The application concatenates untrusted data directly into SQL statements without parameter binding or sanitization, allowing crafted input to break out of expected query structure and inject additional SQL.
Why this happens
The core issue is the construction of SQL statements via Python string formatting functions (str.format()) with parameters derived from untrusted or partially trusted data.
This pattern directly matches CWE-89: Improper Neutralization of Special Elements used in an SQL Command ('SQL Injection'). Instead of using parameterized queries or prepared statements, the application embeds values into the SQL string itself. As a result, characters like quotes and SQL metacharacters are interpreted as part of the command rather than as data, enabling attackers to alter query logic, chain multiple commands, or bypass expected query constraints.
Potential impact
- Modification or deletion of rows in the local SciTokens SQLite database.
- Insertion of crafted records that may affect key lookup, caching behavior, or trust decisions.
- Potential exposure of sensitive cached data, such as key identifiers or metadata that could aid further attacks.
- Corruption of the local cache, causing denial-of-service within components relying on SciTokens for authorization decisions.
- In some environments, influence over which keys are treated as valid, potentially enabling token validation bypass depending on surrounding controls.
General mitigation guidance
Effective mitigation focuses on eliminating direct string-based SQL construction and tightening trust boundaries around query inputs:
- Prefer parameterized SQL queries or prepared statements for all database operations, passing dynamic values as bound parameters instead of interpolating them into the SQL string.
- Centralize database access through a minimal, well-reviewed data access layer to avoid ad hoc query construction scattered throughout the codebase.
- Treat all issuer, key identifier, and similar fields as untrusted input, even if they originate from tokens or configuration, and avoid using them to modify query structure.
- Apply the principle of least privilege to the database schema, limiting tables and operations available via the cache interface so unexpected queries have reduced impact.
- Introduce defensive coding guidelines and linting rules that prohibit use of
str.format(), f-strings, or concatenation for SQL assembly.
How teams detect issues like this
Detection typically combines code-centric and behavior-centric approaches:
- Static analysis: Configure SAST tools or custom rules to flag uses of
str.format(), f-strings, or string concatenation in proximity to SQL keywords (SELECT,INSERT,UPDATE,DELETE,FROM,WHERE, etc.). - Code review: During reviews, focus on data access layers and ensure all database calls use parameter binding, not raw string interpolation; treat any dynamic SQL construction as a high-risk pattern requiring justification.
- Automated policy enforcement: Introduce linters or CI checks that prevent direct string-formatted SQL in the repository and encourage standardized database helper APIs.
- Runtime monitoring (where applicable): For systems with more advanced observability, watch for anomalous or syntactically unusual SQL statements, such as unexpected unions or stacked commands, originating from SciTokens-related components.

