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

pyas2lib.Message.build should force binary encoding for the encrypted payload. #20

Closed
adiroiban opened this issue Aug 14, 2020 · 4 comments

Comments

@adiroiban
Copy link
Contributor

If I am sending an image (.JPG) with Django-AS2 as unsigned encrypted file pyas2lib will always transfer it as text file.. that is replace unix newline with network newline.

I am not sure where this should be fixed.


I would say that pyas2lib.utils.BinaryBytesGenerator should never try to replace unix newlines.

Right now, the implementation will handle binary only if Content-Transfer-Encoding is binary and only for a certain subtype.


build method on the Message has the content_type argument, which can be used to force the payload but it has no way to set the Content-Transfer-Encoding header on the payload.


My suggestion is to have something line this in utils:

class BinaryBytesGenerator(BytesGenerator):
    """Override the bytes generator to always produce binary data."""

    def _handle_text(self, msg):
        """
        Handle writing the binary messages to prevent default behaviour of
        newline replacements.
        """
        payload = msg.get_payload(decode=True)
        if not payload:
            return

        self._fp.write(payload)


    _writeBody = _handle_text
@adiroiban
Copy link
Contributor Author

it looks like Django AS2 has no support for binary file types... so my previous suggestion is wrong.

I ended up with this fix.

Add application/octet-stream as an available content type for a partner in Django AS2.

Then in as2lib utils

class BinaryBytesGenerator(BytesGenerator):
    """Override the bytes generator to better handle binary data."""

    def _handle_text(self, msg):
        """
        Handle writing the binary messages to prevent default behaviour of
        newline replacements.
        """
        if msg.get(
            "Content-Transfer-Encoding"
        ) == "binary" and msg.get_content_subtype() in [
            "pkcs7-mime",
            "pkcs7-signature",
            'octet-stream',
        ]:
            payload = msg.get_payload(decode=True)
            if payload is None:
                return
            else:
                self._fp.write(payload)
        else:
            super()._handle_text(msg)

    _writeBody = _handle_text

and in as2lib as2 Message.build I have

        # Read the input and convert to bytes if value is unicode/str
        # using utf-8 encoding and finally Canonicalize the payload
        self.payload = email_message.Message()
        self.payload.set_payload(data)
        self.payload.set_type(content_type)

        if content_type.lower().startswith('application/octet-stream'):
            self.payload['Content-Transfer-Encoding'] = 'binary'
        else:
            encoders.encode_7or8bit(self.payload)

@abhishek-ram
Copy link
Owner

Makes sense @adiroiban can you open a PR with these changes?

@adiroiban
Copy link
Contributor Author

I will look into this. It looks like master branch already has BinaryBytesGenerator with support for application/octet-stream.

For my tests, I was using the released version.

I am now trying to understand the code and write automated tests. Will create a PR soon.

@adiroiban
Copy link
Contributor Author

adiroiban commented Aug 15, 2020

PR at #21 we can move the discussion there. Will open a separate PR for Django

abhishek-ram/django-pyas2#49

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants