dcwatson / django-pgcrypto

Python and Django utilities for encrypted fields using pgcrypto.
BSD 2-Clause "Simplified" License
67 stars 22 forks source link

Django 1.7, having trouble filtering on encrypted fields #12

Closed synic closed 8 years ago

synic commented 9 years ago

# the model
class PhoneCall(TimestampMixin, models.Model):
    from_number = pgcrypto.EncryptedCharField(max_length=100)

# the query
numbers = PhoneCall.objects.filter(from_number__exact=somenumber)
if numbers:
    # ... do stuff

And the error:

  File "/vagrant/apps/crm/utils.py", line 29, in find_company_for_number
    if numbers:
  File "/home/vagrant/.virtualenvs/eventboard/local/lib/python2.7/site-packages/django/db/models/query.py", line 145, in __nonzero__
    self._fetch_all()
  File "/home/vagrant/.virtualenvs/eventboard/local/lib/python2.7/site-packages/django/db/models/query.py", line 966, in _fetch_all
    self._result_cache = list(self.iterator())
  File "/home/vagrant/.virtualenvs/eventboard/local/lib/python2.7/site-packages/django/db/models/query.py", line 265, in iterator
    for row in compiler.results_iter():
  File "/home/vagrant/.virtualenvs/eventboard/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 700, in results_iter
    for rows in self.execute_sql(MULTI):
  File "/home/vagrant/.virtualenvs/eventboard/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 786, in execute_sql
    cursor.execute(sql, params)
  File "/home/vagrant/.virtualenvs/eventboard/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 81, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/home/vagrant/.virtualenvs/eventboard/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "/home/vagrant/.virtualenvs/eventboard/local/lib/python2.7/site-packages/django/db/utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/home/vagrant/.virtualenvs/eventboard/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
django.db.utils.InternalError: Corrupt ascii-armor
dcwatson commented 9 years ago

I can't tell what's going on from that snippet. You're filtering on number - is that defined in TimestampMixin, or was that just a typo, did you mean from_number?

synic commented 9 years ago

Sorry, yeah, it's from_number.

synic commented 9 years ago

Any updates on this one? We haven't started using pgcrypto because of this error.

dcwatson commented 9 years ago

I can't reproduce this. The unit tests (which use Django 1.7, and pass on Python 2 and 3) already include an exact lookup on an EncryptedCharField:

https://github.com/dcwatson/django-pgcrypto/blob/master/tests/core/tests.py#L61

Do you think you could put together a failing test case?

synic commented 9 years ago

I will see if I can come up with something. Thank you

On Thu, Mar 5, 2015 at 1:26 PM, Dan Watson notifications@github.com wrote:

I can't reproduce this. The unit tests (which use Django 1.7, and pass on Python 2 and 3) already include an exact lookup on an EncryptedCharField:

https://github.com/dcwatson/django-pgcrypto/blob/master/tests/core/tests.py#L61

Do you think you could put together a failing test case?

— Reply to this email directly or view it on GitHub https://github.com/dcwatson/django-pgcrypto/issues/12#issuecomment-77444496 .

dcwatson commented 8 years ago

Re-open with a test case if you can reproduce.

surinderdeveloper commented 3 years ago

In my case, I am getting this issue when the field has empty values than the filter not working for that field, and getting this issue, I was filtering an EncryptedCharField with 700 rows and one-row was with an empty string.