tag:blogger.com,1999:blog-59479581243499962712024-03-19T04:15:01.718-04:00QuetzalcoatalRandom stuff not at all related to misspelled Aztec godsJoshua Cranmerhttp://www.blogger.com/profile/02760318962075959780noreply@blogger.comBlogger145125tag:blogger.com,1999:blog-5947958124349996271.post-83281512792427540122017-08-22T00:59:00.000-04:002017-08-22T00:59:14.627-04:00A review of the solar eclipseOn Monday, I, along with several million other people, decided to view the <a href="https://en.wikipedia.org/wiki/Solar_eclipse_of_August_21,_2017" target="_blank">Great American Eclipse</a>. Since I presently live in Urbana, IL, that meant getting in my car and driving down I-57 towards Carbondale. This route is also what people from Chicago or Milwaukee would have taken, which means traffic was heavy. I ended up leaving around 5:45 AM, which puts me around the last clutch of people leaving.<br />
<br />
Our original destination was Goreville, IL (specifically, Ferne Clyffe State Park), but some people who arrived earlier got dissatisfied with the predicted cloudy forecast, so we moved the destination out to Cerulean, KY, which meant I ended up arriving around 11:00 AM, not much time before the partial eclipse started.<br />
<br />
Partial eclipses are neat, but they're very much a see-them-once affair. When the moon first entered the sun, you get a flurry of activity as everyone puts on the glasses, sees it, and then retreats back into the shade (it was 90°F, not at all comfortable in the sun). Then the temperature starts to drop—is that the eclipse, or this breeze that started up? As more and more gets covered, then it starts to dim: I had the impression that a cloud had just passed in front of the sun, and I wanted to turn and look at that non-existent cloud. And as the sun really gets covered, then trees start acting as pinhole cameras and the shadows take on a distinctive scalloped pattern.<br />
<br />
A total eclipse though? Completely different. The immediate reaction of everyone in the group was to start planning to see the 2024 eclipse. For those of us who spent 10, 15, 20 hours trying to see 2-3 minutes of glory, the sentiment was not only that it was time well spent, but that it was worth doing <i>again</i>. If you missed the 2017 eclipse and are able to see the 2024 eclipse, I urge you to do so. Words and pictures simply do not do it justice.<br />
<br />
What is the eclipse like? In the last seconds of partiality, everyone has their eyes, eclipse glasses on of course, staring at the sun. The thin crescent looks first like a side picture of an eyeball. As the time ticks by, the tendrils of orange slowly diminish until nothing can be seen—totality. Cries come out that it's safe to take the glasses off, but everyone is ripping them off anyways. Out come the camera phones, trying to capture that captivating image. That not-quite-perfect disk of black, floating in a sea of bright white wisps of the corona, not so much a circle as a stretched oval. For those who were quick enough, the <a href="https://en.wikipedia.org/wiki/Baily%27s_beads" target="_blank">Baily's beads</a> can be seen. The photos, of course, are crap: the corona is still bright enough to blot out the dark disk of the moon.<br />
<br />
Then, our attention is drawn away from the sun. It's cold. It's suddenly cold; the last moment of totality makes a huge difference. Probably something like 20°F off the normal high in that moment? Of course, it's dark. Not midnight, all-you-see-are-stars dark; it's more like a dusk dark. But unlike normal dusk, you can see the fringes of daylight in all directions. You can see some stars (or maybe that's just Venus; astronomy is not my strong suit), and of course a few planes are in the sky. One of them is just a moving, blinking light in the distance; another (chasing the eclipse?) is clearly visible with its contrail. And the silence. You don't notice the usual cacophony of sounds most of the time, but when everyone shushes for a moment, you hear the deafening silence of insects, of birds, of everything.<br />
<br />
Naturally, we all point back to the total eclipse and stare at it for most of the short time. Everything else is just a distraction, after all. How long do we have? A minute. Still more time for staring. A running commentary on everything I've mentioned, all while that neck is craned skyward and away from the people you're talking to. When is it no longer safe to keep looking? Is it still safe—no orange in the eclipse glasses, should still be fine. How long do we need to look at the sun to damage our eyes? Have we done that already? Are the glasses themselves safe? As the moon moves off the sun, hold that stare until that last possible moment, catch the return of the Baily's beads. A bright spark of sun, the photosphere is made visible again, and then clamp the eyes shut as hard as possible while you fumble the glasses back on to confirm that orange is once again visible.<br />
<br />
Finally, the rush out of town. There's a reason why everyone leaves after totality is over. Partial eclipses really aren't worth seeing twice, and we just saw one not five minutes ago. It's just the same thing in reverse. (And it's nice to get back in the car before the temperature gets warm again; my dark grey car was quite cool to the touch despite sitting in the sun for 2½ hours). Forget trying to beat the traffic; you've got a 5-hour drive ahead of you anyways, and the traffic is going to keep pouring onto the roads over the next several hours anyways (10 hours later, as I write this, the traffic is still bad on the eclipse exit routes). If you want to avoid it, you have to plan your route away from it instead.<br />
<br />
I ended up using <a href="https://goo.gl/maps/2Rsxjk25Qe42" target="_blank">this route</a> to get back, taking 5 hours 41 minutes and 51 seconds including a refueling stop and a bathroom break. So I don't know how bad I-57 was (I did hear there was a crash on I-57 pretty much just before I got on the road, but I didn't know that at the time), although I did see that I-69 was completely stopped when I crossed it. There were small slowdowns on the major Illinois state roads every time there was a stop sign that could have been mitigated by sitting police cars at those intersections and effectively temporarily signalizing them, but other than that, my trip home was free-flowing at speed limit the entire route.<br />
<br />
Some things I've learned:<br />
<br />
<ul>
<li>It's useful to have a GPS that doesn't require cellphone coverage to figure out your route.</li>
<li>It's useful to have paper maps to help plan a trip that's taking you well off the beaten path.</li>
<li>It's even more useful to have paper maps of the states you're in when doing that.</li>
<li>The Ohio River is much prettier near Cairo, IL than it is near Wheeling, WV.</li>
<li>The Tennessee River dam is also pretty.</li>
<li>Driving directions need to make the "I'm trying to avoid anything that smells like a freeway because it's going to be completely packed and impassable" mode easier to access.</li>
<li>Passing a car by crossing the broken yellow median will never not be scary.</li>
<li>Being passed by a car crossing the broken yellow median is still scary.</li>
<li>Driving on obscure Kentucky state roads while you're playing music is oddly peaceful and relaxing.</li>
<li>The best test for road hypnosis is seeing how you can drive a long, straight, flat, featureless road. You have not seen a long, straight, flat, featureless road until you've driven something like an obscure Illinois county road where the "long, straight" bit means "20 miles without even a hint of a curve" and the "featureless" means "you don't even see a house, shed, barn, or grain elevator to break up corn and soy fields." Interstates break up the straight bit a lot, and state highways tend to have lots of houses and small settlements on them that break up endless farm fields.</li>
<li>Police direction may not permit you to make your intended route work.</li>
</ul>
<ul>
</ul>
Joshua Cranmerhttp://www.blogger.com/profile/12388402847938842081noreply@blogger.com91tag:blogger.com,1999:blog-5947958124349996271.post-34041067288222845992015-04-01T03:00:00.000-04:002015-04-01T03:00:13.831-04:00Breaking newsIt was brought to my attention recently by
<a href="http://web.archive.org/web/20150330184641/http://www.timecube.com/">reputable</a>
<a href="https://www.nsa.gov">sources</a>
that the <a href="https://blog.mozilla.org/thunderbird/2015/02/thunderbird-usage-continues-to-grow/">recent announcement</a> of increased usage in recent
years produced an internal firestorm within Mozilla. Key
figures raised alarm that some of the tech press had interpreted the blog post
as a sign that Thunderbird was not, in fact, dead. As a result, they asked
Thunderbird community members to make corrections to emphasize that Mozilla was
trying to kill Thunderbird.
</p><p>
The primary fear, it seems, is that knowledge that the largest open-source email
client was still receiving regular updates would impel its userbase to agitate
for increased funding and maintenance of the client to help forestall
<a href="https://developers.google.com/gmail/oauth_overview">potential</a>
<a href="http://googleonlinesecurity.blogspot.com/2014/04/new-security-measures-will-affect-older.html">threats</a>
to the open nature of email as well as to
<a href="http://jmap.io/spec.html">innovate</a> in the space of providing usable and
<a href="https://addons.mozilla.org/en-US/thunderbird/addon/enigmail/">private</a>
communication channels. Such funding, however, would
be an unaffordable luxury and would only distract Mozilla from its
<a href="https://www.mozilla.org/en-US/about/manifesto/">central goal</a> of
<a href="https://wiki.mozilla.org/DXR" title="world domination">building</a>
<a href="http://www.rust-lang.org/" title="world domination">developer</a>
<a href="https://developer.mozilla.org/en-US/docs/Tools" title="world domination">productivity</a>
<a href="http://www.vim.org" title="trolling, er, world domination">tooling</a>.
Persistent
rumors that Mozilla would be willing to fund Thunderbird were it renamed
<i>Firefox Email</i> were finally addressed with the comment, "such a renaming would
violate our current policy that <a href="https://www.mozilla.org/en-US/persona/">all</a> <a href="https://addons.mozilla.org/en-US/firefox/themes/">projects</a> be named <i>Persona</i>."
Joshua Cranmerhttp://www.blogger.com/profile/02760318962075959780noreply@blogger.com33tag:blogger.com,1999:blog-5947958124349996271.post-31076845519465416312015-01-12T23:38:00.000-05:002015-01-12T23:38:51.433-05:00Why email is hard, part 8: why email security failedThis post is part 8 of <a href="http://quetzalcoatal.blogspot.com/search/label/email-hard">an intermittent series</a> exploring the difficulties of writing an email client. <a href="http://quetzalcoatal.blogspot.com/2013/09/why-email-is-hard-part-1-architecture.html">Part 1</a> describes a brief history of the infrastructure. <a href="http://quetzalcoatal.blogspot.com/2013/10/why-email-is-hard-part-2.html">Part 2</a> discusses internationalization. <a href="http://quetzalcoatal.blogspot.com/2013/11/why-email-is-hard-part-3-mime.html">Part 3</a> discusses MIME. <a href="http://quetzalcoatal.blogspot.com/2013/12/why-email-is-hard-part-4-email-addresses.html">Part 4</a> discusses email addresses. <a href="http://quetzalcoatal.blogspot.com/2014/01/why-email-is-hard-part-5-mail-headers.html">Part 5</a> discusses the more general problem of email headers. <a href="http://quetzalcoatal.blogspot.com/2014/05/why-email-is-hard-part-6-todays-email.html">Part 6</a> discusses how email security works in practice. <a href="http://quetzalcoatal.blogspot.com/2014/08/why-email-is-hard-part-7-email-security.html">Part 7</a> discusses the problem of trust. This part discusses why email security has largely failed.
</p><p>
At the end of the last part in this series, I posed the question, "Which email security protocol is most popular?" The answer to the question is actually neither S/MIME nor PGP, but a third protocol, DKIM. I haven't brought up DKIM until now because DKIM doesn't try to secure email in the same vein as S/MIME or PGP, but I still consider it relevant to discussing email security.
</p><p>
Unquestionably, DKIM is the only security protocol for email that can be considered successful. There are perhaps 4 billion active email addresses [1]. Of these, about 1-2 billion use DKIM. In contrast, S/MIME can count a few million users, and PGP at best a few hundred thousand. No other security protocols have really caught on past these three. Why did DKIM succeed where the others fail?
</p><p>
DKIM's success stems from its relatively narrow focus. It is nothing more than a cryptographic signature of the message body and a smattering of headers, and is itself stuck in the DKIM-Signature header. It is meant to be applied to messages only on outgoing servers and read and processed at the recipient mail server—it completely bypasses clients. That it bypasses clients allows it to solve the problem of key discovery and key management very easily (public keys are stored in DNS, which is already a key part of mail delivery), and its role in spam filtering is strong motivation to get it implemented quickly (it is 7 years old as of this writing). It's also simple: this one paragraph description is basically all you need to know [2].
</p><p>
The failure of S/MIME and PGP to see large deployment is certainly a large topic of discussion on myriads of cryptography enthusiast mailing lists, which often like to partake in propositions of new end-to-end encryption of email paradigms, such as the recent <a href="https://darkmail.info/downloads/dark-internet-mail-environment-december-2014.pdf">DIME proposal</a>. Quite frankly, all of these solutions suffer broadly from at least the same 5 fundamental weaknesses, and I see it unlikely that a protocol will come about that can fix these weaknesses well enough to become successful.
</p><p>
The first weakness, and one I've harped about many times already, is UI. Most email security UI is abysmal and generally at best usable only by enthusiasts. At least some of this is endemic to security: while it mean seem obvious how to convey what an email signature or an encrypted email signifies, how do you convey the distinctions between sign-and-encrypt, encrypt-and-sign, or an S/MIME triple wrap? The Web of Trust model used by PGP (and many other proposals) is even worse, in that inherently requires users to do other actions out-of-band of email to work properly.
</p><p>
Trust is the second weakness. Consider that, for all intents and purposes, the email address is the unique identifier on the Internet. By extension, that implies that a lot of services are ultimately predicated on the notion that the ability to receive and respond to an email is a sufficient means to identify an individual. However, the entire purpose of secure email, or at least of end-to-end encryption, is subtly based on the fact that other people in fact have access to your mailbox, thus destroying the most natural ways to build trust models on the Internet. The quest for anonymity or privacy also renders untenable many other plausible ways to establish trust (e.g., phone verification or government-issued ID cards).
</p><p>
Key discovery is another weakness, although it's arguably the easiest one to solve. If you try to keep discovery independent of trust, the problem of key discovery is merely picking a protocol to publish and another one to find keys. Some of these already exist: PGP key servers, for example, or using DANE to publish S/MIME or PGP keys.
</p><p>
Key management, on the other hand, is a more troubling weakness. S/MIME, for example, basically works without issue if you have a certificate, but managing to get an S/MIME certificate is a daunting task (necessitated, in part, by its trust model—see how these issues all intertwine?). This is also where it's easy to say that webmail is an unsolvable problem, but on further reflection, I'm not sure I agree with that statement anymore. One solution is just storing the private key with the webmail provider (you're trusting them as an email client, after all), but it's also not impossible to imagine using phones or flash drives as keystores. Other key management factors are more difficult to solve: people who lose their private keys or key rollover create thorny issues. There is also the difficulty of managing user expectations: if I forget my password to most sites (even my email provider), I can usually get it reset somehow, but when a private key is lost, the user is totally and completely out of luck.
</p><p>
Of course, there is one glaring and almost completely insurmountable problem. Encrypted email fundamentally precludes certain features that we have come to take for granted. The lesser known is server-side search and filtration. While there exist some mechanisms to do search on encrypted text, those mechanisms rely on the fact that you can manipulate the text to change the message, destroying the integrity feature of secure email. They also tend to be fairly expensive. It's easy to just say "who needs server-side stuff?", but the contingent of people who do email on smartphones would not be happy to have to pay the transfer rates to download all the messages in their folder just to find one little email, nor the energy costs of doing it on the phone. And those who have really large folders—Fastmail has a design point of 1,000,000 in a single folder—would still prefer to not have to transfer all their mail even on desktops.
</p><p>
The more well-known feature that would disappear is spam filtration. Consider that 90% of all email is spam, and if you think your spam folder is too slim for that to be true, it's because your spam folder only contains messages that your email provider wasn't sure were spam. The loss of server-side spam filtering would dramatically increase the cost of spam (a 10% reduction in efficiency would <em>double</em> the amount of server storage, per my calculations), and client-side spam filtering is quite literally too slow [3] and too costly (remember smartphones? Imagine having your email take 10 times as much energy and bandwidth) to be a tenable option. And privacy or anonymity tends to be an invitation to abuse (cf. Tor and Wikipedia). Proposed solutions to the spam problem are so common that there is <a href="http://craphound.com/spamsolutions.txt">a checklist</a> containing most of the objections.
</p><p>
When you consider all of those weaknesses, it is easy to be pessimistic about the possibility of wide deployment of powerful email security solutions. The strongest future—all email is encrypted, including metadata—is probably impossible or at least woefully impractical. That said, if you weaken some of the assumptions (say, don't desire all or most traffic to be encrypted), then solutions seem possible if difficult.
</p><p>
This concludes my discussion of email security, at least until things change for the better. I don't have a topic for the next part in this series picked out (this part actually concludes the set I knew I wanted to discuss when I started), although OAuth and DMARC are two topics that have been bugging me enough recently to consider writing about. They also have the unfortunate side effect of being things likely to see changes in the near future, unlike most of the topics I've discussed so far. But rest assured that I will find more difficulties in the email infrastructure to write about before long!
</p><p>
[1] All of these numbers are crude estimates and are accurate to only an order of magnitude. To justify my choices: I assume 1 email address per Internet user (this overestimates the developing world and underestimates the developed world). The largest webmail providers have given numbers that claim to be 1 billion active accounts between them, and all of them use DKIM. S/MIME is guessed by assuming that any smartcard deployment supports S/MIME, and noting that the US Department of Defense and Estonia's digital ID project are both heavy users of such smartcards. PGP is estimated from the size of the strong set and old numbers on the reachable set from the core Web of Trust.<br>
[2] Ever since last April, it's become impossible to mention DKIM without referring to DMARC, as a result of Yahoo's controversial DMARC policy. A proper discussion of DMARC (and why what Yahoo did was controversial) requires explaining the mail transmission architecture and spam, however, so I'll defer that to a later post. It's also possible that changes in this space could happen within the next year.<br>
[3] <a href="https://moderncrypto.org/mail-archive/messaging/2014/000780.html">According to a former GMail spam employee</a>, if it takes you as long as three minutes to calculate reputation, the spammer wins.Joshua Cranmerhttp://www.blogger.com/profile/02760318962075959780noreply@blogger.com20tag:blogger.com,1999:blog-5947958124349996271.post-46377318017425162952015-01-10T12:55:00.001-05:002015-01-10T12:55:58.322-05:00A unified history for comm-centralSeveral years back, Ehsan and Jeff Muizelaar attempted to build a unified history of mozilla-central across the Mercurial era and the CVS era. Their result is now used in the <a href="https://github.com/mozilla/gecko-dev">gecko-dev</a> repository. While being distracted on yet another side project, I thought that I might want to do the same for comm-central. It turns out that building a unified history for comm-central makes mozilla-central look easy: mozilla-central merely had one import from CVS. In contrast, comm-central imported twice from CVS (the calendar code came later), four times from mozilla-central (once with converted history), and imported twice from Instantbird's repository (once with converted history). Three of those conversions also involved moving paths. But I've worked through all of those issues to provide <a href="http://hg.mozilla.org/users/Pidgeot18_gmail.com/unified-comm-central">a nice snapshot of the repository</a> [1]. And since I've been frustrated by failing to find good documentation on how this sort of process went for mozilla-central, I'll provide details on the process for comm-central.
</p><p>
The first step and probably the hardest is getting the CVS history in DVCS form (I use hg because I'm more comfortable it, but there's effectively no difference between hg, git, or bzr here). There is <a href="https://github.com/jrmuizel/mozilla-cvs-history">a git version of mozilla's CVS tree</a> available, but I've noticed after doing research that its last revision is about a month before the revision I need for Calendar's import. The documentation for how that repo was built is no longer on the web, although we eventually found a copy after I wrote this post on <a href="http://git.mozilla.org/?p=users/eakhgari@mozilla.com/mozilla-history-tools.git;a=blob;f=initial_conversion/README.md;h=f57d06b1706235406e15b218effba46e572e25ef;hb=HEAD">git.mozilla.org</a>. I tried doing another conversion using hg convert to get CVS tags, but that rudely blew up in my face. For now, I've filed <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=1119438">a bug</a> on getting an official, branchy-and-tag-filled version of this repository, while using the current lack of history as a base. Calendar people will have to suffer missing a month of history.
</p><p>
CVS is famously hard to convert to more modern repositories, and, as I've done my research, Mozilla's CVS looks like it uses those features which make it difficult. In particular, both the calendar CVS import and the comm-central initial CVS import used a CVS tag HG_COMM_INITIAL_IMPORT. That tagging was done, on only a small portion of the tree, twice, about two months apart. Fortunately, mailnews code was never touched on CVS trunk after the import (there appears to be one commit on calendar after the tagging), so it is probably possible to salvage a repository-wide consistent tag.
</p><p>
The start of my script for conversion looks like this:
</p><pre>
#!/bin/bash
<span class="keyword">set</span> -e
WORKDIR=/tmp
HGCVS=<span class="special">$WORKDIR</span>/mozilla-cvs-history
MC=/src/trunk/mozilla-central
CC=/src/trunk/comm-central
OUTPUT=<span class="special">$WORKDIR</span>/full-c-c
<span class="comment"># Bug 445146: m-c/editor/ui -> c-c/editor/ui</span>
MC_EDITOR_IMPORT=d8064eff0a17372c50014ee305271af8e577a204
<span class="comment"># Bug 669040: m-c/db/mork -> c-c/db/mork</span>
MC_MORK_IMPORT=f2a50910befcf29eaa1a29dc088a8a33e64a609a
<span class="comment"># Bug 1027241, bug 611752 m-c/security/manager/ssl/** -> c-c/mailnews/mime/src/*</span>
MC_SMIME_IMPORT=e74c19c18f01a5340e00ecfbc44c774c9a71d11d
<span class="comment"># Step 0: Grab the mozilla CVS history.</span>
<span class="keyword">if</span> [ ! -e <span class="special">$HGCVS</span> ]; <span class="keyword">then</span>
hg clone git+https://github.com/jrmuizel/mozilla-cvs-history.git <span class="special">$HGCVS</span>
<span class="keyword">fi</span>
</pre><p>
Since I don't want to include the changesets useless to comm-central history, I trimmed the history by using <kbd>hg convert</kbd> to eliminate changesets that don't change the necessary files. Most of the files are simple directory-wide changes, but S/MIME only moved a few files over, so it requires a more complex way to grab the file list. In addition, I also replaced the <tt>%</tt> in the usernames with <tt>@</tt> that they are used to appearing in hg. The relevant code is here:
</p><pre>
<span class="comment"># Step 1: Trim mozilla CVS history to include only the files we are ultimately
# interested in.</span>
cat ><span class="special">$WORKDIR</span>/convert-filemap.txt <span class="keyword"><<EOF</span>
<span class="string"># Revision e4f4569d451a
include directory/xpcom
include mail
include mailnews
include other-licenses/branding/thunderbird
include suite
# Revision 7c0bfdcda673
include calendar
include other-licenses/branding/sunbird
# Revision ee719a0502491fc663bda942dcfc52c0825938d3
include editor/ui
# Revision 52efa9789800829c6f0ee6a005f83ed45a250396
include db/mork/
include db/mdb/</span>
<span class="keyword">EOF</span>
<span class="comment"># Add the S/MIME import files</span>
hg -R <span class="special">$MC</span> log -r <span class="string">"children(<span class="special">$MC_SMIME_IMPORT</span>)"</span> \
--template <span class="string">"{file_dels % 'include {file}\n'}"</span> >><span class="special">$WORKDIR</span>/convert-filemap.txt
<span class="keyword">if</span> [ ! -e <span class="special">$WORKDIR</span>/convert-authormap.txt ]; <span class="keyword">then</span>
hg -R <span class="special">$HGCVS</span> log --template <span class="string">"{email(author)}={sub('%', '@', email(author))}\n"</span> \
| sort -u > <span class="special">$WORKDIR</span>/convert-authormap.txt
<span class="keyword">fi</span>
<span class="keyword">cd</span> <span class="special">$WORKDIR</span>
hg convert <span class="special">$HGCVS $OUTPUT</span> --filemap convert-filemap.txt -A convert-authormap.txt
</pre><p>
That last command provides us the subset of the CVS history that we need for unified history. Strictly speaking, I should be pulling a specific revision, but I happen to know that there's no need to (we're cloning the only head) in this case. At this point, we now need to pull in the mozilla-central changes before we pull in comm-central. Order is key; <kbd>hg convert</kbd> will only apply the graft points when converting the child changeset (which it does but once), and it needs the parents to exist before it can do that. We also need to ensure that the mozilla-central graft point is included before continuing, so we do that, and then pull mozilla-central:
</p><pre>
CC_CVS_BASE=$(hg log -R <span class="special">$HGCVS</span> -r <span class="string">'tip'</span> --template <span class="string">'{node}'</span>)
CC_CVS_BASE=$(grep <span class="special">$CC_CVS_BASE</span> <span class="special">$OUTPUT</span>/.hg/shamap | cut -d<span class="string">' '</span> -f2)
MC_CVS_BASE=$(hg log -R <span class="special">$HGCVS</span> -r <span class="string">'gitnode(215f52d06f4260fdcca797eebd78266524ea3d2c)'</span> --template <span class="string">'{node}'</span>)
MC_CVS_BASE=$(grep <span class="special">$MC_CVS_BASE</span> <span class="special">$OUTPUT</span>/.hg/shamap | cut -d<span class="string">' '</span> -f2)
<span class="comment"># Okay, now we need to build the map of revisions.</span>
cat ><span class="special">$WORKDIR</span>/convert-revmap.txt <span class="keyword"><<EOF</span>
<span class="string">e4f4569d451a5e0d12a6aa33ebd916f979dd8faa <span class="special">$CC_CVS_BASE</span> # Thunderbird / Suite
7c0bfdcda6731e77303f3c47b01736aaa93d5534 d4b728dc9da418f8d5601ed6735e9a00ac963c4e, <span class="special">$CC_CVS_BASE</span> # Calendar
9b2a99adc05e53cd4010de512f50118594756650 <span class="special">$MC_CVS_BASE</span> # Mozilla graft point
ee719a0502491fc663bda942dcfc52c0825938d3 78b3d6c649f71eff41fe3f486c6cc4f4b899fd35, <span class="special">$MC_EDITOR_IMPORT</span> # Editor
8cdfed92867f885fda98664395236b7829947a1d 4b5da7e5d0680c6617ec743109e6efc88ca413da, e4e612fcae9d0e5181a5543ed17f705a83a3de71 # Chat</span>
<span class="keyword">EOF</span>
<span class="comment"># Next, import mozilla-central revisions</span>
<span class="keyword">for</span> rev <span class="keyword">in</span> <span class="special">$MC_MORK_IMPORT $MC_EDITOR_IMPORT $MC_SMIME_IMPORT</span>; <span class="keyword">do</span>
hg convert <span class="special">$MC</span> <span class="special">$OUTPUT</span> -r <span class="special">$rev</span> --splicemap <span class="special">$WORKDIR</span>/convert-revmap.txt \
--filemap <span class="special">$WORKDIR</span>/convert-filemap.txt
<span class="keyword">done</span>
</pre><p>
Some notes about all of the revision ids in the script. The splicemap requires the full 40-character SHA ids; anything less and the thing complains. I also need to specify the parents of the revisions that deleted the code for the mozilla-central import, so if you go hunting for those revisions and are surprised that they don't remove the code in question, that's why.
</p><p>
I mentioned complications about the merges earlier. The Mork and S/MIME import codes here moved files, so that what was db/mdb in mozilla-central became db/mork. There's no support for causing the generated splice to record these as a move, so I have to manually construct those renamings:
</p><pre>
<span class="comment"># We need to execute a few hg move commands due to renamings.</span>
<span class="keyword">pushd</span> <span class="special">$OUTPUT</span>
hg update -r $(grep <span class="special">$MC_MORK_IMPORT</span> .hg/shamap | cut -d<span class="string">' '</span> -f2)
(hg -R <span class="special">$MC</span> log -r <span class="string">"children(<span class="special">$MC_MORK_IMPORT</span>)"</span> \
--template <span class="string">"{file_dels % 'hg mv {file} {sub(\"db/mdb\", \"db/mork\", file)}\n'}"</span>) | bash
hg commit -m <span class="string">'Pseudo-changeset to move Mork files'</span> -d <span class="string">'2011-08-06 17:25:21 +0200'</span>
MC_MORK_IMPORT=$(hg log -r tip --template <span class="string">'{node}'</span>)
hg update -r $(grep <span class="special">$MC_SMIME_IMPORT</span> .hg/shamap | cut -d<span class="string">' '</span> -f2)
(hg -R <span class="special">$MC</span> log -r <span class="string">"children(<span class="special">$MC_SMIME_IMPORT</span>)"</span> \
--template <span class="string">"{file_dels % 'hg mv {file} {sub(\"security/manager/ssl\", \"mailnews/mime\", file)}\n'}"</span>) | bash
hg commit -m <span class="string">'Pseudo-changeset to move S/MIME files'</span> -d <span class="string">'2014-06-15 20:51:51 -0700'</span>
MC_SMIME_IMPORT=$(hg log -r tip --template <span class="string">'{node}'</span>)
<span class="keyword">popd</span>
<span class="comment"># Echo the new move commands to the changeset conversion map.</span>
cat >><span class="special">$WORKDIR</span>/convert-revmap.txt <span class="keyword"><<EOF</span>
<span class="string">52efa9789800829c6f0ee6a005f83ed45a250396 abfd23d7c5042bc87502506c9f34c965fb9a09d1, <span class="special">$MC_MORK_IMPORT</span> # Mork
50f5b5fc3f53c680dba4f237856e530e2097adfd 97253b3cca68f1c287eb5729647ba6f9a5dab08a, <span class="special">$MC_SMIME_IMPORT</span> # S/MIME</span>
<span class="keyword">EOF</span>
</pre><p>
Now that we have all of the graft points defined, and all of the external code ready, we can pull comm-central and do the conversion. That's not quite it, though—when we graft the S/MIME history to the original mozilla-central history, we have a small segment of abandoned converted history. A call to <kbd>hg strip</kbd> removes that.
</p><pre>
<span class="comment"># Now, import comm-central revisions that we need</span>
hg convert <span class="special">$CC</span> <span class="special">$OUTPUT</span> --splicemap <span class="special">$WORKDIR</span>/convert-revmap.txt
hg strip 2f69e0a3a05a
</pre><p>
</p><p>
[1] I left out one of the graft points because I just didn't want to deal with it. I'll leave it as an exercise to the reader to figure out which one it was. Hint: it's the only one I didn't know about before I searched for the archive points [2].<br>
[2] Since I wasn't sure I knew all of the graft points, I decided to try to comb through all of the changesets to figure out who imported code. It turns out that <kbd>hg log -r 'adds("**")'</kbd> narrows it down nicely (1667 changesets to look at instead of 17547), and using the <kbd>{file_adds}</kbd> template helps winnow it down more easily.Joshua Cranmerhttp://www.blogger.com/profile/02760318962075959780noreply@blogger.com16tag:blogger.com,1999:blog-5947958124349996271.post-84679892079925852072014-08-05T23:39:00.000-04:002014-08-05T23:39:56.071-04:00Why email is hard, part 7: email security and trustThis post is part 7 of <a href="http://quetzalcoatal.blogspot.com/search/label/email-hard">an intermittent series</a> exploring the difficulties of writing an email client. <a href="http://quetzalcoatal.blogspot.com/2013/09/why-email-is-hard-part-1-architecture.html">Part 1</a> describes a brief history of the infrastructure. <a href="http://quetzalcoatal.blogspot.com/2013/10/why-email-is-hard-part-2.html">Part 2</a> discusses internationalization. <a href="http://quetzalcoatal.blogspot.com/2013/11/why-email-is-hard-part-3-mime.html">Part 3</a> discusses MIME. <a href="http://quetzalcoatal.blogspot.com/2013/12/why-email-is-hard-part-4-email-addresses.html">Part 4</a> discusses email addresses. <a href="http://quetzalcoatal.blogspot.com/2014/01/why-email-is-hard-part-5-mail-headers.html">Part 5</a> discusses the more general problem of email headers. <a href="http://quetzalcoatal.blogspot.com/2014/05/why-email-is-hard-part-6-todays-email.html">Part 6</a> discusses how email security works in practice. This part discusses the problem of trust.
</p><p>
At a technical level, S/MIME and PGP (or at least PGP/MIME) use cryptography essentially identically. Yet the two are treated as radically different models of email security because they diverge on the most important question of public key cryptography: how do you trust the identity of a public key? Trust is critical, as it is the only way to stop an active, man-in-the-middle (MITM) attack. MITM attacks are actually easier to pull off in email, since all email messages effectively have to pass through both the sender's and the recipients' email servers [1], allowing attackers to be able to pull off permanent, long-lasting MITM attacks [2].
</p><p>
S/MIME uses the same trust model that SSL uses, based on X.509 certificates and certificate authorities. X.509 certificates effectively work by providing a certificate that says who you are which is signed by another authority. In the original concept (as you might guess from the name "X.509"), the trusted authority was your telecom provider, and the certificates were furthermore intended to be a part of the global X.500 directory—a natural extension of the OSI internet model. The OSI model of the internet never gained traction, and the trusted telecom providers were replaced with trusted root CAs.
</p><p>
PGP, by contrast, uses a trust model that's generally known as the Web of Trust. Every user has a PGP key (containing their identity and their public key), and users can sign others' public keys. Trust generally flows from these signatures: if you trust a user, you know the keys that they sign are correct. The name "Web of Trust" comes from the vision that trust flows along the paths of signatures, building a tight web of trust.
</p><p>
And now for the controversial part of the post, the comparisons and critiques of these trust models. A disclaimer: I am not a security expert, although I am a programmer who revels in dreaming up arcane edge cases. I also don't use PGP at all, and use S/MIME to a very limited extent for some Mozilla work [3], although I did try a few abortive attempts to dogfood it in the past. I've attempted to replace personal experience with comprehensive research [4], but most existing critiques and comparisons of these two trust models are about 10-15 years old and predate several changes to CA certificate practices.
</p><p>
A basic tenet of development that I have found is that the average user is fairly ignorant. At the same time, a lot of the defense of trust models, both CAs and Web of Trust, tends to hinge on configurability. How many people, for example, know how to add or remove a CA root from Firefox, Windows, or Android? Even among the subgroup of Mozilla developers, I suspect the number of people who know how to do so are rather few. Or in the case of PGP, how many people know how to change the maximum path length? Or even understand the security implications of doing so?
</p><p>
Seen in the light of ignorant users, the Web of Trust is a UX <em>disaster</em>. Its entire security model is predicated on having users precisely specify how much they trust other people to trust others (ultimate, full, marginal, none, unknown) and also on having them continually do out-of-band verification procedures and publicly reporting those steps. In 1998, <a href="http://reports-archive.adm.cs.cmu.edu/anon/1998/CMU-CS-98-155.pdf">a seminal paper</a> on the usability of a GUI for PGP encryption came to the conclusion that the UI was effectively unusable for users, to the point that only a third of the users were able to send an encrypted email (and even then, only with significant help from the test administrators), and a quarter managed to publicly announce their private keys at some point, which is pretty much the worst thing you can do. They also noted that the complex trust UI was never used by participants, although the failure of many users to get that far makes generalization dangerous [5]. While newer versions of security UI have undoubtedly fixed many of the original issues found (in no small part due to the paper, one of the first to argue that usability is integral, not orthogonal, to security), I have yet to find an actual study on the usability of the trust model itself.
</p><p>
The Web of Trust has other faults. The notion of "marginal" trust it turns out is rather broken: if you marginally trust a user who has two keys who both signed another person's key, that's the same as fully trusting a user with one key who signed that key. There are several proposals for different trust formulas [6], but none of them have caught on in practice to my knowledge.
</p><p>
A hidden fault is associated with its manner of presentation: in sharp contrast to CAs, the Web of Trust <em>appears</em> to not delegate trust, but any practical widespread deployment needs to solve the problem of contacting people who have had no prior contact. Combined with the need to bootstrap new users, this implies that there needs to be some keys that have signed a lot of other keys that are essentially default-trusted—in other words, a CA, a fact sometimes lost on advocates of the Web of Trust.
</p><p>
That said, a valid point in favor of the Web of Trust is that it more easily allows people to distrust CAs if they wish to. While I'm skeptical of its utility to a broader audience, the ability to do so for is crucial for a not-insignificant portion of the population, and it's important enough to be explicitly called out.
</p><p>
X.509 certificates are most commonly discussed in the context of SSL/TLS connections, so I'll discuss them in that context as well, as the implications for S/MIME are mostly the same. Almost all criticism of this trust model essentially boils down to a single complaint: certificate authorities aren't trustworthy. A historical criticism is that the addition of CAs to the main root trust stores was ad-hoc. Since then, however, the main oligopoly of these root stores (Microsoft, Apple, Google, and Mozilla) have made <a href="http://www.chromium.org/Home/chromium-security/root-ca-policy">their</a> <a href="http://social.technet.microsoft.com/wiki/contents/articles/3281.introduction-to-the-microsoft-root-certificate-program.aspx">policies</a> <a href="http://www.apple.com/certificateauthority/ca_program.html">public</a> and <a href="http://www.mozilla.org/en-US/about/governance/policies/security-group/certs/policy/">clear</a> [7]. The introduction of the <a href="https://cabforum.org/">CA/Browser Forum</a> in 2005, with a collection of major CAs and the major browsers as members, and several [8] helps in articulating common policies. These policies, simplified immensely, boil down to:
</p><ol>
<li>You must verify information (depending on certificate type). This information must be relatively recent.</li>
<li>You must not use weak algorithms in your certificates (e.g., no MD5).</li>
<li>You must not make certificates that are valid for too long.</li>
<li>You must maintain revocation checking services.</li>
<li>You must have fairly stringent physical and digital security practices and intrusion detection mechanisms.</li>
<li>You must be [externally] audited every year that you follow the above rules.</li>
<li>If you screw up, we can kick you out.</li>
</ol><p>
I'm not going to claim that this is necessarily the best policy or even that any policy can feasibly stop intrusions from happening. But it's a policy, so CAs must abide by some set of rules.
</p><p>
Another CA criticism is the fear that they may be suborned by national government spy agencies. I find this claim underwhelming, considering that the number of certificates acquired by intrusions that were used in the wild is larger than the number of certificates acquired by national governments that were used in the wild: 1 and 0, respectively. Yet no one complains about the untrustworthiness of CAs due to their ability to be hacked by outsiders. Another attack is that CAs are controlled by profit-seeking corporations, which misses the point because the business of CAs is not selling certificates but selling their access to the root databases. As we will see shortly, jeopardizing that access is a great way for a CA to go out of business.
</p><p>
To understand issues involving CAs in greater detail, there are two CAs that are particularly useful to look at. The first is CACert. CACert is favored by many by its attempt to handle X.509 certificates in a Web of Trust model, so invariably every public discussion about CACert ends up devolving into an attack on other CAs for their perceived capture by national governments or corporate interests. Yet what many of the proponents for inclusion of CACert miss (or dismiss) is the fact that CACert actually failed the required audit, and it is unlikely to ever pass an audit. This shows a central failure of both CAs and Web of Trust: different people have different definitions of "trust," and in the case of CACert, some people are favoring a subjective definition (I trust their owners because they're not evil) when an objective definition fails (in this case, that the root signing key is securely kept).
</p><p>
The other CA of note here is DigiNotar. In July 2011, some hackers managed to acquire a few fraudulent certificates by hacking into DigiNotar's systems. By late August, people had become aware of these certificates being used in practice [9] to intercept communications, mostly in Iran. The use appears to have been caught after Chromium updates failed due to invalid certificate fingerprints. After it became clear that the fraudulent certificates were not limited to a single fake Google certificate, and that DigiNotar had failed to notify potentially affected companies of its breach, DigiNotar was swiftly removed from all of the trust databases. It ended up declaring bankruptcy within two weeks.
</p><p>
DigiNotar indicates several things. One, SSL MITM attacks are not theoretical (I have seen at least two or three security experts advising pre-DigiNotar that SSL MITM attacks are "theoretical" and therefore the wrong target for security mechanisms). Two, keeping the trust of browsers is necessary for commercial operation of CAs. Three, the notion that a CA is "too big to fail" is false: DigiNotar played an important role in the Dutch community as a major CA and the operator of Staat der Nederlanden. Yet when DigiNotar screwed up and lost its trust, it was swiftly kicked out despite this role. I suspect that even Verisign could be kicked out if it manages to screw up badly enough.
</p><p>
This isn't to say that the CA model isn't problematic. But the source of its problems is that delegating trust isn't a feasible model in the first place, a problem that it shares with the Web of Trust as well. Different notions of what "trust" actually means and the uncertainty that gets introduced as chains of trust get longer both make delegating trust weak to both social engineering and technical engineering attacks. There appears to be an increasing consensus that the best way forward is some variant of key pinning, much akin to how SSH works: once you know someone's public key, you complain if that public key appears to change, even if it appears to be "trusted." This does leave people open to attacks on first use, and the question of what to do when you need to legitimately re-key is not easy to solve.
</p><p>
In short, both CAs and the Web of Trust have issues. Whether or not you should prefer S/MIME or PGP ultimately comes down to the very conscious question of how you want to deal with trust—a question without a clear, obvious answer. If I appear to be painting CAs and S/MIME in a positive light and the Web of Trust and PGP in a negative one in this post, it is more because I am trying to focus on the positions less commonly taken to balance perspective on the internet. In my next post, I'll round out the discussion on email security by explaining why email security has seen poor uptake and answering the question as to which email security protocol is most popular. The answer may surprise you!
</p><p>
[1] Strictly speaking, you can bypass the sender's SMTP server. In practice, this is considered a hole in the SMTP system that email providers are trying to plug.<br>
[2] I've had 13 different connections to the internet in the same time as I've had my main email address, not counting all the public wifis that I have used. Whereas an attacker would find it extraordinarily difficult to intercept all of my SSH sessions for a MITM attack, intercepting all of my email sessions is clearly far easier if the attacker were my email provider.<br>
[3] Before you read too much into this personal choice of S/MIME over PGP, it's entirely motivated by a simple concern: S/MIME is built into Thunderbird; PGP is not. As someone who does a lot of Thunderbird development work that could easily break the Enigmail extension locally, needing to use an extension would be disruptive to workflow.<br>
[4] This is not to say that I don't heavily research many of my other posts, but I did go so far for this one as to actually start going through a lot of published journals in an attempt to find information.<br>
[5] It's questionable how well the usability of a trust model UI can be measured in a lab setting, since the observer effect is particularly strong for all metrics of trust.<br>
[6] The web of trust makes a nice graph, and graphs invite lots of interesting mathematical metrics. I've always been partial to eigenvectors of the graph, myself.<br>
[7] Mozilla's policy for addition to NSS is basically the standard policy adopted by all open-source Linux or BSD distributions, seeing as OpenSSL never attempted to produce a root database.<br>
[8] It looks to me that it's the browsers who are more in charge in this forum than the CAs.<br>
[9] To my knowledge, this is the first—and so far only—attempt to actively MITM an SSL connection.Joshua Cranmerhttp://www.blogger.com/profile/02760318962075959780noreply@blogger.com8tag:blogger.com,1999:blog-5947958124349996271.post-47440477753365803592014-05-26T15:30:00.001-04:002014-05-26T20:32:27.293-04:00Why email is hard, part 6: today's email securityThis post is part 6 of <a href="http://quetzalcoatal.blogspot.com/search/label/email-hard">an intermittent series</a> exploring the difficulties of writing an email client. <a href="http://quetzalcoatal.blogspot.com/2013/09/why-email-is-hard-part-1-architecture.html">Part 1</a> describes a brief history of the infrastructure. <a href="http://quetzalcoatal.blogspot.com/2013/10/why-email-is-hard-part-2.html">Part 2</a> discusses internationalization. <a href="http://quetzalcoatal.blogspot.com/2013/11/why-email-is-hard-part-3-mime.html">Part 3</a> discusses MIME. <a href="http://quetzalcoatal.blogspot.com/2013/12/why-email-is-hard-part-4-email-addresses.html">Part 4</a> discusses email addresses. <a href="http://quetzalcoatal.blogspot.com/2014/01/why-email-is-hard-part-5-mail-headers.html">Part 5</a> discusses the more general problem of email headers. This part discusses how email security works in practice.
</p><p>
Email security is a rather wide-ranging topic, and one that I've wanted to cover for some time, well before several recent events that have made it come up in the wider public knowledge. There is no way I can hope to cover it in a single post (I think it would outpace even the length of my internationalization discussion), and there are definitely parts for which I am underqualified, as I am by no means an expert in cryptography. Instead, I will be discussing this over the course of several posts of which this is but the first; to ease up on the amount of background explanation, I will assume passing familiarity with cryptographic concepts like public keys, hash functions, as well as knowing what SSL and SSH are (though not necessarily how they work). If you don't have that knowledge, <a href="http://en.wikipedia.org/wiki/Public-key_cryptography">ask Wikipedia</a>.
</p><p>
Before discussing how email security works, it is first necessary to ask what email security actually means. Unfortunately, the layman's interpretation is likely going to differ from the actual precise definition. Security is often treated by laymen as a boolean interpretation: something is either secure or insecure. The most prevalent model of security to people is SSL connections: these allow the establishment of a communication channel whose contents are secret to outside observers while also guaranteeing to the client the authenticity of the server. The server often then gets authenticity of the client via a more normal authentication scheme (i.e., the client sends a username and password). Thus there is, at the end, a channel that has both <em>secrecy</em> and <em>authenticity</em> [1]: channels with both of these are considered secure and channels without these are considered insecure [2].
</p><p>
In email, the situation becomes more difficult. Whereas an SSL connection is between a client and a server, the architecture of email is such that email providers must be considered as distinct entities from end users. In addition, messages can be sent from one person to multiple parties. Thus secure email is a more complex undertaking than just porting relevant details of SSL. There are two major cryptographic implementations of secure email [3]: <a href="http://tools.ietf.org/html/rfc5751">S/MIME</a> and <a href="tools.ietf.org/html/rfc4880">PGP</a>. In terms of implementation, they are basically the same [4], although PGP has an extra mode which wraps general ASCII (known as "ASCII-armor"), which I have been led to believe is less recommended these days. Since I know the S/MIME specifications better, I'll refer specifically to how S/MIME works.
</p><p>
S/MIME defines two main MIME types: <tt>multipart/signed</tt>, which contains the message text as a subpart followed by data indicating the cryptographic signature, and <tt>application/pkcs7-mime</tt>, which contains an encrypted MIME part. The important things to note about this delineation are that only the body data is encrypted [5], that it's theoretically possible to encrypt only part of a message's body, and that the signing and encryption constitute different steps. These factors combine to make for a potentially infuriating UI setup.
</p><p>
How does S/MIME tackle the challenges of encrypting email? First, rather than encrypting using recipients' public keys, the message is encrypted with a symmetric key. This symmetric key is then encrypted with each of the recipients' keys and then attached to the message. Second, by only signing or encrypting the body of the message, the transit headers are kept intact for the mail system to retain its ability to route, process, and deliver the message. The body is supposed to be prepared in the "safest" form before transit to avoid intermediate routers munging the contents. Finally, to actually ascertain what the recipients' public keys are, clients typically passively pull the information from signed emails. LDAP, unsurprisingly, contains <a href="http://tools.ietf.org/html/rfc4523">an entry for a user's public key certificate</a>, which could be useful in large enterprise deployments. There is also work ongoing right now to publish keys via <a href="http://tools.ietf.org/wg/dane/">DNS and DANE</a>.
</p><p>
I mentioned before that S/MIME's use can present some interesting UI design decisions. I ended up actually testing some common email clients on how they handled S/MIME messages: Thunderbird, Apple Mail, Outlook [6], and Evolution. In my attempts to create a surreptitious signed part to confuse the UI, Outlook decided that the message had no body at all, and Thunderbird decided to ignore all indication of the existence of said part. Apple Mail managed to claim the message was signed in one of these scenarios, and Evolution took the cake by always agreeing that the message was signed [7]. It didn't even bother questioning the signature if the certificate's identity disagreed with the easily-spoofable From address. I was actually surprised by how well people did in my tests—I expected far more confusion among clients, particularly since the will to maintain S/MIME has clearly been relatively low, judging by poor support for <a href="http://tools.ietf.org/html/rfc2633">"new"</a> features such as triple-wrapping or header protection.
</p><p>
Another fault of S/MIME's design is that it makes the mistaken belief that composing a signing step and an encryption step is equivalent in strength to a simultaneous sign-and-encrypt. <a href="http://world.std.com/~dtd/sign_encrypt/sign_encrypt7.html">Another page</a> describes this in far better detail than I have room to; note that this flaw is fixed via triple-wrapping (which has relatively poor support). This creates yet more UI burden into how to adequately describe in UI all the various minutiae in differing security guarantees. Considering that users already have a hard time even understanding that just because a message says it's from <tt>example@isp.invalid</tt> doesn't actually mean it's from <tt>example@isp.invalid</tt>, trying to develop UI that both adequately expresses the security issues and is understandable to end-users is an extreme challenge.
</p><p>
What we have in S/MIME (and PGP) is a system that allows for strong guarantees, if certain conditions are met, yet is also vulnerable to breaches of security if the message handling subsystems are poorly designed. Hopefully this is a sufficient guide to the technical impacts of secure email in the email world. My next post will discuss the most critical component of secure email: the trust model. After that, I will discuss why secure email has seen poor uptake and other relevant concerns on the future of email security.
</p><p>
[1] This is a bit of a lie: a channel that does secrecy and authentication at different times isn't as secure as one that does them at the same time.<br>
[2] It is worth noting that authenticity is, in many respects, necessary to achieve secrecy.<br>
[3] This, too, is a bit of a lie. More on this in a subsequent post.<br>
[4] I'm very aware that S/MIME and PGP use radically different trust models. Trust models will be covered later.<br>
[5] S/MIME 3.0 did add a provision stating that if the signed/encrypted part is a <tt>message/rfc822</tt> part, the headers of that part should override the outer message's headers. However, I am not aware of a major email client that actually handles these kind of messages gracefully.<br>
[6] Actually, I tested Windows Live Mail instead of Outlook, but given the presence of an official MIME-to-Microsoft's-internal-message-format document which seems to agree with what Windows Live Mail was doing, I figure their output would be identical.<br>
[7] On a more careful examination after the fact, it appears that Evolution may have tried to indicate signedness on a part-by-part basis, but the UI was sufficiently confusing that ordinary users are going to be easily confused.Joshua Cranmerhttp://www.blogger.com/profile/02760318962075959780noreply@blogger.com8tag:blogger.com,1999:blog-5947958124349996271.post-80337364938271948962014-04-05T13:18:00.001-04:002014-04-05T13:18:15.394-04:00Announcing jsmime 0.2Previously, I've been developing JSMime as a subdirectory within comm-central. However, after discussions with other developers, I have moved the official repository of record for JSMime to its own repository, now found on <a href="https://github.com/mozilla-comm/jsmime">GitHub</a>. The repository has been cleaned up and the feature set for version 0.2 has been selected, so that the current tip on JSMime (also the initial version) is version 0.2. This contains the feature set I imported into Thunderbird's source code <a href="http://hg.mozilla.org/comm-central/rev/2ad8960eb396">last night</a>, which is to say support for parsing MIME messages into the MIME tree, as well as support for parsing and encoding email address headers.
</p><p>
Thunderbird doesn't actually use the new code quite yet (as my current tree is stuck on a mozilla-central build error, so I haven't had time to run those patches through a last minute sanity check before requesting review), but the intent is to replace the current C++ implementations of <tt>nsIMsgHeaderParser</tt> and <tt>nsIMimeConverter</tt> with JSMime instead. Once those are done, I will be moving forward with my structured header plans which more or less ought to make those interfaces obsolete.
</p><p>
Within JSMime itself, the pieces which I will be working on next will be rounding out the implementation of header parsing and encoding support (I have prototypes for Date headers and the infernal RFC 2231 encoding that Content-Disposition needs), as well as support for building MIME messages from their constituent parts (a feature which would be greatly appreciated in the depths of compose and import in Thunderbird). I also want to implement full IDN and EAI support, but that's hampered by the lack of a JS implementation I can use for IDN (yes, there's punycode.js, but that doesn't do StringPrep). The important task of converting the MIME tree to a list of body parts and attachments is something I do want to work on as well, but I've vacillated on the implementation here several times and I'm not sure I've found one I like yet.
</p><p>
JSMime, as its name implies, tries to work in as pure JS as possible, augmented with several web APIs as necessary (such as TextDecoder for charset decoding). I'm using ES6 as the base here, because it gives me several features I consider invaluable for implementing JavaScript: Promises, Map, generators, <tt>let</tt>. This means it can run on an unprivileged web page—I test JSMime using Firefox nightlies and the Firefox debugger where necessary. Unfortunately, it only really works in Firefox at the moment because V8 doesn't support many ES6 features yet (such as destructuring, which is annoying but simple enough to work around, or Map iteration, which is completely necessary for the code). I'm not opposed to changing it to make it work on Node.js or Chrome, but I don't realistically have the time to spend doing it myself; if someone else has the time, please feel free to contact me or send patches.Joshua Cranmerhttp://www.blogger.com/profile/02760318962075959780noreply@blogger.com35tag:blogger.com,1999:blog-5947958124349996271.post-30861144677582472352014-04-03T12:52:00.001-04:002014-04-03T12:52:50.231-04:00If you want fast code, don't use assembly…unless you're an expert at assembly, that is. The title of this post was obviously meant to be an attention-grabber, but it is much truer than you might think: poorly-written assembly code will probably be slower than an optimizing compiler on well-written code (note that you may need to help the compiler along for things like vectorization). Now why is this?
</p><p>
Modern microarchitectures are incredibly complex. A modern x86 processor will be superscalar and use some form of compilation to microcode to do that. Desktop processors will undoubtedly have multiple instruction issues per cycle, forms of register renaming, branch predictors, etc. Minor changes—a misaligned instruction stream, a poor order of instructions, a bad instruction choice—could kill the ability to take advantages of these features. There are very few people who could accurately predict the performance of a given assembly stream (I myself wouldn't attempt it if the architecture can take advantage of ILP), and these people are disproportionately likely to be working on compiler optimizations. So unless you're knowledgeable enough about assembly to help work on a compiler, you probably shouldn't be hand-coding assembly to make code faster.
</p><p>
To give an example to elucidate this point (and the motivation for this blog post in the first place), I was given a link to <a href="http://davidad.github.io/blog/2014/02/25/overkilling-the-8-queens-problem/">an implementation of the N-queens problem in assembly</a>. For various reasons, I decided to use this to start building a fine-grained performance measurement system. This system uses a high-resolution monotonic clock on Linux and runs the function 1000 times to warm up caches and counters and then runs the function 1000 more times, measuring each run independently and reporting the average runtime at the end. This is a single execution of the system; 20 executions of the system were used as the baseline for a t-test to determine statistical significance as well as visual estimation of normality of data. Since the runs observed about a constant 1-2 μs of noise, I ran all of my numbers on the 10-queens problem to better separate the data (total runtimes ended up being in the range of 200-300μs at this level). When I say that some versions are faster, the p-values for individual measurements are on the order of 10<sup>-20</sup>—meaning that there is a 1-in-100,000,000,000,000,000,000 chance that the observed speedups could be produced if the programs take the same amount of time to run.
</p><p>
The initial assembly version of the program took about 288μs to run. The first C++ version I coded, originating from the same genesis algorithm that the author of the assembly version used, ran in 275μs. A recursive program beat out a hand-written assembly block of code... and when I manually converted the recursive program into a single loop, the runtime improved to 249μs. It wasn't until I got rid of all of the assembly in the original code that I could get the program to beat the derecursified code (at 244μs)—so it's not the vectorization that's causing the code to be slow. Intrigued, I started to analyze why the original assembly was so slow.
</p><p>
It turns out that there are three main things that I think cause the slow speed of the original code. The first one is alignment of branches: the assembly code contains no instructions to align basic blocks on particular branches, whereas gcc happily emits these for some basic blocks. I mention this first as it is mere conjecture; I never made an attempt to measure the effects for myself. The other two causes are directly measured from observing runtime changes as I slowly replaced the assembly with code. When I replaced the use of <tt>push</tt> and <tt>pop</tt> instructions with a global static array, the runtime improved dramatically. This suggests that the alignment of the stack could be to blame (although the stack is still 8-byte aligned when I checked via gdb), which just goes to show you how much alignments really do matter in code.
</p><p>
The final, and by far most dramatic, effect I saw involves the use of three assembly instructions: <tt>bsf</tt> (find the index of the lowest bit that is set), <tt>btc</tt> (clear a specific bit index), and <tt>shl</tt> (left shift). When I replaced the use of these instructions with a more complicated expression <tt>int bit = x & -x</tt> and <tt>x = x - bit</tt>, the program's speed improved dramatically. And the rationale for why the speed improved won't be found in latency tables, although those will tell you that <tt>bsf</tt> is not a 1-cycle operation. Rather, it's in minutiae that's not immediately obvious.
</p><p>
The original program used the fact that <tt>bsf</tt> sets the zero flag if the input register is 0 as the condition to do the backtracking; the converted code just checked if the value was 0 (using a simple <tt>test</tt> instruction). The compare and the jump instructions are basically converted into a single instruction in the processor. In contrast, the <tt>bsf</tt> does not get to do this; combined with the lower latency of the instruction intrinsically, it means that empty loops take a lot longer to do nothing. The use of an 8-bit shift value is also interesting, as there is a rather severe penalty for using 8-bit registers in Intel processors as far as I can see.
</p><p>
Now, this isn't to say that the compiler will always produce the best code by itself. My <a href="https://gist.github.com/jcranmer/9955203">final code</a> wasn't above using x86 intrinsics for the vector instructions. Replacing the <tt>_mm_andnot_si128</tt> intrinsic with an actual and-not on vectors caused gcc to use other, slower instructions instead of the <tt>vmovq</tt> to move the result out of the SSE registers for reasons I don't particularly want to track down. The use of the <tt>_mm_blend_epi16</tt> and <tt>_mm_srli_si128</tt> intrinsics can probably be replaced with <tt>__builtin_shuffle</tt> instead for more portability, but I was under the misapprehension that this was a clang-only intrinsic when I first played with the code so I never bothered to try that, and this code has passed out of my memory long enough that I don't want to try to mess with it now.
</p><p>
In short, compilers know things about optimizing for modern architectures that many general programmers don't. Compilers may have issues with autovectorization, but the existence of vector intrinsics allow you to force compilers to use vectorization while still giving them leeway to make decisions about instruction scheduling or code alignment which are easy to screw up in hand-written assembly. Also, compilers are liable to get better in the future, whereas hand-written assembly code is unlikely to get faster in the future. So only write assembly code if you really know what you're doing and you know you're better than the compiler.Joshua Cranmerhttp://www.blogger.com/profile/02760318962075959780noreply@blogger.com5tag:blogger.com,1999:blog-5947958124349996271.post-14872776212144951782014-03-14T00:13:00.000-04:002014-03-14T00:17:51.516-04:00Understanding email charsetsSeveral years ago, I embarked on a project to collect the headers of all the messages I could reach on NNTP, with the original intent of studying the progression of the most common news clients. More recently, <a href="http://quetzalcoatal.blogspot.com/2014/01/charsets-and-nntp.html">I used this dataset to attempt to discover the prevalence of charsets in email messages</a>. In doing so, I identified a critical problem with the dataset: since it only contains headers, there is very little scope for actually understanding the full, sad story of charsets. So I've decided to rectify this problem.
</p><p>
This time, I modified my data-collection scripts to make it much easier to mass-download NNTP messages. The first script effectively lists all the newsgroups, and then all the message IDs in those newsgroups, stuffing the results in a set to remove duplicates (cross-posts). The second script uses <a href="http://docs.python.org/2/library/nntplib.html">Python's <tt>nntplib</tt> package</a> to attempt to download all of those messages.
Of the 32,598,261 messages identified by the first set, I succeeded in obtaining 1,025,586 messages in full or in part. Some messages failed to download due to crashing <tt>nntplib</tt> (which appears to be unable to handle messages of unbounded length), and I suspect my newsserver connections may have just timed out in the middle of the download at times. Others failed due to expiring before I could download them. All in all, 19,288 messages were not downloaded.
</p><p>
Analysis of the contents of messages were hampered due to a strong desire to find techniques that could mangle messages as little as possible. Prior experience with <a href="http://docs.python.org/2/library/email.html">Python's message-parsing libraries</a> lend me to believe that they are rather poor at handling some of the crap that comes into existence, and the errors in <tt>nntplib</tt> suggest they haven't fixed them yet. The only message parsing framework I truly trust to give me the level of finess is the JSMime that I'm writing, but that happens to be in the wrong language for this project. After reading <a href="http://jeffreystedfast.blogspot.com/2013/09/time-for-rant-on-mime-parsers.html">some blog posts of Jeffrey Stedfast</a>, though, I decided I would give <a href="https://developer.gnome.org/gmime/stable/">GMime</a> a try instead of trying to rewrite ad-hoc MIME parser #N.
</p><p>
Ultimately, I wrote a program to investigate the following questions on how messages operate in practice:
</p><ul>
<li>What charsets are used in practice? How are these charsets named?</li>
<li>For undeclared charsets, what are the correct charsets?</li>
<li>For charsets unknown to a decoder, how often would ASCII suffice?</li>
<li>What charsets are used in <a href="http://tools.ietf.org/html/rfc2047">RFC 2047</a> encoded words?</li>
<li>How prevalent are malformed RFC 2047 encoded words?</li>
<li>When HTML and MIME are mixed, who wins?</li>
<li>What is the state of 8-bit headers?</li>
</ul><p>
While those were the questions I seeked the answers to originally, I did come up with others as I worked on my tool, some in part due to what information I was basically already collecting. The tool I wrote primarily uses GMime to convert the body parts to 8-bit text (no charset conversion), as well as parse the <tt>Content-Type</tt> headers, which are really annoying to do without writing a full parser. I used ICU to handle charset conversion and detection. RFC 2047 decoding is done largely by hand since I needed very specific information that I couldn't convince GMime to give me. All code that I used is available upon request; the exact dataset is harder to transport, given that it is some 5.6GiB of data.
</p><p>
Other than GMime being built on GObject and exposing a C API, I can't complain much, although I didn't try to use it to do magic. Then again, in my experience (and as this post will probably convince you as well), you really want your MIME
library to do charset magic for you, so in doing well for my needs, it's actually not doing well for a larger audience. ICU's C API similarly makes me want to complain. However, I'm now very suspect of the quality of its charset detection code, which is the main reason I used it. Trying to figure out how to get it to handle the charset decoding errors also proved far more annoying than it really should.
</p><p>
Some final background regards the biases I expect to crop up in the dataset. As the approximately 1 million messages were drawn from the python set iterator, I suspect that there's no systematic bias towards or away from specific groups, excepting that the ~11K messages found in the <tt>eternal-september.*</tt> hierarchy are completely represented. The newsserver I used, Eternal September, has a respectably large set of newsgroups, although it is likely to be biased towards European languages and under-representing East Asians. The less well-connected South America, Africa, or central Asia are going to be almost completely unrepresented. The download process will be biased away towards particularly heinous messages (such as exceedingly long lines), since <tt>nntplib</tt> itself is failing.
</p><p>
This being news messages, I also expect that use of 8-bit will be far more common than would be the case in regular mail messages. On a related note, the use of 8-bit in headers would be commensurately elevated compared to normal email. What would be far less common is HTML. I also expect that undeclared charsets may be slightly higher.
</p><h3>Charsets</h3><p>
Charset data is mostly collected on the basis of individual body parts within body messages; some messages have more than one. Interestingly enough, the 1,025,587 messages yielded 1,016,765 body parts with some text data, which indicates that either the messages on the server had only headers in the first place or the download process somehow managed to only grab the headers. There were also 393 messages that I identified having parts with different charsets, which only further illustrates how annoying charsets are in messages.
</p><p>
The aliases in charsets are mostly uninteresting in variance, except for the various labels used for US-ASCII (<tt>us - ascii</tt>, <tt>646</tt>, and <tt>ANSI_X3.4-1968</tt> are the less-well-known aliases), as well as the list of charsets whose names ICU was incapable of recognizing, given below. Unknown charsets are treated as equivalent to undeclared charsets in further processing, as there were too few to merit separate handling (45 in all).
</p><ul>
<li>x-windows-949</li>
<li>isolatin</li>
<li>ISO-IR-111</li>
<li>Default</li>
<li>ISO-8859-1 format=flowed</li>
<li>X-UNKNOWN</li>
<li>x-user-defined</li>
<li>windows-874</li>
<li>3D"us-ascii"</li>
<li>x-koi8-ru</li>
<li>windows-1252 (fuer gute Newsreader)</li>
<li>LATIN-1#2 iso-8859-1</li>
</ul><p>
For the next step, I used ICU to attempt to detect the actual charset of the body parts. ICU's charset detector doesn't support the full gamut of charsets, though, so charset names not claimed to be detected were instead processed by checking if they decoded without error. Before using this detection, I detect if the text is pure ASCII (excluding control characters, to enable charsets like ISO-2022-JP, and <tt>+</tt>, if the charset we're trying to check is UTF-7). ICU has a mode which ignores all text in things that look like HTML tags, and this mode is set for all HTML body parts.
</p><p>
I don't quite believe ICU's charset detection results, so I've collapsed the results into a simpler table to capture the most salient feature. The correct column indicates the cases where the detected result was the declared charset. The ASCII column captures the fraction which were pure ASCII. The UTF-8 column indicates if ICU reported that the text was UTF-8 (it always seems to try this first). The Wrong C1 column refers to an ISO-8859-1 text being detected as windows-1252 or vice versa, which is set by ICU if it sees or doesn't see an octet in the appropriate range. The other column refers to all other cases, including invalid cases for charsets not supported by ICU.
</p><table cellspacing=0>
<style scoped>
table { background-color: white; color: black; }
td, th { text-align: right; padding: 1px 0.2em; }
thead th { text-align: center; }
td, th:not(:first-child) { border-left: 1px solid black; }
thead > tr > * { border-bottom: 1px solid black; }
tfoot > tr > * { border-top: 1px solid black; }
tbody > tr:nth-child(2n) { background-color: rgba(189, 220, 240, 1); }
tbody th, tfoot th { text-align: left; }
</style>
<thead><tr>
<th>Declared</th><th>Correct</th><th>ASCII</th><th>UTF-8</th>
<th>Wrong C1</th><th>Other</th><th>Total</th>
</thead><tbody>
<tr><th>ISO-8859-1<td>230,526<td>225,667<td>883<td>8,119<td>1,035<td>466,230</td></tr>
<tr><th>Undeclared<td><td>148,054<td>1,116<td><td>37,626<td>186,796</td></tr>
<tr><th>UTF-8<td>75,674<td>37,600<td><td><td>1,551<td>114,825</td></tr>
<tr><th>US-ASCII<td><td>98,238<td>0<td><td>304<td>98,542</td></tr>
<tr><th>ISO-8859-15<td>67,529<td>18,527<td><td><td>0<td>86,056</td></tr>
<tr><th>windows-1252<td>21,414<td>4,370<td>154<td>3,319<td>130<td>29,387</td></tr>
<tr><th>ISO-8859-2<td>18,647<td>2,138<td>70<td>71<td>2,319<td>23,245</td></tr>
<tr><th>KOI8-R<td>4,616<td>424<td>2<td><td>1,112<td>6,154</td></tr>
<tr><th>GB2312<td>1,307<td>59<td>0<td><td>112<td>1,478</td></tr>
<tr><th>Big5<td>622<td>608<td>0<td><td>174<td>1,404</td></tr>
<tr><th>windows-1256<td>343<td>10<td>0<td><td>45<td>398</td></tr>
<tr><th>IBM437<td>84<td>257<td><td><td>0<td>341</td></tr>
<tr><th>ISO-8859-13<td>311<td>6<td><td><td>0<td>317</td></tr>
<tr><th>windows-1251<td>131<td>97<td>1<td><td>61<td>290</td></tr>
<tr><th>windows-1250<td>69<td>69<td>0<td>14<td>101<td>253</td></tr>
<tr><th>ISO-8859-7<td>26<td>26<td>0<td>0<td>131<td>183</td></tr>
<tr><th>ISO-8859-9<td>127<td>11<td>0<td>0<td>17<td>155</td></tr>
<tr><th>ISO-2022-JP<td>76<td>69<td>0<td><td>3<td>148</td></tr>
<tr><th>macintosh<td>67<td>57<td><td><td>0<td>124</td></tr>
<tr><th>ISO-8859-16<td>0<td>15<td><td><td>101<td>116</td></tr>
<tr><th>UTF-7<td>51<td>4<td><td><td>0<td>55</td></tr>
<tr><th>x-mac-croatian<td>0<td>13<td><td><td>25<td>38</tr>
<tr><th>KOI8-U<td>28<td>2<td><td><td>0<td>30</td></tr>
<tr><th>windows-1255<td>0<td>18<td>0<td>0<td>6<td>24</td></tr>
<tr><th>ISO-8859-4<td>23<td>0<td><td><td>0<td>23</td></tr>
<tr><th>EUC-KR<td>0<td>3<td>0<td><td>16<td>19</td></tr>
<tr><th>ISO-8859-14<td>14<td>4<td><td><td>0<td>18</td></tr>
<tr><th>GB18030<td>14<td>3<td>0<td><td>0<td>17</td></tr>
<tr><th>ISO-8859-8<td>0<td>0<td>0<td>0<td>16<td>16</td></tr>
<tr><th>TIS-620<td>15<td>0<td><td><td>0<td>15</td></tr>
<tr><th>Shift_JIS<td>8<td>4<td>0<td><td>1<td>13</td></tr>
<tr><th>ISO-8859-3<td>9<td>1<td><td><td>1<td>11</td></tr>
<tr><th>ISO-8859-10<td>10<td>0<td><td><td>0<td>10</td></tr>
<tr><th>KSC_5601<td>3<td>6<td><td><td>0<td>9</td></tr>
<tr><th>GBK<td>4<td>2<td><td><td>0<td>6</td></tr>
<tr><th>windows-1253<td>0<td>3<td>0<td>0<td>2<td>5</td></tr>
<tr><th>ISO-8859-5<td>1<td>0<td>0<td><td>3<td>4</td></tr>
<tr><th>IBM850<td>0<td>4<td><td><td>0<td>4</td></tr>
<tr><th>windows-1257<td>0<td>3<td><td><td>0<td>3</td></tr>
<tr><th>ISO-2022-JP-2<td>2<td>0<td><td><td>0<td>2</td></tr>
<tr><th>ISO-8859-6<td>0<td>1<td>0<td><td>0<td>1</td></tr>
</tbody>
<tfoot><tr><th>Total<td>421,751<td>536,373<td>2,226<td>11,523<td>44,892<td>1,016,765</td></tr></tfoot>
</table><p>
The most obvious thing shown in this table is that the most common charsets remain ISO-8859-1, Windows-1252, US-ASCII, UTF-8, and ISO-8859-15, which is to be expected, given an expected prior bias to European languages in newsgroups. The low prevalence of ISO-2022-JP is surprising to me: it means a lower incidence of Japanese than I would have expected. Either that, or Japanese have switched to UTF-8 en masse, which I consider very unlikely given that Japanese have tended to resist the trend towards UTF-8 the most.
</p><p>
Beyond that, this dataset has caused me to lose trust in the ICU charset detectors. KOI8-R is recorded as being 18% malformed text, with most of that ICU believing to be ISO-8859-1 instead. Judging from the results, it appears that ICU has a bias towards guessing ISO-8859-1, which means I don't believe the numbers in the Other column to be accurate at all. For some reason, I don't
appear to have decoders for ISO-8859-16 or x-mac-croatian on my local machine, but running some tests by hand appear to indicate that they are valid and not incorrect.
</p><p>
Somewhere between 0.1% and 1.0% of all messages are subject to mojibake, depending on how much you trust the charset detector. The cases of UTF-8 being misdetected as non-UTF-8 could potentially be explained by having very few non-ASCII sequences (ICU requires four valid sequences before it confidently declares text UTF-8); someone who writes a post in English but has a non-ASCII signature (such as myself) could easily fall into this category. Despite this, however, it does suggest that there is enough mojibake around that users need to be able to override charset decisions.
</p><p>
The undeclared charsets are described, in descending order of popularity, by ISO-8859-1, Windows-1252, KOI8-R, ISO-8859-2, and UTF-8, describing 99% of all non-ASCII undeclared data. ISO-8859-1 and Windows-1252 are probably over-counted here, but the interesting tidbit is that KOI8-R is used half as much undeclared as it is declared, and I suspect it may be undercounted. The practice of using locale-default fallbacks that Thunderbird has been using appears to be the best way forward for now, although UTF-8 is growing enough in popularity that using a specialized detector that decodes as UTF-8 if possible may be worth investigating (3% of all non-ASCII, undeclared messages are UTF-8).
</p><h3>HTML</h3><p>
Unsuprisingly (considering I'm polling newsgroups), very few messages contained any HTML parts at all: there were only 1,032 parts in the total sample size, of which only 552 had non-ASCII characters and were therefore useful for the rest of this analysis. This means that I'm skeptical of generalizing the results of this to email in general, but I'll still summarize the findings.
</p><p>
HTML, unlike plain text, contains a mechanism to explicitly identify the charset of a message. The <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/parsing.html#determining-the-character-encoding">official algorithm</a> for determining the charset of an HTML file can be described simply as "look for a <meta> tag in the first 1024 bytes.
If it can be found, attempt to extract a charset using one of several different techniques depending on what's present or not." Since doing this fully properly is complicated in library-less C++ code, I opted to look first for a <tt><meta[ \t\r\n\f]</tt> production, guess the extent of the tag, and try to find a <tt>charset=</tt> string somewhere in that tag. This appears to be an approach which is more reflective of how this parsing is actually done in email clients than the proper HTML algorithm. One difference is that my regular expressions also support the newer <tt><meta charset="UTF-8"/></tt> construct, although I don't appear to see any use of this.
</p><p>
I found only 332 parts where the HTML declared a charset. Only 22 parts had a case where both a MIME charset and an HTML charset and the two disagreed with each other. I neglected to count how many messages had HTML charsets but no MIME charsets, but random sampling appeared to indicate that this is very rare on the data set (the same order of magnitude or less as those where they
disagreed).
</p><p>
As for the question of who wins: of the 552 non-ASCII HTML parts, only 71 messages did not have the MIME type be the valid charset. Then again, 71 messages did not have the HTML type be valid either, which strongly suggests that ICU was detecting the incorrect charset. Judging from manual inspection of such messages, it appears that the MIME charset ought to be preferred if it exists. There are also a large number of HTML charset specifications saying <tt>unicode</tt>, which ICU treats as UTF-16, which is most certainly wrong.
</p><h3>Headers</h3><p>
In the data set, 1,025,856 header blocks were processed for the following statistics. This is slightly more than the number of messages since the headers of contained <tt>message/rfc822</tt> parts were also processed. The good news is that 97% (996,103) headers were completely ASCII. Of the remaining 29,753 headers, 3.6% (1,058) were UTF-8 and 43.6% (12,965) matched the declared
charset of the first body part. This leaves 52.9% (15,730) that did not match that charset, however.
</p><p>
Now, NNTP messages can generally be expected to have a higher 8-bit header ratio, so this is probably exaggerating the setup in most email messages. That said, the high incidence is definitely an indicator that even non-EAI-aware clients and servers cannot blindly presume that headers are 7-bit, nor can EAI-aware clients and servers presume that 8-bit headers are UTF-8. The high
incidence of mismatching the declared charset suggests that fallback-charset decoding of headers is a necessary step.
</p><p>
RFC 2047 encoded-words is also an interesting statistic to mine. I found 135,951 encoded-words in the data set, which is rather low, considering that messages can be reasonably expected to carry more than one encoded-word. This is likely an artifact of NNTP's tendency towards 8-bit instead of 7-bit communication and understates their presence in regular email.
</p><p>
Counting encoded-words can be difficult, since there is a mechanism to let them continue in multiple pieces. For the purposes of this count, a sequence of such words count as a single word, and I indicate the number of them that had more than one element in a sequence in the Continued column. The 2047 Violation column counts the number of sequences where decoding words individually does not yield the same result as decoding them as a whole, in violation of RFC 2047. The Only ASCII column counts those words containing nothing but ASCII symbols and where the encoding was thus (mostly) pointless. The Invalid column counts the number of sequences that had a decoder error.
</p><table cellspacing=0>
<style scoped>
table { background-color: white; color: black; }
td, th { text-align: right; padding: 1px 0.2em; }
thead th { text-align: center; }
td, th:not(:first-child) { border-left: 1px solid black; }
thead > tr > * { border-bottom: 1px solid black; }
tfoot > tr > * { border-top: 1px solid black; }
tbody > tr:nth-child(2n) { background-color: rgba(189, 220, 240, 1); }
tbody th, tfoot th { text-align: left; }
</style>
<thead>
<tr><th>Charset</th><th>Count</th><th>Continued</th><th>2047 Violation</th><th>Only ASCII</th><th>Invalid</th></tr>
</thead><tbody>
<tr><th>ISO-8859-1</th><td>56,355</td><td>15,610</td><td></td><td>499</td><td>0</td></tr>
<tr><th>UTF-8</th><td>36,563</td><td>14,216</td><td>3,311</td><td>2,704</td><td>9,765</td></tr>
<tr><th>ISO-8859-15</th><td>20,699</td><td>5,695</td><td></td><td>40</td><td>0</td></tr>
<tr><th>ISO-8859-2</th><td>11,247</td><td>2,669</td><td></td><td>9</td><td>0</td></tr>
<tr><th>windows-1252</th><td>5,174</td><td>3,075</td><td></td><td>26</td><td>0</td></tr>
<tr><th>KOI8-R</th><td>3,523</td><td>1,203</td><td></td><td>12</td><td>0</td></tr>
<tr><th>windows-1256</th><td>765</td><td>568</td><td></td><td>0</td><td>0</td></tr>
<tr><th>Big5</th><td>511</td><td>46</td><td>28</td><td>0</td><td>171</td></tr>
<tr><th>ISO-8859-7</th><td>165</td><td>26</td><td></td><td>0</td><td>3</td></tr>
<tr><th>windows-1251</th><td>157</td><td>30</td><td></td><td>2</td><td>0</td></tr>
<tr><th>GB2312</th><td>126</td><td>35</td><td>6</td><td>0</td><td>51</td></tr>
<tr><th>ISO-2022-JP</th><td>102</td><td>8</td><td>5</td><td>0</td><td>49</td></tr>
<tr><th>ISO-8859-13</th><td>78</td><td>45</td><td></td><td>0</td><td>0</td></tr>
<tr><th>ISO-8859-9</th><td>76</td><td>21</td><td></td><td>0</td><td>0</td></tr>
<tr><th>ISO-8859-4</th><td>71</td><td>2</td><td></td><td>0</td><td>0</td></tr>
<tr><th>windows-1250</th><td>68</td><td>21</td><td></td><td>0</td><td>0</td></tr>
<tr><th>ISO-8859-5</th><td>66</td><td>20</td><td></td><td>0</td><td>0</td></tr>
<tr><th>US-ASCII</th><td>38</td><td>10</td><td></td><td>38</td><td>0</td></tr>
<tr><th>TIS-620</th><td>36</td><td>34</td><td></td><td>0</td><td>0</td></tr>
<tr><th>KOI8-U</th><td>25</td><td>11</td><td></td><td>0</td><td>0</td></tr>
<tr><th>ISO-8859-16</th><td>22</td><td>1</td><td></td><td>0</td><td>22</td></tr>
<tr><th>UTF-7</th><td>17</td><td>2</td><td>1</td><td>8</td><td>3</td></tr>
<tr><th>EUC-KR</th><td>17</td><td>4</td><td>4</td><td>0</td><td>9</td></tr>
<tr><th>x-mac-croatian</th><td>10</td><td>3</td><td></td><td>0</td><td>10</td></tr>
<tr><th>Shift_JIS</th><td>8</td><td>0</td><td>0</td><td>0</td><td>3</td></tr>
<tr><th>Unknown</th><td>7</td><td>2</td><td></td><td>0</td><td>7</td></tr>
<tr><th>ISO-2022-KR</th><td>7</td><td>0</td><td>0</td><td>0</td><td>0</td></tr>
<tr><th>GB18030</th><td>6</td><td>1</td><td>0</td><td>0</td><td>1</td></tr>
<tr><th>windows-1255</th><td>4</td><td>0</td><td></td><td>0</td><td>0</td></tr>
<tr><th>ISO-8859-14</th><td>3</td><td>0</td><td></td><td>0</td><td>0</td></tr>
<tr><th>ISO-8859-3</th><td>2</td><td>1</td><td></td><td>0</td><td>0</td></tr>
<tr><th>GBK</th><td>2</td><td>0</td><td>0</td><td>0</td><td>2</td></tr>
<tr><th>ISO-8859-6</th><td>1</td><td>1</td><td></td><td>0</td><td>0</td></tr>
</tbody><tfoot>
<tr><th>Total</th><td>135,951</td><td>43,360</td><td>3,361</td><td>3,338</td><td>10,096</td></tr>
</tfoot></table><p>
This table somewhat mirrors the distribution of regular charsets, with one major class of differences: charsets that represent non-Latin scripts (particularly Asian scripts) appear to be overdistributed compared to their corresponding use in body parts. The exception to this rule is GB2312 which is far lower than relative rankings would presume—I attribute this to people using GB2312 being more likely to use 8-bit headers instead of RFC 2047 encoding, although I don't have direct evidence.
</p><p>
Clearly continuations are common, which is to be relatively expected. The sad part is how few people bother to try to adhere to the specification here: out of 14,312 continuations in languages that could violate the specification, 23.5% of them violated the specification. The mode-shifting versions (ISO-2022-JP and EUC-KR) are basically all violated, which suggests that no one bothered to check if their encoder "returns to ASCII" at the end of the word (I know Thunderbird's does, but the other ones I checked don't appear to).
</p><p>
The number of invalid UTF-8 decoded words, 26.7%, seems impossibly high to me. A brief check of my code indicates that this is working incorrectly in the face of invalid continuations, which certainly exaggerates the effect but still leaves a value too high for my tastes. Of more note are the elevated counts for the East Asian charsets: Big5, GB2312, and ISO-2022-JP. I am not an expert in charsets, but I belive that Big5 and GB2312 in particular are a family of almost-but-not-quite-identical charsets and it may be that ICU is choosing the wrong candidate of each family for these instances.
</p><p>
There is a surprisingly large number of encoded words that encode only ASCII. When searching specifically for the ones that use the US-ASCII charset, I found that these can be divided into three categories. One set comes from a few people who apparently have an unsanitized whitespace (space and LF were the two I recall seeing) in the display name, producing encoded words like
<tt>=?us-ascii?Q?=09Edward_Rosten?=</tt>. Blame 40tude Dialog here. Another set encodes some basic characters (most commonly <tt>=</tt> and <tt>?</tt>, although a few other interpreted characters popped up). The final set of errors were double-encoded words, such as <tt>=?us-ascii?Q?=3D=3FUTF-8=3FQ=3Ff=3DC3=3DBCr=3F=3D?=</tt>, which appear to be all generated by an Emacs-based newsreader.
</p><p>
One interesting thing when sifting the results is finding the crap that people produce in their tools. By far the worst single instance of an RFC 2047 encoded-word that I found is this one: <tt>Subject: Re: [Kitchen Nightmares]
Meow! Gordon Ramsay Is =?ISO-8859-1?B?UEgR lqZ VuIEhlYWQgVH rbGeOIFNob BJc
RP2JzZXNzZW?= With My =?ISO-8859-1?B?SHVzYmFuZ JzX0JhbGxzL JfU2F5c19BbXiScw==?=
Baking Company Owner</tt> (complete with embedded spaces), discovered by crashing my ad-hoc base64 decoder (due to the spaces). The interesting thing is that even after investigating the output encoding, it doesn't look like the text is actually correct ISO-8859-1... or any obvious charset for that matter.
</p><p>
I looked at the unknown charsets by hand. Most of them were actually empty charsets (looked like <tt>=??B?Sy4gSC4gdm9uIFLDvGRlbg==?=</tt>), and all but one of the outright empty ones were generated by KNode and really UTF-8. The other one was a Windows-1252 generated by a minor newsreader.
</p><p>
Another important aspect of headers is how to handle 8-bit headers. <a href="http://tools.ietf.org/html/rfc5322">RFC 5322</a> blindly hopes that headers are pure ASCII, while <a href="http://tools.ietf.org/html/rfc6532">RFC 6532</a> dictates that they are UTF-8. Indeed, 97% of headers are ASCII, leaving just 29,753 headers that are not. Of these, only 1,058 (3.6%) are UTF-8 per RFC 6532. Deducing which charset they are is difficult because the large amount of English text for header names and the important control values will greatly skew any charset detector, and there is too little text to give a charset detector confidence. The only metric I could easily apply was testing Thunderbird's heuristic as "the header blocks are the same charset as the message contents"—which only worked 45.2% of the time.
</p><h3>Encodings</h3><p>
While developing an earlier version of my scanning program, I was intrigued to know how often various content transfer encodings were used. I found 1,028,971 parts in all (1,027,474 of which are text parts). The transfer encoding of binary did manage to sneak in, with 57 such parts. Using 8-bit text was very popular, at 381,223 samples, second only to 7-bit at 496,114 samples.
Quoted-printable had 144,932 samples and base64 only 6,640 samples. Extremely interesting are the presence of 4 illegal transfer encodings in 5 messages, two of them obvious typos and the others appearing to be a client mangling header continuations into the transfer-encoding.
</p><h3>Conclusions</h3><p>
So, drawing from the body of this data, I would like to make the following conclusions as to using charsets in mail messages:
</p><ol>
<li><strong>Have a fallback charset.</strong> Undeclared charsets are extremely common, and I'm skeptical that charset detectors are going to get this stuff right, particularly since email can more naturally combine multiple languages than other bodies of text (think signatures). Thunderbird currently uses a locale-dependent fallback charset, which roughly mirrors what Firefox and I think most web browsers do.</li>
<li><strong>Let users override charsets when reading.</strong> On a similar token, mojibake text, while not particularly common, is common enough to make declared charsets sometimes unreliable. It's also possible that the fallback charset is wrong, so users may need to override the chosen charset.</li>
<li><strong>Testing is mandatory.</strong> In this set of messages, I found base64 encoded words with spaces in them, encoded words without charsets (even UNKNOWN-8BIT), and clearly invalid Content-Transfer-Encodings. Real email messages that are flagrantly in violation of basic spec requirements exist, so you should make sure that your email parser and client can handle the weirdest edge cases.</li>
<li><strong>Non-UTF-8, non-ASCII headers exist.</strong> EAI not withstanding, 8-bit headers are a reality. Combined with a predilection for saying ASCII when text is really ASCII, this means that there is often no good in-band information to tell you what charset is correct for headers, so you have to go back to a fallback charset.</li>
<li><strong>US-ASCII really means ASCII.</strong> Email clients appear to do a very good job of only emitting US-ASCII as a charset label if it's US-ASCII. The sample size is too small for me to grasp what charset 8-bit characters should imply in US-ASCII.</li>
<li><strong>Know your decoders.</strong> ISO-8859-1 actually means Windows-1252 in practice. Big5 and GB1232 are actually small families of charsets with slightly different meanings. ICU notably disagrees with some of these realities, so be sure to include in your tests various charset edge cases so you know that the decoders are correct.</li>
<li><strong>UTF-7 is still relevant.</strong> Of the charsets I found not mentioned in the <a href="http://encoding.spec.whatwg.org/">WHATWG encoding spec</a>, IBM437 and x-mac-croatian are in use only due to specific circumstances that limit their generalizable presence. IBM850 is too rare. UTF-7 is common enough that you need to actually worry about it, as abominable and evil a charset it is.</li>
<li><strong>HTML charsets may matter—but MIME matters more.</strong> I don't have enough data to say if charsets declared in HTML are needed to do proper decoding. I do have enough to say fairly conclusively that the MIME charset declaration is authoritative if HTML disagrees.</li>
<li><strong>Charsets are not languages.</strong> The entire reason x-mac-croatian is used at all can be traced to Thunderbird displaying the charset as "Croatian," despite it being pretty clearly not a preferred charset. Similarly most charsets are often enough ASCII that, say, an instance of GB2312 is a poor indicator of whether or not the message is in English. Anyone trying to filter based on charsets is doing a really, really stupid thing.</li>
<li><strong>RFCs reflect an ideal world, not reality.</strong> This is most notable in RFC 2047: the specification may state that encoded words are supposed to be independently decodable, but the evidence is pretty clear that more clients break this rule than uphold it.</li>
<li><strong>Limit the charsets you support.</strong> Just because your library lets you emit a hundred charsets doesn't mean that you should let someone try to do it. You should emit US-ASCII or UTF-8 unless you have a really compelling reason not to, and those compelling reasons don't require obscure charsets. Some particularly annoying charsets should never be written: EBCDIC is already basically dead on the web, and I'd like to see UTF-7 die as well.</li>
</ol><p>
When I have time, I'm planning on taking some of the more egregious or interesting messages in my dataset and packaging them into a database of emails to help create testsuites on handling messages properly.Joshua Cranmerhttp://www.blogger.com/profile/02760318962075959780noreply@blogger.com3tag:blogger.com,1999:blog-5947958124349996271.post-2713634547905501942014-01-31T22:57:00.000-05:002014-01-31T22:57:13.868-05:00Why email is hard, part 5: mail headersThis post is part 5 of <a href="http://quetzalcoatal.blogspot.com/search/label/email-hard">an intermittent series</a> exploring the difficulties of writing an email client. <a href="http://quetzalcoatal.blogspot.com/2013/09/why-email-is-hard-part-1-architecture.html">Part 1</a> describes a brief history of the infrastructure. <a href="http://quetzalcoatal.blogspot.com/2013/10/why-email-is-hard-part-2.html">Part 2</a> discusses internationalization. <a href="http://quetzalcoatal.blogspot.com/2013/11/why-email-is-hard-part-3-mime.html">Part 3</a> discusses MIME. <a href="http://quetzalcoatal.blogspot.com/2013/12/why-email-is-hard-part-4-email-addresses.html">Part 4</a> discusses email addresses. This post discusses the more general problem of email headers.
</p><p>
Back in my first post, Ludovic kindly posted, in a comment, a link to <a href="http://sietch-tabr.tumblr.com/post/58044873275/a-very-nice-video-explaining-why-email-is-hard-to">a talk of someone else's email rant</a>. And the best place to start this post is with a quote from that talk: "If you want to see an email programmer's face turn red, ask him about CFWS." CFWS is an acronym that stands for "comments and folded whitespace," and I can attest that the mere mention of CFWS is enough for me to start ranting. Comments in email headers are spans of text wrapped in parentheses, and the folding of whitespace refers to the ability to continue headers on multiple lines by inserting a newline before (but not in lieu of) a space.
</p><p>
I'll start by pointing out that there is little advantage to adding in free-form data to headers which are not going to be manually read in the vast majority of cases. In practice, I have seen comments used for only three headers on a reliable basis. One of these is the <tt>Date</tt> header, where a human-readable name of the timezone is sometimes included. The other two are the <tt>Received</tt> and <tt>Authentication-Results</tt> headers, where some debugging aids are thrown in. There would be no great loss in omitting any of this information; if information is really important, appending an X- header with that information is still a viable option (that's where most spam filtration notes get added, for example).
</p><p>
For this feature of questionable utility in the first place, the impact it has on parsing message headers is enormous. <a href="http://tools.ietf.org/html/rfc822">RFC 822</a> is specified in a manner that is familiar to anyone who reads language specifications: there is a low-level lexical scanning phase which feeds tokens into a secondary parsing phase. Like programming languages, comments and white space are semantically meaningless [1]. Unlike programming languages, however, comments can be nested—and therefore lexing an email header is not regular [2]. The problems of folding (a necessary evil thanks to the line length limit I keep complaining about) pale in comparison to comments, but it's extra complexity that makes machine-readability more difficult.
</p><p>
Fortunately, <a href="http://tools.ietf.org/html/rfc2822">RFC 2822</a> made a drastic change to the specification that greatly limited where CFWS could be inserted into headers. For example, in the <tt>Date</tt> header, comments are allowed only following the timezone offset (and whitespace in a few specific places); in addressing headers, CFWS is not allowed within the email address itself [3]. One unanticipated downside is that it makes reading the other RFCs that specify mail headers more difficult: any version that predates RFC 2822 uses the syntax assumptions of RFC 822 (in particular, CFWS may occur between any listed tokens), whereas RFC 2822 and its descendants all explicitly enumerate where CFWS may occur.
</p><p>
Beyond the issues with CFWS, though, syntax is still problematic. The separation of distinct lexing and parsing phases means that you almost see what may be a hint of uniformity which turns out to be an ephemeral illusion. For example, the header parameters define in RFC 2045 for <tt>Content-Type</tt> and <tt>Content-Disposition</tt> set a tradition of ;-separated <tt>param=value</tt> attributes, which has been picked up by, say, the <tt>DKIM-Signature</tt> or <tt>Authentication-Results</tt> headers. Except a close look indicates that <tt>Authenticatin-Results</tt> allows two <tt>param=value</tt> pairs between semicolons. Another side effect was pointed out in my second post: you can't turn a generic 8-bit header into a 7-bit compatible header, since you can't tell without knowing the syntax of the header which parts can be specified as 2047 encoded-words and which ones can't.
</p><p>
There's more to headers than their syntax, though. Email headers are structured as a somewhat-unordered list of headers; this genericity gives rise to <a href="http://www.iana.org/assignments/message-headers/message-headers.xhtml">a very large number of headers</a>, and that's just the list of official headers. There are unofficial headers whose use is generally agreed upon, such as <tt>X-Face</tt>, <tt>X-No-Archive</tt>, or <tt>X-Priority</tt>; other unofficial headers are used for internal tracking such as Mailman's <tt>X-BeenThere</tt> or Mozilla's <tt>X-Mozilla-Status</tt> headers. Choosing how to semantically interpret these headers (or even which headers to interpret!) can therefore be extremely daunting.
</p><p>
Some of the headers are specified in ways that would seem surprising to most users. For example, the venerable <tt>From</tt> header can represent anywhere between 0 mailboxes [4] to an arbitrarily large number—but most clients assume that only one exists. It's also worth noting that the <tt>Sender</tt> header is (if present) a better indication of message origin as far as tracing is concerned [5], but its relative rarity likely results in filtering applications not taking it into account. The suite of <tt>Resent-</tt>* headers also experiences similar issues.
</p><p>
Another impact of email headers is the degree to which they can be trusted. <a href="http://tools.ietf.org/html/rfc5322">RFC 5322</a> gives some nice-sounding platitudes to how headers are supposed to be defined, but many of those interpretations turn out to be difficult to verify in practice. For example, <tt>Message-ID</tt>s are supposed to be globally unique, but they turn out to be extremely lousy UUIDs for emails on a local system, even if you allow for minor differences like adding trace headers [6].
</p><p>
More serious are the spam, phishing, etc. messages that lie as much as possible so as to be seen by end-users. Assuming that a message is hostile, the only header that can be actually guaranteed to be correct is the first <tt>Received</tt> header, which is added by the final user's mailserver [7]. Every other header, including the <tt>Date</tt> and <tt>From</tt> headers most notably, can be a complete and total lie. There's no real way to authenticate the headers or hide them from snoopers—this has critical consequences for both spam detection and email security.
</p><p>
There's more I could say on this topic (especially CFWS), but I don't think it's worth dwelling on. This is more of a preparatory post for the next entry in the series than a full compilation of complaints. Speaking of my next post, I don't think I'll be able to keep up my entirely-unintentional rate of posting one entry this series a month. I've exhausted the topics in email that I am intimately familiar with and thus have to move on to the ones I'm only familiar with.
</p><p>
[1] Some people attempt to be to zealous in following RFCs and ignore the distinction between syntax and semantics, as I complained about in part 4 when discussing the syntax of email addresses.<br>
[2] I mean this in the theoretical sense of the definition. The proof that balanced parentheses is not a regular language is a standard exercise in use of the pumping lemma.<br>
[3] Unless domain literals are involved. But domain literals are their own special category.<br>
[4] Strictly speaking, the 0 value is intended to be used only when the email has been downgraded and the email address cannot be downgraded. Whether or not these will actually occur in practice is an unresolved question.<br>
[5] Semantically speaking, <tt>Sender</tt> is the person who typed the message up and actually sent it out. <tt>From</tt> is the person who dictated the message. If the two headers would be the same, then <tt>Sender</tt> is omitted.<br>
[6] Take a message that's cross-posted to two mailing lists. Each mailing list will generate copies of the message which end up being submitted back into the mail system and will typically avoid touching the <tt>Message-ID</tt>.<br>
[7] Well, this assumes you trust your email provider. However, your email provider can do far worse to your messages than lie about the <tt>Received</tt> header…Joshua Cranmerhttp://www.blogger.com/profile/02760318962075959780noreply@blogger.com9tag:blogger.com,1999:blog-5947958124349996271.post-87620504171121868272014-01-23T19:53:00.001-05:002014-01-23T19:53:36.235-05:00Charsets and NNTPRecently, the question of charsets came up within the context of necessary decoder support for Thunderbird. After much hemming and hawing about how to find this out (which included a plea to the IMAP-protocol list for data), I remembered that I actually had this data. Long-time readers of this blog may recall that I did <a href="http://quetzalcoatal.blogspot.com/2010/09/usage-share-of-newsreaders.html">a study several years ago on the usage share of newsreaders</a>. After that, I was motivated to take my data collection to the most extreme way possible. Instead of considering only the "official" Big-8 newsgroups, I looked at <em>all</em> of them on <a href="http://www.eternal-september.org/">the news server I use</a> (effectively, all but <tt>alt.binaries</tt>). Instead of relying on pulling the data from the server for the headers I needed, I grabbed all of them—the script literally runs <tt>HEAD</tt> and saves the results in a database. And instead of a month of results, I grabbed the results for the entire year of 2011. And then I sat on the data.
</p><p>
After recalling Henri Svinonen's pesterings about data, I decided to see the suitability of my dataset for this task. For data management reasons, I only grabbed the data from the second half of the year (about 10 million messages). I know from memory that the quality of Python's message parser (which was used to extract data in the first place) is surprisingly poor, which introduces bias of unknown consequence to my data. Since I only extracted headers, I can't identify charsets for anything which was sent as, say, <tt>multipart/alternative</tt> (which is more common than you'd think), which introduces further systematic bias. The end result is approximately 9.6M messages that I could extract charsets from and thence do further research.
</p><p>
Discussions revealed one particularly surprising tidbit of information. The most popular charset not accounted for by the <a href="http://encoding.spec.whatwg.org/">Encoding specification</a> was IBM437. Henri Sivonen speculated that the cause was some crufty old NNTP client on Windows using that encoding, so I endeavored to build a correlation database to check that assumption. Using the wonderful magic of d3, I produced <a href="http://www.tjhsst.edu/~jcranmer/heatmap.html">a heatmap comparing distributions of charsets among various user agents</a>. Details about the visualization may be found on that page, but it does refute Henri's claim when you dig into the data (it appears to be caused by specific BBS-to-news gateways, and is mostly localized in particular BBS newsgroups).
</p><p>
Also found on that page are some fun discoveries of just what kind of crap people try to pass off as valid headers. Some of those User-Agents are clearly spoofs (Outlook Express and family used the X-Newsreader header, not the User-Agent header). There also appears to be a fair amount of mojibake in headers (one of them appeared to be venerable double mojibake). The charsets also have some interesting labels to them: the "big5\n" and the "(null)" illustrate that some people don't double check their code very well, and not shown are the 5 examples of people who think charset names have spaces in them. A few people appear to have mixed up POSIX locales with charsets as well.Joshua Cranmerhttp://www.blogger.com/profile/02760318962075959780noreply@blogger.com9tag:blogger.com,1999:blog-5947958124349996271.post-80743940781883532502013-12-04T18:24:00.000-05:002013-12-04T18:24:17.653-05:00Why email is hard, part 4: Email addressesThis post is part 4 of <a href="http://quetzalcoatal.blogspot.com/search/label/email-hard">an intermittent series</a> exploring the difficulties of writing an email client. <a href="http://quetzalcoatal.blogspot.com/2013/09/why-email-is-hard-part-1-architecture.html">Part 1</a> describes a brief history of the infrastructure. <a href="http://quetzalcoatal.blogspot.com/2013/10/why-email-is-hard-part-2.html">Part 2</a> discusses internationalization. <a href="http://quetzalcoatal.blogspot.com/2013/11/why-email-is-hard-part-3-mime.html">Part 3</a> discusses MIME. This post discusses the problems with email addresses.
</p><p>
You might be surprised that I find email addresses difficult enough to warrant a post discussing only this single topic. However, this is a surprisingly complex topic, and one which is made much harder by the presence of a very large number of people purporting to know the answer who then proceed to do the wrong thing [0]. To understand why email addresses are complicated, and why people do the wrong thing, I pose the following challenge: write a regular expression that matches all valid email addresses and only valid email addresses. Go ahead, stop reading, and play with it for a few minutes, and then you can compare your answer with the correct answer.
</p><p> </p><p> </p><p> </p><p>
Done yet? So, if you came up with a regular expression, you got the wrong answer. But that's because it's a trick question: I never defined what I meant by a valid email address. Still, if you're hoping for partial credit, you may able to get some by correctly matching one of the purported definitions I give below.
</p><p>
The most obvious definition meant by "valid email address" is text that matches the <i>addr-spec</i> production of <a href="http://tools.ietf.org/html/rfc822">RFC 822</a>. No regular expression can match this definition, though—and I am aware of the <a href="http://www.ex-parrot.com/pdw/Mail-RFC822-Address.html">enormous regular expression</a> that is often purported to solve this problem. This is because comments can be nested, which means you would need to solve the "balanced parentheses" language, which is easily provable to be non-regular [2].
</p><p>
Matching the <i>addr-spec</i> production, though, is the wrong thing to do: the production dictates the possible syntax forms an address may have, when you arguably want a more semantic interpretation. As a case in point, the two email addresses <tt>example@test.invalid</tt> and <tt>example @ test . invalid</tt> are both meant to refer to the same thing. When you ignore the actual full grammar of an email address and instead read the prose, particularly of <a href="http://tools.ietf.org/html/rfc5322">RFC 5322</a> instead of RFC 822, you'll realize that matching comments and whitespace are entirely the wrong thing to do in the email address.
</p><p>
Here, though, we run into another problem. Email addresses are split into local-parts and the domain, the text before and after the <tt>@</tt> character; the format of the local-part is basically either a quoted string (to escape otherwise illegal characters in a local-part), or an unquoted "dot-atom" production. The quoting is meant to be semantically invisible: <tt>"example"@test.invalid</tt> is the same email address as <tt>example@test.invalid</tt>. Normally, I would say that the use of quoted strings is an artifact of the encoding form, but given the strong appetite for aggressively "correct" email validators that attempt to blindly match the specification, it seems to me that it is better to keep the local-parts quoted if they need to be quoted. The <i>dot-atom</i> production matches a sequence of atoms (spans of text excluding several special characters like <tt>[</tt> or <tt>.</tt>) separated by <tt>.</tt> characters, with no intervening spaces or comments allowed anywhere.
</p><p>
RFC 5322 only specifies how to unfold the syntax into a semantic value, and it does not explain how to semantically interpret the values of an email address. For that, we must turn to SMTP's definition in <a href="http://tools.ietf.org/html/rfc5321">RFC 5321</a>, whose semantic definition clearly imparts requirements on the format of an email address not found in RFC 5322. On domains, RFC 5321 explains that the domain is either a standard domain name [3], or it is a domain literal which is either an IPv4 or an IPv6 address. Examples of the latter two forms are <tt>test@[127.0.0.1]</tt> and <tt>test@[IPv6:::1]</tt>. But when it comes to the local-parts, RFC 5321 decides to just give up and admit no interpretation except at the final host, advising only that servers should avoid local-parts that need to be quoted. In the context of email specification, this kind of recommendation is effectively a requirement to not use such email addresses, and (by implication) most client code can avoid supporting these email addresses [4].
</p><p>
The prospect of internationalized domain names and email addresses throws a massive wrench into the state affairs, however. I've talked at length in part 2 about the problems here; the lack of a definitive decision on Unicode normalization means that the future here is extremely uncertain, although <a href="http://tools.ietf.org/html/rfc6530">RFC 6530</a> does implicitly advise that servers should accept that some (but not all) clients are going to do NFC or NFKC normalization on email addresses.
</p><p>
At this point, it should be clear that asking for a regular expression to validate email addresses is really asking the wrong question. I did it at the beginning of this post because that is how the question tends to be phrased. The real question that people should be asking is "what characters are valid in an email address?" (and more specifically, the left-hand side of the email address, since the right-hand side is obviously a domain name). The answer is simple: among the ASCII printable characters (Unicode is more difficult), all the characters but those in the following string: <tt>" \"\\<>[]();,@"</tt>. Indeed, viewing an email address like this is <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/states-of-the-type-attribute.html#e-mail-state-%28type=email%29">exactly how HTML 5 specifies it</a> in its definition of a format for <tt><input type="email"></tt>
</p><p>
Another, much easier, more obvious, and simpler way to validate an email address relies on zero regular expressions and zero references to specifications. Just send an email to the purported address and ask the user to click on a unique link to complete registration. After all, the most common reason to request an email address is to be able to send messages to that email address, so if mail cannot be sent to it, the email address should be considered invalid, even if it is syntactically valid.
</p><p>
Unfortunately, people persist in trying to write buggy email validators. Some are too simple and ignore valid characters (or valid top-level domain names!). Others are too focused on trying to match the RFC addr-spec syntax that, while they will happily accept most or all addr-spec forms, they also result in email addresses which are very likely to weak havoc if you pass to another system to send email; cause various forms of SQL injection, XSS injection, or even shell injection attacks; and which are likely to confuse tools as to what the email address actually is. This can be ameliorated with complicated normalization functions for email addresses, but none of the email validators I've looked at actually do this (which, again, goes to show that they're missing the point).
</p><p>
Which brings me to a second quiz question: are email addresses case-insensitive? If you answered no, well, you're wrong. If you answered yes, you're also wrong. The local-part, as RFC 5321 emphasizes, is not to be interpreted by anyone but the final destination MTA server. A consequence is that it does not specify if they are case-sensitive or case-insensitive, which means that general code should not assume that it is case-insensitive. Domains, of course, are case-insensitive, unless you're talking about internationalized domain names [5]. In practice, though, RFC 5321 admits that servers should make the names case-insensitive. For everyone else who uses email addresses, the effective result of this admission is that email addresses should be stored in their original case but matched case-insensitively (effectively, code should be case-preserving).
</p><p>
Hopefully this gives you a sense of why email addresses are frustrating and much more complicated then they first appear. There are historical artifacts of email addresses I've decided not to address (the roles of ! and % in addresses), but since they only matter to some SMTP implementations, I'll discuss them when I pick up SMTP in a later part (if I ever discuss them). I've avoided discussing some major issues with the specification here, because they are much better handled as part of the issues with email headers in general.
</p><p>
Oh, and if you were expecting regular expression answers to the challenge I gave at the beginning of the post, here are the answers I threw together for my various definitions of "valid email address." I didn't test or even try to compile any of these regular expressions (as you should have gathered, regular expressions are not what you should be using), so caveat emptor.
</p><dl>
<dt>RFC 822 addr-spec</dt><dd>Impossible. Don't even try.</dd>
<dt>RFC 5322 non-obsolete addr-spec production</dt>
<dd><tt>([^\x00-\x20()<>\[\]:;@\\,.]+(\.[^\x00-\x20()<>\[\]:;@\\,.]+)*|"(\\.|[^\\"])*")@([^\x00-\x20()<>\[\]:;@\\,.]+(.[^\x00-\x20()<>\[\]:;@\\,.]+)*|\[(\\.|[^\\\]])*\])</tt></dd>
<dt>RFC 5322, unquoted email address</dt>
<dd><tt>.*@([^\x00-\x20()<>\[\]:;@\\,.]+(\.[^\x00-\x20()<>\[\]:;@\\,.]+)*|\[(\\.|[^\\\]])*\])</tt></dd>
<dt>HTML 5's interpretation</dt>
<dd><tt>[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*</tt></dd>
<dt>Effective EAI-aware version</dt>
<dd><tt>[^\x00-\x20\x80-\x9f]()<>\[\]:;@\\,]+@[^\x00-\x20\x80-\x9f()<>\[\]:;@\\,]+</tt>, with the caveats that a dot does not begin or end the local-part, nor do two dots appear subsequent, the local part is in NFC or NFKC form, and the domain is a valid domain name.</dd>
</dl><p>
[1] If you're trying to find guides on valid email addresses, a useful way to eliminate incorrect answers are the following litmus tests. First, if the guide mentions an RFC, but does not mention RFC 5321 (or RFC 2821, in a pinch), you can generally ignore it. If the email address <tt>test (not) @ example.com</tt> would be valid, then the author has clearly not carefully read and understood the specifications. If the guide mentions RFC 5321, RFC 5322, RFC 6530, and IDN, then the author clearly has taken the time to actually understand the subject matter and their opinion can be trusted.<br>
[2] I'm using "regular" here in the sense of theoretical regular languages. Perl-compatible regular expressions can match non-regular languages (because of backreferences), but even backreferences can't solve the problem here. It appears that newer versions support a construct which can match balanced parentheses, but I'm going to discount that because by the time you're going to start using that feature, you have at least two problems.<br>
[3] Specifically, if you want to get really technical, the domain name is going to be routed via MX records in DNS.<br>
[4] RFC 5321 is the specification for SMTP, and, therefore, it is only truly binding for things that talk SMTP; likewise, RFC 5322 is only binding on people who speak email headers. When I say that systems can pretend that email addresses with domain literals or quoted local-parts don't exist, I'm excluding mail clients and mail servers. If you're writing a website and you need an email address, there is no need to support email addresses which don't exist on the open, public Internet.<br>
[5] My usual approach to seeing internationalization at this point (if you haven't gathered from the lengthy second post of this series) is to assume that the specifications assume magic where case insensitivity is desired.Joshua Cranmerhttp://www.blogger.com/profile/02760318962075959780noreply@blogger.com48tag:blogger.com,1999:blog-5947958124349996271.post-92148076160893995722013-11-20T14:54:00.003-05:002013-11-20T14:54:48.855-05:00Why email is hard, part 3: MIMEThis post is part 3 of <a href="http://quetzalcoatal.blogspot.com/search/label/email-hard">an intermittent series</a> exploring the difficulties of writing an email client. Part 1 describes a brief history of the infrastructure. Part 2 discuses internationalization. This post discusses MIME, the mechanism by which email evolves beyond plain text.
</p><p>
MIME, which stands for Multipurpose Internet Mail Extensions, is primarily dictated by a set of 5 RFCs: <a href="http://tools.ietf.org/html/rfc2045">RFC 2045</a>, <a href="http://tools.ietf.org/html/rfc2046">RFC 2046</a>, <a href="http://tools.ietf.org/html/rfc2047">RFC 2047</a>, <a href="http://tools.ietf.org/html/rfc2048">RFC 2048</a>, and <a href="http://tools.ietf.org/html/rfc2049">RFC 2049</a>, although RFC 2048 (which governs registration procedures for new MIME types) was updated with newer versions. RFC 2045 covers the format of related headers, as well as the format of the encodings used to convert 8-bit data into 7-bit for transmission. RFC 2046 describes the basic set of MIME types, most importantly the format of multipart/ types. RFC 2047 was discussed in my part 2 of this series, as it discusses encoding internationalized data in headers. RFC 2049 describes a set of guidelines for how to be conformant when processing MIME; as you might imagine, these are woefully inadequate for modern processing anyways. In practice, it is only the first three documents that matter for building an email client.
</p><p>
There are two main contributions of MIME, which actually makes it a bit hard to know what is meant when people refer to MIME in the abstract. The first contribution, which is of interest mostly to email, is the development of a tree-based representation of email which allows for the inclusion of non-textual parts to messages. This tree is ultimately how attachments and other features are incorporated. The other contribution is the development of a registry of MIME types for different types of file contents. MIME types have promulgated far beyond just the email infrastructure: if you want to describe what kind of file binary blob is, you can refer to it by either a magic header sequence, a file extension, or a MIME type. Searching for terms like MIME libraries will sometimes refer to libraries that actually handle the so-called MIME sniffing process (guessing a MIME type from a file extension or the contents of a file).
</p><p>
MIME types are decomposable into two parts, a media type and a subtype. The type <tt>text/plain</tt> has a media type of <tt>text</tt> and a subtype of <tt>plain</tt>, for example. IANA maintains an <a href="http://www.iana.org/assignments/media-types">official repository of MIME types</a>. There are very few media types, and I would argue that there ought to be fewer. In practice, degradation of unknown MIME types means that there are essentially three "fundamental" types: <tt>text/plain</tt> (which represents plain, unformatted text and to which unknown <tt>text/*</tt> types degrade), <tt>multipart/mixed</tt> (the "default" version of multipart messages; more on this later), and <tt>application/octet-stream</tt> (which represents unknown, arbitrary binary data). I can understand the separation of the <tt>message</tt> media type for things which generally follow the basic format of headers+body akin to <tt>message/rfc822</tt>, although the presence of types like <tt>message/partial</tt> that don't follow the headers+body format and the requirement to downgrade to <tt>application/octet-stream</tt> mars usability here. The distinction between <tt>image</tt>, <tt>audio</tt>, <tt>video</tt> and <tt>application</tt> is petty when you consider that in practice, the distinction isn't going to be able to make clients give better recommendations for how to handle these kinds of content (which really means deciding if it can be displayed inline or if it needs to be handed off to an external client).
</p><p>
Is there a better way to label content types than MIME types? Probably not. X.400 (remember that from my first post?) uses OIDs, in line with the rest of the OSI model, and my limited workings with other systems that use these OIDs is that they are obtuse, effectively opaque identifiers with no inherent semantic meaning. People use file extensions in practice to distinguish between different file types, but not all content types are stored in files (such as <tt>multipart/mixed</tt>), and the MIME types is a finer granularity to distinguish when needing to guess the type from the start of a file. My only complaints about MIME types are petty and marginal, not about the idea itself.
</p><p>
No, the part of MIME that I have serious complaints with is the MIME tree structure. This allows you to represent emails in arbitrarily complex structures… and onto which the standard view of email as a body with associated attachments is poorly mapped. The heart of this structure is the <tt>multipart</tt> media type, for which the most important subtypes are <tt>mixed</tt>, <tt>alternative</tt>, <tt>related</tt>, <tt>signed</tt>, and <tt>encrypted</tt>. The last two types are meant for cryptographic security definitions [1], and I won't cover them further here. All <tt>multipart</tt> types have a format where the body consists of parts (each with their own headers) separated by a boundary string. There is space before and after the last parts which consists of semantically-meaningless text sometimes containing a message like "This is a MIME message." meant to be displayed to the now practically-non-existent crowd of people who use clients that don't support MIME.
</p><p>
The simplest type is <tt>multipart/mixed</tt>, which means that there is no inherent structure to the parts. Attachments to a message use this type: the type of the message is set to <tt>multipart/mixed</tt>, a body is added as (typically) the first part, and attachments are added as parts with types like <tt>image/png</tt> (for PNG images). It is also not uncommon to see <tt>multipart/mixed</tt> types that have a <tt>multipart/mixed</tt> part within them: some mailing list software attaches footers to messages by wrapping the original message inside a single part of a <tt>multipart/mixed</tt> message and then appending a <tt>text/plain</tt> footer.
</p><p>
<tt>multipart/related</tt> is intended to refer to an HTML page [2] where all of its external resources are included as additional parts. Linking all of these parts together is done by use of a <tt>cid:</tt> URL scheme. Generating and displaying these messages requires tracking down all URL references in an HTML page, which of course means that email clients that want full support for this feature also need robust HTML (and CSS!) knowledge, and future-proofing is hard. Since the primary body of this type appears first in the tree, it also makes handling this datatype in a streaming manner difficult, since the values to which URLs will be rewritten are not known until after the entire body is parsed.
</p><p>
In contrast, <tt>multipart/alternative</tt> is used to satisfy the plain-text-or-HTML debate by allowing one to provide a message that is either plain text or HTML [3]. It is also the third-biggest failure of the entire email infrastructure, in my opinion. The natural expectation would be that the parts should be listed in decreasing order of preference, so that streaming clients can reject all the data after it finds the part it will display. Instead, the parts are listed in <em>increasing</em> order of preference, which was done in order to make the plain text part be first in the list, which helps increase readability of MIME messages for those reading email without MIME-aware clients. As a result, streaming clients are unable to progressively display the contents of <tt>multipart/alternative</tt> until the entire message has been read.
</p><p>
Although <tt>multipart/alternative</tt> states that all parts must contain the same contents (to varying degrees of degradation), you shouldn't be surprised to learn that this is not exactly the case. There was a period in time when spam filterers looked at only the <tt>text/plain</tt> side of things, so spammers took to putting "innocuous" messages in the <tt>text/plain</tt> half and displaying the real spam in the <tt>text/html</tt> half [4] (this technique appears to have died off a long time ago, though). In another interesting case, I received a bug report with a message containing an <tt>image/jpeg</tt> and a <tt>text/html</tt> part within a <tt>multipart/alternative</tt> [5].
</p><p>
To be fair, the current concept of emails as a body with a set of attachments did not exist when MIME was originally specified. The definition of <tt>multipart/parallel</tt> plays into this a lot (it means what you think it does: show all of the parts in parallel… somehow). Reading between the lines of the specification also indicates a desire to create interactive emails (via <tt>application/postscript</tt>, of course). Given that email clients have trouble even displaying HTML properly [6], and the fact that interactivity has the potential to be a walking security hole, it is not hard to see why this functionality fell by the wayside.
</p><p>
The final major challenge that MIME solved was how to fit arbitrary data into a 7-bit format safe for transit. The two encoding schemes they came up with were quoted-printable (which retains most printable characters, but emits non-printable characters in a <tt>=<i>XX</i></tt> format, where the <i>X</i>s are hex characters), and base64 which reencodes every 3 bytes into 4 ASCII characters. Non-encoded data is separated into three categories: 7-bit (which uses only ASCII characters except NUL and bare CR or LF characters), 8-bit (which uses any character but NUL, bare CR, and bare LF), and binary (where everything is possible). A further limitation is placed on all encodings but binary: every line is at most 998 bytes long, not including the terminating CRLF.
</p><p>
A side-effect of these requirements is that all attachments must be considered binary data, even if they are textual formats (like source code), as end-of-line autoconversion is now considered a major misfeature. To make matters even worse, body text for formats with text written in scripts that don't use spaces (such as Japanese or Chinese) can sometimes be prohibited from using 8-bit transfer format due to overly long lines: you can reach the end of a line in as few as 249 characters (UTF-8, non-BMP characters, although Chinese and Japanese typically take three bytes per character). So a single long paragraph can force a message to be entirely encoded in a format with 33% overhead. There have been suggestions for a binary-to-8-bit encoding in the past, but no standardization effort has been made for one [7].
</p><p>
The binary encoding has none of these problems, but no one claims to support it. However, I suspect that violating maximum line length, or adding 8-bit characters to a quoted-printable part, are likely to make it through the mail system, in part because not doing so either increases your security vulnerabilities or requires more implementation effort. Sending lone CR or LF characters is probably fine so long as one is careful to assume that they may be treated as line breaks. Sending a NUL character I suspect could cause some issues due to lack of testing (but it also leaves room for security vulnerabilities to ignore it). In other words, binary-encoded messages probably already work to a large degree in the mail system. Which makes it extremely tempting (even for me) to ignore the specification requirements when composing messages; small wonder then that blatant violations of specifications are common.
</p><p>
This concludes my discussion of MIME. There are certainly many more complaints I have, but this should be sufficient to lay out why building a generic MIME-aware library by itself is hard, and why you do not want to write such a parser yourself. Too bad Thunderbird has at least two different ad-hoc parsers (not libmime or JSMime) that I can think of off the top of my head, both of which are wrong.
</p><p>
[1] I will be covering this in a later post, but the way that signed and encrypted data is represented in MIME actually makes it really easy to introduce flaws in cryptographic code (which, the last time I surveyed major email clients with support for cryptographic code, was done by all of them).<br>
[2] Other types are of course possible in theory, but HTML is all anyone cares about in practice.<br>
[3] There is also <tt>text/enriched</tt>, which was developed as a stopgap while HTML 3.2 was being developed. Its use in practice is exceedingly slim.<br>
[4] This is one of the reasons I'm minded to make "prefer plain text" do degradation of natural HTML display instead of showing the plain text parts. Not that cleanly degrading HTML is easy.<br>
[5] In the interests of full disclosure, the <tt>image/jpeg</tt> was actually a PNG image and the HTML claimed to be 7-bit UTF-8 but was actually 8-bit, and it contained a Unicode homograph attack.<br>
[6] Of the major clients, Outlook uses Word's HTML rendering engine, which I recall once reading as being roughly equivalent to IE 5.5 in capability. Webmail is forced to do their own sanitization and sandboxing, and the output leaves something to desire; Gmail is the worst offender here, stripping out all but inline style. Thunderbird and SeaMonkey are nearly alone in using a high-quality layout engine: you can even send a <video> in an email to Thunderbird and have it work properly. :-)<br>
[7] There is <a href="http://en.wikipedia.org/wiki/YEnc">yEnc</a>. Its mere existence does contradict several claims (for example, that adding new transfer encodings is infeasible due to install base of software), but it was developed for a slightly different purpose. Some implementation details are hostile to MIME, and although it has been discussed to death on the relevant mailing list several times, no draft was ever made that would integrate it into MIME properly.Joshua Cranmerhttp://www.blogger.com/profile/02760318962075959780noreply@blogger.com111tag:blogger.com,1999:blog-5947958124349996271.post-3677587279186653662013-10-11T00:07:00.000-04:002013-10-11T00:07:30.152-04:00Why email is hard, part 2: internationalizationThis post is part 2 of <a href="http://quetzalcoatal.blogspot.com/search/label/email-hard">an intermittent series</a> exploring the difficulties of writing an email client. <a href="http://quetzalcoatal.blogspot.com/2013/09/why-email-is-hard-part-1-architecture.html">Part 1</a> describes a brief history of the infrastructure, as well as the issues I have with it. This post is discussing internationalization, specifically supporting non-ASCII characters in email.
</p><p>
Internationalization is not a simple task, even if the consideration is limited to "merely" the textual aspect [1]. Languages turn out to be incredibly diverse in their writing systems, so software that tries to support all writing systems equally well ends up running into several problems that admit no general solution. Unfortunately, I am ill-placed to be able to offer personal experience with internationalization concerns [2], so some of the information I give may well be wrong.
</p><p>
A word of caution: this post is rather long, even by my standards, since the problems of internationalization are legion. To help keep this post from being even longer, I'm going to assume passing familiarity with terms like ASCII, Unicode, and UTF-8.
</p><p>
The first issue I'll talk about is Unicode normalization, and it's an issue caused largely by Unicode itself. Unicode has two ways of making accented characters: precomposed characters (such as U+00F1, ñ) or a character followed by a combining character (U+006E, n, followed by U+0303, ◌̃). The display of both is the same: ñ versus ñ (read the HTML), and no one would disagree that the share the meaning. To let software detect that they are the same, Unicode prescribes four algorithms to normalize them. These four algorithms are defined on two axes: whether to prefer composed characters (like U+00F1) or prefer decomposed characters (U+006E U+0303), and whether to normalize by canonical equivalence (noting that, for example, U+212A Kelvin sign is equivalent to the Latin majuscule K) or by compatibility (e.g., superscript 2 to a regular 2).
</p><p>
Another issue is one that mostly affects display. Western European languages all use a left-to-right, top-to-bottom writing order. This isn't universal: Semitic languages like Hebrew or Arabic use right-to-left, top-to-bottom; Japanese and Chinese prefer a top-to-bottom, right-to-left order (although it is sometimes written left-to-right, top-to-bottom). It thus becomes an issue as to the proper order to store these languages using different writing orders in the actual text, although I believe the practice of always storing text in "start-to-finish" order, and reversing it for display, is nearly universal.
</p><p>
Now, both of those issues mentioned so far are minor in the grand scheme of things, in that you can ignore them and they will still probably work properly almost all of the time. Most text that is exposed to the web is already normalized to the same format, and web browsers have gotten away with not normalizing CSS or HTML identifiers with only theoretical objections raised. All of the other issues I'm going to discuss are things that cause problems and illustrate why properly internationalizing email is hard.
</p><p>
Another historical mistake of Unicode is one that we will likely be stuck with for decades, and I need to go into some history first. The first Unicode standard dates from 1991, and its original goal then was to collect all of the characters needed for modern transmission, which was judged to need only a 16-bit set of characters. Unfortunately, the needs of ideographic-centric Chinese, Japanese, and Korean writing systems, particularly rare family names, turns out to rather fill up that space. Thus, in 1996, Unicode was changed to permit more characters: 17 planes of 65,536 characters each, of which the original set was termed the "Basic Multilingual Plane" or BMP for short. Systems that chose to adopt Unicode in those intervening 5 years often adopted a 16-bit character model as their standard internal format, so as to keep the benefits of fixed-width character encodings. However, with the change to a larger format, their fixed-width character encoding is no longer fixed-width.
</p><p>
This issue plagues anybody who works with systems that considered internationalization in that unfortunate window, which notably includes prominent programming languages like C#, Java, and JavaScript. Many cross-platform C and C++ programs implicitly require UTF-16 due to its pervasive inclusion into the Windows operating system and common internationalization libraries [3]. Unsurprisingly, non-BMP characters tend to quickly run into all sorts of hangups by unaware code. For example, right now, it is possible to coax Thunderbird to render these characters unusable in, say, your subject string if the subject is just right, and I suspect similar bugs exist in a majority of email applications [4].
</p><p>
For all of the flaws of Unicode [5], there is a tacit agreement that UTF-8 should be the character set to use for anyone not burdened by legacy concerns. Unfortunately, email is burdened by legacy concerns, and the use of 8-bit characters in headers that are not UTF-8 is more prevalent than it ought to be, RFC 6532 notwithstanding. In any case, email explicitly provides for handling a wide variety of alternative character sets without saying which ones should be supported. The <a href="http://www.iana.org/assignments/character-sets/character-sets.xhtml">official list</a> [6] contains about 200 of them (including the <a href="http://tools.ietf.org/html/rfc1428">UNKNOWN-8BIT</a> character set), but not all of them see widespread use. In practice, the ones that definitely need to be supported are the ISO 8859-* and ISO 2022-* charsets, the EUC-* charsets, Windows-* charsets, GB18030, GBK, Shift-JIS, KOI8-{R,U}, Big5, and of course UTF-8. There are two other major charsets that don't come up directly in email but are important for implementing the entire suite of protocols: UTF-7, used in IMAP (more on that later), and Punycode (more on that later, too).
</p><p>
The suite of character sets falls into three main categories. First is the set of fixed-width character sets, most notably ASCII and the ISO 8859 suite of charsets, as well as UCS-2 (2 bytes per character) and UTF-32 (4 bytes per character). Since the major East Asian languages are all ideographic, which require a rather large number of characters to be encoded, fixed-width character sets are infeasible. Instead, many choose to do a variable-width encoding: Shift-JIS lets some characters (notably ASCII characters and half-width katakana) remain a single byte and uses two bytes to encode all of its other characters. UTF-8 can use between 1 byte (for ASCII characters) and 4 bytes (for non-BMP characters) for a single character. The final set of character sets, such as the ISO 2022 ones, use escape sequences to change the interpretation of subsequent characters. As a result, taking the substring of an encoding string can change its interpretation while remaining valid. This will be important later.
</p><p>
Two more problems related to character sets are worth mentioning. The first is the byte-order mark, or BOM, which is used to distinguish whether UTF-16 is written on a little-endian or big-endian machine. It is also sometimes used in UTF-8 to indicate that the text is UTF-8 versus some unknown legacy encoding. It is also not supposed to appear in email, but I have done some experiments which suggest that people use software that adds it without realizing that this is happening. The second issue, unsurprisingly [7], is that for some character sets (Big5 in particular, I believe), not everyone agrees on how to interpret some of the characters.
</p><p>
The largest problem of internationalization that applies in a general sense is the problem of case insensitivity. The 26 basic Latin letters all map nicely to case, having a single uppercase and a single lowercase variant for each letter. This practice doesn't hold in general—languages like Japanese lack even the notion of case, although it does have two kana variants that hold semantic differences. Rather, there are three basic issues with case insensitivity which showcase enough of its problems to make you want to run away from it altogether [8].
</p><p>
The simplest issue is the Greek sigma. Greek has two lowercase variants of the sigma character: σ and ς (the "final sigma"), but a single uppercase variant, Σ. Thus mapping a string s to uppercase and back to lowercase is not equivalent to mapping s directly to lower-case in some cases. Related to this issue is the story of German ß character. This character evolved as a ligature of a long and short 's', and its uppercase form is generally held to be SS. The existence of a capital form is <a href="http://en.wikipedia.org/wiki/Capital_%C3%9F">in some dispute</a>, and Unicode only recently added it (ẞ, if your software supports it). As a result, merely interconverting between uppercase and lowercase versions of a string does not necessarily lead to a simple fixed point. The third issue is the Turkish dotless i (ı), which is the lowercase variant of the ASCII uppercase I character to those who speak Turkish. So it turns out that case insensitivity isn't quite the same across all locales.
</p><p>
Again unsurprisingly in light of the issues, the general tendency towards case-folding or case-insensitive matching in internationalized-aware specifications is to ignore the issues entirely. For example, asking for clarity on the process of case-insensitive matching for IMAP folder names, the response I got was "don't do it." HTML and CSS moved to the cumbersomely-named variant known as "ASCII-subset case-insensitivity", where only the 26 basic Latin letters are mapped to their (English) variants in case. The solution for email is also a verbose variant of "unspecified," but that is only tradition for email (more on this later).
</p><p>
Now that you have a good idea of the general issues, it is time to delve into how the developers of email rose to the challenge of handling internationalization. It turns out that the developers of email have managed to craft one of the most perfect and exquisite examples I have seen of how to completely and utterly fail. The challenges of internationalized emails are so difficult that buggier implementations are probably more common than fully correct implementations, and any attempt to ignore the issue is completely and totally impossible. In fact, the faults of <a href="http://tools.ietf.org/html/rfc2047">RFC 2047</a> are my personal least favorite part of email, and implementing it made me change the design of JSMime more than any other feature. It is probably the single hardest thing to implement correctly in an email client, and it is so broken that another specification was needed to be able to apply internationalization more widely (<a href="http://tools.ietf.org/html/rfc2231">RFC 2231</a>).
</p><p>
The basic problem RFC 2047 sets out to solve is how to reliably send non-ASCII characters across a medium where only 7-bit characters can be reliably sent. The solution that was set out in the original version, <a href="http://tools.ietf.org/html/rfc1342">RFC 1342</a>, is to encode specific strings in an "encoded-word" format: <tt>=?<i>charset</i>?<i>encoding</i>?<i>encoded text</i>?=</tt>. The encoding can either be a 'B' (for Base64) or a 'Q' (for quoted-printable). Except the quoted-printable encoding in this format isn't quite the same quoted-printable encoding used in bodies: the space character is encoded via a '_' character instead, as spaces aren't allowed in encoded-words. Naturally, the use of spaces in encoded-words is common enough to get at least one or two bugs filed a year about Thunderbird not supporting it, and I wonder if this subtle difference between two quoted-printable variants is what causes the prevalence of such emails.
</p><p>
One of my great hates with regard to email is the strict header line length limit. Since the encoded-word form can get naturally verbose, particularly when you consider languages like Chinese that are going to have little whitespace amenable for breaking lines, the ingenious solution is to have adjacent encoded-word tokens separated only by whitespace be treated as the same word. As RFC 6857 kindly summarizes, "whitespace behavior is somewhat unpredictable, in practice, when multiple encoded words are used." RFC 6857 also suggests that the requirement to limit encoded words to only 74 characters in length is also rather meaningless in practice.
</p><p>
A more serious problem arises when you consider the necessity of treating adjacent encoded-word tokens as a single unit. This one is so serious that it reaches the point where <em>all of your options would break somebody</em>. When implementing an RFC 2047 encoding algorithm, how do you write the code to break up a long span of text into multiple encoded words without ever violating the specification? The naive way of doing so is to encode the text once in one long string, and then break it into checks which are then converted into the encoded-word form as necessary. This is, of course, wrong, as it breaks two strictures of RFC 2047. The first is that you cannot split the middle of multibyte characters. The second is that mode-switching character sets must return to ASCII by the end of a single encoded-word [9]. The smarter way of building encoded-words is to encode words by trying to figure out how much text can be encoded before needing to switch, and breaking the encoded-words when length quotas are exceeded. This is also wrong, since you could end up violating the return-to-ASCII rule if your don't double-check your converters. Also, if UTF-16 is used as the basis for the string before charset conversion, the encoder stands a good chance of splitting up creating unpaired surrogates and a giant mess as a result.
</p><p>
For JSMime, the algorithm I chose to implement is specific to UTF-8, because I can use a property of the UTF-8 implementation to make encoding fast (every octet is looked at exactly three times: once to convert to UTF-8, once to count to know when to break, and once to encode into base64 or quoted-printable). The property of UTF-8 is that the second, third, and fourth octets of a multibyte character all start with the same two bits, and those bits never start the first octet of a character. Essentially, I convert the entire string to a binary buffer using UTF-8. I then pass through the buffer, keeping counters of the length that the buffer would be in base64 form and in quoted-printable form. When both counters are exceeded, I back up to the beginning of the character, and encode that entire buffer in a word and then move on. I made sure to test that I don't break surrogate characters by making liberal use of the non-BMP character U+1F4A9 [10] in my encoding tests.
</p><p>
The sheer ease of writing a broken encoder for RFC 2047 means that broken encodings exist in the wild, so an RFC 2047 decoder needs to support some level of broken RFC 2047 encoding. Unfortunately, to "fix" different kinds of broken encodings requires different support for decoders. Treating adjacent encoded-words as part of the same buffer when decoding makes split multibyte characters work properly but breaks non-return-to-ASCII issues; if they are decoded separately the reverse is true. Recovering issues with isolated surrogates is at best time-consuming and difficult and at worst impossible.
</p><p>
Yet another problem with the way encoded-words are defined is that they are defined as specific tokens in the grammar of structured address fields. This means that you can't hide RFC 2047 encoding or decoding as a final processing step when reading or writing messages. Instead you have to do it during or after parsing (or during or before emission). So the parser as a result becomes fully intertwined with support for encoded-words. Converting a fully UTF-8 message into a 7-bit form is thus a non-trivial operation: there is a specification solely designed to discuss how to do such downgrading, <a href="http://tools.ietf.org/html/rfc6857">RFC 6857</a>. It requires deducing what structure a header has, parsing that harder, and then reencoding the parsed header. This sort of complicated structure makes it much harder to write general-purpose email libraries: the process of emitting a message basically requires doing a generic UTF-8-to-7-bit conversion. Thus, what is supposed to be a more implementation detail of how to send out a message ends up permeating the entire stack.
</p><p>
Unfortunately, the developers of RFC 2047 were a bit too clever for their own good. The specification limits the encoded-words to occurring only inside of phrases (basically, display names for addresses), unstructured text (like the subject), or comments (…). I presume this was done to avoid requiring parsers to handle internationalization in email addresses themselves or possibly even things like MIME boundary delimiters. However, this list leaves out one common source of internationalized text: filenames of attachments. This was ultimately patched by <a href="http://tools.ietf.org/html/rfc2231">RFC 2231</a>.
</p><p>
RFC 2231 is by no means a simple specification, since it attempts to solve three problems simultaneously. The first is the use of non-ASCII characters in parameter values. Like RFC 2047, the excessively low header line length limit causes the second problem, the need to wrap parameter values across multiple line lengths. As a result, the encoding is complicated (it takes more lines of code to parse RFC 2231's new features alone than it does to parse the basic format [11]), but it's not particularly difficult.
</p><p>
The third problem RFC 2231 attempts to solve is a rather different issue altogether: it tries to conclusively assign a language tag to the encoded text and also provides a "fix" for this to RFC 2047's encoded-words. The stated rationale is to be able to have screen readers read the text aloud properly, but the other (much more tangible) benefit is to ameliorate the issues of Unicode's Han unification by clearly identifying if the text is Chinese, Japanese, or Korean. While it sounds like a nice idea, it suffers from a major flaw: there is no way to use this data without converting internal data structures from using flat strings to richer representations. Another issue is that actually setting this value correctly (especially if your goal is supporting screen readers' pronunciations) is difficult if not impossible. Fortunately, this is an entirely optional feature; though I do see very little email that needs to be concerned about internationalization, I have yet to find an example of someone using this in the wild.
</p><p>
If you're the sort of person who finds properly writing internationalized text via RFC 2231 or RFC 2047 too hard (or you don't realize that you need to actually worry about this sort of stuff), and you don't want to use any of the several dozen MIME libraries to do the hard stuff for you, then you will become the bane of everyone who writes email clients, because you've just handed us email messages that have 8-bit text in the headers. At which point everything goes mad, because we have no clue what charset you just used. Well, RFC 6532 says that headers are supposed to be UTF-8, but with the specification being only 19 months old and part of a system which is still (to my knowledge) not supported by any major clients, this should be taken with a grain of salt. UTF-8 has the very nice property that text that is valid UTF-8 is highly unlikely to be any other charset, even if you start considering the various East Asian multibyte charsets. Thus you can try decoding under the assumption that is UTF-8 and switch to a designated fallback charset if decoding fails. Of course, knowing which designated fallback to use is a different matter entirely.
</p><p>
Stepping outside email messages themselves, internationalization is still a concern. IMAP folder names are another well-known example. RFC 3501 specified that mailbox names should be in a modified version of UTF-7 in an awkward compromise. To my knowledge, this is the only remaining significant use of UTF-7, as many web browsers disabled support due to its use in security attacks. RFC 6855, another recent specification (6 months old as of this writing), finally allows UTF-8 mailbox names here, although it too is not yet in widespread usage.
</p><p>
You will note missing from the list so far is email addresses. The topic of email addresses is itself worthy of lengthy discussion, but for the purposes of a discussion on internationalization, all you need to know is that, according to RFCs 821 and 822 and their cleaned-up successors, everything to the right of the '@' is a domain name and everything to the left is basically an opaque ASCII string [12]. It is here that internationalization really runs headlong into an immovable obstacle, for the email address has become the <i>de facto</i> unique identifier of the web, and everyone has their own funky ideas of what an email address looks like. As a result, the motto of "be liberal in what you accept" really breaks down with email addresses, and the amount of software that needs to change to accept internationalization extends far beyond the small segment interested only in the handling of email itself. Unfortunately, the relative newness of the latest specifications and corresponding lack of implementations means that I am less intimately familiar with this aspect of internationalization. Indeed, the impetus for this entire blogpost was a day-long struggle with trying to ascertain when two email addresses are the same if internationalized email address are involved.
</p><p>
The email address is split nicely by the '@' symbol, and internationalization of the two sides happens at two different times. Domains were internationalized first, by <a href="http://tools.ietf.org/html/rfc3490">RFC 3490</a>, a specification with the mouthful of a name "Internationalizing Domain Names in Applications" [13], or IDNA2003 for short. I mention the proper name of the specification here to make a point: the underlying protocol is completely unchanged, and all the work is intended to happen at roughly the level of <tt>getaddrinfo</tt>—the internal DNS resolver is supposed to be involved, but the underlying DNS protocol and tools are expected to remain blissfully unaware of the issues involved. That I mention the year of the specification should tell you that this is going to be a bumpy ride.
</p><p>
An internationalized domain name (IDN for short) is a domain name that has some non-ASCII characters in it. Domain names, according to DNS, are labels terminated by '.' characters, where each label may consist of up to 63 characters. The repertoire of characters are the ASCII alphanumerics and the '-' character, and labels are of course case-insensitive like almost everything else on the Internet. Encoding non-ASCII characters into this small subset while meeting these requirements is difficult for other contemporary schemes: UTF-7 uses Base64, which means 'A' and 'a' are not equivalent; percent-encoding eats up characters extremely quickly. So IDN use a different specification for this purpose, called Punycode, which allows for a dense but utterly unreadable encoding. The basic algorithm of encoding an IDN is to take the input string, apply case-folding, normalize using NFKC, and then encode with Punycode.
</p><p>
Case folding, as I mentioned several paragraphs ago, turns out to have some issues. The ß and ς characters were the ones that caused the most complaints. You see, if you were to register, say, www.weiß.de, you would actually be registering www.weiss.de. As there is no indication of Punycode involved in the name, browsers would show the domain in the ASCII variant. One way of fixing this problem would be to work with browser vendors to institute a "preferred name" specification for websites (much like there exists one for the little icons next to page titles), so that the world could know that the proper capitalization is of course www.GoOgle.com instead of www.google.com. Instead, the German and Greek registrars pushed for a change to IDNA, which they achieved in 2010 with IDNA2008.
</p><p>
IDNA2008 is defined principally in RFCs 5890-5895 and <a href="http://www.unicode.org/reports/tr46/">UTS #46</a>. The principal change is that the normalization step no longer exists in the protocol and is instead supposed to be done by applications, in a possibly locale-specific manner, before looking up the domain name. One reason for doing this was to eliminate the hard dependency on a specific, outdated version of Unicode [14]. It also helps fix things like the Turkish dotless I issue, in theory at least. However, this different algorithm causes some domains to be processed differently from IDNA2003. UTS #46 specifies a "compatibility mode" which changes the algorithm to match IDNA2003 better in the important cases (specifically, ß, ς, and ZWJ/ZWNJ), with a note expressing the hope that this will eventually become unnecessary. To handle the lack of normalization in the protocol, registrars are asked to automatically register all classes of equivalent domain names at the same time. I should note that most major browsers (and email clients, if they implement IDN at all) are still using IDNA2003: an easy test of this fact is to attempt to go to <a href="http://☃.net/">☃.net</a>, which is valid under IDNA2003 but not IDNA2008.
</p><p>
Unicode text processing is often vulnerable to an attack known as the "homograph attack." In most fonts, the Greek omicron and the Latin miniscule o will be displayed in exactly the same way, so an attacker could pretend to be from, say, Google while instead sending you to Gοogle—I used Latin in the first word and Greek in the second. The standard solution is to only display the Unicode form (and not the Punycode form) where this is not an issue; Firefox and Opera display Unicode only for a whitelist of registrars with acceptable polices, Chrome and Internet Explorer only permits scripts that the user claims to read, and Safari only permits scripts that don't permit the homograph attack (i.e., not Cyrillic or Greek). (Note: this information I've summarized from <a href="http://www.chromium.org/developers/design-documents/idn-in-google-chrome">Chromium's documentation</a>; forward any complaints of out-of-date information to them).
</p><p>
IDN satisfies the needs of internationalizing the second half of an email address, so a working group was commissioned to internationalize the first one. The result is EAI, which was first experimentally specified in RFCs 5335-5337, and the standards themselves are found in RFCs 6530-6533 and 6855-6858. The primary difference between the first, experimental version and the second, to-be-implemented version is the removal of attempts to downgrade emails in the middle of transit. In the experimental version, provisions were made to specify with every internalized address an alternate, fully ASCII address to which a downgraded message could be sent if SMTP servers couldn't support the new specifications. These were removed after the experiment found that such automatic downgrading didn't work as well as hoped.
</p><p>
With automatic downgrading removed from the underlying protocol, the onus is on people who generate the emails—mailing lists and email clients—to figure out who can and who can't receive messages and then downgrade messages as appropriate for the recipients of the message. However, the design of SMTP is such that it is impossible to automatically determine if the client can receive these new kinds of messages. Thus, the options are to send them and hope that it works or to rely on the (usually clueless) user to inform you if it works. Clearly an unpalatable set of options, but it is one that can't be avoided due to protocol design.
</p><p>
The largest change of EAI is that the local parts of addresses are specified as a sequence of UTF-8 characters, omitting only the control characters [15]. The working group responsible for the specification adamantly refused to define a Unicode-to-ASCII conversion process, and thus a mechanism to make downgrading work smoothly, for several reasons. First, they didn't want to specify a prefix which could change the meaning of existing local-parts (the structure of local-parts is much less discoverable than the structure of all domain names). Second, they felt that the lack of support for displaying the Unicode variants of Punycode meant that users would have a much worse experience. Finally, the transition period would be hopefully short (although messy), so designing a protocol that supports that short period would worsen it in the long term. Considering that, at the moment of writing, only one of the major SMTP implementations has even a bug filed to support it, I think the working group underestimates just how long transition periods can take.
</p><p>
As far as changes to the message format go, that change is the only real change, considering how much effort is needed to opt-in. Yes, headers are now supposed to be UTF-8, but, in practice, every production MIME parser needs to handle 8-bit characters in headers anyways. Yes, message/global can have MIME encoding applied to it (unlike message/rfc822), but, in practice, you already need to assume that people are going to MIME-encode message/rfc822 in violation of the specification. So, in practice, the changes needed to a parser are to add message/global as an alias to message/rfc822 [16] and possibly tweaking some charset detection heuristics to prefer UTF-8. I would very much have liked the restriction on header line length removed, but, alas, the working group did not feel moved to make those changes. Still, I look forward to the day when I never have to worry about encoding text into RFC 2047 encoded-words.
</p><p>
IMAP, POP, and SMTP are also all slightly modified to take account of the new specifications. Specifically, internationalized headers are supposed to be opt-in only—SMTP are supposed to reject sending to these messages if it doesn't support them in the first place, and IMAP and POP are supposed to downgrade messages when requested unless the client asks for them to not be. As there are no major server implementations yet, I don't know how well these requirements will be followed, especially given that most of the changes already need to be tolerated by clients in practice. The experimental version of internationalization specified a format which would have wreaked havoc to many current parsers, so I suspect some of the strict requirements may be a holdover from that version.
</p><p>
And thus ends my foray into email internationalization, a collection of bad solutions to hard problems. I have probably done a poor job of covering the complete set of inanities involved, but what I have covered are the ones that annoy me the most. This certainly isn't the last I'll talk about the impossibility of message parsing either, but it should be enough at least to convince you that you really don't want to write your own message parser.
</p><p>
[1] Date/time, numbers, and currency are the other major aspects of internalization.<br>
[2] I am a native English speaker who converses with other people almost completely in English. That said, I can comprehend French, although I am not familiar with the finer points that come with fluency, such as collation concerns.<br>
[3] C and C++ have a built-in internationalization and localization API, derived from POSIX. However, this API is generally unsuited to the full needs of people who actually care about these topics, so it's not really worth mentioning.<br>
[4] The basic algorithm to encode RFC 2047 strings for any charset are to try to shift characters into the output string until you hit the maximum word length. If the internal character set for Unicode conversion is UTF-16 instead of UTF-32 and the code is ignorant of surrogate concerns, then this algorithm could break surrogates apart. This is exactly how the bug is triggered in Thunderbird.<br>
[5] I'm not discussing Han unification, which is arguably the single most controversial aspect of Unicode.<br>
[6] Official list here means the official set curated by IANA as valid for use in the charset="" parameter. The actual set of values likely to be acceptable to a majority of clients is rather different.<br>
[7] If you've read this far and find internationalization inoperability surprising, you are either incredibly ignorant or incurably optimistic.<br>
[8] I'm not discussing collation (sorting) or word-breaking issues as this post is long enough already. Nevertheless, these also help very much in making you want to run away from internationalization.<br>
[9] I actually, when writing this post, went to double-check to see if Thunderbird correctly implements return-to-ASCII in its encoder, which I can only do by running tests, since I myself find its current encoder impenetrable. It turns out that it does, but it also looks like if we switched conversion to ICU (as many bugs suggest), we may break this part of the specification, since I don't see the ICU converters switching to ASCII at the end of conversion.<br>
[10] Chosen as a very adequate description of what I think of RFC 2047. Look it up if you can't guess it from context.<br>
[11] As measured by implementation in JSMime, comments and whitespace included. This is biased by the fact that I created a unified lexer for the header parser, which rather simplifies the implementation of the actual parsers themselves.<br>
[12] This is, of course a gross oversimplification, so don't complain that I'm ignoring domain literals or the like. Email addresses will be covered later.<br>
[13] A point of trivia: the 'I' in IDNA2003 is expanded as "Internationalizing" while the 'I' in IDNA2008 is for "Internationalized."<br>
[14] For the technically-minded: IDNA2003 relied on a hard-coded list of banned codepoints in processing, while IDNA2008 derives its lists directly from Unicode codepoint categories, with a small set of hard-coded exceptions.<br>
[15] Certain ASCII characters may require the local-part to be quoted, of course.<br>
[16] Strictly speaking, message/rfc822 remains all-ASCII, and non-ASCII headers need message/global. Given the track record of message/news, I suspect that this distinction will, in practice, not remain for long.Joshua Cranmerhttp://www.blogger.com/profile/02760318962075959780noreply@blogger.com408tag:blogger.com,1999:blog-5947958124349996271.post-38064322351399676192013-09-14T18:50:00.000-04:002013-09-14T18:50:17.785-04:00Why email is hard, part 1: architectureWhich is harder, writing an email client or writing a web browser? Several years ago, I would have guessed the latter. Having worked on an email client for several years, I am now more inclined to guess that email is harder, although I never really worked on a web browser, so perhaps it's just bias. Nevertheless, HTML comes with a specification that tells you how to parse crap that pretends to be HTML; email messages come with no such specification, which forces people working with email to guess based on other implementations and bug reports. To vent some of my frustration with working with email, I've decided to post some of my thoughts on what email did wrong and why it is so hard to work with. Since there is so much to talk about, instead of devoting one post to it, I'll make it an ongoing series with occasional updates (i.e., updates will come out when I feel like it, so don't bother asking).
</p><p>
First off, what do I mean by an email client? The capabilities of, say, Outlook versus Gaia Email versus Thunderbird are all wildly different, and history has afforded many changes in support. I'll consider anything that someone might want to put in an email client as fodder for discussion in this series (so NNTP, RSS, LDAP, CalDAV, and maybe even IM stuff might find discussions later). What I won't consider are things likely to be found in a third-party library, so SSL, HTML, low-level networking, etc., are all out of scope, although I may mention them where relevant in later posts. If one is trying to build a client from scratch, the bare minimum one needs to understand first is the basic message formatting, MIME (which governs attachments), SMTP (email delivery), and either POP or IMAP (email receipt). Unfortunately, each of these requires cross-referencing a dozen RFCs individually when you start considering optional or not-really-optional features.
</p><p>
The current email architecture we work with today doesn't have a unique name, although "Internet email" [1] or "SMTP-based email" are probably the most appropriate appellations. Since there is only one in use in modern times, there is no real need to refer to it by anything other than "email." The reason for the use of SMTP in lieu of any other major protocol to describe the architecture is because the heart of the system is motivated by the need to support SMTP, and because SMTP is how email is delivered across organizational boundaries, even if other protocols (such as LMTP) are used internally.
</p><p>
Some history of email, at least that lead up to SMTP, is in order. In the days of mainframes, mail generally only meant communicating between different users on the same machine, and so a bevy of incompatible systems started to arise. These incompatible systems grew to support connections with other computers as networking computers became possible. The ARPANET project brought with it an attempt to standardize mail transfer on ARPANET, separated into two types of documents: those that standardized message formats, and those that standardized the message transfer. These would eventually culminate in <a href="http://tools.ietf.org/html/rfc822">RFC 822</a> and <a href="http://tools.ietf.org/html/rfc821">RFC 821</a>, respectively. SMTP was designed in the context of ARPANET, and it was originally intended primarily to standardize the messages transferred only on this network. As a result, it was never intended to become <em>the</em> standard for modern email.
</p><p>
The main competitor to SMTP-based email that is worth discussing is X.400. X.400 was at one time expected to be the eventual global email interconnect protocol, and interoperability between SMTP and X.400 was a major focus in the 1980s and 1990s. SMTP has a glaring flaw, to those who work with it, in that it is not so much designed as evolved to meet new needs as they came up. In contrast, X.400 was designed to account for a lot of issues that SMTP hadn't dealt with yet, and included arguably better functionality than SMTP. However, it turned out to be a colossal failure, although theories differ as to why. The most convincing to me boils down to X.400 being developed at a time of great flux in computing (the shift from mainframes to networked PCs) combined with a development process that was ill-suited to reacting quickly to these changes.
</p><p>
I mentioned earlier that SMTP eventually culminates in RFC 821. This is a slight lie, for one of the key pieces of the Internet, and a core of the modern email architecture, didn't exist. That is DNS, which is the closest thing the Internet has to X.500 (a global, searchable directory of everything). Without DNS, figuring out how to route mail via SMTP is a bit of a challenge (hence why SMTP allowed explicit source routing, deprecated post-DNS in RFC 2821). The documents which lay out how to use DNS to route are <a href="http://tools.ietf.org/html/rfc974">RFC 974</a>, <a href="http://tools.ietf.org/html/rfc1035">RFC 1035</a>, and <a href="http://tools.ietf.org/html/rfc1123">RFC 1123</a>. So it's fair to say that RFC 1123 is really the point at which modern SMTP was developed.
</p><p>
But enough about history, and on to the real topic of this post. The most important artifact of SMTP-based architecture is that different protocols are used to send email from the ones used to read email. This is both a good thing and a bad thing. On the one hand, it's easier to experiment with different ways of accessing mailboxes, or only supporting limited functionality where such is desired. On the other, the need to agree on a standard format still keeps all the protocols more or less intertwined, and it makes some heavily-desired features extremely difficult to implement. For example, there is still, thirty years later, no feasible way to send a mail and save it to a "Sent" folder on your IMAP mailbox without submitting it twice [2].
</p><p>
The greatest flaws in the modern architecture, I think, lie in particular in a bevy of historical design mistakes which remain unmitigated to this day, in particular in the base message format and MIME. Changing these specifications is not out of the question, but the rate at which the changes become adopted is agonizingly slow, to the point that changing is generally impossible unless necessary. Sending outright binary messages was proposed as experimental in 1995, proposed as a standard in 2000, and still remains relatively unsupported: the BINARYMIME SMTP keyword only exists on one of my 4 SMTP servers. Sending non-ASCII text is potentially possible, but it is still not used in major email clients to my knowledge (searching for "8BITMIME" leads to the top results generally being "how do I turn this off?"). It will be interesting to see how email address internationalization is handled, since it's the first major overhaul to email since the introduction of MIME—the first major overhaul in 16 years. Intriguingly enough, the NNTP and Usenet communities have shown themselves to be more adept to change: sending 8-bit Usenet messages generally works, and yEnc would have been a worthwhile addition to MIME if its author had ever attempted to push it through. His decision not to (with the weak excuses he claimed) is emblematic of the resistance of the architecture to change, even in cases where such change would be pretty beneficial.
</p><p>
My biggest complaint with the email architecture isn't actually really a flaw in the strict sense of the term but rather a disagreement. The core motto of email could perhaps be summed up with "Be liberal in what you accept and conservative in what you send." Now, I come from a compilers background, and the basic standpoint in compilers is, if a user does something wrong, to scream at them for being a bloody idiot and to reject their code. Actually, there's a tendency to do that even if they do something technically correct but possibly unintentionally wrong. I understand why people dislike this kind of strict checking, but I personally consider it to be a feature, not a bug. My experience with attempting to implement MIME is that accepting what amounts to complete crap not only means that everyone has to worry about parsing the crap, but it actually ends up encouraging it. The attitude people get in bugs starts becoming "this is supported by <insert other client>, and your client is broken for not supporting it," even when pointed out that their message is in flagrant violation of the specification. As I understand it, HTML 5 has the luxury of specifying a massively complex parser that makes <tt>/dev/urandom</tt> in theory reliably parsed across different implementations, but there is no such similar document for the modern email message. But we still have to deal with the utter crap people claim is a valid email message. Just this past week, upon sifting through my spam folder, I found a header which is best described as <tt>=?UTF-8?Q? <i>ISO-8859-1, non-ASCII text</i> ?=</tt> (spaces included). The only way people are going to realize that their tools are producing this kind of crap is if their tools stop working altogether.
</p><p>
These two issues come together most spectacularly when <a href="http://tools.ietf.org/html/rfc2047">RFC 2047</a> is involved. This is worth a blog post by itself, but the very low technically-not-but-effectively-mandatory limit on the header length (to aide people who read email without clients) means that encoded words need to be split up to fit on header lines. If you're not careful, you can end up splitting multibyte characters between different encoded words. This unfortunately occurs in practice. Properly handling it in my new parser required completely reflowing the design of the innermost parsing function and greatly increasing implementation complexity. I would estimate that this single attempt to "gracefully" handle wrong-but-of-obvious-intent scenario is worth 15% or so of the total complexity of decoding RFC 2047-encoded text.
</p><p>
There are other issues with modern email, of course, but all of the ones that I've collected so far are not flaws in the architecture as a whole but rather flaws of individual portions of the architecture, so I'll leave them for later posts.
</p><p>
[1] The capital 'I' in "Internet email" is important, as it's referring to the "Internet" in "Internet Standard" or "Internet Engineering Task Force." Thus, "Internet email" means "the email standards developed for the Internet/by the IETF" and not "email used on the internet."<br>
[2] Yes, I know about BURL. It doesn't count. Look at who supports it: almost nobody.Joshua Cranmerhttp://www.blogger.com/profile/02760318962075959780noreply@blogger.com10tag:blogger.com,1999:blog-5947958124349996271.post-4043424618607067242013-05-07T21:46:00.000-04:002013-05-07T21:46:47.929-04:00Understanding the comm-central build systemAmong the build systems peer, I am very much a newcomer. Despite working with Thunderbird for over 5 years, I've only grown to understand the comm-central build system in gory detail in the past year. Most of my work before then was in trying to get random projects working; understanding it more thoroughly is a result of attempting to eliminate the need for comm-central to maintain its own build system. The goal of <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=847009">moving our build system description to a series of moz.build files</a> has made this task much more urgent.
</p><p>
At a high level, the core of the comm-central build system is not a single build system but rather three copies of the same build system. In simple terms, there's a matrix on two accesses: which repository does the configuration of code (whose config.status invokes it), and which repository does the build (whose rules.mk is used). Most code is configured and built by mozilla-central. That comm-central code which is linked into libxul is configured by mozilla-central but built by comm-central. <tt>tier_app</tt> is configured and built by comm-central. This matrix of possibilities causes interesting bugs—like the bustage caused by the <tt>XPIDLSRCS</tt> migration, or issues I'm facing working with xpcshell manifests—but it is probably possible to make all code get configured by mozilla-central and eliminate several issues for once and all.
</p><p>
With that in mind, here is a step-by-step look at how the amorphous monster that is the comm-central build system works:
</p><h5>python client.py checkout</h5><p>
And comm-central starts with a step that is unknown in mozilla-central. Back when everyone was in CVS, the process of building started with "check out client.mk from the server, set up your mozconfig, and then run <kbd>make -f client.mk checkout</kbd>." The checkout would download exactly the directories needed to build the program you were trying to build. When mozilla-central moved to Mercurial, the only external projects in the tree that Firefox used were NSPR and NSS, both of which were set up to pull from a specific revision. The decision was made to import NSPR and NSS as snapshots on a regular basis, so there was no need for the everyday user to use this feature. Thunderbird, on the other hand, pulled in the LDAP code externally, as well as mozilla-central, while SeaMonkey also pulls in the DOM inspector, Venkman, and Chatzilla as extensions. Importing a snapshot was not a tenable option for mozilla-central, as it updates at an aggressive rate, so the functionality of checkout was ported to comm-central in a replacement python fashion.
</p><h5>./configure [comm-central]</h5><p>
The general purpose of configure is to discover the build system and enable or disable components based on options specified by the user. This results in a long list of variables which is read in by the build system. <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=846540">Recently</a>, I changed the script to eliminate the need to port changes from mozilla-central. Instead, this script reads in a few variables and tweaks them slightly to produce a modified command line to call mozilla-central's configure...
</p><h5>./configure [mozilla-central]</h5><p>
... which does all the hard work. There are hooks in the configure script here to run a few extra commands for comm-central's need (primarily adding a few variables and configuring LDAP). This is done by running a bit of m4 over another file and invoking that as a shell script; the m4 is largely to make it look and feel "like" autoconf. At the end of the line, this dumps out all of the variables to a file called <tt>config.status</tt>; how these get configured in the script is not interesting.
</p><h5>./config.status [mozilla/comm-central]</h5><p>
But config.status is. At this point, we enter the mozbuild world and things become very insane; failure to appreciate what goes on here is a very good way to cause extended bustage for comm-central. The mozbuild code essentially starts at a directory and exhaustively walks it to build a map of all the code. One of the tasks of comm-central's configure is to alert mozbuild to the fact that some of our files use a different build system. We, however, also carefully hide some of our files from mozbuild, so we run another copy of config.status again to add in some more files (<tt>tier_app</tt>, in short). This results in our code having two different notions of how our build system is split, and was never designed that way. Originally, mozilla-central had no knowledge of the existence of comm-central, but some changes made in the Firefox 4 timeframe suddenly required Thunderbird and SeaMonkey to link all of the mailnews code into libxul, which forced this contorted process to come about.
</p><h5>make</h5><p>
Now that all of the Makefiles have bee generated, building can begin. The first directory entered is the top of comm-central, which proceeds to immediately make all of mozilla-central. How mozilla-central builds itself is perhaps an interesting discussion, but not for this article. The important part is that partway through building, mozilla-central will be told to make <tt>../mailnews</tt> (or one of the other few directories). Under recursive make, the only way to "tell" which build system is being used is by the directory that the <tt>$(DEPTH)</tt> variable is pointing to, since <tt>$(DEPTH)/config/config.mk</tt> and <tt>$(DEPTH)/config/rules/mk</tt> are the files included to get the necessary rules. Since mozbuild was informed very early on that comm-central is special, the variables it points to in comm-central are different from those in mozilla-central—and thus comm-central's files are all invariably built with the comm-central build system despite being built from mozilla-central.
</p><p>
However, this is not true of all files. Some of the files, like the <tt>chat</tt> directory are never mentioned to mozilla-central. Thus, after the comm-central top-level build completes building mozilla-central, it proceeds to do a full build under what it thinks is the complete build system. It is here that later hacks to get things like xpcshell tests working correctly are done. Glossed over in this discussion is the fun process of tiers and other dependency voodoo tricks for a recursive make.
</p><h4>The future</h4><p>
With all of the changes going on, this guide is going to become obsolete quickly. I'm experimenting with eliminating one of our three build system clones by making all comm-central code get configured by mozilla-central, so that mozbuild gets a truly global view of what's going on—which would help not break comm-central for things like <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=869635">eliminating the master xpcshell manifest</a>, or <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=850380">compiling IDLs in parallel</a>. The long-term goal, of course, is to eliminate the ersatz comm-central build system altogether, although the final setup of how that build system works out is still not fully clear, as I'm still in the phase of "get it working when I symlink everything everywhere."Joshua Cranmerhttp://www.blogger.com/profile/02760318962075959780noreply@blogger.com6tag:blogger.com,1999:blog-5947958124349996271.post-14346984339711299812013-04-10T17:00:00.000-04:002013-04-10T17:00:35.680-04:00TBPLWhen running final tests for my latest patch queue, I discovered that someone has apparently added a new color to the repertoire: a hot pink. So I now present to you a snapshot of TBPL that uses all the colors except gray (running), gray (pending), and black (I've never seen this one):<br>
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgV0x28KPHf3_xcHgiMt3-_4vYqBj61p6Gy0TEBHrxKz_i4KkV5SAucZg4J1Q5TWmXXZDrSZsbSCaZFQNOeGJvGZIvTRVUZhOGkKnXgOXHDGEvRtVoy5j5ZZfwNBYyKoDr34CckUMJCFWAU/s320/tbpl.png" />Joshua Cranmerhttp://www.blogger.com/profile/02760318962075959780noreply@blogger.com3tag:blogger.com,1999:blog-5947958124349996271.post-73059227524601303602013-04-07T17:47:00.001-04:002013-04-07T17:47:24.830-04:00JSMime status updateThis post is admittedly long overdue, but I kept wanting to delay this post until I actually had patches up for review. But I have the tendency to want to post nothing until I verify that the entire pipeline consisting of over 5,000 lines of changes is fully correct and documented. However, I'm now confident in all but roughly three of my major changes, so patch documentation and redistribution is (hopefully) all that remains before I start saturating all of the reviewers in Thunderbird with this code. An ironic thing to note is that these changes are actually largely a sidetrack from my original goal: I wanted to land my charset-conversion patch, but I first thought it would be helpful to test with <tt>nsIMimeConverter</tt> using the new method, which required me to implement header parsing, which is best tested with <tt>nsIMsgHeaderParser</tt>, which turns out to have needed very major changes.
</p><p>
As you might have gathered, I am getting ready to land a major set of changes. This set of changes is being tracked in bugs <a href="http://bugzilla.mozilla.org/show_bug.cgi?id=790855">790855</a>, <a href="http://bugzilla.mozilla.org/show_bug.cgi?id=842632">842632</a>, and <a href="http://bugzilla.mozilla.org/show_bug.cgi?id=858337">858337</a>. These patches are implementing structured header parsing and emission, as well as RFC 2047 decoding and encoding. My goal still remains to land all of these changes by Thunderbird 24, reviewers permitting.
</p><p>
The first part of JSMime landed back in Thunderbird 21, so anyone using the betas is already using part of it. One of the small auxiliary interfaces (<tt>nsIMimeHeaders</tt>) was switched over to the JS implementation instead of libmime's implementation, as well as the ad-hoc ones used in our test suites. The currently pending changes would use JSMime for the other auxiliary interfaces, <tt>nsIMimeConverter</tt> (which does RFC 2047 processing) and <tt>nsIMsgHeaderParser</tt> (which does structured processing of the addressing headers). The changes to the latter are very much API-breaking, requiring me to audit and fix every single callsite in all of comm-central. On the plus side, thanks to my changes, I know I will incidentally be fixing several bugs such as quoting issues in the compose windows, a valgrind error in <tt>nsSmtpProtocol.cpp</tt>, or the space-in-2047-encoded-words issue.
</p><p>
It's not all the changes, although being able to outright remove 2000 lines of libmime is certainly a welcome change. The brunt of libmime remains the code that is related to the processing of email body parts into the final email display method, which is the next target of my patches and which I originally intended to fix before I got sidetracked. Getting sidetracked isn't altogether a bad thing, since, for the first time, it lets me identify things that can be done in parallel with this work.
</p><p>
A useful change I've identified that is even more invasive than everything else to date would be to alter our view of how message headers work. Right now, we tend to retrieve headers (from, say, <tt>nsIMsgDBHdr</tt>) as strings, where the consumer will use a standard API to reparse them before acting on their contents. A saner solution is to move the structured parsing into the retrieval APIs, by making an <tt>msgIStructuredHeaders</tt> interface, retrievable from <tt>nsIMsgDBHdr</tt> and <tt>nsIMsgCompFields</tt> from which you can manipulate headers in their structured form instead of their string from. It's even more useful on <tt>nsIMsgCompFields</tt>, where keeping things in structured form as long as possible is desirable (I particularly want to kill <tt>nsIMsgCompFields.splitRecipients</tt> as an API).
</p><p>
Another useful change is that our underlying parsing code can properly handle groups, which means we can start using groups to handle mailing lists in our code instead of…the mess we have now. The current implementation sticks mailing lists as individual addresses to be expanded by code in the middle of the compose sequence, which is fragile and suboptimal.
</p><p>
The last useful independent change I can think of is rewriting the addressing widget in the compose frontend to store things internally in a structured form instead of the MIME header kludge it currently uses; this kind of work could also be shared with the similar annoying mailing list editing UI.
</p><p>
As for myself, I will be working on the body part conversion process. I haven't yet finalized the API that extensions will get to use here, as I need to do a lot of playing around with the current implementation to see how flexible it is. The last two entry points into libmime, the stream converter and Gloda, will first be controlled by preference, so that I can land partially-working versions before I land everything that is necessary. My goal is to land a functionality-complete implementation by Thunderbird 31 (i.e., the next ESR branch after 24), so that I can remove the old implementation in Thunderbird 32, but that timescale may still be too aggressive.Joshua Cranmerhttp://www.blogger.com/profile/02760318962075959780noreply@blogger.com4tag:blogger.com,1999:blog-5947958124349996271.post-84336366623716659362013-02-15T23:39:00.001-05:002013-02-15T23:39:27.395-05:00Why software monocultures are badIf you do anything with web development, you are probably well aware that Opera recently announced that it was ditching its Presto layout engine and switching to Webkit. The reception of the blogosphere to this announcement has been decidedly mixed, but I am disheartened by the news for a very simple reason. The loss of one of the largest competitors in the mobile market risks entrenching a monoculture in web browsing.
</p><p>
Now, many people have attempted to argue against this risk by one of three arguments: that Webkit already is a monoculture on mobile browsing; that Webkit is open-source, so it can't be a "bad" monoculture; or that Webkit won't stagnant, so it can't be a "bad" monoculture. The first argument is rather specious, since it presumes that once a monoculture exists it is pointless to try to end it—walk through history, it's easier to cite examples that were broken than ones that weren't. The other two arguments are more dangerous, though, because they presume that a monoculture is bad only because of who is in charge of it, not because it is a monoculture.
</p><p>
The real reason why monocultures are bad are not because the people in control do bad things with it. It's because their implementations—particularly including their bugs—becomes the standards instead of the specifications themselves. And to be able to try to crack into that market, you have to be able to reverse engineer bugs. Reverse engineering bugs, even in open-source code, is far from trivial. Perhaps it's clearer to look at the problems of monoculture by case study.
</p><p>
In the web world, the most well-known monoculture is that of IE 6, which persisted as the sole major browser for about 4 years. One long-lasting ramification of IE is the necessity of all layout engines to support a <tt>document.all</tt> construct while <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/obsolete.html#dom-document-all">pretending that they do not actually support it</a>. This is a clear negative feature of monocultures: new things that get implemented become mandatory specifications, independent of the actual quality of their implementation. Now, some fanboys might proclaim that everything Microsoft does is inherently evil and that this is a bad example, but I will point out later known bad-behaviors of Webkit later.
</p><p>
What about open source monocultures? Perhaps the best example here is GCC, which was effectively the only important C compiler for Linux until about two years ago, when clang become self-hosting. This is probably the closest example I have to a mooted Webkit monoculture: a program that no one wants to write from scratch and that is maintained by necessity by a diverse group of contributors. So surely there are no aftereffects from compatibility problems for Clang, right? Well, to be able to compile code on Linux, Clang has to pretty much mimic GCC, down to command-line compatibility and implementing (almost) all of GCC's extensions to C. This also implies that you have to match various compiler intrinsics (such as those for atomic operations) exactly as GCC does: when GCC first implemented proper atomic operations for C++11, Clang was forced to change its syntax for intrinsic atomic operations to match as well.
</p><p>
The problem of implementations becoming the <em>de facto</em> standard becomes brutally clear when a radically different implementation is necessary and backwards compatibility cannot be sacrificed. IE 6's hasLayout bug is a good example here: Microsoft thought it easier to bundle an old version of the layout engine in their newest web browser to support old-compatibility webpages than to try to adaptively support it. It is much easier to justify sacking backwards compatibility in a vibrant polyculture: if a website works in only one layout engine when there are four major ones, then it is a good sign that the website is broken and needs to be fixed.
</p><p>
All of these may seem academic, theoretical objections, but I will point out that Webkit has already shown troubling signs that do not portend to it being a "good" monoculture. The decision to never retire old Webkit prefixes is nothing short of an arrogant practice, and clearly shows that backwards compatibility (even for nominally non-production features) will be difficult to sacrifice. The feature that became CSS gradients underwent cosmetic changes that made things easier for authors that wouldn't have happened in a monoculture layout engine world. Chrome explicitly went against the CSS specification (although they are trying to change it) in one feature with the rationale that is necessary for better scrolling performance in Webkit's architecture—which neither Gecko nor Presto seem to require. So a Webkit monoculture is not a good thing.Joshua Cranmerhttp://www.blogger.com/profile/02760318962075959780noreply@blogger.com6tag:blogger.com,1999:blog-5947958124349996271.post-14884332601946492602013-02-15T13:37:00.000-05:002013-02-15T13:37:05.721-05:00Updated DXR on dxr.mozilla.orgIf you are a regular user of <a href="http://dxr.mozilla.org">DXR</a>, you may have noticed that the website today looks rather different from what you are used to. This is because it has finally been updated to a version dating back to mid-January, which means it also includes many of the changes developed over the course of the last summer, previously visible only on the development snapshot (which didn't update mozilla-central versions). In the coming months, I also plan to expand the repertoire of indexed repositories from one to two by including comm-central. Other changes that are currently being developed include a documentation output feature for DXR as well as an indexer that will grok our JS half of the codebase.Joshua Cranmerhttp://www.blogger.com/profile/02760318962075959780noreply@blogger.com2tag:blogger.com,1999:blog-5947958124349996271.post-46629223402739637522013-01-21T12:57:00.001-05:002013-01-21T12:57:47.688-05:00DXR's future potentialThe original purpose of DXR, back when the "D" in its name wasn't a historical artifact, was to provide a replacement for MXR that grokked Mozilla's source code a lot better. In the intervening years, DXR has become a lot better at being an MXR replacement, so I think that perhaps it is worth thinking about ways that DXR can start going above and beyond MXR—beyond just letting you searched for things like "derived" and "calls" relationships.
</p><h4>The <tt>#ifdef</tt> problem</h4><p>
In discussing DXR at the 2011 LLVM Developers' Conference, perhaps the most common question I had was asking what it did about the <tt>#ifdef</tt> problem: how does it handle code present in the source files but excluded via conditional compilation constructs? The answer then, as it is now, was "nothing:" at present, it pretends that code not compiled doesn't really exist beyond some weak attempts to lex it for the purposes of syntax highlighting. One item that has been very low priority for several years was an idea to fix this issue by essentially building the code in all of its variations and merging the resulting database to produce a more complete picture of the code. I don't think it's a hard problem at all, but rather just an engineering concern that needs a lot of little details to be worked out, which makes it impractical to implement while the codebase is undergoing flux.
</p><h4>Documentation</h4><p>
Documentation is an intractable unsolved problem that makes me wonder why I bring it up here…oh wait, it's not. Still, from the poor quality of most documentation tools out there when it comes to grokking very large codebases (Doxygen, I'm looking at you), it's a wonder that no one has built a better one. Clang added a feature that lets it associate comments to AST elements, which means that DXR has all the information it needs to be able to build documentation from our in-tree documentation. With complete knowledge of the codebase and a C++ parser that won't get confused by macros, we have all the information we need to be able to make good documentation, and we also have a very good place to list all of this documentation.
</p><h4>Indexing dynamic languages</h4><p>
Here is where things get really hard. A language like Java or C# is very easy to index: every variable is statically typed and named, and fully-qualified names are generally sufficient for global uniqueness. C-based languages lose the last bit, since nothing enforces global uniqueness of type names. C++ templates are effectively another programming language that relies on duck-typing. However, that typing is still static and can probably be solved with some clever naming and UI; dynamic languages like JavaScript or Python make accurately finding the types of variables difficult to impossible.
</p><p>
Assigning static types to dynamic typing is a task I've given some thought to. The advantage in a tool like DXR is that we can afford to be marginally less accurate in typing in trade for precision. An example of such an inaccuracy would be ignoring what happens with JavaScript's <tt>eval</tt> function. Inaccuracies here could be thought of as inaccuracies resulting from a type-unsafe language (much like any C-based callgraph information is almost necessarily inaccurate due to problems inherent to pointer alias analysis). The actual underlying algorithms for recovering types appear known and documented in academic literature, so I don't think that actually doing this is theoretically hard. On the other hand, those are very famous last words…Joshua Cranmerhttp://www.blogger.com/profile/02760318962075959780noreply@blogger.com28tag:blogger.com,1999:blog-5947958124349996271.post-16104022087762809322012-11-07T20:04:00.001-05:002012-11-07T20:04:26.805-05:00Autotools, how I hate theeWhen writing custom passes for a compiler, it's often a good idea to try running them on real-world programs to assess things like scalability and correctness. Taking a large project and seeing your pass work (and provide useful results!) is an exhilarating feeling. On the other hand, trying to feed in your compiler options into build systems is a good route to an insane asylum.
</p><p>
I complained some time ago about autoconf 2.13 failing because it assumes implicit int. People rightly pointed out that newer versions of autoconf don't assume that anymore. But new versions still come with their own cornucopias of pain. Libtool, for example, believes that the best route to linking a program is to delete every compiler flag from the command line except those it knows about. Even if you explicitly specify them in LDFLAGS. Then there's this conftest program that I found while compiling gawk:
</p><pre>
<span class="comment">/* Define memcpy to an innocuous variant, in case <limits.h> declares memcpy.
For example, HP-UX 11i <limits.h> declares gettimeofday. */</span>
<span class="special">#define memcpy innocuous_memcpy</span>
<span class="comment">/* System header to define __stub macros and hopefully few prototypes,
which can conflict with char memcpy (); below.
Prefer <limits.h> to <assert.h> if __STDC__ is defined, since
<limits.h> exists even on freestanding compilers. */</span>
<span class="special">#ifdef __STDC__
# include <limits.h>
#else
# include <assert.h>
#endif
#undef memcpy</span>
<span class="comment">/* Override any GCC internal prototype to avoid an error.
Use char because int might match the return type of a GCC
builtin and then its argument prototype would still apply. */</span>
<span class="special">#ifdef __cplusplus</span>
<span class="keyword">extern</span> <span class="string">"C"</span>
<span class="special">#endif</span>
<span class="type">char</span> memcpy ();
<span class="comment">/* The GNU C library defines this for functions which it implements
to always fail with ENOSYS. Some functions are actually named
something starting with __ and the normal name is an alias. */</span>
<span class="special">#if defined __stub_memcpy || defined __stub___memcpy</span>
choke me
<span class="special">#endif</span>
<span class="type">int</span>
main ()
{
<span class="keyword">return</span> memcpy ();
;
<span class="keyword">return</span> <span class="constant">0</span>;
}
</pre><p>
…I think this code speaks for itself in how broken it is as a test. One of the parts of the compiler pass involved asserted due to memcpy not being used in the right way, crashing the compiler. Naturally, this being autoconf, it proceeded to assume that I didn't have a memcpy and thus decided to provide me one, which causes later code to break in spectacular bad function when you realize that memcpy is effectively a #define in modern glibc. And heaven forbid if I should try to compile with -Werror in configure scripts (nearly every test program causes a compiler warning along the lines of "builtin function is horribly misused").
</p><p>
The saddest part of all is that, as bad as autoconf is, it appears to be the least broken configuration system out there…Joshua Cranmerhttp://www.blogger.com/profile/02760318962075959780noreply@blogger.com6tag:blogger.com,1999:blog-5947958124349996271.post-85851439253026572862012-08-16T19:53:00.001-04:002012-08-16T19:53:36.049-04:00Updated code coverage, now on try!My expeditions in code coverage date back several years, but I am proud to report the biggest milestone to date now. Instead of running all of these tests on various clusters I have access to (and running into problems with random test failures), I am running them on the same servers that power Mozilla's build automation. How, you might ask? Read on to find out.
</p><p>
I've uploaded the results from running Linux64 debug builds (both <a href="http://people.mozilla.org/~jcranmer2/m-ccov/">LCOV results</a> and <a href="http://people.mozilla.org/~jcranmer2/m-ccov/coverage.html">my treemap application</a>). My treemap application has gone through major revisions, too. It now uses CSS transitions instead of the framework's builtin transitions (no more slow script dialogs, and the animations are snappier, although Firefox 14 still chokes on the whole view and Firefox nightly appears to suddenly thrash memory). I've also tweaked the UI a bit to fix minor things that you probably wouldn't notice if I didn't point them out. Instructions on using the view are now in place (it turns out that treemaps aren't as intuitive as I feel them to be). You can also break down results by testsuite, although that feature was very hurriedly hacked in and has very obvious pain points. Code for this portion of the site can be found <a href="https://github.com/jcranmer/mozilla-coverage">on github</a>.
</p><p>
The other potentially interesting part is how I get this to work on Try. I don't have this in a public repository yet, but you can follow along the changes <a href="https://hg.mozilla.org/try/rev/119497c56df4">on the patch I pushed to try</a>. This is how it works: first, I patch the mozconfigs to include gcov results. Then, I package up all of the notes files and include them in the tarball that contains firefox. Now is where the fun begins: at opportune places in all of the test suites, I output (to standard out) a base64-encoded tarball of all the data files collected during the run of the program. For test suites other than <kbd>make check</kbd>, I need to munge the environment to set <tt>GCOV_PREFIX</tt> to a directory where I can find all of the data files again.
</p><p>
When the magic patch is pushed to try, and after it builds, I can now find in all of the output log files of the build a tarball (or several, in the case of mochitest-other) of all the coverage data. The steps after this are done manually, by dint of only getting the try stuff working last night. I download all of the log files, and extract the tarballs of coverage data into a directory. Then I pull the gcov note files into that directory and run the LCOV tools to collect data. I use ccov (my work-in-progress to replace LCOV) to combine all the test suites into a single file and then produce the data for the nice tree view. LCOV is used to produce the detailed per-file data. After everything is produced, I gather it all up and upload it.
</p><p>
Where to from here? I want to automate the latter part of the process, as babysitting steps that take 10-20 minutes each is surprisingly unproductive. I also want to tackle a replacement for LCOV's HTML output, since it both takes forever to run (well over an hour), and the output is surprisingly lossy in information (you don't know which lines are covered by which tests, and weird corner cases in branch and function coverage could be presented better). Eliminating LCOV altogether is another goal, but I can live with it in the first data collection step for now because gcov does really crazy stuff in coverage. I also want to expand all of these tests to run on more than just one platform—ideally, I want code coverage for all test suites run on <strong>all</strong> platforms. Assuming, of course, releng doesn't decide to kill me first.Joshua Cranmerhttp://www.blogger.com/profile/02760318962075959780noreply@blogger.com8tag:blogger.com,1999:blog-5947958124349996271.post-23345844093061012792012-08-10T16:48:00.000-04:002012-08-10T16:48:09.076-04:00How to test new clang versions with tryMac OS X builds of Firefox now use clang, and as work was put in to make the switch happen, this necessitated testing individual versions of clang. This means we have infrastructure in place that makes it (relatively) easy to run tests on the try server with even patched versions of clang. Here's how you do it:
</p><h5>Step 1: Build your version of clang</h5><p>
This should be trivial, although one wrinkle is that you need to specify where the gcc-toolchain is located explictly (<tt>/tools/gcc-4.5-0moz3</tt> for now). If you're like me and lazy, you can just use the <a href="http://hg.mozilla.org/mozilla-central/file/tip/build/unix/build-clang/build-clang.py">build-clang.py</a> script to make the final tarball, after tweaking it to include your patch. Note that it expects to be located in specific directories (<tt>/builds/slave/moz-toolchain</tt> in particular). If you're building by hand, be sure to make a .tar.bz2 of the
</p><h5>Step 2: Place packages in appropriate location</h5><p>
The next script assumes things in particular places. It wants a directory layout that looks like:
</p><pre>
$ pwd
/path/ending/in/clang-<i>SVN revision</i>
$ ls
clang-darwin.tar.bz2 clang-linux32.tar.bz2 clang-linux64.tzr.bz2
</pre>
</p><h5>Step 3: Make manifests</h5><p>
This script is <a href="http://hg.mozilla.org/mozilla-central/file/tip/build/unix/build-clang/create-manifest.py">create-manifest.py</a>, which has a requirement of simplejson 2.5 (which is newer than what mozilla-central's virtualenv python provides, alas). If you don't have a new enough python environment, just eliminate the <tt>item_sort_key=key_sort</tt> parameter and live with the fact that your output files are going to change more lines when importing to mozilla-central. This step produces files like <tt>darwin.manifest</tT>; these should be copied to <tt>browser/config/tooltool-manifests/<i>platform</i>/clang.manifest</tt> and the respective <tt>releng.manifest</tt>. It will also produce files with long, ugly filenames that look like someone dumped out a SHA512 hash as the filename (this is in fact what happens).
</p><h5>Step 4: Upload the files</h5><p>
First, copy the SHA512-named filenames somewhere public, like people.mozilla.org. Next, go into <a href="irc://irc.mozilla.org/it>#it on IRC</a> and bug someone about uploading the files to the tooltool server. When they've been uploaded, proceed to the next step.
</p><h5>Step 5: Push to try</h5><p>
Remember to use the modified manifests produced in step 3. On Linux and Linux64, you'll also need to modify <a href="http://hg.mozilla.org/mozilla-central/file/tip/build/unix/mozconfig.linux">the mozconfigs</a> to use clang instead of gcc. An example is here:
</p><pre>
export CC="$topsrcdir/clang/bin/clang -fgnu89-inline"
export CXX=$topsrcdir/clang/bin/clang++
</pre><p>
Disabling warnings as errors is also probably a good idea, since it seems that Linux people can't stop those extra semicolons sneaking in. Then, push to try and hope for the best!Joshua Cranmerhttp://www.blogger.com/profile/02760318962075959780noreply@blogger.com0tag:blogger.com,1999:blog-5947958124349996271.post-68805643027347217022012-07-16T19:56:00.002-04:002012-07-16T19:56:28.445-04:00Mozilla-central code coverageI have been posting code-coverage results of comm-central off and on for several years. One common complaint I get from developers is that I don't run any of the mozilla-central testsuites. So I finally buckled down and <a href="http://people.mozilla.org/~jcranmer2/m-ccov/coverage.html">built mozilla-central's coverage treemap</a> (and <a href="http://people.mozilla.org/~jcranmer2/m-ccov/">LCOV output too</a>).
</p><p>
The testsuites here correspond to running top-level check, mochitest-plain, mochitest-a11y, mochitest-ipcplugins, mochitest-chrome, reftest, crashtest, jstestbrowser, and xpcshell-tests, which should correspond to most of the test suite that Tinderbox runs (excluding Talos and some of the ipc-only-looking things). The LCOV output can break things down by test results, but the treemap still lacks this functionality (I only built in multiple test support to the framework this afternoon while waiting for things to compile).
</p><p>
Caveats of course. This is an x86-64 Linux opt-without-optimizations build. This isn't my laptop, and X forwarding failed, so I had to resort to using Xvfb for the display (which managed to crash during one test run). It seems that some of the mochitests failed due to not having focus, and I have no idea how to make Xvfb give it focus, so not all mochitests ran. Some of the mozapps tests just fail generally because of recursion issues. So this isn't exactly what the tinderboxes run. Oh, and gcov consistently fails to parse jschuff.cpp's coverage data.
</p><p>
Lcov is also getting more painful to use—I finished running tests on Saturday night, and it took me most of Sunday and Monday to actually get the output (!!!). Fortunately, I've reimplemented most of the functionality in <a href="https://github.com/jcranmer/mozilla-coverage/blob/master/ccov.py">my own coverage analysis scripts</a>, so the only parts missing are branch coverage data and the generated HTML index which I want to integrate with my web UI anyways.Joshua Cranmerhttp://www.blogger.com/profile/02760318962075959780noreply@blogger.com1