Skip to content

Commit dfc2444

Browse files
authored
Merge pull request #455 from hookdeck/test/input-shape-guards
test: guard input shapes by class, and fix --delivery-group the guard found
2 parents 49d2782 + 7acd99a commit dfc2444

7 files changed

Lines changed: 300 additions & 11 deletions

File tree

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
package cmd
2+
3+
import (
4+
"regexp"
5+
"testing"
6+
7+
"github.com/spf13/cobra"
8+
"github.com/spf13/pflag"
9+
"github.com/stretchr/testify/assert"
10+
11+
"github.com/hookdeck/hookdeck-cli/pkg/hookdeck"
12+
)
13+
14+
// A flag whose help says "comma-separated" is a promise that a list reaches the
15+
// API. Twice that promise was broken the same way: the value went out as one
16+
// query parameter, which the API matched against nothing, so the command
17+
// returned zero rows with exit 0. --id did it (#411), and --delivery-group did
18+
// it again after that fix because nothing tied the promise to the encoding.
19+
//
20+
// So every such flag must appear below, with how its value is actually sent,
21+
// and a query filter must be one the client expands into a list. A new
22+
// comma-separated flag fails here until someone checks it and says how.
23+
24+
type commaFlagHandling int
25+
26+
const (
27+
// A filter on a list endpoint, sent through hookdeck.listQuery. The param
28+
// must be list-valued there, or a comma-joined value goes out as one.
29+
sentAsQueryList commaFlagHandling = iota
30+
// Split into an array in the JSON request body before sending.
31+
splitIntoBodyArray
32+
// Split into a slice the client sends as a list (metrics measures and
33+
// dimensions; every Outpost filter, via splitCommaList).
34+
splitIntoList
35+
// Sent as one string on purpose: the API turns the commas into spaces
36+
// (OAuth2 scopes, which the token endpoint expects space-separated).
37+
sentAsStringAPIConvertsCommas
38+
)
39+
40+
type commaFlag struct {
41+
kind commaFlagHandling
42+
param string // for sentAsQueryList: the query parameter the flag sets
43+
}
44+
45+
var commaSeparatedFlags = map[string]commaFlag{
46+
"hookdeck gateway event list --delivery-group": {kind: sentAsQueryList, param: "delivery_group"},
47+
"hookdeck gateway event list --id": {kind: sentAsQueryList, param: "id"},
48+
"hookdeck gateway request events --delivery-group": {kind: sentAsQueryList, param: "delivery_group"},
49+
"hookdeck gateway request list --id": {kind: sentAsQueryList, param: "id"},
50+
"hookdeck connection create --rule-deduplicate-exclude-fields": {kind: splitIntoBodyArray},
51+
"hookdeck connection create --rule-deduplicate-include-fields": {kind: splitIntoBodyArray},
52+
"hookdeck connection create --rule-retry-response-status-codes": {kind: splitIntoBodyArray},
53+
"hookdeck connection create --source-allowed-http-methods": {kind: splitIntoBodyArray},
54+
"hookdeck connection update --rule-deduplicate-exclude-fields": {kind: splitIntoBodyArray},
55+
"hookdeck connection update --rule-deduplicate-include-fields": {kind: splitIntoBodyArray},
56+
"hookdeck connection update --rule-retry-response-status-codes": {kind: splitIntoBodyArray},
57+
"hookdeck connection upsert --rule-deduplicate-exclude-fields": {kind: splitIntoBodyArray},
58+
"hookdeck connection upsert --rule-deduplicate-include-fields": {kind: splitIntoBodyArray},
59+
"hookdeck connection upsert --rule-retry-response-status-codes": {kind: splitIntoBodyArray},
60+
"hookdeck connection upsert --source-allowed-http-methods": {kind: splitIntoBodyArray},
61+
"hookdeck gateway connection create --rule-deduplicate-exclude-fields": {kind: splitIntoBodyArray},
62+
"hookdeck gateway connection create --rule-deduplicate-include-fields": {kind: splitIntoBodyArray},
63+
"hookdeck gateway connection create --rule-retry-response-status-codes": {kind: splitIntoBodyArray},
64+
"hookdeck gateway connection create --source-allowed-http-methods": {kind: splitIntoBodyArray},
65+
"hookdeck gateway connection update --rule-deduplicate-exclude-fields": {kind: splitIntoBodyArray},
66+
"hookdeck gateway connection update --rule-deduplicate-include-fields": {kind: splitIntoBodyArray},
67+
"hookdeck gateway connection update --rule-retry-response-status-codes": {kind: splitIntoBodyArray},
68+
"hookdeck gateway connection upsert --rule-deduplicate-exclude-fields": {kind: splitIntoBodyArray},
69+
"hookdeck gateway connection upsert --rule-deduplicate-include-fields": {kind: splitIntoBodyArray},
70+
"hookdeck gateway connection upsert --rule-retry-response-status-codes": {kind: splitIntoBodyArray},
71+
"hookdeck gateway connection upsert --source-allowed-http-methods": {kind: splitIntoBodyArray},
72+
"hookdeck gateway request retry --connection-ids": {kind: splitIntoBodyArray},
73+
"hookdeck gateway source create --allowed-http-methods": {kind: splitIntoBodyArray},
74+
"hookdeck gateway source update --allowed-http-methods": {kind: splitIntoBodyArray},
75+
"hookdeck gateway source upsert --allowed-http-methods": {kind: splitIntoBodyArray},
76+
"hookdeck connection create --destination-oauth2-scopes": {kind: sentAsStringAPIConvertsCommas},
77+
"hookdeck connection upsert --destination-oauth2-scopes": {kind: sentAsStringAPIConvertsCommas},
78+
"hookdeck gateway connection create --destination-oauth2-scopes": {kind: sentAsStringAPIConvertsCommas},
79+
"hookdeck gateway connection upsert --destination-oauth2-scopes": {kind: sentAsStringAPIConvertsCommas},
80+
"hookdeck gateway metrics attempts --dimensions": {kind: splitIntoList},
81+
"hookdeck gateway metrics attempts --measures": {kind: splitIntoList},
82+
"hookdeck gateway metrics events --dimensions": {kind: splitIntoList},
83+
"hookdeck gateway metrics events --measures": {kind: splitIntoList},
84+
"hookdeck gateway metrics requests --dimensions": {kind: splitIntoList},
85+
"hookdeck gateway metrics requests --measures": {kind: splitIntoList},
86+
"hookdeck gateway metrics transformations --dimensions": {kind: splitIntoList},
87+
"hookdeck gateway metrics transformations --measures": {kind: splitIntoList},
88+
"hookdeck outpost attempt get --include": {kind: splitIntoList},
89+
"hookdeck outpost attempt list --destination-id": {kind: splitIntoList},
90+
"hookdeck outpost attempt list --destination-type": {kind: splitIntoList},
91+
"hookdeck outpost attempt list --event-id": {kind: splitIntoList},
92+
"hookdeck outpost attempt list --include": {kind: splitIntoList},
93+
"hookdeck outpost attempt list --tenant-id": {kind: splitIntoList},
94+
"hookdeck outpost attempt list --topic": {kind: splitIntoList},
95+
"hookdeck outpost destination create --topics": {kind: splitIntoList},
96+
"hookdeck outpost destination list --topics": {kind: splitIntoList},
97+
"hookdeck outpost destination list --type": {kind: splitIntoList},
98+
"hookdeck outpost destination update --topics": {kind: splitIntoList},
99+
"hookdeck outpost event list --destination-id": {kind: splitIntoList},
100+
"hookdeck outpost event list --id": {kind: splitIntoList},
101+
"hookdeck outpost event list --tenant-id": {kind: splitIntoList},
102+
"hookdeck outpost event list --topic": {kind: splitIntoList},
103+
"hookdeck outpost metrics attempts --dimensions": {kind: splitIntoList},
104+
"hookdeck outpost metrics attempts --measures": {kind: splitIntoList},
105+
"hookdeck outpost metrics events --dimensions": {kind: splitIntoList},
106+
"hookdeck outpost metrics events --measures": {kind: splitIntoList},
107+
"hookdeck outpost tenant list --id": {kind: splitIntoList},
108+
}
109+
110+
var commaSeparated = regexp.MustCompile(`(?i)comma[- ]separated`)
111+
112+
func discoverCommaSeparatedFlags() map[string]bool {
113+
found := map[string]bool{}
114+
var walk func(c *cobra.Command)
115+
walk = func(c *cobra.Command) {
116+
// Flags() and PersistentFlags(), not LocalFlags(): LocalFlags merges
117+
// every parent's persistent flags into the command as a side effect,
118+
// which mutates the shared rootCmd for every later test in the package.
119+
// It put root's hidden --api-key onto outpost mcp and failed
120+
// TestOutpostMCPCommandIsRegistered.
121+
visit := func(f *pflag.Flag) {
122+
if commaSeparated.MatchString(f.Usage) {
123+
found[c.CommandPath()+" --"+f.Name] = true
124+
}
125+
}
126+
c.Flags().VisitAll(visit)
127+
c.PersistentFlags().VisitAll(visit)
128+
for _, sub := range c.Commands() {
129+
walk(sub)
130+
}
131+
}
132+
walk(rootCmd)
133+
return found
134+
}
135+
136+
func TestEveryCommaSeparatedFlagSendsAList(t *testing.T) {
137+
found := discoverCommaSeparatedFlags()
138+
if len(found) < 40 {
139+
t.Fatalf("found only %d comma-separated flags; the walk is probably broken and the guard would pass vacuously", len(found))
140+
}
141+
142+
for flag := range found {
143+
handling, ok := commaSeparatedFlags[flag]
144+
if !assert.True(t, ok,
145+
"%s says comma-separated but is not in commaSeparatedFlags. Check how its value "+
146+
"reaches the API -- a query filter sent as one value returns nothing (#411) -- "+
147+
"and add it with its handling.", flag) {
148+
continue
149+
}
150+
if handling.kind == sentAsQueryList {
151+
assert.True(t, hookdeck.IsListValuedParam(handling.param),
152+
"%s is a comma-separated query filter, but %q is not list-valued in hookdeck.listQuery, "+
153+
"so \"a,b\" is sent as one value and matches nothing", flag, handling.param)
154+
}
155+
}
156+
157+
// A stale entry would quietly cover a different flag added under the same
158+
// name later, so every entry must still exist.
159+
for flag := range commaSeparatedFlags {
160+
assert.True(t, found[flag], "%s is in commaSeparatedFlags but no longer exists or no longer says comma-separated", flag)
161+
}
162+
}

