Skip to content

Commit 3a60cf7

Browse files
authored
🧹 chore: Refactor EnvVar middleware (#3513)
* remove ExcludeVars option * Simplify envvar export logic
1 parent f00e355 commit 3a60cf7

4 files changed

Lines changed: 26 additions & 34 deletions

File tree

docs/middleware/envvar.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,13 @@ import (
2626
After you initiate your Fiber app, you can use the following possibilities:
2727

2828
```go
29-
// Initialize default config
29+
// Initialize default config (exports no variables)
3030
app.Use("/expose/envvars", envvar.New())
3131

3232
// Or extend your config for customization
3333
app.Use("/expose/envvars", envvar.New(
3434
envvar.Config{
35-
ExportVars: map[string]string{"testKey": "", "testDefaultKey": "testDefaultVal"},
36-
ExcludeVars: map[string]string{"excludeKey": ""},
35+
ExportVars: map[string]string{"testKey": "", "testDefaultKey": "testDefaultVal"},
3736
}),
3837
)
3938
```
@@ -60,11 +59,11 @@ Http response contract:
6059

6160
| Property | Type | Description | Default |
6261
|:------------|:--------------------|:-----------------------------------------------------------------------------|:--------|
63-
| ExportVars | `map[string]string` | ExportVars specifies the environment variables that should be exported. | `nil` |
64-
| ExcludeVars | `map[string]string` | ExcludeVars specifies the environment variables that should not be exported. | `nil` |
62+
| ExportVars | `map[string]string` | ExportVars specifies the environment variables that should be exported. | `nil` |
6563

6664
## Default Config
6765

6866
```go
6967
Config{}
68+
// Exports no environment variables
7069
```

docs/whats_new.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,6 +1012,10 @@ We've added support for `zstd` compression on top of `gzip`, `deflate`, and `bro
10121012

10131013
Added support for specifying Key length when using `encryptcookie.GenerateKey(length)`. This allows the user to generate keys compatible with `AES-128`, `AES-192`, and `AES-256` (Default).
10141014

1015+
### EnvVar
1016+
1017+
The `ExcludeVars` field has been removed from the EnvVar middleware configuration. When upgrading, remove any references to this field and explicitly list the variables you wish to expose using `ExportVars`.
1018+
10151019
### Filesystem
10161020

10171021
We've decided to remove filesystem middleware to clear up the confusion between static and filesystem middleware.

middleware/envvar/envvar.go

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package envvar
22

33
import (
44
"os"
5-
"strings"
65

76
"github.com/gofiber/fiber/v3"
87
)
@@ -11,8 +10,6 @@ import (
1110
type Config struct {
1211
// ExportVars specifies the environment variables that should export
1312
ExportVars map[string]string
14-
// ExcludeVars specifies the environment variables that should not export
15-
ExcludeVars map[string]string
1613
}
1714

1815
type EnvVar struct {
@@ -47,20 +44,16 @@ func New(config ...Config) fiber.Handler {
4744
func newEnvVar(cfg Config) *EnvVar {
4845
vars := &EnvVar{Vars: make(map[string]string)}
4946

50-
if len(cfg.ExportVars) > 0 {
51-
for key, defaultVal := range cfg.ExportVars {
52-
vars.set(key, defaultVal)
53-
if envVal, exists := os.LookupEnv(key); exists {
54-
vars.set(key, envVal)
55-
}
56-
}
57-
} else {
58-
const numElems = 2
59-
for _, envVal := range os.Environ() {
60-
keyVal := strings.SplitN(envVal, "=", numElems)
61-
if _, exists := cfg.ExcludeVars[keyVal[0]]; !exists {
62-
vars.set(keyVal[0], keyVal[1])
63-
}
47+
if len(cfg.ExportVars) == 0 {
48+
// do not expose environment variables when no configuration
49+
// is supplied to prevent accidental information disclosure
50+
return vars
51+
}
52+
53+
for key, defaultVal := range cfg.ExportVars {
54+
vars.set(key, defaultVal)
55+
if envVal, exists := os.LookupEnv(key); exists {
56+
vars.set(key, envVal)
6457
}
6558
}
6659

middleware/envvar/envvar_test.go

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,15 @@ import (
1111
"github.com/stretchr/testify/require"
1212
)
1313

14-
func Test_EnvVarStructWithExportVarsExcludeVars(t *testing.T) {
14+
func Test_EnvVarStructWithExportVars(t *testing.T) {
1515
t.Setenv("testKey", "testEnvValue")
1616
t.Setenv("anotherEnvKey", "anotherEnvVal")
17-
t.Setenv("excludeKey", "excludeEnvValue")
18-
1917
vars := newEnvVar(Config{
20-
ExportVars: map[string]string{"testKey": "", "testDefaultKey": "testDefaultVal"},
21-
ExcludeVars: map[string]string{"excludeKey": ""},
18+
ExportVars: map[string]string{"testKey": "", "testDefaultKey": "testDefaultVal"},
2219
})
2320

2421
require.Equal(t, "testEnvValue", vars.Vars["testKey"])
2522
require.Equal(t, "testDefaultVal", vars.Vars["testDefaultKey"])
26-
require.Equal(t, "", vars.Vars["excludeKey"])
2723
require.Equal(t, "", vars.Vars["anotherEnvKey"])
2824
}
2925

@@ -92,8 +88,8 @@ func Test_EnvVarHandlerDefaultConfig(t *testing.T) {
9288

9389
var envVars EnvVar
9490
require.NoError(t, json.Unmarshal(respBody, &envVars))
95-
val := envVars.Vars["testEnvKey"]
96-
require.Equal(t, "testEnvVal", val)
91+
_, exists := envVars.Vars["testEnvKey"]
92+
require.False(t, exists)
9793
}
9894

9995
func Test_EnvVarHandlerMethod(t *testing.T) {
@@ -113,8 +109,8 @@ func Test_EnvVarHandlerSpecialValue(t *testing.T) {
113109
t.Setenv(testEnvKey, fakeBase64)
114110

115111
app := fiber.New()
116-
app.Use("/envvars", New())
117112
app.Use("/envvars/export", New(Config{ExportVars: map[string]string{testEnvKey: ""}}))
113+
app.Use("/envvars", New())
118114

119115
req, err := http.NewRequestWithContext(context.Background(), fiber.MethodGet, "http://localhost/envvars", nil)
120116
require.NoError(t, err)
@@ -126,8 +122,8 @@ func Test_EnvVarHandlerSpecialValue(t *testing.T) {
126122

127123
var envVars EnvVar
128124
require.NoError(t, json.Unmarshal(respBody, &envVars))
129-
val := envVars.Vars[testEnvKey]
130-
require.Equal(t, fakeBase64, val)
125+
_, exists := envVars.Vars[testEnvKey]
126+
require.False(t, exists)
131127

132128
req, err = http.NewRequestWithContext(context.Background(), fiber.MethodGet, "http://localhost/envvars/export", nil)
133129
require.NoError(t, err)
@@ -139,6 +135,6 @@ func Test_EnvVarHandlerSpecialValue(t *testing.T) {
139135

140136
var envVarsExport EnvVar
141137
require.NoError(t, json.Unmarshal(respBody, &envVarsExport))
142-
val = envVarsExport.Vars[testEnvKey]
138+
val := envVarsExport.Vars[testEnvKey]
143139
require.Equal(t, fakeBase64, val)
144140
}

0 commit comments

Comments
 (0)