browser

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Oct 13, 2026 License: MIT Imports: 20 Imported by: 0 modules, 0 packages

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.

Documentation

Overview

Package browser reads a Moodle session out of a browser's own storage.

It exists because Moodle's session cookie is the credential a signed-in browser holds, and exchanging it for a web service token is the only way in on a site that issues no token of its own. Until now that meant asking the user to open developer tools and copy a cookie by hand.

Two things about Firefox decide the shape of this package, and both were measured rather than assumed:

Moodle sets its session cookie with no expiry — lifetime 0 in lib/classes/session/manager.php — which makes it a session cookie, and Firefox deliberately keeps those out of cookies.sqlite. Measured on Firefox 156 against a live Moodle 5.2: cookies.sqlite held nothing at all, while sessionstore-backups/recovery.jsonlz4 held the cookie. Reading the SQLite database, which is where one would look first, answers the wrong question.

What this package can therefore say is narrow, and the wording elsewhere has to respect it: a cookie was or was not found in the snapshot Firefox last wrote. Not finding one is not evidence that the user is not signed in. The snapshot is written periodically, privacy settings can exclude it, and a different profile or a container tab can hold the real session.

Index

Constants

View Source
const SessionCookieName = "MoodleSession"

SessionCookieName is what Moodle calls its session cookie by default. A site can rename it, which is why the caller may say so.

Variables

This section is empty.

Functions

func SafariCookiePath added in v0.1.2

func SafariCookiePath(home string) string

SafariCookiePath is the current sandboxed cookie store location. Its format and location are not a public Apple API, so callers must allow it to fail.

Types

type Found

type Found struct {
	// Host is the cookie's own host as the browser recorded it, which is not
	// necessarily the address the user typed.
	Host  string
	Name  string
	Value string
	// Source names the file it was read from, so the user can see exactly
	// what was opened on their behalf.
	Source string
}

Found is a cookie read out of a browser's storage, with where it came from.

func ChromiumSession

func ChromiumSession(profile, host, cookieName string) (Found, error)

ChromiumSession looks for one site's session cookie in a Chromium profile.

Unlike Firefox, Chromium does keep session cookies in its database — measured on Chrome 153: the row is there with is_persistent 0 and no expiry. What it does not do is keep them in the clear.

func FirefoxSession

func FirefoxSession(profile, host, cookieName string) (Found, error)

FirefoxSession looks for one site's session cookie in one Firefox profile.

It reads the named cookie for the named host and returns nothing else. The file it opens does contain other sites' session cookies — that cannot be avoided, since they share one container — but nothing else in it is returned, logged or kept.

func ReadSession

func ReadSession(profile Profile, host, cookieName string) (Found, error)

ReadSession reads one site's session from whichever browser a profile is.

An unknown directory is tried as Firefox and Chromium; an explicitly named Safari cookie file is recognized by its filename.

func SafariSession added in v0.1.2

func SafariSession(path, host, cookieName string) (Found, error)

SafariSession reads a single named cookie from Safari's best-effort store. The caller must explicitly request browser import; this is never a login side effect. Moodle verifies the returned session before storing it.

type Kind

type Kind string

Kind names which browser family a profile belongs to. They store sessions in different ways — Firefox in a compressed snapshot, Chromium in an encrypted SQLite database, Safari in an undocumented binary cookie file — so this decides which reader runs rather than being decoration.

const (
	Unknown  Kind = ""
	Firefox  Kind = "firefox"
	Chromium Kind = "chromium"
	Safari   Kind = "safari"
)

type Profile

type Profile struct {
	Name string
	Path string
	// Default marks the one the browser itself would open. For Firefox it
	// comes from the install's own section rather than from the older
	// per-profile marker: measured on 156, a machine can have both and they
	// can disagree.
	Default bool
	// Kind decides which reader runs. The two families store sessions in
	// entirely different places.
	Kind Kind
}

Profile is one Firefox profile on this machine.

func ChromiumProfiles

func ChromiumProfiles(home, configHome string) []Profile

ChromiumProfiles lists the Chromium-family profiles on this machine.

A browser's profiles are named in its Local State file rather than in the directory listing: the directories are called "Default", "Profile 1" and so on, while the names people recognise live in that index. A profile with no cookie database is left out: there is nothing in it to import.

func FirefoxProfiles

func FirefoxProfiles(home, configHome string) ([]Profile, error)

FirefoxProfiles lists the profiles on this machine.

Nothing is opened here beyond profiles.ini: this says what exists, and the caller decides what to read. A machine with no Firefox is not an error — it is an answer.

func PickProfile

func PickProfile(profiles []Profile) (Profile, error)

PickProfile chooses which profile to read, or refuses to choose.

A machine can hold several, and they hold different sessions. Picking one silently would mean reporting "no cookie" for a site the user is signed in to in the other, which reads as being signed out.

func SafariProfiles added in v0.1.2

func SafariProfiles(home string) []Profile

SafariProfiles discovers Safari only on macOS. A permission-denied store is still listed so an explicit import can give the user the real error.

Jump to

Keyboard shortcuts

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