‎pkg/hookdeck/list_query.go‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,21 @@ import (
1111
// The comma-joined string used to be sent as one scalar value, which the API
1212
// matched against nothing: `--id evt_A,evt_B` returned zero rows with exit 0,
1313
// while each id on its own returned its row. See #411.
14+
//
15+
// delivery_group is declared the same way as id in the API -- a single value or
16+
// an array, with no comma splitting -- and was sent as one value in the same
17+
// way, so --delivery-group a,b returned nothing too.
1418
var listValuedParams = map[string]bool{
15-
"id": true,
19+
"id": true,
20+
"delivery_group": true,
1621
}
1722

23+
// IsListValuedParam reports whether a list-endpoint filter is sent as a list
24+
// when given a comma-separated value. The CLI's guard over flags documented as
25+
// comma-separated uses it, so a flag cannot promise a list the query does not
26+
// send.
27+
func IsListValuedParam(name string) bool { return listValuedParams[name] }
28+
1829
// listParamKey is how a repeated value is spelled on the wire.
1930
var listParamKey = func(key string) string { return key + "[]" }
2031

‎pkg/hookdeck/list_query_test.go‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,38 @@ func TestListEventsSendsIDsAsAList(t *testing.T) {
6868
assert.Equal(t, []string{"evt_A", "evt_B"}, got["id[]"])
6969
assert.Empty(t, got["id"])
7070
}
71+
72+
// delivery_group is declared exactly like id in the API -- a single value or an
73+
// array, no comma splitting -- so "a,b" as one value matched nothing and
74+
// --delivery-group a,b returned zero rows with exit 0, the #411 shape. Found by
75+
// the guard over flags documented as comma-separated.
76+
func TestListQueryExpandsCommaSeparatedDeliveryGroups(t *testing.T) {
77+
q := listQuery(map[string]string{"delivery_group": "cus_1,cus_2"})
78+
assert.Equal(t, []string{"cus_1", "cus_2"}, q["delivery_group[]"])
79+
assert.Empty(t, q["delivery_group"])
80+
}
81+
82+
// GetRequestEvents built its own query and so was missed by the #411 fix. This
83+
// covers it using the shared builder, for both list-valued filters.
84+
func TestGetRequestEventsSendsListValuedFiltersAsLists(t *testing.T) {
85+
var got url.Values
86+
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
87+
got = r.URL.Query()
88+
_ = json.NewEncoder(w).Encode(map[string]any{"models": []any{}, "pagination": map[string]any{}})
89+
}))
90+
defer srv.Close()
91+
92+
base, err := url.Parse(srv.URL)
93+
require.NoError(t, err)
94+
c := &Client{BaseURL: base}
95+
96+
_, err = c.GetRequestEvents(context.Background(), "req_1", map[string]string{
97+
"id": "evt_A,evt_B",
98+
"delivery_group": "cus_1,cus_2",
99+
})
100+
require.NoError(t, err)
101+
102+
assert.Equal(t, []string{"evt_A", "evt_B"}, got["id[]"])
103+
assert.Equal(t, []string{"cus_1", "cus_2"}, got["delivery_group[]"])
104+
assert.Empty(t, got["delivery_group"])
105+
}

