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

DIsplayHtmlWithImages: added the dynamic section script #26945

Merged
merged 6 commits into from
May 29, 2023
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
6 changes: 6 additions & 0 deletions Packs/CommonScripts/ReleaseNotes/1_11_81.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

#### Scripts

##### New: DisplayHTMLWithImages

- Script for dynamic-section to display HTML with embedded images. (Available from Cortex XSOAR 6.5.0).
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import re
import demistomock as demisto # noqa: F401
from CommonServerPython import * # noqa: F401


def create_html_with_images(email_html='', entry_id_list=None):
if not entry_id_list:
return email_html

account_name = get_tenant_account_name()

for entry_id in entry_id_list:
# Handling inline attachments from Gmail mailboxes
if re.search(f'src="[^>]+"(?=[^>]+alt="{entry_id[0]}")', email_html):
email_html = re.sub(f'src="[^>]+"(?=[^>]+alt="{entry_id[0]}")',
f'src={account_name}/entry/download/{entry_id[1]}',
email_html
)
# Handling inline attachments from Outlook mailboxes
# Note: when tested, entry id list and inline attachments were in the same order, so there was no need in
# special validation that the right src was being replaced.
else:
email_html = re.sub('(src="cid(.*?"))',
f'src={account_name}/entry/download/{entry_id[1]}',
email_html, count=1,
)
return email_html


def get_entry_id_list(attachments, files):
"""Get the email attachments and create entry id list.
Args:
attachments (list): The attachments of the email.
files (list): The uploaded files in the context.
Returns:
list. Attachments entries ids list.
"""
if not (attachments and files):
return []

entry_id_list = []
files = [files] if not isinstance(files, list) else files
for attachment in attachments:
attachment_name = attachment.get('name', '')
for file in files:
if attachment_name == file.get('Name'):
entry_id_list.append((attachment_name, file.get('EntryID')))
demisto.info(f'\n\n idlist \n\n{entry_id_list}')
return entry_id_list


def main(args):
incident = demisto.incident()
custom_fields = incident.get('CustomFields', {})
html_body = custom_fields.get('renderedhtml', '') or \
custom_fields.get('emailhtml', '') or \
custom_fields.get('emailbody', '')
attachments = incident.get('attachment', {})
files = demisto.context().get('File', [])

if 'src="cid' in html_body:
entry_id_list = get_entry_id_list(attachments, files)
html_body = create_html_with_images(html_body, entry_id_list)

return_results({
'ContentsFormat': formats['html'],
'Type': entryTypes['note'],
'Contents': html_body,
})


if __name__ in ('__builtin__', 'builtins', '__main__'):
main(demisto.args())
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
commonfields:
id: DisplayHTMLWithImages
version: -1
name: DisplayHTMLWithImages
script: ''
type: python
subtype: python3
comment: Display HTML with embedded images.
tags:
- dynamic-section
system: true
scripttarget: 0
runonce: false
fromversion: 6.5.0
dockerimage: demisto/python3:3.10.11.61265
tests:
- No tests (auto formatted)
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
import demistomock as demisto
import pytest

EMAIL_HTML = """
<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body><div dir="ltr">image 1:
<div><div><img src="cid:ii_kgjzy6yh0" alt="image_1.png" width="275" height="184"><br></div></div><div>image 2:
</div><div><div><img src="cid:ii_kgjzygxz1" alt="image_2.png" width="225" height="224"><br></div></div></div><br>
<div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, Oct 22, 2020 at 1:56 AM Some Person &lt;
<a href="mailto:[email protected]">[email protected]</a>&gt; wrote:<br></div>
<blockquote class="gmail_quote" style="margin: 0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204)"><u></u><div>
<p>please add multiple inline images</p></div></blockquote></div></body></html>"""

EMAIL_HTML_NO_ALT = """
<html><head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8"><style type="text/css" style="display:none">

<!–

p

{margin-top:0;

margin-bottom:0}

–>

</style></head>
<body dir="ltr"><div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<img size="178792" data-outlook-trace="F:1|T:1" src="cid:89593b98-b18d-46aa-ba4f-26773138c3f7" style="max-width:100%">
</div><div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<img size="8023" data-outlook-trace="F:1|T:1" src="cid:6a65eb70-7748-4bba-aaac-fe93235f63bd" style="max-width:100%">
</div></body></html>
"""

