package host import ( "bytes" "encoding/json" "io" "os" "github.com/carlosframework/platform/internal/rootstate" ) func readClaimUnitShapes(path string) (map[string]claimUnitShape, error) { f, err := rootstate.OpenAuthorityFile(path) if err != nil { return nil, err } defer f.Close() raw, err := io.ReadAll(io.LimitReader(f, rootstate.MaxRecord+1)) if err != nil { return nil, err } return decodeClaimUnitShapes(raw) } func decodeClaimUnitShapes(raw []byte) (map[string]claimUnitShape, error) { var config struct { Schema int Units []claimUnitShape } dec := json.NewDecoder(bytes.NewReader(raw)) dec.DisallowUnknownFields() if len(raw) > rootstate.MaxRecord || dec.Decode(&config) != nil || dec.Decode(new(any)) != io.EOF || config.Schema != 1 || len(config.Units) > 4096 { return nil, os.ErrPermission } hosts, units := map[string]claimUnitShape{}, map[string]bool{} for _, s := range config.Units { if !s.valid() || hosts[s.Host].Host != "" || units[s.Unit] { return nil, os.ErrPermission } hosts[s.Host], units[s.Unit] = s, true } return hosts, nil }