nodemailer 10.1.3 → 10.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +23 -0
- package/dist/cjs/addressparser/index.js +513 -112
- package/dist/cjs/mime-node/index.js +8 -1
- package/dist/cjs/package-info.d.ts +1 -1
- package/dist/cjs/package-info.js +1 -1
- package/dist/esm/addressparser/index.js +513 -112
- package/dist/esm/mime-node/index.js +8 -1
- package/dist/esm/package-info.d.ts +1 -1
- package/dist/esm/package-info.js +1 -1
- package/package.json +1 -1
|
@@ -1,5 +1,99 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
/* eslint-disable no-bitwise -- the kind of a token is a set of bit flags packed into one slot of a typed array */
|
|
4
|
+
/** The token is an operator, a text run otherwise */
|
|
5
|
+
const TOKEN_OPERATOR = 1;
|
|
6
|
+
/** The next character continues the same run without whitespace in between */
|
|
7
|
+
const TOKEN_NO_BREAK = 2;
|
|
8
|
+
/** The text run holds characters its value leaves out or changes, so it is no plain slice */
|
|
9
|
+
const TOKEN_DIRTY = 4;
|
|
10
|
+
/** A backslash escapes the next character in the text run, inside a quoted string or a comment */
|
|
11
|
+
const TOKEN_ESCAPES = 8;
|
|
12
|
+
/** The text run opens on a character an escaping backslash in front of it has escaped */
|
|
13
|
+
const TOKEN_STARTS_ESCAPED = 16;
|
|
14
|
+
/**
|
|
15
|
+
* The tokens of an address field, kept as offsets into it.
|
|
16
|
+
*
|
|
17
|
+
* Every token used to be an object of its own, an operator for every ',' and a value string
|
|
18
|
+
* for every run of text, and a list of one letter names, 'a,a,a,...', took some 65 times its
|
|
19
|
+
* size in tokens before any address was read. A token is now three numbers in one typed
|
|
20
|
+
* array, its kind and the range of the field it covers, and its value is sliced off the field
|
|
21
|
+
* when it is read. The few text runs whose value differs from their slice, as they hold an
|
|
22
|
+
* escaping backslash, a line break or a control character, are rebuilt once and kept aside.
|
|
23
|
+
*/
|
|
24
|
+
class TokenList {
|
|
25
|
+
constructor(str) {
|
|
26
|
+
this.str = str;
|
|
27
|
+
this.slots = new Int32Array(48);
|
|
28
|
+
this.length = 0;
|
|
29
|
+
this.cleaned = new Map();
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Appends a token
|
|
33
|
+
*
|
|
34
|
+
* @param kind Kind and flags of the token
|
|
35
|
+
* @param start Offset of its first character
|
|
36
|
+
* @param end Offset just past its last character
|
|
37
|
+
* @return Index of the token
|
|
38
|
+
*/
|
|
39
|
+
push(kind, start, end) {
|
|
40
|
+
if ((this.length + 1) * 3 > this.slots.length) {
|
|
41
|
+
const grown = new Int32Array(this.slots.length * 2);
|
|
42
|
+
grown.set(this.slots);
|
|
43
|
+
this.slots = grown;
|
|
44
|
+
}
|
|
45
|
+
const at = this.length * 3;
|
|
46
|
+
this.slots[at] = kind;
|
|
47
|
+
this.slots[at + 1] = start;
|
|
48
|
+
this.slots[at + 2] = end;
|
|
49
|
+
return this.length++;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* The character of an operator token, an empty string for a text run
|
|
53
|
+
*/
|
|
54
|
+
operatorAt(index) {
|
|
55
|
+
const at = index * 3;
|
|
56
|
+
return this.slots[at] & TOKEN_OPERATOR ? this.str.charAt(this.slots[at + 1]) : '';
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Whether a token is an operator
|
|
60
|
+
*/
|
|
61
|
+
isOperator(index) {
|
|
62
|
+
return (this.slots[index * 3] & TOKEN_OPERATOR) !== 0;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Whether the next character continues the run of a token without whitespace in between
|
|
66
|
+
*/
|
|
67
|
+
noBreak(index) {
|
|
68
|
+
return (this.slots[index * 3] & TOKEN_NO_BREAK) !== 0;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Value of a token
|
|
72
|
+
*/
|
|
73
|
+
value(index) {
|
|
74
|
+
const cleaned = this.cleaned.get(index);
|
|
75
|
+
if (cleaned !== undefined) {
|
|
76
|
+
return cleaned;
|
|
77
|
+
}
|
|
78
|
+
return this.str.slice(this.slots[index * 3 + 1], this.slots[index * 3 + 2]);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* A domain literal that ends an address. dtext includes '@' (RFC 5322 3.4.1), so the '@'
|
|
83
|
+
* in front of the literal is the one that splits the domain off, not the last one: reading
|
|
84
|
+
* 'a@[x@evil.com]' at its last '@' made it an address at evil.com.
|
|
85
|
+
*/
|
|
86
|
+
const DOMAIN_LITERAL_TAIL = /@(\[[^[\]\\]*\])$/;
|
|
87
|
+
/**
|
|
88
|
+
* Offset of the '@' that splits the domain off an address, or -1 when there is none
|
|
89
|
+
*
|
|
90
|
+
* @param address Address to split
|
|
91
|
+
* @return Offset of the '@'
|
|
92
|
+
*/
|
|
93
|
+
function _domainAt(address) {
|
|
94
|
+
const literal = address.charAt(address.length - 1) === ']' ? DOMAIN_LITERAL_TAIL.exec(address) : null;
|
|
95
|
+
return literal ? address.length - literal[1].length - 1 : address.lastIndexOf('@');
|
|
96
|
+
}
|
|
3
97
|
/**
|
|
4
98
|
* Restores the quoting of a local part that was read out of a quoted string.
|
|
5
99
|
*
|
|
@@ -18,7 +112,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
18
112
|
* @return Address with the local part as a quoted-string
|
|
19
113
|
*/
|
|
20
114
|
function _quoteLocalPart(address) {
|
|
21
|
-
const lastAt = address
|
|
115
|
+
const lastAt = _domainAt(address);
|
|
22
116
|
if (lastAt < 0) {
|
|
23
117
|
// no domain to split off, nothing can be misrouted
|
|
24
118
|
return address;
|
|
@@ -44,6 +138,23 @@ const QUOTED_LOCAL_ADDR = /^("(?:[^"\\]|\\[\s\S])*"@\S+)(?:\s+([\s\S]+))?$/;
|
|
|
44
138
|
* One run holding a single '@' and no whitespace, the shape an addr-spec has to have.
|
|
45
139
|
*/
|
|
46
140
|
const ADDR_SPEC = /^[^@\s]+@[^@\s]+$/;
|
|
141
|
+
/**
|
|
142
|
+
* The same with a domain literal for the domain, which may hold an '@' of its own.
|
|
143
|
+
*/
|
|
144
|
+
const LITERAL_ADDR_SPEC = /^[^@\s]+@\[[^[\]\\\s]*\]$/;
|
|
145
|
+
/**
|
|
146
|
+
* The '@' and domain that follow a quoted local part in the same run.
|
|
147
|
+
*/
|
|
148
|
+
const QUOTED_LOCAL_DOMAIN = /^@(?:[^@\s"]+|\[[^[\]\\\s]*\])$/;
|
|
149
|
+
/**
|
|
150
|
+
* Whether a whitespace free run reads as an addr-spec under the strict grammar
|
|
151
|
+
*
|
|
152
|
+
* @param value Run to check
|
|
153
|
+
* @return true when it does
|
|
154
|
+
*/
|
|
155
|
+
function _isAddrSpec(value) {
|
|
156
|
+
return ADDR_SPEC.test(value) || LITERAL_ADDR_SPEC.test(value);
|
|
157
|
+
}
|
|
47
158
|
/**
|
|
48
159
|
* The looser reading applied once the strict one finds nothing, which tolerates the
|
|
49
160
|
* further '@' that a domain should not have but malformed headers carry anyway.
|
|
@@ -110,6 +221,62 @@ function _indexOfAt(text, from, to) {
|
|
|
110
221
|
}
|
|
111
222
|
return -1;
|
|
112
223
|
}
|
|
224
|
+
/**
|
|
225
|
+
* A quoted-string a value opens with.
|
|
226
|
+
*/
|
|
227
|
+
const LEADING_QUOTED_STRING = /^"(?:[^"\\]|\\[\s\S])*"/;
|
|
228
|
+
/**
|
|
229
|
+
* Drops the whitespace on either side of the '@' at an offset.
|
|
230
|
+
*
|
|
231
|
+
* obs-local-part and obs-domain allow CFWS around the '@' (RFC 5322 4.4), so 'user @ good.com'
|
|
232
|
+
* names user@good.com. Kept, the whitespace went into the envelope as '"user "@ good.com'.
|
|
233
|
+
* The scan is by index, a pattern such as /\s+@/ backtracks over a long whitespace run that
|
|
234
|
+
* no '@' follows.
|
|
235
|
+
*
|
|
236
|
+
* @param value Value holding the '@'
|
|
237
|
+
* @param at Offset of the '@', or -1
|
|
238
|
+
* @return The value with the whitespace beside that '@' removed
|
|
239
|
+
*/
|
|
240
|
+
function _joinAtSpacing(value, at) {
|
|
241
|
+
if (at < 0) {
|
|
242
|
+
return value;
|
|
243
|
+
}
|
|
244
|
+
let start = at;
|
|
245
|
+
while (start > 0 && _isSpaceCode(value.charCodeAt(start - 1))) {
|
|
246
|
+
start--;
|
|
247
|
+
}
|
|
248
|
+
let end = at + 1;
|
|
249
|
+
while (end < value.length && _isSpaceCode(value.charCodeAt(end))) {
|
|
250
|
+
end++;
|
|
251
|
+
}
|
|
252
|
+
return start === at && end === at + 1 ? value : value.slice(0, start) + '@' + value.slice(end);
|
|
253
|
+
}
|
|
254
|
+
/**
|
|
255
|
+
* Drops the whitespace beside the '@' that splits the domain off a value. That is the '@'
|
|
256
|
+
* right after a quoted local part the value opens with, or else the only '@' it holds outside
|
|
257
|
+
* any quoted string. With several it is not clear which one the whitespace belongs to, and
|
|
258
|
+
* '<x@evil.com @good.com>' is left to the addr-spec recovery to read as it always has.
|
|
259
|
+
*
|
|
260
|
+
* @param value Value to clean up
|
|
261
|
+
* @return The value with the whitespace beside that '@' removed
|
|
262
|
+
*/
|
|
263
|
+
function _collapseAtSpacing(value) {
|
|
264
|
+
const quoted = LEADING_QUOTED_STRING.exec(value);
|
|
265
|
+
if (quoted) {
|
|
266
|
+
let at = quoted[0].length;
|
|
267
|
+
while (at < value.length && _isSpaceCode(value.charCodeAt(at))) {
|
|
268
|
+
at++;
|
|
269
|
+
}
|
|
270
|
+
if (value.charAt(at) === '@' && value.indexOf('@', at + 1) < 0) {
|
|
271
|
+
return _joinAtSpacing(value, at);
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
const at = value.indexOf('@');
|
|
275
|
+
if (at < 0 || value.indexOf('@', at + 1) >= 0 || value.lastIndexOf('"') > at) {
|
|
276
|
+
return value;
|
|
277
|
+
}
|
|
278
|
+
return _joinAtSpacing(value, at);
|
|
279
|
+
}
|
|
113
280
|
/**
|
|
114
281
|
* Finds the offset LOOSE_TEXT_ADDR matches at, or -1 when it does not match at all.
|
|
115
282
|
*
|
|
@@ -199,6 +366,7 @@ function _looseAddressStart(text) {
|
|
|
199
366
|
* @param data Collected address parts, mutated in place
|
|
200
367
|
*/
|
|
201
368
|
function _recoverAddrSpec(data) {
|
|
369
|
+
data.address = _collapseAtSpacing(data.address);
|
|
202
370
|
if (!HAS_WHITESPACE.test(data.address)) {
|
|
203
371
|
return;
|
|
204
372
|
}
|
|
@@ -225,7 +393,7 @@ function _recoverAddrSpec(data) {
|
|
|
225
393
|
return;
|
|
226
394
|
}
|
|
227
395
|
const parts = data.address.split(/\s+/);
|
|
228
|
-
let addrIndex = parts.findIndex(
|
|
396
|
+
let addrIndex = parts.findIndex(_isAddrSpec);
|
|
229
397
|
if (addrIndex < 0) {
|
|
230
398
|
addrIndex = parts.findIndex(part => LOOSE_ADDR_SPEC.test(part));
|
|
231
399
|
}
|
|
@@ -252,6 +420,15 @@ function _recoverAddrSpec(data) {
|
|
|
252
420
|
* A comment is folding whitespace, so it is read as a space that splits two atoms and as
|
|
253
421
|
* nothing next to an '@', the same rule the token walk applies outside the brackets. Quoted
|
|
254
422
|
* strings and domain literals are copied through untouched, a '(' in there is not a comment.
|
|
423
|
+
* An unexpected '<' outside them drops everything in front of it, comments included.
|
|
424
|
+
*
|
|
425
|
+
* RFC 822 also let an angle-addr name the relays to go through in front of the mailbox,
|
|
426
|
+
* '<@relay1,@relay2:user@example.com>', which RFC 5322 still parses as obs-route. The route
|
|
427
|
+
* is not part of the mailbox, and folded into the local part it made
|
|
428
|
+
* '"@relay1,@relay2:user"@example.com' a recipient that does not exist. A local part can not
|
|
429
|
+
* start with '@', so contents that do, up to the first ':' outside a domain literal and in
|
|
430
|
+
* front of any quoted string, are a route and are dropped, as long as a mailbox with an '@'
|
|
431
|
+
* of its own follows.
|
|
255
432
|
*
|
|
256
433
|
* @param address Contents of the angle brackets
|
|
257
434
|
* @return The address with the comments removed, and the text of the comments
|
|
@@ -265,6 +442,16 @@ function _stripAddressComments(address) {
|
|
|
265
442
|
// carried along rather than read back off the growing result, which would flatten it on
|
|
266
443
|
// every comment (see lastChars in _handleAddress)
|
|
267
444
|
let lastChar = '';
|
|
445
|
+
// A comment that just closed stands for a space, written once the next character shows
|
|
446
|
+
// whether one is wanted: not beside an '@', and not on top of whitespace that follows.
|
|
447
|
+
// Deciding on the spot saw only the character right after the ')', so a second comment
|
|
448
|
+
// in a row hid the '@' behind it and 'a(x)(y)@b' read as 'a @b'
|
|
449
|
+
let pendingSpace = false;
|
|
450
|
+
// first character of the result and whether a quoted string has opened since, which
|
|
451
|
+
// together tell a route from a local part
|
|
452
|
+
let firstChar = '';
|
|
453
|
+
let quoteSeen = false;
|
|
454
|
+
let routeEnd = -1;
|
|
268
455
|
for (let i = 0, len = address.length; i < len; i++) {
|
|
269
456
|
const chr = address.charAt(i);
|
|
270
457
|
if (depth) {
|
|
@@ -278,16 +465,20 @@ function _stripAddressComments(address) {
|
|
|
278
465
|
else if (chr === ')' && !--depth) {
|
|
279
466
|
comments.push(comment.trim());
|
|
280
467
|
comment = '';
|
|
281
|
-
|
|
282
|
-
result += ' ';
|
|
283
|
-
lastChar = ' ';
|
|
284
|
-
}
|
|
468
|
+
pendingSpace = lastChar !== '@';
|
|
285
469
|
}
|
|
286
470
|
else {
|
|
287
471
|
comment += chr;
|
|
288
472
|
}
|
|
289
473
|
continue;
|
|
290
474
|
}
|
|
475
|
+
if (pendingSpace && chr !== '(') {
|
|
476
|
+
pendingSpace = false;
|
|
477
|
+
if (chr !== '@' && chr !== '<' && !_isSpaceCode(address.charCodeAt(i))) {
|
|
478
|
+
result += ' ';
|
|
479
|
+
lastChar = ' ';
|
|
480
|
+
}
|
|
481
|
+
}
|
|
291
482
|
if (closer) {
|
|
292
483
|
if (chr === '\\' && closer === '"' && i < len - 1) {
|
|
293
484
|
result += chr + address.charAt(++i);
|
|
@@ -300,6 +491,10 @@ function _stripAddressComments(address) {
|
|
|
300
491
|
}
|
|
301
492
|
else if (chr === '"') {
|
|
302
493
|
closer = '"';
|
|
494
|
+
quoteSeen = true;
|
|
495
|
+
}
|
|
496
|
+
else if (chr === ':' && routeEnd < 0 && firstChar === '@' && !quoteSeen) {
|
|
497
|
+
routeEnd = result.length + 1;
|
|
303
498
|
}
|
|
304
499
|
else if (chr === '[') {
|
|
305
500
|
closer = ']';
|
|
@@ -308,8 +503,27 @@ function _stripAddressComments(address) {
|
|
|
308
503
|
depth = 1;
|
|
309
504
|
continue;
|
|
310
505
|
}
|
|
506
|
+
else if (chr === '<') {
|
|
507
|
+
// Apple Mail drops everything between an unexpected '<' and the address after it.
|
|
508
|
+
// Read here rather than off the raw brackets so that a '<' inside a quoted string,
|
|
509
|
+
// a comment or a domain literal stays where it is: '<"a<b"@victim.com>' cut at
|
|
510
|
+
// the first '<' left the address 'b"@victim.com'
|
|
511
|
+
result = '';
|
|
512
|
+
lastChar = '';
|
|
513
|
+
comments.length = 0;
|
|
514
|
+
firstChar = '';
|
|
515
|
+
quoteSeen = false;
|
|
516
|
+
routeEnd = -1;
|
|
517
|
+
continue;
|
|
518
|
+
}
|
|
311
519
|
result += chr;
|
|
312
520
|
lastChar = chr;
|
|
521
|
+
if (!firstChar && !_isSpaceCode(address.charCodeAt(i))) {
|
|
522
|
+
firstChar = chr;
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
if (routeEnd >= 0 && result.indexOf('@', routeEnd) >= 0) {
|
|
526
|
+
result = result.slice(routeEnd);
|
|
313
527
|
}
|
|
314
528
|
if (depth) {
|
|
315
529
|
// an unterminated comment runs to the end of the address
|
|
@@ -320,11 +534,13 @@ function _stripAddressComments(address) {
|
|
|
320
534
|
/**
|
|
321
535
|
* Converts tokens for a single address into an address object
|
|
322
536
|
*
|
|
323
|
-
* @param tokens Tokens
|
|
537
|
+
* @param tokens Tokens of the whole address list
|
|
538
|
+
* @param from Index of the first token of this address
|
|
539
|
+
* @param to Index just past the last token of this address
|
|
324
540
|
* @param depth Current recursion depth for nested group protection
|
|
325
541
|
* @return Address object
|
|
326
542
|
*/
|
|
327
|
-
function _handleAddress(tokens, depth) {
|
|
543
|
+
function _handleAddress(tokens, from, to, depth) {
|
|
328
544
|
let isGroup = false;
|
|
329
545
|
let state = 'text';
|
|
330
546
|
const addresses = [];
|
|
@@ -333,9 +549,13 @@ function _handleAddress(tokens, depth) {
|
|
|
333
549
|
comment: [],
|
|
334
550
|
group: [],
|
|
335
551
|
text: [],
|
|
336
|
-
textWasQuoted: []
|
|
552
|
+
textWasQuoted: [],
|
|
553
|
+
quotedLocalAt: []
|
|
337
554
|
};
|
|
338
555
|
let insideQuotes = false;
|
|
556
|
+
// whether the last text run holds nothing but quoted text so far, which is what a quoted
|
|
557
|
+
// local part is until the '@' after it joins the run
|
|
558
|
+
let lastTextPureQuoted = false;
|
|
339
559
|
// Last character of the run each state is currently accumulating. Reading it back off
|
|
340
560
|
// the accumulator with slice(-1) makes the engine flatten the whole growing string on
|
|
341
561
|
// every token, which is quadratic over an address built from many comment-joined atoms
|
|
@@ -343,11 +563,13 @@ function _handleAddress(tokens, depth) {
|
|
|
343
563
|
// character is carried along instead of re-read.
|
|
344
564
|
const lastChars = { address: '', comment: '', group: '', text: '' };
|
|
345
565
|
// Filter out <addresses>, (comments) and regular text
|
|
346
|
-
for (let i =
|
|
347
|
-
const
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
566
|
+
for (let i = from; i < to; i++) {
|
|
567
|
+
const value = tokens.value(i);
|
|
568
|
+
// the operator in front of this token, if there is one, and whether it runs on into it
|
|
569
|
+
const prevOperator = i > from ? tokens.operatorAt(i - 1) : '';
|
|
570
|
+
const prevNoBreak = i > from && tokens.noBreak(i - 1);
|
|
571
|
+
if (tokens.isOperator(i)) {
|
|
572
|
+
switch (value) {
|
|
351
573
|
case '<':
|
|
352
574
|
state = 'address';
|
|
353
575
|
insideQuotes = false;
|
|
@@ -371,7 +593,7 @@ function _handleAddress(tokens, depth) {
|
|
|
371
593
|
break;
|
|
372
594
|
}
|
|
373
595
|
}
|
|
374
|
-
else
|
|
596
|
+
else {
|
|
375
597
|
// An empty quoted string is dropped by the tokenizer, leaving no text token of its
|
|
376
598
|
// own for textWasQuoted to be recorded on, so the pair of quote operators right in
|
|
377
599
|
// front of this token is all that is left of it and the run it opens carries the
|
|
@@ -379,48 +601,47 @@ function _handleAddress(tokens, depth) {
|
|
|
379
601
|
// the quotes never go back on, and the value is no longer an addr-spec a trailing
|
|
380
602
|
// comment can be peeled off of. It only ever opens a run: a run that already holds
|
|
381
603
|
// material collected outside the quotes is not a quoted string, whatever follows it
|
|
382
|
-
const
|
|
383
|
-
const opensAfterEmptyQuotedString = prevToken?.type === 'operator' &&
|
|
384
|
-
prevToken.value === '"' &&
|
|
385
|
-
!!prevToken.noBreak &&
|
|
386
|
-
prevPrevToken?.type === 'operator' &&
|
|
387
|
-
prevPrevToken.value === '"';
|
|
388
|
-
if (state === 'address') {
|
|
389
|
-
// Handle unquoted name that includes a "<".
|
|
390
|
-
// Apple Mail truncates everything between an unexpected < and an address.
|
|
391
|
-
token.value = token.value.replace(/^[^<]*<\s*/, '');
|
|
392
|
-
}
|
|
604
|
+
const opensAfterEmptyQuotedString = prevOperator === '"' && prevNoBreak && i > from + 1 && tokens.operatorAt(i - 2) === '"';
|
|
393
605
|
// A comment is folding whitespace. It may sit inside an addr-spec, on either side
|
|
394
606
|
// of the '@', but it cannot join two atoms into one: gluing across it would read
|
|
395
607
|
// 'user@example.com(x)evil.com' as the single domain 'example.comevil.com' and
|
|
396
608
|
// deliver to a domain the sender never named.
|
|
397
609
|
const parts = data[state];
|
|
398
|
-
const joins =
|
|
399
|
-
prevToken.noBreak &&
|
|
400
|
-
parts.length &&
|
|
401
|
-
(prevToken.value !== ')' || lastChars[state] === '@' || token.value.charAt(0) === '@');
|
|
610
|
+
const joins = prevNoBreak && parts.length && (prevOperator !== ')' || lastChars[state] === '@' || value.charAt(0) === '@');
|
|
402
611
|
if (joins) {
|
|
403
|
-
data[state]
|
|
404
|
-
if (
|
|
405
|
-
|
|
612
|
+
const last = data[state].length - 1;
|
|
613
|
+
if (state === 'text' && !insideQuotes) {
|
|
614
|
+
if (lastTextPureQuoted && value.charAt(0) === '@') {
|
|
615
|
+
data.quotedLocalAt[last] = data.text[last].length;
|
|
616
|
+
}
|
|
617
|
+
lastTextPureQuoted = false;
|
|
406
618
|
}
|
|
619
|
+
data[state][last] += value;
|
|
620
|
+
lastChars[state] = value.charAt(value.length - 1);
|
|
407
621
|
if (state === 'text' && insideQuotes) {
|
|
408
|
-
data.textWasQuoted[
|
|
622
|
+
data.textWasQuoted[last] = true;
|
|
409
623
|
}
|
|
410
624
|
}
|
|
411
625
|
else {
|
|
412
|
-
data[state].push(
|
|
413
|
-
lastChars[state] =
|
|
626
|
+
data[state].push(value);
|
|
627
|
+
lastChars[state] = value.charAt(value.length - 1);
|
|
414
628
|
if (state === 'text') {
|
|
415
629
|
data.textWasQuoted.push(insideQuotes || opensAfterEmptyQuotedString);
|
|
630
|
+
// an empty quoted string leaves no text of its own, so the '@' opens the run
|
|
631
|
+
data.quotedLocalAt.push(opensAfterEmptyQuotedString && value.charAt(0) === '@' ? 0 : -1);
|
|
632
|
+
lastTextPureQuoted = insideQuotes;
|
|
416
633
|
}
|
|
417
634
|
}
|
|
418
635
|
}
|
|
419
636
|
}
|
|
420
|
-
// If there is no text but a comment, replace the two
|
|
637
|
+
// If there is no text but a comment, replace the two. A comment is never part of a
|
|
638
|
+
// mailbox, so text that came out of one only ever names it: reading an address out of it
|
|
639
|
+
// made '(a@evil.com)' a recipient where no conformant reading has a mailbox at all
|
|
640
|
+
let textFromComment = false;
|
|
421
641
|
if (!data.text.length && data.comment.length) {
|
|
422
642
|
data.text = data.comment;
|
|
423
643
|
data.comment = [];
|
|
644
|
+
textFromComment = true;
|
|
424
645
|
}
|
|
425
646
|
if (isGroup) {
|
|
426
647
|
// http://tools.ietf.org/html/rfc2822#appendix-A.1.3
|
|
@@ -449,10 +670,6 @@ function _handleAddress(tokens, depth) {
|
|
|
449
670
|
const addressComments = [];
|
|
450
671
|
const addressParts = [];
|
|
451
672
|
for (const part of data.address) {
|
|
452
|
-
if (part.indexOf('(') < 0) {
|
|
453
|
-
addressParts.push(part);
|
|
454
|
-
continue;
|
|
455
|
-
}
|
|
456
673
|
const stripped = _stripAddressComments(part);
|
|
457
674
|
for (const comment of stripped.comments) {
|
|
458
675
|
addressComments.push(comment);
|
|
@@ -463,25 +680,42 @@ function _handleAddress(tokens, depth) {
|
|
|
463
680
|
}
|
|
464
681
|
data.address = addressParts;
|
|
465
682
|
// If no address was found, try to detect one from regular text
|
|
466
|
-
if (!data.address.length && data.text.length) {
|
|
467
|
-
|
|
683
|
+
if (!data.address.length && data.text.length && !textFromComment) {
|
|
684
|
+
// Whitespace beside the '@' is CFWS, so 'user @ good.com' is read as one run. Both
|
|
685
|
+
// tiers read the collapsed runs, and only a run an address comes out of is replaced
|
|
686
|
+
// by its collapsed form, the display text keeps its spacing
|
|
687
|
+
const candidates = data.text.map((part) => _collapseAtSpacing(part));
|
|
688
|
+
// The first addr-spec in the text is the mailbox, as it is for a conformant parser
|
|
689
|
+
// reading as far as it can. Taking the last one let a comment part two of them and
|
|
690
|
+
// pick the second, 'user@good.com (x) evil@evil.com' went to evil.com, while the
|
|
691
|
+
// same two without the comment, one run, went to good.com
|
|
692
|
+
for (let i = 0; i < data.text.length; i++) {
|
|
468
693
|
// Security: Do not extract email addresses from quoted strings.
|
|
469
694
|
// RFC 5321 allows @ inside quoted local-parts like "user@domain"@example.com.
|
|
470
695
|
// Extracting emails from quoted text leads to misrouting vulnerabilities.
|
|
471
|
-
|
|
472
|
-
|
|
696
|
+
// A run that is a quoted local part with its '@' and domain is an addr-spec of
|
|
697
|
+
// its own, with the quoting put back on, and takes its turn like any other
|
|
698
|
+
const local = data.quotedLocalAt[i];
|
|
699
|
+
const quotedLocal = data.textWasQuoted[i] && local >= 0 && QUOTED_LOCAL_DOMAIN.test(data.text[i].slice(local))
|
|
700
|
+
? _quoteLocalPart(data.text[i])
|
|
701
|
+
: '';
|
|
702
|
+
if (quotedLocal || (!data.textWasQuoted[i] && _isAddrSpec(candidates[i]))) {
|
|
703
|
+
data.address = [quotedLocal || candidates[i]];
|
|
704
|
+
data.text.splice(i, 1);
|
|
473
705
|
data.textWasQuoted.splice(i, 1);
|
|
706
|
+
data.quotedLocalAt.splice(i, 1);
|
|
474
707
|
break;
|
|
475
708
|
}
|
|
476
709
|
}
|
|
477
710
|
// Try a looser regex match if strict match found nothing
|
|
478
711
|
if (!data.address.length) {
|
|
479
712
|
let extracted = false;
|
|
480
|
-
for (let i = data.text.length
|
|
713
|
+
for (let i = 0; i < data.text.length; i++) {
|
|
481
714
|
// Security: Do not extract email addresses from quoted strings
|
|
482
715
|
if (!data.textWasQuoted[i]) {
|
|
483
|
-
|
|
484
|
-
|
|
716
|
+
// the strict tier found nothing, so it spliced nothing and the indexes still line up
|
|
717
|
+
const part = candidates[i];
|
|
718
|
+
let remainder = data.text[i];
|
|
485
719
|
const at = _looseAddressStart(part);
|
|
486
720
|
if (at >= 0) {
|
|
487
721
|
LOOSE_TEXT_ADDR.lastIndex = at;
|
|
@@ -530,16 +764,19 @@ function _handleAddress(tokens, depth) {
|
|
|
530
764
|
// on a value that is no addr-spec at all (GHSA-g57g-f23g-4646). Putting the quotes
|
|
531
765
|
// back first is what lets the recovery tell the whitespace an addr-spec may carry
|
|
532
766
|
// from the wreckage trailing one, as only a quoted local part may hold whitespace
|
|
533
|
-
|
|
767
|
+
// The quotes are gone at this point, so the '@' the domain splits off at is the
|
|
768
|
+
// last one, not the single one _collapseAtSpacing insists on
|
|
769
|
+
data.address = _quoteLocalPart(_joinAtSpacing(data.text, _domainAt(data.text)));
|
|
534
770
|
data.text = '';
|
|
535
771
|
}
|
|
536
772
|
_recoverAddrSpec(data);
|
|
537
773
|
// a comment names the mailbox only when nothing else does, as one outside the brackets
|
|
538
774
|
if (!data.text && addressComments.length) {
|
|
539
775
|
data.text = addressComments.join(' ');
|
|
776
|
+
textFromComment = true;
|
|
540
777
|
}
|
|
541
778
|
const address = {
|
|
542
|
-
address: data.address || data.text || '',
|
|
779
|
+
address: data.address || (textFromComment ? '' : data.text) || '',
|
|
543
780
|
name: data.text || data.address || ''
|
|
544
781
|
};
|
|
545
782
|
if (address.address === address.name) {
|
|
@@ -554,6 +791,7 @@ function _handleAddress(tokens, depth) {
|
|
|
554
791
|
}
|
|
555
792
|
return addresses;
|
|
556
793
|
}
|
|
794
|
+
/* eslint-disable no-bitwise -- see TokenList */
|
|
557
795
|
/**
|
|
558
796
|
* Creates a Tokenizer object for tokenizing address field strings
|
|
559
797
|
*
|
|
@@ -565,11 +803,15 @@ class Tokenizer {
|
|
|
565
803
|
this.str = (str || '').toString();
|
|
566
804
|
this.operatorCurrent = '';
|
|
567
805
|
this.operatorExpecting = '';
|
|
568
|
-
this.node =
|
|
806
|
+
this.node = -1;
|
|
569
807
|
this.escaped = false;
|
|
570
808
|
this.inDomainLiteral = false;
|
|
571
809
|
this.afterAt = false;
|
|
572
|
-
this.
|
|
810
|
+
this.innerQuoted = false;
|
|
811
|
+
this.innerComment = 0;
|
|
812
|
+
this.innerLiteral = false;
|
|
813
|
+
this.innerEscaped = false;
|
|
814
|
+
this.list = new TokenList(this.str);
|
|
573
815
|
/**
|
|
574
816
|
* Operator tokens and which tokens are expected to end the sequence
|
|
575
817
|
*/
|
|
@@ -594,27 +836,96 @@ class Tokenizer {
|
|
|
594
836
|
* @return An array of operator|text tokens
|
|
595
837
|
*/
|
|
596
838
|
tokenize() {
|
|
597
|
-
const
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
this.checkChar(chr, nextChr);
|
|
839
|
+
const str = this.str;
|
|
840
|
+
const len = str.length;
|
|
841
|
+
for (let i = 0; i < len; i++) {
|
|
842
|
+
this.checkChar(str.charAt(i), i);
|
|
602
843
|
}
|
|
603
|
-
this.
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
844
|
+
this._closeText(len);
|
|
845
|
+
// Compacted in place: a text run is trimmed to its value and one left empty is
|
|
846
|
+
// dropped, and the value of a run that is no plain slice is rebuilt and kept aside
|
|
847
|
+
const list = this.list;
|
|
848
|
+
const slots = list.slots;
|
|
849
|
+
let kept = 0;
|
|
850
|
+
for (let i = 0, count = list.length; i < count; i++) {
|
|
851
|
+
const kind = slots[i * 3];
|
|
852
|
+
let start = slots[i * 3 + 1];
|
|
853
|
+
let end = slots[i * 3 + 2];
|
|
854
|
+
if (kind & TOKEN_DIRTY) {
|
|
855
|
+
const value = this._cleanValue(start, end, kind).trim();
|
|
856
|
+
if (!value) {
|
|
857
|
+
continue;
|
|
858
|
+
}
|
|
859
|
+
list.cleaned.set(kept, value);
|
|
607
860
|
}
|
|
608
|
-
|
|
861
|
+
else if (!(kind & TOKEN_OPERATOR)) {
|
|
862
|
+
while (start < end && _isSpaceCode(str.charCodeAt(start))) {
|
|
863
|
+
start++;
|
|
864
|
+
}
|
|
865
|
+
while (end > start && _isSpaceCode(str.charCodeAt(end - 1))) {
|
|
866
|
+
end--;
|
|
867
|
+
}
|
|
868
|
+
if (start === end) {
|
|
869
|
+
continue;
|
|
870
|
+
}
|
|
871
|
+
}
|
|
872
|
+
slots[kept * 3] = kind;
|
|
873
|
+
slots[kept * 3 + 1] = start;
|
|
874
|
+
slots[kept * 3 + 2] = end;
|
|
875
|
+
kept++;
|
|
876
|
+
}
|
|
877
|
+
list.length = kept;
|
|
609
878
|
return list;
|
|
610
879
|
}
|
|
880
|
+
/**
|
|
881
|
+
* Ends the text run being collected, if there is one
|
|
882
|
+
*
|
|
883
|
+
* @param end Offset just past its last character
|
|
884
|
+
*/
|
|
885
|
+
_closeText(end) {
|
|
886
|
+
if (this.node >= 0) {
|
|
887
|
+
this.list.slots[this.node * 3 + 2] = end;
|
|
888
|
+
this.node = -1;
|
|
889
|
+
}
|
|
890
|
+
}
|
|
891
|
+
/**
|
|
892
|
+
* Rebuilds the value of a text run that is no plain slice of the field, applying the
|
|
893
|
+
* rules checkChar collects characters by: an escaping backslash is dropped, a line break
|
|
894
|
+
* reads as a space and any other control character is left out
|
|
895
|
+
*
|
|
896
|
+
* @param start Offset of the first character of the run
|
|
897
|
+
* @param end Offset just past its last character
|
|
898
|
+
* @param kind Kind and flags of the run
|
|
899
|
+
* @return Value of the run
|
|
900
|
+
*/
|
|
901
|
+
_cleanValue(start, end, kind) {
|
|
902
|
+
const str = this.str;
|
|
903
|
+
const escapes = (kind & TOKEN_ESCAPES) !== 0;
|
|
904
|
+
let escaped = (kind & TOKEN_STARTS_ESCAPED) !== 0;
|
|
905
|
+
let value = '';
|
|
906
|
+
for (let i = start; i < end; i++) {
|
|
907
|
+
const chr = str.charAt(i);
|
|
908
|
+
if (escapes && !escaped && chr === '\\') {
|
|
909
|
+
escaped = true;
|
|
910
|
+
continue;
|
|
911
|
+
}
|
|
912
|
+
escaped = false;
|
|
913
|
+
if (chr === '\n') {
|
|
914
|
+
value += ' ';
|
|
915
|
+
}
|
|
916
|
+
else if (chr.charCodeAt(0) >= 0x21 || chr === ' ' || chr === '\t') {
|
|
917
|
+
value += chr;
|
|
918
|
+
}
|
|
919
|
+
}
|
|
920
|
+
return value;
|
|
921
|
+
}
|
|
611
922
|
/**
|
|
612
923
|
* Checks if a character is an operator or text and acts accordingly
|
|
613
924
|
*
|
|
614
925
|
* @param chr Character from the address field
|
|
615
|
-
* @param
|
|
926
|
+
* @param i Offset of the character in the field
|
|
616
927
|
*/
|
|
617
|
-
checkChar(chr,
|
|
928
|
+
checkChar(chr, i) {
|
|
618
929
|
// Track RFC 5322 domain-literals ("[" *dtext "]"). The ":" of an IPv6
|
|
619
930
|
// address-literal (user@[IPv6:2001:db8::1]) is dtext and must not be treated as
|
|
620
931
|
// the group delimiter while inside the brackets. That is the only operator the
|
|
@@ -635,39 +946,53 @@ class Tokenizer {
|
|
|
635
946
|
this.inDomainLiteral = false;
|
|
636
947
|
}
|
|
637
948
|
}
|
|
949
|
+
// The closer of an angle-addr or a group is only read outside the quoted strings,
|
|
950
|
+
// comments and domain literals it holds. Looking for nothing but the closer let a '>'
|
|
951
|
+
// inside a quoted local part end the angle-addr, '<"a@evil.com>"@good.com>' was read
|
|
952
|
+
// as an address at evil.com, and a ';' inside a quoted display name or a comment end
|
|
953
|
+
// the group and release the rest of that text as recipients. The characters are still
|
|
954
|
+
// collected as they are, the angle-addr and the group content are read again later
|
|
955
|
+
let nested = false;
|
|
956
|
+
if (this.operatorExpecting === '>' || this.operatorExpecting === ';') {
|
|
957
|
+
nested = this._trackInner(chr);
|
|
958
|
+
}
|
|
959
|
+
else if (this.operatorExpecting === ')' && !this.escaped) {
|
|
960
|
+
// Comments nest (RFC 5322 3.2.2). Ending one at the first ')' let the ')' of a
|
|
961
|
+
// nested comment close the outer one and released the rest of it, an address
|
|
962
|
+
// included, into the header
|
|
963
|
+
if (chr === '(') {
|
|
964
|
+
this.innerComment++;
|
|
965
|
+
}
|
|
966
|
+
else if (chr === ')' && this.innerComment) {
|
|
967
|
+
this.innerComment--;
|
|
968
|
+
nested = true;
|
|
969
|
+
}
|
|
970
|
+
}
|
|
638
971
|
if (this.escaped) {
|
|
639
972
|
// ignore next condition blocks
|
|
640
973
|
}
|
|
641
|
-
else if (chr === this.operatorExpecting) {
|
|
642
|
-
this.
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
};
|
|
646
|
-
if (nextChr && ![' ', '\t', '\r', '\n', ',', ';'].includes(nextChr)) {
|
|
647
|
-
this.node.noBreak = true;
|
|
648
|
-
}
|
|
649
|
-
this.list.push(this.node);
|
|
650
|
-
this.node = null;
|
|
974
|
+
else if (chr === this.operatorExpecting && !nested) {
|
|
975
|
+
const nextChr = this.str.charAt(i + 1);
|
|
976
|
+
this._closeText(i);
|
|
977
|
+
this.list.push(nextChr && ![' ', '\t', '\r', '\n', ',', ';'].includes(nextChr) ? TOKEN_OPERATOR | TOKEN_NO_BREAK : TOKEN_OPERATOR, i, i + 1);
|
|
651
978
|
if (chr !== ')') {
|
|
652
979
|
// a comment is folding whitespace, it does not part the "@" from the "["
|
|
653
980
|
this.afterAt = false;
|
|
654
981
|
}
|
|
655
982
|
this.operatorExpecting = '';
|
|
656
983
|
this.escaped = false;
|
|
984
|
+
this._resetInner();
|
|
657
985
|
return;
|
|
658
986
|
}
|
|
659
987
|
else if (!this.operatorExpecting && !(this.inDomainLiteral && chr === ':') && chr in this.operators) {
|
|
660
|
-
this.
|
|
661
|
-
|
|
662
|
-
value: chr
|
|
663
|
-
};
|
|
664
|
-
this.list.push(this.node);
|
|
665
|
-
this.node = null;
|
|
988
|
+
this._closeText(i);
|
|
989
|
+
this.list.push(TOKEN_OPERATOR, i, i + 1);
|
|
666
990
|
if (chr !== '(') {
|
|
667
991
|
this.afterAt = false;
|
|
668
992
|
}
|
|
669
993
|
this.operatorExpecting = this.operators[chr];
|
|
670
994
|
this.escaped = false;
|
|
995
|
+
this._resetInner();
|
|
671
996
|
return;
|
|
672
997
|
}
|
|
673
998
|
else if (['"', "'", ')'].includes(this.operatorExpecting) && chr === '\\') {
|
|
@@ -676,38 +1001,117 @@ class Tokenizer {
|
|
|
676
1001
|
// only in quoted strings let an escaped parenthesis close the comment, which
|
|
677
1002
|
// released the rest of it, an address included, into the header
|
|
678
1003
|
this.escaped = true;
|
|
1004
|
+
if (this.node >= 0) {
|
|
1005
|
+
// the backslash is left out of the value
|
|
1006
|
+
this.list.slots[this.node * 3] |= TOKEN_DIRTY;
|
|
1007
|
+
}
|
|
679
1008
|
return;
|
|
680
1009
|
}
|
|
681
|
-
if (
|
|
682
|
-
this.node =
|
|
683
|
-
|
|
684
|
-
value: ''
|
|
685
|
-
};
|
|
686
|
-
this.list.push(this.node);
|
|
1010
|
+
if (this.node < 0) {
|
|
1011
|
+
this.node = this.list.push((this.operatorExpecting === '"' || this.operatorExpecting === ')' ? TOKEN_ESCAPES : 0) |
|
|
1012
|
+
(this.escaped ? TOKEN_STARTS_ESCAPED : 0), i, i);
|
|
687
1013
|
}
|
|
688
1014
|
if (chr === '\n') {
|
|
689
1015
|
// Convert newlines to spaces. Carriage return is ignored as \r and \n usually
|
|
690
1016
|
// go together anyway and there already is a WS for \n. Lone \r means something is fishy.
|
|
691
1017
|
chr = ' ';
|
|
1018
|
+
this.list.slots[this.node * 3] |= TOKEN_DIRTY;
|
|
692
1019
|
}
|
|
693
|
-
if (chr.charCodeAt(0) >= 0x21 ||
|
|
694
|
-
//
|
|
695
|
-
this.node.value += chr;
|
|
1020
|
+
if (chr.charCodeAt(0) >= 0x21 || chr === ' ' || chr === '\t') {
|
|
1021
|
+
// the character is part of the value, which is read off the field later
|
|
696
1022
|
// text inside a quoted string or a comment is not part of an addr-spec. Tracked
|
|
697
1023
|
// as it goes rather than read back off the value (see lastChars in _handleAddress)
|
|
698
1024
|
if (!this.operatorExpecting && chr !== ' ' && chr !== '\t') {
|
|
699
1025
|
this.afterAt = chr === '@';
|
|
700
1026
|
}
|
|
701
1027
|
}
|
|
1028
|
+
else {
|
|
1029
|
+
// skip command bytes
|
|
1030
|
+
this.list.slots[this.node * 3] |= TOKEN_DIRTY;
|
|
1031
|
+
}
|
|
702
1032
|
this.escaped = false;
|
|
703
1033
|
}
|
|
1034
|
+
/**
|
|
1035
|
+
* Tracks the quoted strings, comments and domain literals inside an angle-addr or a
|
|
1036
|
+
* group, which the closer of either must not be read from. A backslash escapes the next
|
|
1037
|
+
* character inside a quoted string and a comment. A domain literal is only tracked in an
|
|
1038
|
+
* angle-addr, where it opens right after an "@" and ends at the "]". dtext holds the list
|
|
1039
|
+
* separators as well, and inside the brackets they never split the list anyway. In a group
|
|
1040
|
+
* they do, so a literal is not tracked there and an unclosed "[" can not swallow the
|
|
1041
|
+
* recipients after it.
|
|
1042
|
+
*
|
|
1043
|
+
* @param chr Character from the address field
|
|
1044
|
+
* @return true while the character sits inside one of them, the one that closes it included
|
|
1045
|
+
*/
|
|
1046
|
+
_trackInner(chr) {
|
|
1047
|
+
if (this.innerEscaped) {
|
|
1048
|
+
this.innerEscaped = false;
|
|
1049
|
+
return true;
|
|
1050
|
+
}
|
|
1051
|
+
if (this.innerQuoted) {
|
|
1052
|
+
if (chr === '\\') {
|
|
1053
|
+
this.innerEscaped = true;
|
|
1054
|
+
}
|
|
1055
|
+
else if (chr === '"') {
|
|
1056
|
+
this.innerQuoted = false;
|
|
1057
|
+
}
|
|
1058
|
+
return true;
|
|
1059
|
+
}
|
|
1060
|
+
if (this.innerComment) {
|
|
1061
|
+
if (chr === '\\') {
|
|
1062
|
+
this.innerEscaped = true;
|
|
1063
|
+
}
|
|
1064
|
+
else if (chr === '(') {
|
|
1065
|
+
this.innerComment++;
|
|
1066
|
+
}
|
|
1067
|
+
else if (chr === ')') {
|
|
1068
|
+
this.innerComment--;
|
|
1069
|
+
}
|
|
1070
|
+
return true;
|
|
1071
|
+
}
|
|
1072
|
+
if (this.innerLiteral) {
|
|
1073
|
+
if (chr === ']') {
|
|
1074
|
+
this.innerLiteral = false;
|
|
1075
|
+
}
|
|
1076
|
+
return true;
|
|
1077
|
+
}
|
|
1078
|
+
if (chr === '"') {
|
|
1079
|
+
this.innerQuoted = true;
|
|
1080
|
+
return true;
|
|
1081
|
+
}
|
|
1082
|
+
if (chr === '(') {
|
|
1083
|
+
this.innerComment = 1;
|
|
1084
|
+
return true;
|
|
1085
|
+
}
|
|
1086
|
+
if (chr === '[' && this.afterAt && this.operatorExpecting === '>') {
|
|
1087
|
+
this.innerLiteral = true;
|
|
1088
|
+
return true;
|
|
1089
|
+
}
|
|
1090
|
+
// afterAt is otherwise only kept outside the operators and is cleared when one opens
|
|
1091
|
+
// or ends, so inside an angle-addr it serves the literal tracked here
|
|
1092
|
+
if (!_isSpaceCode(chr.charCodeAt(0))) {
|
|
1093
|
+
this.afterAt = chr === '@';
|
|
1094
|
+
}
|
|
1095
|
+
return false;
|
|
1096
|
+
}
|
|
1097
|
+
/**
|
|
1098
|
+
* Forgets the nesting collected for the operator that just opened or ended
|
|
1099
|
+
*/
|
|
1100
|
+
_resetInner() {
|
|
1101
|
+
this.innerQuoted = false;
|
|
1102
|
+
this.innerComment = 0;
|
|
1103
|
+
this.innerLiteral = false;
|
|
1104
|
+
this.innerEscaped = false;
|
|
1105
|
+
}
|
|
704
1106
|
}
|
|
1107
|
+
/* eslint-enable no-bitwise */
|
|
705
1108
|
/**
|
|
706
1109
|
* Maximum recursion depth for parsing nested groups.
|
|
707
1110
|
* RFC 5322 doesn't allow nested groups, so this is a safeguard against
|
|
708
|
-
* malicious input that could cause stack overflow.
|
|
1111
|
+
* malicious input that could cause stack overflow. Every level parses what is left of the
|
|
1112
|
+
* value once more, so the limit is also the factor a crafted header multiplies the parse by.
|
|
709
1113
|
*/
|
|
710
|
-
const MAX_NESTED_GROUP_DEPTH =
|
|
1114
|
+
const MAX_NESTED_GROUP_DEPTH = 10;
|
|
711
1115
|
/**
|
|
712
1116
|
* Parses an address list, recursing into the groups it holds.
|
|
713
1117
|
*
|
|
@@ -727,31 +1131,28 @@ function _parseAddressList(str, depth) {
|
|
|
727
1131
|
}
|
|
728
1132
|
const tokenizer = new Tokenizer(str);
|
|
729
1133
|
const tokens = tokenizer.tokenize();
|
|
730
|
-
const addresses = [];
|
|
731
|
-
let address = [];
|
|
732
1134
|
const parsedAddresses = [];
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
1135
|
+
// Every address is handed over as the range of the token list it covers, as soon as the
|
|
1136
|
+
// separator after it is seen. Copying the tokens of each address into an array of its own,
|
|
1137
|
+
// and collecting those before handling any, held a second structure as large as the token
|
|
1138
|
+
// list for the whole parse, and an array that receives a single push is allocated room for
|
|
1139
|
+
// seventeen: a list of one letter names took some 80 times its size in that alone
|
|
1140
|
+
let from = 0;
|
|
1141
|
+
for (let i = 0, len = tokens.length; i <= len; i++) {
|
|
1142
|
+
const operator = i < len ? tokens.operatorAt(i) : '';
|
|
1143
|
+
if (i < len && operator !== ',' && operator !== ';') {
|
|
1144
|
+
continue;
|
|
739
1145
|
}
|
|
740
|
-
|
|
741
|
-
|
|
1146
|
+
if (i > from) {
|
|
1147
|
+
const handled = _handleAddress(tokens, from, i, depth);
|
|
1148
|
+
// Appended in place. Rebuilding the accumulator with concat() would copy every
|
|
1149
|
+
// entry collected so far on each address, making a flat list cost O(n^2).
|
|
1150
|
+
for (let j = 0; j < handled.length; j++) {
|
|
1151
|
+
parsedAddresses.push(handled[j]);
|
|
1152
|
+
}
|
|
742
1153
|
}
|
|
743
|
-
|
|
744
|
-
if (address.length) {
|
|
745
|
-
addresses.push(address);
|
|
1154
|
+
from = i + 1;
|
|
746
1155
|
}
|
|
747
|
-
addresses.forEach(addr => {
|
|
748
|
-
const handled = _handleAddress(addr, depth);
|
|
749
|
-
// Appended in place. Rebuilding the accumulator with concat() would copy every
|
|
750
|
-
// entry collected so far on each address, making a flat list cost O(n^2).
|
|
751
|
-
for (let i = 0; i < handled.length; i++) {
|
|
752
|
-
parsedAddresses.push(handled[i]);
|
|
753
|
-
}
|
|
754
|
-
});
|
|
755
1156
|
// Merge fragments produced when unquoted display names contain commas.
|
|
756
1157
|
// "Joe Foo, PhD <joe@example.com>" is split on the comma into
|
|
757
1158
|
// [{name:"Joe Foo", address:""}, {name:"PhD", address:"joe@example.com"}].
|