-
Notifications
You must be signed in to change notification settings - Fork 8.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[SECURITY_SOLUTION] delete advanced Policy fields when they are empty #84368
Changes from all commits
4307a9f
d56f0e3
e982777
9ac26da
ee92d62
b620c6e
a488234
c746fc8
fd115dd
58ee34b
1b3d631
c601ea8
b943055
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,13 +15,45 @@ import { AdvancedPolicySchema } from '../models/advanced_policy_schema'; | |
|
||
function setValue(obj: Record<string, unknown>, value: string, path: string[]) { | ||
let newPolicyConfig = obj; | ||
|
||
// First set the value. | ||
for (let i = 0; i < path.length - 1; i++) { | ||
if (!newPolicyConfig[path[i]]) { | ||
newPolicyConfig[path[i]] = {} as Record<string, unknown>; | ||
} | ||
newPolicyConfig = newPolicyConfig[path[i]] as Record<string, unknown>; | ||
} | ||
newPolicyConfig[path[path.length - 1]] = value; | ||
|
||
// Then, if the user is deleting the value, we need to ensure we clean up the config. | ||
// We delete any sections that are empty, whether that be an empty string, empty object, or undefined. | ||
if (value === '' || value === undefined) { | ||
newPolicyConfig = obj; | ||
for (let k = path.length; k >= 0; k--) { | ||
const nextPath = path.slice(0, k); | ||
for (let i = 0; i < nextPath.length - 1; i++) { | ||
// Traverse and find the next section | ||
newPolicyConfig = newPolicyConfig[nextPath[i]] as Record<string, unknown>; | ||
} | ||
if ( | ||
newPolicyConfig[nextPath[nextPath.length - 1]] === undefined || | ||
newPolicyConfig[nextPath[nextPath.length - 1]] === '' || | ||
Object.keys(newPolicyConfig[nextPath[nextPath.length - 1]] as object).length === 0 | ||
) { | ||
// If we're looking at the `advanced` field, we leave it undefined as opposed to deleting it. | ||
// This is because the UI looks for this field to begin rendering. | ||
if (nextPath[nextPath.length - 1] === 'advanced') { | ||
newPolicyConfig[nextPath[nextPath.length - 1]] = undefined; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If I'm reading this right, it seems that if the key name is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the UI looks for an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i'm also a little confused about the if/else logic here; maybe more comments would be clarifying There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added more comments |
||
// In all other cases, if field is empty, we'll delete it to clean up. | ||
} else { | ||
delete newPolicyConfig[nextPath[nextPath.length - 1]]; | ||
} | ||
newPolicyConfig = obj; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see this assignment multiple times, what does it do? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it updates the reference to the area in the config as we follow the given path. Say our path is |
||
} else { | ||
break; // We are looking at a non-empty section, so we can terminate. | ||
} | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can this be covered by testing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, I can write a test case for it There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. pushed up a test |
||
} | ||
|
||
function getValue(obj: Record<string, unknown>, path: string[]) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm assuming this casting here is safe at runtime? will it always be an object if defined at this point?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
correct
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
'as object' ? Shouldn't it be 'as Object' ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think both work. The type error I had on this went away after I added the cast