Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
fix: recover TOTP code from non-strict-JSON webhook bodies (#6354)
Shuchang Zhengd2632bc5b0cb
diff --git a/skyvern/services/otp_service.py b/skyvern/services/otp_service.pyindex 5044d159a..726a59532 100644--- a/skyvern/services/otp_service.py+++ b/skyvern/services/otp_service.py@@ -29,6 +29,10 @@ _MFA_PARAMETER_KEY_HINTS = ("mfa", "otp", "verification")_MFA_METADATA_KEY_HINTS = ("identifier", "url", "secret", "seed", "key")_NON_ALNUM_PATTERN = re.compile(r"[^a-z0-9]")_EXPECTED_TOTP_WEBHOOK_RESPONSE_SHAPE = '{"verification_code":"123456"}'+# Recovers the verification_code value when the surrounding JSON is malformed+# (e.g. unescaped quotes inside a relayed email). Assumes verification_code is+# the final field, which is the common shape.+_VERIFICATION_CODE_FIELD_PATTERN = re.compile(r'"verification_code"\s*:\s*"(?P<value>.*)"\s*}\s*\Z', re.DOTALL)_TOTP_WEBHOOK_BODY_PREVIEW_LIMIT = 200_TOTP_WEBHOOK_REQUEST_MAX_ATTEMPTS = 3_TOTP_WEBHOOK_REQUEST_RETRY_TIMEOUT_SECONDS = 5@@ -170,6 +174,30 @@ def _totp_webhook_contract_error_reason()+def _coerce_totp_response_body(body: str) -> tuple[Any, bool]:+ """Decode a TOTP webhook body into a JSON value, tolerating the malformations+ customers produce when relaying a raw OTP email into ``verification_code``.++ Returns ``(value, True)`` when a JSON value is recovered, else ``(body, False)``.+ The downstream extractor runs the value through the LLM, so an imperfectly+ recovered string is still useful — better than failing the whole login.+ """+ try:+ return json.loads(body), True+ except (json.JSONDecodeError, ValueError):+ pass+ # Literal control characters (raw email newlines/tabs) are the most common+ # malformation; strict=False tolerates them inside string values.+ try:+ return json.loads(body, strict=False), True+ except (json.JSONDecodeError, ValueError):+ pass+ match = _VERIFICATION_CODE_FIELD_PATTERN.search(body)+ if match is not None:+ return {"verification_code": match.group("value")}, True+ return body, False++async def _post_totp_verification_url(*,url: str,@@ -193,15 +221,13 @@ async def _post_totp_verification_url()# Content-Type gate: only trust an explicit non-JSON header to mean# "this is not JSON". Missing header (e.g. proxy responses, which- # don't preserve upstream headers) falls through to optimistic JSON+ # don't preserve upstream headers) falls through to tolerant JSON# parsing — customer TOTP endpoints contractually return JSON.content_type = response.headers.get("content-type", "").lower()if content_type and "json" not in content_type:return response.status_code, response.headers, response.body, False- try:- return response.status_code, response.headers, json.loads(response.body), True- except (json.JSONDecodeError, ValueError):- return response.status_code, response.headers, response.body, False+ parsed, is_json = _coerce_totp_response_body(response.body)+ return response.status_code, response.headers, parsed, is_jsonexcept Exception:LOG.debug("TOTP webhook request attempt failed",diff --git a/tests/unit/test_otp_service.py b/tests/unit/test_otp_service.pyindex eb0b86bdf..55616999d 100644--- a/tests/unit/test_otp_service.py+++ b/tests/unit/test_otp_service.py@@ -318,6 +318,45 @@ class TestGetOtpValueFromUrl:assert result is Noneassert any("TOTP webhook returned non-200 response" in log.get("event", "") for log in logs)+ @pytest.mark.asyncio+ async def test_relayed_email_in_malformed_json_extracts_code_instead_of_raising(+ self, monkeypatch: pytest.MonkeyPatch+ ) -> None:+ """End-to-end: a customer relays a raw OTP email into verification_code,+ producing a body that isn't strict JSON. The login must recover the code+ via the LLM extractor rather than terminating with a contract error."""+ from types import SimpleNamespace++ from skyvern.forge.agent_functions import TOTPVerificationResponse+ from skyvern.services import otp_service++ raw_email = (+ "Verification Code - Your One-Time Passcode for Secure Account Login\r\n"+ "WARNING! EXTERNAL EMAIL.\r\nYour passcode is 654321."+ )+ body = '{\r\n "task_id": "tsk_test",\r\n "verification_code": "' + raw_email + '"\r\n}'+ seam = AsyncMock(return_value=TOTPVerificationResponse(status_code=200, body=body, headers={}))+ monkeypatch.setattr(+ otp_service.app, "AGENT_FUNCTION", SimpleNamespace(post_totp_verification_request=seam), raising=False+ )++ async def fake_parse_otp_login(+ content: str, organization_id: str, *_args: object, **_kwargs: object+ ) -> OTPValue:+ assert "654321" in content+ return OTPValue(value="654321", type="totp")++ monkeypatch.setattr(otp_service, "parse_otp_login", fake_parse_otp_login)++ result = await _get_otp_value_from_url(+ organization_id="o_test",+ url="https://example.com/totp",+ api_key="api-key",+ task_id="tsk_test",+ )++ assert result == OTPValue(value="654321", type="totp")+class TestPollOtpValueRetry:"""poll_otp_value retries fetch failures across the full wall-clock timeout window."""@@ -739,3 +778,153 @@ class TestPostTotpVerificationUrlSeam:)assert seam.await_count == 2++ @pytest.mark.asyncio+ async def test_body_with_literal_control_chars_recovers_as_json(self, monkeypatch: pytest.MonkeyPatch) -> None:+ """A customer that relays a raw multi-line OTP email into+ ``verification_code`` produces a body with literal newlines, which strict+ JSON rejects. The helper must still recover it as a JSON object so the+ downstream extractor can read the field instead of failing the login."""+ from skyvern.forge.agent_functions import TOTPVerificationResponse+ from skyvern.services import otp_service++ raw_email = "Verification Code\r\nYour one-time passcode is 123456\r\nThanks"+ body = '{\r\n "task_id": "tsk_1",\r\n "verification_code": "' + raw_email + '"\r\n}'+ seam = AsyncMock(return_value=TOTPVerificationResponse(status_code=200, body=body, headers={}))+ monkeypatch.setattr(+ otp_service.app, "AGENT_FUNCTION", SimpleNamespace(post_totp_verification_request=seam), raising=False+ )++ status, _headers, parsed, is_json = await otp_service._post_totp_verification_url(+ url="https://example.com/totp",+ signed_payload="{}",+ headers={},+ organization_id="o_test",+ retry_timeout=0,+ )++ assert status == 200+ assert is_json is True+ assert isinstance(parsed, dict)+ assert parsed["verification_code"] == raw_email++ @pytest.mark.asyncio+ async def test_body_with_unescaped_quotes_recovers_verification_code(self, monkeypatch: pytest.MonkeyPatch) -> None:+ """When the relayed email contains unescaped quotes, even lenient JSON+ parsing fails. The helper must best-effort recover the verification_code+ field so the downstream LLM extractor can still pull the code."""+ from skyvern.forge.agent_functions import TOTPVerificationResponse+ from skyvern.services import otp_service++ raw_email = 'Your "one-time" passcode is 123456'+ body = '{"task_id": "tsk_1", "verification_code": "' + raw_email + '"}'+ seam = AsyncMock(return_value=TOTPVerificationResponse(status_code=200, body=body, headers={}))+ monkeypatch.setattr(+ otp_service.app, "AGENT_FUNCTION", SimpleNamespace(post_totp_verification_request=seam), raising=False+ )++ status, _headers, parsed, is_json = await otp_service._post_totp_verification_url(+ url="https://example.com/totp",+ signed_payload="{}",+ headers={},+ organization_id="o_test",+ retry_timeout=0,+ )++ assert status == 200+ assert is_json is True+ assert isinstance(parsed, dict)+ assert parsed["verification_code"] == raw_email++ @pytest.mark.asyncio+ async def test_truly_non_json_body_still_marks_is_json_false(self, monkeypatch: pytest.MonkeyPatch) -> None:+ """A body with no recoverable verification_code (e.g. an HTML error page)+ must still trip the non-JSON contract path so genuinely broken+ integrations stay visible to operators."""+ from skyvern.forge.agent_functions import TOTPVerificationResponse+ from skyvern.services import otp_service++ seam = AsyncMock(+ return_value=TOTPVerificationResponse(+ status_code=200,+ body="<html><body>Service Unavailable</body></html>",+ headers={},+ )+ )+ monkeypatch.setattr(+ otp_service.app, "AGENT_FUNCTION", SimpleNamespace(post_totp_verification_request=seam), raising=False+ )++ status, _headers, body, is_json = await otp_service._post_totp_verification_url(+ url="https://example.com/totp",+ signed_payload="{}",+ headers={},+ organization_id="o_test",+ retry_timeout=0,+ )++ assert status == 200+ assert is_json is False+ assert body == "<html><body>Service Unavailable</body></html>"++ @pytest.mark.asyncio+ async def test_forwarded_email_payload_recovers_verification_code(self, monkeypatch: pytest.MonkeyPatch) -> None:+ """Mirrors a real-world payload shape: a customer relays an entire+ forwarded OTP email (multi-line, with literal CRLF line breaks) into+ verification_code. The literal control characters make it invalid strict+ JSON, but the helper must still recover the field so the downstream LLM+ extractor can read the code instead of terminating the login."""+ from skyvern.forge.agent_functions import TOTPVerificationResponse+ from skyvern.services import otp_service++ email_lines = [+ "Fw: Verification Code - Your Example Business One-Time Passcode for Secure Account Login",+ "",+ "Example Company",+ "www.example.comhttps://www.example.com",+ "",+ "CONFIDENTIALITY NOTICE: This e-mail and any attachments are intended only for the named "+ "recipient. If you received this in error, please notify the sender and delete it.",+ "",+ "________________________________",+ "From: Example Business [email protected]",+ "Sent: Wednesday, June 3, 2026 11:01 AM",+ "Subject: Verification Code - Your Example Business One-Time Passcode for Secure Account Login",+ "",+ "WARNING! EXTERNAL EMAIL: Please confirm its authenticity before acting on any requests.",+ "________________________________",+ "[Verification Code]",+ "",+ "Hello Jane Doe,",+ "",+ "You requested to verify your Example Business account.",+ "",+ "[Verified] Your verification code is: 123456",+ "",+ "[Important] For your security, this code expires 10 minutes after it is requested.",+ "",+ "Terms of Use https://example.com/legal/terms | Your Privacy Center https://example.com/privacy",+ "(c) 2026 Example Corp. All rights reserved.",+ "",+ ]+ email = "\r\n".join(email_lines)+ body = '{\r\n "task_id": "t_123123123",\r\n "verification_code": "' + email + '"\r\n}'+ seam = AsyncMock(return_value=TOTPVerificationResponse(status_code=200, body=body, headers={}))+ monkeypatch.setattr(+ otp_service.app, "AGENT_FUNCTION", SimpleNamespace(post_totp_verification_request=seam), raising=False+ )++ status, _headers, parsed, is_json = await otp_service._post_totp_verification_url(+ url="https://example.com/totp",+ signed_payload="{}",+ headers={},+ organization_id="o_test",+ retry_timeout=0,+ )++ assert status == 200+ assert is_json is True+ assert isinstance(parsed, dict)+ assert parsed["task_id"] == "t_123123123"+ assert parsed["verification_code"] == email+ assert "123456" in parsed["verification_code"]