Skip to content

Commit 64a977c

Browse files
committedApr 6, 2022
fix: correct username and token validation
The current username and token validation regular expressions will match any string. This adds tests and fixes the regular expressions.
1 parent 5e85166 commit 64a977c

File tree

2 files changed

+61
-8
lines changed

2 files changed

+61
-8
lines changed
 

‎lib/auth.js

+24-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import assert from 'node:assert';
21
import fs from 'node:fs';
32
import { ClientRequest } from 'node:http';
43
import util from 'node:util';
@@ -11,9 +10,27 @@ const ghauth = util.promisify(ghauthBase);
1110

1211
export default lazy(auth);
1312

13+
function errorExit(message) {
14+
process.stderr.write(`${message}\n`);
15+
process.exit(1);
16+
}
17+
1418
function check(username, token) {
15-
assert(typeof username === 'string' && /^[a-zA-Z0-9]*/.test(username));
16-
assert(typeof token === 'string' && /^[0-9a-f]*/.test(token));
19+
if (typeof username !== 'string') {
20+
errorExit(`username must be a string, received ${typeof username}`);
21+
}
22+
if (!/^[a-zA-Z0-9-]+$/.test(username)) {
23+
errorExit(
24+
'username may only contain alphanumeric characters or hyphens, ' +
25+
`received ${username}`
26+
);
27+
}
28+
if (typeof token !== 'string') {
29+
errorExit(`token must be a string, received ${typeof token}`);
30+
}
31+
if (!/^[0-9a-f]+$/.test(token)) {
32+
errorExit(`token must be lowercase hexadecimal, received ${token}`);
33+
}
1734
}
1835

1936
function lazy(fn) {
@@ -36,8 +53,7 @@ async function tryCreateGitHubToken(githubAuth) {
3653
note: 'node-core-utils CLI tools'
3754
});
3855
} catch (e) {
39-
process.stderr.write(`Could not get token: ${e.message}\n`);
40-
process.exit(1);
56+
errorExit(`Could not get token: ${e.message}`);
4157
}
4258
return credentials;
4359
}
@@ -84,11 +100,11 @@ async function auth(
84100
if (options.jenkins) {
85101
const { username, jenkins_token } = getMergedConfig();
86102
if (!username || !jenkins_token) {
87-
process.stdout.write(
103+
errorExit(
88104
'Get your Jenkins API token in https://ci.nodejs.org/me/configure ' +
89105
'and run the following command to add it to your ncu config: ' +
90-
'ncu-config --global set jenkins_token TOKEN\n');
91-
process.exit(1);
106+
'ncu-config --global set jenkins_token TOKEN'
107+
);
92108
};
93109
check(username, jenkins_token);
94110
result.jenkins = encode(username, jenkins_token);

‎test/unit/auth.test.js

+37
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,43 @@ describe('auth', async function() {
7070
'Could not get token: Bad credentials\n', 'run-auth-error'
7171
);
7272
});
73+
74+
it('does not accept a non-string username', async function() {
75+
this.timeout(2000);
76+
await runAuthScript(
77+
{ HOME: { username: {}, token: '0123456789abcdef' } },
78+
[],
79+
'username must be a string, received object\n'
80+
);
81+
});
82+
83+
it('does not accept a non-string token', async function() {
84+
this.timeout(2000);
85+
await runAuthScript(
86+
{ HOME: { username: 'nyancat', token: 42 } },
87+
[],
88+
'token must be a string, received number\n'
89+
);
90+
});
91+
92+
it('does not accept an invalid username format', async function() {
93+
this.timeout(2000);
94+
await runAuthScript(
95+
{ HOME: { username: ' ^^^ ', token: '0123456789abcdef' } },
96+
[],
97+
'username may only contain alphanumeric characters or hyphens, ' +
98+
'received ^^^ \n'
99+
);
100+
});
101+
102+
it('does not accept an invalid token format', async function() {
103+
this.timeout(2000);
104+
await runAuthScript(
105+
{ HOME: { username: 'nyancat', token: '0123456789ABCDEF' } },
106+
[],
107+
'token must be lowercase hexadecimal, received 0123456789ABCDEF\n'
108+
);
109+
});
73110
});
74111

75112
// ncurc: { HOME: 'text to put in home ncurc',

0 commit comments

Comments
 (0)
Please sign in to comment.