diff --git a/base_geoengine/expressions.py b/base_geoengine/expressions.py index 2a3deccc2..1eff2a575 100644 --- a/base_geoengine/expressions.py +++ b/base_geoengine/expressions.py @@ -51,6 +51,13 @@ def __leaf_to_sql(self, leaf, model, alias): rel_model = key[0:i] rel_col = key[i + 1 :] rel_model = model.env[rel_model] + # rel_col is interpolated into the SQL below: only accept + # the name of an actual GeoField of the related model + if not isinstance(rel_model._fields.get(rel_col), GeoField): + raise ValueError( + f"Invalid geo field {rel_col!r} on model " + f"{rel_model._name!r} in leaf {leaf!r}" + ) # we compute the attributes search on spatial rel if ref_search[key]: rel_alias = ( diff --git a/base_geoengine/tests/test_model.py b/base_geoengine/tests/test_model.py index 12faceb2a..c15570d53 100644 --- a/base_geoengine/tests/test_model.py +++ b/base_geoengine/tests/test_model.py @@ -425,6 +425,34 @@ def test_search_intersect_with_dict_id(self): ) self.assertEqual(len(result.ids), 5) + def test_search_indirect_rejects_non_geo_column(self): + """The column of an indirect geo domain key must be a GeoField""" + retails = self.env["retail.machine"] + with self.assertRaises(ValueError): + retails.search( + [ + ( + "the_point", + "geo_intersect", + {"dummy.zip.name": [("id", "!=", 0)]}, + ) + ] + ) + + def test_search_indirect_rejects_sql_in_column(self): + """SQL injected through an indirect geo domain key must not run""" + retails = self.env["retail.machine"] + with self.assertRaises(ValueError): + retails.search( + [ + ( + "the_point", + "geo_intersect", + {"dummy.zip.the_geom) OR (1=1": [("id", "!=", 0)]}, + ) + ] + ) + def test_search_intersect_for_zip_1169_with_multiple_domain(self): retails = self.env["retail.machine"] result = retails.search(