2022-03-03 15:57:19 -05:00
|
|
|
// @ts-ignore: Module has no default export
|
2020-08-19 13:27:35 -05:00
|
|
|
import crypto from 'crypto';
|
2021-01-30 17:41:33 -05:00
|
|
|
import MockDate from 'mockdate';
|
|
|
|
|
|
|
|
import { DEFAULT_BCRYPT_ROUNDS } from '../src/htpasswd';
|
2020-09-16 23:48:16 -05:00
|
|
|
import {
|
2021-10-29 10:33:05 -05:00
|
|
|
HtpasswdHashAlgorithm,
|
2020-09-16 23:48:16 -05:00
|
|
|
addUserToHTPasswd,
|
|
|
|
changePasswordToHTPasswd,
|
2021-01-30 17:41:33 -05:00
|
|
|
generateHtpasswdLine,
|
2021-10-29 10:33:05 -05:00
|
|
|
lockAndRead,
|
|
|
|
parseHTPasswd,
|
|
|
|
sanityCheck,
|
|
|
|
verifyPassword,
|
2020-09-16 23:48:16 -05:00
|
|
|
} from '../src/utils';
|
2020-08-19 13:27:35 -05:00
|
|
|
|
|
|
|
const mockReadFile = jest.fn();
|
|
|
|
const mockUnlockFile = jest.fn();
|
|
|
|
|
2021-01-30 17:41:33 -05:00
|
|
|
const defaultHashConfig = {
|
|
|
|
algorithm: HtpasswdHashAlgorithm.bcrypt,
|
|
|
|
rounds: DEFAULT_BCRYPT_ROUNDS,
|
|
|
|
};
|
|
|
|
|
|
|
|
const mockTimeAndRandomBytes = () => {
|
|
|
|
MockDate.set('2018-01-14T11:17:40.712Z');
|
|
|
|
crypto.randomBytes = jest.fn(() => {
|
|
|
|
return {
|
|
|
|
toString: (): string => '$6',
|
|
|
|
};
|
|
|
|
});
|
|
|
|
Math.random = jest.fn(() => 0.38849);
|
|
|
|
};
|
|
|
|
|
2020-08-19 13:27:35 -05:00
|
|
|
jest.mock('@verdaccio/file-locking', () => ({
|
|
|
|
readFile: () => mockReadFile(),
|
|
|
|
unlockFile: () => mockUnlockFile(),
|
|
|
|
}));
|
|
|
|
|
|
|
|
describe('parseHTPasswd', () => {
|
|
|
|
it('should parse the password for a single line', () => {
|
|
|
|
const input = 'test:$6b9MlB3WUELU:autocreated 2017-11-06T18:17:21.957Z';
|
|
|
|
const output = { test: '$6b9MlB3WUELU' };
|
|
|
|
expect(parseHTPasswd(input)).toEqual(output);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should parse the password for two lines', () => {
|
|
|
|
const input = `user1:$6b9MlB3WUELU:autocreated 2017-11-06T18:17:21.957Z
|
|
|
|
user2:$6FrCaT/v0dwE:autocreated 2017-12-14T13:30:20.838Z`;
|
|
|
|
const output = { user1: '$6b9MlB3WUELU', user2: '$6FrCaT/v0dwE' };
|
|
|
|
expect(parseHTPasswd(input)).toEqual(output);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should parse the password for multiple lines', () => {
|
|
|
|
const input = `user1:$6b9MlB3WUELU:autocreated 2017-11-06T18:17:21.957Z
|
|
|
|
user2:$6FrCaT/v0dwE:autocreated 2017-12-14T13:30:20.838Z
|
|
|
|
user3:$6FrCdfd\v0dwE:autocreated 2017-12-14T13:30:20.838Z
|
|
|
|
user4:$6FrCasdvppdwE:autocreated 2017-12-14T13:30:20.838Z`;
|
|
|
|
const output = {
|
|
|
|
user1: '$6b9MlB3WUELU',
|
|
|
|
user2: '$6FrCaT/v0dwE',
|
|
|
|
user3: '$6FrCdfd\v0dwE',
|
|
|
|
user4: '$6FrCasdvppdwE',
|
|
|
|
};
|
|
|
|
expect(parseHTPasswd(input)).toEqual(output);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('verifyPassword', () => {
|
2022-03-03 15:57:19 -05:00
|
|
|
it('should verify the MD5/Crypt3 password with true', async () => {
|
2020-08-19 13:27:35 -05:00
|
|
|
const input = ['test', '$apr1$sKXK9.lG$rZ4Iy63Vtn8jF9/USc4BV0'];
|
2022-03-03 15:57:19 -05:00
|
|
|
expect(await verifyPassword(input[0], input[1])).toBeTruthy();
|
2020-08-19 13:27:35 -05:00
|
|
|
});
|
2022-03-03 15:57:19 -05:00
|
|
|
it('should verify the MD5/Crypt3 password with false', async () => {
|
2020-08-19 13:27:35 -05:00
|
|
|
const input = ['testpasswordchanged', '$apr1$sKXK9.lG$rZ4Iy63Vtn8jF9/USc4BV0'];
|
2022-03-03 15:57:19 -05:00
|
|
|
expect(await verifyPassword(input[0], input[1])).toBeFalsy();
|
2020-08-19 13:27:35 -05:00
|
|
|
});
|
2022-03-03 15:57:19 -05:00
|
|
|
it('should verify the plain password with true', async () => {
|
2020-08-19 13:27:35 -05:00
|
|
|
const input = ['testpasswordchanged', '{PLAIN}testpasswordchanged'];
|
2022-03-03 15:57:19 -05:00
|
|
|
expect(await verifyPassword(input[0], input[1])).toBeTruthy();
|
2020-08-19 13:27:35 -05:00
|
|
|
});
|
2022-03-03 15:57:19 -05:00
|
|
|
it('should verify the plain password with false', async () => {
|
2020-08-19 13:27:35 -05:00
|
|
|
const input = ['testpassword', '{PLAIN}testpasswordchanged'];
|
2022-03-03 15:57:19 -05:00
|
|
|
expect(await verifyPassword(input[0], input[1])).toBeFalsy();
|
2020-08-19 13:27:35 -05:00
|
|
|
});
|
2022-03-03 15:57:19 -05:00
|
|
|
it('should verify the crypto SHA password with true', async () => {
|
2020-08-19 13:27:35 -05:00
|
|
|
const input = ['testpassword', '{SHA}i7YRj4/Wk1rQh2o740pxfTJwj/0='];
|
2022-03-03 15:57:19 -05:00
|
|
|
expect(await verifyPassword(input[0], input[1])).toBeTruthy();
|
2020-08-19 13:27:35 -05:00
|
|
|
});
|
2022-03-03 15:57:19 -05:00
|
|
|
it('should verify the crypto SHA password with false', async () => {
|
2020-08-19 13:27:35 -05:00
|
|
|
const input = ['testpasswordchanged', '{SHA}i7YRj4/Wk1rQh2o740pxfTJwj/0='];
|
2022-03-03 15:57:19 -05:00
|
|
|
expect(await verifyPassword(input[0], input[1])).toBeFalsy();
|
2020-08-19 13:27:35 -05:00
|
|
|
});
|
2022-03-03 15:57:19 -05:00
|
|
|
it('should verify the bcrypt password with true', async () => {
|
2020-08-19 13:27:35 -05:00
|
|
|
const input = ['testpassword', '$2y$04$Wqed4yN0OktGbiUdxSTwtOva1xfESfkNIZfcS9/vmHLsn3.lkFxJO'];
|
2022-03-03 15:57:19 -05:00
|
|
|
expect(await verifyPassword(input[0], input[1])).toBeTruthy();
|
2020-08-19 13:27:35 -05:00
|
|
|
});
|
2022-03-03 15:57:19 -05:00
|
|
|
it('should verify the bcrypt password with false', async () => {
|
2020-09-16 23:48:16 -05:00
|
|
|
const input = [
|
|
|
|
'testpasswordchanged',
|
|
|
|
'$2y$04$Wqed4yN0OktGbiUdxSTwtOva1xfESfkNIZfcS9/vmHLsn3.lkFxJO',
|
|
|
|
];
|
2022-03-03 15:57:19 -05:00
|
|
|
expect(await verifyPassword(input[0], input[1])).toBeFalsy();
|
2020-08-19 13:27:35 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-01-30 17:41:33 -05:00
|
|
|
describe('generateHtpasswdLine', () => {
|
|
|
|
beforeAll(mockTimeAndRandomBytes);
|
|
|
|
|
|
|
|
const [user, passwd] = ['username', 'password'];
|
|
|
|
|
|
|
|
it('should correctly generate line for md5', () => {
|
|
|
|
const md5Conf = { algorithm: HtpasswdHashAlgorithm.md5 };
|
|
|
|
expect(generateHtpasswdLine(user, passwd, md5Conf)).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should correctly generate line for sha1', () => {
|
|
|
|
const sha1Conf = { algorithm: HtpasswdHashAlgorithm.sha1 };
|
|
|
|
expect(generateHtpasswdLine(user, passwd, sha1Conf)).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should correctly generate line for crypt', () => {
|
|
|
|
const cryptConf = { algorithm: HtpasswdHashAlgorithm.crypt };
|
|
|
|
expect(generateHtpasswdLine(user, passwd, cryptConf)).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should correctly generate line for bcrypt', () => {
|
|
|
|
const bcryptAlgoConfig = {
|
|
|
|
algorithm: HtpasswdHashAlgorithm.bcrypt,
|
|
|
|
rounds: 2,
|
|
|
|
};
|
|
|
|
expect(generateHtpasswdLine(user, passwd, bcryptAlgoConfig)).toMatchSnapshot();
|
2020-08-19 13:27:35 -05:00
|
|
|
});
|
2021-01-30 17:41:33 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('addUserToHTPasswd - bcrypt', () => {
|
|
|
|
beforeAll(mockTimeAndRandomBytes);
|
2020-08-19 13:27:35 -05:00
|
|
|
|
|
|
|
it('should add new htpasswd to the end', () => {
|
|
|
|
const input = ['', 'username', 'password'];
|
2021-01-30 17:41:33 -05:00
|
|
|
expect(addUserToHTPasswd(input[0], input[1], input[2], defaultHashConfig)).toMatchSnapshot();
|
2020-08-19 13:27:35 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should add new htpasswd to the end in multiline input', () => {
|
|
|
|
const body = `test1:$6b9MlB3WUELU:autocreated 2017-11-06T18:17:21.957Z
|
|
|
|
test2:$6FrCaT/v0dwE:autocreated 2017-12-14T13:30:20.838Z`;
|
|
|
|
const input = [body, 'username', 'password'];
|
2021-01-30 17:41:33 -05:00
|
|
|
expect(addUserToHTPasswd(input[0], input[1], input[2], defaultHashConfig)).toMatchSnapshot();
|
2020-08-19 13:27:35 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw an error for incorrect username with space', () => {
|
|
|
|
const [a, b, c] = ['', 'firstname lastname', 'password'];
|
2021-01-30 17:41:33 -05:00
|
|
|
expect(() => addUserToHTPasswd(a, b, c, defaultHashConfig)).toThrowErrorMatchingSnapshot();
|
2020-08-19 13:27:35 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('lockAndRead', () => {
|
|
|
|
it('should call the readFile method', () => {
|
|
|
|
// console.log(fileLocking);
|
|
|
|
// const spy = jest.spyOn(fileLocking, 'readFile');
|
|
|
|
const cb = (): void => {};
|
|
|
|
lockAndRead('.htpasswd', cb);
|
|
|
|
expect(mockReadFile).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('sanityCheck', () => {
|
|
|
|
let users;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
users = { test: '$6FrCaT/v0dwE' };
|
|
|
|
});
|
|
|
|
|
2022-03-03 15:57:19 -05:00
|
|
|
test('should throw error for user already exists', async () => {
|
2020-08-19 13:27:35 -05:00
|
|
|
const verifyFn = jest.fn();
|
2022-03-03 15:57:19 -05:00
|
|
|
const input = await sanityCheck('test', users.test, verifyFn, users, Infinity);
|
2020-08-19 13:27:35 -05:00
|
|
|
expect(input.status).toEqual(401);
|
|
|
|
expect(input.message).toEqual('unauthorized access');
|
|
|
|
expect(verifyFn).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
2022-03-03 15:57:19 -05:00
|
|
|
test('should throw error for registration disabled of users', async () => {
|
2020-08-19 13:27:35 -05:00
|
|
|
const verifyFn = (): void => {};
|
2022-03-03 15:57:19 -05:00
|
|
|
const input = await sanityCheck('username', users.test, verifyFn, users, -1);
|
2020-08-19 13:27:35 -05:00
|
|
|
expect(input.status).toEqual(409);
|
|
|
|
expect(input.message).toEqual('user registration disabled');
|
|
|
|
});
|
|
|
|
|
2022-03-03 15:57:19 -05:00
|
|
|
test('should throw error max number of users', async () => {
|
2020-08-19 13:27:35 -05:00
|
|
|
const verifyFn = (): void => {};
|
2022-03-03 15:57:19 -05:00
|
|
|
const input = await sanityCheck('username', users.test, verifyFn, users, 1);
|
2020-08-19 13:27:35 -05:00
|
|
|
expect(input.status).toEqual(403);
|
|
|
|
expect(input.message).toEqual('maximum amount of users reached');
|
|
|
|
});
|
|
|
|
|
2022-03-03 15:57:19 -05:00
|
|
|
test('should not throw anything and sanity check', async () => {
|
2020-08-19 13:27:35 -05:00
|
|
|
const verifyFn = (): void => {};
|
2022-03-03 15:57:19 -05:00
|
|
|
const input = await sanityCheck('username', users.test, verifyFn, users, 2);
|
2020-08-19 13:27:35 -05:00
|
|
|
expect(input).toBeNull();
|
|
|
|
});
|
|
|
|
|
2022-03-03 15:57:19 -05:00
|
|
|
test('should throw error for required username field', async () => {
|
2020-08-19 13:27:35 -05:00
|
|
|
const verifyFn = (): void => {};
|
2022-03-03 15:57:19 -05:00
|
|
|
const input = await sanityCheck(undefined, users.test, verifyFn, users, 2);
|
2020-08-19 13:27:35 -05:00
|
|
|
expect(input.message).toEqual('username and password is required');
|
|
|
|
expect(input.status).toEqual(400);
|
|
|
|
});
|
|
|
|
|
2022-03-03 15:57:19 -05:00
|
|
|
test('should throw error for required password field', async () => {
|
2020-08-19 13:27:35 -05:00
|
|
|
const verifyFn = (): void => {};
|
2022-03-03 15:57:19 -05:00
|
|
|
const input = await sanityCheck('username', undefined, verifyFn, users, 2);
|
2020-08-19 13:27:35 -05:00
|
|
|
expect(input.message).toEqual('username and password is required');
|
|
|
|
expect(input.status).toEqual(400);
|
|
|
|
});
|
|
|
|
|
2022-03-03 15:57:19 -05:00
|
|
|
test('should throw error for required username & password fields', async () => {
|
2020-08-19 13:27:35 -05:00
|
|
|
const verifyFn = (): void => {};
|
2022-03-03 15:57:19 -05:00
|
|
|
const input = await sanityCheck(undefined, undefined, verifyFn, users, 2);
|
2020-08-19 13:27:35 -05:00
|
|
|
expect(input.message).toEqual('username and password is required');
|
|
|
|
expect(input.status).toEqual(400);
|
|
|
|
});
|
|
|
|
|
2022-03-03 15:57:19 -05:00
|
|
|
test('should throw error for existing username and password', async () => {
|
2020-08-19 13:27:35 -05:00
|
|
|
const verifyFn = jest.fn(() => true);
|
2022-03-03 15:57:19 -05:00
|
|
|
const input = await sanityCheck('test', users.test, verifyFn, users, 2);
|
2020-08-19 13:27:35 -05:00
|
|
|
expect(input.status).toEqual(409);
|
|
|
|
expect(input.message).toEqual('username is already registered');
|
|
|
|
expect(verifyFn).toHaveBeenCalledTimes(1);
|
|
|
|
});
|
|
|
|
|
2020-10-24 06:17:21 -05:00
|
|
|
test(
|
|
|
|
'should throw error for existing username and password with max number ' + 'of users reached',
|
2022-03-03 15:57:19 -05:00
|
|
|
async () => {
|
2020-10-24 06:17:21 -05:00
|
|
|
const verifyFn = jest.fn(() => true);
|
2022-03-03 15:57:19 -05:00
|
|
|
const input = await sanityCheck('test', users.test, verifyFn, users, 1);
|
2020-10-24 06:17:21 -05:00
|
|
|
expect(input.status).toEqual(409);
|
|
|
|
expect(input.message).toEqual('username is already registered');
|
|
|
|
expect(verifyFn).toHaveBeenCalledTimes(1);
|
|
|
|
}
|
|
|
|
);
|
2020-08-19 13:27:35 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('changePasswordToHTPasswd', () => {
|
2022-03-03 15:57:19 -05:00
|
|
|
test('should throw error for wrong password', async () => {
|
2020-08-19 13:27:35 -05:00
|
|
|
const body = 'test:$6b9MlB3WUELU:autocreated 2017-11-06T18:17:21.957Z';
|
|
|
|
|
|
|
|
try {
|
2022-03-03 15:57:19 -05:00
|
|
|
await changePasswordToHTPasswd(
|
2021-01-30 17:41:33 -05:00
|
|
|
body,
|
|
|
|
'test',
|
|
|
|
'somerandompassword',
|
|
|
|
'newPassword',
|
|
|
|
defaultHashConfig
|
|
|
|
);
|
2021-08-30 01:19:08 -05:00
|
|
|
} catch (error: any) {
|
2022-03-03 15:57:19 -05:00
|
|
|
expect(error.message).toEqual(
|
|
|
|
`Unable to change password for user 'test': invalid old password`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should throw error when user does not exist', async () => {
|
|
|
|
const body = 'test:$6b9MlB3WUELU:autocreated 2017-11-06T18:17:21.957Z';
|
|
|
|
|
|
|
|
try {
|
|
|
|
await changePasswordToHTPasswd(
|
|
|
|
body,
|
|
|
|
'test2',
|
|
|
|
'somerandompassword',
|
|
|
|
'newPassword',
|
|
|
|
defaultHashConfig
|
|
|
|
);
|
|
|
|
} catch (error: any) {
|
|
|
|
expect(error.message).toEqual(
|
|
|
|
`Unable to change password for user 'test2': user does not currently exist`
|
|
|
|
);
|
2020-08-19 13:27:35 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-03-03 15:57:19 -05:00
|
|
|
test('should change the password', async () => {
|
2020-08-19 13:27:35 -05:00
|
|
|
const body = 'root:$6qLTHoPfGLy2:autocreated 2018-08-20T13:38:12.164Z';
|
|
|
|
|
2021-01-30 17:41:33 -05:00
|
|
|
expect(
|
2022-03-03 15:57:19 -05:00
|
|
|
await changePasswordToHTPasswd(body, 'root', 'demo123', 'newPassword', defaultHashConfig)
|
2021-01-30 17:41:33 -05:00
|
|
|
).toMatchSnapshot();
|
2020-08-19 13:27:35 -05:00
|
|
|
});
|
|
|
|
});
|