From 01704bdae10ccc08e920e1192d34f0186cf66c45 Mon Sep 17 00:00:00 2001 From: Dmitrii Amelin Date: Wed, 18 Nov 2026 00:32:58 +0100 Subject: [PATCH] keep only logbook func_args in pyscript_running event The pyscript_running event carried all of func_args. Besides values that aren't JSON serializable, these can include large or sensitive data, eg, a webhook payload with an auth token and the webhook_id, or service call data once @service fires the event. The event now keeps only the func_args the logbook describes it with: trigger_type, event_type, var_name, value and trigger_time. Anything that read other func_args from this event won't see them anymore. --- custom_components/pyscript/decorator.py | 6 ++++-- custom_components/pyscript/logbook.py | 4 ++++ custom_components/pyscript/trigger.py | 4 ++-- tests/decorators/test_webhook.py | 8 +++++--- tests/test_decorator_manager.py | 9 ++++++--- 5 files changed, 21 insertions(+), 10 deletions(-) diff --git a/custom_components/pyscript/decorator.py b/custom_components/pyscript/decorator.py index cd5fbc4c..78b6eda1 100644 --- a/custom_components/pyscript/decorator.py +++ b/custom_components/pyscript/decorator.py @@ -25,6 +25,7 @@ ) from .eval import AstEval, EvalFunc, EvalFuncVar from .function import Function +from .logbook import RUNNING_EVENT_FUNC_ARGS from .state import State _LOGGER = logging.getLogger(__name__) @@ -269,8 +270,9 @@ async def _call(self, data: DispatchData) -> None: ev_name = self.name.replace(".", "_") ev_entity_id = f"pyscript.{ev_name}" - # leave out the webhook request, which isn't JSON serializable - event_func_args = {key: val for key, val in data.func_args.items() if key != "request"} + event_func_args = { + key: data.func_args[key] for key in RUNNING_EVENT_FUNC_ARGS if key in data.func_args + } event_data = {"name": ev_name, "entity_id": ev_entity_id, "func_args": event_func_args} self.hass.bus.async_fire("pyscript_running", event_data, context=data.hass_context) # Store HASS Context for this Task diff --git a/custom_components/pyscript/logbook.py b/custom_components/pyscript/logbook.py index 7d2ae99b..6e37fa32 100644 --- a/custom_components/pyscript/logbook.py +++ b/custom_components/pyscript/logbook.py @@ -8,6 +8,10 @@ _LOGGER = logging.getLogger(__name__) +# func_args kept in the pyscript_running event, which are what the logbook describes it with; +# others can be large or not JSON serializable (eg, a webhook request or service call data) +RUNNING_EVENT_FUNC_ARGS = ("trigger_type", "event_type", "var_name", "value", "trigger_time") + @callback def async_describe_events(hass, async_describe_event): # type: ignore diff --git a/custom_components/pyscript/trigger.py b/custom_components/pyscript/trigger.py index d6a0e27e..2a16e3e5 100644 --- a/custom_components/pyscript/trigger.py +++ b/custom_components/pyscript/trigger.py @@ -21,6 +21,7 @@ from .eval import AstEval, EvalFunc, EvalFuncVar from .event import Event from .function import Function +from .logbook import RUNNING_EVENT_FUNC_ARGS from .mqtt import Mqtt from .state import STATE_VIRTUAL_ATTRS, State from .webhook import Webhook @@ -1366,8 +1367,7 @@ def call_action(self, notify_type, func_args, run_task=True): ev_name = self.name.replace(".", "_") ev_entity_id = f"pyscript.{ev_name}" - # leave out the webhook request, which isn't JSON serializable - event_func_args = {key: val for key, val in func_args.items() if key != "request"} + event_func_args = {key: func_args[key] for key in RUNNING_EVENT_FUNC_ARGS if key in func_args} event_data = {"name": ev_name, "entity_id": ev_entity_id, "func_args": event_func_args} Function.hass.bus.async_fire("pyscript_running", event_data, context=hass_context) diff --git a/tests/decorators/test_webhook.py b/tests/decorators/test_webhook.py index f6ed5d39..b377e785 100644 --- a/tests/decorators/test_webhook.py +++ b/tests/decorators/test_webhook.py @@ -55,10 +55,11 @@ def webhook_no_request(payload): await webhook.async_handle_webhook(hass, "test_no_req_hook", request) await pyscript.wait_done({"a": 1}) - # the request isn't JSON serializable, so it's left out of the pyscript_running event + # only the func_args the logbook uses are kept in the pyscript_running event + await hass.async_block_till_done() assert len(running) == 2 for event in running: - assert "request" not in event.data["func_args"] + assert event.data["func_args"] == {"trigger_type": "webhook"} json_bytes(event.data) @@ -77,8 +78,9 @@ def webhook_handler_test(payload, request): response = await webhook.async_handle_webhook(hass, "handler_hook", request) assert json.loads(response.body) == {"method": "POST", "payload": {"b": 2}} + await hass.async_block_till_done() assert len(running) == 1 - assert "request" not in running[0].data["func_args"] + assert running[0].data["func_args"] == {"trigger_type": "webhook"} json_bytes(running[0].data) diff --git a/tests/test_decorator_manager.py b/tests/test_decorator_manager.py index fd8e28e6..cfd32604 100644 --- a/tests/test_decorator_manager.py +++ b/tests/test_decorator_manager.py @@ -653,6 +653,7 @@ async def test_function_decorator_manager_success_calls_result_handlers(hass): call_ast_ctx = DummyCallAstCtx(result="ok") manager.add(result_handler) hass_context = Context(id="call-parent") + func_args = {"trigger_type": "state", "var_name": "sensor.x", "value": "on", "arg1": object()} fired_events = [] def event_listener(event): @@ -662,17 +663,19 @@ def event_listener(event): with patch.object(Function, "store_hass_context") as store_hass_context: await call_function_manager( - manager, make_dispatch_data({"arg1": 1}, call_ast_ctx=call_ast_ctx, hass_context=hass_context) + manager, + make_dispatch_data(func_args, call_ast_ctx=call_ast_ctx, hass_context=hass_context), ) await hass.async_block_till_done() - assert call_ast_ctx.calls == [(manager.eval_func, None, {"arg1": 1})] + assert call_ast_ctx.calls == [(manager.eval_func, None, func_args)] assert result_handler.results == ["ok"] assert len(fired_events) == 1 assert fired_events[0].data == { "name": "file_hello_func", "entity_id": "pyscript.file_hello_func", - "func_args": {"arg1": 1}, + # only the func_args the logbook uses are kept + "func_args": {"trigger_type": "state", "var_name": "sensor.x", "value": "on"}, } store_hass_context.assert_called_once_with(hass_context)