CVE-2022-45786
SQL injection in AGE for PostgreSQL 11 & 12 via Golang and Python drivers due to lack of parameterization in cypher function
- CVSS 8.1
- CWE-89 Improper Neutralization of Special Elements used in an SQL Command ('SQL Injection')
- Input Validation and Sanitization
- Remote
There are issues with the AGE drivers for Golang and Python that enable SQL injections to occur. This impacts AGE for PostgreSQL 11 & AGE for PostgreSQL 12, all versions up-to-and-including 1.1.0, when using those drivers. The fix is to update to the latest Golang and Python drivers in addition to the latest version of AGE that is used for PostgreSQL 11 or PostgreSQL 12. The update of AGE will add a new function to enable parameterization of the cypher() function, which, in conjunction with the driver updates, will resolve this issue. Background (for those who want more information): After thoroughly researching this issue, we found that due to the nature of the cypher() function, it was not easy to parameterize the values passed into it. This enabled SQL injections, if the developer of the driver wasn't careful. The developer of the Golang and Pyton drivers didn't fully utilize parameterization, likely because of this, thus enabling SQL injections. The obvious fix to this issue is to use parameterization in the drivers for all PG SQL queries. However, parameterizing all PG queries is complicated by the fact that the cypher() function call itself cannot be parameterized directly, as it isn't a real function. At least, not the parameters that would take the graph name and cypher query. The reason the cypher() function cannot have those values parameterized is because the function is a placeholder and never actually runs. The cypher() function node, created by PG in the query tree, is transformed and replaced with a query tree for the actual cypher query during the analyze phase. The problem is that parameters – that would be passed in and that the cypher() function transform needs to be resolved – are only resolved in the execution phase, which is much later. Since the transform of the cypher() function needs to know the graph name and cypher query prior to execution, they can't be passed as parameters. The fix that we are testing right now, and are proposing to use, is to create a function that will be called prior to the execution of the cypher() function transform. This new function will allow values to be passed as parameters for the graph name and cypher query. As this command will be executed prior to the cypher() function transform, its values will be resolved. These values can then be cached for the immediately following cypher() function transform to use. As added features, the cached values will store the calling session's pid, for validation. And, the cypher() function transform will clear this cached information after function invocation, regardless of whether it was used. This method will allow the parameterizing of the cypher() function indirectly and provide a way to lock out SQL injection attacks.
- CVSS base score
- 8.1
- Published
- 2023-02-04
- OWASP
- A03 Injection
- Orthogonal defect classification
- Function
- Code defect classification
- Incorrect Functionality
- Category
- Input Validation and Sanitization
- Subcategory
- SQL Injection
- Accessibility scope
- Remote
- Impact
- SQL Injection
- Fixed by upgrading
- Yes
Solution
Update AGE drivers for Golang and Python to the latest versions, along with AGE for PostgreSQL 11 or PostgreSQL 12.
Vulnerable code sample
import psycopg2
def execute_cypher_query(graph_name, cypher_query):
# VULNERABLE: This code is susceptible to sql injection
conn = psycopg2.connect(database="your_database", user="your_user", password="your_password", host="localhost", port="5432")
cursor = conn.cursor()
sql = f"SELECT cypher('{graph_name}', '{cypher_query}')"
cursor.execute(sql)
results = cursor.fetchall()
cursor.close()
conn.commit()
conn.close()
return results
graph_name = "my_graph"
cypher_query = "MATCH (n) RETURN n; DROP TABLE users;"
results = execute_cypher_query(graph_name, cypher_query)
print(results)Patched code sample
import psycopg2
def execute_cypher_query(graph_name, cypher_query):
# SECURE: This version prevents sql injection
conn = psycopg2.connect(database="your_database", user="your_user", password="your_password", host="localhost", port="5432")
cursor = conn.cursor()
cursor.execute("SELECT set_cypher_params(%s, %s)", (graph_name, cypher_query))
cursor.execute("SELECT cypher()")
results = cursor.fetchall()
cursor.close()
conn.commit()
conn.close()
return results
graph_name = "my_graph"
cypher_query = "MATCH (n) RETURN n; DROP TABLE users;"
results = execute_cypher_query(graph_name, cypher_query)
print(results)Cite this entry
@misc{vaitp:cve202245786,
title = {{SQL injection in AGE for PostgreSQL 11 & 12 via Golang and Python drivers due to lack of parameterization in cypher function}},
author = {Bogaerts, Fr\'ed\'eric and Ivaki, Naghmeh and Fonseca, Jos\'e},
year = {2023},
note = {VAITP Python Vulnerability Dataset, entry CVE-2022-45786},
howpublished = {\url{https://netpack.pt/vaitp/vulnerability/CVE-2022-45786/}}
}
Introducing the "VAITP dataset": a specialized repository of Python vulnerabilities and patches, meticulously compiled for the use of the security research community. As Python's prominence grows, understanding and addressing potential security vulnerabilities become crucial. Crafted by and for the cybersecurity community, this dataset offers a valuable resource for researchers, analysts, and developers to analyze and mitigate the security risks associated with Python. Through the comprehensive exploration of vulnerabilities and corresponding patches, the VAITP dataset fosters a safer and more resilient Python ecosystem, encouraging collaborative advancements in programming security.
The supreme art of war is to subdue the enemy without fighting.
Sun Tzu – “The Art of War”
:: Shaping the future through research and ingenuity ::
