ActivityPub-federated video streaming platform using P2P directly in your web browser
Prevent serving invalid JSON file
Chocobozzz9e1687ee2fe6
diff --git a/packages/tests/src/api/live/live-fast-restream.ts b/packages/tests/src/api/live/live-fast-restream.tsindex ac02aac58b..bc45c585ea 100644--- a/packages/tests/src/api/live/live-fast-restream.ts+++ b/packages/tests/src/api/live/live-fast-restream.ts@@ -77,15 +77,9 @@ describe('Fast restream in live', function () {const video = await server.videos.get({ id: liveId })expect(video.streamingPlaylists).to.have.lengthOf(1)- try {- await server.live.getSegmentFile({ videoUUID: liveId, segment: 0, playlistNumber: 0 })- await server.streamingPlaylists.get({ url: video.streamingPlaylists[0].playlistUrl })- await server.streamingPlaylists.getSegmentSha256({ url: video.streamingPlaylists[0].segmentsSha256Url })- } catch (err) {- // FIXME: try to debug error in CI "Unexpected end of JSON input"- console.error(err)- throw err- }+ await server.live.getSegmentFile({ videoUUID: liveId, segment: 0, playlistNumber: 0 })+ await server.streamingPlaylists.get({ url: video.streamingPlaylists[0].playlistUrl })+ await server.streamingPlaylists.getSegmentSha256({ url: video.streamingPlaylists[0].segmentsSha256Url })await wait(100)}diff --git a/server/core/controllers/object-storage-proxy.ts b/server/core/controllers/object-storage-proxy.tsindex 67312ac0a6..a78ffccaab 100644--- a/server/core/controllers/object-storage-proxy.ts+++ b/server/core/controllers/object-storage-proxy.ts@@ -6,7 +6,7 @@ import {ensureCanAccessVideoPrivateWebVideoFiles,ensurePrivateObjectStorageProxyIsEnabled,optionalAuthenticate,- privateHLSFileValidator,+ hlsFileValidator,privateWebVideoFileValidator} from '@server/middlewares/index.js'import cors from 'cors'@@ -30,7 +30,7 @@ objectStorageProxyRouter.get(OBJECT_STORAGE_PROXY_PATHS.STREAMING_PLAYLISTS.PRIVATE_HLS + ':videoUUID/:filename',ensurePrivateObjectStorageProxyIsEnabled,optionalAuthenticate,- privateHLSFileValidator,+ hlsFileValidator,asyncMiddleware(ensureCanAccessPrivateVideoHLSFiles),asyncMiddleware(proxifyHLSController))diff --git a/server/core/controllers/static.ts b/server/core/controllers/static.tsindex 4c9b9f6bd2..0e9970d11b 100644--- a/server/core/controllers/static.ts+++ b/server/core/controllers/static.ts@@ -1,16 +1,16 @@-import { HttpStatusCode } from '@peertube/peertube-models'import { injectQueryToPlaylistUrls } from '@server/lib/hls.js'import {asyncMiddleware,ensureCanAccessPrivateVideoHLSFiles,ensureCanAccessVideoPrivateWebVideoFiles,handleStaticError,+ hlsFileValidator,optionalAuthenticate,- privateHLSFileValidator,privateM3U8PlaylistValidator} from '@server/middlewares/index.js'import cors from 'cors'import express from 'express'+import { readJSON } from 'fs-extra/esm'import { readFile } from 'fs/promises'import { join } from 'path'import { CONFIG } from '../initializers/config.js'@@ -60,17 +60,37 @@ staticRouter.use(STATIC_PATHS.STREAMING_PLAYLISTS.PRIVATE_HLS + ':videoUUID/:playlistNameWithoutExtension([a-z0-9-]+).m3u8',privateM3U8PlaylistValidator,...privateHLSStaticMiddlewares,- asyncMiddleware(servePrivateM3U8)+ asyncMiddleware(servePrivateM3U8),+ handleStaticError+)++// segments-sha256.json is frequently rewritten (on every new live segment), so we can't rely on express.static/sendFile+// (stat + range read) that could read a truncated file if it's overwritten between the stat and the read+staticRouter.use(+ STATIC_PATHS.STREAMING_PLAYLISTS.PRIVATE_HLS + ':videoUUID/:filename([a-z0-9-]*segments-sha256\\.json)',+ hlsFileValidator,+ ...privateHLSStaticMiddlewares,+ asyncMiddleware(serveSha256Segments(DIRECTORIES.HLS_STREAMING_PLAYLIST.PRIVATE)),+ handleStaticError)staticRouter.use(STATIC_PATHS.STREAMING_PLAYLISTS.PRIVATE_HLS + ':videoUUID/:filename',- privateHLSFileValidator,+ hlsFileValidator,...privateHLSStaticMiddlewares,- servePrivateHLSFile+ servePrivateHLSFile,+ handleStaticError)// ---------------------------------------------------------------------------+// Same as above: avoid express.static for this frequently rewritten file to prevent serving truncated content+staticRouter.use(+ STATIC_PATHS.STREAMING_PLAYLISTS.HLS + '/:videoUUID/:filename([a-z0-9-]*segments-sha256\\.json)',+ hlsFileValidator,+ asyncMiddleware(serveSha256Segments(DIRECTORIES.HLS_STREAMING_PLAYLIST.PUBLIC)),+ handleStaticError+)+staticRouter.use(STATIC_PATHS.STREAMING_PLAYLISTS.HLS,express.static(DIRECTORIES.HLS_STREAMING_PLAYLIST.PUBLIC, { fallthrough: false }),@@ -99,24 +119,19 @@ function servePrivateHLSFile (req: express.Request, res: express.Response) {return res.sendFile(path)}+function serveSha256Segments (baseDirectory: string) {+ return async (req: express.Request, res: express.Response) => {+ const path = join(baseDirectory, req.params.videoUUID, req.params.filename)++ return res.json(await readJSON(path))+ }+}+async function servePrivateM3U8 (req: express.Request, res: express.Response) {const path = join(DIRECTORIES.HLS_STREAMING_PLAYLIST.PRIVATE, req.params.videoUUID, req.params.playlistNameWithoutExtension + '.m3u8')const filename = req.params.playlistNameWithoutExtension + '.m3u8'- let playlistContent: string-- try {- playlistContent = await readFile(path, 'utf-8')- } catch (err) {- if (err.message.includes('ENOENT')) {- return res.fail({- status: HttpStatusCode.NOT_FOUND_404,- message: 'File not found'- })- }-- throw err- }+ const playlistContent = await readFile(path, 'utf-8')// Inject token in playlist so players that cannot alter the HTTP request can still watch the videoconst transformedContent = doReinjectVideoFileToken(req)diff --git a/server/core/middlewares/error.ts b/server/core/middlewares/error.tsindex 6b84370b5b..acf270809e 100644--- a/server/core/middlewares/error.ts+++ b/server/core/middlewares/error.ts@@ -41,14 +41,8 @@ function apiFailMiddleware (req: express.Request, res: express.Response, next: e}function handleStaticError (err: any, req: express.Request, res: express.Response, next: express.NextFunction) {- const message = err.message || ''-- if (message.includes('ENOENT')) {- return res.fail({- status: err.status || HttpStatusCode.INTERNAL_SERVER_ERROR_500,- message: err.message,- type: err.name- })+ if (err instanceof Error && (err as NodeJS.ErrnoException).code === 'ENOENT') {+ return res.sendStatus(HttpStatusCode.NOT_FOUND_404)}return next(err)diff --git a/server/core/middlewares/validators/static.ts b/server/core/middlewares/validators/static.tsindex 715f8e00ee..5dfb6b5b56 100644--- a/server/core/middlewares/validators/static.ts+++ b/server/core/middlewares/validators/static.ts@@ -86,7 +86,7 @@ export const privateM3U8PlaylistValidator = [}]-export const privateHLSFileValidator = [+export const hlsFileValidator = [param('videoUUID').custom(isUUIDValid),