Skip to content

Commit

Permalink
Validate length of message id when user provided
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonjoyce committed Nov 24, 2023
1 parent 97832d6 commit ef49efe
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
14 changes: 10 additions & 4 deletions pyas2lib/as2.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,8 +357,8 @@ def build(
(default "[email protected]")
:param message_id:
The message id to be used for the message. If not provided a
unique message id is generated. (default None)
A value to be used for the left side of the message id. If not provided a
unique id is generated. (default None)
"""

# Validations
Expand All @@ -378,13 +378,19 @@ def build(
)

if message_id:
self.message_id = message_id
self.message_id = f"{message_id}@{self.sender.domain}"
else:
# Generate message id using UUID 1 as it uses both hostname and time
self.message_id = (
email_utils.make_msgid(domain=self.sender.domain).lstrip("<").rstrip(">")
)

# ensure the total length of the message id is no more than 255 characters
if len(self.message_id) > 255:
raise ValueError(
f"Message ID must be no more than 255 characters for compatibility with some AS2 servers. "
f"Current message ID length is {len(self.message_id)}."
)

# Set up the message headers
as2_headers = {
"AS2-Version": AS2_VERSION,
Expand Down
15 changes: 14 additions & 1 deletion pyas2lib/tests/test_basic.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Module for testing the basic features of pyas2."""
import pytest
import socket
from pyas2lib import as2
from . import Pyas2TestCase
Expand Down Expand Up @@ -204,9 +205,21 @@ def test_plain_message_with_custom_message_id(self):
"""Test Message building with a custom message id"""

# Build an As2 message to be transmitted to partner
self.org.domain = "example.com"
out_message = as2.Message(self.org, self.partner)
out_message.build(self.test_data, message_id="some_custom_id")
self.assertEqual(out_message.message_id, "some_custom_id")
self.assertEqual(out_message.message_id, "[email protected]")

def test_invalid_message_id_length_raises_error(self):
"""Test Message building with a custom message id that's invalid"""

# Build an As2 message to be transmitted to partner
self.org.domain = "example.com"
out_message = as2.Message(self.org, self.partner)
very_long_message_id = "a" * 1000
with pytest.raises(ValueError) as excinfo:
out_message.build(self.test_data, message_id=very_long_message_id)
assert "Message ID must be no more than 255 characters for compatibility" in str(excinfo.value)

def find_org(self, as2_id):
return self.org
Expand Down

0 comments on commit ef49efe

Please sign in to comment.