-
Notifications
You must be signed in to change notification settings - Fork 51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Single quote in PGCRYPTO_KEY string causes syntax error #319
Comments
PR welcome if you can figure out how to parameterize it or another solution could be to use Postgres dollar quoting. |
To avoid syntax errors caused by single quotes in the PGCRYPTO_KEY string when constructing SQL statements in django-pgcrypto-fields, you can use parameterized queries instead of concatenating strings. This will automatically escape any special characters in the input values, including single quotes. For example, instead of using a query like this:
You can use a parameterized query like this:
Alternatively, you can use Postgres dollar quoting to avoid the need for escaping special characters. This involves wrapping the SQL statement in a pair of dollar signs, and using a unique identifier to mark the beginning and end of the statement. For example:
Using parameterized queries or dollar quoting can help prevent syntax errors and improve the security and reliability of your code. Here is an example with Postgres dollar quoting:
Here we're using the string formatting capabilities of Python to insert the key value into the SQL statement. By wrapping our key value in the Here's an example of how to modify the encrypt_value method in DjangoPGPSymmetricKeyField of django-pgcrypto-fields to use parameterized queries with Postgres dollar quoting.
Note that the $$ syntax is used for Postgres dollar quoting, which allows us o embed single quotes within the SQL query without having to escape them. Using parameterized queries instead of string interpolation ensures that the query is safe from SQL injection attacks. With these modifications, you can use the DjangoPGPSymmetricKeyField in the same way as before, with the added benefit of increased security and protection against SQL injection. Here's an example of how you can use Postgres dollar quoting with the
In this example, the PGCRYPTO_KEY value is passed as a parameter to the query, and is inserted into the SQL statement using the |
It seems that proper escaping is needed when constructing SQL statements.
The text was updated successfully, but these errors were encountered: