51 lines
2.1 KiB
Go
51 lines
2.1 KiB
Go
package main
|
|
|
|
import (
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
// adpanel:fs-scope - lock in the server-scoped FS confinement (#2). The generic
|
|
// /v1/fs/* handlers must, when a server name is supplied, reject any path that
|
|
// belongs to a *different* server on the same disk root, even though that path is
|
|
// still under a valid volumes root (so safeUnderVolumes alone would allow it).
|
|
func TestSafeUnderServerOrVolumes_ScopesToSingleServer(t *testing.T) {
|
|
a, vol := newTestAgent(t)
|
|
|
|
alphaFile := filepath.Join(vol, "alpha", "config", "app.cfg")
|
|
betaFile := filepath.Join(vol, "beta", "secrets.txt")
|
|
|
|
// No scope: behaves exactly like safeUnderVolumes (back-compat).
|
|
if got, ok := a.safeUnderServerOrVolumes("", alphaFile); !ok || got != filepath.Clean(alphaFile) {
|
|
t.Fatalf("empty scope should accept any in-volume path, got (%q,%v)", got, ok)
|
|
}
|
|
|
|
// Matching scope: accepted.
|
|
if got, ok := a.safeUnderServerOrVolumes("alpha", alphaFile); !ok || got != filepath.Clean(alphaFile) {
|
|
t.Fatalf("alpha scope should accept alpha path, got (%q,%v)", got, ok)
|
|
}
|
|
|
|
// The server root itself is accepted (e.g. listing the root directory).
|
|
if _, ok := a.safeUnderServerOrVolumes("alpha", a.serverDir("alpha")); !ok {
|
|
t.Fatalf("alpha scope should accept alpha root")
|
|
}
|
|
|
|
// Cross-server access is the key boundary: beta's files must be refused when
|
|
// the scope is alpha, even though betaFile is under a valid volumes root.
|
|
if _, ok := a.safeUnderServerOrVolumes("alpha", betaFile); ok {
|
|
t.Fatalf("alpha scope must reject beta's file %q", betaFile)
|
|
}
|
|
|
|
// A traversal that climbs out of alpha into beta must also be refused.
|
|
escape := filepath.Join(vol, "alpha", "..", "beta", "secrets.txt")
|
|
if _, ok := a.safeUnderServerOrVolumes("alpha", escape); ok {
|
|
t.Fatalf("alpha scope must reject traversal into beta: %q", escape)
|
|
}
|
|
|
|
// An unknown/invalid scope name resolves to a server dir that exists nowhere,
|
|
// so nothing matches and access is denied (fail-closed).
|
|
if _, ok := a.safeUnderServerOrVolumes("does-not-exist", alphaFile); ok {
|
|
t.Fatalf("unknown scope should not match alpha path")
|
|
}
|
|
}
|