Skip to content
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] Fixes required_fields being removed after rule PATCH calls #199901

Merged
merged 5 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -453,4 +453,36 @@ describe('applyRulePatch', () => {
})
).rejects.toThrowError('new_terms_fields: Expected array, received string');
});

test('should retain existing required_fields when not present in rule patch body', async () => {
const rulePatch = {
name: 'new name',
} as PatchRuleRequestBody;
const existingRule = {
...getRulesSchemaMock(),
required_fields: [
{
name: 'event.action',
type: 'keyword',
ecs: true,
},
],
};
const patchedRule = await applyRulePatch({
rulePatch,
existingRule,
prebuiltRuleAssetClient,
});
expect(patchedRule).toEqual(
expect.objectContaining({
required_fields: [
{
name: 'event.action',
type: 'keyword',
ecs: true,
},
],
})
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ export const applyRulePatch = async ({
meta: rulePatch.meta ?? existingRule.meta,
max_signals: rulePatch.max_signals ?? existingRule.max_signals,
related_integrations: rulePatch.related_integrations ?? existingRule.related_integrations,
required_fields: addEcsToRequiredFields(rulePatch.required_fields),
required_fields: rulePatch.required_fields
? addEcsToRequiredFields(rulePatch.required_fields)
: existingRule.required_fields,
risk_score: rulePatch.risk_score ?? existingRule.risk_score,
risk_score_mapping: rulePatch.risk_score_mapping ?? existingRule.risk_score_mapping,
rule_name_override: rulePatch.rule_name_override ?? existingRule.rule_name_override,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,33 @@ export default ({ getService }: FtrProviderContext) => {
);
});
});

it('should not change required_fields when not present in patch body', async () => {
await securitySolutionApi.createRule({
body: getCustomQueryRuleParams({
rule_id: 'rule-1',
required_fields: [
{
name: 'event.action',
type: 'keyword',
},
],
}),
});

// patch a simple rule's name
const { body: patchedRule } = await securitySolutionApi
.patchRule({ body: { rule_id: 'rule-1', name: 'some other name' } })
.expect(200);

expect(patchedRule.required_fields).toEqual([
{
name: 'event.action',
type: 'keyword',
ecs: true,
},
]);
});
});
});
};