350 lines
14 KiB
Go
350 lines
14 KiB
Go
package server
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/maspend/hostora/internal/store"
|
|
)
|
|
|
|
// seedTenant menyiapkan admin + dua pengguna biasa (a dan b) beserta
|
|
// satu situs milik masing-masing, lalu mengembalikan cookie ketiganya.
|
|
func seedTenant(t *testing.T, srv *Server) (admin, userA, userB *http.Cookie) {
|
|
t.Helper()
|
|
seedUser(t, srv, "admin", "", "admin", "active", "adminpass123")
|
|
seedUser(t, srv, "usera", "a@example.com", "user", "active", "rahasia123")
|
|
seedUser(t, srv, "userb", "b@example.com", "user", "active", "rahasia123")
|
|
err := srv.store.Update(func(d *store.Data) error {
|
|
d.Sites = append(d.Sites,
|
|
store.Site{ID: "site-a", Domain: "a.com", PHP: "8.3", Root: "/var/www/a.com", Owner: "usera", Status: "active", CreatedAt: time.Now()},
|
|
store.Site{ID: "site-b", Domain: "b.com", PHP: "8.3", Root: "/var/www/b.com", Owner: "userb", Status: "active", CreatedAt: time.Now()},
|
|
)
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("seed sites: %v", err)
|
|
}
|
|
return sessionCookie(srv, "admin"), sessionCookie(srv, "usera"), sessionCookie(srv, "userb")
|
|
}
|
|
|
|
func decodeList(t *testing.T, rec *httptest.ResponseRecorder) []map[string]any {
|
|
t.Helper()
|
|
var out []map[string]any
|
|
if err := json.NewDecoder(rec.Body).Decode(&out); err != nil {
|
|
t.Fatalf("decode list: %v (body: %s)", err, rec.Body.String())
|
|
}
|
|
return out
|
|
}
|
|
|
|
func TestSiteScoping(t *testing.T) {
|
|
srv := newTestServer(t)
|
|
admin, userA, _ := seedTenant(t, srv)
|
|
|
|
// List sebagai user A: hanya situs miliknya.
|
|
rec := doJSON(t, srv.handleSites, http.MethodGet, "/api/sites", nil, userA, "")
|
|
if rec.Code != http.StatusOK {
|
|
t.Fatalf("GET /api/sites sebagai A: got %d", rec.Code)
|
|
}
|
|
rows := decodeList(t, rec)
|
|
if len(rows) != 1 || rows[0]["domain"] != "a.com" {
|
|
t.Errorf("list situs A: got %v, want hanya a.com", rows)
|
|
}
|
|
// List sebagai admin: semua.
|
|
rec = doJSON(t, srv.handleSites, http.MethodGet, "/api/sites", nil, admin, "")
|
|
if len(decodeList(t, rec)) != 2 {
|
|
t.Errorf("list situs admin: got %d, want 2", len(decodeList(t, rec)))
|
|
}
|
|
// GET langsung situs B sebagai A -> 403.
|
|
req := httptest.NewRequest(http.MethodGet, "/api/sites/site-b", nil)
|
|
req.AddCookie(userA)
|
|
req.SetPathValue("id", "site-b")
|
|
rec2 := httptest.NewRecorder()
|
|
srv.handleSiteByID(rec2, req)
|
|
if rec2.Code != http.StatusForbidden {
|
|
t.Errorf("GET situs B sebagai A: got %d, want 403", rec2.Code)
|
|
}
|
|
// DELETE situs B sebagai A -> 403 dan situs tetap ada.
|
|
req = httptest.NewRequest(http.MethodDelete, "/api/sites/site-b", nil)
|
|
req.AddCookie(userA)
|
|
req.SetPathValue("id", "site-b")
|
|
rec2 = httptest.NewRecorder()
|
|
srv.handleSiteByID(rec2, req)
|
|
if rec2.Code != http.StatusForbidden {
|
|
t.Errorf("DELETE situs B sebagai A: got %d, want 403", rec2.Code)
|
|
}
|
|
if _, found := srv.findSite("site-b"); !found {
|
|
t.Error("situs B hilang setelah DELETE yang ditolak")
|
|
}
|
|
// PUT situs sendiri sebagai A: ubah PHP boleh, ubah root tidak.
|
|
req = httptest.NewRequest(http.MethodPut, "/api/sites/site-a", strings.NewReader(`{"php":"8.2"}`))
|
|
req.AddCookie(userA)
|
|
req.SetPathValue("id", "site-a")
|
|
rec2 = httptest.NewRecorder()
|
|
srv.handleSiteByID(rec2, req)
|
|
if rec2.Code != http.StatusOK {
|
|
t.Errorf("PUT situs sendiri (php): got %d, want 200 (%s)", rec2.Code, rec2.Body.String())
|
|
}
|
|
req = httptest.NewRequest(http.MethodPut, "/api/sites/site-a", strings.NewReader(`{"root":"/etc"}`))
|
|
req.AddCookie(userA)
|
|
req.SetPathValue("id", "site-a")
|
|
rec2 = httptest.NewRecorder()
|
|
srv.handleSiteByID(rec2, req)
|
|
if rec2.Code != http.StatusForbidden {
|
|
t.Errorf("PUT root situs sebagai user: got %d, want 403", rec2.Code)
|
|
}
|
|
}
|
|
|
|
func TestDatabaseScoping(t *testing.T) {
|
|
srv := newTestServer(t)
|
|
admin, userA, _ := seedTenant(t, srv)
|
|
_ = srv.store.Update(func(d *store.Data) error {
|
|
d.Databases = append(d.Databases,
|
|
store.Database{ID: "db-legacy", Name: "legacy_db", User: "legacy", Status: "planned", CreatedAt: time.Now()}, // tanpa Owner = milik admin
|
|
store.Database{ID: "db-a", Name: "a_db", User: "a_user", Owner: "usera", Status: "planned", CreatedAt: time.Now()},
|
|
store.Database{ID: "db-b", Name: "b_db", User: "b_user", Owner: "userb", Status: "planned", CreatedAt: time.Now()},
|
|
)
|
|
return nil
|
|
})
|
|
// List sebagai A: hanya db-a (legacy dan db-b tidak terlihat).
|
|
rec := doJSON(t, srv.handleDatabases, http.MethodGet, "/api/databases", nil, userA, "")
|
|
rows := decodeList(t, rec)
|
|
if len(rows) != 1 || rows[0]["name"] != "a_db" {
|
|
t.Errorf("list database A: got %v, want hanya a_db", rows)
|
|
}
|
|
// List sebagai admin: semua (3).
|
|
rec = doJSON(t, srv.handleDatabases, http.MethodGet, "/api/databases", nil, admin, "")
|
|
if len(decodeList(t, rec)) != 3 {
|
|
t.Errorf("list database admin: got %d, want 3", len(decodeList(t, rec)))
|
|
}
|
|
// DELETE db-b sebagai A -> 403.
|
|
req := httptest.NewRequest(http.MethodDelete, "/api/databases/db-b", nil)
|
|
req.AddCookie(userA)
|
|
req.SetPathValue("id", "db-b")
|
|
rec2 := httptest.NewRecorder()
|
|
srv.handleDatabaseByID(rec2, req)
|
|
if rec2.Code != http.StatusForbidden {
|
|
t.Errorf("DELETE database B sebagai A: got %d, want 403", rec2.Code)
|
|
}
|
|
// DELETE database legacy (tanpa Owner) sebagai A -> 403.
|
|
req = httptest.NewRequest(http.MethodDelete, "/api/databases/db-legacy", nil)
|
|
req.AddCookie(userA)
|
|
req.SetPathValue("id", "db-legacy")
|
|
rec2 = httptest.NewRecorder()
|
|
srv.handleDatabaseByID(rec2, req)
|
|
if rec2.Code != http.StatusForbidden {
|
|
t.Errorf("DELETE database legacy sebagai A: got %d, want 403", rec2.Code)
|
|
}
|
|
}
|
|
|
|
func TestDNSZoneScoping(t *testing.T) {
|
|
srv := newTestServer(t)
|
|
_, userA, _ := seedTenant(t, srv)
|
|
_ = srv.store.Update(func(d *store.Data) error {
|
|
d.DNSZones = append(d.DNSZones,
|
|
store.DNSZone{Domain: "a.com", CreatedAt: time.Now()},
|
|
store.DNSZone{Domain: "b.com", CreatedAt: time.Now()},
|
|
store.DNSZone{Domain: "sub.a.com", CreatedAt: time.Now()},
|
|
)
|
|
return nil
|
|
})
|
|
// List sebagai A: a.com + sub.a.com saja.
|
|
rec := doJSON(t, srv.handleDNSZones, http.MethodGet, "/api/dns", nil, userA, "")
|
|
rows := decodeList(t, rec)
|
|
if len(rows) != 2 {
|
|
t.Errorf("list zona A: got %d zona, want 2 (a.com, sub.a.com)", len(rows))
|
|
}
|
|
// GET zona b.com sebagai A -> 403.
|
|
req := httptest.NewRequest(http.MethodGet, "/api/dns/b.com", nil)
|
|
req.AddCookie(userA)
|
|
req.SetPathValue("domain", "b.com")
|
|
rec2 := httptest.NewRecorder()
|
|
srv.handleDNSZone(rec2, req)
|
|
if rec2.Code != http.StatusForbidden {
|
|
t.Errorf("GET zona b.com sebagai A: got %d, want 403", rec2.Code)
|
|
}
|
|
// Buat zona untuk domain orang lain -> 403.
|
|
rec = doJSON(t, srv.handleDNSZones, http.MethodPost, "/api/dns",
|
|
map[string]string{"domain": "b.com"}, userA, "")
|
|
if rec.Code != http.StatusForbidden {
|
|
t.Errorf("POST zona b.com sebagai A: got %d, want 403", rec.Code)
|
|
}
|
|
// Buat zona subdomain dari situs sendiri -> 201.
|
|
rec = doJSON(t, srv.handleDNSZones, http.MethodPost, "/api/dns",
|
|
map[string]string{"domain": "blog.a.com"}, userA, "")
|
|
if rec.Code != http.StatusCreated {
|
|
t.Errorf("POST zona blog.a.com sebagai A: got %d, want 201 (%s)", rec.Code, rec.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestFileRootsScoping(t *testing.T) {
|
|
srv := newTestServer(t)
|
|
admin, userA, _ := seedTenant(t, srv)
|
|
// Admin: ada "File panel" + kedua situs.
|
|
rec := doJSON(t, srv.handleFileRoots, http.MethodGet, "/api/files/roots", nil, admin, "")
|
|
body := rec.Body.String()
|
|
if !strings.Contains(body, "File panel") || !strings.Contains(body, "a.com") || !strings.Contains(body, "b.com") {
|
|
t.Errorf("roots admin tidak lengkap: %s", body)
|
|
}
|
|
// User A: hanya situsnya sendiri, tanpa "File panel"/Logs/tmp/b.com.
|
|
rec = doJSON(t, srv.handleFileRoots, http.MethodGet, "/api/files/roots", nil, userA, "")
|
|
body = rec.Body.String()
|
|
if strings.Contains(body, "File panel") || strings.Contains(body, "b.com") || strings.Contains(body, "Logs") {
|
|
t.Errorf("roots user A membocorkan akar lain: %s", body)
|
|
}
|
|
if !strings.Contains(body, "a.com") {
|
|
t.Errorf("roots user A tidak berisi situsnya sendiri: %s", body)
|
|
}
|
|
// List file di root situs orang lain -> ditolak.
|
|
rec = doJSON(t, srv.handleFileList, http.MethodGet, "/api/files/list?root=/var/www/b.com&path=", nil, userA, "")
|
|
if rec.Code != http.StatusBadRequest {
|
|
t.Errorf("list root b.com sebagai A: got %d, want 400", rec.Code)
|
|
}
|
|
}
|
|
|
|
func TestAdminOnlyEndpoints(t *testing.T) {
|
|
srv := newTestServer(t)
|
|
_, userA, _ := seedTenant(t, srv)
|
|
cases := []struct {
|
|
name string
|
|
handler http.HandlerFunc
|
|
method string
|
|
target string
|
|
}{
|
|
{"nameservers", srv.handleNameservers, http.MethodGet, "/api/nameservers"},
|
|
{"dns/status", srv.handleDNSStatus, http.MethodGet, "/api/dns/status"},
|
|
{"dns/sync", srv.handleDNSSync, http.MethodPost, "/api/dns/sync"},
|
|
{"mail/status", srv.handleMailStatus, http.MethodGet, "/api/mail/status"},
|
|
{"smtp", srv.handleSMTP, http.MethodGet, "/api/smtp"},
|
|
{"smtp/test", srv.handleSMTPTest, http.MethodPost, "/api/smtp/test"},
|
|
{"transfer/connect", srv.handleTransferConnect, http.MethodPost, "/api/transfer/connect"},
|
|
{"transfers", srv.handleTransfers, http.MethodGet, "/api/transfers"},
|
|
{"transfer/run", srv.handleTransferRun, http.MethodPost, "/api/transfer/run"},
|
|
{"dashboard", srv.handleDashboard, http.MethodGet, "/api/dashboard"},
|
|
{"activity", srv.handleActivity, http.MethodGet, "/api/activity"},
|
|
{"users", srv.handleUsers, http.MethodGet, "/api/users"},
|
|
{"scan", srv.handleScan, http.MethodGet, "/api/scan"},
|
|
{"waf/log", srv.handleWAFLog, http.MethodGet, "/api/waf/log"},
|
|
}
|
|
for _, c := range cases {
|
|
rec := doJSON(t, c.handler, c.method, c.target, nil, userA, "")
|
|
if rec.Code != http.StatusForbidden {
|
|
t.Errorf("%s sebagai user: got %d, want 403", c.name, rec.Code)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestMailboxScoping(t *testing.T) {
|
|
srv := newTestServer(t)
|
|
_, userA, _ := seedTenant(t, srv)
|
|
_ = srv.store.Update(func(d *store.Data) error {
|
|
d.Mailboxes = append(d.Mailboxes,
|
|
store.Mailbox{ID: "mb-a", Address: "halo@a.com", Status: "active", MTAStatus: "planned", CreatedAt: time.Now()},
|
|
store.Mailbox{ID: "mb-b", Address: "halo@b.com", Status: "active", MTAStatus: "planned", CreatedAt: time.Now()},
|
|
)
|
|
return nil
|
|
})
|
|
// List sebagai A: hanya halo@a.com.
|
|
rec := doJSON(t, srv.handleMailboxes, http.MethodGet, "/api/mailboxes", nil, userA, "")
|
|
rows := decodeList(t, rec)
|
|
if len(rows) != 1 || rows[0]["address"] != "halo@a.com" {
|
|
t.Errorf("list mailbox A: got %v, want hanya halo@a.com", rows)
|
|
}
|
|
// Buat mailbox di domain orang lain -> 403.
|
|
rec = doJSON(t, srv.handleMailboxes, http.MethodPost, "/api/mailboxes",
|
|
map[string]string{"address": "x@b.com", "password": "rahasia123"}, userA, "")
|
|
if rec.Code != http.StatusForbidden {
|
|
t.Errorf("POST mailbox b.com sebagai A: got %d, want 403", rec.Code)
|
|
}
|
|
// Hapus mailbox orang lain -> 403.
|
|
req := httptest.NewRequest(http.MethodDelete, "/api/mailboxes/mb-b", nil)
|
|
req.AddCookie(userA)
|
|
req.SetPathValue("id", "mb-b")
|
|
rec2 := httptest.NewRecorder()
|
|
srv.handleMailboxByID(rec2, req)
|
|
if rec2.Code != http.StatusForbidden {
|
|
t.Errorf("DELETE mailbox b.com sebagai A: got %d, want 403", rec2.Code)
|
|
}
|
|
}
|
|
|
|
func TestCronScoping(t *testing.T) {
|
|
srv := newTestServer(t)
|
|
_, userA, _ := seedTenant(t, srv)
|
|
_ = srv.store.Update(func(d *store.Data) error {
|
|
d.CronJobs = append(d.CronJobs,
|
|
store.CronJob{ID: "cron-a", Name: "tugas A", Schedule: "0 3 * * *", Command: "echo a", Owner: "usera", Enabled: true, CreatedAt: time.Now()},
|
|
store.CronJob{ID: "cron-b", Name: "tugas B", Schedule: "0 3 * * *", Command: "echo b", Owner: "userb", Enabled: true, CreatedAt: time.Now()},
|
|
)
|
|
return nil
|
|
})
|
|
// List sebagai A: hanya cron-a.
|
|
rec := doJSON(t, srv.handleCron, http.MethodGet, "/api/cron", nil, userA, "")
|
|
rows := decodeList(t, rec)
|
|
if len(rows) != 1 || rows[0]["name"] != "tugas A" {
|
|
t.Errorf("list cron A: got %v, want hanya tugas A", rows)
|
|
}
|
|
// PUT cron B sebagai A -> 403.
|
|
req := httptest.NewRequest(http.MethodPut, "/api/cron/cron-b", strings.NewReader(`{"name":"diubah"}`))
|
|
req.AddCookie(userA)
|
|
req.SetPathValue("id", "cron-b")
|
|
rec2 := httptest.NewRecorder()
|
|
srv.handleCronByID(rec2, req)
|
|
if rec2.Code != http.StatusForbidden {
|
|
t.Errorf("PUT cron B sebagai A: got %d, want 403", rec2.Code)
|
|
}
|
|
// DELETE cron B sebagai A -> 403.
|
|
req = httptest.NewRequest(http.MethodDelete, "/api/cron/cron-b", nil)
|
|
req.AddCookie(userA)
|
|
req.SetPathValue("id", "cron-b")
|
|
rec2 = httptest.NewRecorder()
|
|
srv.handleCronByID(rec2, req)
|
|
if rec2.Code != http.StatusForbidden {
|
|
t.Errorf("DELETE cron B sebagai A: got %d, want 403", rec2.Code)
|
|
}
|
|
// Buat cron dengan site milik orang lain -> 403.
|
|
rec = doJSON(t, srv.handleCron, http.MethodPost, "/api/cron",
|
|
map[string]string{"name": "x", "schedule": "0 3 * * *", "command": "echo x", "site": "site-b"}, userA, "")
|
|
if rec.Code != http.StatusForbidden {
|
|
t.Errorf("POST cron site B sebagai A: got %d, want 403", rec.Code)
|
|
}
|
|
}
|
|
|
|
func TestBackupScoping(t *testing.T) {
|
|
srv := newTestServer(t)
|
|
_, userA, _ := seedTenant(t, srv)
|
|
_ = srv.store.Update(func(d *store.Data) error {
|
|
d.Backups = append(d.Backups,
|
|
store.Backup{ID: "bk-a", SiteID: "site-a", Domain: "a.com", File: "a.tar.gz", Status: "ok", CreatedAt: time.Now()},
|
|
store.Backup{ID: "bk-b", SiteID: "site-b", Domain: "b.com", File: "b.tar.gz", Status: "ok", CreatedAt: time.Now()},
|
|
)
|
|
return nil
|
|
})
|
|
// List sebagai A: hanya bk-a.
|
|
rec := doJSON(t, srv.handleBackups, http.MethodGet, "/api/backups", nil, userA, "")
|
|
rows := decodeList(t, rec)
|
|
if len(rows) != 1 || rows[0]["domain"] != "a.com" {
|
|
t.Errorf("list backup A: got %v, want hanya a.com", rows)
|
|
}
|
|
// Download bk-b sebagai A -> 403.
|
|
req := httptest.NewRequest(http.MethodGet, "/api/backups/bk-b/download", nil)
|
|
req.AddCookie(userA)
|
|
req.SetPathValue("id", "bk-b")
|
|
rec2 := httptest.NewRecorder()
|
|
srv.handleBackupDownload(rec2, req)
|
|
if rec2.Code != http.StatusForbidden {
|
|
t.Errorf("download backup B sebagai A: got %d, want 403", rec2.Code)
|
|
}
|
|
// Restore bk-b sebagai A -> 403.
|
|
req = httptest.NewRequest(http.MethodPost, "/api/backups/bk-b/restore", nil)
|
|
req.AddCookie(userA)
|
|
req.SetPathValue("id", "bk-b")
|
|
rec2 = httptest.NewRecorder()
|
|
srv.handleBackupRestore(rec2, req)
|
|
if rec2.Code != http.StatusForbidden {
|
|
t.Errorf("restore backup B sebagai A: got %d, want 403", rec2.Code)
|
|
}
|
|
}
|