Skip to content

Commit

Permalink
Fix potential panic in audit during header formatting (#22694)
Browse files Browse the repository at this point in the history
  • Loading branch information
peteski22 committed Aug 31, 2023
1 parent 56ce895 commit d2d3d8a
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
7 changes: 6 additions & 1 deletion audit/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package audit

import (
"errors"
"reflect"
"strings"
"time"
)
Expand Down Expand Up @@ -150,9 +151,13 @@ func WithHMACAccessor(h bool) Option {
}

// WithHeaderFormatter provides an Option to supply a HeaderFormatter.
// If the HeaderFormatter interface supplied is nil (type or value), the option will not be applied.
func WithHeaderFormatter(f HeaderFormatter) Option {
return func(o *options) error {
o.withHeaderFormatter = f
if f != nil && !reflect.ValueOf(f).IsNil() {
o.withHeaderFormatter = f
}

return nil
}
}
49 changes: 49 additions & 0 deletions audit/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package audit

import (
"context"
"testing"
"time"

Expand Down Expand Up @@ -361,6 +362,45 @@ func TestOptions_WithOmitTime(t *testing.T) {
}
}

// TestOptions_WithHeaderFormatter exercises the WithHeaderFormatter Option to
// ensure it applies the option as expected under various circumstances.
func TestOptions_WithHeaderFormatter(t *testing.T) {
tests := map[string]struct {
Value HeaderFormatter
ExpectedValue HeaderFormatter
ShouldLeaveUninitialized bool
}{
"nil": {
Value: nil,
ExpectedValue: nil,
},
"unassigned-interface": {
ShouldLeaveUninitialized: true,
},
"happy-path": {
Value: &testHeaderFormatter{},
ExpectedValue: &testHeaderFormatter{},
},
}

for name, tc := range tests {
name := name
tc := tc
t.Run(name, func(t *testing.T) {
t.Parallel()
opts := &options{}
var f HeaderFormatter
if !tc.ShouldLeaveUninitialized {
f = tc.Value
}
applyOption := WithHeaderFormatter(f)
err := applyOption(opts)
require.NoError(t, err)
require.Equal(t, tc.ExpectedValue, opts.withHeaderFormatter)
})
}
}

// TestOptions_Default exercises getDefaultOptions to assert the default values.
func TestOptions_Default(t *testing.T) {
opts := getDefaultOptions()
Expand Down Expand Up @@ -485,3 +525,12 @@ func TestOptions_Opts(t *testing.T) {
})
}
}

// testHeaderFormatter is a stub to prevent the need to import the vault package
// to bring in vault.AuditedHeadersConfig for testing.
type testHeaderFormatter struct{}

// ApplyConfig satisfied the HeaderFormatter interface for testing.
func (f *testHeaderFormatter) ApplyConfig(ctx context.Context, headers map[string][]string, salter Salter) (result map[string][]string, retErr error) {
return nil, nil
}
3 changes: 3 additions & 0 deletions changelog/22694.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
audit: Prevent panic due to nil pointer receiver for audit header formatting.
```

0 comments on commit d2d3d8a

Please sign in to comment.