Просмотр исходного кода

fix(api): only validate custom regex types for configured arguments (#1172)

James Read 2 дней назад
Родитель
Сommit
4c7ddbcc5c
2 измененных файлов с 69 добавлено и 1 удалено
  1. 11 1
      service/internal/api/api.go
  2. 58 0
      service/internal/api/api_test.go

+ 11 - 1
service/internal/api/api.go

@@ -977,7 +977,7 @@ func (api *oliveTinAPI) validateArgumentTypeConnectResponse(msg *apiv1.ValidateA
 
 func (api *oliveTinAPI) validateArgumentTypeInternal(msg *apiv1.ValidateArgumentTypeRequest) error {
 	if msg.BindingId == "" || msg.ArgumentName == "" {
-		return executor.TypeSafetyCheck("", msg.Value, msg.Type)
+		return validateUnboundArgumentType(msg)
 	}
 
 	arg, action := api.findArgumentForValidation(msg.BindingId, msg.ArgumentName)
@@ -992,6 +992,16 @@ func (api *oliveTinAPI) validateArgumentTypeInternal(msg *apiv1.ValidateArgument
 	return executor.ValidateArgument(arg, msg.Value, action)
 }
 
+// Custom regex types only exist on configured arguments, so validating one
+// without a binding would only evaluate a caller-supplied pattern.
+func validateUnboundArgumentType(msg *apiv1.ValidateArgumentTypeRequest) error {
+	if strings.HasPrefix(msg.Type, "regex:") {
+		return fmt.Errorf("custom regex types can only be validated for a configured action argument")
+	}
+
+	return executor.TypeSafetyCheck("", msg.Value, msg.Type)
+}
+
 func (api *oliveTinAPI) findArgumentForValidation(bindingId string, argumentName string) (*config.ActionArgument, *config.Action) {
 	binding := api.executor.FindBindingByID(bindingId)
 	if binding == nil || binding.Action == nil {

+ 58 - 0
service/internal/api/api_test.go

@@ -1194,3 +1194,61 @@ func TestBuildChoicesExpandsChecklistEntityChoices(t *testing.T) {
 	assert.Equal(t, "basement", choices[1].Value)
 	assert.Equal(t, "basement", choices[1].Title)
 }
+
+func TestValidateArgumentTypeRejectsUnboundCustomRegex(t *testing.T) {
+	cfg := config.DefaultConfig()
+	ts, client := getNewTestServerAndClient(cfg)
+	defer ts.Close()
+
+	resp, err := client.ValidateArgumentType(context.Background(), connect.NewRequest(&apiv1.ValidateArgumentTypeRequest{
+		Value: "aaaa",
+		Type:  "regex:a+",
+	}))
+	require.NoError(t, err)
+	assert.False(t, resp.Msg.Valid, "a caller-supplied regex must not be evaluated without a binding")
+	assert.Contains(t, resp.Msg.Description, "custom regex types can only be validated for a configured action argument")
+}
+
+func TestValidateArgumentTypeUnboundBuiltinTypeStillValidates(t *testing.T) {
+	cfg := config.DefaultConfig()
+	ts, client := getNewTestServerAndClient(cfg)
+	defer ts.Close()
+
+	valid, err := client.ValidateArgumentType(context.Background(), connect.NewRequest(&apiv1.ValidateArgumentTypeRequest{
+		Value: "hello",
+		Type:  "ascii",
+	}))
+	require.NoError(t, err)
+	assert.True(t, valid.Msg.Valid)
+
+	invalid, err := client.ValidateArgumentType(context.Background(), connect.NewRequest(&apiv1.ValidateArgumentTypeRequest{
+		Value: "hello; rm -rf /",
+		Type:  "ascii_identifier",
+	}))
+	require.NoError(t, err)
+	assert.False(t, invalid.Msg.Valid)
+}
+
+func TestValidateArgumentTypeBoundCustomRegexStillValidates(t *testing.T) {
+	cfg := config.DefaultConfig()
+	cfg.Actions = append(cfg.Actions, &config.Action{
+		ID:    "regex_action",
+		Title: "Regex action",
+		Exec:  []string{"echo", "{{ code }}"},
+		Arguments: []config.ActionArgument{
+			{Name: "code", Type: "regex:[a-z]{3}"},
+		},
+	})
+	cfg.Sanitize()
+	ts, client := getNewTestServerAndClient(cfg)
+	defer ts.Close()
+
+	resp, err := client.ValidateArgumentType(context.Background(), connect.NewRequest(&apiv1.ValidateArgumentTypeRequest{
+		BindingId:    "regex_action",
+		ArgumentName: "code",
+		Value:        "abc",
+		Type:         "regex:[a-z]{3}",
+	}))
+	require.NoError(t, err)
+	assert.True(t, resp.Msg.Valid, "configured custom regex arguments must still validate")
+}