CVE-2009-2940: PyGreSQL SQL Injection Vulnerability
HIGHCVE-2009-2940 is a HIGH sql injection vulnerability (CWE-89: Improper Neutralization of Special Elements used in an SQL Command ('SQL Injection')) affecting PyGreSQL libraries in the PyPI ecosystem. The issue involves pygresql might be vulnerable to encoding-based sql injection. Remediation is available by upgrading affected packages to version 4.1.
CVE-2009-2940 at a Glance
How to Fix CVE-2009-2940
To remediate this vulnerability, upgrade all affected PyGreSQL dependencies to a patched release. Both direct and transitive dependencies should be checked, as frameworks or security libraries may pull in vulnerable versions transitively.
pip install PyGreSQL>=Latest secure release
pip install PyGreSQL>=4.1
Run the following command in your project directory to verify that all resolved dependencies use the patched release:
pip show PyGreSQL
How to Check if You Are Affected
To determine whether your Python environment is affected by CVE-2009-2940, inspect your installed packages using pip:
pip show PyGreSQL
Analyze the terminal output to locate any version of PyGreSQL in your dependency tree. If any resolved version matches the vulnerable ranges listed in the table below (for example, versions earlier than 4.1), your application is affected.
Note: The pip show PyGreSQL command checks currently installed packages in your active Python environment.
Technical Details
Vulnerability Mechanism & Technical Analysis
This vulnerability is classified under CWE-89: Improper Neutralization of Special Elements used in an SQL Command ('SQL Injection'), where the product constructs all or part of an sql command using externally-influenced input from an upstream component, but it does not neutralize or incorrectly neutralizes special elements that could modify the intended sql command when it is sent to a downstream component. without sufficient removal or quoting of sql syntax in user-controllable inputs, the generated sql query can cause those inputs to be interpreted as sql instead of ordinary user data. In PyGreSQL, software state validation or input boundaries are circumvented during processing. Review the official source advisory statement below for specific affected components, classes, and reproduction details.
Source Advisory Statement
CVE-2009-2940 Affected PyGreSQL Versions
2 component(s)| Product / Package | Ecosystem | Affected Versions | Fixed Version |
|---|---|---|---|
|
PyGreSQL
|
PyPI |
≤ 3.8.1
|
|
|
PyGreSQL
|
PyPI |
≥ 4.0
< 4.1
All 1 versions
4.0
|
4.1
|
References
Frequently Asked Questions
What is CVE-2009-2940?
Which PyGreSQL versions are affected?
What version fixes CVE-2009-2940?
Is PyGreSQL affected?
What CWE is associated with CVE-2009-2940?
How can I check PyPI dependencies for CVE-2009-2940?
Machine-readable OSV JSON
OSV v1.5 Schema
{
"schema_version" : "1.4.0",
"id" : "GHSA-xv6x-43gq-4hfj",
"modified" : "2026-06-08T19:04:54Z",
"published" : "2022-05-02T03:40:08Z",
"aliases" : [ "CVE-2009-2940" ],
"summary" : "PyGreSQL Might Be Vulnerable to Encoding-Based SQL Injection",
"details" : "PyGreSQL 3.8 did not use PostgreSQL’s safe `string` and `bytea` functions in its own escaping functions. As a result, applications written to use PyGreSQL’s escaping functions are vulnerable to SQL injections when processing certain multi-byte character sequences. Because the safe functions require a database connection, to maintain backwards compatibility, `pg.escape_string()` and `pg.escape_bytea()` are still available, but applications will have to be adjusted to use the new `pyobj.escape_string()` and `pyobj.escape_bytea()` functions. For example, code containing:\n\n```python\nimport pg\nconnection = pg.connect(...)\nescaped = pg.escape_string(untrusted_input)\n```\nshould be adjusted to use:\n\n```python\nimport pg\nconnection = pg.connect(...)\nescaped = connection.escape_string(untrusted_input)\n```",
"severity" : [ ],
"affected" : [ {
"package" : {
"ecosystem" : "PyPI",
"name" : "PyGreSQL"
},
"ranges" : [ {
"type" : "ECOSYSTEM",
"events" : [ {
"introduced" : "0"
}, {
"last_affected" : "3.8.1"
} ]
} ]
}, {
"package" : {
"ecosystem" : "PyPI",
"name" : "PyGreSQL"
},
"ranges" : [ {
"type" : "ECOSYSTEM",
"events" : [ {
"introduced" : "4.0"
}, {
"fixed" : "4.1"
} ]
} ],
"versions" : [ "4.0" ]
} ],
"references" : [ {
"type" : "ADVISORY",
"url" : "https://nvd.nist.gov/vuln/detail/CVE-2009-2940"
}, {
"type" : "WEB",
"url" : "https://github.com/PyGreSQL/PyGreSQL/commit/8e19320b130946eed6f043297e3e4e005a523612"
}, {
"type" : "WEB",
"url" : "https://github.com/PyGreSQL/PyGreSQL/commit/f7237d773e6f4d5a7da3d99bb6bc5062bd07935e"
}, {
"type" : "PACKAGE",
"url" : "https://github.com/PyGreSQL/PyGreSQL"
}, {
"type" : "WEB",
"url" : "https://github.com/pypa/advisory-database/tree/main/vulns/pygresql/PYSEC-2009-18.yaml"
}, {
"type" : "WEB",
"url" : "http://ubuntu.com/usn/usn-870-1"
}, {
"type" : "WEB",
"url" : "http://www.debian.org/security/2009/dsa-1911"
} ],
"database_specific" : {
"cwe_ids" : [ "CWE-89" ],
"severity" : "HIGH",
"github_reviewed" : true,
"github_reviewed_at" : "2024-02-08T21:31:52Z",
"nvd_published_at" : "2009-10-22T16:30:00Z"
}
}