Skip to content
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

Error debugging GeoDjango queries #423

Open
rodrigoolmo opened this issue Oct 23, 2013 · 8 comments · Fixed by #1426
Open

Error debugging GeoDjango queries #423

rodrigoolmo opened this issue Oct 23, 2013 · 8 comments · Fixed by #1426
Labels

Comments

@rodrigoolmo
Copy link

When debugging queries that use GeoDjango they are parsed like normal strings.
Example:

params = [u"ST_GeomFromEWKB('\\x0101000020e6100000555458045a9d16c010470d5cd47c4440'::bytea)"]
sql = 'SELECT (ST_distance_sphere("location"."point",%s)) AS "distance" FROM "location"'

What it should execute:

SELECT (ST_distance_sphere("location"."point", ST_GeomFromEWKB('\x0101000020e6100000555458045a9d16c010470d5cd47c4440'::bytea))) AS "distance" FROM "location"

What it really executes:

SELECT (ST_distance_sphere("location"."point", 'ST_GeomFromEWKB(''\x0101000020e6100000555458045a9d16c010470d5cd47c4440''::bytea)')) AS "distance" FROM "location"

I am using django-debug-toolbar version 0.9.4, Django 1.4 and PostgreSQL with PostGIS.

@aaugustin
Copy link
Contributor

I'm not sure where the problem you're describing occurs. Is it in the EXPLAIN feature?

@rodrigoolmo
Copy link
Author

Yes, in the views sql_select and sql_explain.
It raises an InternalError in cursor.execute(sql, params) and cursor.execute("EXPLAIN %s" % (sql,), params) respectively.

@aaugustin
Copy link
Contributor

I looked at this in detail today I don't understand your example at all. If it worked, it would be an SQL injection. Parameters you pass to the database are escaped. It's incorrect to pass a parameter such as "ST_GeomFromEWKB('\\x0101000020e6100000555458045a9d16c010470d5cd47c4440'::bytea)".

Here's how I have reproduced your example in a project of mine, slightly adapted because I'm using geography while you appear to be using geometry.

The following query works:

% ./manage.py dbshell
psql (9.1.4)
Type "help" for help.

