Skip to content

Commit

Permalink
[bugfix] Remove leading and trailing slashes from mounts (#2698)
Browse files Browse the repository at this point in the history
* [bugfix] Remove leading and trailing slashes from mounts

Fixes #2669

Signed-off-by: Dominik Schulz <[email protected]>

* Add tests for leading slashes

Signed-off-by: Dominik Schulz <[email protected]>

* Reorder mount alias checks

Signed-off-by: Dominik Schulz <[email protected]>

---------

Signed-off-by: Dominik Schulz <[email protected]>
  • Loading branch information
dominikschulz authored Nov 18, 2023
1 parent 55ea5cf commit 73b880d
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 3 deletions.
1 change: 1 addition & 0 deletions internal/store/root/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func (r *Store) IsInitialized(ctx context.Context) (bool, error) {

// Init tries to initialize a new password store location matching the object.
func (r *Store) Init(ctx context.Context, alias, path string, ids ...string) error {
alias = CleanMountAlias(alias)
debug.Log("Instantiating new sub store %s at %s for %+v", alias, path, ids)

if !backend.HasCryptoBackend(ctx) {
Expand Down
30 changes: 27 additions & 3 deletions internal/store/root/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ func (r *Store) AddMount(ctx context.Context, alias, path string, keys ...string
}

func (r *Store) addMount(ctx context.Context, alias, path string, keys ...string) error {
if alias == "" {
return fmt.Errorf("alias must not be empty")
}
// disallow filepath separators in alias and always disallow regular slashes
// even on Windows, since these are used internally to separate folders.
if strings.HasSuffix(alias, "/") {
Expand All @@ -37,6 +34,11 @@ func (r *Store) addMount(ctx context.Context, alias, path string, keys ...string
return fmt.Errorf("alias must not end with '%s'", string(filepath.Separator))
}

alias = CleanMountAlias(alias)
if alias == "" {
return fmt.Errorf("alias must not be empty")
}

if r.mounts == nil {
r.mounts = make(map[string]*leaf.Store, 1)
}
Expand Down Expand Up @@ -65,6 +67,7 @@ func (r *Store) addMount(ctx context.Context, alias, path string, keys ...string
}

func (r *Store) initSub(ctx context.Context, alias, path string, keys []string) (*leaf.Store, error) {
alias = CleanMountAlias(alias)
// init regular sub store
s, err := leaf.New(ctx, alias, path)
if err != nil {
Expand Down Expand Up @@ -206,3 +209,24 @@ func (r *Store) checkMounts() error {

return nil
}

// CleanMountAlias removes all leading and trailing slashes from a mount alias.
// Note: Slashes inside the alias are valid and will be kept.
func CleanMountAlias(alias string) string {
for {
if !strings.HasPrefix(alias, "/") && !strings.HasPrefix(alias, "\\") {
break
}
alias = strings.TrimPrefix(strings.TrimSuffix(alias, "/"), "/")
alias = strings.TrimPrefix(strings.TrimSuffix(alias, "\\"), "\\")
}
for {
if !strings.HasSuffix(alias, "/") && !strings.HasSuffix(alias, "\\") {
break
}
alias = strings.TrimSuffix(strings.TrimPrefix(alias, "/"), "/")
alias = strings.TrimSuffix(strings.TrimPrefix(alias, "\\"), "\\")
}

return alias
}
117 changes: 117 additions & 0 deletions internal/store/root/mount_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,120 @@ func TestMountPointIllegal(t *testing.T) {
}
require.Error(t, rs.AddMount(ctx, "sub2/", u.StoreDir("sub2")))
}

func TestCleanMountAlias(t *testing.T) {
for _, tc := range []struct {
name string
in, want string
}{
{
name: "simple",
in: "foo",
want: "foo",
},
{
name: "simple",
in: "foo/bar",
want: "foo/bar",
},
{
name: "simple",
in: "foo/bar/",
want: "foo/bar",
},
{
name: "simple",
in: "foo/bar//",
want: "foo/bar",
},
{
name: "simple",
in: "foo/bar////",
want: "foo/bar",
},
{
name: "simple",
in: "foo/bar/////",
want: "foo/bar",
},
{
name: "simple",
in: "foo/bar//////",
want: "foo/bar",
},
{
name: "simple",
in: "foo/bar///////",
want: "foo/bar",
},
{
name: "simple",
in: "foo/bar////////",
want: "foo/bar",
},
{
name: "simple",
in: "foo/bar/////////",
want: "foo/bar",
},
{
name: "simple",
in: "foo/bar//////////",
want: "foo/bar",
},
{
name: "simple",
in: "foo/bar///////////",
want: "foo/bar",
},
{
name: "simple",
in: "foo/bar////////////",
want: "foo/bar",
},
{
name: "simple",
in: "foo/bar/////////////",
want: "foo/bar",
},
{
name: "simple",
in: "foo/bar//////////////",
want: "foo/bar",
},
{
name: "simple",
in: "foo/bar///////////////",
want: "foo/bar",
},
{
name: "simple",
in: "foo/bar////////////////",
want: "foo/bar",
},
{
name: "mixed",
in: "foo/bar/\\///\\////",
want: "foo/bar",
},
{
name: "simple",
in: "/foo/bar",
want: "foo/bar",
},
{
name: "simple",
in: "//foo/bar",
want: "foo/bar",
},
{
name: "simple",
in: "////foo/bar",
want: "foo/bar",
},
} {
t.Run(tc.name, func(t *testing.T) {
assert.Equal(t, tc.want, CleanMountAlias(tc.in))
})
}
}

0 comments on commit 73b880d

Please sign in to comment.