Skip to content

Automate browser based workflows with AI

Validate Bitwarden items before accepting CLI advisories (SKY-16319) (#8569)

LawyZheng73fecfa5e79f
diff --git a/skyvern/forge/sdk/services/bitwarden.py b/skyvern/forge/sdk/services/bitwarden.py
index 7c6314c10..789f9fef8 100644
--- a/skyvern/forge/sdk/services/bitwarden.py
+++ b/skyvern/forge/sdk/services/bitwarden.py
@@ -10,6 +10,7 @@ import secrets
import shutil
import tempfile
import time
+import uuid
from collections import OrderedDict
from collections.abc import AsyncIterator, Awaitable, Callable
from dataclasses import dataclass
@@ -79,6 +80,25 @@ _EXPIRED_SESSION_MARKERS = (
# worth one forced sync before believing the miss.
_STALE_VAULT_MARKERS = ("not found", "no items found", "no item found")
+# The CLI can exit successfully after substituting this poison value for an undecryptable field.
+_DECRYPTION_FAILURE_STDERR_MARKER = "failed to decrypt"
+_DECRYPTION_FAILURE_VALUE = "[error: cannot decrypt]"
+
+# The only stderr line advisory enough to accept alongside a valid item: the CLI's generic
+# audit-event upload failure. Everything else stays fatal.
+_EVENT_POST_FAILED_ADVISORY = "Event post failed."
+
+
+def _contains_decryption_failure(value: Any) -> bool:
+ if isinstance(value, str):
+ return _DECRYPTION_FAILURE_VALUE in value
+ if isinstance(value, dict):
+ return any(_contains_decryption_failure(child) for child in value.values())
+ if isinstance(value, list):
+ return any(_contains_decryption_failure(child) for child in value)
+ return False
+
+
# The fingerprint is only ever compared against others made in this process. A random per-process
# salt prevents cross-process correlation, while a password KDF prevents a log line carrying the
# fingerprint from becoming a cheap offline oracle for guessing the master password.
@@ -619,8 +639,14 @@ class BitwardenService:
"""
Run a CLI command with the specified additional environment variables and return the result.
"""
- env = os.environ.copy() # Copy the current environment
- # Make sure node isn't returning warnings. Warnings are sent through stderr and we raise exceptions on stderr.
+ env = os.environ.copy()
+ # The CLI reads ~ten BW_* env vars (client id/secret/password/session plus the
+ # CLEANEXIT/RESPONSE/QUIET/PRETTY/RAW/NOINTERACTION output modes). An inherited output-mode var
+ # can suppress a failure, wrap the item payload, or — with BW_RAW — make `bw unlock` print the
+ # raw vault session key on stdout. Scrub the whole BW_* prefix fail-closed and re-supply only
+ # what additional_env sets below; do not narrow this back to a fixed allowlist that can drift.
+ for key in [name for name in env if name.startswith("BW_")]:
+ del env[key]
env["NODE_NO_WARNINGS"] = "1"
if additional_env:
env.update(additional_env) # Update with any additional environment variables
@@ -821,21 +847,86 @@ class BitwardenService:
@staticmethod
def _parse_fetched_item(item_result: RunCommandResult, item_id: str) -> dict:
- """Read `bw get item` output, naming the CLI's own complaint before blaming the JSON.
-
- Same reasoning as `_parse_listed_items`: a dead session and an item the cached vault has
- never seen both report themselves on stderr, and both are recoverable — but only if the
- message survives to the caller.
+ """Require exit zero and a matching, supported item with valid fields and no decryption failures.
+ The CLI reports advisory event-upload failures on stderr, so only validated items can tolerate diagnostics.
"""
- if item_result.returncode != 0 or item_result.stderr:
- raise BitwardenGetItemError(f"Failed to get the bitwarden item {item_id}. Error: {item_result.stderr}")
+ if item_result.returncode != 0:
+ raise BitwardenGetItemError(f"Failed to get Bitwarden item. Error: {item_result.stderr}")
+ if _DECRYPTION_FAILURE_STDERR_MARKER in item_result.stderr.lower():
+ raise BitwardenGetItemError(f"Failed to decrypt Bitwarden item. Error: {item_result.stderr}")
+
+ # Decide the stderr posture before parsing: an unknown or mixed line is fatal here so the CLI's
+ # repair-relevant diagnostic (RELOGIN/RESYNC) survives instead of being masked by a later static
+ # shape error. The exact advisory line(s) are deferred and accepted only after item validation.
+ advisory_lines = [line.strip() for line in item_result.stderr.splitlines() if line.strip()]
+ if any(line != _EVENT_POST_FAILED_ADVISORY for line in advisory_lines):
+ raise BitwardenGetItemError(f"Failed to get Bitwarden item. Error: {item_result.stderr}")
try:
item = json.loads(item_result.stdout)
except json.JSONDecodeError:
- raise BitwardenGetItemError(f"Failed to parse item JSON for item ID: {item_id}")
- if not item:
- raise BitwardenGetItemError(f"No item found in Bitwarden for item ID: {item_id}")
+ raise BitwardenGetItemError(f"Failed to parse Bitwarden item JSON. Error: {item_result.stderr}")
+ if not isinstance(item, dict) or item.get("object") != "item":
+ raise BitwardenGetItemError("Invalid Bitwarden item envelope")
+ returned_id = item.get("id")
+ # `is_uuid` accepts an uppercase request while the CLI lowercases it and returns the canonical
+ # form, so compare by parsed UUID rather than exact string; an unparseable returned id is fatal.
+ try:
+ returned_uuid = uuid.UUID(returned_id) if isinstance(returned_id, str) else None
+ requested_uuid = uuid.UUID(item_id)
+ except ValueError:
+ raise BitwardenGetItemError("Bitwarden item identity is not a valid UUID")
+ if returned_uuid is None or returned_uuid != requested_uuid:
+ raise BitwardenGetItemError("Bitwarden item identity does not match the request")
+ item_type = item.get("type")
+ if type(item_type) is not int or item_type not in (BitwardenItemType.LOGIN, BitwardenItemType.CREDIT_CARD):
+ raise BitwardenGetItemError("Unsupported Bitwarden item type")
+ if _contains_decryption_failure(item):
+ raise BitwardenGetItemError("Bitwarden item contains a value that failed to decrypt")
+
+ if item_type == BitwardenItemType.LOGIN:
+ login = item.get("login")
+ login_fields = ("username", "password", "totp")
+ if not isinstance(login, dict) or not any(field in login for field in login_fields):
+ raise BitwardenGetItemError("Invalid Bitwarden login payload")
+ if any(login.get(field) is not None and not isinstance(login[field], str) for field in login_fields):
+ raise BitwardenGetItemError("Invalid Bitwarden login field")
+ else:
+ card = item.get("card")
+ card_fields = ("cardholderName", "number", "expMonth", "expYear", "code", "brand")
+ if not isinstance(card, dict) or any(field not in card for field in card_fields):
+ raise BitwardenGetItemError("Invalid Bitwarden card payload")
+ if any(card[field] is not None and not isinstance(card[field], str) for field in card_fields):
+ raise BitwardenGetItemError("Invalid Bitwarden card field")
+ fields = item.get("fields")
+ if fields is not None and (
+ not isinstance(fields, list)
+ or any(
+ not isinstance(field, dict)
+ or any(field.get(key) is not None and not isinstance(field[key], str) for key in ("name", "value"))
+ for field in fields
+ )
+ ):
+ raise BitwardenGetItemError("Invalid Bitwarden card custom fields")
+ organization_id = item.get("organizationId")
+ collection_ids = item.get("collectionIds")
+ if organization_id is not None and not isinstance(organization_id, str):
+ raise BitwardenGetItemError("Invalid Bitwarden card organization")
+ if collection_ids is not None and (
+ not isinstance(collection_ids, list) or any(not isinstance(value, str) for value in collection_ids)
+ ):
+ raise BitwardenGetItemError("Invalid Bitwarden card collections")
+
+ # The item is valid; the only stderr that reached here is the exact advisory (possibly repeated),
+ # accepted with a bounded classification now that item validation has passed.
+ if advisory_lines:
+ LOG.warning(
+ "Bitwarden CLI reported advisory stderr while fetching an item",
+ command_kind="get_item",
+ stderr_class="event_post_failed",
+ returncode=item_result.returncode,
+ item_valid=True,
+ )
return item
@staticmethod
@@ -1015,6 +1106,8 @@ class BitwardenService:
command = ["bw", "get", "item", item_id, "--session", session_key]
item_result = await BitwardenService.run_command(command, additional_env=session.env, timeout=timeout)
item = BitwardenService._parse_fetched_item(item_result, item_id)
+ if item["type"] != BitwardenItemType.LOGIN:
+ raise BitwardenGetItemError("Bitwarden item is not a login type")
login = item["login"]
totp = BitwardenService.normalize_totp_config(login.get("totp") or "")
@@ -1304,11 +1397,10 @@ class BitwardenService:
unlock_command = ["bw", "unlock", "--passwordenv", "BW_PASSWORD"]
unlock_result = await BitwardenService.run_command(unlock_command, env, timeout=timeout)
- # Validate the unlock result
+ # Validate the unlock result. Never interpolate unlock stdout into the error: on this path it
+ # can be the raw vault session key (e.g. under BW_RAW), so keep only stderr for repair triage.
if unlock_result.stdout and "Your vault is now unlocked!" not in unlock_result.stdout:
- raise BitwardenUnlockError(
- f"Failed to unlock vault. stdout: {unlock_result.stdout} stderr: {unlock_result.stderr}"
- )
+ raise BitwardenUnlockError(f"Failed to unlock vault. stderr: {unlock_result.stderr}")
# Extract session key
try:
diff --git a/tests/unit/test_bitwarden_login.py b/tests/unit/test_bitwarden_login.py
index 2cf4af396..6699b13d6 100644
--- a/tests/unit/test_bitwarden_login.py
+++ b/tests/unit/test_bitwarden_login.py
@@ -1,9 +1,13 @@
import asyncio
import json
+import os
+import sys
from unittest.mock import AsyncMock, MagicMock
import pytest
+from structlog.testing import capture_logs
+from skyvern.exceptions import BitwardenGetItemError, BitwardenUnlockError
from skyvern.forge.sdk.schemas.credentials import PasswordCredential
from skyvern.forge.sdk.services import bitwarden as bitwarden_module
from skyvern.forge.sdk.services.bitwarden import (
@@ -23,6 +27,42 @@ MISSING_TOTP_LOGINS = [
]
+def _cli_item(item_type: BitwardenItemType = BitwardenItemType.LOGIN) -> dict:
+ item = {
+ "object": "item",
+ "id": "11111111-1111-1111-1111-111111111111",
+ "type": int(item_type),
+ "organizationId": None,
+ "collectionIds": [],
+ "folderId": None,
+ "name": "Example item",
+ "notes": None,
+ "favorite": False,
+ "fields": None,
+ "reprompt": 0,
+ "revisionDate": "2026-01-01T00:00:00.000Z",
+ "creationDate": "2026-01-01T00:00:00.000Z",
+ "deletedDate": None,
+ }
+ if item_type == BitwardenItemType.LOGIN:
+ item["login"] = {
+ "username": "[email protected]",
+ "password": "example-password",
+ "totp": None,
+ "uris": [{"match": None, "uri": "https://example.test/login"}],
+ }
+ else:
+ item["card"] = {
+ "cardholderName": "Example Holder",
+ "number": "4111111111111111",
+ "expMonth": "12",
+ "expYear": "2030",
+ "code": None,
+ "brand": "Visa",
+ }
+ return item
+
+
@pytest.mark.asyncio
async def test_login_ignores_data_file_creation_notice_on_stderr(
monkeypatch: pytest.MonkeyPatch,
@@ -118,6 +158,318 @@ async def test_get_credential_item_by_id_reads_login_without_totp(monkeypatch: p
assert fetched_item.credential.totp == ""
[email protected]("item_type", [BitwardenItemType.LOGIN, BitwardenItemType.CREDIT_CARD])
+ ("stderr", "stderr_class"),
+ [
+ ("", None),
+ ("Event post failed.\n", "event_post_failed"),
+ ("Event post failed.\nEvent post failed.\n", "event_post_failed"),
+ ],
+ ids=["clean", "event-upload", "event-upload-repeated"],
+)
+def test_get_item_keeps_valid_credentials_with_safe_advisory_telemetry(
+ item_type: BitwardenItemType, stderr: str, stderr_class: str | None
+) -> None:
+ item = _cli_item(item_type)
+ result = RunCommandResult(stdout=json.dumps(item), stderr=stderr, returncode=0)
+
+ with capture_logs() as logs:
+ assert BitwardenService._parse_fetched_item(result, item["id"]) == item
+
+ if stderr_class is None:
+ assert not logs
+ else:
+ assert len(logs) == 1
+ # Accepted-advisory telemetry stays bounded: a classification only, never raw stderr or the item id.
+ assert logs[0] == {
+ "event": "Bitwarden CLI reported advisory stderr while fetching an item",
+ "log_level": "warning",
+ "command_kind": "get_item",
+ "stderr_class": stderr_class,
+ "returncode": 0,
+ "item_valid": True,
+ }
+
+
+def test_get_item_accepts_uppercase_uuid_request_matching_lowercase_canonical_id() -> None:
+ # is_uuid() accepts an uppercase UUID, but the CLI lowercases the request and returns the
+ # canonical lowercase id, so identity must compare by canonical UUID, not exact string.
+ item = _cli_item()
+ item["id"] = "479d0fec-6ab3-4f74-a92e-b4c2007704d3"
+ requested_id = "479D0FEC-6AB3-4F74-A92E-B4C2007704D3"
+ result = RunCommandResult(stdout=json.dumps(item), stderr="Event post failed.\n", returncode=0)
+
+ with capture_logs() as logs:
+ assert BitwardenService._parse_fetched_item(result, requested_id) == item
+ assert len(logs) == 1 and logs[0]["stderr_class"] == "event_post_failed"
+
+
+def test_get_item_rejects_valid_item_with_unknown_stderr() -> None:
+ # A valid item does not license an unknown stderr line; it stays fatal and keeps the CLI's
+ # diagnostic so `_repair_for_failure` can still classify it (here a stale-vault marker).
+ item = _cli_item()
+ result = RunCommandResult(stdout=json.dumps(item), stderr="Not found.\n", returncode=0)
+
+ with capture_logs() as logs:
+ with pytest.raises(BitwardenGetItemError, match="Not found."):
+ BitwardenService._parse_fetched_item(result, item["id"])
+ assert not logs
+
+
+def test_get_item_rejects_exact_advisory_mixed_with_unknown_line() -> None:
+ # The exact advisory line does not whitelist the whole stderr; a mixed unknown line stays fatal
+ # and its repair-relevant diagnostic is retained.
+ item = _cli_item()
+ result = RunCommandResult(
+ stdout=json.dumps(item), stderr="Event post failed.\nYou are not logged in.\n", returncode=0
+ )
+
+ with capture_logs() as logs:
+ with pytest.raises(BitwardenGetItemError, match="You are not logged in."):
+ BitwardenService._parse_fetched_item(result, item["id"])
+ assert not logs
+
+
[email protected]("stderr", ["You are not logged in.\n", "Not found.\n"], ids=["relogin", "resync"])
+def test_get_item_preserves_repair_stderr_over_shape_error(stderr: str) -> None:
+ # An exit-zero error/status envelope with repairable stderr must keep the CLI diagnostic so
+ # `_repair_for_failure` can still pick RELOGIN/RESYNC — not be masked by a static shape error.
+ result = RunCommandResult(stdout=json.dumps({"error": "not an item"}), stderr=stderr, returncode=0)
+
+ with capture_logs() as logs:
+ with pytest.raises(BitwardenGetItemError, match=stderr.strip()):
+ BitwardenService._parse_fetched_item(result, _cli_item()["id"])
+ assert not logs
+
+
+def test_get_item_defers_exact_advisory_until_after_item_validation() -> None:
+ # A malformed item carrying only the exact advisory must fail as a shape/envelope error and must
+ # not be accepted or logged as a valid item — advisory acceptance is deferred until validation passes.
+ result = RunCommandResult(stdout=json.dumps({"object": "not-item"}), stderr="Event post failed.\n", returncode=0)
+
+ with capture_logs() as logs:
+ with pytest.raises(BitwardenGetItemError):
+ BitwardenService._parse_fetched_item(result, _cli_item()["id"])
+ assert not logs
+
+
+def test_get_item_still_raises_and_keeps_the_clis_message_on_a_nonzero_exit() -> None:
+ result = RunCommandResult(stdout="", stderr="Not found.\n", returncode=1)
+
+ with pytest.raises(BitwardenGetItemError, match="Not found."):
+ BitwardenService._parse_fetched_item(result, "item-1")
+
+
+def test_get_item_stays_fatal_when_a_field_failed_to_decrypt_despite_exit_zero() -> None:
+ item = _cli_item()
+ item["login"]["password"] = "[error: cannot decrypt]"
+ result = RunCommandResult(
+ stdout=json.dumps(item),
+ stderr="[EncString Generic Decrypt] failed to decrypt encstring\n",
+ returncode=0,
+ )
+
+ with pytest.raises(BitwardenGetItemError, match="failed to decrypt"):
+ BitwardenService._parse_fetched_item(result, item["id"])
+
+
[email protected]("nested", [False, True], ids=["password", "nested-value"])
[email protected]("escaped", [False, True], ids=["literal", "json-escaped"])
+def test_get_item_rejects_stdout_only_decrypt_poison(nested: bool, escaped: bool) -> None:
+ item = _cli_item()
+ if nested:
+ item["fields"] = [{"name": "extra", "value": {"nested": ["prefix [error: cannot decrypt] suffix"]}}]
+ else:
+ item["login"]["password"] = "[error: cannot decrypt]"
+ stdout = json.dumps(item)
+ if escaped:
+ stdout = stdout.replace("[error: cannot decrypt]", r"\u005berror: cannot decrypt\u005d")
+
+ with pytest.raises(BitwardenGetItemError):
+ BitwardenService._parse_fetched_item(RunCommandResult(stdout=stdout, stderr="", returncode=0), item["id"])
+
+
+def test_get_item_rejects_stderr_only_decrypt_failure() -> None:
+ item = _cli_item()
+ result = RunCommandResult(
+ stdout=json.dumps(item), stderr="[EncString Generic Decrypt] FAILED TO DECRYPT encstring\n", returncode=0
+ )
+
+ with pytest.raises(BitwardenGetItemError):
+ BitwardenService._parse_fetched_item(result, item["id"])
+
+
+ "stdout",
+ [
+ "",
+ "{",
+ "null",
+ "{}",
+ "[]",
+ '[{"object":"item"}]',
+ '"item"',
+ "1",
+ "true",
+ '{"error":"Not found."}',
+ '{"response":null,"statusCode":500}',
+ '{"success":false,"message":"Not found."}',
+ json.dumps({"success": True, "data": _cli_item()}),
+ '{"serverUrl":null,"lastSync":null,"status":"unlocked"}',
+ ],
+ ids=[
+ "empty",
+ "invalid-json",
+ "null",
+ "empty-object",
+ "empty-list",
+ "item-list",
+ "string",
+ "number",
+ "bool",
+ "error",
+ "http-response",
+ "failure-response",
+ "success-response",
+ "status",
+ ],
+)
+def test_get_item_rejects_non_item_envelopes_before_logging_advisories(stdout: str) -> None:
+ result = RunCommandResult(stdout=stdout, stderr="Event post failed.\n", returncode=0)
+
+ with capture_logs() as logs, pytest.raises(BitwardenGetItemError):
+ BitwardenService._parse_fetched_item(result, _cli_item()["id"])
+
+ assert not logs
+
+
+ ("key", "value"),
+ [
+ ("id", "22222222-2222-2222-2222-222222222222"),
+ ("id", None),
+ ("object", "response"),
+ ("object", None),
+ ("type", True),
+ ("type", "1"),
+ ("type", 1.0),
+ ("type", 2),
+ ("type", 4),
+ ("type", None),
+ ("login", None),
+ ("login", []),
+ ("login", "login"),
+ ("login", {}),
+ ("login", {"uris": []}),
+ ("login", {"username": ["user"]}),
+ ("login", {"password": {"value": "pw"}}),
+ ("login", {"totp": 123}),
+ ("login", {"password": False}),
+ ],
+)
+def test_get_item_rejects_invalid_identity_type_and_login_shape(key: str, value: object) -> None:
+ item = _cli_item()
+ requested_id = item["id"]
+ item[key] = value
+ result = RunCommandResult(stdout=json.dumps(item), stderr="Event post failed.\n", returncode=0)
+
+ with capture_logs() as logs, pytest.raises(BitwardenGetItemError):
+ BitwardenService._parse_fetched_item(result, requested_id)
+
+ assert not logs
+
+
+ ("key", "value"),
+ [
+ ("card", None),
+ ("card", []),
+ ("card", "card"),
+ ("card", {}),
+ ("fields", "fields"),
+ ("fields", {}),
+ ("fields", [None]),
+ ("fields", ["field"]),
+ ("fields", [{"name": ["billing_email"], "value": "value"}]),
+ ("fields", [{"name": "billing_email", "value": 123}]),
+ ],
+)
+def test_get_item_rejects_malformed_card_payload_and_custom_fields(key: str, value: object) -> None:
+ item = _cli_item(BitwardenItemType.CREDIT_CARD)
+ item[key] = value
+ result = RunCommandResult(stdout=json.dumps(item), stderr="Event post failed.\n", returncode=0)
+
+ with capture_logs() as logs, pytest.raises(BitwardenGetItemError):
+ BitwardenService._parse_fetched_item(result, item["id"])
+
+ assert not logs
+
+
[email protected]("field", ["cardholderName", "number", "expMonth", "expYear", "code", "brand"])
[email protected]("missing", [False, True], ids=["non-string", "missing"])
+def test_get_item_rejects_invalid_consumed_card_fields(field: str, missing: bool) -> None:
+ item = _cli_item(BitwardenItemType.CREDIT_CARD)
+ if missing:
+ del item["card"][field]
+ else:
+ item["card"][field] = 123
+
+ with pytest.raises(BitwardenGetItemError):
+ BitwardenService._parse_fetched_item(
+ RunCommandResult(stdout=json.dumps(item), stderr="", returncode=0), item["id"]
+ )
+
+
[email protected]("explicit_mode", [None, "true"], ids=["parent-only", "explicit-override"])
+async def test_run_command_scrubs_inherited_cli_output_modes(
+ monkeypatch: pytest.MonkeyPatch, explicit_mode: str | None
+) -> None:
+ # Every inherited BW_* var is scrubbed fail-closed — including BW_RAW (which makes `bw unlock`
+ # print the raw vault session key on stdout) and BW_SESSION — so only additional_env can re-supply
+ # a BW_* var to the child.
+ flags = ["BW_CLEANEXIT", "BW_RESPONSE", "BW_QUIET", "BW_PRETTY", "BW_RAW", "BW_SESSION"]
+ for flag in flags:
+ monkeypatch.setenv(flag, "true")
+ monkeypatch.setenv("BW_TEST_PARENT", "parent-value")
+ monkeypatch.setenv("KEEP_PARENT", "keep-value")
+ additional_env = {"BW_TEST_EXPLICIT": "command-value"}
+ if explicit_mode:
+ additional_env["BW_PRETTY"] = explicit_mode
+ keys = [*flags, "BW_TEST_PARENT", "KEEP_PARENT", "BW_TEST_EXPLICIT", "NODE_NO_WARNINGS"]
+ script = f"import json, os; print(json.dumps({{key: os.environ.get(key) for key in {keys!r}}}))"
+
+ result = await BitwardenService.run_command([sys.executable, "-c", script], additional_env=additional_env)
+
+ assert result.returncode == 0
+ assert json.loads(result.stdout) == {
+ **dict.fromkeys(flags), # every inherited BW_* scrubbed to None...
+ "BW_PRETTY": explicit_mode, # ...unless additional_env re-supplies it (additional_env wins)
+ "BW_TEST_PARENT": None, # an inherited BW_* is scrubbed even if it is not a known flag
+ "KEEP_PARENT": "keep-value", # non-BW parent env still passes through
+ "BW_TEST_EXPLICIT": "command-value",
+ "NODE_NO_WARNINGS": "1",
+ }
+ assert all(os.environ[flag] == "true" for flag in flags)
+
+
+async def test_unlock_never_echoes_raw_session_key_in_error(monkeypatch: pytest.MonkeyPatch) -> None:
+ # With an inherited BW_RAW, `bw unlock` prints the bare session key (the vault decryption key) on
+ # stdout with no banner. The unlock failure path must never interpolate that stdout into an error.
+ raw_session_key = "cV3rYS3cr3tS3ss10nK3yAAAABBBBCCCC=="
+
+ async def fake_run_command(command: list[str], additional_env: dict | None = None, timeout: int = 60):
+ return RunCommandResult(stdout=raw_session_key, stderr="mac failed", returncode=0)
+
+ monkeypatch.setattr(BitwardenService, "run_command", fake_run_command)
+
+ with pytest.raises(BitwardenUnlockError) as excinfo:
+ await BitwardenService.unlock("master-password")
+ assert raw_session_key not in str(excinfo.value)
+
+
class _HangingProcess:
"""A subprocess whose communicate() never returns, so only the deadline can end the call."""
@@ -208,7 +560,8 @@ async def test_every_cli_step_receives_the_attempt_budget(monkeypatch: pytest.Mo
stdout='Your vault is now unlocked!\n$ export BW_SESSION="abc"', stderr="", returncode=0
)
if command[1] == "get":
- item = {"id": "item-1", "login": {"username": "u", "password": "p", "totp": "", "uris": []}}
+ item = _cli_item()
+ item["id"] = command[3]
return RunCommandResult(stdout=json.dumps(item), stderr="", returncode=0)
return RunCommandResult(stdout="", stderr="", returncode=0)
diff --git a/tests/unit/test_bitwarden_null_credentials.py b/tests/unit/test_bitwarden_null_credentials.py
index ba9e6f09a..0c568bf38 100644
--- a/tests/unit/test_bitwarden_null_credentials.py
+++ b/tests/unit/test_bitwarden_null_credentials.py
@@ -52,16 +52,24 @@ def _stub_bitwarden_auth(monkeypatch: pytest.MonkeyPatch) -> None:
@pytest.mark.asyncio
+ ("login", "expected"),
+ [
+ ({"username": None, "password": None, "totp": None}, ("", "", "")),
+ ({"username": "example-user"}, ("example-user", "", "")),
+ ({"password": "example-password", "username": None}, ("", "example-password", "")),
+ ({"totp": " EXAMPLESEED "}, ("", "", "EXAMPLESEED")),
+ ],
+ ids=["all-null", "username-only", "password-only", "totp-only"],
+)
async def test_get_secret_value_by_item_id_coerces_null_fields_to_empty_string(
- monkeypatch: pytest.MonkeyPatch,
+ monkeypatch: pytest.MonkeyPatch, login: dict, expected: tuple[str, str, str]
) -> None:
item_payload = {
+ "object": "item",
"id": "11111111-1111-1111-1111-111111111111",
- "login": {
- "username": None,
- "password": None,
- "totp": None,
- },
+ "type": 1,
+ "login": login,
}
async def fake_run_command(command: list[str], **_: object) -> RunCommandResult:
@@ -78,9 +86,9 @@ async def test_get_secret_value_by_item_id_coerces_null_fields_to_empty_string(
item_id="11111111-1111-1111-1111-111111111111",
)
- assert result[BitwardenConstants.USERNAME] == ""
- assert result[BitwardenConstants.PASSWORD] == ""
- assert result[BitwardenConstants.TOTP] == ""
+ assert result[BitwardenConstants.USERNAME] == expected[0]
+ assert result[BitwardenConstants.PASSWORD] == expected[1]
+ assert result[BitwardenConstants.TOTP] == expected[2]
@pytest.mark.asyncio
@@ -123,7 +131,9 @@ async def test_get_secret_value_by_item_id_preserves_real_values(
monkeypatch: pytest.MonkeyPatch,
) -> None:
item_payload = {
+ "object": "item",
"id": "33333333-3333-3333-3333-333333333333",
+ "type": 1,
"login": {
"username": "[email protected]",
"password": "hunter2",
@@ -156,7 +166,9 @@ async def test_get_secret_value_by_item_id_preserves_raw_totp_uri(
) -> None:
totp_uri = "otpauth://totp/[email protected]?secret=JBSWY3DPEHPK3PXP&issuer=Example"
item_payload = {
+ "object": "item",
"id": "55555555-5555-5555-5555-555555555555",
+ "type": 1,
"login": {
"username": "[email protected]",
"password": "hunter2",
@@ -182,10 +194,12 @@ async def test_get_secret_value_by_item_id_preserves_raw_totp_uri(
@pytest.mark.asyncio
[email protected]("card_code", [None, "123"], ids=["null-code", "with-code"])
async def test_get_credit_card_data_includes_billing_custom_fields(
- monkeypatch: pytest.MonkeyPatch,
+ monkeypatch: pytest.MonkeyPatch, card_code: str | None
) -> None:
item_payload = {
+ "object": "item",
"id": "44444444-4444-4444-4444-444444444444",
"type": 3,
"organizationId": "org-id",
@@ -195,7 +209,7 @@ async def test_get_credit_card_data_includes_billing_custom_fields(
"number": "4111111111111111",
"expMonth": "12",
"expYear": "2030",
- "code": "123",
+ "code": card_code,
"brand": "visa",
},
"fields": [
@@ -222,6 +236,7 @@ async def test_get_credit_card_data_includes_billing_custom_fields(
)
assert result[BitwardenConstants.CREDIT_CARD_NUMBER] == "4111111111111111"
+ assert result[BitwardenConstants.CREDIT_CARD_CVV] == card_code
assert result["billing_address_line1"] == "123 Main St"
assert result["billing_address_country_code"] == "US"
assert result["billing_email"] == "[email protected]"
diff --git a/tests/unit/test_bitwarden_session_cache.py b/tests/unit/test_bitwarden_session_cache.py
index ba0f72403..485cf64c2 100644
--- a/tests/unit/test_bitwarden_session_cache.py
+++ b/tests/unit/test_bitwarden_session_cache.py
@@ -19,6 +19,7 @@ from collections.abc import Iterator
import pytest
+from skyvern.exceptions import BitwardenGetItemError
from skyvern.forge.sdk.services import bitwarden as bitwarden_module
from skyvern.forge.sdk.services.bitwarden import (
BitwardenConstants,
@@ -30,6 +31,7 @@ APPDATA_ENV = bitwarden_module._BITWARDEN_APPDATA_ENV_VAR
ITEM_ID = "11111111-1111-1111-1111-111111111111"
OTHER_ITEM_ID = "22222222-2222-2222-2222-222222222222"
CARD_ITEM = {
+ "object": "item",
"id": ITEM_ID,
"type": 3,
"organizationId": "org-id",
@@ -62,6 +64,7 @@ class FakeVaultCli:
def __init__(self) -> None:
self.calls: list[tuple[str, str | None]] = []
self.item: dict = {
+ "object": "item",
"id": ITEM_ID,
"type": 1,
"login": {"username": "[email protected]", "password": "hunter2", "totp": ""},
@@ -70,6 +73,7 @@ class FakeVaultCli:
# stderr to answer the next `bw get` / `bw list` calls with, one per entry, before succeeding.
self.get_failures: list[str] = []
self.list_failures: list[str] = []
+ self.get_stderr = ""
@property
def steps(self) -> list[str]:
@@ -91,7 +95,7 @@ class FakeVaultCli:
if command[1] == "get":
if self.get_failures:
return RunCommandResult(stdout="", stderr=self.get_failures.pop(0), returncode=1)
- return RunCommandResult(stdout=json.dumps(self.item), stderr="", returncode=0)
+ return RunCommandResult(stdout=json.dumps(self.item), stderr=self.get_stderr, returncode=0)
if command[1] == "list":
if self.list_failures:
# How the CLI reports a dead session: non-zero, nothing on stdout, reason on stderr.
@@ -348,7 +352,14 @@ async def test_fetches_for_different_organizations_do_not_serialize(monkeypatch:
order.append("second")
second_get_entered.set()
return RunCommandResult(
- stdout=json.dumps({"id": ITEM_ID, "login": {"username": "u", "password": "p", "totp": ""}}),
+ stdout=json.dumps(
+ {
+ "object": "item",
+ "id": ITEM_ID,
+ "type": 1,
+ "login": {"username": "u", "password": "p", "totp": ""},
+ }
+ ),
stderr="",
returncode=0,
)
@@ -465,7 +476,14 @@ async def test_concurrent_cli_commands_are_bounded(monkeypatch: pytest.MonkeyPat
return RunCommandResult(stdout="You are logged in!", stderr="", returncode=0)
if command[1] == "get":
return RunCommandResult(
- stdout=json.dumps({"id": ITEM_ID, "login": {"username": "u", "password": "p", "totp": ""}}),
+ stdout=json.dumps(
+ {
+ "object": "item",
+ "id": ITEM_ID,
+ "type": 1,
+ "login": {"username": "u", "password": "p", "totp": ""},
+ }
+ ),
stderr="",
returncode=0,
)
@@ -600,6 +618,39 @@ async def _fetch_card(item_id: str = ITEM_ID) -> dict[str, str]:
)
[email protected]("card", [False, True], ids=["login", "card"])
+async def test_event_upload_advisory_preserves_fetched_credentials(cli: FakeVaultCli, card: bool) -> None:
+ cli.get_stderr = "Event post failed.\n"
+ if card:
+ cli.item = CARD_ITEM.copy()
+ result = await _fetch_card()
+ assert result[BitwardenConstants.CREDIT_CARD_NUMBER] == CARD_ITEM["card"]["number"]
+ assert result[BitwardenConstants.CREDIT_CARD_CVV] == CARD_ITEM["card"]["code"]
+ else:
+ result = await _fetch()
+ assert result == {
+ BitwardenConstants.USERNAME: "[email protected]",
+ BitwardenConstants.PASSWORD: "hunter2",
+ BitwardenConstants.TOTP: "",
+ }
+
+
+async def test_login_fetch_rejects_a_valid_card_before_reading_login(cli: FakeVaultCli) -> None:
+ cli.item = CARD_ITEM.copy()
+
+ with pytest.raises(BitwardenGetItemError):
+ await BitwardenService._get_secret_value_from_url(
+ client_id="client-id",
+ client_secret="client-secret",
+ master_password="master-password",
+ bw_organization_id=None,
+ bw_collection_ids=None,
+ item_id=ITEM_ID,
+ )
+
+
@pytest.mark.asyncio
async def test_an_expired_session_is_re_established_on_the_credit_card_path(cli: FakeVaultCli) -> None:
cli.item = CARD_ITEM
@@ -714,7 +765,14 @@ async def test_a_burst_across_more_vaults_than_the_cache_holds_drains_back_to_ca
all_reading.set()
await all_reading.wait()
return RunCommandResult(
- stdout=json.dumps({"id": ITEM_ID, "login": {"username": "u", "password": "p", "totp": ""}}),
+ stdout=json.dumps(
+ {
+ "object": "item",
+ "id": ITEM_ID,
+ "type": 1,
+ "login": {"username": "u", "password": "p", "totp": ""},
+ }
+ ),
stderr="",
returncode=0,
)