@@ -322,7 +322,7 @@ def query(self, offset=0, limit=10, resulttype='results',
322322 LOGGER .debug ('processing properties' )
323323
324324 attribute_filter = ' and ' .join (
325- map (lambda x : f'{ x [0 ]} = \' { x [1 ]} \' ' , properties )
325+ map (lambda x : f'{ x [0 ]} = { sanitize_attribute_value ( x [1 ]) } ' , properties ) # noqa
326326 )
327327
328328 LOGGER .debug (attribute_filter )
@@ -410,7 +410,9 @@ def get(self, identifier, crs_transform_spec=None, **kwargs):
410410 LOGGER .debug (f'Fetching identifier { identifier } ' )
411411 layer = self ._get_layer ()
412412
413- layer .SetAttributeFilter (f"{ self .id_field } = '{ identifier } '" )
413+ identifier2 = sanitize_attribute_value (identifier )
414+
415+ layer .SetAttributeFilter (f'{ self .id_field } = { identifier2 } ' )
414416
415417 ogr_feature = self ._get_next_feature (layer , identifier )
416418 result = self ._ogr_feature_to_json (
@@ -902,3 +904,25 @@ def _ignore_gdal_error(inst, fn, *args, **kwargs) -> Any:
902904 """
903905 value = getattr (inst , fn )(* args , ** kwargs )
904906 return value
907+
908+
909+ def sanitize_attribute_value (value ) -> str :
910+ """
911+ Sanitize an attribute value used in an
912+ OGR layer SetAttributeFilter function
913+
914+ :param value: `str` of attribute value
915+
916+ :returns: `str` of sanitized attribute value
917+ """
918+
919+ if value is None :
920+ return 'NULL'
921+
922+ if isinstance (value , bool ):
923+ return '1' if value else '0'
924+
925+ if isinstance (value , (int , float )):
926+ return f"'{ value } '"
927+
928+ return "'" + str (value ).replace ("'" , "''" ) + "'"
0 commit comments