Files

141 lines
5.0 KiB
Go

package app
import (
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/gin-gonic/gin"
"github.com/oapi-codegen/nullable"
"github.com/phishingclub/phishingclub/middleware"
"github.com/phishingclub/phishingclub/model"
"github.com/phishingclub/phishingclub/vo"
"go.uber.org/zap"
)
func TestDomainRedirectStatus(t *testing.T) {
tests := []struct {
name string
host string
path string
target string
status int
ok bool
}{
{"empty target", "a.test", "/", "", 0, false},
{"relative other path", "a.test", "/x", "/landing", http.StatusFound, true},
{"relative same path", "a.test", "/landing", "/landing", 0, false},
{"relative same path with query", "a.test", "/landing", "/landing?a=1", 0, false},
{"relative same path after clean", "a.test", "/foo/../landing/", "/landing", 0, false},
{"relative root same path", "a.test", "/", "/", 0, false},
{"relative root from other path", "a.test", "/x", "/", http.StatusFound, true},
{"absolute other host", "a.test", "/x", "https://b.test/x", http.StatusMovedPermanently, true},
{"absolute same host other path", "a.test", "/x", "https://a.test/y", http.StatusMovedPermanently, true},
{"absolute same host same path", "a.test", "/x", "https://A.test/x", 0, false},
{"absolute same host root", "a.test", "/", "https://a.test", 0, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
status, ok := domainRedirectStatus(tt.host, tt.path, tt.target)
if ok != tt.ok || status != tt.status {
t.Fatalf("got (%d, %v), want (%d, %v)", status, ok, tt.status, tt.ok)
}
})
}
}
func TestGetHostOnlyLowercases(t *testing.T) {
s := &Server{}
cases := map[string]string{
"micro.test": "micro.test",
"MIcro.Test": "micro.test",
"MIcro.Test:8000": "micro.test",
"10.0.0.1:80": "10.0.0.1",
"[FE80::1]:443": "fe80::1",
"micro.test.": "micro.test.", // trailing dot is kept, it does not match a stored name
"MIcro.Test.:8000": "micro.test.",
}
for in, want := range cases {
got, err := s.getHostOnly(in)
if err != nil {
t.Errorf("getHostOnly(%q) error: %v", in, err)
continue
}
if got != want {
t.Errorf("getHostOnly(%q) = %q, want %q", in, got, want)
}
}
}
func TestHandlerLowercasesRequestHost(t *testing.T) {
gin.SetMode(gin.TestMode)
s := &Server{remoteBrowserWSPath: "rbpath", logger: zap.NewNop().Sugar()}
var seen string
r := gin.New()
r.Use(s.Handler)
// the remote browser prefix makes Handler pass through right after the host
// is lowercased, so the next handler sees the rewritten host
r.GET("/rbpath/x", func(c *gin.Context) {
seen = c.Request.Host
c.Status(http.StatusOK)
})
req := httptest.NewRequest(http.MethodGet, "/rbpath/x", nil)
req.Host = "MIcro.Test:8000"
r.ServeHTTP(httptest.NewRecorder(), req)
if seen != "micro.test:8000" {
t.Fatalf("request host = %q, want %q", seen, "micro.test:8000")
}
}
// TestDenyPageLogFieldsAnonymous checks the deny page debug log names the
// recipient only on a campaign known not to be anonymous
func TestDenyPageLogFieldsAnonymous(t *testing.T) {
recipient := &model.Recipient{Email: nullable.NewNullableWithValue(*vo.NewEmailMust("alice@example.com"))}
cases := map[string]struct {
anonymous nullable.Nullable[bool]
named bool
}{
"normal": {nullable.NewNullableWithValue(false), true},
"anonymous": {nullable.NewNullableWithValue(true), false},
"unknown": {nullable.Nullable[bool]{}, false},
}
for name, c := range cases {
campaign := &model.Campaign{IsAnonymous: c.anonymous}
text := fmt.Sprint(denyPageLogFields(campaign, recipient, "page", "id"))
if strings.Contains(text, "alice@example.com") != c.named || strings.Contains(text, "recipientEmail") != c.named {
t.Fatalf("%s: fields %s", name, text)
}
}
if text := fmt.Sprint(denyPageLogFields(nil, recipient, "page", "id")); strings.Contains(text, "alice") {
t.Fatalf("no campaign: fields %s", text)
}
}
// TestSetJA4IgnoresClientHeader checks the fingerprint header a client sends
// is never kept: it is replaced by the fingerprint of the connection, or
// removed when the connection has none
func TestSetJA4IgnoresClientHeader(t *testing.T) {
gin.SetMode(gin.TestMode)
s := &Server{logger: zap.NewNop().Sugar(), ja4Middleware: &middleware.JA4Middleware{}}
s.ja4Middleware.ConnectionFingerprints.Store("192.0.2.10:40000", &middleware.FingerprintEntry{Fingerprint: "t13d_real"})
for remoteAddr, want := range map[string]string{
"192.0.2.10:40000": "t13d_real",
"192.0.2.99:40000": "",
} {
g, _ := gin.CreateTestContext(httptest.NewRecorder())
g.Request = httptest.NewRequest(http.MethodGet, "/rbpath/a/b", nil)
g.Request.RemoteAddr = remoteAddr
g.Request.Header.Set(middleware.HeaderJA4, "t13d_forged")
s.setJA4(g)
if got := g.Request.Header.Get(middleware.HeaderJA4); got != want {
t.Errorf("%s: header is %q, want %q", remoteAddr, got, want)
}
if got := g.GetString(middleware.ContextKeyJA4); got != want {
t.Errorf("%s: context value is %q, want %q", remoteAddr, got, want)
}
}
}