address-rfc2822
Advanced tools
Comparing version 2.0.1 to 2.0.2
@@ -0,1 +1,5 @@ | ||
## 2.0.2 - 2018-02-24 | ||
- Fix a possible regexp backtracking DoS | ||
## 2.0.1 - 2017-06-26 | ||
@@ -2,0 +6,0 @@ |
57
index.js
@@ -1,11 +0,11 @@ | ||
"use strict"; | ||
'use strict'; | ||
var ea_lib = require("email-addresses"); | ||
const ea_lib = require('email-addresses'); | ||
exports.parse = function parse (line, startAt) { | ||
if (!line) throw "Nothing to parse"; | ||
if (!line) throw 'Nothing to parse'; | ||
line = line.trim(); | ||
var addr = ea_lib({ | ||
const addr = ea_lib({ | ||
input: line, | ||
@@ -19,6 +19,5 @@ rfc6532: true, // unicode | ||
}); | ||
if (!addr) { | ||
throw "No results"; | ||
} | ||
if (!addr) throw 'No results'; | ||
// console.log("Parsed to: ", require('util').inspect(addr, {depth: 10, colors: true})); | ||
@@ -32,3 +31,3 @@ | ||
} | ||
var comments; | ||
let comments; | ||
if (adr.parts.comments) { | ||
@@ -40,3 +39,3 @@ comments = adr.parts.comments.map(function (c) { return c.tokens.trim() }).join(' ').trim(); | ||
} | ||
var l = adr.local; | ||
let l = adr.local; | ||
if (!adr.name && /:/.test(l)) l = '"' + l + '"'; | ||
@@ -69,3 +68,3 @@ return new Address(adr.name, l + '@' + adr.domain, comments); | ||
Group.prototype.name = function () { | ||
var phrase = this.phrase; | ||
let phrase = this.phrase; | ||
@@ -76,3 +75,3 @@ if (!(phrase && phrase.length)) { | ||
var name = _extract_name(phrase); | ||
const name = _extract_name(phrase); | ||
return name; | ||
@@ -88,3 +87,3 @@ } | ||
Address.prototype.host = function () { | ||
let match = /.*@(.*)$/.exec(this.address); | ||
const match = /.*@(.*)$/.exec(this.address); | ||
if (!match) return null; | ||
@@ -95,3 +94,3 @@ return match[1]; | ||
Address.prototype.user = function () { | ||
let match = /^(.*)@/.exec(this.address); | ||
const match = /^(.*)@/.exec(this.address); | ||
if (!match) return null; | ||
@@ -115,12 +114,12 @@ return match[1]; | ||
var atext = new RegExp('^(?:\\s*[\\-\\w !#$%&\'*+/=?^`{|}~]\\s*)+$'); | ||
const atext = new RegExp('^[\\-\\w !#$%&\'*+/=?^`{|}~]+$'); | ||
Address.prototype.format = function () { | ||
var phrase = this.phrase; | ||
var email = this.address; | ||
var comment = this.comment; | ||
const phrase = this.phrase; | ||
const email = this.address; | ||
let comment = this.comment; | ||
var addr = []; | ||
const addr = []; | ||
if (phrase && phrase.length) { | ||
addr.push(atext.test(phrase) ? phrase | ||
addr.push(atext.test(phrase.trim()) ? phrase | ||
: _quote_no_esc(phrase) ? phrase | ||
@@ -150,4 +149,4 @@ : ('"' + phrase + '"')); | ||
Address.prototype.name = function () { | ||
var phrase = this.phrase; | ||
var addr = this.address; | ||
let phrase = this.phrase; | ||
const addr = this.address; | ||
@@ -158,9 +157,9 @@ if (!(phrase && phrase.length)) { | ||
var name = _extract_name(phrase); | ||
let name = _extract_name(phrase); | ||
// first.last@domain address | ||
if (name === '') { | ||
let match = /([^\%\.\@_]+([\._][^\%\.\@_]+)+)[\@\%]/.exec(addr); | ||
const match = /([^%.@_]+([._][^%.@_]+)+)[@%]/.exec(addr); | ||
if (match) { | ||
name = match[1].replace(/[\._]+/g, ' '); | ||
name = match[1].replace(/[._]+/g, ' '); | ||
name = _extract_name(name); | ||
@@ -171,6 +170,6 @@ } | ||
if (name === '' && /\/g=/i.test(addr)) { // X400 style address | ||
let match = /\/g=([^\/]*)/i.exec(addr); | ||
var f = match[1]; | ||
match = /\/s=([^\/]*)/i.exec(addr); | ||
var l = match[1]; | ||
let match = /\/g=([^/]*)/i.exec(addr); | ||
const f = match[1]; | ||
match = /\/s=([^/]*)/i.exec(addr); | ||
const l = match[1]; | ||
name = _extract_name(f + " " + l); | ||
@@ -215,3 +214,3 @@ } | ||
// Using encodings, too hard. See Mail::Message::Field::Full. | ||
if (/\=\?.*?\?\=/.test(name)) return ''; | ||
if (/=?.*?\?=/.test(name)) return ''; | ||
@@ -218,0 +217,0 @@ // trim whitespace |
{ | ||
"name": "address-rfc2822", | ||
"version": "2.0.1", | ||
"version": "2.0.2", | ||
"description": "RFC 2822 (Header) email address parser", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -1,8 +0,12 @@ | ||
var parse = require('../index').parse; | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const parse = require('../index').parse; | ||
function _check (test, line, details) { | ||
test.expect(Object.keys(details).length); | ||
var parsed = parse(line)[0]; | ||
const parsed = parse(line)[0]; | ||
// console.log("Parsed: ", parsed); | ||
for (var k in details) { | ||
for (const k in details) { | ||
test.equal(parsed[k](), details[k], "Test '" + k + "' for '" + parsed[k]() + "' = '" + details[k] + "' from " + JSON.stringify(parsed)); | ||
@@ -13,6 +17,6 @@ } | ||
var raw_data = require('fs').readFileSync(__dirname + '/emails.txt', "UTF-8"); | ||
const raw_data = fs.readFileSync(path.join(__dirname, 'emails.txt'), "UTF-8"); | ||
var tests = raw_data.split(/\n\n/).map(function (rows) { | ||
var lines = rows.split(/\n/); | ||
const tests = raw_data.split(/\n\n/).map(function (rows) { | ||
const lines = rows.split(/\n/); | ||
if (lines[0] === '') lines.shift(); | ||
@@ -24,3 +28,3 @@ return lines.filter(function (l) { return !/^#/.test(l) }); | ||
tests.forEach(function (test) { | ||
var details = {}; | ||
const details = {}; | ||
details.format = test[1]; | ||
@@ -27,0 +31,0 @@ if (test[2]) { |
@@ -1,2 +0,2 @@ | ||
var address = require('../index'); | ||
const address = require('../index'); | ||
@@ -3,0 +3,0 @@ exports.isAllLower = { |
45424
13
547