Skip to content

Commit

Permalink
🐛 Fix: EnvVar middleware parses base64 incorrectly (#2069)
Browse files Browse the repository at this point in the history
* Fix: EnvVar middleware parses base64 incorrectly

* Chore: fix typo in README.md

* Chore: standardize and simplify response
  • Loading branch information
fufuok committed Sep 3, 2022
1 parent b7500a8 commit 87faeda
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 23 deletions.
22 changes: 11 additions & 11 deletions middleware/envvar/README.md
Expand Up @@ -44,10 +44,12 @@ app.Use("/expose/envvars", envvar.New())
### Custom Config

```go
app.Use("/expose/envvars", envvar.New(envvar.Config{
ExportVars: map[string]string{"testKey": "", "testDefaultKey": "testDefaultVal"},
ExcludeVars: map[string]string{"excludeKey": ""}}
}))
app.Use("/expose/envvars", envvar.New(
envvar.Config{
ExportVars: map[string]string{"testKey": "", "testDefaultKey": "testDefaultVal"},
ExcludeVars: map[string]string{"excludeKey": ""},
}),
)
```

### Response
Expand All @@ -57,23 +59,21 @@ Http response contract:
{
"vars": {
"someEnvVariable": "someValue",
"anotherEnvVariable": "anotherValue",
"anotherEnvVariable": "anotherValue"
}
}
```

## Config

```go
// Config defines the config for middleware.
type Config struct {
// ExportVars specifies the environment variables that should export
ExportVars map[string]string
// ExcludeVars specifies the environment variables that should not export
ExcludeVars map[string]string
// ExportVars specifies the environment variables that should export
ExportVars map[string]string
// ExcludeVars specifies the environment variables that should not export
ExcludeVars map[string]string
}

```

## Default Config
Expand Down
16 changes: 6 additions & 10 deletions middleware/envvar/envvar.go
@@ -1,10 +1,10 @@
package envvar

import (
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/utils"
"os"
"strings"

"github.com/gofiber/fiber/v2"
)

// Config defines the config for middleware.
Expand Down Expand Up @@ -39,14 +39,10 @@ func New(config ...Config) fiber.Handler {
envVar := newEnvVar(cfg)
varsByte, err := c.App().Config().JSONEncoder(envVar)
if err != nil {
c.Response().SetBodyRaw(utils.UnsafeBytes(err.Error()))
c.Response().SetStatusCode(fiber.StatusInternalServerError)
return nil
return c.Status(fiber.StatusInternalServerError).SendString(err.Error())
}
c.Response().SetBodyRaw(varsByte)
c.Response().SetStatusCode(fiber.StatusOK)
c.Response().Header.Set("Content-Type", "application/json; charset=utf-8")
return nil
c.Set(fiber.HeaderContentType, fiber.MIMEApplicationJSONCharsetUTF8)
return c.Send(varsByte)
}
}

Expand All @@ -62,7 +58,7 @@ func newEnvVar(cfg Config) *EnvVar {
}
} else {
for _, envVal := range os.Environ() {
keyVal := strings.Split(envVal, "=")
keyVal := strings.SplitN(envVal, "=", 2)
if _, exists := cfg.ExcludeVars[keyVal[0]]; !exists {
vars.set(keyVal[0], keyVal[1])
}
Expand Down
40 changes: 38 additions & 2 deletions middleware/envvar/envvar_test.go
Expand Up @@ -2,12 +2,13 @@ package envvar

import (
"encoding/json"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/utils"
"io/ioutil"
"net/http"
"os"
"testing"

"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/utils"
)

func TestEnvVarStructWithExportVarsExcludeVars(t *testing.T) {
Expand Down Expand Up @@ -102,3 +103,38 @@ func TestEnvVarHandlerMethod(t *testing.T) {
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, fiber.StatusMethodNotAllowed, resp.StatusCode)
}

func TestEnvVarHandlerSpecialValue(t *testing.T) {
testEnvKey := "testEnvKey"
fakeBase64 := "testBase64:TQ=="
os.Setenv(testEnvKey, fakeBase64)
defer os.Unsetenv(testEnvKey)

app := fiber.New()
app.Use("/envvars", New())
app.Use("/envvars/export", New(Config{ExportVars: map[string]string{testEnvKey: ""}}))

req, _ := http.NewRequest("GET", "http://localhost/envvars", nil)
resp, err := app.Test(req)
utils.AssertEqual(t, nil, err)

respBody, err := ioutil.ReadAll(resp.Body)
utils.AssertEqual(t, nil, err)

var envVars EnvVar
utils.AssertEqual(t, nil, json.Unmarshal(respBody, &envVars))
val := envVars.Vars[testEnvKey]
utils.AssertEqual(t, fakeBase64, val)

req, _ = http.NewRequest("GET", "http://localhost/envvars/export", nil)
resp, err = app.Test(req)
utils.AssertEqual(t, nil, err)

respBody, err = ioutil.ReadAll(resp.Body)
utils.AssertEqual(t, nil, err)

var envVarsExport EnvVar
utils.AssertEqual(t, nil, json.Unmarshal(respBody, &envVarsExport))
val = envVarsExport.Vars[testEnvKey]
utils.AssertEqual(t, fakeBase64, val)
}

1 comment on commit 87faeda

@ReneWerner87
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 2.

Benchmark suite Current: 87faeda Previous: 400c828 Ratio
Benchmark_Ctx_Protocol 19.99 ns/op 0 B/op 0 allocs/op 2.729 ns/op 0 B/op 0 allocs/op 7.33

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.