fix(demo): journey and collab uploads are refused for the demo account too

Six upload routes refused the shared demo account in demo mode; journey photos, videos, gallery media and covers, collab note files and chat images did not, so any visitor could put images in front of every other one for the hour. They answer the same 403 now, checked in the handler after the upload is read, and a text-only chat message still goes through. The Demo Mode wiki page lists them, and says document sync is not covered.
This commit is contained in:
Maurice
2026-10-25 00:46:23 +00:00
parent 42ed858853
commit 2372dde716
11 changed files with 395 additions and 108 deletions
@@ -1,6 +1,8 @@
import type { User } from '../../types';
import { RuntimeEnvService } from '../app-config/runtime-env.service';
import { CurrentUser } from '../auth-core/current-user.decorator';
import { JwtAuthGuard } from '../auth-core/jwt-auth.guard';
import { DEMO_WRITE_ERROR, isDemoWriteBlocked } from '../common/demo-write';
import { ResponseContract } from '../common/response-contract';
import { SpoolCleanupInterceptor } from '../common/spool-cleanup.interceptor';
import { BLOCKED_EXTENSIONS, MAX_FILE_SIZE } from '../files/files.constants';
@@ -130,6 +132,8 @@ export class CollabController {
constructor(
private readonly collab: CollabService,
private readonly storage: StorageService,
// Whether DEMO_MODE is on, read on every upload: the demo account's are refused.
private readonly env: RuntimeEnvService,
) {}
private async requireTrip(tripId: string, user: User) {
@@ -242,6 +246,12 @@ export class CollabController {
};
try {
const trip = await this.requireTrip(tripId, user);
// After the 404, as on the trip-file upload, so the demo account cannot
// learn which trips exist; refused like every other upload of it
// (common/demo-write.ts).
if (isDemoWriteBlocked(this.env, user.email)) {
throw new HttpException(DEMO_WRITE_ERROR, 403);
}
if (!(await this.collab.canUploadFiles(trip, user))) {
throw new HttpException({ error: 'No permission to upload files' }, 403);
}
@@ -491,6 +501,11 @@ export class CollabController {
cleanupSpool();
throw err;
}
// Only the images are refused to the demo account; its text still posts.
if (uploaded.length && isDemoWriteBlocked(this.env, user.email)) {
cleanupSpool();
throw new HttpException(DEMO_WRITE_ERROR, 403);
}
if (uploaded.length && !(await this.collab.canUploadFiles(trip, user))) {
cleanupSpool();
throw new HttpException({ error: 'No permission to upload files' }, 403);
+3
View File
@@ -7,6 +7,7 @@ import { CollabPolls } from '../../db/entities/CollabPolls.entity';
import { Trips } from '../../db/entities/Trips.entity';
import { PluginGuardsModule } from '../../nest-rpc/plugin-guards.module';
import { AddonsModule } from '../addons/addons.module';
import { AppConfigModule } from '../app-config/app-config.module';
import { RateLimitModule } from '../common/rate-limit.module';
import { McpSharedModule } from '../mcp-shared/mcp-shared.module';
import { NotificationsModule } from '../notifications/notifications.module';
@@ -57,6 +58,8 @@ import { MulterModule } from '@nestjs/platform-express';
PluginGuardsModule,
AddonsModule,
RateLimitModule,
// RuntimeEnvService: the controller refuses the demo account's uploads in demo mode.
AppConfigModule,
],
controllers: [CollabController],
providers: [CollabService, CollabMcp, CollabRpc],
+5 -3
View File
@@ -3,7 +3,7 @@ import { RuntimeEnvService } from '../app-config/runtime-env.service';
import { DEMO_ACCOUNT_SETTINGS, isDemoEmail } from './demo';
import { DomainError } from './domain-error';
/** The exact 403 body the six upload endpoints have always returned. */
/** The exact 403 body the upload endpoints answer the demo account with. */
export const DEMO_WRITE_ERROR = {
error: 'Uploads are disabled in demo mode. Self-host TREK for full functionality.',
} as const;
@@ -12,9 +12,11 @@ export const DEMO_WRITE_ERROR = {
* Whether this user's writes are blocked because the instance runs in demo mode.
*
* The condition was copy-pasted into six upload handlers, each with its own copy
* of the message. It lives here once now, and every one of them calls it.
* of the message. It lives here once now, and every one of them calls it, as do
* the journey photo, video and cover uploads and the collab note file and chat
* image uploads that came without it.
*
* Deliberately a function and not a guard, though all six sites are handlers and
* Deliberately a function and not a guard, though every site is a handler and
* a guard is the obvious shape. Guards run before the multipart parser, so
* throwing from one leaves the request body unread and Node resets the
* connection: the client sees ECONNRESET instead of the 403. PROFILE-015 catches
+1 -1
View File
@@ -16,7 +16,7 @@ import { Injectable } from '@nestjs/common';
* hazard, confirmed by `task-0-report.md` §6), so an injected `EntityManager`
* resolving the current request's fork is exactly the right shape.
*
* `isDemoWriteBlocked`/`DEMO_WRITE_ERROR` (the six-upload-endpoint sibling
* `isDemoWriteBlocked`/`DEMO_WRITE_ERROR` (the upload endpoints' sibling
* check) stay exactly where they are, in `common/demo-write.ts` — a
* different, pure-function check over an already-known email string that
* this task does not touch.
@@ -22,6 +22,8 @@ import path from 'node:path';
import fs from 'node:fs';
import crypto from 'node:crypto';
import type { User } from '../../types';
import { RuntimeEnvService } from '../app-config/runtime-env.service';
import { isDemoWriteBlocked, DEMO_WRITE_ERROR } from '../common/demo-write';
import { StorageService } from '../storage/storage.service';
import { journeyThumbName } from '../memories/thumbnail.service';
import { JourneyService } from './journey.service';
@@ -172,6 +174,8 @@ export class JourneyController {
private readonly photoCapture: JourneyPhotoCaptureService,
// Which entries the gallery's photos sit on, told to every tab once written.
private readonly photoLinks: JourneyPhotoLinksService,
// Whether DEMO_MODE is on, read on every upload (refuseDemoUpload).
private readonly env: RuntimeEnvService,
) {}
/**
@@ -208,6 +212,22 @@ export class JourneyController {
}
}
/**
* Refuse the shared demo account's upload in demo mode, with the 403 the
* other upload routes answer (common/demo-write.ts). Called in the handler,
* once multer has drained the body, so the 403 arrives instead of an
* ECONNRESET. It runs before the commit: the spooled parts are all there is
* to take back, and a committed object would stay, since a demo instance
* keeps what is deleted (StorageService.delete).
*/
private refuseDemoUpload(user: User, files: ReadonlyArray<Express.Multer.File | undefined>): void {
if (!isDemoWriteBlocked(this.env, user.email)) return;
for (const f of files) {
if (f?.path) { try { fs.unlinkSync(f.path); } catch { /* best-effort */ } }
}
throw new HttpException(DEMO_WRITE_ERROR, 403);
}
// ── Static prefix routes (before /:id) ──────────────────────────────────
@Get()
@ResponseContract(journeyListResponseSchema)
@@ -271,6 +291,7 @@ export class JourneyController {
@Post('entries/:entryId/photos')
@UseInterceptors(FilesInterceptor('photos'))
async uploadEntryPhotos(@CurrentUser() user: User, @Param('entryId') entryId: string, @UploadedFiles() files: Express.Multer.File[] | undefined, @Body() body: JourneyEntryPhotoUploadDto) {
this.refuseDemoUpload(user, files ?? []);
if (!files?.length) {
throw new HttpException({ error: 'No files uploaded' }, 400);
}
@@ -337,6 +358,7 @@ export class JourneyController {
) {
const video = files?.video?.[0];
const poster = files?.poster?.[0];
this.refuseDemoUpload(user, [video, poster]);
const cleanup = () => {
for (const f of [video, poster]) {
if (f?.path) { try { fs.unlinkSync(f.path); } catch { /* best-effort */ } }
@@ -478,6 +500,7 @@ export class JourneyController {
@Post(':id/gallery/photos')
@UseInterceptors(FilesInterceptor('photos'))
async uploadGalleryPhotos(@CurrentUser() user: User, @Param('id') id: string, @UploadedFiles() files: Express.Multer.File[] | undefined) {
this.refuseDemoUpload(user, files ?? []);
if (!files?.length) {
throw new HttpException({ error: 'No files uploaded' }, 400);
}
@@ -510,6 +533,7 @@ export class JourneyController {
) {
const video = files?.video?.[0];
const poster = files?.poster?.[0];
this.refuseDemoUpload(user, [video, poster]);
// multer already spooled both parts; clean them up on a pre-commit rejection
// so a bad POST doesn't orphan a 500 MB clip (#823).
const cleanup = () => {
@@ -580,6 +604,7 @@ export class JourneyController {
@HttpCode(200) // Express answers cover with res.json (200).
@UseInterceptors(FileInterceptor('cover'))
async cover(@CurrentUser() user: User, @Param('id') id: string, @UploadedFile() file: Express.Multer.File | undefined, @Headers('x-socket-id') socketId?: string) {
this.refuseDemoUpload(user, [file]);
if (!file) {
throw new HttpException({ error: 'No file uploaded' }, 400);
}
@@ -1,5 +1,6 @@
import { Users } from '../../db/entities/Users.entity';
import { AddonsModule } from '../addons/addons.module';
import { AppConfigModule } from '../app-config/app-config.module';
import { UserConnectionRepositoriesModule } from '../database/user-connection-repositories.module';
import { AllowedFileTypesModule } from '../files/allowed-file-types.module';
import { AllowedFileTypesService } from '../files/allowed-file-types.service';
@@ -42,6 +43,8 @@ import { MulterModule } from '@nestjs/platform-express';
}),
StorageModule,
AddonsModule,
// RuntimeEnvService: the controller refuses the demo account's uploads in demo mode.
AppConfigModule,
MemoriesModule,
JourneyDomainModule,
JourneyPhotoCaptureModule,