sweetcookie

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 24, 2026 License: MIT Imports: 24 Imported by: 9 modules, 1 package

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

sweetcookie 🍪 — cookies from every browser jar

CI GitHub release Go License

Sweetcookie is a Go library for local tools that read, decrypt, filter, and deduplicate cookies from Chromium-family browsers, Firefox-family browsers, Safari on macOS, and JSON input. It reads local browser state and may trigger keychain or keyring prompts, so it is intended for CLI helpers, development scripts, and test harnesses on macOS, Windows, and Linux rather than servers.

Install

go get github.com/steipete/sweetcookie@latest

Sweetcookie requires Go 1.25 or newer.

Quick start

This example uses an inline cookie so it runs without browser access while exercising the same origin filtering and result API:

package main

import (
	"context"
	"fmt"

	"github.com/steipete/sweetcookie"
)

func main() {
	result, err := sweetcookie.Get(context.Background(), sweetcookie.Options{
		URL:      "https://example.com/account",
		Browsers: []sweetcookie.Browser{sweetcookie.BrowserInline},
		Inline: sweetcookie.InlineCookies{JSON: []byte(
			`[{"name":"session","value":"ready","domain":"example.com","path":"/"}]`,
		)},
	})
	if err != nil {
		panic(err)
	}

	for _, cookie := range result.Cookies {
		fmt.Printf("%s=%s (%s)\n", cookie.Name, cookie.Value, cookie.Source.Browser)
	}
}
$ go run .
session=ready (inline)

For browser profiles, omit Inline and Browsers to try the default browser order, or choose sources explicitly:

result, err := sweetcookie.Get(context.Background(), sweetcookie.Options{
	URL:      "https://example.com/",
	Names:    []string{"session", "csrf"},
	Browsers: []sweetcookie.Browser{sweetcookie.BrowserChrome, sweetcookie.BrowserFirefox},
	Mode:     sweetcookie.ModeMerge,
})

result.Cookies contains matching cookies and their source metadata. Browser-specific failures are returned in result.Warnings so one unavailable profile does not prevent other sources from succeeding.

Supported browsers

Browsers Platforms
Chrome, Chromium, Microsoft Edge, Brave, Vivaldi, Opera, Whale macOS, Windows, Linux
Arc, Comet macOS, Windows
Dia, ChatGPT Atlas macOS
Helium macOS; explicit opt-in
Firefox, Zen, Floorp, Waterfox, LibreWolf macOS, Windows, Linux
Safari macOS

Helium is not part of DefaultBrowsers(); request it with BrowserHelium. Linux Firefox discovery checks the legacy ~/.mozilla/firefox root first, then Snap, Flatpak, and XDG configuration roots; duplicate cookies keep legacy precedence. XDG_CONFIG_HOME must be absolute, otherwise ~/.config is used. Firefox Multi-Account Container metadata is exposed through Cookie.Container: ID is Firefox's userContextId, and Name comes from containers.json. Cookies from different containers remain distinct.

Selecting cookies and profiles

Get matches cookie domains, paths, secure schemes, names, and expiry against the requested origins. The main options are:

Option Purpose
URL, Origins Select cookies for one or more destinations.
Names Limit results to specific cookie names.
Browsers Set the source order; an empty list uses DefaultBrowsers().
Profiles Select a profile name, profile directory, or cookie database per browser.
ModeMerge Read all requested sources and deduplicate the result.
ModeFirst Stop after the first source that returns cookies.
Inline Read cookie JSON from bytes, base64, or a file before browser profiles.
IncludeExpired Keep expired cookies in the result.
AllowAllHosts Permit calls without URL or Origins.
Timeout Limit operating-system credential helper calls.

Inline input accepts either a cookie array or an object with a cookies array. Use it when a browser database is locked or an encryption scheme is unavailable. The full API is documented on pkg.go.dev.

Browser access

Sweetcookie snapshots Chromium and Firefox cookie databases together with their WAL sidecars before reading, so the original browser data stays untouched.

Chromium cookie decryption follows each platform's credential system:

  • On macOS, it reads the browser's Safe Storage password from Keychain and derives the legacy AES-128-CBC key.
  • On Windows, it unwraps the master key from Local State with DPAPI and decrypts AES-256-GCM values.
  • On Linux, it tries go-keyring, then secret-tool for GNOME or kwallet-query with dbus-send for KDE.

On macOS and Linux, per-browser environment variables such as GOOKIE_CHROME_SAFE_STORAGE_PASSWORD and GOOKIE_HELIUM_SAFE_STORAGE_PASSWORD can supply the Safe Storage password for deterministic local tooling. Some recent Chromium builds on Windows use app-bound encryption that requires additional OS-specific access; use inline cookies when those values cannot be decrypted.

Safari reads Cookies.binarycookies on macOS. Profile paths can be overridden through Options.Profiles when automatic discovery does not match the desired browser profile.

Development

go test ./...
make ci

make ci checks formatting, lint, tests, and the 90% macOS coverage threshold. On Linux and Windows, run make fmt-check lint test; the cross-platform CI runs those checks while macOS enforces the coverage threshold. Development, CI, and release verification use the patched Go 1.27.3 toolchain. CI tests macOS 27 (xcode-27), Windows, and Linux. The module retains Go 1.25.0 as its supported source minimum.

License

MIT. See LICENSE.

Releases

See RELEASING.md for the signed-tag GitHub release workflow and its no-publish rehearsal.

Documentation

Overview

