Skip to main content
CVE Vulnerability Database
Vulnerability Database/CVE-2026-65321

CVE-2026-65321: PyAthena SQL Injection Vulnerability

CVE-2026-65321 is a SQL injection flaw in PyAthena that allows attackers to inject arbitrary SQL through improper quote-escaping. This post covers technical details, affected versions, impact, and mitigation steps.

Updated:

CVE-2026-65321 Overview

CVE-2026-65321 is a SQL injection vulnerability in PyAthena versions prior to 3.35.4. The flaw resides in DefaultParameterFormatter.format(), which routes DELETE and CTAS (CREATE TABLE AS SELECT) statements through the _escape_hive function. This function backslash-escapes single quotes instead of doubling them. Because Amazon Athena and Trino do not interpret backslashes as escape characters inside string literals, attacker-supplied quotes terminate the literal prematurely. Unauthenticated attackers can inject arbitrary SQL, exfiltrate data via UNION SELECT, execute destructive statements, and control CTAS destination and content. The vulnerability is tracked under [CWE-89].

Critical Impact

Unauthenticated SQL injection enables data exfiltration, destructive query execution, and attacker-controlled CTAS output in applications using PyAthena as a database driver.

Affected Products

  • PyAthena versions prior to 3.35.4
  • Applications using PyAthena's DefaultParameterFormatter for DELETE statements
  • Applications using PyAthena's DefaultParameterFormatter for CTAS statements

Discovery Timeline

  • 2026-08-02 - CVE-2026-65321 published to NVD
  • 2026-08-03 - Last updated in NVD database

Technical Details for CVE-2026-65321

Vulnerability Analysis

PyAthena is a Python DB API 2.0 client for Amazon Athena. The library exposes DefaultParameterFormatter to build parameterized queries. When formatting parameters for DELETE and CTAS statements, the formatter delegates to _escape_hive, which escapes single quotes by prepending a backslash. Athena (Presto/Trino) SQL grammar does not honor backslashes as escape characters inside string literals. The correct escape strategy doubles single quotes (''). The mismatch means user-controlled input can close a string literal and append arbitrary SQL clauses.

Root Cause

The root cause is improper input neutralization in pyathena/formatter.py. The _escape_hive function uses backslash-escaping semantics that do not match the target SQL dialect. Because backslashes are treated as literal characters by Trino string parsers, an input value containing a single quote breaks out of the intended string context and is parsed as SQL syntax.

Attack Vector

An unauthenticated attacker supplies input containing a single quote followed by SQL syntax to any application parameter that flows into a PyAthena DELETE or CTAS statement. The parser terminates the string literal early and executes attacker-controlled SQL. Exploitation paths include UNION SELECT for data exfiltration, destructive DML/DDL, and CTAS statements that redirect output to attacker-chosen locations with attacker-chosen content.

python
 from __future__ import annotations
 
 import logging
+import re
 import textwrap
 import uuid
 from abc import ABCMeta, abstractmethod

Source: pyathena-dev/PyAthena commit 27901d1 — the patch introduces stricter regex-based escaping in pyathena/formatter.py and replaces backslash-escaping with dialect-correct quote doubling.

Detection Methods for CVE-2026-65321

Indicators of Compromise

  • Athena or Trino query logs containing unexpected UNION SELECT clauses appended to application-generated DELETE or CTAS statements.
  • CTAS statements writing to S3 destinations that are not part of the application's configured output paths.
  • Application logs showing parameter values containing single quotes followed by SQL keywords such as UNION, DROP, SELECT, or -- comment terminators.

Detection Strategies

  • Inspect CloudTrail athena:StartQueryExecution events and correlate submitted QueryString values against expected application query templates.
  • Enable Athena query history review and alert on queries containing anomalous keywords or unusually long parameter bodies originating from PyAthena-based services.
  • Perform static analysis of application code for PyAthena DELETE and CTAS statements using string interpolation or DefaultParameterFormatter prior to 3.35.4.

Monitoring Recommendations

  • Continuously monitor Athena workgroup query logs for UNION SELECT, INFORMATION_SCHEMA access, and CTAS output locations outside approved prefixes.
  • Alert on new or unexpected S3 write destinations created by CTAS operations tied to application IAM roles.
  • Track PyAthena package version across all Python runtime environments and flag installations pinned below 3.35.4.

How to Mitigate CVE-2026-65321

Immediate Actions Required

  • Upgrade PyAthena to version 3.35.4 or later in all applications and CI/CD pipelines.
  • Audit application code for DELETE and CTAS statements built with DefaultParameterFormatter and validate all user-controlled inputs.
  • Rotate credentials and review Athena query history for signs of exploitation across the affected exposure window.

Patch Information

The fix is delivered in PyAthena 3.35.4 via commit 27901d12245ea722b3b4e211c60e2ade4e7c8efd. The patch replaces backslash-escaping in _escape_hive with dialect-correct escaping. See the GitHub Security Advisory GHSA-xwj5-g6cv-4r5c and the VulnCheck advisory for additional detail.

Workarounds

  • Enforce strict allow-list validation on all parameter values that flow into PyAthena DELETE or CTAS statements until the upgrade is deployed.
  • Restrict the IAM role used by PyAthena clients to the minimum required Athena, Glue, and S3 permissions to reduce the blast radius of injection.
  • Route Athena workloads through a review layer that rejects queries containing unexpected UNION, --, or ; sequences originating from application-generated SQL.
bash
# Upgrade PyAthena to the patched release
pip install --upgrade 'PyAthena>=3.35.4'

# Verify installed version
python -c "import pyathena; print(pyathena.__version__)"

Disclaimer: This content was generated using AI. While we strive for accuracy, please verify critical information with official sources.

Default Legacy - Prefooter | Experience the World’s Most Advanced Cybersecurity Platform

Experience the Most Advanced Cybersecurity Platform

See how the world’s most intelligent, autonomous cybersecurity platform can protect your organization today and into the future.