Files
TREK/server/src/nest/trip-read-model/trip-read-model.service.ts
T
Maurice 8c8cc2a727 Require share_manage for the two public credentials (#1883)
Both advisories are the same mistake: a route that hands out a bearer token
treated trip membership as authorisation. Membership lets someone read the trip
while they are signed in. It does not let them mint a URL that reads it without
an account, keeps working after they are removed, and can be rotated out from
under the owner's calendar subscriptions.

The calendar feed token now sits behind TripAccessGuard plus
@RequirePermission('share_manage'), the same shape the rest of the trip-scoped
controllers use, which also replaces a hand-written access check that GET never
called at all. GET is gated with the writes because it returns the credential
itself, not its status. The share link's GET does what its own POST and DELETE
already did, and what the invite link next door already does for exactly this
reason.

Two things had to come with it or the gate would have been decoration. The MCP
tool get_share_link read the same token on membership alone, so it would simply
have become the new way in. And feed_token rides along in every trip payload
because TRIP_SELECT reads t.*: any member could have read the value out of
GET /api/trips/:id, the summary tool or the plugin RPC. It is blanked once in
the select and stripped in the two places that select the row directly.

The three service writes take the acting user and scope their UPDATE to a trip
that user can reach. The guard decides who may manage the credential; this only
makes sure a caller arriving another way cannot write to an id it guessed.

Client side, "Subscribe to calendar" is now behind the same permission on
desktop and mobile. Without that a member would have kept an entry whose enable
button silently does nothing, because IcsSubscribeModal swallows every non-200.
The one-off ICS download stays where it is: that is a file they may already read.

Instances that want members to manage these links can lower share_manage to
trip_member in the admin panel, which the tests cover.
2026-08-11 11:15:32 +02:00

119 lines
4.7 KiB
TypeScript

import { Injectable } from '@nestjs/common';
import { DatabaseService } from '../database/database.service';
import { DaysService } from '../days/days.service';
import { AccommodationsService } from '../accommodations/accommodations.service';
import { BudgetService } from '../budget/budget.service';
import { PackingService } from '../packing/packing.service';
import { ReservationsService } from '../reservations/reservations.service';
import { CollabService } from '../collab/collab.service';
import { PlacesService } from '../places/places.service';
import { TodoService } from '../todo/todo.service';
import { FilesService } from '../files/files.service';
import { TripMembersService } from '../trip-members/trip-members.service';
import { withoutFeedToken } from '../trips/trips.service';
/**
* The two read aggregates over a trip: the MCP summary and the offline bundle.
*
* They were the reason TripsService injected nine other domain services while
* its write path needs a handful. Reading is the fan-out; the writing core is
* not. Split apart, the aggregate root talks to database, permissions, realtime
* and the four domains its own updates touch, and everything that only ever
* appeared inside a summary lives here.
*/
@Injectable()
export class TripReadModelService {
constructor(
private readonly dbs: DatabaseService,
private readonly members: TripMembersService,
private readonly days: DaysService,
private readonly accommodations: AccommodationsService,
private readonly budget: BudgetService,
private readonly packing: PackingService,
private readonly reservations: ReservationsService,
private readonly collab: CollabService,
private readonly places: PlacesService,
private readonly todo: TodoService,
private readonly files: FilesService,
) {}
private get db() {
return this.dbs.connection;
}
private getOwner(tripId: string | number): { user_id: number } | undefined {
return this.db.prepare('SELECT user_id FROM trips WHERE id = ?').get(tripId) as { user_id: number } | undefined;
}
// ── Trip summary (used by MCP get_trip_summary tool) ──────────────────────
getTripSummary(tripId: number, viewerUserId?: number) {
const trip = withoutFeedToken(
this.db.prepare('SELECT * FROM trips WHERE id = ?').get(tripId) as Record<string, unknown> | undefined,
);
if (!trip) return null;
const ownerRow = this.getOwner(tripId);
if (!ownerRow) return null;
const { owner, members } = this.members.listMembers(tripId, ownerRow.user_id);
const { days: rawDays } = this.days.list(tripId);
const days = rawDays.map(({ notes_items, ...day }) => ({ ...day, notes: notes_items }));
const accommodations = this.accommodations.list(tripId);
const budgetItems = this.budget.listBudgetItems(tripId);
const budget = {
items: budgetItems,
item_count: budgetItems.length,
total: budgetItems.reduce((sum, i) => sum + (i.total_price || 0), 0),
currency: trip.currency,
};
// Thread the viewer so another member's private/personal packing items (#858)
// stay hidden — without it listItems returns the UNFILTERED list.
const packingItems = this.packing.listItems(tripId, viewerUserId);
const packing = {
items: packingItems,
total: packingItems.length,
checked: (packingItems as { checked: number }[]).filter(i => i.checked).length,
};
const reservations = this.reservations.list(tripId);
const collab_notes = this.collab.listNotes(tripId);
return {
trip,
members: { owner, collaborators: members },
days,
accommodations,
budget,
packing,
reservations,
collab_notes,
};
}
// ── Bundle / notifications (route helpers) ────────────────────────────────
/** Aggregates every trip sub-collection for offline caching (legacy /:id/bundle). */
bundle(tripId: string, trip: { user_id: number }, viewerId: number) {
const { days } = this.days.list(tripId);
const { owner, members } = this.members.listMembers(tripId, trip.user_id);
return {
trip,
days,
places: this.places.list(String(tripId), {}),
// Scope to the requesting member so other members' private packing items
// (#858) never land in this viewer's offline cache.
packingItems: this.packing.listItems(tripId, viewerId),
todoItems: this.todo.listItems(tripId),
budgetItems: this.budget.listBudgetItems(tripId),
reservations: this.reservations.list(tripId),
files: this.files.listFiles(tripId, false),
accommodations: this.accommodations.list(tripId),
members: [owner, ...(members || [])].filter(Boolean),
};
}
}