EXPECTED_RESULT_1 = """
<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body><div dir="ltr">image 1:
<div><div><img src=/entry/download/37@119 alt="image_1.png" width="275" height="184"><br></div></div><div>image 2:
</div><div><div><img src=/entry/download/38@120 alt="image_2.png" width="225" height="224"><br></div></div></div><br>
<div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, Oct 22, 2020 at 1:56 AM Some Person &lt;
<a href="mailto:[email protected]">[email protected]</a>&gt; wrote:<br></div>
<blockquote class="gmail_quote" style="margin: 0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204)"><u></u><div>
<p>please add multiple inline images</p></div></blockquote></div></body></html>"""

EXPECTED_RESULT_2 = """
<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body><div dir="ltr">image 1:
<div><div><img src=acc_test_tenant/entry/download/37@119 alt="image_1.png" width="275" height="184"><br></div></div>\
<div>image 2:
</div><div><div><img src=acc_test_tenant/entry/download/38@120 alt="image_2.png" width="225" height="224"><br></div>\
</div></div><br>
<div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, Oct 22, 2020 at 1:56 AM Some Person &lt;
<a href="mailto:[email protected]">[email protected]</a>&gt; wrote:<br></div>
<blockquote class="gmail_quote" style="margin: 0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204)"><u></u><div>
<p>please add multiple inline images</p></div></blockquote></div></body></html>"""

EXPECTED_RESULT_NO_ALT = """
<html><head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8"><style type="text/css" style="display:none">

<!–

p

{margin-top:0;

margin-bottom:0}

–>

</style></head>
<body dir="ltr"><div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<img size="178792" data-outlook-trace="F:1|T:1" src=acc_test_tenant/entry/download/37@119 style="max-width:100%">
</div><div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<img size="8023" data-outlook-trace="F:1|T:1" src=acc_test_tenant/entry/download/38@120 style="max-width:100%">
</div></body></html>
"""


@pytest.mark.parametrize(
"email_html,expected",
[
(EMAIL_HTML, EXPECTED_RESULT_2),
(EMAIL_HTML_NO_ALT, EXPECTED_RESULT_NO_ALT)
]
)
def test_main_mt(mocker, email_html, expected):
"""
Given
- Html contained images src
When
- All images were uploaded to the server
Then
- The images' src attribute would be replaced as expected with account tenant name
"""
import DisplayHTMLWithImages
from DisplayHTMLWithImages import main

mocked_incident = {
'CustomFields': {
'emailbody': email_html
},
'attachment': [
{'name': 'image_1.png'},
{'name': 'image_2.png'}
]
}
mocked_files = [
{'Name': 'image_1.png', 'EntryID': '37@119'},
{'Name': 'image_2.png', 'EntryID': '38@120'}
]

mocker.patch.object(demisto, 'demistoUrls', return_value={'server': 'https://localhost:8443:/acc_test_tenant'})
mocker.patch.object(demisto, 'incident', return_value=mocked_incident)
mocker.patch.object(demisto, 'context', return_value={'File': mocked_files})
mocker.patch.object(DisplayHTMLWithImages, 'return_results')

main({})

assert expected in DisplayHTMLWithImages.return_results.call_args[0][0]['Contents']
17 changes: 17 additions & 0 deletions Packs/CommonScripts/Scripts/DisplayHTMLWithImages/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Display HTML with embedded images.

## Script Data
---

| **Name** | **Description** |
| --- | --- |
| Script Type | python3 |
| Tags | dynamic-section |

## Inputs
---
There are no inputs for this script.

## Outputs
---
There are no outputs for this script.
2 changes: 1 addition & 1 deletion Packs/CommonScripts/pack_metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "Common Scripts",
"description": "Frequently used scripts pack.",
"support": "xsoar",
"currentVersion": "1.11.80",
"currentVersion": "1.11.81",
"author": "Cortex XSOAR",
"url": "https://www.paloaltonetworks.com/cortex",
"email": "",
Expand Down