From 5f1230aac542f30640451fe3b34e91a0cd867dd0 Mon Sep 17 00:00:00 2001 From: Gianmaria Del Monte Date: Fri, 22 Oct 2021 09:10:33 +0200 Subject: [PATCH] Filter provider by only showing allowed users --- pkg/storage/registry/static/static.go | 41 +++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 3 deletions(-) diff --git a/pkg/storage/registry/static/static.go b/pkg/storage/registry/static/static.go index 58d3845013e..e0ad098d819 100644 --- a/pkg/storage/registry/static/static.go +++ b/pkg/storage/registry/static/static.go @@ -32,6 +32,7 @@ import ( "github.com/cs3org/reva/pkg/storage" "github.com/cs3org/reva/pkg/storage/registry/registry" "github.com/cs3org/reva/pkg/storage/utils/templates" + ua "github.com/mileusna/useragent" "github.com/mitchellh/mapstructure" "github.com/pkg/errors" ) @@ -43,9 +44,10 @@ func init() { var bracketRegex = regexp.MustCompile(`\[(.*?)\]`) type rule struct { - Mapping string `mapstructure:"mapping"` - Address string `mapstructure:"address"` - Aliases map[string]string `mapstructure:"aliases"` + Mapping string `mapstructure:"mapping"` + Address string `mapstructure:"address"` + Aliases map[string]string `mapstructure:"aliases"` + AllowedUserAgents []string `mapstructure:"allowed_user_agents"` } type config struct { @@ -138,6 +140,23 @@ func (b *reg) GetHome(ctx context.Context) (*registrypb.ProviderInfo, error) { return nil, errors.New("static: home not found") } +func userAgentIsAllowed(ua *ua.UserAgent, userAgents []string) bool { + for _, userAgent := range userAgents { + switch userAgent { + case "web": + if ua.IsChrome() || ua.IsEdge() || ua.IsFirefox() || ua.IsSafari() || + ua.IsInternetExplorer() || ua.IsOpera() || ua.IsOperaMini() { + return true + } + case "desktop": + if ua.Desktop { + return true + } + } + } + return false +} + func (b *reg) FindProviders(ctx context.Context, ref *provider.Reference) ([]*registrypb.ProviderInfo, error) { // find longest match var match *registrypb.ProviderInfo @@ -147,6 +166,22 @@ func (b *reg) FindProviders(ctx context.Context, ref *provider.Reference) ([]*re if ref.ResourceId != nil { if ref.ResourceId.StorageId != "" { for prefix, rule := range b.c.Rules { + + // check if the provider is allowed to be shown according to the + // user agent that made the request + // if the list of AllowedUserAgents is empty, this means that + // every agents that made the request could see the provider + + if len(rule.AllowedUserAgents) != 0 { + ua, ok := ctxpkg.ContextGetUserAgent(ctx) + if !ok { + continue + } + if !userAgentIsAllowed(ua, rule.AllowedUserAgents) { + continue // skip this provider + } + } + addr := getProviderAddr(ctx, rule) r, err := regexp.Compile("^" + prefix + "$") if err != nil {