Skip to content

Commit

Permalink
Fix file token reinjection on fragments
Browse files Browse the repository at this point in the history
  • Loading branch information
wickloww authored and Chocobozzz committed Mar 7, 2023
1 parent a3e5f7e commit 73fb3dc
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 8 deletions.
2 changes: 1 addition & 1 deletion server/controllers/object-storage-proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ async function proxifyHLS (req: express.Request, res: express.Response) {
setS3Headers(res, s3Response)

const streamReplacer = filename.endsWith('.m3u8') && doReinjectVideoFileToken(req)
? new StreamReplacer(line => injectQueryToPlaylistUrls(line, buildReinjectVideoFileTokenQuery(req)))
? new StreamReplacer(line => injectQueryToPlaylistUrls(line, buildReinjectVideoFileTokenQuery(req, filename.endsWith('master.m3u8'))))
: new PassThrough()

return pipeline(
Expand Down
8 changes: 6 additions & 2 deletions server/controllers/shared/m3u8-playlist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ function doReinjectVideoFileToken (req: express.Request) {
return req.query.videoFileToken && req.query.reinjectVideoFileToken
}

function buildReinjectVideoFileTokenQuery (req: express.Request) {
return 'videoFileToken=' + req.query.videoFileToken
function buildReinjectVideoFileTokenQuery (req: express.Request, isMaster: boolean) {
const query = 'videoFileToken=' + req.query.videoFileToken
if (isMaster) {
return query + '&reinjectVideoFileToken=true'
}
return query
}

export {
Expand Down
3 changes: 2 additions & 1 deletion server/controllers/static.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ export {

async function servePrivateM3U8 (req: express.Request, res: express.Response) {
const path = join(DIRECTORIES.HLS_STREAMING_PLAYLIST.PRIVATE, req.params.videoUUID, req.params.playlistName + '.m3u8')
const filename = req.params.playlistName + '.m3u8'

let playlistContent: string

Expand All @@ -108,7 +109,7 @@ async function servePrivateM3U8 (req: express.Request, res: express.Response) {

// Inject token in playlist so players that cannot alter the HTTP request can still watch the video
const transformedContent = doReinjectVideoFileToken(req)
? injectQueryToPlaylistUrls(playlistContent, buildReinjectVideoFileTokenQuery(req))
? injectQueryToPlaylistUrls(playlistContent, buildReinjectVideoFileTokenQuery(req, filename.endsWith('master.m3u8')))
: playlistContent

return res.set('content-type', 'application/vnd.apple.mpegurl').send(transformedContent).end()
Expand Down
5 changes: 2 additions & 3 deletions server/tests/api/videos/video-static-file-privacy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,10 +256,9 @@ describe('Test video static file privacy', function () {
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 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 })

Expand Down
3 changes: 2 additions & 1 deletion server/tests/shared/streaming-playlists.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ async function checkVideoFileTokenReinjection (options: {
? i
: `-${resolution}`

expect(text).to.contain(`${suffix}.m3u8?videoFileToken=${videoFileToken}`)
expect(text).to.contain(`${suffix}.m3u8?videoFileToken=${videoFileToken}&reinjectVideoFileToken=true`)
}

const resolutionPlaylists = extractResolutionPlaylistUrls(hls.playlistUrl, text)
Expand All @@ -228,6 +228,7 @@ async function checkVideoFileTokenReinjection (options: {
: '.mp4'

expect(text).to.contain(`${extension}?videoFileToken=${videoFileToken}`)
expect(text).not.to.contain(`reinjectVideoFileToken=true`)
}
}

Expand Down

0 comments on commit 73fb3dc

Please sign in to comment.