Skip to content

Commit 6f103f3

Browse files
committed
api,adaptation: more strict plugin name check.
Verify that a registered plugin name is not empty and only contains characters from the set [a-zA-Z0-9_.+-]. Signed-off-by: Krisztian Litkey <[email protected]>
1 parent 95ba09f commit 6f103f3

File tree

3 files changed

+37
-3
lines changed

3 files changed

+37
-3
lines changed

pkg/adaptation/adaptation_suite_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,19 @@ var _ = Describe("Plugin connection", func() {
201201
s.Cleanup()
202202
})
203203

204+
It("should reject plugins with an invalid name", func() {
205+
var (
206+
invalidPlugin = &mockPlugin{
207+
name: "foo,bar",
208+
idx: "10",
209+
}
210+
)
211+
212+
s.Startup()
213+
214+
Expect(invalidPlugin.Start(s.dir)).ToNot(Succeed())
215+
})
216+
204217
It("should configure the plugin", func() {
205218
var (
206219
plugin = s.plugins[0]

pkg/adaptation/plugin.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -435,9 +435,9 @@ func (p *plugin) qualifiedName() string {
435435
// RegisterPlugin handles the plugin's registration request.
436436
func (p *plugin) RegisterPlugin(ctx context.Context, req *RegisterPluginRequest) (*RegisterPluginResponse, error) {
437437
if p.isExternal() {
438-
if req.PluginName == "" {
439-
p.regC <- fmt.Errorf("plugin %q registered with an empty name", p.qualifiedName())
440-
return &RegisterPluginResponse{}, errors.New("invalid (empty) plugin name")
438+
if err := api.CheckPluginName(req.PluginName); err != nil {
439+
p.regC <- fmt.Errorf("plugin registered with an invalid name: %w", err)
440+
return &RegisterPluginResponse{}, fmt.Errorf("invalid plugin name: %w", err)
441441
}
442442
if err := api.CheckPluginIndex(req.PluginIdx); err != nil {
443443
p.regC <- fmt.Errorf("plugin %q registered with an invalid index: %w", req.PluginName, err)

pkg/api/plugin.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package api
1818

1919
import (
20+
"errors"
2021
"fmt"
2122
"strings"
2223
)
@@ -57,3 +58,23 @@ func CheckPluginIndex(idx string) error {
5758
}
5859
return nil
5960
}
61+
62+
// CheckPluginName verifies that a plugin name is not empty and only contains
63+
// characters from the set [a-zA-Z0-9_.+-].
64+
func CheckPluginName(name string) error {
65+
if name == "" {
66+
return errors.New("invalid plugin name: name is empty")
67+
}
68+
69+
for _, r := range name {
70+
switch {
71+
case 'a' <= r && r <= 'z', 'A' <= r && r <= 'Z':
72+
case '0' <= r && r <= '9':
73+
case r == '-', r == '_', r == '.', r == '+':
74+
default:
75+
return fmt.Errorf("invalid plugin name %q: contains invalid character %q", name, r)
76+
}
77+
}
78+
79+
return nil
80+
}

0 commit comments

Comments
 (0)