Skip to content

Commit 75d9f69

Browse files
authored
add support for encoded allowlist urls (#116)
* add support for encoded urls * rm test
1 parent b980f47 commit 75d9f69

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

pkg/allowlist.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,16 @@ func (config AllowlistItem) Matches(method string, url *url.URL) bool {
1212
return false
1313
}
1414

15-
parsedUrl, _ := url.Parse(config.URL)
15+
parsedUrl, err := url.Parse(config.URL)
16+
if err != nil {
17+
return false
18+
}
1619

1720
if parsedUrl.Scheme != url.Scheme || parsedUrl.Host != url.Host {
1821
return false
1922
}
2023

21-
matcher := urlpath.New(parsedUrl.Path)
24+
matcher := urlpath.New(parsedUrl.EscapedPath())
2225
if _, matches := matcher.Match(url.EscapedPath()); matches {
2326
return true
2427
}

pkg/allowlist_test.go

+21
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,24 @@ func TestAllowlistPathMatch(t *testing.T) {
118118
assertAllowlistMatch(t, allowlist, "GET", "https://foo.com/variable-path/bla%2Fbla/suffix", true)
119119
assertAllowlistMatch(t, allowlist, "GET", "https://foo.com/variable-path/bla/bla/suffix", false)
120120
}
121+
122+
func TestAllowlistEncodedPathMatch(t *testing.T) {
123+
allowlist := &Allowlist{
124+
AllowlistItem{
125+
URL: "https://gitlab.example.com/api/v4/projects/group%2Fproject/repository/files/*",
126+
Methods: ParseHttpMethods([]string{"GET"}),
127+
},
128+
AllowlistItem{
129+
URL: "https://gitlab.example.com/api/v4/projects/:group%2F:project/repository/files/*",
130+
Methods: ParseHttpMethods([]string{"GET"}),
131+
},
132+
}
133+
134+
// Test that encoded forward slashes in the path match correctly
135+
assertAllowlistMatch(t, allowlist, "GET", "https://gitlab.example.com/api/v4/projects/group%2Fproject/repository/files/path/to/file", true)
136+
assertAllowlistMatch(t, allowlist, "GET", "https://gitlab.example.com/api/v4/projects/group/project/repository/files/path/to/file", false)
137+
138+
// Test with variables containing encoded characters
139+
assertAllowlistMatch(t, allowlist, "GET", "https://gitlab.example.com/api/v4/projects/test-group%2Ftest-project/repository/files/path/to/file", true)
140+
assertAllowlistMatch(t, allowlist, "GET", "https://gitlab.example.com/api/v4/projects/test-group/test-project/repository/files/path/to/file", false)
141+
}

0 commit comments

Comments
 (0)