Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 1 addition & 16 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"os"
"path"
"path/filepath"
"reflect"
"regexp"
"sort"
"strconv"
Expand Down Expand Up @@ -352,7 +351,6 @@ var (
initConfigTemplate = template.Must(template.New("initConfig").Parse(initConfigEmbed))

invalidProjectId = regexp.MustCompile("[^a-zA-Z0-9_.-]+")
envPattern = regexp.MustCompile(`^env\((.*)\)$`)
refPattern = regexp.MustCompile(`^[a-z]{20}$`)
)

Expand Down Expand Up @@ -428,7 +426,7 @@ func (c *config) loadFromReader(v *viper.Viper, r io.Reader) error {
dc.TagName = "toml"
dc.Squash = true
dc.ZeroFields = true
dc.DecodeHook = c.newDecodeHook(LoadEnvHook)
dc.DecodeHook = c.newDecodeHook(LoadEnvHook, ValidateFunctionsHook)
}); err != nil {
return errors.Errorf("failed to parse config: %w", err)
}
Expand Down Expand Up @@ -776,19 +774,6 @@ func assertEnvLoaded(s string) error {
return nil
}

func LoadEnvHook(f reflect.Kind, t reflect.Kind, data interface{}) (interface{}, error) {
if f != reflect.String {
return data, nil
}
value := data.(string)
if matches := envPattern.FindStringSubmatch(value); len(matches) > 1 {
if env := os.Getenv(matches[1]); len(env) > 0 {
value = env
}
}
return value, nil
}

func truncateText(text string, maxLen int) string {
if len(text) > maxLen {
return text[:maxLen]
Expand Down
64 changes: 64 additions & 0 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -431,3 +431,67 @@ func TestLoadFunctionImportMap(t *testing.T) {
assert.Equal(t, "supabase/custom_import_map.json", config.Functions["hello"].ImportMap)
})
}

func TestLoadFunctionErrorMessageParsing(t *testing.T) {
t.Run("returns error for array-style function config", func(t *testing.T) {
config := NewConfig()
fsys := fs.MapFS{
"supabase/config.toml": &fs.MapFile{Data: []byte(`
project_id = "bvikqvbczudanvggcord"
[[functions]]
name = "hello"
verify_jwt = true
`)},
}
// Run test
err := config.Load("", fsys)
// Check error contains both decode errors
assert.Error(t, err)
assert.Contains(t, err.Error(), invalidFunctionsConfigFormat)
})
t.Run("returns error with function slug for invalid non-existent field", func(t *testing.T) {
config := NewConfig()
fsys := fs.MapFS{
"supabase/config.toml": &fs.MapFile{Data: []byte(`
project_id = "bvikqvbczudanvggcord"
[functions.hello]
unknown_field = true
`)},
}
// Run test
err := config.Load("", fsys)
// Check error contains both decode errors
assert.Error(t, err)
assert.Contains(t, err.Error(), "'functions[hello]' has invalid keys: unknown_field")
})
t.Run("returns error with function slug for invalid field value", func(t *testing.T) {
config := NewConfig()
fsys := fs.MapFS{
"supabase/config.toml": &fs.MapFile{Data: []byte(`
project_id = "bvikqvbczudanvggcord"
[functions.hello]
verify_jwt = "not-a-bool"
`)},
}
// Run test
err := config.Load("", fsys)
// Check error contains both decode errors
assert.Error(t, err)
assert.Contains(t, err.Error(), "cannot parse 'functions[hello].verify_jwt' as bool: strconv.ParseBool: parsing \"not-a-bool\"")
})
t.Run("returns error for unknown function fields", func(t *testing.T) {
config := NewConfig()
fsys := fs.MapFS{
"supabase/config.toml": &fs.MapFile{Data: []byte(`
project_id = "bvikqvbczudanvggcord"
[functions]
name = "hello"
verify_jwt = true
`)},
}
// Run test
err := config.Load("", fsys)
assert.Error(t, err)
assert.Contains(t, err.Error(), invalidFunctionsConfigFormat)
})
}
68 changes: 68 additions & 0 deletions pkg/config/decode_hooks.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package config

import (
"os"
"reflect"
"regexp"

"github.com/go-errors/errors"
)

var envPattern = regexp.MustCompile(`^env\((.*)\)$`)

const invalidFunctionsConfigFormat = `Invalid functions config format. Functions should be configured as:

[functions.<function-name>]
field = value

Example:
[functions.hello]
verify_jwt = true`

// LoadEnvHook is a mapstructure decode hook that loads environment variables
// from strings formatted as env(VAR_NAME).
func LoadEnvHook(f reflect.Kind, t reflect.Kind, data interface{}) (interface{}, error) {
if f != reflect.String {
return data, nil
}
value := data.(string)
if matches := envPattern.FindStringSubmatch(value); len(matches) > 1 {
if env := os.Getenv(matches[1]); len(env) > 0 {
value = env
}
}
return value, nil
}

// ValidateFunctionsHook is a mapstructure decode hook that validates the functions config format.
func ValidateFunctionsHook(f reflect.Type, t reflect.Type, data interface{}) (interface{}, error) {
// Only handle FunctionConfig type
if t != reflect.TypeOf(FunctionConfig{}) {
return data, nil
}

// Check if source is not a map
if f.Kind() != reflect.Map {
return nil, errors.New(invalidFunctionsConfigFormat)
}

// Check if any fields are defined directly under [functions] instead of [functions.<name>]
if m, ok := data.(map[string]interface{}); ok {
for _, value := range m {
// Skip nil values and empty function configs as they're valid
if value == nil {
continue
}
// If it's already a function type, it's valid
if _, isFunction := value.(function); isFunction {
continue
}
// If the value is not a map, it means it's defined directly under [functions]
if _, isMap := value.(map[string]interface{}); !isMap {
return nil, errors.New(invalidFunctionsConfigFormat)
}
}
}

return data, nil
}