Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
[SKY-8782] fix: prevent totp_identifier value from being typed as MFA code (#5399)
Marc Kelechavab6371eeff87b
diff --git a/skyvern/services/otp_service.py b/skyvern/services/otp_service.pyindex 71e9d4de0..d06feaa51 100644--- a/skyvern/services/otp_service.py+++ b/skyvern/services/otp_service.py@@ -18,6 +18,9 @@ from skyvern.forge.sdk.schemas.totp_codes import OTPTypeLOG = structlog.get_logger()_MFA_PARAMETER_KEY_HINTS = ("mfa", "otp", "verification")+# Keys that contain an MFA hint but are TOTP *metadata*, not actual OTP codes.+# "totpidentifier" matches "otp" but carries a lookup key, not a 6-digit code.+_MFA_METADATA_KEY_HINTS = ("identifier", "url", "secret", "seed", "key")_NON_ALNUM_PATTERN = re.compile(r"[^a-z0-9]")MFANavigationPayload = dict | list | str | None@@ -64,8 +67,14 @@ async def parse_otp_login(def _is_mfa_like_parameter_key(key: object) -> bool:- """Return True when a payload key appears to represent an MFA/OTP parameter."""+ """Return True when a payload key appears to represent an MFA/OTP code value.++ Excludes TOTP metadata keys (identifier, url, secret, etc.) that contain an+ MFA hint but carry lookup/config data rather than an actual verification code.+ """normalized_key = _NON_ALNUM_PATTERN.sub("", str(key).lower())+ if any(meta in normalized_key for meta in _MFA_METADATA_KEY_HINTS):+ return Falsereturn any(hint in normalized_key for hint in _MFA_PARAMETER_KEY_HINTS)diff --git a/tests/unit/test_otp_service.py b/tests/unit/test_otp_service.pynew file mode 100644index 000000000..e4d2c4245--- /dev/null+++ b/tests/unit/test_otp_service.py@@ -0,0 +1,97 @@+"""Tests for skyvern.services.otp_service — MFA key detection and TOTP extraction."""++from __future__ import annotations++import pytest++from skyvern.services.otp_service import _is_mfa_like_parameter_key, extract_totp_from_navigation_inputs+++class TestIsMfaLikeParameterKey:+ """_is_mfa_like_parameter_key should match OTP *code* keys but reject metadata keys."""++ @pytest.mark.parametrize(+ "key",+ [+ "otp_code",+ "mfa_code",+ "verification_code",+ "otp",+ "mfa",+ "verification",+ "OTP_CODE",+ "MFA_Code",+ ],+ )+ def test_matches_actual_otp_code_keys(self, key: str) -> None:+ assert _is_mfa_like_parameter_key(key) is True++ @pytest.mark.parametrize(+ "key",+ [+ # The root-cause bug: totp_identifier contains "otp" but is metadata+ "totp_identifier",+ "totp_url",+ "totp_secret",+ "totp_seed",+ "otp_identifier",+ "mfa_secret_key",+ "verification_url",+ # Unrelated keys+ "username",+ "password",+ "email",+ "url",+ ],+ )+ def test_rejects_metadata_and_unrelated_keys(self, key: str) -> None:+ assert _is_mfa_like_parameter_key(key) is False+++class TestExtractTotpFromNavigationInputs:+ """extract_totp_from_navigation_inputs should only return actual OTP codes."""++ def test_returns_none_for_none_payload(self) -> None:+ assert extract_totp_from_navigation_inputs(None) is None++ def test_returns_none_for_empty_dict(self) -> None:+ assert extract_totp_from_navigation_inputs({}) is None++ def test_extracts_otp_code_from_payload(self) -> None:+ payload = {"otp_code": "123456"}+ result = extract_totp_from_navigation_inputs(payload)+ assert result is not None+ assert result.value == "123456"++ def test_ignores_totp_identifier_key(self) -> None:+ """The core bug: totp_identifier value must NOT be returned as OTP code."""+ payload = {+ "totp_identifier": "2fa.00000000-0000-0000-0000-000000000000.wpid_000000000000000000",+ "totp_url": "https://example.com/totp",+ }+ result = extract_totp_from_navigation_inputs(payload)+ assert result is None++ def test_extracts_code_even_with_metadata_keys_present(self) -> None:+ """When both metadata and code keys exist, only the code value is returned."""+ payload = {+ "totp_identifier": "2fa.ab4a9cf0-xxx",+ "otp_code": "654321",+ }+ result = extract_totp_from_navigation_inputs(payload)+ assert result is not None+ assert result.value == "654321"++ def test_extracts_mfa_code_from_nested_dict(self) -> None:+ payload = {"data": {"mfa_code": "789012"}}+ result = extract_totp_from_navigation_inputs(payload)+ assert result is not None+ assert result.value == "789012"++ def test_returns_none_for_only_metadata_keys(self) -> None:+ payload = {+ "totp_identifier": "[email protected]",+ "totp_secret": "JBSWY3DPEHPK3PXP",+ }+ result = extract_totp_from_navigation_inputs(payload)+ assert result is None