Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filter root paths according to user agent #2193

Merged
merged 4 commits into from
Oct 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions changelog/unreleased/storage-ua-filtering.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Enhancement: Filter root paths according to user agent

Adds a new rule setting in the storage registry ("allowed_user_agents"),
that allows a user to specify which storage provider shows according
to the user agent that made the request.

https://github.com/cs3org/reva/pull/2193
44 changes: 44 additions & 0 deletions pkg/ctx/agentctx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright 2018-2021 CERN
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// In applying this license, CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.

package ctx

import (
"context"

ua "github.com/mileusna/useragent"
"google.golang.org/grpc/metadata"
)

// ContextGetUserAgent returns the user agent if set in the given context.
// see https://github.com/grpc/grpc-go/issues/1100
func ContextGetUserAgent(ctx context.Context) (*ua.UserAgent, bool) {
md, ok := metadata.FromIncomingContext(ctx)
if !ok {
return nil, false
}
userAgentLst, ok := md["user-agent"]
if !ok {
return nil, false
}
if len(userAgentLst) == 0 {
return nil, false
}
userAgent := ua.Parse(userAgentLst[0])
return &userAgent, true
}
45 changes: 42 additions & 3 deletions pkg/storage/registry/static/static.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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 {
Expand Down Expand Up @@ -138,6 +140,27 @@ 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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ua.Desktop will be true if the request comes from a desktop browser. We need to review the rules

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this can wait. The important bit is that we can differentiate desktop traffic from web traffic, the rest we don't care at this point.

From desktop traffic,
"Mozilla/5.0 (Linux) mirall/2.7.1 (build 2596) (cernboxcmd, centos-3.10.0-1160.36.2.el7.x86_64 ClientArchitecture: x86_64 OsArchitecture: x86_64)" this should be flagged as Desktop: true

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But Desktop will also be true for requests coming from desktop browsers

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see what you mean by that, you're right.
I suggest we add "mirall" hard check in our rules like we do for basic auth support.
The rule should check if the user agent contains "mirall" keyword, this will match all sync client I think.

return true
}
case "grpc":
if strings.HasPrefix(ua.Name, "grpc") {
return true
}
}
}
return false
}

func (b *reg) FindProviders(ctx context.Context, ref *provider.Reference) ([]*registrypb.ProviderInfo, error) {
// find longest match
var match *registrypb.ProviderInfo
Expand Down Expand Up @@ -173,6 +196,22 @@ func (b *reg) FindProviders(ctx context.Context, ref *provider.Reference) ([]*re
fn := path.Clean(ref.GetPath())
if fn != "" {
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 {
Expand Down