mirror of
https://github.com/phishingclub/phishingclub.git
synced 2026-12-06 01:53:42 +01:00
88 lines
3.2 KiB
Go
88 lines
3.2 KiB
Go
package service
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/oapi-codegen/nullable"
|
|
"github.com/phishingclub/phishingclub/database"
|
|
"github.com/phishingclub/phishingclub/model"
|
|
"github.com/phishingclub/phishingclub/repository"
|
|
"github.com/phishingclub/phishingclub/vo"
|
|
)
|
|
|
|
func proxyConfigYAML(to string) string {
|
|
return "version: \"0.0\"\nlogin.example.com:\n to: \"" + to + "\"\n"
|
|
}
|
|
|
|
func newProxyModel(to string) *model.Proxy {
|
|
return &model.Proxy{
|
|
StartURL: nullable.NewNullableWithValue(*vo.NewString1024Must("https://login.example.com/")),
|
|
ProxyConfig: nullable.NewNullableWithValue(*vo.NewString1MBMust(proxyConfigYAML(to))),
|
|
}
|
|
}
|
|
|
|
// TestProxyConfigValidatesToDomain checks each 'to' domain is validated in the
|
|
// config before any domain is created, and that a legacy name already owned by
|
|
// the proxy is kept on update (19)
|
|
func TestProxyConfigValidatesToDomain(t *testing.T) {
|
|
f := newDomainFixture(t)
|
|
|
|
if err := f.proxy.validateProxyConfig(f.ctx, newProxyModel("phish.example.test")); err != nil {
|
|
t.Fatalf("valid config: %v", err)
|
|
}
|
|
cases := map[string]string{
|
|
"bad_name.example.test": "underscores",
|
|
"Upper.example.test": "lowercase",
|
|
testAdminHost: "reserved",
|
|
}
|
|
for to, want := range cases {
|
|
err := f.proxy.validateProxyConfig(f.ctx, newProxyModel(to))
|
|
if err == nil || !strings.Contains(err.Error(), want) {
|
|
t.Errorf("to %q: err = %v, want it to mention %q", to, err, want)
|
|
}
|
|
}
|
|
var count int64
|
|
f.db.Model(&database.Domain{}).Count(&count)
|
|
if count != 0 {
|
|
t.Fatalf("validation created %d domains, want 0", count)
|
|
}
|
|
|
|
// update: a legacy invalid name owned by this proxy is kept
|
|
proxyID := uuid.New()
|
|
f.insertLegacyDomain("legacy_proxy.example.test", "proxy", &proxyID, "")
|
|
if err := f.proxy.validateProxyToDomain(f.ctx, "legacy_proxy.example.test", &proxyID); err != nil {
|
|
t.Fatalf("legacy owned domain: %v", err)
|
|
}
|
|
// owned by another proxy or new, the format check applies
|
|
other := uuid.New()
|
|
if err := f.proxy.validateProxyToDomain(f.ctx, "legacy_proxy.example.test", &other); err == nil {
|
|
t.Fatal("expected legacy name owned by another proxy to be rejected")
|
|
}
|
|
if err := f.proxy.validateProxyToDomain(f.ctx, "new_name.example.test", &proxyID); err == nil {
|
|
t.Fatal("expected new invalid name to be rejected")
|
|
}
|
|
// the reserved check always applies
|
|
f.insertLegacyDomain(testAdminHost, "proxy", &proxyID, "")
|
|
if err := f.proxy.validateProxyToDomain(f.ctx, testAdminHost, &proxyID); err == nil {
|
|
t.Fatal("expected reserved host to be rejected even when owned")
|
|
}
|
|
|
|
// sync keeps a legacy proxy domain: the update path no longer runs the
|
|
// name format check
|
|
legacy := uuid.New()
|
|
legacyID := f.insertLegacyDomain("sync_legacy.example.test", "proxy", &legacy, "")
|
|
existing, err := f.domain.DomainRepository.GetByID(f.ctx, &legacyID, &repository.DomainOption{})
|
|
if err != nil {
|
|
t.Fatalf("get legacy: %v", err)
|
|
}
|
|
existing.ProxyTargetDomain.Set(*vo.NewOptionalString255Must("other.example.com"))
|
|
if err := f.domain.UpdateProxyDomain(f.ctx, f.session, &legacyID, existing); err != nil {
|
|
t.Fatalf("update legacy proxy domain: %v", err)
|
|
}
|
|
if got := f.storedDomain(legacyID).ProxyTargetDomain; got != "other.example.com" {
|
|
t.Fatalf("proxy target = %q, want other.example.com", got)
|
|
}
|
|
}
|