SwissDataScienceCenter / renku-data-services

Services that handle reading and writing data from a database
Apache License 2.0
3 stars 2 forks source link

Query parameters validation from sanic can fail if the query field is empty #324

Open olevski opened 1 month ago

olevski commented 1 month ago

To reproduce:

This is not very likely to occur very often but we should still fix it. Probably wrapping the sanic validator in our own decorator or just fully making our own decorator for validating query params will work.

The response from the curl request should be 422 but we just get an ambiguous 500.

This is also an opportunity for us to contribute this fix upstream to sanic.

The trace from sanic is:

Srv 0 12:38:17 ERROR:  Exception occurred while handling uri: 'http://localhost:8000/api/data/user/secrets?kind=&=null'
Traceback (most recent call last):
  File "/poetry_cache/virtualenvs/renku-data-services-xS3fZVNL-py3.12/lib/python3.12/site-packages/sanic/app.py", line 1385, in handle_request
    response = await response
               ^^^^^^^^^^^^^^
  File "/workspace/components/renku_data_services/base_api/auth.py", line 39, in decorated_function
    response = await f(request, user, *args, **kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/workspace/components/renku_data_services/base_api/auth.py", line 150, in decorated_function
    response = await f(*args, **kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/poetry_cache/virtualenvs/renku-data-services-xS3fZVNL-py3.12/lib/python3.12/site-packages/sanic_ext/extras/validation/decorator.py", line 63, in decorated_function
    await do_validation(
  File "/poetry_cache/virtualenvs/renku-data-services-xS3fZVNL-py3.12/lib/python3.12/site-packages/sanic_ext/extras/validation/setup.py", line 36, in do_validation
    validation = validate_body(validator, model, data)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/poetry_cache/virtualenvs/renku-data-services-xS3fZVNL-py3.12/lib/python3.12/site-packages/sanic_ext/extras/validation/validators.py", line 26, in validate_body
    return validator(model, body)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/poetry_cache/virtualenvs/renku-data-services-xS3fZVNL-py3.12/lib/python3.12/site-packages/sanic_ext/extras/validation/validators.py", line 47, in _validate_instance
    data = clean_data(model, body) if allow_coerce else body
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "/poetry_cache/virtualenvs/renku-data-services-xS3fZVNL-py3.12/lib/python3.12/site-packages/sanic_ext/extras/validation/clean.py", line 6, in clean_data
    return {key: _coerce(hints[key], value) for key, value in data.items()}
                         ~~~~~^^^^^
KeyError: ''
leafty commented 1 month ago

I think we can temporarily fix this with:

@schemathesis.hook
def filter_query(context: HookContext, query: dict[str, str] | None) -> bool:
    if query is not None and "" in query:
        return False
    return True