Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
fix: replace CodeQL-flagged prompt regex in workflow script service [SKY-8311] (#5063)
Marc Kelechava0ad769fa7a92
diff --git a/fern/openapi/skyvern_openapi.json b/fern/openapi/skyvern_openapi.jsonindex 6a5039842..7e87856d8 100644--- a/fern/openapi/skyvern_openapi.json+++ b/fern/openapi/skyvern_openapi.json@@ -12564,8 +12564,7 @@"enum": ["free","hobby",- "pro",- "enterprise"+ "pro"],"title": "PlanTier","description": "Self-serve subscription tiers. Enterprise customers use manual billing (bypass)."diff --git a/skyvern/services/workflow_script_service.py b/skyvern/services/workflow_script_service.pyindex 72f37c097..c60020fe7 100644--- a/skyvern/services/workflow_script_service.py+++ b/skyvern/services/workflow_script_service.py@@ -2,7 +2,7 @@ import base64import hashlibimport reimport urllib.parse-from typing import Any+from typing import Any, NamedTupleimport structlogfrom cachetools import TTLCache@@ -494,9 +494,99 @@ _FOR_LOOP_RE = re.compile(# or `:nth-child(2)` don't truncate the match — and we avoid a regex whose# nested quantifiers would cause exponential backtracking._PAGE_CLICK_START_RE = re.compile(r"await page\.click\(")-# Matches a prompt='...' or prompt="..." keyword argument inside a function call.-# Uses (?:\\.|…) to skip backslash-escaped quotes so apostrophes don't truncate the match.-_PROMPT_KWARG_RE = re.compile(r"""prompt\s*=\s*(['"])((?:\\.|(?!\1).)*?)\1""", re.DOTALL)+++class _PromptKwargMatch(NamedTuple):+ start: int+ end: int+ quote: str+ value: str+++def _find_string_literal_end(text: str, start: int, quote: str) -> int | None:+ """Return the closing quote index for a quoted string, or ``None`` if unterminated."""+ pos = start + 1+ escaped = False++ while pos < len(text):+ ch = text[pos]+ if escaped:+ escaped = False+ elif ch == "\\":+ escaped = True+ elif ch == quote:+ return pos+ pos += 1++ return None+++def _is_identifier_char(ch: str) -> bool:+ return ch == "_" or ch.isalnum()+++def _find_quoted_prompt_kwarg(text: str) -> tuple[_PromptKwargMatch | None, bool]:+ """Find a quoted ``prompt=...`` kwarg with a deterministic single-pass scan.++ Returns ``(match, False)`` when a quoted prompt kwarg is found, ``(None, False)``+ when no such kwarg exists, and ``(None, True)`` when scanning encounters an+ unterminated string and the call should be left untouched.+ """+ i = 0+ while i < len(text):+ ch = text[i]++ if ch in ("'", '"'):+ string_end = _find_string_literal_end(text, i, ch)+ if string_end is None:+ return None, True+ i = string_end + 1+ continue++ if not text.startswith("prompt", i):+ i += 1+ continue++ prev_char = text[i - 1] if i > 0 else ""+ next_idx = i + len("prompt")+ next_char = text[next_idx] if next_idx < len(text) else ""+ if (prev_char and _is_identifier_char(prev_char)) or (next_char and _is_identifier_char(next_char)):+ i += 1+ continue++ value_start = next_idx+ while value_start < len(text) and text[value_start].isspace():+ value_start += 1+ if value_start >= len(text) or text[value_start] != "=":+ i += 1+ continue++ value_start += 1+ while value_start < len(text) and text[value_start].isspace():+ value_start += 1+ if value_start >= len(text):+ return None, True++ quote = text[value_start]+ if quote not in ("'", '"'):+ i += 1+ continue++ string_end = _find_string_literal_end(text, value_start, quote)+ if string_end is None:+ return None, True++ return (+ _PromptKwargMatch(+ start=i,+ end=string_end + 1,+ quote=quote,+ value=text[value_start + 1 : string_end],+ ),+ False,+ )++ return None, Falsedef _find_click_calls(text: str) -> list[tuple[int, int, str]]:@@ -619,15 +709,17 @@ def _patch_static_clicks_in_block(body: str) -> str:# Append current_value context to the prompt so the LLM knows which item to target# Look for an existing prompt= kwarg and append to it- prompt_match = _PROMPT_KWARG_RE.search(patched)+ prompt_match, malformed_prompt = _find_quoted_prompt_kwarg(patched)+ if malformed_prompt:+ continueif prompt_match:- quote = prompt_match.group(1)- original_prompt = prompt_match.group(2)+ quote = prompt_match.quote+ original_prompt = prompt_match.value# Use an f-string so current_value is evaluated at runtime.# Escape existing braces so they are literal in the f-string.escaped_prompt = original_prompt.replace("{", "{{").replace("}", "}}")new_prompt = f"prompt=f{quote}{escaped_prompt} Target: {{current_value}}{quote}"- patched = patched[: prompt_match.start()] + new_prompt + patched[prompt_match.end() :]+ patched = patched[: prompt_match.start] + new_prompt + patched[prompt_match.end :]else:# No prompt= kwarg — add one with current_value context# Insert before the closing parendiff --git a/tests/unit/test_fix_static_actions_in_for_loops.py b/tests/unit/test_fix_static_actions_in_for_loops.pyindex 15246a469..1543565ab 100644--- a/tests/unit/test_fix_static_actions_in_for_loops.py+++ b/tests/unit/test_fix_static_actions_in_for_loops.py@@ -126,6 +126,56 @@ class TestPatchStaticClicksInBlock:assert "Download the quarterly report" in resultassert "Target: {current_value}" in result+ def test_single_quoted_prompt_with_escaped_apostrophe_is_preserved(self) -> None:+ body = textwrap.dedent("""\+ await page.click(+ selector='a.download-link',+ ai='fallback',+ prompt='Open the applicant\\'s file',+ )""")+ result = _patch_static_clicks_in_block(body)+ assert "ai='proactive'" in result+ assert "applicant\\'s file" in result+ assert "Target: {current_value}" in result++ def test_double_quoted_prompt_with_escaped_quotes_is_preserved(self) -> None:+ body = textwrap.dedent("""\+ await page.click(+ selector="a.download-link",+ ai="fallback",+ prompt="Click the \\\"download\\\" link",+ )""")+ result = _patch_static_clicks_in_block(body)+ assert 'ai="proactive"' in result+ assert '\\"download\\"' in result+ assert "Target: {current_value}" in result++ def test_multiline_prompt_is_preserved(self) -> None:+ body = textwrap.dedent("""\+ await page.click(+ selector='a.download-link',+ ai='fallback',+ prompt='Click the first matching row+and then open the details panel',+ )""")+ result = _patch_static_clicks_in_block(body)+ assert "ai='proactive'" in result+ assert "Click the first matching row" in result+ assert "and then open the details panel" in result+ assert "Target: {current_value}" in result++ def test_unterminated_prompt_with_many_backslashes_is_left_unchanged(self) -> None:+ repeated_backslashes = "\\\\a" * 4000+ body = (+ "await page.click(\n"+ " selector='a.download-link',\n"+ " ai='fallback',\n"+ f' prompt="{repeated_backslashes}\n'+ ")"+ )+ result = _patch_static_clicks_in_block(body)+ assert result == body+# ---------------------------------------------------------------------------# _fix_static_actions_in_for_loops (top-level function)