mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-11-03 02:32:35 +01:00
* filer: scope a prefix JWT to the directory a zip extract writes under Without a trailing slash, an extract request writes members beside the named path, in its parent directory, while the JWT check only saw the request path. A token scoped to /tenant1 could therefore extract into / by posting to /tenant1?extract=zip. Add the extract root to the paths a prefix-restricted token must cover. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * filer: do not keep an extracted archive under the name ".." A multipart file name of ".." passes path.Base unchanged, so extract.keep stored the archive as an entry literally named ".." in the target directory, a name that local sinks resolve to the parent. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * leveldb3: keep a non-local bucket name out of the per-bucket folder findDB splits the bucket out of a /buckets/ path and uses it as a folder name under the store directory. The name cannot hold a slash and dot names already go to the default DB, so this only rejects an empty or non-local name, but it also lets static analysis see the folder cannot escape the store directory. * filer: clean the zip extract root before scoping and writing A percent-encoded dot segment survives the path cleaning in front of the mux, so a request path ending in "/" reached extractZipRoot with ".." in it. The JWT scope check cleans the path it compares, but the members were written under the raw root, so the two could disagree. * leveldb3: apply the bucket folder check to bucket create and delete events findDB already keeps dot and non-local bucket names out of the per-bucket folders, but OnBucketCreation and OnBucketDeletion used the name as is. A directory event for /buckets/.. opened a DB in the parent of the store directory, and its delete removed that parent. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * leveldb3: keep a bucket delete event off the default DB A bucket named _main maps to the default DB in findDB, so deleting /buckets/_main closed the default DB and removed its folder, taking every non-bucket entry with it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * leveldb3: keep bucket names with a path separator out of the store dir A bucket event named x/../_main passed the local-name check, and its delete removed the default DB folder once store.dir/x existed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * leveldb3: only reject the platform separator in a bucket name A backslash is an ordinary file name character on Unix, and a bucket folder named that way already exists for anyone who created one. Rejecting it on every platform would route those entries to the default DB after an upgrade. The slash can never appear in a bucket name here, and on Windows the backslash is the separator, so rejecting os.PathSeparator alongside the slash keeps the folder flat without changing Unix routing. * leveldb3: check for an escaped bucket db before its delete event --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
333 lines
10 KiB
Go
333 lines
10 KiB
Go
package weed_server
|
|
|
|
import (
|
|
"net/http/httptest"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/golang-jwt/jwt/v5"
|
|
"github.com/seaweedfs/seaweedfs/weed/security"
|
|
)
|
|
|
|
func TestFilerServer_maybeCheckJwtAuthorization_Scoped(t *testing.T) {
|
|
signingKey := "secret"
|
|
filerGuard := security.NewGuard(nil, signingKey, 0, signingKey, 0)
|
|
fs := &FilerServer{
|
|
filerGuard: filerGuard,
|
|
}
|
|
|
|
// Helper to generate token
|
|
genToken := func(allowedPrefixes []string, allowedMethods []string) string {
|
|
claims := security.SeaweedFilerClaims{
|
|
AllowedPrefixes: allowedPrefixes,
|
|
AllowedMethods: allowedMethods,
|
|
RegisteredClaims: jwt.RegisteredClaims{
|
|
ExpiresAt: jwt.NewNumericDate(time.Now().Add(1 * time.Hour)),
|
|
},
|
|
}
|
|
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
|
str, err := token.SignedString([]byte(signingKey))
|
|
if err != nil {
|
|
t.Fatalf("failed to sign token: %v", err)
|
|
}
|
|
return str
|
|
}
|
|
|
|
tests := []struct {
|
|
name string
|
|
token string
|
|
method string
|
|
path string
|
|
header map[string]string
|
|
isWrite bool
|
|
expectAuthorized bool
|
|
}{
|
|
{
|
|
name: "no restrictions",
|
|
token: genToken(nil, nil),
|
|
method: "GET",
|
|
path: "/data/test",
|
|
isWrite: false,
|
|
expectAuthorized: true,
|
|
},
|
|
{
|
|
name: "allowed prefix match",
|
|
token: genToken([]string{"/data"}, nil),
|
|
method: "GET",
|
|
path: "/data/test",
|
|
isWrite: false,
|
|
expectAuthorized: true,
|
|
},
|
|
{
|
|
name: "allowed prefix mismatch",
|
|
token: genToken([]string{"/private"}, nil),
|
|
method: "GET",
|
|
path: "/data/test",
|
|
isWrite: false,
|
|
expectAuthorized: false,
|
|
},
|
|
{
|
|
name: "allowed method match",
|
|
token: genToken(nil, []string{"GET"}),
|
|
method: "GET",
|
|
path: "/data/test",
|
|
isWrite: false,
|
|
expectAuthorized: true,
|
|
},
|
|
{
|
|
name: "allowed method mismatch",
|
|
token: genToken(nil, []string{"POST"}),
|
|
method: "GET",
|
|
path: "/data/test",
|
|
isWrite: false,
|
|
expectAuthorized: false,
|
|
},
|
|
{
|
|
name: "both match",
|
|
token: genToken([]string{"/data"}, []string{"GET"}),
|
|
method: "GET",
|
|
path: "/data/test",
|
|
isWrite: false,
|
|
expectAuthorized: true,
|
|
},
|
|
{
|
|
name: "prefix match, method mismatch",
|
|
token: genToken([]string{"/data"}, []string{"POST"}),
|
|
method: "GET",
|
|
path: "/data/test",
|
|
isWrite: false,
|
|
expectAuthorized: false,
|
|
},
|
|
{
|
|
name: "multiple prefixes match",
|
|
token: genToken([]string{"/other", "/data"}, nil),
|
|
method: "GET",
|
|
path: "/data/test",
|
|
isWrite: false,
|
|
expectAuthorized: true,
|
|
},
|
|
{
|
|
name: "write operation with method restriction",
|
|
token: genToken(nil, []string{"POST", "PUT"}),
|
|
method: "POST",
|
|
path: "/data/upload",
|
|
isWrite: true,
|
|
expectAuthorized: true,
|
|
},
|
|
{
|
|
name: "root listing denied to a prefix restricted token",
|
|
token: genToken([]string{"/data"}, nil),
|
|
method: "GET",
|
|
path: "/",
|
|
isWrite: false,
|
|
expectAuthorized: false,
|
|
},
|
|
{
|
|
name: "root listing denied without token",
|
|
token: "",
|
|
method: "GET",
|
|
path: "/",
|
|
isWrite: false,
|
|
expectAuthorized: false,
|
|
},
|
|
{
|
|
name: "root listing allowed with an unrestricted token",
|
|
token: genToken(nil, nil),
|
|
method: "GET",
|
|
path: "/",
|
|
isWrite: false,
|
|
expectAuthorized: true,
|
|
},
|
|
{
|
|
name: "exact prefix match",
|
|
token: genToken([]string{"/data"}, nil),
|
|
method: "GET",
|
|
path: "/data",
|
|
isWrite: false,
|
|
expectAuthorized: true,
|
|
},
|
|
{
|
|
name: "tenant prefix does not match sibling tenant",
|
|
token: genToken([]string{"/tenant1"}, nil),
|
|
method: "GET",
|
|
path: "/tenant1234/secret",
|
|
isWrite: false,
|
|
expectAuthorized: false,
|
|
},
|
|
{
|
|
name: "tenant prefix does not match dashed sibling",
|
|
token: genToken([]string{"/tenant1"}, nil),
|
|
method: "GET",
|
|
path: "/tenant1-old/secret",
|
|
isWrite: false,
|
|
expectAuthorized: false,
|
|
},
|
|
{
|
|
name: "tenant prefix matches own subtree",
|
|
token: genToken([]string{"/tenant1"}, nil),
|
|
method: "GET",
|
|
path: "/tenant1/ok.txt",
|
|
isWrite: false,
|
|
expectAuthorized: true,
|
|
},
|
|
{
|
|
name: "tenant prefix with trailing slash matches own subtree",
|
|
token: genToken([]string{"/tenant1/"}, nil),
|
|
method: "GET",
|
|
path: "/tenant1/ok.txt",
|
|
isWrite: false,
|
|
expectAuthorized: true,
|
|
},
|
|
{
|
|
name: "root prefix matches anywhere",
|
|
token: genToken([]string{"/"}, nil),
|
|
method: "GET",
|
|
path: "/tenant1234/secret",
|
|
isWrite: false,
|
|
expectAuthorized: true,
|
|
},
|
|
{
|
|
name: "dot-dot cannot escape allowed subtree",
|
|
token: genToken([]string{"/tenant1"}, nil),
|
|
method: "GET",
|
|
path: "/tenant1/../tenant2/secret",
|
|
isWrite: false,
|
|
expectAuthorized: false,
|
|
},
|
|
{
|
|
name: "copy source within subtree allowed",
|
|
token: genToken([]string{"/tenant1"}, nil),
|
|
method: "POST",
|
|
path: "/tenant1/loot?cp.from=/tenant1/src",
|
|
isWrite: true,
|
|
expectAuthorized: true,
|
|
},
|
|
{
|
|
name: "copy source outside subtree denied",
|
|
token: genToken([]string{"/tenant1"}, nil),
|
|
method: "POST",
|
|
path: "/tenant1/loot?cp.from=/tenant2/secret",
|
|
isWrite: true,
|
|
expectAuthorized: false,
|
|
},
|
|
{
|
|
name: "copy source dot-dot escape denied",
|
|
token: genToken([]string{"/tenant1"}, nil),
|
|
method: "POST",
|
|
path: "/tenant1/loot?cp.from=/tenant1/../tenant2/secret",
|
|
isWrite: true,
|
|
expectAuthorized: false,
|
|
},
|
|
{
|
|
name: "move source outside subtree denied",
|
|
token: genToken([]string{"/tenant1"}, nil),
|
|
method: "POST",
|
|
path: "/tenant1/dst?mv.from=/tenant2/secret",
|
|
isWrite: true,
|
|
expectAuthorized: false,
|
|
},
|
|
{
|
|
name: "write at the prefix itself allowed",
|
|
token: genToken([]string{"/tenant1"}, nil),
|
|
method: "POST",
|
|
path: "/tenant1",
|
|
isWrite: true,
|
|
expectAuthorized: true,
|
|
},
|
|
{
|
|
name: "extract into the prefix directory allowed",
|
|
token: genToken([]string{"/tenant1"}, nil),
|
|
method: "POST",
|
|
path: "/tenant1/?extract=zip",
|
|
isWrite: true,
|
|
expectAuthorized: true,
|
|
},
|
|
{
|
|
name: "extract beside an archive inside the prefix allowed",
|
|
token: genToken([]string{"/tenant1"}, nil),
|
|
method: "PUT",
|
|
path: "/tenant1/pack.zip?extract=zip",
|
|
isWrite: true,
|
|
expectAuthorized: true,
|
|
},
|
|
{
|
|
name: "extract at the prefix itself writes to its parent, denied",
|
|
token: genToken([]string{"/tenant1"}, nil),
|
|
method: "POST",
|
|
path: "/tenant1?extract=zip",
|
|
isWrite: true,
|
|
expectAuthorized: false,
|
|
},
|
|
{
|
|
name: "extract header at the prefix itself denied",
|
|
token: genToken([]string{"/tenant1/"}, []string{"PUT"}),
|
|
method: "PUT",
|
|
path: "/tenant1",
|
|
header: map[string]string{"X-Seaweedfs-Extract": "zip"},
|
|
isWrite: true,
|
|
expectAuthorized: false,
|
|
},
|
|
{
|
|
name: "extract at a nested prefix into the sibling parent denied",
|
|
token: genToken([]string{"/buckets/bucket-a"}, nil),
|
|
method: "POST",
|
|
path: "/buckets/bucket-a?extract=zip",
|
|
isWrite: true,
|
|
expectAuthorized: false,
|
|
},
|
|
{
|
|
name: "extract with a root prefix allowed",
|
|
token: genToken([]string{"/"}, nil),
|
|
method: "POST",
|
|
path: "/tenant1?extract=zip",
|
|
isWrite: true,
|
|
expectAuthorized: true,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
req := httptest.NewRequest(tt.method, tt.path, nil)
|
|
if tt.token != "" {
|
|
req.Header.Set("Authorization", "Bearer "+tt.token)
|
|
}
|
|
for k, v := range tt.header {
|
|
req.Header.Set(k, v)
|
|
}
|
|
if authorized := fs.maybeCheckJwtAuthorization(req, tt.isWrite); authorized != tt.expectAuthorized {
|
|
t.Errorf("expected authorized=%v, got %v", tt.expectAuthorized, authorized)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestPathHasComponentPrefix(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
path string
|
|
prefix string
|
|
want bool
|
|
}{
|
|
{"exact match", "/tenant1", "/tenant1", true},
|
|
{"subtree match", "/tenant1/a/b", "/tenant1", true},
|
|
{"trailing slash prefix", "/tenant1/a", "/tenant1/", true},
|
|
{"sibling numeric suffix", "/tenant1234/secret", "/tenant1", false},
|
|
{"sibling dash suffix", "/tenant1-old/secret", "/tenant1", false},
|
|
{"sibling dot suffix", "/tenant1.bak/x", "/tenant1", false},
|
|
{"unrelated tree", "/other/x", "/tenant1", false},
|
|
{"root prefix matches root", "/", "/", true},
|
|
{"root prefix matches any", "/tenant1234/secret", "/", true},
|
|
{"empty prefix denies", "/tenant1/x", "", false},
|
|
{"dot-dot does not escape", "/tenant1/../tenant2/secret", "/tenant1", false},
|
|
{"dot-dot stays inside", "/tenant1/a/../b", "/tenant1", true},
|
|
{"double slashes normalised", "/tenant1//a", "/tenant1", true},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
if got := pathHasComponentPrefix(tt.path, tt.prefix); got != tt.want {
|
|
t.Errorf("pathHasComponentPrefix(%q, %q) = %v, want %v", tt.path, tt.prefix, got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|