π¨ The ultimate agentic social media scheduling tool π€
TypeScript nextjsopen-sourceopen-source-social-media-scheduling-toolosspostiz-cloudpostiz-hostedredisscheduling-toolsocial-media-scheduling-tooltypescript
fix: access to local files
Nevo David12db9bd2f428
diff --git a/Dockerfile.dev b/Dockerfile.devindex 01ad14d63..476ad2161 100644--- a/Dockerfile.dev+++ b/Dockerfile.dev@@ -1,4 +1,4 @@-FROM node:22.20-bookworm-slim+FROM node:22.23.4-bookworm-slimARG NEXT_PUBLIC_VERSIONENV NEXT_PUBLIC_VERSION=$NEXT_PUBLIC_VERSIONRUN apt-get update && apt-get install -y --no-install-recommends \diff --git a/apps/backend/src/api/routes/media.controller.ts b/apps/backend/src/api/routes/media.controller.tsindex 3c6902e88..665f3c5d9 100644--- a/apps/backend/src/api/routes/media.controller.ts+++ b/apps/backend/src/api/routes/media.controller.ts@@ -111,6 +111,19 @@ export class MediaController {if (!name) {return false;}++ // the uploader only sends the bare object name the bucket upload returned,+ // anything else would make the stored path point outside the bucket+ if (+ !process.env.CLOUDFLARE_BUCKET_URL ||+ typeof name !== 'string' ||+ /[\\/]/.test(name) ||+ name === '.' ||+ name === '..'+ ) {+ throw new BadRequestException('Invalid file name');+ }+return this._mediaService.saveFile(org.id,name,diff --git a/libraries/nestjs-libraries/src/database/prisma/posts/posts.service.ts b/libraries/nestjs-libraries/src/database/prisma/posts/posts.service.tsindex bf866441b..6756ae653 100644--- a/libraries/nestjs-libraries/src/database/prisma/posts/posts.service.ts+++ b/libraries/nestjs-libraries/src/database/prisma/posts/posts.service.ts@@ -31,7 +31,7 @@ import {minifyPostsList,minifyPosts,} from '@gitroom/helpers/utils/posts.list.minify';-import axios from 'axios';+import { getSsrfSafeAxios } from '@gitroom/nestjs-libraries/dtos/webhooks/ssrf.safe.dispatcher';import sharp from 'sharp';import { UploadFactory } from '@gitroom/nestjs-libraries/upload/upload.factory';import { Readable } from 'stream';@@ -556,7 +556,9 @@ export class PostsService {if (hasExtension(m.path, 'png')) {imageUpdateNeeded = true;- const response = await axios.get(m.url, {+ // the media url is user-influenced, so it goes through the same+ // SSRF-safe client as the providers' media reads+ const response = await getSsrfSafeAxios().get(m.url, {responseType: 'arraybuffer',});@@ -797,15 +799,24 @@ export class PostsService {e.replace('(post:', '').replace(')', ''));const urls = await this._postRepository.getPostUrls(orgId, ids);- const newPlainText = ids.reduce((acc, value) => {- const findUrl = urls?.find?.((u) => u.id === value)?.releaseURL || '';- return acc.replace(- new RegExp(`\\(post:${value}\\)`, 'g'),- findUrl.split(',')[0]- );- }, plainText);-- return this.updateTags(orgId, JSON.parse(newPlainText) as Post[]);+ const replaceTags = (text: string, escape = (url: string) => url) =>+ ids.reduce((acc, value) => {+ const findUrl = urls?.find?.((u) => u.id === value)?.releaseURL || '';+ return acc.replace(new RegExp(`\\(post:${value}\\)`, 'g'), () =>+ escape(findUrl.split(',')[0])+ );+ }, text);++ // the release url is whatever the platform answered, so it only goes where+ // a tag is written, the content and the settings (escaped for their json),+ // and never into the media list, which was validated before this runs+ return post.map((p) => ({+ ...p,+ content: p.content && replaceTags(p.content),+ settings:+ p.settings &&+ replaceTags(p.settings, (url) => JSON.stringify(url).slice(1, -1)),+ }));}public async checkInternalPlug(diff --git a/libraries/nestjs-libraries/src/integrations/social.abstract.ts b/libraries/nestjs-libraries/src/integrations/social.abstract.tsindex 13a89ec37..093981452 100644--- a/libraries/nestjs-libraries/src/integrations/social.abstract.ts+++ b/libraries/nestjs-libraries/src/integrations/social.abstract.ts@@ -12,6 +12,7 @@ import {} from '@gitroom/nestjs-libraries/dtos/webhooks/ssrf.safe.dispatcher';import sharp from 'sharp';import { createReadStream, readFileSync, statSync } from 'fs';+import { resolve, sep } from 'path';import { Readable } from 'stream';import { DashboardFunction } from '@gitroom/nestjs-libraries/integrations/dashboard.function.decorator';@@ -276,7 +277,7 @@ export abstract class SocialAbstract {// classify a stall like the ranged reads below do.protected async readOrFetch(path: string, retried = false): Promise<Buffer> {if (path.indexOf('http') !== 0) {- return readFileSync(path);+ return readFileSync(this.localMediaPath(path));}try {@@ -341,7 +342,28 @@ export abstract class SocialAbstract {return length;}- return statSync(path).size;+ return statSync(this.localMediaPath(path, identifier)).size;+ }++ // Media that isn't a remote url is read from the worker's disk. The stored+ // path is user-influenced, so only a file inside UPLOAD_DIRECTORY is ever+ // opened: resolve() collapses any `..` segments before the comparison, the+ // same check the frontend /api/uploads route makes.+ protected localMediaPath(path: string, identifier = '') {+ const base = process.env.UPLOAD_DIRECTORY+ ? resolve(process.env.UPLOAD_DIRECTORY)+ : '';+ const filePath = resolve(path);+ if (!base || !filePath.startsWith(base + sep)) {+ throw new BadBody(+ identifier,+ JSON.stringify({ path }),+ Buffer.from('{}'),+ 'The media for this post is not in the upload directory, so it was not published'+ );+ }++ return filePath;}// Reads a single [start, end] byte range into memory. Used by providers whose@@ -409,9 +431,10 @@ export abstract class SocialAbstract {}}+ const filePath = this.localMediaPath(path, identifier);return new Promise((resolve, reject) => {const chunks: Buffer[] = [];- createReadStream(path, { start, end })+ createReadStream(filePath, { start, end }).on('data', (chunk) => chunks.push(chunk as Buffer)).on('end', () => resolve(Buffer.concat(chunks))).on('error', reject);@@ -426,7 +449,7 @@ export abstract class SocialAbstract {identifier = ''): Promise<Readable> {if (path.indexOf('http') !== 0) {- return createReadStream(path);+ return createReadStream(this.localMediaPath(path, identifier));}// identity encoding so the streamed byte count matches the size mediaSizediff --git a/libraries/nestjs-libraries/src/integrations/social/telegram.provider.ts b/libraries/nestjs-libraries/src/integrations/social/telegram.provider.tsindex fa5947cf0..90bcc01ec 100644--- a/libraries/nestjs-libraries/src/integrations/social/telegram.provider.ts+++ b/libraries/nestjs-libraries/src/integrations/social/telegram.provider.ts@@ -147,6 +147,11 @@ export class TelegramProvider extends SocialAbstract implements SocialProvider {if (mediaStorage === 'local' && mediaUrl.startsWith(frontendURL)) {mediaUrl = mediaUrl.replace(frontendURL, '');}+ // node-telegram-bot-api uploads any existing local path it is given, so+ // everything that isn't a url has to be a file of the upload directory+ if (mediaUrl.indexOf('http') !== 0) {+ mediaUrl = this.localMediaPath(mediaUrl, 'telegram-error');+ }//get mime type to pass contentType to telegram api.//some photos and videos might not pass telegram api restrictions, so they are sent as documents instead of returning errorsconst mimeType = mime.getType(mediaUrl); // Detect MIME typediff --git a/libraries/nestjs-libraries/src/integrations/social/tiktok.provider.ts b/libraries/nestjs-libraries/src/integrations/social/tiktok.provider.tsindex bae187233..960663b68 100644--- a/libraries/nestjs-libraries/src/integrations/social/tiktok.provider.ts+++ b/libraries/nestjs-libraries/src/integrations/social/tiktok.provider.ts@@ -760,7 +760,10 @@ export class TiktokProvider extends SocialAbstract implements SocialProvider {return response.body;}- return createReadStream(path, { start, end });+ return createReadStream(this.localMediaPath(path, 'tiktok-error-upload'), {+ start,+ end,+ });}// Streams the video bytes to the upload_url returned by the init call.diff --git a/libraries/nestjs-libraries/src/integrations/social/youtube.provider.ts b/libraries/nestjs-libraries/src/integrations/social/youtube.provider.tsindex 4a31d30c7..7699a0f41 100644--- a/libraries/nestjs-libraries/src/integrations/social/youtube.provider.ts+++ b/libraries/nestjs-libraries/src/integrations/social/youtube.provider.ts@@ -483,7 +483,7 @@ export class YoutubeProvider extends SocialAbstract implements SocialProvider {return Number(length);}- return statSync(path).size;+ return statSync(this.localMediaPath(path, this.identifier)).size;}// Returns a streaming body for the [start, end] byte range of the media so we@@ -519,7 +519,10 @@ export class YoutubeProvider extends SocialAbstract implements SocialProvider {return response.body;}- return createReadStream(path, { start, end });+ return createReadStream(this.localMediaPath(path, this.identifier), {+ start,+ end,+ });}// Asks the upload session for the truth: the created video when the uploaddiff --git a/libraries/nestjs-libraries/src/upload/local.storage.ts b/libraries/nestjs-libraries/src/upload/local.storage.tsindex 6a4452e1f..24a6e298b 100644--- a/libraries/nestjs-libraries/src/upload/local.storage.ts+++ b/libraries/nestjs-libraries/src/upload/local.storage.ts@@ -1,5 +1,6 @@import { IUploadProvider, UploadedStream } from './upload.interface';import { createWriteStream, mkdirSync, unlink, writeFileSync } from 'fs';+import { resolve, sep } from 'path';import { Readable } from 'stream';import { pipeline } from 'stream/promises';import { isSafePublicHttpsUrl } from '@gitroom/nestjs-libraries/dtos/webhooks/webhook.url.validator';@@ -137,12 +138,20 @@ export class LocalStorage implements IUploadProvider {}}- // Accepts either the public URL or the filesystem path+ // Accepts either the public URL or the filesystem path. Callers can pass a+ // stored media name or path, so only a file inside the upload directory is+ // ever deleted: resolve() collapses any `..` segments before the comparisonasync removeFile(filePath: string): Promise<void> {const publicPrefix = process.env.FRONTEND_URL + '/uploads';- const localPath = filePath.startsWith(publicPrefix)- ? this.uploadDirectory + filePath.slice(publicPrefix.length)- : filePath;+ const base = resolve(String(this.uploadDirectory));+ const localPath = resolve(+ filePath.startsWith(publicPrefix)+ ? this.uploadDirectory + filePath.slice(publicPrefix.length)+ : filePath+ );+ if (!localPath.startsWith(base + sep)) {+ throw new Error('Unsafe file path');+ }// Logic to remove the file from the filesystem goes herereturn new Promise((resolve, reject) => {unlink(localPath, (err) => {