Skip to content

A local-first personal finance app

10.3 KB
Raw Blame
import * as argon2 from 'argon2';
import * as bcrypt from 'bcrypt';

import { bootstrap, getAccountDb } from '#account-db';

import {
  bootstrapPassword,
  changePassword,
  checkPassword,
  hashPassword,
  isValidPassword,
  loginWithPassword,
  setPasswordHash,
  verifyPassword,
} from './password';

vi.mock('argon2', { spy: true });

const FALLBACK_OWNER_ID = 'password-test-owner';

function getStoredPasswordHash() {
  const row = getAccountDb().first(
    "SELECT extra_data FROM auth WHERE method = 'password'",
  );
  return row ? row.extra_data : null;
}

function getAuthMethods() {
  return getAccountDb().all('SELECT method, active FROM auth ORDER BY method');
}

function insertOpenIdAuth(extraData) {
  getAccountDb().mutate(
    'INSERT INTO auth (method, display_name, extra_data, active) VALUES (?, ?, ?, ?)',
    ['openid', 'OpenID', JSON.stringify(extraData), 1],
  );
}

function ensureOwner() {
  const db = getAccountDb();
  const owner = db.first("SELECT id FROM users WHERE user_name = ''");
  if (!owner) {
    db.mutate(
      'INSERT INTO users (id, user_name, display_name, enabled, owner, role) VALUES (?, ?, ?, 1, 1, ?)',
      [FALLBACK_OWNER_ID, '', '', 'ADMIN'],
    );
  }
}

beforeEach(() => {
  const db = getAccountDb();
  db.mutate('DELETE FROM auth');
  db.mutate("DELETE FROM sessions WHERE auth_method = 'password'");
  ensureOwner();
});

afterEach(() => {
  const db = getAccountDb();
  db.mutate('DELETE FROM auth');
  db.mutate("DELETE FROM sessions WHERE auth_method = 'password'");
  db.mutate('DELETE FROM users WHERE id = ?', [FALLBACK_OWNER_ID]);
});

describe('isValidPassword', () => {
  it.each(
    [null, undefined, '', {}, { length: 1000000000 }, [], 123, true].map(
      value => [value],
    ),
  )(
    'rejects invalid input without passing it to a password hasher: %j',
    async password => {
      const hash = vi.spyOn(argon2, 'hash');
      const verify = vi.spyOn(argon2, 'verify');
      try {
        expect(isValidPassword(password)).toBe(false);
        expect(() => hashPassword(password)).toThrow('invalid-password');
        expect(await verifyPassword(password, '$argon2id$test')).toBe(false);
        expect(await bootstrap({ password })).toEqual({
          error: 'invalid-password',
        });
        expect(hash).not.toHaveBeenCalled();
        expect(verify).not.toHaveBeenCalled();
      } finally {
        hash.mockRestore();
        verify.mockRestore();
      }
    },
  );

  it('rejects invalid passwords without hashing on a configured server', async () => {
    await bootstrapPassword('existing password');
    const hash = vi.spyOn(argon2, 'hash');
    hash.mockClear();
    try {
      for (const password of [{}, { length: 1000000000 }, [], 123, null, '']) {
        expect(await bootstrap({ password })).toEqual({
          error: 'invalid-password',
        });
      }
      expect(hash).not.toHaveBeenCalled();
      expect(await checkPassword('existing password')).toBe(true);
    } finally {
      hash.mockRestore();
    }
  });

  it('rejects null, undefined and empty passwords', () => {
    expect(isValidPassword(null)).toBe(false);
    expect(isValidPassword(undefined)).toBe(false);
    expect(isValidPassword('')).toBe(false);
  });

  it('accepts a non-empty password', () => {
    expect(isValidPassword('hunter2')).toBe(true);
  });
});

describe('hashPassword / verifyPassword', () => {
  it('hashes to an argon2id string and verifies the correct password', async () => {
    const hash = await hashPassword('correct horse');

    expect(hash).toMatch(/^\$argon2id\$/);
    expect(await verifyPassword('correct horse', hash)).toBe(true);
  });

  it('rejects an incorrect password', async () => {
    const hash = await hashPassword('correct horse');

    expect(await verifyPassword('battery staple', hash)).toBe(false);
  });

  it('verifies a legacy bcrypt hash for backwards compatibility', async () => {
    const legacy = await bcrypt.hash('correct horse', 10);

    expect(await verifyPassword('correct horse', legacy)).toBe(true);
    expect(await verifyPassword('battery staple', legacy)).toBe(false);
  });

  it('returns false for non-string or malformed hashes instead of throwing', async () => {
    expect(await verifyPassword('x', null)).toBe(false);
    expect(await verifyPassword('x', undefined)).toBe(false);
    expect(await verifyPassword('x', 12345)).toBe(false);
    expect(await verifyPassword('x', 'not-a-real-hash')).toBe(false);
    expect(await verifyPassword('x', '$argon2id$garbage')).toBe(false);
  });
});

