Something went wrong. Try again.
small gleam coding and persistent agent daemon with a detachable cli and a canvas ui forked from okami.mom/albedo
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110package webui
import ( "net/url" "strconv" "strings" "testing"
"albedo/cli/internal/property" "pgregory.net/rapid")
// An origin identity and the browser form of its host, with spellings a flag,// a proxy, or a browser may use for it. The oracle compares identities, so it// shares nothing with the normalization under test.type originHost struct { identity string canonical string spellings []string}
var originHosts = []originHost{ {"tailnet", "albedo.example.ts.net", []string{"albedo.example.ts.net", "Albedo.Example.TS.net", "ALBEDO.EXAMPLE.TS.NET"}}, {"other", "other.example", []string{"other.example", "Other.Example"}}, {"v4", "127.0.0.1", []string{"127.0.0.1"}}, {"loopback6", "[::1]", []string{"[::1]", "[0:0::1]", "[0:0:0:0:0:0:0:1]", "[::0001]"}}, {"doc6", "[2001:db8::1:0:0:1]", []string{"[2001:DB8:0:0:1:0:0:1]", "[2001:db8::1:0:0:1]", "[2001:0db8:0000:0000:0001:0000:0000:0001]"}}, {"mapped6", "[::ffff:7f00:1]", []string{"[::ffff:127.0.0.1]", "[::FFFF:7F00:1]"}},}
type generatedOrigin struct { text string forwardedHost string scheme string host originHost port int}
// The written ports, each with the number it names. Leading zeros are the// same number; a browser never writes them, but a flag may.var originPorts = []struct { text string number int}{{"", 0}, {":80", 80}, {":443", 443}, {":8080", 8080}, {":0443", 443}, {":080", 80}}
func drawOrigin(t *rapid.T, label string) generatedOrigin { scheme := rapid.SampledFrom([]string{"http", "https"}).Draw(t, label+" scheme") written := rapid.SampledFrom([]string{scheme, strings.ToUpper(scheme), strings.ToUpper(scheme[:1]) + scheme[1:]}).Draw(t, label+" scheme spelling") host := rapid.SampledFrom(originHosts).Draw(t, label+" host") spelling := rapid.SampledFrom(host.spellings).Draw(t, label+" spelling") port := rapid.SampledFrom(originPorts).Draw(t, label+" port") slash := rapid.SampledFrom([]string{"", "/"}).Draw(t, label+" slash") space := rapid.SampledFrom([]string{"", " ", "\t"}).Draw(t, label+" space") number := port.number if number == 0 { number = map[string]int{"http": 80, "https": 443}[scheme] } return generatedOrigin{text: space + written + "://" + spelling + port.text + slash + space, forwardedHost: spelling + port.text, scheme: scheme, host: host, port: number}}
// browserForm is how the URL Standard serializes the origin: the default port// left out, the host in its canonical form.func (o generatedOrigin) browserForm() string { form := o.scheme + "://" + o.host.canonical if o.port != map[string]int{"http": 80, "https": 443}[o.scheme] { form += ":" + strconv.Itoa(o.port) } return form}
// TestOriginsCompareByWhatTheyName generates origins from a few identities in// many spellings: the scheme's case, the host's case and IPv6 form, a default// or leading-zero port, a trailing slash, surrounding space. ParseOrigin must// answer the browser's form, reading that form back must change nothing, and// SameOrigin must hold exactly when scheme, host identity, and port number// agree. A page whose flag and Origin header disagree on any of these refuses// every POST it sends.func TestOriginsCompareByWhatTheyName(t *testing.T) { property.Check(t, func(t *rapid.T) { a, b := drawOrigin(t, "a"), drawOrigin(t, "b") parsedA, err := ParseOrigin(a.text) if err != nil { t.Fatalf("ParseOrigin(%q): %v", a.text, err) } parsedB, err := ParseOrigin(b.text) if err != nil { t.Fatalf("ParseOrigin(%q): %v", b.text, err) } if got := parsedA.String(); got != a.browserForm() { t.Fatalf("ParseOrigin(%q) = %q, want %q", a.text, got, a.browserForm()) } again, err := ParseOrigin(parsedA.String()) if err != nil || again.String() != parsedA.String() { t.Fatalf("reading %q back gave %v (%v)", parsedA, again, err) } same := a.scheme == b.scheme && a.host.identity == b.host.identity && a.port == b.port if SameOrigin(parsedA, parsedB) != same { t.Fatalf("SameOrigin(%q, %q) = %v, want %v", a.text, b.text, !same, same) } // A proxy forwards the host as the browser asked for it, unparsed. forwarded := &url.URL{Scheme: b.scheme, Host: b.forwardedHost} if SameOrigin(parsedA, forwarded) != same { t.Fatalf("SameOrigin(%q, forwarded %q) = %v, want %v", a.text, b.forwardedHost, !same, same) } if !SameOrigin(parsedA, parsedA) || SameOrigin(parsedA, nil) { t.Fatalf("%q is its own origin and not nil", parsedA) } })}