From 71e3e879c0616882ee82a0e44f8c2e5ee9698a3e Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 2 Dec 2022 14:47:21 +0100 Subject: Support reinjecting token in private m3u8 playlist --- .../object-storage/video-static-file-privacy.ts | 36 ++++++++++++- .../tests/api/videos/video-static-file-privacy.ts | 61 +++++++++++++++++++++- server/tests/shared/checks.ts | 7 +++ server/tests/shared/index.ts | 2 +- server/tests/shared/playlists.ts | 22 -------- server/tests/shared/streaming-playlists.ts | 50 +++++++++++++++++- server/tests/shared/video-playlists.ts | 22 ++++++++ 7 files changed, 173 insertions(+), 27 deletions(-) delete mode 100644 server/tests/shared/playlists.ts create mode 100644 server/tests/shared/video-playlists.ts (limited to 'server/tests') diff --git a/server/tests/api/object-storage/video-static-file-privacy.ts b/server/tests/api/object-storage/video-static-file-privacy.ts index 62edd10ba..71ad35a43 100644 --- a/server/tests/api/object-storage/video-static-file-privacy.ts +++ b/server/tests/api/object-storage/video-static-file-privacy.ts @@ -2,7 +2,7 @@ import { expect } from 'chai' import { basename } from 'path' -import { expectStartWith } from '@server/tests/shared' +import { checkVideoFileTokenReinjection, expectStartWith } from '@server/tests/shared' import { areScalewayObjectStorageTestsDisabled, getAllFiles, getHLS } from '@shared/core-utils' import { HttpStatusCode, LiveVideo, VideoDetails, VideoPrivacy } from '@shared/models' import { @@ -191,6 +191,20 @@ describe('Object storage for video static file privacy', function () { } }) + it('Should reinject video file token', async function () { + this.timeout(120000) + + const videoFileToken = await server.videoToken.getVideoFileToken({ videoId: privateVideoUUID }) + + await checkVideoFileTokenReinjection({ + server, + videoUUID: privateVideoUUID, + videoFileToken, + resolutions: [ 240, 720 ], + isLive: false + }) + }) + it('Should update public video to private', async function () { this.timeout(60000) @@ -315,6 +329,26 @@ describe('Object storage for video static file privacy', function () { await checkLiveFiles(permanentLive, permanentLiveId) }) + it('Should reinject video file token in permanent live', async function () { + this.timeout(240000) + + const ffmpegCommand = sendRTMPStream({ rtmpBaseUrl: permanentLive.rtmpUrl, streamKey: permanentLive.streamKey }) + await server.live.waitUntilPublished({ videoId: permanentLiveId }) + + const video = await server.videos.getWithToken({ id: permanentLiveId }) + const videoFileToken = await server.videoToken.getVideoFileToken({ videoId: video.uuid }) + + await checkVideoFileTokenReinjection({ + server, + videoUUID: permanentLiveId, + videoFileToken, + resolutions: [ 720 ], + isLive: true + }) + + await stopFfmpeg(ffmpegCommand) + }) + it('Should have created a replay of the normal live with a private static path', async function () { this.timeout(240000) diff --git a/server/tests/api/videos/video-static-file-privacy.ts b/server/tests/api/videos/video-static-file-privacy.ts index eaaed5aad..ef0774b41 100644 --- a/server/tests/api/videos/video-static-file-privacy.ts +++ b/server/tests/api/videos/video-static-file-privacy.ts @@ -2,7 +2,7 @@ import { expect } from 'chai' import { decode } from 'magnet-uri' -import { expectStartWith } from '@server/tests/shared' +import { checkVideoFileTokenReinjection, expectStartWith } from '@server/tests/shared' import { getAllFiles, wait } from '@shared/core-utils' import { HttpStatusCode, LiveVideo, VideoDetails, VideoPrivacy } from '@shared/models' import { @@ -248,6 +248,35 @@ describe('Test video static file privacy', function () { await checkVideoFiles({ id: uuid, expectedStatus: HttpStatusCode.OK_200, token: server.accessToken, videoFileToken }) }) + it('Should reinject video file token', async function () { + this.timeout(120000) + + const { uuid } = await server.videos.quickUpload({ name: 'video', privacy: VideoPrivacy.PRIVATE }) + + const videoFileToken = await server.videoToken.getVideoFileToken({ videoId: uuid }) + await waitJobs([ server ]) + + const video = await server.videos.getWithToken({ id: uuid }) + const hls = video.streamingPlaylists[0] + + { + const query = { videoFileToken } + const { text } = await makeRawRequest({ url: hls.playlistUrl, query, expectedStatus: HttpStatusCode.OK_200 }) + + expect(text).to.not.include(videoFileToken) + } + + { + await checkVideoFileTokenReinjection({ + server, + videoUUID: uuid, + videoFileToken, + resolutions: [ 240, 720 ], + isLive: false + }) + } + }) + it('Should be able to access a private video of another user with an admin OAuth token or file token', async function () { this.timeout(120000) @@ -360,6 +389,36 @@ describe('Test video static file privacy', function () { await checkLiveFiles(permanentLive, permanentLiveId) }) + it('Should reinject video file token on permanent live', async function () { + this.timeout(240000) + + const ffmpegCommand = sendRTMPStream({ rtmpBaseUrl: permanentLive.rtmpUrl, streamKey: permanentLive.streamKey }) + await server.live.waitUntilPublished({ videoId: permanentLiveId }) + + const video = await server.videos.getWithToken({ id: permanentLiveId }) + const videoFileToken = await server.videoToken.getVideoFileToken({ videoId: video.uuid }) + const hls = video.streamingPlaylists[0] + + { + const query = { videoFileToken } + const { text } = await makeRawRequest({ url: hls.playlistUrl, query, expectedStatus: HttpStatusCode.OK_200 }) + + expect(text).to.not.include(videoFileToken) + } + + { + await checkVideoFileTokenReinjection({ + server, + videoUUID: permanentLiveId, + videoFileToken, + resolutions: [ 720 ], + isLive: true + }) + } + + await stopFfmpeg(ffmpegCommand) + }) + it('Should have created a replay of the normal live with a private static path', async function () { this.timeout(240000) diff --git a/server/tests/shared/checks.ts b/server/tests/shared/checks.ts index 55ebc6c3e..523d37420 100644 --- a/server/tests/shared/checks.ts +++ b/server/tests/shared/checks.ts @@ -23,6 +23,12 @@ function expectNotStartWith (str: string, start: string) { expect(str.startsWith(start), `${str} does not start with ${start}`).to.be.false } +function expectEndWith (str: string, end: string) { + expect(str.endsWith(end), `${str} does not end with ${end}`).to.be.true +} + +// --------------------------------------------------------------------------- + async function expectLogDoesNotContain (server: PeerTubeServer, str: string) { const content = await server.servers.getLogContent() @@ -103,6 +109,7 @@ export { testFileExistsOrNot, expectStartWith, expectNotStartWith, + expectEndWith, checkBadStartPagination, checkBadCountPagination, checkBadSortPagination, diff --git a/server/tests/shared/index.ts b/server/tests/shared/index.ts index 9f7ade53d..963ef8fe6 100644 --- a/server/tests/shared/index.ts +++ b/server/tests/shared/index.ts @@ -6,7 +6,7 @@ export * from './directories' export * from './generate' export * from './live' export * from './notifications' -export * from './playlists' +export * from './video-playlists' export * from './plugins' export * from './requests' export * from './streaming-playlists' diff --git a/server/tests/shared/playlists.ts b/server/tests/shared/playlists.ts deleted file mode 100644 index 8db303fd8..000000000 --- a/server/tests/shared/playlists.ts +++ /dev/null @@ -1,22 +0,0 @@ -import { expect } from 'chai' -import { readdir } from 'fs-extra' -import { PeerTubeServer } from '@shared/server-commands' - -async function checkPlaylistFilesWereRemoved ( - playlistUUID: string, - server: PeerTubeServer, - directories = [ 'thumbnails' ] -) { - for (const directory of directories) { - const directoryPath = server.getDirectoryPath(directory) - - const files = await readdir(directoryPath) - for (const file of files) { - expect(file).to.not.contain(playlistUUID) - } - } -} - -export { - checkPlaylistFilesWereRemoved -} diff --git a/server/tests/shared/streaming-playlists.ts b/server/tests/shared/streaming-playlists.ts index 824c3dcef..5c62af812 100644 --- a/server/tests/shared/streaming-playlists.ts +++ b/server/tests/shared/streaming-playlists.ts @@ -1,7 +1,7 @@ /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ import { expect } from 'chai' -import { basename } from 'path' +import { basename, dirname, join } from 'path' import { removeFragmentedMP4Ext, uuidRegex } from '@shared/core-utils' import { sha256 } from '@shared/extra-utils' import { HttpStatusCode, VideoStreamingPlaylist, VideoStreamingPlaylistType } from '@shared/models' @@ -188,9 +188,55 @@ async function completeCheckHlsPlaylist (options: { } } +async function checkVideoFileTokenReinjection (options: { + server: PeerTubeServer + videoUUID: string + videoFileToken: string + resolutions: number[] + isLive: boolean +}) { + const { server, resolutions, videoFileToken, videoUUID, isLive } = options + + const video = await server.videos.getWithToken({ id: videoUUID }) + const hls = video.streamingPlaylists[0] + + const query = { videoFileToken, reinjectVideoFileToken: 'true' } + const { text } = await makeRawRequest({ url: hls.playlistUrl, query, expectedStatus: HttpStatusCode.OK_200 }) + + for (let i = 0; i < resolutions.length; i++) { + const resolution = resolutions[i] + + const suffix = isLive + ? i + : `-${resolution}` + + expect(text).to.contain(`${suffix}.m3u8?videoFileToken=${videoFileToken}`) + } + + const resolutionPlaylists = extractResolutionPlaylistUrls(hls.playlistUrl, text) + expect(resolutionPlaylists).to.have.lengthOf(resolutions.length) + + for (const url of resolutionPlaylists) { + const { text } = await makeRawRequest({ url, query, expectedStatus: HttpStatusCode.OK_200 }) + + const extension = isLive + ? '.ts' + : '.mp4' + + expect(text).to.contain(`${extension}?videoFileToken=${videoFileToken}`) + } +} + +function extractResolutionPlaylistUrls (masterPath: string, masterContent: string) { + return masterContent.match(/^([^.]+\.m3u8.*)/mg) + .map(filename => join(dirname(masterPath), filename)) +} + export { checkSegmentHash, checkLiveSegmentHash, checkResolutionsInMasterPlaylist, - completeCheckHlsPlaylist + completeCheckHlsPlaylist, + extractResolutionPlaylistUrls, + checkVideoFileTokenReinjection } diff --git a/server/tests/shared/video-playlists.ts b/server/tests/shared/video-playlists.ts new file mode 100644 index 000000000..8db303fd8 --- /dev/null +++ b/server/tests/shared/video-playlists.ts @@ -0,0 +1,22 @@ +import { expect } from 'chai' +import { readdir } from 'fs-extra' +import { PeerTubeServer } from '@shared/server-commands' + +async function checkPlaylistFilesWereRemoved ( + playlistUUID: string, + server: PeerTubeServer, + directories = [ 'thumbnails' ] +) { + for (const directory of directories) { + const directoryPath = server.getDirectoryPath(directory) + + const files = await readdir(directoryPath) + for (const file of files) { + expect(file).to.not.contain(playlistUUID) + } + } +} + +export { + checkPlaylistFilesWereRemoved +} -- cgit v1.2.3