tanx / mailreader

RFC parser as an AMD module written with node API for the browser.
MIT License
9 stars 5 forks source link

Empty text nodes #13

Open toberndo opened 9 years ago

toberndo commented 9 years ago

A bit of an edge case, but parsing of a message with an empty body:

Content-Type: multipart/signed;
    boundary="Apple-Mail=_2FA6320C-6453-4AF8-B142-BB0B5A86BF5D";
    protocol="application/pgp-signature";
    micalg=pgp-sha512

--Apple-Mail=_2FA6320C-6453-4AF8-B142-BB0B5A86BF5D
Content-Transfer-Encoding: 7bit
Content-Type: text/plain

--Apple-Mail=_2FA6320C-6453-4AF8-B142-BB0B5A86BF5D
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
    filename=signature.asc
Content-Type: application/pgp-signature;
    name=signature.asc
Content-Description: Message signed with OpenPGP using GPGMail

-----BEGIN PGP SIGNATURE-----
Comment: GPGTools - https://gpgtools.org

iQIcBAEBCgAGBQJVwfvFAAoJEAf+2SAqAy1Ei5cQAKY2d9irduVuZOIN1SSJvxJG
f12BB5J1AMvwrN9UCsJxTydO3DlmLr2e9jMDULNOmfR7H+MHKxAGnKez9lXQ0uyA
n83086+bYNyBHD1DWq/BNdIsrp8kphEbqRjRPLzWmKeKL9KQnls5dC0+UKn4hwwe
5g7V4ko9Ps8P6qZGp2KSEhfJ2hJnJWhj1EPR5q/rAumFv/QQiki3DpKh/kTZx+ZH
ADqWhs/JxRJaKBQ0StVgZW/kpZ0Tp72fob7lw/mNfRttjZhiyjZn7xetaZNAViWa
ZEm96rDnawVO2TWY1M2GH4o+ZkloJeoc14AyXWaOR7eiTJuHYzHu0BsNQRJLS7kM
0UasJDYfnVt4TZyA42Qj2RlYFex4jPFUX8DZIzbEaYr2XpmrezbdQuGJTy9Cpdph
f0U6wUVy2bYlsOoFmJJKq/2G0RAQN1tQzs8rBCcgBZEc/xTBh43iA7g9We5mxC95
3bC9GBSBjfe1eInBJ+95Kv3t7TIgbyrKArhWAasrGrN6a8oKVDk3SY0KUTN4YT3+
r/LqLf4HOekJ3VmPW3W+2aLGInMAKJjXWng7WQ5iEnwe9Bjyf1IOg8pvbwlX9PVu
QClkjbD7rD3I4UC4VMtkPK3Xl18UZ91fisrbTqRU80wyhDWxaMWTL0urSAYWZp1h
kCqpmgzbyeeLeDfT7bAs
=nHSm
-----END PGP SIGNATURE-----

--Apple-Mail=_2FA6320C-6453-4AF8-B142-BB0B5A86BF5D--

leads to the exception: Failed to execute 'decode' on 'TextDecoder': The provided value is not of type '(ArrayBuffer or ArrayBufferView)'

felixhammerl commented 9 years ago

:+1: thx for pointing it out. sounds like this error bubbles up from the mimeparser. will be in berlin the next couple of days, so ... patches welcome ;)

toberndo commented 9 years ago

Not yet a patch, but I found the place (in mailreader-parser.js):

function matchText(node, bodyPart) {
        var disposition = node.headers['content-disposition'],
            isText = (/^text\/plain/i.test(node.contentType.value) && (!disposition || (disposition && disposition[0].value !== 'attachment')));

        if (!isText) {
            return false;
        }

        var content = new TextDecoder('utf-8').decode(node.content).replace(/([\r]?\n)*$/g, '');
        if (bodyPart.type === 'text') {
            // this mime node is the text node we gave to the mimeparser
            bodyPart.content = content;
        } else {
            // this mime node is part of a signed or encrypted node
            bodyPart.content.push({
                type: 'text',
                content: content
            });
        }

        return true;
    }

node.contentis an empty string and therefore new TextDecoder('utf-8').decode('') throws an exception.

andris9 commented 9 years ago

I think the content property is not an empty string but it is missing from the object entirely (as there were no body in the node) and thus undefined is used for decoding

toberndo commented 9 years ago

I see an empty string:

bildschirmfoto 2015-08-06 um 10 43 36