Add session mode: signing in and adding to the cart.

With cart_mode = "session", dibs signs in to the shop's account and puts
an item in the cart when it fires. The account's cookies are kept apart
from the watcher's and stored encrypted, with the key in the OS keyring.
Without a keyring, they're stored in a file only you can read.

An alert now goes out right away, without a cart link, and a second one
follows once the item is in the cart. If adding fails, the second alert
carries the cart link instead.

Amazon only accepts additions its page scripts have seen, so plugins can
now press a button in headless Chrome with ctx:click.

The TUI asks for one-time codes and shows the plugin's warning about
account locks in a modal. 'dibs login' signs in from a terminal, or
with --browser in a Chrome window, and 'dibs logout' deletes the cookies.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
OverlordandClaude Bot committed 2026-10-14 17:26:26 +02:00
1 parent e2cbc1a55c
commit 15a34e1f84
39 files changed
+3529 -163

No files matched your search

+145
View File
@@ -0,0 +1,145 @@
package session
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/base64"
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"github.com/zalando/go-keyring"
"git.security-command.org/Overlord/dibs/internal/fetch"
"git.security-command.org/Overlord/dibs/internal/fsutil"
)
const (
keyringService = "dibs"
keyringUser = "session-key"
)
// Vault keeps each shop's signed-in cookies in a file of their own. The
// files are encrypted with a key kept in the OS keyring. Without a keyring,
// they are plain files only the user can read.
//
// The keyring holds only the key: Windows limits a keyring entry to 2560
// bytes, which a shop's cookies can exceed.
type Vault struct {
dir string
codec fetch.Codec // nil without a keyring
}
// OpenVault returns the vault in dir/sessions. When the keyring cannot be
// used, it also returns an error saying so, and the vault stores plain
// files.
func OpenVault(dir string) (*Vault, error) {
v := &Vault{dir: filepath.Join(dir, "sessions")}
key, err := v.key()
if err != nil { return v, fmt.Errorf("no usable OS keyring, so signed-in cookies are stored unencrypted in %s: %w", v.dir, err) }
block, err := aes.NewCipher(key)
if err != nil { return v, err }
aead, err := cipher.NewGCM(block)
if err != nil { return v, err }
v.codec = sealer{aead}
return v, nil
}
// Encrypted reports whether the files are encrypted.
func (v *Vault) Encrypted() bool { return v.codec != nil }
// key returns the vault's key from the keyring, creating it on first use.
func (v *Vault) key() ([]byte, error) {
// Two processes creating a key at once would each encrypt with their
// own.
unlock, err := fsutil.Lock(filepath.Join(v.dir, "key.lock"))
if err != nil { return nil, err }
defer unlock()
s, err := keyring.Get(keyringService, keyringUser)
switch {
case err == nil:
key, err := base64.StdEncoding.DecodeString(s)
if err != nil || len(key) != 32 { return nil, errors.New("the session key in the keyring is not a 256-bit key") }
return key, nil
case errors.Is(err, keyring.ErrNotFound):
key := make([]byte, 32)
_, _ = rand.Read(key)
if err := keyring.Set(keyringService, keyringUser, base64.StdEncoding.EncodeToString(key)); err != nil { return nil, err }
return key, nil
default:
return nil, err
}
}
func (v *Vault) path(shop string) string {
if v.codec != nil { return filepath.Join(v.dir, shop+".cookies") }
return v.plainPath(shop)
}
func (v *Vault) plainPath(shop string) string { return filepath.Join(v.dir, shop+".json") }
// Load reads shop's cookies into jar. Cookies stored unencrypted before a
// keyring was available are encrypted now.
func (v *Vault) Load(shop string, jar *fetch.Jar) error {
if err := jar.LoadWith(v.path(shop), v.codec); err != nil { return fmt.Errorf("reading %s: %w", v.path(shop), err) }
if v.codec == nil { return nil }
plain := v.plainPath(shop)
if _, err := os.Stat(plain); errors.Is(err, fs.ErrNotExist) { return nil }
if err := jar.Load(plain); err != nil { return fmt.Errorf("reading %s: %w", plain, err) }
if err := v.Save(shop, jar); err != nil { return err }
return os.Remove(plain)
}
// Save writes jar as shop's cookies, after taking in newer cookies other
// dibs processes saved.
func (v *Vault) Save(shop string, jar *fetch.Jar) error {
return jar.SaveWith(v.path(shop), v.codec)
}
// Forget removes shop's file, such as one that cannot be decrypted.
func (v *Vault) Forget(shop string) error {
for _, p := range []string{v.path(shop), v.plainPath(shop)} {
if err := os.Remove(p); err != nil && !errors.Is(err, fs.ErrNotExist) { return err }
}
return nil
}
// sealer encrypts with AES-GCM, with the nonce in front of the ciphertext.
type sealer struct{ aead cipher.AEAD }
var additionalData = []byte("dibs session cookies v1")
func (s sealer) Encode(data []byte) ([]byte, error) {
nonce := make([]byte, s.aead.NonceSize())
_, _ = rand.Read(nonce)
return s.aead.Seal(nonce, nonce, data, additionalData), nil
}
func (s sealer) Decode(data []byte) ([]byte, error) {
n := s.aead.NonceSize()
if len(data) < n { return nil, errors.New("the file is too short to be encrypted cookies") }
out, err := s.aead.Open(nil, data[:n], data[n:], additionalData)
if err != nil { return nil, errors.New("the file cannot be decrypted with the key in the keyring") }
return out, nil
}