1
0
mirror of https://github.com/gofiber/fiber.git synced 2025-02-21 07:12:51 +00:00

🐛 Fix: EnvVar middleware parses base64 incorrectly (#2069)

* Fix: EnvVar middleware parses base64 incorrectly

* Chore: fix typo in README.md

* Chore: standardize and simplify response
This commit is contained in:
Fufu 2022-09-04 01:03:51 +08:00 committed by GitHub
parent b7500a8d08
commit 87faeda5c1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 55 additions and 23 deletions

View File

@ -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
@ -57,10 +59,9 @@ Http response contract:
{
"vars": {
"someEnvVariable": "someValue",
"anotherEnvVariable": "anotherValue",
"anotherEnvVariable": "anotherValue"
}
}
```
## Config
@ -68,12 +69,11 @@ Http response contract:
```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

View File

@ -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.
@ -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)
}
}
@ -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])
}

View File

@ -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) {
@ -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)
}