Files
Gabriel20xx 97c237be80
Lint / Lint (push) Successful in 1m8s
Tests / Go backend (push) Failing after 1m23s
Tests / Dashboard checks (push) Failing after 38s
Docker / Docker Build & Push (push) Failing after 6m29s
feat(sync): paginate Android sync with change tokens
2026-10-20 15:27:53 +00:00

142 lines
6.3 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import crypto from 'node:crypto';
import {
ResetManager,
createSessionToken,
hashPassword,
hashPasswordAsync,
hashToken,
passwordHashNeedsUpgrade,
verifyAndUpgradePasswordAsync,
verifyPassword,
verifyPasswordAsync,
} from '../src/auth.js';
import { totpProvisioningUri, verifyTotp, verifyTotpStep } from '../src/totp.js';
test('reset codes are printed, case-insensitive, expiring, and one-time', () => {
const messages = [];
const manager = new ResetManager({ log: (message) => messages.push(message) });
const code = manager.request();
assert.equal(code.length, 8);
assert.equal(messages.length, 1);
assert.match(messages[0], new RegExp(`ADMIN RESET CODE: ${code}`));
assert.equal(manager.confirm(` ${code.toLowerCase()} `), true);
assert.equal(manager.confirm(code), false);
});
test('expired reset codes are rejected', () => {
const manager = new ResetManager({ ttlMs: -1, log: () => {} });
const code = manager.request();
assert.equal(manager.confirm(code), false);
});
test('password hashes verify and reject a different password', () => {
const hash = hashPassword('correct horse battery staple');
assert.equal(verifyPassword('correct horse battery staple', hash), true);
assert.equal(verifyPassword('wrong password', hash), false);
});
test('new password hashes use the shared bcrypt scheme and cost', () => {
const hash = hashPassword('correct horse battery staple');
assert.match(hash, /^bcrypt\$\$2b\$12\$/);
});
test('legacy salt:hex hashes still verify at the original iteration count', () => {
const salt = crypto.randomBytes(16).toString('hex');
const hash = crypto.pbkdf2Sync('legacy password', salt, 100000, 32, 'sha256').toString('hex');
assert.equal(verifyPassword('legacy password', `${salt}:${hash}`), true);
assert.equal(verifyPassword('wrong password', `${salt}:${hash}`), false);
});
test('a hash with a malformed iteration count never verifies', () => {
assert.equal(verifyPassword('correct horse battery staple', 'pbkdf2-sha256$0$aa$bb'), false);
assert.equal(verifyPassword('correct horse battery staple', 'pbkdf2-sha256$x$aa$bb'), false);
});
test('async password helpers produce and verify the same bcrypt format', async () => {
const hash = await hashPasswordAsync('correct horse battery staple');
assert.match(hash, /^bcrypt\$\$2b\$12\$/);
assert.equal(await verifyPasswordAsync('correct horse battery staple', hash), true);
assert.equal(await verifyPasswordAsync('wrong password', hash), false);
assert.equal(verifyPassword('correct horse battery staple', hash), true);
});
test('formats this module does not verify fail closed', () => {
for (const stored of ['', '$2b$12$abc', 'bcrypt$$2a$12$abc', 'bcrypt-sha256$$2b$12$abc', 'pbkdf2-sha256$600000$00$00$extra']) {
assert.equal(verifyPassword('correct horse battery staple', stored), false);
}
assert.equal(passwordHashNeedsUpgrade('$2b$12$abc'), true);
});
test('a legacy salt:hex hash is rewritten in the current scheme after a successful login', async () => {
const salt = crypto.randomBytes(16).toString('hex');
const legacy = `${salt}:${crypto.pbkdf2Sync('legacy password', salt, 100000, 32, 'sha256').toString('hex')}`;
assert.equal(passwordHashNeedsUpgrade(legacy), true);
const result = await verifyAndUpgradePasswordAsync('legacy password', legacy);
assert.equal(result.valid, true);
assert.match(result.upgradedHash, /^bcrypt\$\$2b\$12\$/);
assert.equal(passwordHashNeedsUpgrade(result.upgradedHash), false);
assert.equal(verifyPassword('legacy password', result.upgradedHash), true);
});
test('a long legacy PBKDF2 password is migrated without truncation', async () => {
const longPassword = 'a'.repeat(80);
const salt = crypto.randomBytes(16).toString('hex');
const legacy = `pbkdf2-sha256$600000$${salt}$${crypto.pbkdf2Sync(longPassword, salt, 600000, 32, 'sha256').toString('hex')}`;
const result = await verifyAndUpgradePasswordAsync(longPassword, legacy);
assert.equal(result.valid, true);
assert.match(result.upgradedHash, /^bcrypt-sha256\$\$2b\$12\$/);
assert.equal(await verifyPasswordAsync(longPassword, result.upgradedHash), true);
assert.equal(await verifyPasswordAsync(`${longPassword}x`, result.upgradedHash), false);
});
test('a low-cost bcrypt hash is rewritten, and a short legacy password still upgrades', async () => {
const bcrypt = (await import('bcryptjs')).default;
const low = `bcrypt$${await bcrypt.hash('correct horse battery staple', 4)}`;
const lowResult = await verifyAndUpgradePasswordAsync('correct horse battery staple', low);
assert.match(lowResult.upgradedHash, /^bcrypt\$\$2b\$12\$/);
const salt = crypto.randomBytes(16).toString('hex');
const shortLegacy = `${salt}:${crypto.pbkdf2Sync('short', salt, 100000, 32, 'sha256').toString('hex')}`;
const shortResult = await verifyAndUpgradePasswordAsync('short', shortLegacy);
assert.equal(shortResult.valid, true);
assert.equal(verifyPassword('short', shortResult.upgradedHash), true);
});
test('a wrong password neither verifies nor upgrades, and a current hash is not rewritten', async () => {
const hash = await hashPasswordAsync('correct horse battery staple');
assert.deepEqual(await verifyAndUpgradePasswordAsync('wrong password', hash), { valid: false, upgradedHash: null });
const current = await hashPasswordAsync('correct horse battery staple');
assert.equal(passwordHashNeedsUpgrade(current), false);
assert.deepEqual(await verifyAndUpgradePasswordAsync('correct horse battery staple', current), {
valid: true,
upgradedHash: null,
});
});
test('TOTP verifies RFC 6238 SHA-1 vectors', () => {
const secret = 'GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ';
const vectors = [
[59, '287082'],
[1111111109, '081804'],
[1111111111, '050471'],
[1234567890, '005924'],
[2000000000, '279037'],
];
for (const [seconds, code] of vectors) {
assert.equal(verifyTotp(secret, code, seconds * 1000), true);
assert.equal(verifyTotpStep(secret, code, seconds * 1000), Math.floor(seconds / 30));
}
assert.match(totpProvisioningUri('Example App', 'admin@example.com', secret), /^otpauth:\/\/totp\//);
});
test('session token hashes are not the raw token', () => {
const { token, tokenHash } = createSessionToken();
assert.notEqual(token, tokenHash);
assert.equal(hashToken(token), tokenHash);
});