-
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
__in
type queries do not work with encrypted fields?
#435
Comments
It is possible that the __in query is not working with encrypted fields because the django-pgcrypto-fields library does not support encrypting lists or arrays by default. To modify the library to automatically encrypt lists as well, you may need to modify the pgcrypto/fields.py file to add support for encrypting and decrypting lists or arrays. This may involve converting the list to a compatible data type, such as a string or JSON object, before encrypting it, and then converting it back to a list after decrypting it. Once you have modified the library to support encrypting lists, you should be able to use the __in query with encrypted fields in the same way as with unencrypted fields. However, it is important to note that encrypting large lists or arrays can have a significant impact on performance, and may not be suitable for all use cases. It looks like the behavior you're seeing is indeed intended. When using encrypted fields with Django, any filtering or querying has to happen post-decryption, as the encrypted data is stored in the database in an unreadable form. In the case of your first code snippet, the encrypt function is used to encrypt the account number before it is stored in the database. When querying for a specific account number, the value you pass to the filter method is converted to the encrypted format using the decrypt and convert_from functions, and then compared to the stored (encrypted) value in the database. However, in the second code snippet where you're trying to use One possible solution to this would be to create a custom query method that can handle an encrypted list of values. This would involve decrypting the stored values in the database, and then comparing them to a list of decrypted values. However, this approach may be slower and more resource-intensive than a standard Django query, so it should be used with caution. Here's an example solution based on the django-pgcrypto-fields library that allows for encryption and decryption of lists or arrays:
With this custom field, you can create a model that includes an EncryptedListField to store an encrypted list of values. Here's an example:
To use this field in your Django queries, you can construct the query as follows:
This will return all instances of the MyModel class where the encrypted my_list` field contains the values "item1" and "item2". Note that when using encrypted fields in Django, it's important to keep in mind the performance implications of decrypting and comparing data for queries. Also, ensure that your data values fit within the size limits for the encryption function used, and consider the security implications of the data being stored in a decrypted form somewhere in your application. |
Hey there! Thanks so much for releasing this for free, so far it's been working pretty well for us.
We have
Working code + the query it generates:
Not working code + query it generates
It seems like the
__in
query has removed the encryption "stuff?" Is this intended, is it possible that we augment this to automatically encrypt lists as well?The text was updated successfully, but these errors were encountered: