Closed Apreche closed 1 year ago
There is uncontrolled string transformation near https://github.com/pgspider/sqlite_fdw/blob/75183518ffc1b6d7be942a6b99136bb36e0fc9d8/deparse.c#L2601
in sqlite_deparse_const
.
@Apreche Thank you for reporting.
I can reproduce the issue on psql
.
In case of comparison operator, string is escaped correctly. But in case of IN condition, the escape procedure is not worked. (IN condition with a single value is converted to a comparison operator. So one parameter case was no problem.)
We will fix it.
Temporary, you might be able to fix it by appending the following code before the line 2943 in deparse.c.
https://github.com/pgspider/sqlite_fdw/blob/master/deparse.c#L2943
if (SQL_STR_DOUBLE(ch, true)) // New
appendStringInfoChar(buf, ch); // New
appendStringInfoChar(buf, ch); // 2493
@Apreche This issue was fixed by https://github.com/pgspider/sqlite_fdw/commit/9fd31f43b08ca64006b86d0de316222c213d0081
@Apreche , If no problem, could you close this issue?
NOTE: I first reported this bug to psycopg2 believing it was more likely to be a problem on their end. I was told to report it here instead. I will say that I have not attempted to test for this bug using any other SQL client libraries besides psycopg2. If it is a problem with the fdw, more testing using other types of clients will be necessary.
https://github.com/psycopg/psycopg/issues/513
Summary
When making a query to an SQLite foreign table that has multiple parameters inside of a set of parentheses, those parameters will fail to be escaped properly, causing errors, and potentially allowing for SQL injection. This only happens in a very specific circumstance, so I have provided many example queries to illustrate the issue as clearly as possible.
I tested this bug using the latest version of this docker image https://hub.docker.com/r/toleg/postgres_sqlite_fdw
toleg/postgres_sqlite_fdw:latest
as of 2023-02-19.Python version 3.11.2 psycopg2 version 2.9.5 sqlite verison 3.34.1
Steps to reproduce
title
.When the exact same query with the exact same parameters was sent to the normal table, the quote in the parameter was escaped and the query executed correctly. When the query is sent to the foreign table, the quote is not escaped properly, and the query fails to execute.
Expected Behavior
The query in step 9 should work exactly the same as the query in step 8. The quote in the query parameter should be properly escaped regardless of whether the table in the query is foreign or not.