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

Prevent the pattern matching for KV V2 operations if a trailing slash… #64

Merged
merged 4 commits into from Sep 28, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion path_data.go
Expand Up @@ -18,11 +18,15 @@ import (
"github.com/mitchellh/mapstructure"
)

func matchAllNoTrailingSlashRegex(name string) string {
return fmt.Sprintf(`(?P<%s>.*?[^/]$)`, name)
}

// pathConfig returns the path configuration for CRUD operations on the backend
// configuration.
func pathData(b *versionedKVBackend) *framework.Path {
return &framework.Path{
Pattern: "data/" + framework.MatchAllRegex("path"),
Pattern: "data/" + matchAllNoTrailingSlashRegex("path"),
Fields: map[string]*framework.FieldSchema{
"path": {
Type: framework.TypeString,
Expand Down
30 changes: 29 additions & 1 deletion path_data_test.go
Expand Up @@ -4,12 +4,14 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/go-test/deep"
"reflect"
"regexp"
"strings"
"testing"
"time"

"github.com/go-test/deep"

log "github.com/hashicorp/go-hclog"
"github.com/hashicorp/vault/sdk/helper/logging"
"github.com/hashicorp/vault/sdk/logical"
Expand Down Expand Up @@ -1160,3 +1162,29 @@ func TestVersionedKV_Patch_CurrentVersionDestroyed(t *testing.T) {
t.Fatalf("Expected 404 status code for destroyed version: resp:%#v\n", resp)
}
}

func TestRegex_AllNoTrailingSlash(t *testing.T) {
tests := map[string]struct {
input string
want bool
}{
"single-part-no-trailing-slash": {input: "data/foo", want: true},
"single-part-trailing-slash": {input: "data/foo/", want: false},
"multi-part-no-trailing-slash": {input: "data/foo/bar", want: true},
"multi-part-trailing-slash": {input: "data/foo/bar/", want: false},
"no-data-single-part-no-trailing-slash": {input: "foo", want: false},
"no-data-single-part-trailing-slash": {input: "foo/", want: false},
"no-data-multi-part-no-trailing-slash": {input: "foo/bar", want: false},
"no-data-multi-part-trailing-slash": {input: "foo/bar/", want: false},
}

p := "data/" + matchAllNoTrailingSlashRegex("path")
r, _ := regexp.Compile(p)

for name, tc := range tests {
got := r.MatchString(tc.input)
if tc.want != got {
t.Errorf("%s: expected: %v, got: %v", name, tc.want, got)
}
}
}