Skip to content
Snippets Groups Projects
Commit 06d06b3a authored by Francé Wilke's avatar Francé Wilke
Browse files

Merge branch 'audit-log-prevent-subkey-match' into 'main'

Prevent false positives when dealing with heavily nested objects

See merge request uafrica/go-utils!26
parents 93bf3aa2 83f5be4c
No related branches found
No related tags found
1 merge request!26Prevent false positives when dealing with heavily nested objects
...@@ -198,6 +198,17 @@ func GetAllChanges(original interface{}, new interface{}) (map[string]interface{ ...@@ -198,6 +198,17 @@ func GetAllChanges(original interface{}, new interface{}) (map[string]interface{
if index := strings.Index(changesJson, split); index > -1 && changesJson != "{" { if index := strings.Index(changesJson, split); index > -1 && changesJson != "{" {
// Prevent reverse traversal // Prevent reverse traversal
if index > lastOccurrence { if index > lastOccurrence {
// Prevent finding nested keys as opposed to our "change" keys
opener := changesJson[index+len(split)+2 : index+len(split)+3]
// Prevent a "base level" key from matching a nested key
baseLevel := false
if index >= 1 {
containedBy := changesJson[index-1 : index]
if levelsDeep == 0 && (containedBy == "{" || containedBy == "[") {
baseLevel = true
}
}
if (opener == "{" || opener == "[") && !baseLevel {
lastOccurrence = index lastOccurrence = index
lastMatched = split lastMatched = split
levelsDeep = sIndex levelsDeep = sIndex
...@@ -205,6 +216,7 @@ func GetAllChanges(original interface{}, new interface{}) (map[string]interface{ ...@@ -205,6 +216,7 @@ func GetAllChanges(original interface{}, new interface{}) (map[string]interface{
} }
} }
} }
}
// If the "key" is already present, handle it differently // If the "key" is already present, handle it differently
if lastOccurrence > 0 { if lastOccurrence > 0 {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment