Initial upload to Gitea
This commit is contained in:
1 parent
78193254ad
commit
8478998cae
1485 files changed
+461025
-42348
No files matched your search
@@ -58,6 +58,24 @@ func decodeJSONBody(t *testing.T, rr *httptest.ResponseRecorder) map[string]any
|
||||
return m
|
||||
}
|
||||
|
||||
// allowedTempMountDir returns a real, writable directory that passes
|
||||
// pathAllowedForDisk. t.TempDir() lives under /tmp (or $TMPDIR), which the
|
||||
// system-tree deny list now rejects, so the mount is created under the test
|
||||
// binary's working directory instead and removed on cleanup.
|
||||
func allowedTempMountDir(t *testing.T) string {
|
||||
t.Helper()
|
||||
wd, err := os.Getwd()
|
||||
if err != nil {
|
||||
t.Skipf("no working dir for a mount outside the denied system trees: %v", err)
|
||||
}
|
||||
dir, err := os.MkdirTemp(wd, "adpanel-disk-mount-")
|
||||
if err != nil {
|
||||
t.Skipf("cannot create a mount outside the denied system trees: %v", err)
|
||||
}
|
||||
t.Cleanup(func() { _ = os.RemoveAll(dir) })
|
||||
return filepath.Clean(dir)
|
||||
}
|
||||
|
||||
// --- Path traversal / symlink escape rejection across all disk roots ---
|
||||
|
||||
func TestSymlinkEscapeBlockedAcrossDisks(t *testing.T) {
|
||||
@@ -101,7 +119,11 @@ func TestSymlinkEscapeBlockedAcrossDisks(t *testing.T) {
|
||||
|
||||
func TestDiskAdd_NonAdmin403(t *testing.T) {
|
||||
a, _ := newSignedTestAgent(t)
|
||||
mount := t.TempDir()
|
||||
// Must pass pathAllowedForDisk: path validation runs before the admin
|
||||
// check, and no filesystem access happens before the 403, so a
|
||||
// non-existent but allowed path is fine here (t.TempDir() is under /tmp,
|
||||
// which the system-tree deny list rejects).
|
||||
mount := "/data/disks/guard-test"
|
||||
body := map[string]any{"mountPath": mount, "limitGb": 10}
|
||||
buf, _ := json.Marshal(body)
|
||||
req := httptest.NewRequest("POST", "/v1/disks", bytes.NewReader(buf))
|
||||
@@ -119,7 +141,7 @@ func TestDiskAdd_NonAdmin403(t *testing.T) {
|
||||
|
||||
func TestDiskAdd_MissingSignature401(t *testing.T) {
|
||||
a, _ := newSignedTestAgent(t)
|
||||
mount := t.TempDir()
|
||||
mount := "/data/disks/guard-test" // allowed by pathAllowedForDisk; see NonAdmin403
|
||||
body := map[string]any{"mountPath": mount, "limitGb": 10}
|
||||
buf, _ := json.Marshal(body)
|
||||
req := httptest.NewRequest("POST", "/v1/disks", bytes.NewReader(buf))
|
||||
@@ -135,7 +157,7 @@ func TestDiskAdd_MissingSignature401(t *testing.T) {
|
||||
|
||||
func TestDiskAdd_ForgedSignature401(t *testing.T) {
|
||||
a, _ := newSignedTestAgent(t)
|
||||
mount := t.TempDir()
|
||||
mount := "/data/disks/guard-test" // allowed by pathAllowedForDisk; see NonAdmin403
|
||||
req := signedAdminRequest(a, "POST", "/v1/disks", "disk-add", mount,
|
||||
map[string]any{"mountPath": mount, "limitGb": 10})
|
||||
// Tamper with the signature.
|
||||
@@ -150,7 +172,7 @@ func TestDiskAdd_ForgedSignature401(t *testing.T) {
|
||||
|
||||
func TestDiskAdd_ValidSignatureSucceeds(t *testing.T) {
|
||||
a, _ := newSignedTestAgent(t)
|
||||
mount := t.TempDir() // real, writable, allowlisted (TempDir is under /tmp)
|
||||
mount := allowedTempMountDir(t) // real, writable, and outside the denied system trees
|
||||
req := signedAdminRequest(a, "POST", "/v1/disks", "disk-add", filepath.Clean(mount),
|
||||
map[string]any{"mountPath": mount, "limitGb": 1})
|
||||
|
||||
@@ -320,7 +342,7 @@ func TestFSWriteOnSecondDiskSucceeds(t *testing.T) {
|
||||
|
||||
func TestDiskAdd_LimitExceedsFreeSpaceRejected(t *testing.T) {
|
||||
a, _ := newSignedTestAgent(t)
|
||||
mount := t.TempDir()
|
||||
mount := allowedTempMountDir(t) // real dir, or freeDiskMb below is meaningless
|
||||
|
||||
// Determine current free space, then request a limit far above it.
|
||||
freeGb := freeDiskMb(mount) / 1024
|
||||
@@ -343,6 +365,21 @@ func TestDiskAdd_LimitExceedsFreeSpaceRejected(t *testing.T) {
|
||||
|
||||
// --- Disk delete blocked while referenced by a server -> 409 ---
|
||||
|
||||
func TestDiskDeleteRefusesAnUnavailableVolumeDirectory(t *testing.T) {
|
||||
a, _ := newSignedTestAgent(t)
|
||||
mount := t.TempDir()
|
||||
disk := registerTestDisk(t, a, mount, 50)
|
||||
if err := os.Rename(a.diskLayoutVolumes(mount), filepath.Join(mount, "unavailable")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
request := signedAdminRequest(a, "DELETE", "/v1/disks", "disk-remove", disk.ID, map[string]any{"id": disk.ID})
|
||||
response := httptest.NewRecorder()
|
||||
a.handleDiskDelete(response, request)
|
||||
if response.Code != 409 || len(a.registeredDisks()) != 1 {
|
||||
t.Fatalf("unavailable disk lost its registration: %d %s", response.Code, response.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestDiskDeleteBlockedWhileReferenced(t *testing.T) {
|
||||
a, _ := newSignedTestAgent(t)
|
||||
mount := t.TempDir()
|
||||
|
||||
Reference in new issue
Block a user