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.
This commit is contained in:
Maurice
2026-11-30 14:36:42 +01:00
parent ddfb7ad7ad
commit 55fc5f6679
29 changed files with 1540 additions and 197 deletions
@@ -90,3 +90,27 @@ export const journeyPublicResponseSchema = z.strictObject({
}),
});
export type JourneyPublicResponse = z.infer<typeof journeyPublicResponseSchema>;
/**
* POST /api/public/journey/:token/unlock: the password a reader typed for a
* protected link. Bounded but not held to the owner's rules, so a reader only
* ever learns "wrong password", never a hint about what the password looks like.
*/
export const journeyPublicUnlockRequestSchema = z.object({
password: z.string().min(1).max(200),
});
export type JourneyPublicUnlockRequest = z.infer<typeof journeyPublicUnlockRequestSchema>;
/**
* The `code` beside `error` when a public journey route refuses, so the reader's
* page can tell "ask for the password" from "this link is gone" without reading
* the message.
*/
export const JOURNEY_PUBLIC_ERROR_CODES = {
/** 401: the link has a password and this browser has not unlocked it yet. */
locked: 'JOURNEY_PASSWORD_REQUIRED',
/** 401 from the unlock route: the password did not match. */
denied: 'JOURNEY_PASSWORD_INVALID',
/** 410: the link ran past the date its owner set. */
expired: 'JOURNEY_LINK_EXPIRED',
} as const;
+27
View File
@@ -20,6 +20,7 @@ import {
journeyUpdateToolSchema,
journeyTrackSchema,
} from './journey.schema';
import { journeyPublicUnlockRequestSchema } from './journey-public.schema';
import { describe, it, expect } from 'vitest';
@@ -145,6 +146,32 @@ describe('journeyShareLinkRequestSchema', () => {
expect(journeyShareLinkRequestSchema.safeParse({ share_plan: 'true' }).success).toBe(false);
expect(journeyShareLinkRequestSchema.safeParse({ share_recordings: 1 }).success).toBe(false);
});
it('sets, clears or keeps a password, within what bcrypt reads whole (#2553)', () => {
expect(journeyShareLinkRequestSchema.safeParse({ password: 'secret' }).success).toBe(true);
expect(journeyShareLinkRequestSchema.safeParse({ password: null }).success).toBe(true);
expect(journeyShareLinkRequestSchema.safeParse({ share_map: true }).data).not.toHaveProperty('password');
expect(journeyShareLinkRequestSchema.safeParse({ password: 'abc' }).success).toBe(false);
expect(journeyShareLinkRequestSchema.safeParse({ password: 'x'.repeat(73) }).success).toBe(false);
expect(journeyShareLinkRequestSchema.safeParse({ password: 1234 }).success).toBe(false);
});
it('takes an expiry as an ISO timestamp or null, and nothing else', () => {
expect(journeyShareLinkRequestSchema.safeParse({ expires_at: '2027-01-01T00:00:00Z' }).success).toBe(true);
expect(journeyShareLinkRequestSchema.safeParse({ expires_at: '2027-01-01T00:00:00+02:00' }).success).toBe(true);
expect(journeyShareLinkRequestSchema.safeParse({ expires_at: null }).success).toBe(true);
expect(journeyShareLinkRequestSchema.safeParse({ expires_at: '2027-01-01' }).success).toBe(false);
expect(journeyShareLinkRequestSchema.safeParse({ expires_at: 'tomorrow' }).success).toBe(false);
});
});
describe('journeyPublicUnlockRequestSchema', () => {
it('needs a password and caps its length', () => {
expect(journeyPublicUnlockRequestSchema.safeParse({ password: 'a' }).success).toBe(true);
expect(journeyPublicUnlockRequestSchema.safeParse({ password: '' }).success).toBe(false);
expect(journeyPublicUnlockRequestSchema.safeParse({}).success).toBe(false);
expect(journeyPublicUnlockRequestSchema.safeParse({ password: 'x'.repeat(201) }).success).toBe(false);
});
});
describe('the free-form bodies keep their unknown keys', () => {
+25 -1
View File
@@ -1,4 +1,4 @@
import { idSchema } from '../common/primitives.schema';
import { idSchema, isoDateTime } from '../common/primitives.schema';
import { journeyRecordingDateSchema } from './journey-recordings.schema';
import { z } from 'zod';
@@ -414,6 +414,12 @@ export type JourneyGalleryVideoRequest = z.infer<typeof journeyGalleryVideoReque
export const journeyPreferencesRequestSchema = z.looseObject({});
export type JourneyPreferencesRequest = z.infer<typeof journeyPreferencesRequestSchema>;
/**
* A share link's password. bcrypt reads at most 72 bytes, so a longer one would
* be cut short without anyone noticing.
*/
export const journeySharePasswordSchema = z.string().min(4).max(72);
export const journeyShareLinkRequestSchema = z.looseObject({
share_timeline: z.unknown().optional(),
share_gallery: z.unknown().optional(),
@@ -424,6 +430,16 @@ export const journeyShareLinkRequestSchema = z.looseObject({
share_plan: z.boolean().optional(),
/** Put the recorded days on the shared map, on days with a published entry (JRN-04). */
share_recordings: z.boolean().optional(),
/**
* Ask a reader for this password before the link shows anything (#2553).
* A string sets or replaces it, null takes it off, leaving it out keeps it.
*/
password: journeySharePasswordSchema.nullable().optional(),
/**
* When the link stops working, as an ISO timestamp. Null means never (the
* default), leaving it out keeps what the link had.
*/
expires_at: isoDateTime.nullable().optional(),
});
export type JourneyShareLinkRequest = z.infer<typeof journeyShareLinkRequestSchema>;
@@ -510,6 +526,14 @@ export const journeyShareLinkSchema = z.object({
newest_first: z.boolean(),
share_plan: z.boolean(),
share_recordings: z.boolean(),
/** Whether a reader has to enter a password first. The password itself never leaves the server. */
has_password: z.boolean(),
/** When the link stops working (ISO, UTC), or null when it never does. */
expires_at: z.string().nullable(),
/** How many readers opened the link, each counted once a day. Only the owner sees it. */
view_count: z.number(),
/** When a reader last opened the link (ISO, UTC), or null before the first one. */
last_viewed_at: z.string().nullable(),
});
export type JourneyShareLink = z.infer<typeof journeyShareLinkSchema>;