diff --git a/internal/app/cmd/register.go b/internal/app/cmd/register.go index dd369381..5e2f5d8a 100644 --- a/internal/app/cmd/register.go +++ b/internal/app/cmd/register.go @@ -172,7 +172,7 @@ func (r *registerInputs) assignToNext(stage registerStage, value string, cfg *co case StageInputLabels: r.Labels = defaultLabels if value != "" { - r.Labels = strings.Split(value, ",") + r.Labels = commaSplit(value) } if validateLabels(r.Labels) != nil { @@ -260,7 +260,7 @@ func registerNoInteractive(ctx context.Context, configFile string, regArgs *regi regArgs.Labels = strings.TrimSpace(regArgs.Labels) // command line flag. if regArgs.Labels != "" { - inputs.Labels = strings.Split(regArgs.Labels, ",") + inputs.Labels = commaSplit(regArgs.Labels) } // specify labels in config file. if len(cfg.Runner.Labels) > 0 { @@ -353,3 +353,24 @@ func doRegister(ctx context.Context, cfg *config.Config, inputs *registerInputs) } return nil } + +// Splits a string by commas, but trims whitespace around the commas. Used for label parsing to avoid a rogue whitespace +// like "docker:docker://node:22-bookworm, self-hosted:host, lxc:lxc://debian:bookworm" becoming labeled with +// " self-hosted" and " lxc". +func commaSplit(s string) []string { + if s == "" { + return make([]string, 0) + } + + parts := strings.Split(s, ",") + result := make([]string, 0, len(parts)) + + for _, part := range parts { + trimmed := strings.TrimSpace(part) + if trimmed != "" { + result = append(result, trimmed) + } + } + + return result +} diff --git a/internal/app/cmd/register_test.go b/internal/app/cmd/register_test.go new file mode 100644 index 00000000..a69775a8 --- /dev/null +++ b/internal/app/cmd/register_test.go @@ -0,0 +1,31 @@ +// Copyright 2025 The Forgejo Authors +// SPDX-License-Identifier: MIT + +package cmd + +import ( + "slices" + "testing" +) + +func TestCommaSplit(t *testing.T) { + tests := []struct { + input string + expected []string + }{ + {"", []string{}}, + {"abc", []string{"abc"}}, + {"abc,def", []string{"abc", "def"}}, + {"abc, def", []string{"abc", "def"}}, + {" abc , def ", []string{"abc", "def"}}, + {"abc, ,def", []string{"abc", "def"}}, + {" , , ", []string{}}, + } + + for _, test := range tests { + result := commaSplit(test.input) + if !slices.Equal(result, test.expected) { + t.Errorf("commaSplit(%q) = %v, want %v", test.input, result, test.expected) + } + } +} diff --git a/internal/pkg/labels/labels.go b/internal/pkg/labels/labels.go index 7befaf42..e256964a 100644 --- a/internal/pkg/labels/labels.go +++ b/internal/pkg/labels/labels.go @@ -31,6 +31,10 @@ func Parse(str string) (*Label, error) { Schema: "docker", } + if strings.TrimSpace(label.Name) != label.Name { + return nil, fmt.Errorf("invalid label %q: starting or ending with a space is invalid", label.Name) + } + if len(splits) >= 2 { label.Schema = splits[1] if label.Schema != SchemeHost && label.Schema != SchemeDocker && label.Schema != SchemeLXC { diff --git a/internal/pkg/labels/labels_test.go b/internal/pkg/labels/labels_test.go index db2f0adf..bf798fa2 100644 --- a/internal/pkg/labels/labels_test.go +++ b/internal/pkg/labels/labels_test.go @@ -63,7 +63,6 @@ func TestParse(t *testing.T) { }, wantErr: false, }, - { args: "label1:host", want: &Label{ @@ -78,12 +77,21 @@ func TestParse(t *testing.T) { want: nil, wantErr: true, }, - { args: "label1:invalidscheme", want: nil, wantErr: true, }, + { + args: " label1:lxc://debian:buster", + want: nil, + wantErr: true, + }, + { + args: "label1 :lxc://debian:buster", + want: nil, + wantErr: true, + }, } for _, tt := range tests { t.Run(tt.args, func(t *testing.T) {