|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const simpleParser = require('../lib/simple-parser'); |
| 4 | + |
| 5 | +module.exports['Should not fabricate address from bare Base64 encoded email'] = test => { |
| 6 | + // attacker@evil.com encoded as Base64 |
| 7 | + let source = Buffer.from(`From: =?utf-8?b?YXR0YWNrZXJAZXZpbC5jb20=?=\r\nTo: victim@example.com\r\n\r\ntest`); |
| 8 | + |
| 9 | + simpleParser(source, {}, (err, mail) => { |
| 10 | + test.ifError(err); |
| 11 | + test.ok(mail); |
| 12 | + |
| 13 | + test.equal(mail.from.value[0].address, '', 'Bare encoded email must not become an address'); |
| 14 | + test.equal(mail.from.value[0].name, 'attacker@evil.com', 'Decoded text should be treated as display name'); |
| 15 | + |
| 16 | + test.done(); |
| 17 | + }); |
| 18 | +}; |
| 19 | + |
| 20 | +module.exports['Should still parse legitimate encoded Name <email> addresses'] = test => { |
| 21 | + // "Rydel" <Rydelkalot@17guagua.com> encoded as Base64 |
| 22 | + let source = Buffer.from(`From: test@example.com\r\nTo: =?utf-8?B?IlJ5ZGVsIiA8UnlkZWxrYWxvdEAxN2d1YWd1YS5jb20+?=, andris@tr.ee\r\n\r\ntest`); |
| 23 | + |
| 24 | + simpleParser(source, {}, (err, mail) => { |
| 25 | + test.ifError(err); |
| 26 | + test.ok(mail); |
| 27 | + |
| 28 | + let toAddresses = mail.to.value; |
| 29 | + let rydel = toAddresses.find(a => a.address === 'Rydelkalot@17guagua.com'); |
| 30 | + test.ok(rydel, 'Legitimate encoded address with angle brackets should still be parsed'); |
| 31 | + test.equal(rydel.name, 'Rydel'); |
| 32 | + |
| 33 | + test.done(); |
| 34 | + }); |
| 35 | +}; |
| 36 | + |
| 37 | +module.exports['Should decode and reject encoded-words in addr-spec that produce invalid addresses'] = test => { |
| 38 | + // =40 decodes to @, producing @attacker.com@microsoft.com (two @ signs) |
| 39 | + let source = Buffer.from(`From: =?utf-8?q?=40attacker.com?=@microsoft.com\r\nTo: victim@example.com\r\n\r\ntest`); |
| 40 | + |
| 41 | + simpleParser(source, {}, (err, mail) => { |
| 42 | + test.ifError(err); |
| 43 | + test.ok(mail); |
| 44 | + |
| 45 | + test.equal(mail.from.value[0].address, '', 'Encoded-word in addr-spec producing invalid address should be cleared'); |
| 46 | + |
| 47 | + test.done(); |
| 48 | + }); |
| 49 | +}; |
| 50 | + |
| 51 | +module.exports['Should not touch normal addresses'] = test => { |
| 52 | + let source = Buffer.from(`From: "Sender" <sender@example.com>\r\nTo: recipient@example.com\r\n\r\ntest`); |
| 53 | + |
| 54 | + simpleParser(source, {}, (err, mail) => { |
| 55 | + test.ifError(err); |
| 56 | + test.ok(mail); |
| 57 | + |
| 58 | + test.equal(mail.from.value[0].address, 'sender@example.com'); |
| 59 | + test.equal(mail.from.value[0].name, 'Sender'); |
| 60 | + test.equal(mail.to.value[0].address, 'recipient@example.com'); |
| 61 | + |
| 62 | + test.done(); |
| 63 | + }); |
| 64 | +}; |
| 65 | + |
| 66 | +module.exports['Should not touch percent-hack addresses'] = test => { |
| 67 | + let source = Buffer.from(`From: user%attacker.com@microsoft.com\r\nTo: victim@example.com\r\n\r\ntest`); |
| 68 | + |
| 69 | + simpleParser(source, {}, (err, mail) => { |
| 70 | + test.ifError(err); |
| 71 | + test.ok(mail); |
| 72 | + |
| 73 | + test.equal(mail.from.value[0].address, 'user%attacker.com@microsoft.com', 'Percent-hack addresses should pass through as-is'); |
| 74 | + |
| 75 | + test.done(); |
| 76 | + }); |
| 77 | +}; |
0 commit comments