Skip to content

Commit

Permalink
Do not set values of unexported fields (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomerShor authored Nov 21, 2022
1 parent ef59055 commit bb8a9a4
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 10 deletions.
25 changes: 15 additions & 10 deletions gosecretive.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ var DefaultOnValueFuncHandler = func(fieldPath string, valueToScrub interface{})
}

// Scrub will scrub the given object. if not scrubFuncHandler is given, default to DefaultOnValueFuncHandler.
// returns the scrubbed object and a map of the scrubbed data
// returns the scrubbed object and a map of the scrubbed data.
// if the object contains unexported fields, they will be ignored and won't be in the scrubbed object.
// e.g.:
// original := map[string]interface{}{
// "field": "value",
Expand Down Expand Up @@ -147,18 +148,22 @@ func travel(fieldPath string,

// value is a string. call the callback
case reflect.String:
newContent := callback(fieldPath, original.Interface())

// callback returns a new value to set (override)
if newContent != nil && original.String() != *newContent {
secrets[*newContent] = original.String()
travelValue.SetString(*newContent)
} else {
travelValue.SetString(original.String())
if travelValue.CanSet() {
newContent := callback(fieldPath, original.Interface())

// callback returns a new value to set (override)
if newContent != nil && original.String() != *newContent {
secrets[*newContent] = original.String()
travelValue.SetString(*newContent)
} else {
travelValue.SetString(original.String())
}
}

default:
travelValue.Set(original)
if travelValue.CanSet() {
travelValue.Set(original)
}
}

}
22 changes: 22 additions & 0 deletions gosecretive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,28 @@ func TestRoundTrip(t *testing.T) {
}
}

func TestSkipUnxportedFields(t *testing.T) {
t.Run("SkipUnexportedFields", func(t *testing.T) {
type structWithUnexported struct {
Exported string
unexported string
}
s := structWithUnexported{
Exported: "exported",
unexported: "unexported",
}
scrubbedObject, _ := Scrub(s, scrubbingFunction([]string{
"/unexported",
}))
if scrubbedObject.(structWithUnexported).unexported == s.unexported || scrubbedObject.(structWithUnexported).unexported != "" {
t.Errorf("Unexported field wasn't skipped. scrubbedObject = %v, expectedRestoredObject %v", scrubbedObject, s)
}
if scrubbedObject.(structWithUnexported).Exported != s.Exported {
t.Errorf("Exported field should not be modified. scrubbedObject = %v, expectedRestoredObject %v", scrubbedObject, s)
}
})
}

func scrubbingFunction(allowedFieldPaths []string) OnValueFuncHandler {
return func(fieldPath string, valueToScrub interface{}) *string {
if stringInSlice(fieldPath, allowedFieldPaths) {
Expand Down

0 comments on commit bb8a9a4

Please sign in to comment.