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

Remove null characters in any string passed to the container / debug task #997

Merged
merged 1 commit into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions dispatcher/backend/src/common/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from common.external import advertise_book_to_cms
from common.notifications import handle_notification
from errors.http import TaskNotFound, WorkerNotFound
from utils.check import cleanup_value
from utils.scheduling import update_schedule_duration

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -87,13 +88,13 @@ def add_to_container_if_present(
task: dbm.Task, kwargs_key: str, container_key: str
) -> None:
if kwargs_key in kwargs:
task.container[container_key] = kwargs[kwargs_key]
task.container[container_key] = cleanup_value(kwargs[kwargs_key])

def add_to_debug_if_present(
task: dbm.Task, kwargs_key: str, debug_key: str
) -> None:
if kwargs_key in kwargs:
task.debug[debug_key] = kwargs[kwargs_key]
task.debug[debug_key] = cleanup_value(kwargs[kwargs_key])

if "worker" in kwargs:
task.worker = dbm.Worker.get(session, kwargs["worker"], WorkerNotFound)
Expand Down
22 changes: 22 additions & 0 deletions dispatcher/backend/src/tests/integration/routes/tasks/test_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,3 +184,25 @@ def test_cancel_task(self, client, access_token, tasks):
}
response = client.post(url, headers=headers)
assert response.status_code == 204


class TestTaskPatch:

def test_patch_task(self, client, access_token, tasks):
for task in filter(lambda x: x["status"] in [TaskStatus.started], tasks):
url = "/tasks/{}".format(task["_id"])
headers = {
"Authorization": access_token,
"Content-Type": "application/json",
}
response = client.patch(
url,
headers=headers,
json={
"event": "scraper_running",
"payload": { # control character below must be ignored
"stdout": "some string with ignore bad \u0000character"
},
},
)
assert response.status_code == 204
15 changes: 15 additions & 0 deletions dispatcher/backend/src/tests/unit/utils/test_check.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import pytest

from utils.check import cleanup_value


@pytest.mark.parametrize(
"value, expected",
[
pytest.param("ok", "ok", id="str_simple_ok"),
pytest.param("o\u0000k", "ok", id="str_null_character"),
pytest.param(123, 123, id="int_simple"),
],
)
def test_cleanup_value(value: str, expected: str):
assert cleanup_value(value) == expected
7 changes: 7 additions & 0 deletions dispatcher/backend/src/utils/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,10 @@ def raise_if(
"""
if condition:
raise exception_class(*exception_args)


def cleanup_value(value: Any) -> Any:
"""Remove unwanted characters before inserting / updating in DB"""
if isinstance(value, str):
return value.replace("\u0000", "")
return value