‎pkg/hookdeck/requests.go‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,13 +133,12 @@ func (c *Client) GetRequestEvents(ctx context.Context, requestID string, params
133133
if err != nil {
134134
return nil, err
135135
}
136+
// Through listQuery like the other event lists, so a comma-separated
137+
// list-valued filter is sent as a list. This built its own query and was
138+
// missed by #411, so --delivery-group a,b still went out as one value.
136139
queryStr := ""
137140
if len(params) > 0 {
138-
q := url.Values{}
139-
for k, v := range params {
140-
q.Add(k, v)
141-
}
142-
queryStr = q.Encode()
141+
queryStr = listQuery(params).Encode()
143142
}
144143
resp, err := c.Get(ctx, path, queryStr, nil)
145144
if err != nil {

‎pkg/mcpcore/input.go‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,17 @@ func (in Input) BoolOrStringE(key string) (*bool, error) {
163163
return nil, fmt.Errorf("%s must be true or false, got %v", key, v)
164164
}
165165

166-
// StringSlice returns the string slice for a key, or nil if missing.
167-
func (in Input) StringSlice(key string) []string {
166+
// stringSlice reads a value only when it is a JSON array, and returns nil for
167+
// anything else -- including the bare or comma-separated string a model sends
168+
// where an array is declared.
169+
//
170+
// It is unexported on purpose. As a public method, tool handlers read
171+
// caller-supplied lists with it, and a string was dropped without an error:
172+
// gateway_metrics_read told callers "measures is required" for a measures they
173+
// had just passed (#440), and a merge later reverted the fix unnoticed. Tool
174+
// code must use StringList, which accepts both forms; with this unexported, a
175+
// handler that reaches for the array-only read no longer compiles.
176+
func (in Input) stringSlice(key string) []string {
168177
v, ok := in[key]
169178
if !ok {
170179
return nil
@@ -240,7 +249,7 @@ func SetPayloadSearchFilters(params map[string]string, in Input) error {
240249
// StringList reads a value that may be given either as an array of strings or,
241250
// mirroring the CLI's comma-separated flags, as a single string.
242251
func StringList(in Input, key string) []string {
243-
if values := in.StringSlice(key); len(values) > 0 {
252+
if values := in.stringSlice(key); len(values) > 0 {
244253
return values
245254
}
246255
raw := in.String(key)
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package mcpcore
2+
3+
import (
4+
"os"
5+
"path/filepath"
6+
"regexp"
7+
"strings"
8+
"testing"
9+
)
10+
11+
// Models routinely send a bare or comma-separated string where a tool declares
12+
// an array. mcpcore.StringList accepts both; reading a caller-supplied list any
13+
// other way drops the string without an error. That is how gateway_metrics_read
14+
// came to tell callers "measures is required" for a measures they had passed
15+
// (#440) -- and how a merge later put the bug back unnoticed.
16+
//
17+
// stringSlice is unexported, so tool code cannot call the array-only reader.
18+
// This covers the remaining way round it: asserting .([]interface{}) directly
19+
// on a tool argument. Each existing use is listed with the reason it is right;
20+
// a new one fails here until someone decides it belongs on that list.
21+
var arrayAssertionAllowed = map[string]string{
22+
// rules is an array of rule OBJECTS, not strings, so StringList does not
23+
// apply -- and ruleList errors on a non-array rather than dropping it.
24+
"gateway/mcp/tool_connections.go": "rules: array of objects, and a non-array is an error",
25+
}
26+
27+
var arrayAssertion = regexp.MustCompile(`\.\(\s*\[\](interface\{\}|any)\s*\)`)
28+
29+
func TestToolArgumentsAreNotReadAsArraysDirectly(t *testing.T) {
30+
for _, dir := range []string{"../gateway/mcp", "../outpost/mcp"} {
31+
files, err := filepath.Glob(filepath.Join(dir, "*.go"))
32+
if err != nil {
33+
t.Fatal(err)
34+
}
35+
if len(files) == 0 {
36+
t.Fatalf("no Go files found in %s; the guard would pass vacuously", dir)
37+
}
38+
for _, f := range files {
39+
if strings.HasSuffix(f, "_test.go") {
40+
continue
41+
}
42+
src, err := os.ReadFile(f)
43+
if err != nil {
44+
t.Fatal(err)
45+
}
46+
if !arrayAssertion.Match(src) {
47+
continue
48+
}
49+
key := strings.TrimPrefix(filepath.ToSlash(f), "../")
50+
if _, ok := arrayAssertionAllowed[key]; ok {
51+
continue
52+
}
53+
t.Errorf("%s asserts .([]interface{}) on tool input. Read a list argument with "+
54+
"mcpcore.StringList, which also accepts the string forms models send; "+
55+
"if this really is not a list of strings, add the file to arrayAssertionAllowed with the reason.", key)
56+
}
57+
}
58+
}
59+
60+
// An allow-list entry for a file that no longer has the assertion is stale, and
61+
// a stale entry would silently cover the next use added to that file.
62+
func TestArrayAssertionAllowListIsCurrent(t *testing.T) {
63+
for key := range arrayAssertionAllowed {
64+
src, err := os.ReadFile(filepath.Join("..", key))
65+
if err != nil {
66+
t.Errorf("allow-listed file %s: %v", key, err)
67+
continue
68+
}
69+
if !arrayAssertion.Match(src) {
70+
t.Errorf("%s is allow-listed but no longer asserts .([]interface{}); remove the entry", key)
71+
}
72+
}
73+
}

‎pkg/mcpcore/input_test.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ func TestInput_Accessors(t *testing.T) {
6969
assert.Equal(t, 99, in.Int("nonexistent", 99))
7070
assert.Equal(t, true, in.Bool("active"))
7171
assert.Equal(t, false, in.Bool("nonexistent"))
72-
assert.Equal(t, []string{"a", "b"}, in.StringSlice("tags"))
73-
assert.Nil(t, in.StringSlice("nonexistent"))
72+
assert.Equal(t, []string{"a", "b"}, in.stringSlice("tags"))
73+
assert.Nil(t, in.stringSlice("nonexistent"))
7474

7575
bp := in.BoolOrString("active")
7676
require.NotNil(t, bp)

0 commit comments

Comments
 (0)