browser_vault_context.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. """Run a vault fill, or an automatic one-time-code entry, inside the selected backend's ``fill_context``
  2. (a plugin backend's lock/snapshot/manager session).
  3. Order, for every backend: the item's metadata is read and the page origin checked against it
  4. (``_bound_page_origin``) BEFORE the context is entered, so a page on the wrong origin never opens a
  5. backend's manager session or lock. Inside the context a backend with its own ``fill_context`` has the
  6. item re-read, so the origins checked and the secret resolved come from one snapshot.
  7. A plugin backend's failure arrives as ``PluginBackendError`` (fixed text, see
  8. ``agent/vault_backends/plugin.py``) and becomes a typed refusal; built-in backends behave as before.
  9. """
  10. import json
  11. from functools import wraps
  12. def plugin_errors_as_refusal(fn):
  13. """A ``PluginBackendError`` raised anywhere in ``fn`` (metadata read, context enter/exit, resolve)
  14. becomes the ``backend_unavailable`` refusal."""
  15. @wraps(fn)
  16. def wrapped(*args, **kwargs):
  17. from agent.vault_backends.plugin import PluginBackendError
  18. try:
  19. return fn(*args, **kwargs)
  20. except PluginBackendError as exc:
  21. return json.dumps({"success": False, "error_type": "backend_unavailable", "error": str(exc)})
  22. return wrapped
  23. def saved_origins(meta) -> list[str]:
  24. """The exact origins a vault item may be filled on (several for manager items)."""
  25. return list(meta.allowed_origins) or ([str(meta.origin)] if meta.origin else [])
  26. def snapshot_refusal(backend, handle, meta, page_origin):
  27. """Inside ``fill_context``: ``None`` when the item still binds ``page_origin``, else the refusal.
  28. Only a backend with its own context is re-read (the built-in managers' metadata read is a full
  29. listing, and their default context freezes nothing)."""
  30. from agent.vault_backends.base import LoginBackend
  31. if type(backend).fill_context is LoginBackend.fill_context:
  32. return None
  33. fresh = backend.get_meta(handle)
  34. if fresh is not None and fresh.kind == meta.kind and page_origin in saved_origins(fresh):
  35. return None
  36. return {"success": False, "error_type": "origin_mismatch",
  37. "error": (f"Refused: the saved item changed before the fill and no longer binds {page_origin}. "
  38. "Nothing was resolved or written.")}
  39. def write_backend_code(backend, handle, meta, origin, write):
  40. """Mint the backend's one-time code for ``handle`` and ``write`` it, both inside ``fill_context``, as a
  41. password fill is. The caller has already checked ``origin`` against ``meta``. ``None`` when the backend
  42. has no code (or failed to give one): the caller asks the user."""
  43. from agent.vault_backends.plugin import PluginBackendError
  44. written = None
  45. try:
  46. with backend.fill_context():
  47. refusal = snapshot_refusal(backend, handle, meta, origin)
  48. if refusal is not None:
  49. return json.dumps(refusal)
  50. try:
  51. code = backend.resolve_otp(handle)
  52. except Exception: # health: allow BLE001 -- no automatic code; the user is asked instead
  53. code = None
  54. if code:
  55. written = write(code)
  56. except PluginBackendError: # the plugin's context failed to open or close; a code already written stands
  57. pass
  58. return written