Skip to content

Commit ac3b200

Browse files
Benjamin Coeiarna
Benjamin Coe
authored andcommitted
fix: added regex for blocking illegal characters in usernames
1 parent c800063 commit ac3b200

File tree

4 files changed

+31
-15
lines changed

4 files changed

+31
-15
lines changed

npm-user-validate.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
exports.email = email
22
exports.pw = pw
33
exports.username = username
4-
54
var requirements = exports.requirements = {
65
username: {
76
length: 'Name length must be less than or equal to 214 characters long',
87
lowerCase: 'Name must be lowercase',
98
urlSafe: 'Name may not contain non-url-safe chars',
10-
dot: 'Name may not start with "."'
9+
dot: 'Name may not start with "."',
10+
illegal: 'Name may not contain illegal character'
1111
},
1212
password: {},
1313
email: {
1414
valid: 'Email must be an email address'
1515
}
16-
};
16+
}
17+
18+
var illegalCharacterRe = new RegExp('([' + [
19+
"'"
20+
].join() + '])')
1721

1822
function username (un) {
1923
if (un !== un.toLowerCase()) {
@@ -32,6 +36,11 @@ function username (un) {
3236
return new Error(requirements.username.length)
3337
}
3438

39+
var illegal = un.match(illegalCharacterRe)
40+
if (illegal) {
41+
return new Error(requirements.username.illegal + ' "' + illegal[0] + '"')
42+
}
43+
3544
return null
3645
}
3746

test/email.test.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,25 @@ var test = require('tap').test
22
var v = require('../npm-user-validate.js').email
33

44
test('email misses an @', function (t) {
5-
err = v('namedomain')
5+
var err = v('namedomain')
66
t.type(err, 'object')
77
t.end()
88
})
99

1010
test('email misses a dot', function (t) {
11-
err = v('name@domain')
11+
var err = v('name@domain')
1212
t.type(err, 'object')
1313
t.end()
1414
})
1515

1616
test('email misses a string before the @', function (t) {
17-
err = v('@domain')
17+
var err = v('@domain')
1818
t.type(err, 'object')
1919
t.end()
2020
})
2121

2222
test('email is ok', function (t) {
23-
err = v('[email protected]')
23+
var err = v('[email protected]')
2424
t.type(err, 'null')
2525
t.end()
26-
})
26+
})

test/pw.test.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,31 @@ var test = require('tap').test
22
var v = require('../npm-user-validate.js').pw
33

44
test('pw contains a \'', function (t) {
5-
err = v('\'')
5+
var err = v('\'')
66
t.type(err, 'null')
77
t.end()
88
})
99

1010
test('pw contains a :', function (t) {
11-
err = v(':')
11+
var err = v(':')
1212
t.type(err, 'null')
1313
t.end()
1414
})
1515

1616
test('pw contains a @', function (t) {
17-
err = v('@')
17+
var err = v('@')
1818
t.notOk(err, 'null')
1919
t.end()
2020
})
2121

2222
test('pw contains a "', function (t) {
23-
err = v('"')
23+
var err = v('"')
2424
t.type(err, 'null')
2525
t.end()
2626
})
2727

2828
test('pw is ok', function (t) {
29-
err = v('duck')
29+
var err = v('duck')
3030
t.type(err, 'null')
3131
t.end()
3232
})

test/username.test.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ test('username may not contain non-url-safe chars', function (t) {
1515
t.end()
1616
})
1717

18+
test('username may not contain illegal characters', function (t) {
19+
var err = v("ben's")
20+
t.type(err, 'object')
21+
t.match(err.message, /illegal character "'"/)
22+
t.end()
23+
})
24+
1825
test('username may not start with "."', function (t) {
1926
var err = v('.username')
2027
t.type(err, 'object')
@@ -27,13 +34,13 @@ test('username may not be longer than 214 characters', function (t) {
2734
t.type(err, 'object')
2835
t.match(err.message, /less than or equal to 214/)
2936
t.end()
30-
});
37+
})
3138

3239
test('username may be as long as 214 characters', function (t) {
3340
var err = v('bacon-ipsum-dolor-amet-tongue-short-loin-landjaeger-tenderloin-ball-tip-pork-loin-porchetta-pig-pork-chop-beef-ribs-pork-belly--shankle-t-bone-turducken-tongue-landjaeger-pork-loin-beef-chicken-short-loin-porchetta')
3441
t.type(err, 'null')
3542
t.end()
36-
});
43+
})
3744

3845
test('username is ok', function (t) {
3946
var err = v('ente')

0 commit comments

Comments
 (0)