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

Boefje settings integer handling #108

Closed
zcrt opened this issue Jan 10, 2023 · 5 comments · Fixed by #280 or #939
Closed

Boefje settings integer handling #108

zcrt opened this issue Jan 10, 2023 · 5 comments · Fixed by #280 or #939
Assignees
Labels
bug Something isn't working

Comments

@zcrt
Copy link
Contributor

zcrt commented Jan 10, 2023

Describe the bug
An "integer" setting in a boefje will only work if it is set after the boefje is enabled. If it is set before enabling the boefje the following error is generated:

HTTPError at /kat-alogus/plugins/boefje/<boefje-id>/False/

404 Client Error: Not Found for url: http://katalogus:8000/v1/organisations/_dev/repositories/LOCAL/plugins/<boefje-id>

Request Method: 	POST
Request URL: 	http://localhost:8000/kat-alogus/plugins/boefje/<boefje-id>/False/
Django Version: 	3.2.16
Exception Type: 	HTTPError
Exception Value: 	

404 Client Error: Not Found for url: http://katalogus:8000/v1/organisations/_dev/repositories/LOCAL/plugins/<boefje-id>

Exception Location: 	/usr/local/lib/python3.8/site-packages/requests/models.py, line 1021, in raise_for_status
Python Executable: 	/usr/local/bin/python3
Python Version: 	3.8.16
Python Path: 	

['/app/rocky',
 '/usr/local/lib/python38.zip',
 '/usr/local/lib/python3.8',
 '/usr/local/lib/python3.8/lib-dynload',
 '/usr/local/lib/python3.8/site-packages']

The Katalogus will throw the error: boefjes.katalogus.storage.interfaces.SettingsNotConformingToSchema:

Failed validating 'type' in schema['properties']['TOP_PORTS']:
    {'description': 'Scan TOP_PORTS most common ports. Defaults to 250.',
     'title': 'TOP_PORTS',
     'type': 'integer'}

On instance['TOP_PORTS']:
    '33'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/starlette/middleware/exceptions.py", line 64, in __call__
    await self.app(scope, receive, sender)
  File "/usr/local/lib/python3.8/site-packages/fastapi/middleware/asyncexitstack.py", line 21, in __call__
    raise e
  File "/usr/local/lib/python3.8/site-packages/fastapi/middleware/asyncexitstack.py", line 18, in __call__
    await self.app(scope, receive, send)
  File "/usr/local/lib/python3.8/site-packages/starlette/routing.py", line 680, in __call__
    await route.handle(scope, receive, send)
  File "/usr/local/lib/python3.8/site-packages/starlette/routing.py", line 275, in handle
    await self.app(scope, receive, send)
  File "/usr/local/lib/python3.8/site-packages/starlette/routing.py", line 65, in app
    response = await func(request)
  File "/usr/local/lib/python3.8/site-packages/fastapi/routing.py", line 235, in app
    raw_response = await run_endpoint_function(
  File "/usr/local/lib/python3.8/site-packages/fastapi/routing.py", line 163, in run_endpoint_function
    return await run_in_threadpool(dependant.call, **values)
  File "/usr/local/lib/python3.8/site-packages/starlette/concurrency.py", line 41, in run_in_threadpool
    return await anyio.to_thread.run_sync(func, *args)
  File "/usr/local/lib/python3.8/site-packages/anyio/to_thread.py", line 31, in run_sync
    return await get_asynclib().run_sync_in_worker_thread(
  File "/usr/local/lib/python3.8/site-packages/anyio/_backends/_asyncio.py", line 937, in run_sync_in_worker_thread
    return await future
  File "/usr/local/lib/python3.8/site-packages/anyio/_backends/_asyncio.py", line 867, in run
    result = context.run(func, *args)
  File "/app/boefjes/./boefjes/katalogus/routers/plugins.py", line 83, in update_plugin_state
    p.update_by_id(repository_id, plugin_id, organisation_id, enabled)
  File "/app/boefjes/./boefjes/katalogus/dependencies/plugins.py", line 146, in update_by_id
    self._assert_settings_match_schema(organisation_id, plugin_id)
  File "/app/boefjes/./boefjes/katalogus/dependencies/plugins.py", line 182, in _assert_settings_match_schema
    raise SettingsNotConformingToSchema(organisation_id, plugin_id, e.message) from e
boefjes.katalogus.storage.interfaces.SettingsNotConformingToSchema: ('_dev', 'nmap', "'33' is not of type 'integer'")

because the integer is seen as a string.

To Reproduce
Steps to reproduce the behavior:

  1. Go to the Nmap boefje
  2. Set a value for TOP_PORTS
  3. Enable the boefje
  4. See error

To get it to work, first enable the boefje and set the setting afterwards. This is not an option for boefjes with required settings.

Expected behavior
No error

OpenKAT version
1.4 (with make update)

Additional context
See also minvws/nl-kat-boefjes#43 (comment) and minvws/nl-kat-rocky#91

@zcrt zcrt added the bug Something isn't working label Jan 10, 2023
@underdarknl
Copy link
Contributor

Hm, the value is a string, as we don't turn it into an integer anywhere. For now we should probably change the schema for this boefje to ask for a string.
Better schema support is on the table if you ask me.

@noamblitz
Copy link
Contributor

Why not just store an int?

@underdarknl
Copy link
Contributor

The data is send to us over http (thus its a string). When storing these we currently do not check the schema for what type of data it should have been, (but should) and try to cast the user input to that type of data. Since we currently don't store the whole json that the schema expects we would need to implement this casting and checking ourselves, where the correct route would be to just except a fully formed json blob which either adheres to the json schema (easily checked with readily available libraries), or not. Casting can then be done in the frontend where we would need to render the form with the correct input types anyway.

@zcrt
Copy link
Contributor Author

zcrt commented Feb 28, 2023

@ammar92 shouldn't the relevant integer boefjes be updated and tested before closing?

@ammar92
Copy link
Contributor

ammar92 commented Feb 28, 2023

@zcrt Apparently GitHub auto-closed this issue, I'll reopen it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
5 participants