Package sweetcookie loads cookies from local browser profiles (Chrome-family including Arc and opt-in Helium, Firefox, Safari).

This is intended for local tooling (CLI helpers, dev scripts, test harnesses). It reads local browser state, may trigger keychain/keyring prompts, and should not be used in server contexts.

Index

Constants

This section is empty.

Variables

View Source
var ErrNoOrigin = errors.New("sweetcookie: URL or Origins required (or AllowAllHosts)")

ErrNoOrigin is returned when neither URL nor Origins is set and AllowAllHosts is false.

Functions

This section is empty.

Types

type Browser

type Browser string

Browser identifies a cookie source.

const (
	// BrowserInline is the inline cookie payload source.
	BrowserInline Browser = "inline"

	// BrowserChrome is Google Chrome.
	BrowserChrome Browser = "chrome"
	// BrowserChromium is Chromium.
	BrowserChromium Browser = "chromium"
	// BrowserEdge is Microsoft Edge.
	BrowserEdge Browser = "edge"
	// BrowserBrave is Brave Browser.
	BrowserBrave Browser = "brave"
	// BrowserVivaldi is Vivaldi.
	BrowserVivaldi Browser = "vivaldi"
	// BrowserOpera is Opera.
	BrowserOpera Browser = "opera"
	// BrowserArc is Arc Browser.
	BrowserArc Browser = "arc"
	// BrowserHelium is Helium Browser.
	BrowserHelium Browser = "helium"
	// BrowserDia is Dia.
	BrowserDia Browser = "dia"
	// BrowserComet is Perplexity Comet.
	BrowserComet Browser = "comet"
	// BrowserAtlas is ChatGPT Atlas.
	BrowserAtlas Browser = "atlas"
	// BrowserWhale is Naver Whale.
	BrowserWhale Browser = "whale"

	// BrowserFirefox is Mozilla Firefox.
	BrowserFirefox Browser = "firefox"
	// BrowserZen is Zen.
	BrowserZen Browser = "zen"
	// BrowserFloorp is Floorp.
	BrowserFloorp Browser = "floorp"
	// BrowserWaterfox is Waterfox.
	BrowserWaterfox Browser = "waterfox"
	// BrowserLibreWolf is LibreWolf.
	BrowserLibreWolf Browser = "librewolf"

	// BrowserSafari is Apple Safari (macOS only).
	BrowserSafari Browser = "safari"
)

func DefaultBrowsers

func DefaultBrowsers() []Browser

DefaultBrowsers returns a default source preference order.

type Container added in v0.0.2

type Container struct {
	// ID is Firefox's profile-local userContextId; zero means the default container.
	ID int
	// Name comes from containers.json and may be empty.
	Name string
}

Container is a Firefox multi-account container (zero value = default / not Firefox).

type Cookie struct {
	Name     string
	Value    string
	Domain   string
	Path     string
	Secure   bool
	HTTPOnly bool
	SameSite SameSite

	// Container identifies a Firefox container; its zero value means none/default.
	Container Container

	Expires *time.Time
	Source  Source
}

Cookie is a browser cookie record.

type InlineCookies

type InlineCookies struct {
	// Exactly one of these is expected to be set. If multiple are set, JSON wins over Base64 over File.
	JSON   []byte
	Base64 string
	File   string
}

InlineCookies is an optional cookie payload source (JSON/base64/file).

type Mode

type Mode string

Mode controls how results from multiple sources are combined.

const (
	// ModeMerge merges results from all sources.
	ModeMerge Mode = "merge"
	// ModeFirst returns once at least one cookie is found.
	ModeFirst Mode = "first"
)

type Options

type Options struct {
	// URL is used to filter cookies by (scheme, host, path).
	// If empty, Origins must be set, or AllowAllHosts must be true.
	URL string

	// Origins are additional origins to consider (e.g. OAuth redirects).
	// If set, they are used for filtering alongside URL.
	Origins []string

	// Names is an allowlist of cookie names (empty means "all names").
	Names []string

	// Browsers is a source priority list. If empty, DefaultBrowsers() is used.
	Browsers []Browser

	// Mode controls how multiple sources are combined.
	Mode Mode

	// Profile overrides per-browser selection.
	// For Chromium-family: profile name (e.g. "Default"), profile dir, or explicit Cookies DB path.
	// For Firefox: profile name/dir, or explicit cookies.sqlite path.
	// For Safari: explicit Cookies.binarycookies path (macOS only).
	Profiles map[Browser]string

	// Inline is an optional source that is always tried before browser reads.
	Inline InlineCookies

	IncludeExpired bool
	AllowAllHosts  bool

	// Timeout for OS helper calls (keychain/keyring).
	Timeout time.Duration

	Debug bool
}

Options configures cookie loading and filtering.

type Result

type Result struct {
	Cookies  []Cookie
	Warnings []string
}

Result is returned by Get.

func Get

func Get(ctx context.Context, opts Options) (Result, error)

Get loads cookies from configured sources and returns a filtered, de-duplicated result.

type SameSite

type SameSite string

SameSite is the cookie SameSite attribute.

const (
	// SameSiteNone is SameSite=None.
	SameSiteNone SameSite = "None"
	// SameSiteLax is SameSite=Lax.
	SameSiteLax SameSite = "Lax"
	// SameSiteStrict is SameSite=Strict.
	SameSiteStrict SameSite = "Strict"
)

type Source

type Source struct {
	Browser    Browser
	Profile    string
	StorePath  string
	IsFallback bool
}

Source describes where a cookie came from.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL