From d0e5eca1f4ec5495265f51fd8ce8296853ba1e45 Mon Sep 17 00:00:00 2001 From: Alex Tan Date: Sun, 11 Jul 2021 16:09:55 -0500 Subject: [PATCH] Don't panic when regex doesn't compile Instead, print a warning and discard the invalid rule. --- customHeaders.go | 38 +++++++++++++++++++++++++++++--------- customHeaders_test.go | 39 +++++++++++++++++++++++++++++++++++++++ go.mod | 2 ++ go.sum | 11 +++++++++++ 4 files changed, 81 insertions(+), 9 deletions(-) create mode 100644 customHeaders_test.go create mode 100644 go.sum diff --git a/customHeaders.go b/customHeaders.go index f001a32..29aff43 100644 --- a/customHeaders.go +++ b/customHeaders.go @@ -26,10 +26,20 @@ type HeaderConfig struct { CompiledRegex *regexp.Regexp } -func (config *HeaderConfig) Init() { +func (config *HeaderConfig) Init() (ok bool) { if len(config.Regex) > 0 { - config.CompiledRegex = regexp.MustCompile(config.Regex) + regex, err := regexp.Compile(config.Regex) + + if err != nil { + fmt.Println("WARNING: Ignoring rule with regex error:", err) + fmt.Println("") + return + } + config.CompiledRegex = regex } + + ok = true + return } func (config *HeaderConfig) UsesRegex() bool { @@ -42,7 +52,7 @@ type HeaderDefiniton struct { Value string `json:"value"` } -var headerConfigs *HeaderConfigArray +var headerConfigs HeaderConfigArray func fileExists(filename string) bool { info, err := os.Stat(filename) @@ -52,7 +62,7 @@ func fileExists(filename string) bool { return !info.IsDir() } -func logHeaderConfig(config *HeaderConfig) { +func logHeaderConfig(config HeaderConfig) { if config.UsesRegex() { fmt.Println("Regex: " + config.Regex) } else { @@ -74,7 +84,7 @@ func initHeaderConfig(headerConfigPath string) bool { if fileExists(headerConfigPath) { jsonFile, err := os.Open(headerConfigPath) if err != nil { - fmt.Println("Cant't read header config file. Error:") + fmt.Println("Can't read header config file. Error:") fmt.Println(err) } else { byteValue, _ := ioutil.ReadAll(jsonFile) @@ -83,12 +93,22 @@ func initHeaderConfig(headerConfigPath string) bool { if len(headerConfigs.Configs) > 0 { headerConfigValid = true + + // Only keep valid config entries. + keepers := make([]HeaderConfig, 0) + for _, configEntry := range headerConfigs.Configs { + ok := configEntry.Init() + if ok { + keepers = append(keepers, configEntry) + } + } + + headerConfigs.Configs = keepers + + // Print the config entries that are kept. fmt.Println("Found header config file. Rules:") fmt.Println("------------------------------") - - for i := 0; i < len(headerConfigs.Configs); i++ { - configEntry := &headerConfigs.Configs[i] - configEntry.Init() + for _, configEntry := range headerConfigs.Configs { logHeaderConfig(configEntry) } } else { diff --git a/customHeaders_test.go b/customHeaders_test.go new file mode 100644 index 0000000..81a478b --- /dev/null +++ b/customHeaders_test.go @@ -0,0 +1,39 @@ +package main + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestHeaderConfigWithValidRegex(t *testing.T) { + assert := assert.New(t) + config := HeaderConfig{Regex: "/$"} + + ok := config.Init() + assert.True(ok) + assert.NotNil(config.CompiledRegex) + assert.True(config.UsesRegex()) +} + +func TestHeaderConfigWithInvalidRegex(t *testing.T) { + assert := assert.New(t) + config := HeaderConfig{Regex: "["} + + ok := config.Init() + assert.False(ok) + assert.Nil(config.CompiledRegex) +} + +func TestHeaderConfigWithoutRegex(t *testing.T) { + assert := assert.New(t) + config := HeaderConfig{ + Path: "/page-data", + FileExtension: "json", + } + + ok := config.Init() + assert.True(ok) + assert.Nil(config.CompiledRegex) + assert.False(config.UsesRegex()) +} diff --git a/go.mod b/go.mod index 0d42a78..af73224 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ module github.com/PierreZ/goStatic go 1.16 + +require github.com/stretchr/testify v1.7.0 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..acb88a4 --- /dev/null +++ b/go.sum @@ -0,0 +1,11 @@ +github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=