Skip to content

Commit

Permalink
MailListenerV2: fix exception in attachemnet parsing (demisto#36075)
Browse files Browse the repository at this point in the history
* fix

* fix v2

* fix + RN

* ruff changes

* return the payload in case of error

* Update Packs/MailListener/ReleaseNotes/1_0_57.md

Co-authored-by: ShirleyDenkberg <62508050+ShirleyDenkberg@users.noreply.github.com>

* fix problem

* fix problem

* fix validation

* revert ruff change

* remove unused code + ignore ruff

---------

Co-authored-by: ShirleyDenkberg <62508050+ShirleyDenkberg@users.noreply.github.com>
  • Loading branch information
ilappe and ShirleyDenkberg authored Sep 9, 2024
1 parent 6e94027 commit f00da23
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 14 deletions.
3 changes: 3 additions & 0 deletions Packs/MailListener/.pack-ignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
[file:MailListenerV2.yml]
ignore=IN126

[file:README.md]
ignore=RM106
21 changes: 9 additions & 12 deletions Packs/MailListener/Integrations/MailListenerV2/MailListenerV2.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def __init__(self, message_bytes: bytes, include_raw_body: bool, save_file: bool
self.headers = email_object.headers
self.raw_body = email_object.body if include_raw_body else None
# According to the mailparser documentation the datetime object is in utc
self.date = email_object.date.replace(tzinfo=timezone.utc) if email_object.date else None
self.date = email_object.date.replace(tzinfo=timezone.utc) if email_object.date else None # noqa: UP017
self.raw_json = self.generate_raw_json()
self.save_eml_file = save_file
self.labels = self._generate_labels()
Expand Down Expand Up @@ -162,7 +162,11 @@ def parse_attachments(self) -> list:
for attachment in self.attachments:
payload = attachment.get('payload')

file_data = base64.b64decode(payload) if attachment.get('binary') else payload
try:
file_data = base64.b64decode(payload) if attachment.get('binary') else payload
except Exception as e:
file_data = payload
demisto.error(f'parse_attachments: Failed to decode the attachment data - {str(e)}')

# save the attachment
file_result = fileResult(attachment.get('filename'), file_data, attachment.get('mail_content_type'))
Expand Down Expand Up @@ -192,13 +196,13 @@ def convert_to_incident(self) -> dict[str, Any]:
date = self.date
if not date:
demisto.info(f'Could not identify date for mail with ID {self.id}. Setting its date to be now.')
date = datetime.now(timezone.utc).isoformat()
date = datetime.now(timezone.utc).isoformat() # noqa: UP017
else:
date = self.date.isoformat()
return {
'labels': self._generate_labels(),
'occurred': date,
'created': datetime.now(timezone.utc).isoformat(),
'created': datetime.now(timezone.utc).isoformat(), # noqa: UP017
'details': self.text or self.html,
'name': self.subject,
'attachment': self.parse_attachments(),
Expand Down Expand Up @@ -520,7 +524,7 @@ def list_emails(client: IMAPClient,
permitted_from_domains=permitted_from_domains,
limit=_limit)
results = [{'Subject': email.subject,
'Date': email.date.isoformat() if email.date else datetime.now(timezone.utc).isoformat(),
'Date': email.date.isoformat() if email.date else datetime.now(timezone.utc).isoformat(), # noqa: UP017
'To': email.to,
'From': email.from_,
'ID': email.id} for email in mails_fetched]
Expand All @@ -544,13 +548,6 @@ def get_email_as_eml(client: IMAPClient, message_id: int) -> dict:
return mail_file[0] if mail_file else {}


def _convert_to_bytes(data) -> bytes:
demisto.debug("Converting data to bytes.")
bytes_data = bytes(data)
demisto.debug("Converted data successfully.")
return bytes_data


def replace_spaces_in_credentials(credentials: str | None) -> str | None:
"""
This function is used in case of credential from type: 9 is in the wrong format
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ script:
required: true
description: Fetches an email by message ID and returns the information in an eml file format.
name: mail-listener-get-email-as-eml
dockerimage: demisto/py3-tools:1.0.0.99035
dockerimage: demisto/py3-tools:1.0.0.108682
isfetch: true
runonce: false
script: '-'
Expand Down
7 changes: 7 additions & 0 deletions Packs/MailListener/ReleaseNotes/1_0_57.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

#### Integrations

##### Mail Listener v2

- Fixed an issue where the ***fetch-incidents*** command failed when ingesting some emails.
- Updated the Docker image to: *demisto/py3-tools:1.0.0.108682*.
2 changes: 1 addition & 1 deletion Packs/MailListener/pack_metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "Mail Listener",
"description": "Listen to a mailbox, enable incident triggering via e-mail",
"support": "xsoar",
"currentVersion": "1.0.56",
"currentVersion": "1.0.57",
"author": "Cortex XSOAR",
"url": "https://www.paloaltonetworks.com/cortex",
"email": "",
Expand Down

0 comments on commit f00da23

Please sign in to comment.