Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
fix: strip NUL bytes from JSON columns before persisting (#6091)
Shuchang Zhengc35b47bbf792
diff --git a/skyvern/forge/agent.py b/skyvern/forge/agent.pyindex f791caf47..a9043eaa6 100644--- a/skyvern/forge/agent.py+++ b/skyvern/forge/agent.py@@ -2828,7 +2828,8 @@ class ForgeAgent:)try:- skyvern_frame = await SkyvernFrame.create_instance(frame=working_page)+ if skyvern_frame is None:+ skyvern_frame = await SkyvernFrame.create_instance(frame=working_page)html = await skyvern_frame.get_content()_ctx = skyvern_context.current()# Encode once to fix the html_bytes char-vs-byte mismatch and avoid adiff --git a/skyvern/forge/sdk/db/utils.py b/skyvern/forge/sdk/db/utils.pyindex 08960ac0e..f2980beb8 100644--- a/skyvern/forge/sdk/db/utils.py+++ b/skyvern/forge/sdk/db/utils.py@@ -221,11 +221,27 @@ ACTION_TYPE_TO_CLASS = {}+def _scrub_nul_chars(obj: typing.Any) -> typing.Any:+ # PG text/jsonb cannot store NUL; strip it pre-serialization so strings that+ # spell out the escape sequence (\u0000) are not affected.+ if isinstance(obj, str):+ return obj.replace("\x00", "") if "\x00" in obj else obj+ if isinstance(obj, dict):+ return {_scrub_nul_chars(k): _scrub_nul_chars(v) for k, v in obj.items()}+ if isinstance(obj, list):+ return [_scrub_nul_chars(item) for item in obj]+ if isinstance(obj, tuple):+ return tuple(_scrub_nul_chars(item) for item in obj)+ return obj++@typing.no_type_checkdef _custom_json_serializer(*args, **kwargs) -> str:"""Encodes json in the same way that pydantic does."""+ if args:+ args = (_scrub_nul_chars(args[0]),) + args[1:]return json.dumps(*args, default=pydantic.json.pydantic_encoder, **kwargs)diff --git a/tests/unit/test_output_parameter_size_guards.py b/tests/unit/test_output_parameter_size_guards.pyindex 8e5e5d6b4..c7d148fd5 100644--- a/tests/unit/test_output_parameter_size_guards.py+++ b/tests/unit/test_output_parameter_size_guards.py@@ -6,10 +6,11 @@ Covers the three helpers added by the fix:- ``_trim_branch_evaluations`` / ``_cap_debug_field`` (Layer 3: DecisionBlock filter)"""+import jsonfrom typing import Anyfrom unittest.mock import patch-from skyvern.forge.sdk.db.utils import truncate_oversized_jsonb_value+from skyvern.forge.sdk.db.utils import _custom_json_serializer, truncate_oversized_jsonb_valuefrom skyvern.forge.sdk.workflow.models.block import (DECISION_BLOCK_FIELD_MAX_BYTES,_cap_debug_field,@@ -205,3 +206,30 @@ def test_trim_branch_evaluations_caps_matched_branch_rendered_expression() -> Nodef test_trim_branch_evaluations_handles_empty_or_none() -> None:assert _trim_branch_evaluations(None) is Noneassert _trim_branch_evaluations([]) == []+++def test_custom_json_serializer_strips_nul_in_default_ascii_mode() -> None:+ """PG text/jsonb cannot store NUL — default ensure_ascii=True emits the 6-char \\u0000 escape."""+ serialized = _custom_json_serializer({"key\x00with\x00nul": "value\x00with\x00nul", "nested": ["a\x00b"]})+ assert "\\u0000" not in serialized+ assert "\x00" not in serialized+ assert json.loads(serialized) == {"keywithnul": "valuewithnul", "nested": ["ab"]}+++def test_custom_json_serializer_strips_nul_in_ensure_ascii_false_mode() -> None:+ """Same scrub when ensure_ascii=False emits literal NUL bytes instead of escapes."""+ serialized = _custom_json_serializer({"a": "hi\x00there"}, ensure_ascii=False)+ assert "\x00" not in serialized+++def test_custom_json_serializer_passes_clean_payload_unchanged() -> None:+ serialized = _custom_json_serializer({"a": "clean", "b": [1, 2, 3]})+ assert serialized == '{"a": "clean", "b": [1, 2, 3]}'+++def test_custom_json_serializer_preserves_literal_unicode_escape_strings() -> None:+ """Regression: only actual NUL bytes get stripped, not user strings that literally spell out an escape."""+ literal_six_chars = "\\u0000"+ payload = {"x": literal_six_chars, "y": "abc" + literal_six_chars + "def"}+ serialized = _custom_json_serializer(payload)+ assert json.loads(serialized) == payload