It fails signing when looking message lines in python3

Bug #1523067 reported by pablo
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
dkimpy
Undecided
Unassigned

Bug Description

>>> import dkim
>>> dkim_header = dkim.sign('hola', 'asd', 'pablo.com', 'asd')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.4/site-packages/dkim/__init__.py", line 591, in sign
    d = DKIM(message,logger=logger)
  File "/usr/local/lib/python3.4/site-packages/dkim/__init__.py", line 294, in __init__
    self.set_message(message)
  File "/usr/local/lib/python3.4/site-packages/dkim/__init__.py", line 334, in set_message
    self.headers, self.body = rfc822_parse(message)
  File "/usr/local/lib/python3.4/site-packages/dkim/__init__.py", line 195, in rfc822_parse
    lines = re.split(b"\r?\n", message)
  File "/usr/local/lib/python3.4/re.py", line 196, in split
    return _compile(pattern, flags).split(string, maxsplit)
TypeError: can't use a bytes pattern on a string-like object
>>> lines = re.split(b"\r?\n", 'hola')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.4/re.py", line 196, in split
    return _compile(pattern, flags).split(string, maxsplit)
TypeError: can't use a bytes pattern on a string-like object

The problem seems that line:
lines = re.split(b"\r?\n", message)

while this doesn't fails:
>>> import re
>>> lines = re.split("\r?\n", 'hola')
>>> print(lines)

Revision history for this message
pablo (pablomoreno61) wrote :

I've already testes this:

dkim_header = dkim.sign(b'hola', 'asd', 'pablo.com', 'asd')

but it gives me the following error:

>>> dkim_header = dkim.sign(b'hola', 'asd', 'pablo.com', 'asd')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.4/site-packages/dkim/__init__.py", line 591, in sign
    d = DKIM(message,logger=logger)
  File "/usr/local/lib/python3.4/site-packages/dkim/__init__.py", line 294, in __init__
    self.set_message(message)
  File "/usr/local/lib/python3.4/site-packages/dkim/__init__.py", line 334, in set_message
    self.headers, self.body = rfc822_parse(message)
  File "/usr/local/lib/python3.4/site-packages/dkim/__init__.py", line 211, in rfc822_parse
    raise MessageFormatError("Unexpected characters in RFC822 header: %s" % lines[i])
dkim.MessageFormatError: Unexpected characters in RFC822 header: b'hola'

what's wrong in a simple string like 'hola'?

Revision history for this message
pablo (pablomoreno61) wrote :

I've finished fixing, just to encode all my parameters (message, selector, domain and key).

But I still cannot make it work, it says:

"The From header field MUST be signed"

But this is my e-mail header and FROM header is there:

MIME-Version: 1.0
Precedence: Bulk
Subject: new campaign 111
From: =?utf-8?q?campaigns?= <email address hidden>
Reply-To: <email address hidden>
Message-ID: <101920.eyJFIjogImVzIiwgIkQiOiAiNTY2MmJhMTkzYzJmNWQwYThiNDBmNmQwIiwgIlUiOiAxLCAiQyI6IDE0NDkzMTA3NDR9@consupermiso.com>
List-Id: =?utf-8?q?campaigns?=
List-Unsubscribe: <mailto:<email address hidden>?<email address hidden>/5662ba193c2f5d0a8b40f6d0/f3866da971bb7603408250b3fbd9f24cfb5a1e2eda08cca4b11fa978>,
 <http://dev.testingkdim.com/withdraw?c=112&u=1>
Sender: <email address hidden>
Content-Type: multipart/alternative; charset="utf-8"; boundary="===============2492613666488585062=="
Content-Transfer-Encoding: quoted-printable
To: <email address hidden>
Date: Sat, 05 Dec 2015 10:19:20 -0000

Revision history for this message
pablo (pablomoreno61) wrote :

Still blocked here, "The From header field MUST be signed" cannot be avoided. Any idea?

Revision history for this message
William Grant (wgrant) wrote :

What's the code you're using? Your first example was correctly rejected because 'hola' isn't a valid RFC822 message; it doesn't have any headers.

Revision history for this message
pablo (pablomoreno61) wrote :

I created another bug, but I guess its all the same stuff: https://answers.launchpad.net/dkimpy/+question/277080

I've replaced my __init__.py by http://bazaar.launchpad.net/~dkimpy-hackers/dkimpy/trunk/view/head:/dkim/__init__.py and it works perfectly doing something like this:

Now my message has all the headers and body

                dkim_signer = dkim.DKIM(msg.as_string().encode('utf-8'))

                dkim_identity = '@%s' % (dkim_parameters['domain'])

                dkim_header = dkim_signer.sign(
                    selector = dkim_parameters['selector'].encode('utf-8'),
                    domain = dkim_parameters['domain'].encode('utf-8'),
                    privkey = dkim_parameters['key'].encode('utf-8'),
                    identity = dkim_identity.encode('utf-8')
                )

Revision history for this message
Scott Kitterman (kitterman) wrote :

Using bzr, you can do:

bzr branch lp:dkimpy

in the directory where you want it installed and then run setup.py to install it. If that version is working for you, I can do a new release so you'll get that version from pypi?

Revision history for this message
pablo (pablomoreno61) wrote :

It would be nice!!

This is how I installed dkimpy:

> pip3 install dkimpy

Should I specify any version? or just get the master would be enough?

many thanks!

Revision history for this message
Scott Kitterman (kitterman) wrote : Re: [Bug 1523067] Re: It fails signing when looking message lines in python3

Master would be enough.

Revision history for this message
Scott Kitterman (kitterman) wrote :

I've released version 0.5.5 which, as I understand it, should resolve this for you.

Changed in dkimpy:
status: New → Fix Released
Revision history for this message
pablo (pablomoreno61) wrote :

0.5.6 version downloaded with: pip3 install dkimpy --upgrade

[root@csp csp]# pip3 install dkimpy --upgrade
Collecting dkimpy
  Downloading dkimpy-0.5.6.tar.gz
Building wheels for collected packages: dkimpy
  Running setup.py bdist_wheel for dkimpy
  Stored in directory: /root/.cache/pip/wheels/80/06/ab/5f4968c640173a369942e200b98583e3289119733a739df936
Successfully built dkimpy
Installing collected packages: dkimpy
  Found existing installation: dkimpy 0.5.4
    Uninstalling dkimpy-0.5.4:
      Successfully uninstalled dkimpy-0.5.4
Successfully installed dkimpy-0.5.6

Revision history for this message
pablo (pablomoreno61) wrote :

it works like a charm.

Many thanks Scott!

To post a comment you must log in.
This report contains Public information  Edit
Everyone can see this information.

Other bug subscribers