CVE-2009-2940: PyGreSQL SQL Injection Vulnerability

HIGH
Advisory Identifier: GHSA-xv6x-43gq-4hfj
CWE-89
Published
Advisory Summary

CVE-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

CVE Identifier CVE-2009-2940
GHSA Identifier GHSA-xv6x-43gq-4hfj
Severity HIGH
CVSS v3.1 Base Score N/A
CWE Classification CWE-89: Improper Neutralization of Special Elements used in an SQL Command ('SQL Injection')
Affected Product PyGreSQL
Package Ecosystem PyPI
Published Date
Last Updated

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.

PyGreSQL fixed: Latest secure release
pip install PyGreSQL>=Latest secure release
PyGreSQL fixed: 4.1
pip install PyGreSQL>=4.1
Verify Direct and Transitive Dependencies

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

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: ```python import pg connection = pg.connect(...) escaped = pg.escape_string(untrusted_input) ``` should be adjusted to use: ```python import pg connection = pg.connect(...) escaped = connection.escape_string(untrusted_input) ```

CVE-2009-2940 Affected PyGreSQL Versions

2 component(s)
Fixed Versions:
4.1
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?
CVE-2009-2940 is a security vulnerability in PyGreSQL involving PyGreSQL Might Be Vulnerable to Encoding-Based SQL Injection under CWE-89: Improper Neutralization of Special Elements used in an SQL Command ('SQL Injection').
Which PyGreSQL versions are affected?
Affected versions include releases prior to 4.1 across affected packages: PyGreSQL.
What version fixes CVE-2009-2940?
Upgrading to 4.1 (or the latest patched release for your respective branch) resolves the vulnerability.
Is PyGreSQL affected?
Yes, PyGreSQL is affected in releases prior to Latest secure release, 4.1. Upgrade your dependency to Latest secure release, 4.1 or later.
What CWE is associated with CVE-2009-2940?
CVE-2009-2940 is classified under CWE-89: Improper Neutralization of Special Elements used in an SQL Command ('SQL Injection') (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.).
How can I check PyPI dependencies for CVE-2009-2940?
Run the dependency tree command: 'pip show PyGreSQL'. This command lists direct and transitive dependencies in your build configuration so you can verify if a vulnerable version is resolved.
Machine-readable OSV JSON OSV v1.5 Schema
Normalized schema output
{
  "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"
  }
}