Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit a7c1b2e

Browse files
committedMar 19, 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 a7c1b2e

File tree

2 files changed

+57
-8
lines changed

2 files changed

+57
-8
lines changed
 

‎lib/auth.js

Lines changed: 21 additions & 8 deletions
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,24 @@ 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(`username must be alphanumeric, received ${username}`);
24+
}
25+
if (typeof token !== 'string') {
26+
errorExit(`token must be a string, received ${typeof token}`);
27+
}
28+
if (!/^[0-9a-f]+$/.test(token)) {
29+
errorExit(`token must be lowercase hexadecimal, received ${token}`);
30+
}
1731
}
1832

1933
function lazy(fn) {
@@ -36,8 +50,7 @@ async function tryCreateGitHubToken(githubAuth) {
3650
note: 'node-core-utils CLI tools'
3751
});
3852
} catch (e) {
39-
process.stderr.write(`Could not get token: ${e.message}\n`);
40-
process.exit(1);
53+
errorExit(`Could not get token: ${e.message}`);
4154
}
4255
return credentials;
4356
}
@@ -84,11 +97,11 @@ async function auth(
8497
if (options.jenkins) {
8598
const { username, jenkins_token } = getMergedConfig();
8699
if (!username || !jenkins_token) {
87-
process.stdout.write(
100+
errorExit(
88101
'Get your Jenkins API token in https://ci.nodejs.org/me/configure ' +
89102
'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);
103+
'ncu-config --global set jenkins_token TOKEN'
104+
);
92105
};
93106
check(username, jenkins_token);
94107
result.jenkins = encode(username, jenkins_token);

‎test/unit/auth.test.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,42 @@ 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 must be alphanumeric, received ^^^ \n'
98+
);
99+
});
100+
101+
it('does not accept an invalid token format', async function() {
102+
this.timeout(2000);
103+
await runAuthScript(
104+
{ HOME: { username: 'nyancat', token: '0123456789ABCDEF' } },
105+
[],
106+
'token must be lowercase hexadecimal, received 0123456789ABCDEF\n'
107+
);
108+
});
73109
});
74110

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

0 commit comments

Comments
 (0)
Please sign in to comment.