Skip to content

armgit / TREK

Public Imported from GitHub
0

The most complete open-source travel planner: real-time collaboration, bookings, budgets, packing lists, journals, offline PWA, SSO and an AI-ready MCP server, with planet-wide place search and routing built in.

fix(places): only attach tags owned by someone on the trip

Mauricec9124c9b6371
diff --git a/server/src/nest/collections/collections.service.ts b/server/src/nest/collections/collections.service.ts
index c2fe4b438..967835628 100644
--- a/server/src/nest/collections/collections.service.ts
+++ b/server/src/nest/collections/collections.service.ts
@@ -441,10 +441,23 @@ export class CollectionsService {
// Saved places CRUD
// -------------------------------------------------------------------------
+ /**
+ * A tag has no list of its own, only a `user_id`, so "belongs here" resolves
+ * through the people on the list — the same shape places.service uses against
+ * a trip roster. Off-list tag ids drop silently rather than being stored and
+ * read straight back out: the tag read-back ships `tags.user_id`, so an
+ * unfiltered id answers who owns a tag the caller cannot otherwise see.
+ */
private attachTags(collectionPlaceId: number, tagIds: number[] | undefined): void {
if (!tagIds || tagIds.length === 0) return;
+ const unique = [...new Set(tagIds)];
+ const eligible = this.collectionMemberIds(this.collectionIdOfPlace(collectionPlaceId));
+ const owned = this.db.all<{ id: number; user_id: number }>(
+ `SELECT id, user_id FROM tags WHERE id IN (${unique.map(() => '?').join(',')})`,
+ ...unique,
+ );
const stmt = this.db.prepare('INSERT OR IGNORE INTO collection_place_tags (collection_place_id, tag_id) VALUES (?, ?)');
- for (const tid of tagIds) stmt.run(collectionPlaceId, tid);
+ for (const t of owned) if (eligible.has(t.user_id)) stmt.run(collectionPlaceId, t.id);
}
/** Owner + accepted members — the users whose votes may live in this list. */
diff --git a/server/src/nest/places/places.service.ts b/server/src/nest/places/places.service.ts
index ceef2576d..552422c2e 100644
--- a/server/src/nest/places/places.service.ts
+++ b/server/src/nest/places/places.service.ts
@@ -122,6 +122,29 @@ export class PlacesService {
return this.permissions.checkPermission('place_edit', user.role, trip.user_id, user.id, trip.user_id !== user.id);
}
+ /**
+ * The subset of `tagIds` that someone on this trip owns. A tag has no trip of
+ * its own, only a `user_id`, so "belongs to this trip" resolves through the
+ * roster — which keeps the shared case working: a member tags a place, another
+ * member re-saves it and sends the id back, and the tag survives because its
+ * owner is still on the trip. Scoping to the caller instead would quietly strip
+ * a co-traveller's tag on every foreign edit.
+ *
+ * Off-roster ids drop silently. The place body is an open record, so an id can
+ * arrive from an older client that had no business knowing it existed, and the
+ * read-back join hands `tags.user_id` straight to the caller.
+ */
+ private tagsOnTrip(tripId: string | number, tagIds: number[]): number[] {
+ const unique = [...new Set(tagIds)];
+ if (unique.length === 0) return [];
+ const roster = this.dbs.rosterUserIds(tripId);
+ const owned = this.dbs.all<{ id: number; user_id: number }>(
+ `SELECT id, user_id FROM tags WHERE id IN (${unique.map(() => '?').join(',')})`,
+ ...unique,
+ );
+ return owned.filter(t => roster.has(t.user_id)).map(t => t.id);
+ }
+
broadcast<E extends TrekWsTripEventName>(tripId: string, event: E, payload: TrekWsPayload<E>, socketId: string | undefined): void {
this.realtime.broadcast(tripId, event, payload, socketId);
}
@@ -223,7 +246,7 @@ export class PlacesService {
if (tags && tags.length > 0) {
const insertTag = this.dbs.prepare('INSERT OR IGNORE INTO place_tags (place_id, tag_id) VALUES (?, ?)');
- for (const tagId of tags) {
+ for (const tagId of this.tagsOnTrip(tripId, tags)) {
insertTag.run(placeId, tagId);
}
}
@@ -324,7 +347,7 @@ export class PlacesService {
this.dbs.run('DELETE FROM place_tags WHERE place_id = ?', placeId);
if (tags.length > 0) {
const insertTag = this.dbs.prepare('INSERT OR IGNORE INTO place_tags (place_id, tag_id) VALUES (?, ?)');
- for (const tagId of tags) {
+ for (const tagId of this.tagsOnTrip(tripId, tags)) {
insertTag.run(placeId, tagId);
}
}
diff --git a/server/tests/unit/nest/places.service.test.ts b/server/tests/unit/nest/places.service.test.ts
index e98decef5..615135716 100644
--- a/server/tests/unit/nest/places.service.test.ts
+++ b/server/tests/unit/nest/places.service.test.ts
@@ -64,7 +64,7 @@ const photoCacheStub = { removeIfUnreferenced: removeIfUnreferencedSpy } as unkn
import { createTables } from '../../../src/db/schema';
import { runMigrations } from '../../../src/db/migrations';
import { resetTestDb } from '../../helpers/test-db';
-import { createUser, createTrip, createPlace, createCategory, createTag } from '../../helpers/factories';
+import { createUser, createTrip, createPlace, createCategory, createTag, addTripMember } from '../../helpers/factories';
import path from 'path';
import fs from 'fs';
import { DatabaseService } from '../../../src/nest/database/database.service';
@@ -201,6 +201,33 @@ describe('create', () => {
expect(place.tags[0].id).toBe(tag.id);
});
+ // A tag belongs to a user, not a trip, and the place body is an open record, so
+ // an id from someone outside the trip could be attached and then read straight
+ // back — the tag projection carries the owner's user_id with it.
+ it('PLACE-SVC-008a — drops a tag owned by someone outside the trip', () => {
+ const { user } = createUser(testDb);
+ const { user: outsider } = createUser(testDb);
+ const trip = createTrip(testDb, user.id);
+ const mine = createTag(testDb, user.id, { name: 'Mine' }) as any;
+ const theirs = createTag(testDb, outsider.id, { name: 'Theirs' }) as any;
+
+ const place = svc.create(String(trip.id), { name: 'Tagged Place', tags: [mine.id, theirs.id] }) as any;
+
+ expect(place.tags.map((t: any) => t.id)).toEqual([mine.id]);
+ });
+
+ it('PLACE-SVC-008b — keeps a co-traveller tag, so a shared place survives a foreign re-save', () => {
+ const { user: owner } = createUser(testDb);
+ const { user: member } = createUser(testDb);
+ const trip = createTrip(testDb, owner.id);
+ addTripMember(testDb, trip.id, member.id);
+ const theirTag = createTag(testDb, member.id, { name: 'Theirs' }) as any;
+
+ const place = svc.create(String(trip.id), { name: 'Shared Place', tags: [theirTag.id] }) as any;
+
+ expect(place.tags.map((t: any) => t.id)).toEqual([theirTag.id]);
+ });
+
it('PLACE-SVC-009 — place is associated with correct trip', () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);