From fb68d2b340bac10167b75da965604bf3f6e10d23 Mon Sep 17 00:00:00 2001 From: Moshe Immerman Date: Fri, 30 Oct 2026 10:47:32 +0200 Subject: [PATCH] fix(rpc): keep every value of a repeated query parameter and reject a repeated scalar Claude-Session-Id: 5962da15-cdee-4203-b595-c3fe577180fd --- rpc/cobra_command.go | 31 ++++++++- rpc/execution_http.go | 2 +- rpc/executor.go | 25 ++++++- rpc/paged.go | 6 +- rpc/repeated_query_param_ginkgo_test.go | 86 +++++++++++++++++++++++++ rpc/stream.go | 19 ++++-- 6 files changed, 157 insertions(+), 12 deletions(-) create mode 100644 rpc/repeated_query_param_ginkgo_test.go diff --git a/rpc/cobra_command.go b/rpc/cobra_command.go index 007d926d..9cabfd1f 100644 --- a/rpc/cobra_command.go +++ b/rpc/cobra_command.go @@ -3,9 +3,11 @@ package rpc import ( "bytes" "context" + "encoding/csv" "fmt" "io" "os" + "strings" "sync" "github.com/flanksource/clicky/entity" @@ -61,7 +63,7 @@ func (c *CobraExecutableCommand) Execute(ctx context.Context, opts entity.Execut for name, val := range opts.Flags { if flag := lookupAnyFlag(cmd, name); flag != nil { - if err := flag.Value.Set(val); err != nil { + if err := setFlagValue(flag, val); err != nil { return "", "", fmt.Errorf("invalid value for flag %s: %w", name, err) } flag.Changed = true @@ -186,11 +188,36 @@ func resetFlagSet(flags *pflag.FlagSet) { return } flags.VisitAll(func(flag *pflag.Flag) { - _ = flag.Value.Set(flag.DefValue) + if isSliceFlag(flag) { + _ = setFlagValue(flag, strings.TrimSuffix(strings.TrimPrefix(flag.DefValue, "["), "]")) + } else { + _ = flag.Value.Set(flag.DefValue) + } flag.Changed = false }) } +// setFlagValue applies one request value to a flag. A slice flag's value is a +// CSV record — what ExtractRequestFromHTTP builds from a repeated query key and +// csvRecord builds from a JSON array — and replaces the flag's values outright. +// pflag's Set would append to whatever an earlier call (or the reset to the +// "[]" default) left behind. +func setFlagValue(flag *pflag.Flag, value string) error { + slice, ok := flag.Value.(pflag.SliceValue) + if !ok { + return flag.Value.Set(value) + } + items := []string{} + if value != "" { + record, err := csv.NewReader(strings.NewReader(value)).Read() + if err != nil { + return fmt.Errorf("parsing %q as a CSV record: %w", value, err) + } + items = record + } + return slice.Replace(items) +} + func lookupAnyFlag(cmd *cobra.Command, name string) *pflag.Flag { if flag := cmd.Flags().Lookup(name); flag != nil { return flag diff --git a/rpc/execution_http.go b/rpc/execution_http.go index d4be2817..72ee14f1 100644 --- a/rpc/execution_http.go +++ b/rpc/execution_http.go @@ -189,7 +189,7 @@ func (s *SwaggerServer) handleLookupCommand(w http.ResponseWriter, r *http.Reque continue } if _, declared := request.Flags[key]; !declared { - request.Flags[key] = values[0] + request.Flags[key] = strings.Join(values, ",") } } diff --git a/rpc/executor.go b/rpc/executor.go index 10c270ab..83b872b4 100644 --- a/rpc/executor.go +++ b/rpc/executor.go @@ -317,7 +317,10 @@ func (e *CommandExecutor) ExtractRequestFromHTTP(r *http.Request, op *RPCOperati continue } req.Args = []string{} - if value := query.Get(name); value != "" { + for _, value := range query[name] { + if value == "" { + continue + } for _, arg := range strings.Split(value, ",") { req.Args = append(req.Args, strings.TrimSpace(arg)) } @@ -327,7 +330,11 @@ func (e *CommandExecutor) ExtractRequestFromHTTP(r *http.Request, op *RPCOperati // Then handle all other query parameters defined in the operation for _, param := range op.Parameters { if param.In == "query" && !IsPositionalArgument(op, param.Name) && query.Has(param.Name) { - req.Flags[param.Name] = query.Get(param.Name) // Override body value, even if empty + value, err := queryFlagValue(param, query[param.Name]) + if err != nil { + return nil, err + } + req.Flags[param.Name] = value // Override body value, even if empty } } @@ -336,6 +343,20 @@ func (e *CommandExecutor) ExtractRequestFromHTTP(r *http.Request, op *RPCOperati return req, missingRequiredParameter(req, op) } +// queryFlagValue reads a query parameter the way the CLI reads a repeated flag. +// An array parameter keeps every value, joined into one CSV record that the +// slice flag (pflag's stringSlice, MultiFilter, the request binder) splits back +// apart, so ?kind=a&kind=b is --kind a --kind b and a single ?kind=a,b splits as +// it always has. A scalar given twice has no CLI meaning and is refused rather +// than silently resolved to one of its values. +func queryFlagValue(param RPCParameter, values []string) (string, error) { + if len(values) > 1 && param.Type != "array" { + return "", entity.NewStatusErrorf(http.StatusBadRequest, "invalid_parameters", + "query parameter %s takes a single value but was given %d: %q", param.Name, len(values), values) + } + return strings.Join(values, ","), nil +} + // IsPositionalArgument reports whether a request field carries the operation's // positional args: the generic "args", or a non-path parameter marked Positional. // Path parameters reach Args through the URL template instead. diff --git a/rpc/paged.go b/rpc/paged.go index a017e179..3fc7fbd3 100644 --- a/rpc/paged.go +++ b/rpc/paged.go @@ -192,7 +192,9 @@ func llmToolPageRequest(flags map[string]string) (entity.PageRequest, error) { // does: an entity resolved at runtime carries filters the operation was // generated without, and dropping them would silently ignore what the caller // asked to filter on. The transport's own parameters travel with them — an -// operation that declares a parameter named "limit" means it. +// operation that declares a parameter named "limit" means it. A repeated +// undeclared key is a list filter, so every value travels, joined as the +// declared array parameters are (queryFlagValue). func (s *SwaggerServer) requestFlags(r *http.Request, op *RPCOperation) (map[string]string, error) { req, err := s.executor.ExtractRequestFromHTTP(r, op) if err != nil { @@ -203,7 +205,7 @@ func (s *SwaggerServer) requestFlags(r *http.Request, op *RPCOperation) (map[str continue } if _, declared := req.Flags[key]; !declared { - req.Flags[key] = values[0] + req.Flags[key] = strings.Join(values, ",") } } return req.Flags, nil diff --git a/rpc/repeated_query_param_ginkgo_test.go b/rpc/repeated_query_param_ginkgo_test.go new file mode 100644 index 00000000..89f5b9ed --- /dev/null +++ b/rpc/repeated_query_param_ginkgo_test.go @@ -0,0 +1,86 @@ +package rpc + +import ( + "errors" + "net/http" + "net/http/httptest" + "reflect" + + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + "github.com/spf13/cobra" + + "github.com/flanksource/clicky/entity" + "github.com/flanksource/clicky/flags" +) + +// A query key repeated on a GET must reach the operation the way a repeated +// flag reaches it on the command line: ?kind=a&kind=b is --kind a --kind b. +var _ = Describe("repeated query parameters", func() { + var ( + executor *CommandExecutor + operation *RPCOperation + kinds []string + ) + + BeforeEach(func() { + kinds = nil + cmd := &cobra.Command{ + Use: "targets", + RunE: func(*cobra.Command, []string) error { return nil }, + } + cmd.Flags().StringSliceVar(&kinds, "kind", nil, "kinds to include") + cmd.Flags().String("name", "", "target name") + var err error + operation, err = NewConverter(nil).ConvertCommand(cmd) + Expect(err).NotTo(HaveOccurred()) + executor = NewCommandExecutor(&RPCService{Operations: []RPCOperation{*operation}}, + &ExecutorConfig{Enabled: true, SkipPreRun: true, PathPrefix: "/api/v1"}) + }) + + DescribeTable("a slice flag receives every value, as the CLI would", + func(target string, want []string) { + request, err := executor.ExtractRequestFromHTTP(httptest.NewRequest(http.MethodGet, target, nil), operation) + Expect(err).NotTo(HaveOccurred()) + + _, _, err = executor.ExecuteCommand(operation, request) + + Expect(err).NotTo(HaveOccurred()) + Expect(kinds).To(Equal(want)) + }, + Entry("repeated key", "/api/v1/targets?kind=table&kind=!column", []string{"table", "!column"}), + Entry("comma-separated single value", "/api/v1/targets?kind=table,!column", []string{"table", "!column"}), + Entry("repeated key mixing comma values", "/api/v1/targets?kind=table,view&kind=!column", []string{"table", "view", "!column"}), + ) + + It("binds every repeated value onto a MultiFilter field", func() { + type targetFlags struct { + Kind entity.MultiFilter `flag:"kind"` + } + request, err := executor.ExtractRequestFromHTTP(httptest.NewRequest(http.MethodGet, "/api/v1/targets?kind=table&kind=!column", nil), operation) + Expect(err).NotTo(HaveOccurred()) + fields, err := flags.ParseStructFields(reflect.TypeOf(targetFlags{})) + Expect(err).NotTo(HaveOccurred()) + + var decoded targetFlags + Expect(flags.PopulateFromRequest(reflect.ValueOf(&decoded).Elem(), fields, request.Flags, nil)).To(Succeed()) + + Expect(decoded.Kind).To(Equal(entity.MultiFilter{"table", "!column"})) + }) + + It("refuses a scalar flag given more than once with a 400 naming it", func() { + _, err := executor.ExtractRequestFromHTTP(httptest.NewRequest(http.MethodGet, "/api/v1/targets?name=a&name=b", nil), operation) + + Expect(err).To(MatchError(ContainSubstring("name"))) + var status *entity.StatusError + Expect(errors.As(err, &status)).To(BeTrue()) + Expect(status.StatusCode()).To(Equal(http.StatusBadRequest)) + }) + + It("passes every repeated positional value as args", func() { + request, err := executor.ExtractRequestFromHTTP(httptest.NewRequest(http.MethodGet, "/api/v1/targets?args=a&args=b,c", nil), operation) + + Expect(err).NotTo(HaveOccurred()) + Expect(request.Args).To(Equal([]string{"a", "b", "c"})) + }) +}) diff --git a/rpc/stream.go b/rpc/stream.go index d2566ac0..8d42428d 100644 --- a/rpc/stream.go +++ b/rpc/stream.go @@ -19,10 +19,14 @@ import ( // `error` event instead — a client that has already begun reading learns why // the stream stopped rather than seeing it end silently. func serveStream(w http.ResponseWriter, r *http.Request, op *RPCOperation) { - flags, args := streamInputs(r, op) + flags, args, err := streamInputs(r, op) + if err != nil { + writeStreamStatus(w, err) + return + } stream := sse.NewWriter(w) - err := op.StreamFunc(r.Context(), flags, args, stream.Send) + err = op.StreamFunc(r.Context(), flags, args, stream.Send) switch { case err == nil, errors.Is(err, r.Context().Err()) && r.Context().Err() != nil: // Done, or the client hung up. Neither is a failure. @@ -41,10 +45,15 @@ func serveStream(w http.ResponseWriter, r *http.Request, op *RPCOperation) { // streamInputs reads the operation's declared parameters out of the request. // A stream is a GET in practice, so query parameters and path values are the // whole input; there is no body to decode. -func streamInputs(r *http.Request, op *RPCOperation) (map[string]string, []string) { +func streamInputs(r *http.Request, op *RPCOperation) (map[string]string, []string, error) { flags := map[string]string{} + query := r.URL.Query() for _, param := range op.Parameters { - if value := r.URL.Query().Get(param.Name); value != "" { + value, err := queryFlagValue(param, query[param.Name]) + if err != nil { + return nil, nil, err + } + if value != "" { flags[param.Name] = value } } @@ -58,7 +67,7 @@ func streamInputs(r *http.Request, op *RPCOperation) (map[string]string, []strin args = append(args, value) } } - return flags, args + return flags, args, nil } // writeStreamStatus answers a stream that failed before it began, using the