#2187

Sessions sharing a restore key overwrite each other's saved state

OpenLuzivogopened 9d ago

When several sessions use the same restore key (for example "restore": "shared" in config.json, so every agent on a machine reuses one set of sign-ins), the session that saves last decides what every later session loads. A sign-in made in one session is lost whenever another session that was already running saves after it.

Reproduce (0.39.1, also on main at fd93075)

agent-browser --restore repro --session a open https://example.com
agent-browser --restore repro --session b open https://example.com
agent-browser --restore repro --session b eval "document.cookie='signed_in=yes; max-age=86400; path=/'"
agent-browser --restore repro --session b close
agent-browser --restore repro --session a close
agent-browser --restore repro --session c open https://example.com
agent-browser --restore repro --session c eval "document.cookie"

Session c prints "". Expected: "signed_in=yes", the cookie session b set and saved. Waiting 30 seconds instead of closing session a gives the same result, through the periodic autosave.

Cause

Each daemon saves its whole cookie jar and localStorage to its own file, <key>-<session>.json, on close and every 30 seconds (maybe_autosave_restore_state and auto_save_restore_state in cli/src/native/actions.rs, save_auto_state_transactional in cli/src/native/state.rs). A launching daemon loads the file with the newest mtime (find_auto_state_file). Nothing merges the files, so a session that started before another session's sign-in never holds that sign-in, and once it saves, its older view is the one every new session loads. With a few concurrent sessions, a new sign-in survives only if the session that made it happens to save last.

Proposed fix

Each autosave publishes only what its own session changed since it last loaded or saved, applied on top of the newest file for the key. Cookies it added or changed are upserted. A cookie it no longer holds is removed only while the newest file still holds the copy this session started from, so a copy another session refreshed survives. Expired persistent cookies are pruned. Storage for origins this save did not read is left as the newest file has it. A per-key lock around read, merge and write keeps two sessions saving at once from dropping each other's changes. A pull request with this change and tests follows.

Activity

0

No activity yet.