ActivityPub-federated video streaming platform using P2P directly in your web browser
Ensure filename is valid
Chocobozzz8a4bc0ddf25e
diff --git a/packages/tests/src/api/object-storage/video-static-file-privacy.ts b/packages/tests/src/api/object-storage/video-static-file-privacy.tsindex eda09c65ad..7f1eb256f6 100644--- a/packages/tests/src/api/object-storage/video-static-file-privacy.ts+++ b/packages/tests/src/api/object-storage/video-static-file-privacy.ts@@ -260,6 +260,16 @@ describe('Object storage for video static file privacy', function () {await makeRawRequest({ url: goodUrl, token: server.accessToken, expectedStatus: HttpStatusCode.OK_200 })})+ it('Should refuse access to public video files with invalid filename', async function () {+ const video = await server.videos.getWithToken({ id: privateVideoUUID })+ const filename = basename(getHLS(video).files[0].fileUrl)+ const uuid = privateVideoUUID++ const url = `${server.url}/object-storage-proxy/streaming-playlists/hls/private/${uuid}/..%2f${uuid}%2f${filename}`++ await makeRawRequest({ url: url, token: server.accessToken, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })+ })+it('Should correctly check user token, video file token of private video', async function () {this.timeout(60000)diff --git a/server/core/controllers/object-storage-proxy.ts b/server/core/controllers/object-storage-proxy.tsindex 7ae4537c4a..67312ac0a6 100644--- a/server/core/controllers/object-storage-proxy.ts+++ b/server/core/controllers/object-storage-proxy.ts@@ -5,7 +5,9 @@ import {ensureCanAccessPrivateVideoHLSFiles,ensureCanAccessVideoPrivateWebVideoFiles,ensurePrivateObjectStorageProxyIsEnabled,- optionalAuthenticate+ optionalAuthenticate,+ privateHLSFileValidator,+ privateWebVideoFileValidator} from '@server/middlewares/index.js'import cors from 'cors'import express from 'express'@@ -19,6 +21,7 @@ objectStorageProxyRouter.get([ OBJECT_STORAGE_PROXY_PATHS.PRIVATE_WEB_VIDEOS + ':filename', OBJECT_STORAGE_PROXY_PATHS.LEGACY_PRIVATE_WEB_VIDEOS + ':filename' ],ensurePrivateObjectStorageProxyIsEnabled,optionalAuthenticate,+ privateWebVideoFileValidator,asyncMiddleware(ensureCanAccessVideoPrivateWebVideoFiles),asyncMiddleware(proxifyWebVideoController))@@ -27,6 +30,7 @@ objectStorageProxyRouter.get(OBJECT_STORAGE_PROXY_PATHS.STREAMING_PLAYLISTS.PRIVATE_HLS + ':videoUUID/:filename',ensurePrivateObjectStorageProxyIsEnabled,optionalAuthenticate,+ privateHLSFileValidator,asyncMiddleware(ensureCanAccessPrivateVideoHLSFiles),asyncMiddleware(proxifyHLSController))diff --git a/server/core/lib/object-storage/keys.ts b/server/core/lib/object-storage/keys.tsindex 4faca32583..18151e3b9d 100644--- a/server/core/lib/object-storage/keys.ts+++ b/server/core/lib/object-storage/keys.ts@@ -3,6 +3,8 @@ import { MVideoUUID } from '@server/types/models/index.js'import { join } from 'path'export function generateHLSObjectStorageKey (video: MVideoUUID, filename: string) {+ if (!isSafeKey(filename)) throw new Error('Invalid filename ' + filename + ' for HLS object storage key generation')+return join(generateHLSObjectBaseStorageKey(video), filename)}@@ -11,17 +13,35 @@ export function generateHLSObjectBaseStorageKey (video: MVideoUUID) {}export function generateWebVideoObjectStorageKey (filename: string) {+ if (!isSafeKey(filename)) throw new Error('Invalid filename ' + filename + ' for web video object storage key generation')+return filename}export function generateOriginalVideoObjectStorageKey (filename: string) {+ if (!isSafeKey(filename)) throw new Error('Invalid filename ' + filename + ' for original video object storage key generation')+return filename}export function generateCaptionObjectStorageKey (filename: string) {+ if (!isSafeKey(filename)) throw new Error('Invalid filename ' + filename + ' for caption object storage key generation')+return filename}export function generateUserExportObjectStorageKey (filename: string) {+ if (!isSafeKey(filename)) throw new Error('Invalid filename ' + filename + ' for user export object storage key generation')+return filename}++// ---------------------------------------------------------------------------+// Private+// ---------------------------------------------------------------------------++function isSafeKey (key: string) {+ const regex = new RegExp(`^[a-zA-Z0-9-:.]+\\.[a-z0-9]{1,8}$`)++ return typeof key === 'string' && !!key.match(regex)+}diff --git a/server/core/middlewares/validators/static.ts b/server/core/middlewares/validators/static.tsindex 252da7dad3..715f8e00ee 100644--- a/server/core/middlewares/validators/static.ts+++ b/server/core/middlewares/validators/static.ts@@ -100,6 +100,17 @@ export const privateHLSFileValidator = [}]+export const privateWebVideoFileValidator = [+ param('filename')+ .custom(v => isSafeFilename(v)),++ (req: express.Request, res: express.Response, next: express.NextFunction) => {+ if (areValidationErrors(req, res)) return++ return next()+ }+]+export const ensureCanAccessPrivateVideoHLSFiles = [query('videoFileToken').optional()