Files
TREK/server/tests/unit/db/journey-share-protection-migration.test.ts
T
Maurice 55fc5f6679 feat(journey): protect a public journey link with a password and an expiry
A journey's share link can now ask readers for a password, stop working on
a date its owner picks, and count how often it was opened (#2553, #1702,
no password, no expiry and no views.

- journey_share_tokens gains password_hash, password_version, expires_at,
  view_count and last_viewed_at (append-only migration, SQLite and
  Postgres, added only where missing).
- The password is hashed with bcrypt at the account cost. POST
  /api/public/journey/:token/unlock checks it, rate limited per address
  and link (only refusals count), and sets an httpOnly cookie scoped to
  the link's API path holding an HMAC over the token and the password
  version. Secure follows the request, so plain-HTTP installs keep working.
  Changing or removing the password raises the version and locks every
  old cookie out.
- Every public route, the photo proxies included, first checks the link:
  404 unknown, 410 expired (JOURNEY_LINK_EXPIRED), 401 locked
  (JOURNEY_PASSWORD_REQUIRED).
- Saving the share switches keeps the password and the expiry; the owner's
  read returns has_password, expires_at, view_count and last_viewed_at,
  never the hash, and none of it reaches the public payload.
- A reader is counted once a day per address and browser, in an in-memory
  store behind an injectable port; a restart may count them again.
- create_journey_share_link takes password and expires_at like the REST
  route; get_journey_share_link reports has_password.
2026-11-30 16:56:14 +01:00

92 lines
3.9 KiB
TypeScript

/**
* `Migration20200101046620_a_journey_link_can_ask_for_a_password_and_run_out`:
* the password, expiry and view-count columns on `journey_share_tokens`.
* Migrate to the step before it with a published journey already there, apply
* just this one, and check that the existing link comes through unprotected,
* never expiring and uncounted, with every flag it had. On Postgres the same
* step runs in the Postgres lane, where PARITY-PG holds the migrated columns to
* the entity.
*/
import { createMigrationOrm, migrateTo, pendingNames, rawExec, rawQuery } from '../../helpers/migration-step';
import { describe, expect, it } from 'vitest';
const TARGET = 'Migration20200101046620_a_journey_link_can_ask_for_a_password_and_run_out';
describe('journey_share_tokens protection migration', () => {
it('JSHAREPROT-MIG-001: adds the columns and leaves an existing link open, unexpiring and uncounted', async () => {
const orm = await createMigrationOrm();
try {
const names = await pendingNames(orm);
const idx = names.indexOf(TARGET);
expect(idx).toBeGreaterThan(0);
const previous = names[idx - 1];
if (!previous) throw new Error('no step before the target');
await migrateTo(orm, previous);
await rawExec(orm, "INSERT INTO users (id, username, email, password_hash) VALUES (1, 'owner', 'o@test', 'x')");
await rawExec(orm, "INSERT INTO journeys (id, user_id, title, created_at, updated_at) VALUES (5, 1, 'Iceland', 0, 0)");
await rawExec(
orm,
"INSERT INTO journey_share_tokens (id, journey_id, token, created_by, share_timeline, share_gallery, share_map, share_plan) VALUES (9, 5, 'tok', 1, 1, 0, 1, 1)",
);
await migrateTo(orm, TARGET);
const columns = await rawQuery<{ name: string; type: string; notnull: number; dflt_value: string | null }>(
orm,
"SELECT name, type, \"notnull\", dflt_value FROM pragma_table_info('journey_share_tokens') WHERE name IN ('password_hash', 'password_version', 'expires_at', 'view_count', 'last_viewed_at') ORDER BY cid",
);
expect(columns).toEqual([
{ name: 'password_hash', type: 'TEXT', notnull: 0, dflt_value: null },
{ name: 'password_version', type: 'INTEGER', notnull: 1, dflt_value: '0' },
{ name: 'expires_at', type: 'DATETIME', notnull: 0, dflt_value: null },
{ name: 'view_count', type: 'INTEGER', notnull: 1, dflt_value: '0' },
{ name: 'last_viewed_at', type: 'DATETIME', notnull: 0, dflt_value: null },
]);
expect(
await rawQuery(
orm,
'SELECT token, share_timeline, share_gallery, share_map, share_plan, password_hash, password_version, expires_at, view_count, last_viewed_at FROM journey_share_tokens WHERE id = 9',
),
).toEqual([
{
token: 'tok',
share_timeline: 1,
share_gallery: 0,
share_map: 1,
share_plan: 1,
password_hash: null,
password_version: 0,
expires_at: null,
view_count: 0,
last_viewed_at: null,
},
]);
} finally {
await orm.close(true);
}
});
it('JSHAREPROT-MIG-002: running it on a database that already has the columns changes nothing', async () => {
const orm = await createMigrationOrm();
try {
const names = await pendingNames(orm);
const previous = names[names.indexOf(TARGET) - 1];
if (!previous) throw new Error('no step before the target');
await migrateTo(orm, previous);
// A development build that added one of them by hand already.
await rawExec(orm, 'ALTER TABLE journey_share_tokens ADD COLUMN view_count INTEGER NOT NULL DEFAULT 0');
await migrateTo(orm, TARGET);
const columns = await rawQuery<{ name: string }>(
orm,
"SELECT name FROM pragma_table_info('journey_share_tokens') WHERE name = 'view_count'",
);
expect(columns).toHaveLength(1);
} finally {
await orm.close(true);
}
});
});