From 7906843b403056d339d2ca8d2a024fa6b1eefe3b Mon Sep 17 00:00:00 2001 From: tannevaled Date: Tue, 10 Nov 2026 17:21:56 +0100 Subject: [PATCH] feat: run subcommand and the full guardrail set MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit webtest run renders the scenario's page and drives it via driver.Run, with every guardrail go-wasmcontainers asked for: - Dry-run by default: a submit step always computes PreviewSubmit's own preview; only --submit performs the real request. - Pinned-page check: already in driver.Run itself, nothing new needed here. - Session persistence (session.go): cookies the real http.Client actually received are kept in a 0600 file under the user's own config dir, never in an argument/env var/log line. A file found with any other permission is treated as corrupted, not silently trusted. cookiejar.Jar exposes no "list every cookie" method, so saving asks it per-host for exactly the hosts this run touched. - Append-only audit log (auditlog.go): one JSON line per submit step (time/url/method/body/dry_run), 0600, rotated past 10 MiB. - Bounds: --timeout (default 30s), --max-steps (default 200, checked before any rendering happens). - Anti-bot detection (antibot.go): a small, named marker set for well-known CAPTCHA providers and Cloudflare's own interstitial — found, run refuses outright. A heuristic, not exhaustive, and deliberately biased toward false positives over false negatives (stopping on an ordinary page that happens to mention one of these words is the safer failure direction for a tool whose whole point is never to route around a real challenge). Decisive test: TestRunPersistsCookiesAcrossInvocations — a login run (--submit) followed by a SEPARATE run invocation that only succeeds because the persisted cookie carried the session across. Co-Authored-By: Claude Sonnet 5 --- README.md | 48 +++++++-- cmd/webtest/antibot.go | 41 ++++++++ cmd/webtest/antibot_test.go | 26 +++++ cmd/webtest/auditlog.go | 56 ++++++++++ cmd/webtest/auditlog_test.go | 79 ++++++++++++++ cmd/webtest/main.go | 1 + cmd/webtest/run.go | 150 +++++++++++++++++++++++++++ cmd/webtest/run_test.go | 192 +++++++++++++++++++++++++++++++++++ cmd/webtest/session.go | 119 ++++++++++++++++++++++ cmd/webtest/session_test.go | 83 +++++++++++++++ 10 files changed, 785 insertions(+), 10 deletions(-) create mode 100644 cmd/webtest/antibot.go create mode 100644 cmd/webtest/antibot_test.go create mode 100644 cmd/webtest/auditlog.go create mode 100644 cmd/webtest/auditlog_test.go create mode 100644 cmd/webtest/run.go create mode 100644 cmd/webtest/run_test.go create mode 100644 cmd/webtest/session.go create mode 100644 cmd/webtest/session_test.go diff --git a/README.md b/README.md index 979e585..fedb525 100644 --- a/README.md +++ b/README.md @@ -2,14 +2,43 @@ HCL-driven browser-automation CLI for [go-webengine/engine](https://github.com/go-webengine/engine): click/fill-form/assert test scenarios, for driving a rendered page's own `LiveDocument` (`Focus`/`Type`/`Click`/`Submit`) from a declarative scenario file instead of hand-written Go. -Status: in progress. This repository currently holds the scenario format -(`hclscenario`), a `driver` package that executes a parsed scenario -against a real `engine.LiveDocument` (click/fill/assert/submit, plus the -pinned-page identity check), and a `webtest validate ` CLI command -(parse-only). The `run` CLI subcommand and the session/dry-run-by-default/ -audit-log/bounds/anti-bot guardrails are tracked separately and not -implemented yet — `driver.Run`'s own `doSubmit` parameter is the one -escape hatch those guardrails will gate. +## Usage + +```sh +webtest validate scenario.hcl # parse + schema-check only, no render +webtest run scenario.hcl # render + drive; every submit step is a DRY RUN (no request) +webtest run scenario.hcl --submit # submit steps perform the real request +``` + +`run`'s guardrails: + +- **Dry-run by default.** A `submit` step always computes and reports + what it WOULD send (method, URL, body); only `--submit` makes it real. +- **Pinned-page check**, built into `driver.Run` itself: before the first + step runs, the rendered page's own URL and every `pin.selectors` entry + must already match, or the run refuses with a named diff. +- **Session persistence.** Cookies the real HTTP client actually received + (e.g. from a prior `run --submit` that logged in) are kept in a 0600 + file under the user's own config dir (`--cookie-file` to override) — + never in an argument, environment variable, or log line. A file found + with any other permission is treated as corrupted, not silently + trusted. +- **Append-only audit log** (`--audit-log` to override), one JSON line + per submit step — timestamp, URL, method, body, and whether it was a + dry run — 0600, rotated past 10 MiB. +- **Bounds.** `--timeout` (default 30s) covers the whole render + every + step; `--max-steps` (default 200) refuses an oversized scenario before + rendering anything. +- **Anti-bot detection that stops, never evades**: a small, named set of + markers for well-known CAPTCHA providers and Cloudflare's own + interstitial — found on the page, `run` refuses outright rather than + attempting to continue. A heuristic, not exhaustive; check a target + site's own terms of service before automating it regardless. + +The `run` subcommand does not perform a login on its own behalf — "the +user logs in themselves" (a scenario's own `submit` step against a real +login form, run once with `--submit`); the resulting session cookie is +then reused by later `run` invocations against the same host. ## Scenario format @@ -51,8 +80,7 @@ scenario "login" { present, or the run refuses with a named diff rather than acting against an unexpected page. - `step "submit"` always computes a dry-run preview (method, URL, body) by - default; only the CLI's own `--submit` flag (once the CLI exists) makes - it a real request. + default; only `run`'s own `--submit` flag makes it a real request. - Every attribute value is an HCL expression and may call a function from a small, fixed, reviewed allowlist (`hclscenario.Functions`) — currently `upper`/`lower`/`trim`/`join`. The allowlist is deliberately NOT cty's diff --git a/cmd/webtest/antibot.go b/cmd/webtest/antibot.go new file mode 100644 index 0000000..9c06d8b --- /dev/null +++ b/cmd/webtest/antibot.go @@ -0,0 +1,41 @@ +// Copyright (c) the go-webengine/webtest authors. +// SPDX-License-Identifier: BSD-3-Clause + +package main + +import "strings" + +// antiBotMarkers is a small, named set of strings that show up on a real +// CAPTCHA/anti-bot challenge page from a well-known provider, or on +// Cloudflare's own interstitial — deliberately a HEURISTIC, not an +// exhaustive detector: go-wasmcontainers' own guardrail is "detect and +// STOP, never evade," so a false negative (a challenge this doesn't +// recognize) just means the scenario proceeds and then fails its own +// pin/assert steps normally — safe either way. A false POSITIVE (stopping +// on an ordinary page that merely mentions one of these words, e.g. in a +// footer) is the safer failure direction for a tool whose whole point is +// never to route around a real challenge. +var antiBotMarkers = []string{ + "recaptcha", + "hcaptcha", + "are you a robot", + "just a moment", // Cloudflare's own interstitial title + "attention required", // Cloudflare's own block-page title + "cf-challenge", + "distil_r_captcha", + "perimeterx", + "datadome", +} + +// detectAntiBot scans title+html (the rendered page's own and raw +// decoded source) for any marker, case-insensitively, returning the first +// one found. +func detectAntiBot(title, html string) (marker string, found bool) { + haystack := strings.ToLower(title + " " + html) + for _, m := range antiBotMarkers { + if strings.Contains(haystack, m) { + return m, true + } + } + return "", false +} diff --git a/cmd/webtest/antibot_test.go b/cmd/webtest/antibot_test.go new file mode 100644 index 0000000..7d859ae --- /dev/null +++ b/cmd/webtest/antibot_test.go @@ -0,0 +1,26 @@ +// Copyright (c) the go-webengine/webtest authors. +// SPDX-License-Identifier: BSD-3-Clause + +package main + +import "testing" + +func TestDetectAntiBotFindsRecaptcha(t *testing.T) { + marker, found := detectAntiBot("Verify you are human", `<div class="g-recaptcha"></div>`) + if !found || marker != "recaptcha" { + t.Fatalf("detectAntiBot = (%q, %v), want (recaptcha, true)", marker, found) + } +} + +func TestDetectAntiBotFindsCloudflareInterstitial(t *testing.T) { + marker, found := detectAntiBot("Just a moment...", `<p>Checking your browser</p>`) + if !found || marker != "just a moment" { + t.Fatalf("detectAntiBot = (%q, %v), want (just a moment, true)", marker, found) + } +} + +func TestDetectAntiBotOrdinaryPage(t *testing.T) { + if marker, found := detectAntiBot("Sign in", `<form><input name="email"></form>`); found { + t.Fatalf("detectAntiBot on an ordinary login page = (%q, true), want false", marker) + } +} diff --git a/cmd/webtest/auditlog.go b/cmd/webtest/auditlog.go new file mode 100644 index 0000000..a97e400 --- /dev/null +++ b/cmd/webtest/auditlog.go @@ -0,0 +1,56 @@ +// Copyright (c) the go-webengine/webtest authors. +// SPDX-License-Identifier: BSD-3-Clause + +package main + +import ( + "encoding/json" + "os" + "path/filepath" + "time" +) + +// maxAuditLogBytes bounds the audit log file size: past this, the +// current file is rotated (renamed to a .1 suffix, overwriting any +// previous one) rather than left to grow without bound — this tool's +// own "every new capability needs its own bound" convention, inherited +// from go-webengine/engine's. +const maxAuditLogBytes = 10 << 20 + +// auditEntry is one line of the append-only audit log — go-wasmcontainers' +// own guardrail: "an append-only log of what was sent (time, URL, fields, +// response status)". DryRun is recorded explicitly so the log itself +// shows which entries were ever a REAL request, not just a preview. +type auditEntry struct { + Time time.Time `json:"time"` + URL string `json:"url"` + Method string `json:"method"` + Body string `json:"body,omitempty"` + DryRun bool `json:"dry_run"` +} + +// appendAuditLog appends entry as one JSON line to path (0600, created if +// missing), rotating first if the file has already grown past +// maxAuditLogBytes. +func appendAuditLog(path string, entry auditEntry) error { + if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil { + return err + } + if info, err := os.Stat(path); err == nil && info.Size() > maxAuditLogBytes { + if err := os.Rename(path, path+".1"); err != nil { + return err + } + } + f, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o600) + if err != nil { + return err + } + defer f.Close() + data, err := json.Marshal(entry) + if err != nil { + return err + } + data = append(data, '\n') + _, err = f.Write(data) + return err +} diff --git a/cmd/webtest/auditlog_test.go b/cmd/webtest/auditlog_test.go new file mode 100644 index 0000000..59914de --- /dev/null +++ b/cmd/webtest/auditlog_test.go @@ -0,0 +1,79 @@ +// Copyright (c) the go-webengine/webtest authors. +// SPDX-License-Identifier: BSD-3-Clause + +package main + +import ( + "bufio" + "encoding/json" + "os" + "path/filepath" + "strings" + "testing" + "time" +) + +func TestAppendAuditLogCreatesAndAppends(t *testing.T) { + path := filepath.Join(t.TempDir(), "audit.log") + e1 := auditEntry{Time: time.Now(), URL: "https://x/a", Method: "POST", Body: "a=1", DryRun: true} + e2 := auditEntry{Time: time.Now(), URL: "https://x/b", Method: "GET", DryRun: false} + if err := appendAuditLog(path, e1); err != nil { + t.Fatalf("append 1: %v", err) + } + if err := appendAuditLog(path, e2); err != nil { + t.Fatalf("append 2: %v", err) + } + info, err := os.Stat(path) + if err != nil { + t.Fatal(err) + } + if mode := info.Mode().Perm(); mode != 0o600 { + t.Fatalf("audit log mode = %#o, want 0600", mode) + } + + f, err := os.Open(path) + if err != nil { + t.Fatal(err) + } + defer f.Close() + sc := bufio.NewScanner(f) + var lines []string + for sc.Scan() { + lines = append(lines, sc.Text()) + } + if len(lines) != 2 { + t.Fatalf("audit log has %d lines, want 2", len(lines)) + } + var got auditEntry + if err := json.Unmarshal([]byte(lines[0]), &got); err != nil { + t.Fatalf("line 1 is not valid JSON: %v", err) + } + if got.URL != "https://x/a" || !got.DryRun { + t.Fatalf("line 1 = %+v", got) + } +} + +func TestAppendAuditLogRotatesWhenTooLarge(t *testing.T) { + path := filepath.Join(t.TempDir(), "audit.log") + big := strings.Repeat("x", maxAuditLogBytes+1) + if err := os.WriteFile(path, []byte(big), 0o600); err != nil { + t.Fatal(err) + } + if err := appendAuditLog(path, auditEntry{URL: "https://x/after-rotation"}); err != nil { + t.Fatalf("append after exceeding the size bound: %v", err) + } + rotated := path + ".1" + if _, err := os.Stat(rotated); err != nil { + t.Fatalf("rotated file %s: %v, want it to exist", rotated, err) + } + data, err := os.ReadFile(path) + if err != nil { + t.Fatal(err) + } + if !strings.Contains(string(data), "after-rotation") { + t.Fatalf("new log file = %q, want it to contain the post-rotation entry, not the old giant content", data) + } + if len(data) > len(big) { + t.Fatalf("new log file is %d bytes, want it SMALLER than the pre-rotation file (%d) — rotation must have moved the old content out", len(data), len(big)) + } +} diff --git a/cmd/webtest/main.go b/cmd/webtest/main.go index 21ca190..9951ec8 100644 --- a/cmd/webtest/main.go +++ b/cmd/webtest/main.go @@ -32,5 +32,6 @@ func RootCmd() *cobra.Command { SilenceUsage: true, } cmd.AddCommand(newValidateCmd()) + cmd.AddCommand(newRunCmd()) return cmd } diff --git a/cmd/webtest/run.go b/cmd/webtest/run.go new file mode 100644 index 0000000..2ed1f81 --- /dev/null +++ b/cmd/webtest/run.go @@ -0,0 +1,150 @@ +// Copyright (c) the go-webengine/webtest authors. +// SPDX-License-Identifier: BSD-3-Clause + +package main + +import ( + "context" + "fmt" + "image" + "net/http" + "net/url" + "os" + "time" + + "github.com/go-webengine/engine" + "github.com/go-webengine/webtest/driver" + "github.com/go-webengine/webtest/hclscenario" + "github.com/spf13/cobra" +) + +const defaultMaxSteps = 200 + +func newRunCmd() *cobra.Command { + var ( + submit bool + timeout time.Duration + maxSteps int + auditPath string + cookiePath string + ) + cmd := &cobra.Command{ + Use: "run <scenario.hcl>", + Short: "Render the scenario's page and drive it (dry-run by default)", + Args: cobra.ExactArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + return runScenario(cmd, runOptions{ + path: args[0], submit: submit, timeout: timeout, + maxSteps: maxSteps, auditPath: auditPath, cookiePath: cookiePath, + }) + }, + } + cmd.Flags().BoolVar(&submit, "submit", false, + "Actually perform submit steps for real (default: a dry-run preview only — method/URL/body, no request)") + cmd.Flags().DurationVar(&timeout, "timeout", 30*time.Second, "Overall time budget for the render and every step") + cmd.Flags().IntVar(&maxSteps, "max-steps", defaultMaxSteps, "Refuse a scenario with more than this many steps") + cmd.Flags().StringVar(&auditPath, "audit-log", "", "Path to the append-only audit log (default: a file under the user config dir)") + cmd.Flags().StringVar(&cookiePath, "cookie-file", "", "Path to the persisted cookie jar (default: a file under the user config dir)") + return cmd +} + +type runOptions struct { + path string + submit bool + timeout time.Duration + maxSteps int + auditPath, cookiePath string +} + +func runScenario(cmd *cobra.Command, opt runOptions) error { + src, err := os.ReadFile(opt.path) + if err != nil { + return fmt.Errorf("read %s: %w", opt.path, err) + } + sc, diags := hclscenario.Parse(src, opt.path) + if diags.HasErrors() { + fmt.Fprint(cmd.ErrOrStderr(), diags) + return fmt.Errorf("%s: invalid scenario", opt.path) + } + if len(sc.Steps) > opt.maxSteps { + return fmt.Errorf("%s: %d steps exceeds --max-steps=%d", opt.path, len(sc.Steps), opt.maxSteps) + } + + cookiePath := opt.cookiePath + if cookiePath == "" { + if cookiePath, err = defaultCookiePath(); err != nil { + return fmt.Errorf("resolve default cookie path: %w", err) + } + } + auditPath := opt.auditPath + if auditPath == "" { + if auditPath, err = defaultAuditPath(); err != nil { + return fmt.Errorf("resolve default audit log path: %w", err) + } + } + + jar, err := loadCookieJar(cookiePath) + if err != nil { + return fmt.Errorf("cookie file: %w", err) + } + + e := engine.New() + e.Client = &http.Client{Jar: jar} + + ctx, cancel := context.WithTimeout(cmd.Context(), opt.timeout) + defer cancel() + + live, err := e.Open(ctx, sc.URL, image.Rect(0, 0, 1280, 1024)) + if err != nil { + return fmt.Errorf("open %s: %w", sc.URL, err) + } + defer func() { + if live != nil { + live.Close() + } + }() + + hosts := map[string]bool{} + addHost := func(rawurl string) { + if u, err := url.Parse(rawurl); err == nil && u.Host != "" { + hosts[u.Host] = true + } + } + addHost(sc.URL) + + doc := live.Document() + if marker, found := detectAntiBot(doc.Title, doc.HTML); found { + return fmt.Errorf("refusing to proceed: anti-bot challenge detected (%q) on %s — never evaded, only reported", marker, doc.URL) + } + + final, result, runErr := driver.Run(ctx, live, sc, opt.submit) + live = final + addHost(final.Document().URL) + + var hostList []string + for h := range hosts { + hostList = append(hostList, h) + } + if err := saveCookieJar(cookiePath, jar, hostList); err != nil { + fmt.Fprintf(cmd.ErrOrStderr(), "warning: could not save cookie jar to %s: %v\n", cookiePath, err) + } + + for _, sr := range result.Steps { + if sr.Step.Kind != hclscenario.StepSubmit { + continue + } + entry := auditEntry{ + Time: time.Now(), URL: sr.SubmitTarget, Method: sr.SubmitMethod, + Body: sr.SubmitBody, DryRun: !opt.submit, + } + if err := appendAuditLog(auditPath, entry); err != nil { + fmt.Fprintf(cmd.ErrOrStderr(), "warning: could not write audit log to %s: %v\n", auditPath, err) + } + } + + if runErr != nil { + return runErr + } + fmt.Fprintf(cmd.OutOrStdout(), "%s: scenario %q completed (%d step(s))\n", opt.path, sc.Name, len(result.Steps)) + return nil +} diff --git a/cmd/webtest/run_test.go b/cmd/webtest/run_test.go new file mode 100644 index 0000000..5a2a11a --- /dev/null +++ b/cmd/webtest/run_test.go @@ -0,0 +1,192 @@ +// Copyright (c) the go-webengine/webtest authors. +// SPDX-License-Identifier: BSD-3-Clause + +package main + +import ( + "bufio" + "encoding/json" + "net/http" + "net/http/httptest" + "os" + "path/filepath" + "strings" + "testing" +) + +// runEnv returns isolated --cookie-file/--audit-log paths under t.TempDir() +// so a test never touches the real user's config dir. +func runEnv(t *testing.T) (cookieFile, auditFile string) { + t.Helper() + dir := t.TempDir() + return filepath.Join(dir, "cookies.json"), filepath.Join(dir, "audit.log") +} + +func TestRunDryRunByDefaultNeverPosts(t *testing.T) { + var posted bool + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method == http.MethodPost { + posted = true + } + w.Write([]byte(`<html><body><form id="f" method="POST" action="/login"><input name="x" value="1"></form></body></html>`)) + })) + defer srv.Close() + + src := ` +scenario "s" { + url = "` + srv.URL + `/" + step "submit" { + selector = "#f" + } +} +` + cookieFile, auditFile := runEnv(t) + path := writeScenario(t, src) + stdout, _, err := runCmd(t, "run", path, "--cookie-file", cookieFile, "--audit-log", auditFile) + if err != nil { + t.Fatalf("run: %v", err) + } + if posted { + t.Fatal("run with no --submit must never perform a real request") + } + if !strings.Contains(stdout, "completed (1 step") { + t.Fatalf("stdout = %q", stdout) + } + + // The dry run must still be audited. + data, err := os.ReadFile(auditFile) + if err != nil { + t.Fatalf("audit log: %v", err) + } + var entry auditEntry + if err := json.Unmarshal([]byte(firstLine(t, data)), &entry); err != nil { + t.Fatal(err) + } + if !entry.DryRun { + t.Fatal("audit entry for a dry run must have DryRun=true") + } +} + +func TestRunSubmitFlagPerformsRealRequest(t *testing.T) { + var posted bool + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method == http.MethodPost { + posted = true + w.Header().Set("Content-Type", "text/html") + w.Write([]byte(`<html><body>ok</body></html>`)) + return + } + w.Write([]byte(`<html><body><form id="f" method="POST" action="/login"><input name="x" value="1"></form></body></html>`)) + })) + defer srv.Close() + + src := ` +scenario "s" { + url = "` + srv.URL + `/" + step "submit" { + selector = "#f" + } +} +` + cookieFile, auditFile := runEnv(t) + path := writeScenario(t, src) + if _, _, err := runCmd(t, "run", path, "--submit", "--cookie-file", cookieFile, "--audit-log", auditFile); err != nil { + t.Fatalf("run --submit: %v", err) + } + if !posted { + t.Fatal("run --submit must perform the real request") + } +} + +func TestRunMaxStepsExceeded(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Write([]byte(`<html><body><div id="a"></div></body></html>`)) + })) + defer srv.Close() + + src := ` +scenario "s" { + url = "` + srv.URL + `/" + step "assert" { selector = "#a" } +} +` + cookieFile, auditFile := runEnv(t) + path := writeScenario(t, src) + _, _, err := runCmd(t, "run", path, "--max-steps", "0", "--cookie-file", cookieFile, "--audit-log", auditFile) + if err == nil { + t.Fatal("run with --max-steps=0 against a 1-step scenario: want a refusal") + } +} + +func TestRunRefusesAntiBotPage(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Write([]byte(`<html><head><title>Just a moment...`)) + })) + defer srv.Close() + + src := ` +scenario "s" { + url = "` + srv.URL + `/" + step "assert" { selector = "body" } +} +` + cookieFile, auditFile := runEnv(t) + path := writeScenario(t, src) + _, _, err := runCmd(t, "run", path, "--cookie-file", cookieFile, "--audit-log", auditFile) + if err == nil { + t.Fatal("run against a Cloudflare interstitial page: want a refusal, not proceeding") + } +} + +func TestRunPersistsCookiesAcrossInvocations(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if c, err := r.Cookie("session"); err == nil && c.Value == "logged-in" { + w.Write([]byte(`
authenticated
`)) + return + } + if r.Method == http.MethodPost { + http.SetCookie(w, &http.Cookie{Name: "session", Value: "logged-in", Path: "/"}) + w.Header().Set("Content-Type", "text/html") + w.Write([]byte(`welcome`)) + return + } + w.Write([]byte(`
`)) + })) + defer srv.Close() + + cookieFile, auditFile := runEnv(t) + + loginSrc := ` +scenario "login" { + url = "` + srv.URL + `/" + step "submit" { + selector = "#f" + } +} +` + if _, _, err := runCmd(t, "run", writeScenario(t, loginSrc), "--submit", "--cookie-file", cookieFile, "--audit-log", auditFile); err != nil { + t.Fatalf("login run: %v", err) + } + + checkSrc := ` +scenario "check" { + url = "` + srv.URL + `/" + step "assert" { + selector = "#whoami" + text = "authenticated" + } +} +` + if _, _, err := runCmd(t, "run", writeScenario(t, checkSrc), "--cookie-file", cookieFile, "--audit-log", auditFile); err != nil { + t.Fatalf("second run did not see the persisted session cookie: %v", err) + } +} + +func firstLine(t *testing.T, data []byte) string { + t.Helper() + sc := bufio.NewScanner(strings.NewReader(string(data))) + if !sc.Scan() { + t.Fatal("expected at least one line") + } + return sc.Text() +} diff --git a/cmd/webtest/session.go b/cmd/webtest/session.go new file mode 100644 index 0000000..5118813 --- /dev/null +++ b/cmd/webtest/session.go @@ -0,0 +1,119 @@ +// Copyright (c) the go-webengine/webtest authors. +// SPDX-License-Identifier: BSD-3-Clause + +package main + +import ( + "encoding/json" + "fmt" + "net/http" + "net/http/cookiejar" + "net/url" + "os" + "path/filepath" + "time" +) + +// defaultCookiePath/defaultAuditPath are under the user's own config dir — +// never an argument, environment variable, or log line (the one directly +// asked for in go-wasmcontainers' own guardrail list): a cookie is +// effectively a credential, and an argument/env var is visible in a +// process listing or a shell history the way a file with the RIGHT +// permissions is not. +func defaultCookiePath() (string, error) { + dir, err := os.UserConfigDir() + if err != nil { + return "", err + } + return filepath.Join(dir, "webtest", "cookies.json"), nil +} + +func defaultAuditPath() (string, error) { + dir, err := os.UserConfigDir() + if err != nil { + return "", err + } + return filepath.Join(dir, "webtest", "audit.log"), nil +} + +// storedCookie is the cookie file's own on-disk shape — deliberately NOT +// *http.Cookie directly (whose JSON tags are not stable API and which +// carries fields, like Raw, this tool never needs to persist). +type storedCookie struct { + Name string `json:"name"` + Value string `json:"value"` + Domain string `json:"domain"` + Path string `json:"path"` + Expires time.Time `json:"expires,omitempty"` +} + +// loadCookieJar reads path (the persisted session from a PRIOR run — e.g. +// one that logged in — this tool never performs a login or writes to this +// file on its own BEHALF, only what a prior run's own real request +// actually received) into a fresh cookiejar.Jar. A missing file is not an +// error: a fresh jar (no session) is a legitimate starting state. Refuses +// to read a file whose permissions are not exactly 0600 — a +// group/world-readable cookie file is exactly the exposure this +// guardrail exists to prevent, so this tool treats it as corrupted +// rather than silently trusting it. +func loadCookieJar(path string) (*cookiejar.Jar, error) { + jar, err := cookiejar.New(nil) + if err != nil { + return nil, err + } + info, err := os.Stat(path) + if os.IsNotExist(err) { + return jar, nil + } + if err != nil { + return nil, err + } + if mode := info.Mode().Perm(); mode != 0o600 { + return nil, fmt.Errorf("%s has mode %#o, want exactly 0600 — refusing to read a cookie file others may be able to read", path, mode) + } + data, err := os.ReadFile(path) + if err != nil { + return nil, err + } + var stored []storedCookie + if err := json.Unmarshal(data, &stored); err != nil { + return nil, fmt.Errorf("parse %s: %w", path, err) + } + byHost := map[string][]*http.Cookie{} + for _, c := range stored { + byHost[c.Domain] = append(byHost[c.Domain], &http.Cookie{ + Name: c.Name, Value: c.Value, Path: c.Path, Expires: c.Expires, + }) + } + for host, cookies := range byHost { + jar.SetCookies(&url.URL{Scheme: "https", Host: host}, cookies) + } + return jar, nil +} + +// saveCookieJar writes jar's CURRENT cookies for exactly the given hosts +// (the scenario's own starting host and whatever host it may have +// navigated to — cookiejar.Jar exposes no "list every cookie it holds" +// method, only Cookies(url) for one host at a time, so the caller must +// name which hosts to ask about) back to path, 0600. A host's entry fully +// replaces whatever was stored for it before (naturally dropping an +// expired or cleared cookie too — this reflects the jar's CURRENT state, +// not an ever-growing accumulation). +func saveCookieJar(path string, jar *cookiejar.Jar, hosts []string) error { + if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil { + return err + } + var out []storedCookie + for _, host := range hosts { + for _, scheme := range [...]string{"https", "http"} { + for _, c := range jar.Cookies(&url.URL{Scheme: scheme, Host: host}) { + out = append(out, storedCookie{Name: c.Name, Value: c.Value, Domain: host, Path: c.Path, Expires: c.Expires}) + } + } + } + data, err := json.MarshalIndent(out, "", " ") + if err != nil { + return err + } + return os.WriteFile(path, data, 0o600) +} diff --git a/cmd/webtest/session_test.go b/cmd/webtest/session_test.go new file mode 100644 index 0000000..7c346eb --- /dev/null +++ b/cmd/webtest/session_test.go @@ -0,0 +1,83 @@ +// Copyright (c) the go-webengine/webtest authors. +// SPDX-License-Identifier: BSD-3-Clause + +package main + +import ( + "net/http" + "net/url" + "os" + "path/filepath" + "testing" +) + +func TestLoadCookieJarMissingFileIsEmptyNotError(t *testing.T) { + jar, err := loadCookieJar(filepath.Join(t.TempDir(), "nope.json")) + if err != nil { + t.Fatalf("loadCookieJar of a missing file: %v, want a fresh empty jar", err) + } + if got := jar.Cookies(&url.URL{Scheme: "https", Host: "example.test"}); len(got) != 0 { + t.Fatalf("fresh jar already has cookies: %v", got) + } +} + +func TestLoadCookieJarRefusesWrongPermissions(t *testing.T) { + path := filepath.Join(t.TempDir(), "cookies.json") + if err := os.WriteFile(path, []byte(`[]`), 0o644); err != nil { + t.Fatal(err) + } + if _, err := loadCookieJar(path); err == nil { + t.Fatal("loadCookieJar of a 0644 (group/world-readable) cookie file: want a refusal, not a silent read") + } +} + +func TestSaveAndLoadCookieJarRoundtrip(t *testing.T) { + path := filepath.Join(t.TempDir(), "cookies.json") + jar, err := loadCookieJar(filepath.Join(t.TempDir(), "nope.json")) // fresh empty jar + if err != nil { + t.Fatal(err) + } + u := &url.URL{Scheme: "https", Host: "example.test"} + jar.SetCookies(u, []*http.Cookie{{Name: "session", Value: "abc123", Path: "/"}}) + + if err := saveCookieJar(path, jar, []string{"example.test"}); err != nil { + t.Fatalf("saveCookieJar: %v", err) + } + info, err := os.Stat(path) + if err != nil { + t.Fatal(err) + } + if mode := info.Mode().Perm(); mode != 0o600 { + t.Fatalf("saved cookie file mode = %#o, want 0600", mode) + } + + reloaded, err := loadCookieJar(path) + if err != nil { + t.Fatalf("loadCookieJar after save: %v", err) + } + got := reloaded.Cookies(u) + if len(got) != 1 || got[0].Name != "session" || got[0].Value != "abc123" { + t.Fatalf("reloaded cookies = %v, want [session=abc123]", got) + } +} + +func TestSaveCookieJarOnlyNamedHosts(t *testing.T) { + path := filepath.Join(t.TempDir(), "cookies.json") + jar, _ := loadCookieJar(filepath.Join(t.TempDir(), "nope.json")) + jar.SetCookies(&url.URL{Scheme: "https", Host: "a.test"}, []*http.Cookie{{Name: "x", Value: "1", Path: "/"}}) + jar.SetCookies(&url.URL{Scheme: "https", Host: "b.test"}, []*http.Cookie{{Name: "y", Value: "2", Path: "/"}}) + + if err := saveCookieJar(path, jar, []string{"a.test"}); err != nil { + t.Fatal(err) + } + reloaded, err := loadCookieJar(path) + if err != nil { + t.Fatal(err) + } + if got := reloaded.Cookies(&url.URL{Scheme: "https", Host: "a.test"}); len(got) != 1 { + t.Fatalf("a.test cookies = %v, want 1", got) + } + if got := reloaded.Cookies(&url.URL{Scheme: "https", Host: "b.test"}); len(got) != 0 { + t.Fatalf("b.test cookies = %v, want 0 (not in the named hosts list)", got) + } +}