Opened 3 weeks ago

Last modified 2 days ago

#37492 assigned Cleanup/optimization

GeometryField(widget=forms.TextInput).to_python() doesn't handle GDALException from invalid GeoJSON

Reported by: Philip Sørensen Owned by: Anisayak Mitra
Component: GIS Version: dev
Severity: Low Keywords: GeoJSON
Cc: Philip Sørensen Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: no UI/UX: no

Description (last modified by Jacob Walls)

#29978 made GeometryField.to_python() catch GDALException from widget.deserialize(). However, when the widget has no deserialize() method (e.g. widget=forms.TextInput, which test_field_with_text_widget covers), to_python() calls GEOSGeometry() directly and only catches GEOSException, ValueError and TypeError. For GeoJSON input that GDAL rejects, GEOSGeometry() raises GDALException, so clean() crashes (HTTP 500) instead of raising a ValidationError:

from django import forms as django_forms
from django.contrib.gis import forms
forms.GeometryField(widget=django_forms.TextInput).clean("{}")
# GDALException: Invalid geometry pointer returned from "OGR_G_CreateGeometryFromJson".

'{"type": "Foo"}', '{"type": "Point"}' and '{"type": "Point", "coordinates": "x"}' crash the same way. This reproduces on 6.1.2, 6.1.3 and main. The default widget isn't affected, because BaseGeometryWidget.deserialize() catches GDALException.

Patch: add GDALException to the exceptions caught in the fallback.

Change History (4)

comment:1 by Philip Sørensen, 3 weeks ago

Has patch: set

comment:2 by Jacob Walls, 3 weeks ago

Description: modified (diff)
Keywords: GeoJSON added
Patch needs improvement: set
Severity: Normal → Low
Summary: GeometryField.to_python() crashes with GDALException on invalid GeoJSON when the widget has no deserialize() → GeometryField(widget=forms.TextInput).to_python() doesn't handle GDALException from invalid GeoJSON
Triage Stage: Unreviewed → Accepted

That seems fair. Setting PNI due to PR limit.

comment:3 by Jacob Walls, 3 weeks ago

Type: Bug → Cleanup/optimization

comment:4 by Anisayak Mitra, 2 days ago

Owner: set to Anisayak Mitra
Status: new → assigned

I am preparing a patch with tests for this.

Note: See TracTickets for help on using tickets.
Back to Top