test_db=> SELECT (
test_db(>     ST_Distance(
test_db(>         "generic_position"."coords"::geometry,
test_db(>         ST_GeomFromEWKB('\x0101000020e6100000555458045a9d16c010470d5cd47c4440'::bytea)
test_db(>     )
test_db(> ) AS "distance"
test_db-> FROM "generic_position"
test_db-> LIMIT 1;
     distance     
------------------
 11.1782329195295
(1 row)

The following code in a view crashes:

from django.db import connection
sql = """
SELECT (
    ST_Distance(
        "generic_position"."coords"::geometry,
        %s
    )
) AS "distance"
FROM "generic_position"
LIMIT 1;
"""
params = ["ST_GeomFromEWKB('\\x0101000020e6100000555458045a9d16c010470d5cd47c4440'::bytea)"]
cursor = connection.cursor()
cursor.execute(sql, params)

DatabaseError at /
parse error - invalid geometry
LINE 5:             'ST_GeomFromEWKB(''\x0101000020e6100000555458045...
                    ^
HINT:  You must specify a valid OGC WKT geometry type such as POINT, LINESTRING or POLYGON

The crash happens even before the code reaches the toolbar.

There might be a bug in this area, but you haven't provided enough information for me to reproduce it.

Please remember that I do not have access to your brain and your computer when reporting bugs.

@jperelli
Copy link
Contributor

I have the same problem, using django==2.1.4 and django-debug-toolbar==1.11

jieter added a commit to jieter/django-debug-toolbar that referenced this issue Dec 22, 2020
Without this fix, pushing the 'sel' or 'explain' button for a query containing some EWKB-encoded geometry 
as parameter results in this crash:
```
Internal Server Error: /__debug__/sql_explain/
Traceback (most recent call last):
  File "/Users/jieter/.pyenv/versions/obs/lib/python3.8/site-packages/django/db/backends/utils.py", line 86, in _execute
    return self.cursor.execute(sql, params)
psycopg2.errors.InternalError_: parse error - invalid geometry
LINE 1: ...ure" IN (0, 1, 5)) AND "waarneming"."geo_point" @ 'ST_GeomFr...
                                                             ^
HINT:  "ST" <-- parse error at position 2 within geometry
```

I'm not sure if this is the appropriate location in the code, but with this fix, both `sql_select` and `sql_explain` 
work without flaws.

Previous PR adding a similar fix: django-commons#1130
Fixes: django-commons#423
jieter added a commit to jieter/django-debug-toolbar that referenced this issue Oct 26, 2021
Without this fix, pushing the 'sel' or 'explain' button for a query containing some EWKB-encoded geometry 
as parameter results in this crash:
```
Internal Server Error: /__debug__/sql_explain/
Traceback (most recent call last):
  File "/Users/jieter/.pyenv/versions/obs/lib/python3.8/site-packages/django/db/backends/utils.py", line 86, in _execute
    return self.cursor.execute(sql, params)
psycopg2.errors.InternalError_: parse error - invalid geometry
LINE 1: ...ure" IN (0, 1, 5)) AND "waarneming"."geo_point" @ 'ST_GeomFr...
                                                             ^
HINT:  "ST" <-- parse error at position 2 within geometry
```

I'm not sure if this is the appropriate location in the code, but with this fix, both `sql_select` and `sql_explain` 
work without flaws.

Previous PR adding a similar fix: django-commons#1130
Fixes: django-commons#423
auvipy pushed a commit that referenced this issue Oct 26, 2021
Without this fix, pushing the 'sel' or 'explain' button for a query containing some EWKB-encoded geometry 
as parameter results in this crash:
```
Internal Server Error: /__debug__/sql_explain/
Traceback (most recent call last):
  File "/Users/jieter/.pyenv/versions/obs/lib/python3.8/site-packages/django/db/backends/utils.py", line 86, in _execute
    return self.cursor.execute(sql, params)
psycopg2.errors.InternalError_: parse error - invalid geometry
LINE 1: ...ure" IN (0, 1, 5)) AND "waarneming"."geo_point" @ 'ST_GeomFr...
                                                             ^
HINT:  "ST" <-- parse error at position 2 within geometry
```

I'm not sure if this is the appropriate location in the code, but with this fix, both `sql_select` and `sql_explain` 
work without flaws.

Previous PR adding a similar fix: #1130
Fixes: #423
tim-schilling added a commit that referenced this issue Dec 15, 2021
The stripping logic is stripping general sql parameters and the test
written for it does not run. I'm not confident that the test actually
hits the code we expect it to run either.

Closes #1543
Reopens #423
tim-schilling added a commit that referenced this issue Dec 15, 2021
The stripping logic is stripping general sql parameters and the test
written for it does not run. I'm not confident that the test actually
hits the code we expect it to run either.

Closes #1543
Reopens #423
@tim-schilling
Copy link
Member

Reopend as per #1547

@tim-schilling tim-schilling reopened this Dec 15, 2021
@jperelli
Copy link
Contributor

jperelli commented Aug 17, 2022

I was impacted by this issue here #423 (comment), used this today and I was very surprised that just worked fine!

image

I couldn't find it in the changelog but it seems you landed the fix in #1426

I think you can close this one @tim-schilling / @aaugustin

Thank you very much guys!!

@tim-schilling
Copy link
Member

@jperelli that change was reverted as it mutates data going into your database. You should upgrade versions of the toolbar.

@jperelli
Copy link
Contributor

@tim-schilling You are right, sorry abot that. I can see it now in a query that uses a geom as a parameter
image
image
image
👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
4 participants