Skip to content

Commit

Permalink
urlconfig: anchor regexp for key matching
Browse files Browse the repository at this point in the history
Currently, we don't anchor the regexp for matching keys.  That means
that if we have a value of http.URL.sslCert and
http.URL.sslCertPasswordProtected, the latter can be read when when
looking up the former.  To avoid that, let's anchor the regexp properly
against the key at the beginning and end.  In addition, let's escape the
dots to prevent matching the wrong string.
  • Loading branch information
bk2204 committed Aug 30, 2021
1 parent 763252b commit ea0d264
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
2 changes: 1 addition & 1 deletion config/url_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (c *URLConfig) getAll(prefix, rawurl, key string) []string {

config := c.git.All()

re := regexp.MustCompile(fmt.Sprintf(`%s.(\S+).%s`, prefix, key))
re := regexp.MustCompile(fmt.Sprintf(`\A%s\.(\S+)\.%s\z`, prefix, key))

bestMatch := urlMatch{
key: "",
Expand Down
6 changes: 6 additions & 0 deletions config/url_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func TestURLConfig(t *testing.T) {
"http.https://host.com:443/repo3.git.key": []string{"port"},
"http.ssh://host.com:22/repo3.git.key": []string{"ssh-port"},
"http.https://host.*/a.key": []string{"wild"},
"httpXhttps://host.*/aXkey": []string{"invalid"},
})))

getOne := map[string]string{
Expand Down Expand Up @@ -52,6 +53,11 @@ func TestURLConfig(t *testing.T) {
assert.Equal(t, expected, value, "get one: "+rawurl)
}

value, _ := u.Get("http", "https://host.wild/a/b/c", "k")
assert.Equal(t, value, "")
value, _ = u.Get("ttp", "https://host.wild/a/b/c", "key")
assert.Equal(t, value, "")

getAll := map[string][]string{
"https://root.com/a/b/c": []string{"root", "root-2"},
"https://host.com/": []string{"host", "host-2"},
Expand Down

0 comments on commit ea0d264

Please sign in to comment.