describe('loginWithPassword legacy hash upgrade', () => {
  it('logs in with a legacy bcrypt hash and upgrades it to argon2id', async () => {
    const legacy = await bcrypt.hash('correct horse', 10);
    setPasswordHash(legacy);

    const result = await loginWithPassword('correct horse');

    expect(result.error).toBeUndefined();
    expect(result.token).toBeTruthy();

    const stored = getStoredPasswordHash();
    expect(stored).not.toBe(legacy);
    expect(stored).toMatch(/^\$argon2id\$/);
    expect(await verifyPassword('correct horse', stored)).toBe(true);
  });

  it('does not upgrade or change the stored hash when the password is wrong', async () => {
    const legacy = await bcrypt.hash('correct horse', 10);
    setPasswordHash(legacy);

    const result = await loginWithPassword('battery staple');

    expect(result.error).toBe('invalid-password');
    expect(result.token).toBeUndefined();
    expect(getStoredPasswordHash()).toBe(legacy);
  });

  it('does not rehash an existing argon2id hash on login', async () => {
    const hash = await hashPassword('correct horse');
    setPasswordHash(hash);

    const result = await loginWithPassword('correct horse');

    expect(result.token).toBeTruthy();
    expect(getStoredPasswordHash()).toBe(hash);
  });
});

describe('bootstrapPassword / changePassword', () => {
  it('stores an argon2id hash that verifies the password', async () => {
    expect(await bootstrapPassword('correct horse')).toEqual({});

    const stored = getStoredPasswordHash();
    expect(stored).toMatch(/^\$argon2id\$/);
    expect(await verifyPassword('correct horse', stored)).toBe(true);
  });

  it('rejects an empty password on bootstrap', async () => {
    expect(await bootstrapPassword('')).toEqual({ error: 'invalid-password' });
  });

  it('replaces the stored hash on changePassword and verifies the new one', async () => {
    await bootstrapPassword('old password');
    const before = getStoredPasswordHash();

    expect(await changePassword('new password')).toEqual({});

    const after = getStoredPasswordHash();
    expect(after).not.toBe(before);
    expect(after).toMatch(/^\$argon2id\$/);
    expect(await verifyPassword('new password', after)).toBe(true);
    expect(await verifyPassword('old password', after)).toBe(false);
  });

  it('rejects an empty password on changePassword', async () => {
    expect(await changePassword('')).toEqual({ error: 'invalid-password' });
  });
});

describe('changePassword on an OIDC-only instance', () => {
  it('reports an error instead of silently succeeding', async () => {
    insertOpenIdAuth({ issuer: 'https://issuer.example.com' });

    expect(await changePassword('new password')).toEqual({
      error: 'no-password-method',
    });
    expect(getStoredPasswordHash()).toBeNull();
  });

  it('does not disable the existing openid method', async () => {
    insertOpenIdAuth({ issuer: 'https://issuer.example.com' });

    await changePassword('new password');

    expect(getAuthMethods()).toEqual([{ method: 'openid', active: 1 }]);
  });
});

describe('checkPassword', () => {
  it('returns true for the correct stored password and false otherwise', async () => {
    await bootstrapPassword('correct horse');

    expect(await checkPassword('correct horse')).toBe(true);
    expect(await checkPassword('battery staple')).toBe(false);
  });

  it('returns false when no password is configured', async () => {
    expect(await checkPassword('anything')).toBe(false);
  });
});

describe('password session revocation', () => {
  it('issues distinct sessions and revokes both when the password changes', async () => {
    await bootstrapPassword('old password');
    const first = await loginWithPassword('old password');
    const second = await loginWithPassword('old password');
    expect(first.token).not.toBe(second.token);
    expect(first.token).toBeTruthy();
    expect(second.token).toBeTruthy();

    await changePassword('new password');
    for (const token of [first.token, second.token]) {
      expect(
        getAccountDb().first('SELECT token FROM sessions WHERE token = ?', [
          token,
        ]),
      ).toBeNull();
    }
    expect(await loginWithPassword('old password')).toEqual({
      error: 'invalid-password',
    });
    const fresh = await loginWithPassword('new password');
    expect(fresh.token).toBeTruthy();
    expect([first.token, second.token]).not.toContain(fresh.token);
  });

  it('rolls back the password update if session revocation fails', async () => {
    await bootstrapPassword('old password');
    const { token } = await loginWithPassword('old password');
    const db = getAccountDb();
    db.exec(
      "CREATE TEMP TRIGGER fail_password_revocation BEFORE DELETE ON sessions WHEN OLD.auth_method = 'password' BEGIN SELECT RAISE(ABORT, 'revocation failed'); END",
    );
    try {
      await expect(changePassword('new password')).rejects.toThrow(
        'revocation failed',
      );
      expect(await checkPassword('old password')).toBe(true);
      expect(
        db.first('SELECT token FROM sessions WHERE token = ?', [token]),
      ).not.toBeNull();
    } finally {
      db.exec('DROP TRIGGER fail_password_revocation');
    }
  });

  it('does not issue a session if the password changes during verification', async () => {
    await bootstrapPassword('old password');
    let finishVerification;
    const verify = vi.spyOn(argon2, 'verify').mockImplementationOnce(
      () =>
        new Promise(resolve => {
          finishVerification = resolve;
        }),
    );
    try {
      const pendingLogin = loginWithPassword('old password');
      await changePassword('new password');
      finishVerification(true);
      expect(await pendingLogin).toEqual({ error: 'invalid-password' });
      expect(
        getAccountDb().all(
          "SELECT token FROM sessions WHERE auth_method = 'password'",
        ),
      ).toEqual([]);
    } finally {
      verify.mockRestore();
    }
  });
});