Document engine redistribution requirements; exclude upstream engine assets and keep the viewer disabled by default.
285 lines
8.1 KiB
Go
285 lines
8.1 KiB
Go
package geckorelay
|
|
|
|
import (
|
|
"context"
|
|
"encoding/binary"
|
|
"io"
|
|
"net"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"net/netip"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
|
|
"golang.org/x/net/websocket"
|
|
)
|
|
|
|
const testKey = "synthetic-relay-key-at-least-32-bytes-long"
|
|
|
|
func TestAssetsPin(t *testing.T) {
|
|
dir := t.TempDir()
|
|
if VerifyAssets(dir) == nil {
|
|
t.Fatal("missing assets accepted")
|
|
}
|
|
for _, name := range []string{"gecko.js", "gecko.wasm.zst"} {
|
|
if err := os.WriteFile(filepath.Join(dir, name), []byte("untrusted"), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
if VerifyAssets(dir) == nil {
|
|
t.Fatal("unverified assets accepted")
|
|
}
|
|
if assets := os.Getenv("PAGESWIKI_GECKO_TEST_ASSETS"); assets != "" {
|
|
if err := VerifyAssets(assets); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestGrant(t *testing.T) {
|
|
now := time.Now()
|
|
g := Grant{"domain:actor", "0123456789abcdef", now.Add(30 * time.Second).Unix()}
|
|
token := Sign(testKey, g)
|
|
if got, e := Verify(testKey, token, now); e != nil || got != g {
|
|
t.Fatal(got, e)
|
|
}
|
|
for _, bad := range []string{token + "x", "", strings.Repeat("x", 2049), Sign(testKey, Grant{g.Subject, g.Nonce, now.Add(2 * time.Minute).Unix()}), Sign(testKey, Grant{g.Subject, g.Nonce, now.Unix()})} {
|
|
if _, e := Verify(testKey, bad, now); e == nil {
|
|
t.Fatal("accepted invalid grant")
|
|
}
|
|
}
|
|
if _, e := Verify("other-key-at-least-32-bytes-long!!", token, now); e == nil {
|
|
t.Fatal("wrong key")
|
|
}
|
|
}
|
|
|
|
func TestDestinations(t *testing.T) {
|
|
for _, host := range []string{"127.0.0.1", "localhost", "host.localhost", "10.0.0.1", "169.254.169.254", "100.64.0.1", "192.0.2.2", "198.19.0.1", "::1", "::ffff:127.0.0.1", "64:ff9b::808:808", "2002:808:808::1", "2001:db8::1", "3fff::1", "fd00::1", "fe80::1%eth0", "bad/host", "example.org\x00.evil", "singlelabel"} {
|
|
if ValidDestination(host, 443) {
|
|
t.Fatalf("allowed %q", host)
|
|
}
|
|
}
|
|
for _, host := range []string{"example.org", "EXAMPLE.org.", "8.8.8.8", "2606:4700:4700::1111"} {
|
|
if !ValidDestination(host, 443) {
|
|
t.Fatal("blocked", host)
|
|
}
|
|
}
|
|
if ValidDestination("example.org", 22) {
|
|
t.Fatal("port allowed")
|
|
}
|
|
called := false
|
|
dial := func(_ context.Context, _, addr string) (net.Conn, error) {
|
|
called = true
|
|
if addr != "8.8.8.8:443" {
|
|
t.Fatal("unvalidated DNS re-resolution", addr)
|
|
}
|
|
a, b := net.Pipe()
|
|
b.Close()
|
|
return a, nil
|
|
}
|
|
lookup := func(_ context.Context, _, _ string) ([]netip.Addr, error) {
|
|
return []netip.Addr{netip.MustParseAddr("8.8.8.8"), netip.MustParseAddr("127.0.0.1")}, nil
|
|
}
|
|
if _, err := dialPublic(context.Background(), "example.org", 443, lookup, dial); err == nil || called {
|
|
t.Fatal("mixed DNS allowed")
|
|
}
|
|
lookup = func(_ context.Context, _, _ string) ([]netip.Addr, error) {
|
|
return []netip.Addr{netip.MustParseAddr("8.8.8.8")}, nil
|
|
}
|
|
c, err := dialPublic(context.Background(), "example.org", 443, lookup, dial)
|
|
if err != nil || !called {
|
|
t.Fatal(err)
|
|
}
|
|
c.Close()
|
|
}
|
|
|
|
func TestRelayFramesAndLimits(t *testing.T) {
|
|
r, _ := New(testKey, t.TempDir())
|
|
r.dial = func(ctx context.Context, host string, port uint16) (net.Conn, error) {
|
|
a, b := net.Pipe()
|
|
go func() { defer b.Close(); io.Copy(b, b) }()
|
|
return a, nil
|
|
}
|
|
s := httptest.NewServer(r)
|
|
defer s.Close()
|
|
grant := Sign(testKey, Grant{"domain:actor", "nonce-first-connection", time.Now().Add(30 * time.Second).Unix()})
|
|
dial := func(token string) (*websocket.Conn, error) {
|
|
cfg, _ := websocket.NewConfig("ws"+strings.TrimPrefix(s.URL, "http")+"/wisp/", "https://pages.example")
|
|
cfg.Header.Set(GrantHeader, token)
|
|
return websocket.DialConfig(cfg)
|
|
}
|
|
if c, e := dial(""); e == nil {
|
|
c.Close()
|
|
t.Fatal("anonymous websocket")
|
|
}
|
|
c, err := dial(grant)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer c.Close()
|
|
c.SetDeadline(time.Now().Add(5 * time.Second))
|
|
receive := func() []byte {
|
|
var p []byte
|
|
if e := websocket.Message.Receive(c, &p); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
return p
|
|
}
|
|
p := receive()
|
|
if p[0] != 3 || binary.LittleEndian.Uint32(p[1:]) != 0 || binary.LittleEndian.Uint32(p[5:]) != window {
|
|
t.Fatal(p)
|
|
}
|
|
if second, e := dial(grant); e == nil {
|
|
second.Close()
|
|
t.Fatal("duplicate actor/replayed grant")
|
|
}
|
|
connect := func(id uint32, host string, port uint16, kind byte) {
|
|
body := []byte{kind, byte(port), byte(port >> 8)}
|
|
body = append(body, host...)
|
|
if e := websocket.Message.Send(c, packet(1, id, body)); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
}
|
|
connect(1, "127.0.0.1", 80, 1)
|
|
if p = receive(); p[0] != 4 || p[5] != 0x48 {
|
|
t.Fatal(p)
|
|
}
|
|
connect(2, "example.org", 443, 2)
|
|
if p = receive(); p[0] != 4 {
|
|
t.Fatal("UDP accepted")
|
|
}
|
|
connect(3, "example.org", 443, 1)
|
|
for n := 0; n < window; n++ {
|
|
if e := websocket.Message.Send(c, packet(2, 3, []byte("hello"))); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
}
|
|
got := 0
|
|
creditSeen := false
|
|
for got < window*5 || !creditSeen {
|
|
p = receive()
|
|
switch p[0] {
|
|
case 2:
|
|
got += len(p) - 5
|
|
case 3:
|
|
creditSeen = true
|
|
default:
|
|
t.Fatal(p)
|
|
}
|
|
}
|
|
// Wisp credits count DATA frames, including empty payloads.
|
|
for n := 0; n < window; n++ {
|
|
if e := websocket.Message.Send(c, packet(2, 3, nil)); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
}
|
|
if p = receive(); p[0] != 3 || binary.LittleEndian.Uint32(p[1:]) != 3 {
|
|
t.Fatal("empty frame credit", p)
|
|
}
|
|
websocket.Message.Send(c, packet(4, 3, []byte{2}))
|
|
websocket.Message.Send(c, packet(9, 4, nil))
|
|
var b []byte
|
|
if websocket.Message.Receive(c, &b) == nil && b[0] != 4 {
|
|
t.Fatal("invalid packet accepted")
|
|
}
|
|
resp, e := http.Get(s.URL + "/status")
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
resp.Body.Close()
|
|
if resp.StatusCode != 403 {
|
|
t.Fatal("unprotected metrics")
|
|
}
|
|
}
|
|
|
|
func TestRelayStreamBoundAndCancellation(t *testing.T) {
|
|
r, _ := New(testKey, t.TempDir())
|
|
var dialing atomic.Int64
|
|
r.dial = func(ctx context.Context, host string, port uint16) (net.Conn, error) {
|
|
dialing.Add(1)
|
|
defer dialing.Add(-1)
|
|
<-ctx.Done()
|
|
return nil, ctx.Err()
|
|
}
|
|
server := httptest.NewServer(r)
|
|
defer server.Close()
|
|
cfg, _ := websocket.NewConfig("ws"+strings.TrimPrefix(server.URL, "http")+"/wisp/", "https://pages.example")
|
|
cfg.Header.Set(GrantHeader, Sign(testKey, Grant{"domain:actor", "stream-bound-nonce", time.Now().Add(30 * time.Second).Unix()}))
|
|
c, err := websocket.DialConfig(cfg)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer c.Close()
|
|
c.SetDeadline(time.Now().Add(5 * time.Second))
|
|
var frame []byte
|
|
if err = websocket.Message.Receive(c, &frame); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for id := 1; id <= maxStreams+1; id++ {
|
|
if err = websocket.Message.Send(c, packet(1, uint32(id), append([]byte{1, 80, 0}, []byte("example.org")...))); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
if err = websocket.Message.Receive(c, &frame); err != nil || len(frame) != 6 || frame[0] != 4 || binary.LittleEndian.Uint32(frame[1:]) != maxStreams+1 || frame[5] != 0x49 {
|
|
t.Fatal("stream cap", frame, err)
|
|
}
|
|
c.Close()
|
|
for n := 0; n < 100; n++ {
|
|
r.mu.Lock()
|
|
active := len(r.active)
|
|
r.mu.Unlock()
|
|
if active == 0 && dialing.Load() == 0 {
|
|
return
|
|
}
|
|
time.Sleep(10 * time.Millisecond)
|
|
}
|
|
t.Fatal("closing session did not cancel pending dials")
|
|
}
|
|
|
|
func TestRelaySessionBoundAndReplay(t *testing.T) {
|
|
r, _ := New(testKey, t.TempDir())
|
|
server := httptest.NewServer(r)
|
|
defer server.Close()
|
|
dial := func(subject, nonce string) (*websocket.Conn, error) {
|
|
cfg, _ := websocket.NewConfig("ws"+strings.TrimPrefix(server.URL, "http")+"/wisp/", "https://pages.example")
|
|
cfg.Header.Set(GrantHeader, Sign(testKey, Grant{subject, nonce, time.Now().Add(30 * time.Second).Unix()}))
|
|
return websocket.DialConfig(cfg)
|
|
}
|
|
var conns []*websocket.Conn
|
|
for _, subject := range []string{"one", "two", "three", "four"} {
|
|
c, e := dial(subject, "unique-session-nonce-"+subject)
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
conns = append(conns, c)
|
|
defer c.Close()
|
|
}
|
|
if c, e := dial("five", "unique-session-nonce-five"); e == nil {
|
|
c.Close()
|
|
t.Fatal("global cap exceeded")
|
|
}
|
|
conns[0].Close()
|
|
for n := 0; n < 100; n++ {
|
|
r.mu.Lock()
|
|
active := len(r.active)
|
|
r.mu.Unlock()
|
|
if active == 3 {
|
|
break
|
|
}
|
|
time.Sleep(10 * time.Millisecond)
|
|
}
|
|
if c, e := dial("one", "unique-session-nonce-one"); e == nil {
|
|
c.Close()
|
|
t.Fatal("replayed after close")
|
|
}
|
|
c, e := dial("five", "unique-session-nonce-retry")
|
|
if e != nil {
|
|
t.Fatal("slot not released", e)
|
|
}
|
|
c.Close()
|
|
}
|