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][Response Ops] Rule actions sending malformed JSON when using {{context.alerts}} action template #137114

Open
dplumlee opened this issue Jul 25, 2022 · 7 comments
Labels
bug Fixes for quality problems that affect the customer experience Team:ResponseOps Label for the ResponseOps team (formerly the Cases and Alerting teams) Team: SecuritySolution Security Solutions Team working on SIEM, Endpoint, Timeline, Resolver, etc. triage_needed

Comments

@dplumlee
Copy link
Contributor

Rule actions are failing to fire when using the {{context.alerts}} placeholder due to the JSON output being malformed when sent to the assigned webhook. This is causing 502 errors to come back from the webhook and not properly fire when the rule executes successfully. When looking at the malformed output, the JSON is often arbitrarily cut off and not escaped properly which is causing it to error out.

So far the action template we've used in testing has been

{\r\n   \"rule.name\" : \"{{rule.name}}\",\r\n   \"description\" : \"{{context.rule.description}}\",\r\n   \"rule.severity\" : \"{{context.rule.severity}}\", \r\n   \"alert.ID\" : \"{{alert.id}}\", \r\n   \"context.alerts\" : \"{{context.alerts}}\", \r\n   \"signal.tags\" : \"{{rule.tags}}\",\r\n   \"rule.id\" : \"{{rule.id}}\"\r\n}"

but we've also modified it with different forms of mustache syntax in the template and gotten similar, malformed results. This has also been tested on multiple different webhooks each with similar 502 outputs. The issue seems to consistently be occurring with the {{context.alerts}} placeholder, but it's possible the issue is not limited to that field alone.

In the SDH (internal link) this was originally discovered in, the same action template and rule action was working for a certain rule while failing for others, leading us to believe maybe it has something to do with the alerts themselves not meshing well with the mustache formatting or something to that effect.

We've triaged it to the formatAlertsForNotificationActions sending alerts to the alerting framework in this file as still working and sending valid JSON so presumably the bug is downstream from that.

@dplumlee dplumlee added bug Fixes for quality problems that affect the customer experience triage_needed Team:ResponseOps Label for the ResponseOps team (formerly the Cases and Alerting teams) Team: SecuritySolution Security Solutions Team working on SIEM, Endpoint, Timeline, Resolver, etc. labels Jul 25, 2022
@elasticmachine
Copy link
Contributor

Pinging @elastic/response-ops (Team:ResponseOps)

@elasticmachine
Copy link
Contributor

Pinging @elastic/security-solution (Team: SecuritySolution)

@pmuellr
Copy link
Member

pmuellr commented Jul 28, 2022

When looking at the malformed output

Could you provide some sample output here?

The issue seems to consistently be occurring with the {{context.alerts}} placeholder

What is the value in context.alerts? If it's an array, we got problems. We don't currently have a way of expanding out an array like this, as JSON, in the rendered output.

So far the action template we've used in testing has been {{elided}} but we've also modified it with different forms of mustache syntax in the template and gotten similar, malformed results.

I haven't seen anyone successfully render an array of JSON values as a valid JSON value in the output, but I'd be interested to see what you try.

The most relevant issue related to this is this one: #84217 - just had a quick thought on a low-cost way of adding better JSON support for arrays, and posted a comment there.

@akusei
Copy link

akusei commented Aug 5, 2022

I'm having this same issue and I've tried many different iterations of mustache templates to get the output to be valid json. The weird think here is that the email connector parses the mustache template properly and produces the expected result.

{
  "raw_json": [
  {{#context.alerts}}
    {{.}},
  {{/context.alerts}}
    {}
  ]
}

The above renders as "{\r\n \"json_raw\": [\r\n{<alertdoc1>},\r\n{<alertdoc2>},\r\n{}\r\n]\r\n"
What's interesting though is the email connector does not have this issue. It will happily take the above template and produce valid json. I've also tried these which produces the same results

{
  "raw_json": [{{context.alerts}}]
}
[
{{#context.alerts}}
  {
    "raw_json": {{.}}
  },
{{/contexst.alerts}}
  {}
]

This is really difficult to work with, especially since the elastic alerts can and for us most times will have multiple alerts per execution. A workaround I'm using for now is to render the json as follows and then parse the raw_json value on the remote end with a call to python's json.loads

{
  "raw_json": "[{{#context.alerts}}{{.}},{{/context.alerts}},{}]"
}

@jamesspi
Copy link

I thought I'd mention that some users ran into a similar issue when not explicitly setting the content-type header to JSON. There was a 7.17 update that changed the underling axios version, and it was defaulting to a content-type of form.

@aarju
Copy link

aarju commented Sep 7, 2022

We use the webhook connector to send our alerts to our SOAR system internally. I responded to this discuss post with how we do it. https://discuss.elastic.co/t/elastic-rule-connector-sends-a-string-instead-of-json-to-the-webhook/313833

We send the entire contents of the alert body as an ndjson with this action template:

{{#context.alerts}}
{{{.}}}
{{/context.alerts}}

@ersin-erdal
Copy link
Contributor

Hi,

This issue came up as an sdh issue as well.

Unfortunately there isn't built-in way in mustache to tackle this, but IMHO, we can do:

1- Create an issue to add an extra field (like {last:true}) to context.alerts items, so we can detect the last item in the list and use inverted sections feature of mustasche to skip rendering the last comma.
e.g.: {{context.alerts}} ... {{^last}}, {{/last}} {{/context.alerts}}

This has to be approved by the @elastic/security-solution as they are the owner of the rule type.

2- We can try to replace trailing commas by using a regex in the webhook connector type executor.
Sounds a bit clumsy to me though.

cc: @mikecote @XavierM

@Zacqary Zacqary moved this from Awaiting Triage to Needs Discussion in AppEx: ResponseOps - Rules & Alerts Management Feb 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Fixes for quality problems that affect the customer experience Team:ResponseOps Label for the ResponseOps team (formerly the Cases and Alerting teams) Team: SecuritySolution Security Solutions Team working on SIEM, Endpoint, Timeline, Resolver, etc. triage_needed
Projects
No open projects
Status: Needs Discussion
Development

No branches or pull requests

7 participants