package security import ( "context" "fmt" "io" "path" "slices" "strings" "unicode" jwt "github.com/golang-jwt/jwt/v5" ) // maxFilerTokenFileSize bounds what a token file read may load into memory. const maxFilerTokenFileSize = 64 << 10 // FilerTokenFile is the token source of a client that holds a pre-issued // filer token instead of a signing key. It reads path on every call, so a // replaced file, including an atomic rename that keeps size and mtime, is // used from the next RPC attempt or stream opening on. Surrounding whitespace // is trimmed. A missing, unreadable, empty or malformed file is an error, and // the call fails without falling back to anonymous or admin authority. // Errors name the path but never carry the file's contents. func FilerTokenFile(path string) FilerTokenSource { return func(context.Context) (EncodedJwt, error) { f, err := openFilerTokenFile(path) if err != nil { return "", fmt.Errorf("filer token file: %w", err) } defer f.Close() data, err := io.ReadAll(io.LimitReader(f, maxFilerTokenFileSize+1)) if err != nil { return "", fmt.Errorf("filer token file: %w", err) } if len(data) > maxFilerTokenFileSize { return "", fmt.Errorf("filer token file %s is larger than %d bytes", path, maxFilerTokenFileSize) } token := strings.TrimSpace(string(data)) if token == "" { return "", fmt.Errorf("filer token file %s is empty", path) } if strings.IndexFunc(token, func(r rune) bool { return unicode.IsSpace(r) || !unicode.IsPrint(r) }) >= 0 { return "", fmt.Errorf("filer token file %s does not hold a single token", path) } return EncodedJwt(token), nil } } // FilerTokenClaims decodes a filer token's claims without verifying it. A // client that holds only the token uses them to plan reconnects before the // token expires; they never decide access, which stays with the filer. func FilerTokenClaims(token EncodedJwt) (*SeaweedFilerClaims, bool) { claims := &SeaweedFilerClaims{} if _, _, err := jwt.NewParser().ParseUnverified(string(token), claims); err != nil { return nil, false } return claims, true } // FilerTokenGrantCovers reports, from the tokens' unverified claims, whether // next lets its holder see everything prev did: every path prev could read // and every method prev could call. A client that cached what prev showed it // can keep that cache when next covers prev. A token that cannot be decoded, // or names a prefix the filer would refuse, covers nothing but itself. func FilerTokenGrantCovers(prev, next EncodedJwt) bool { if prev == next { return true } p, ok := FilerTokenClaims(prev) if !ok { return false } n, ok := FilerTokenClaims(next) if !ok { return false } if len(n.AllowedMethods) > 0 { if len(p.AllowedMethods) == 0 { return false } for _, m := range p.AllowedMethods { if !slices.Contains(n.AllowedMethods, m) { return false } } } nextPrefixes, ok := filerTokenPrefixes(n) if !ok { return false } prevPrefixes, ok := filerTokenPrefixes(p) if !ok { return false } if len(nextPrefixes) == 0 { return true } if len(prevPrefixes) == 0 { return false } for _, prefix := range prevPrefixes { if !MetadataPathInScope(prefix, nextPrefixes) { return false } } return true } // filerTokenPrefixes lists the paths a token may read, empty when it is not // scoped. ok is false when a prefix is not a clean absolute path, which the // filer refuses. func filerTokenPrefixes(c *SeaweedFilerClaims) (prefixes []string, ok bool) { prefixes = append(append(prefixes, c.AllowedPrefixes...), c.ReadPrefixes...) for _, p := range prefixes { if !strings.HasPrefix(p, "/") || path.Clean(p) != p { return nil, false } } return prefixes, true }