Skip to content

ActivityPub-federated video streaming platform using P2P directly in your web browser

Prevent videos/playlists metadata leak

Chocobozzz60166629b639
diff --git a/packages/tests/src/api/search/search-activitypub-video-playlists.ts b/packages/tests/src/api/search/search-activitypub-video-playlists.ts
index c343c73759..0ba5caf0a5 100644
--- a/packages/tests/src/api/search/search-activitypub-video-playlists.ts
+++ b/packages/tests/src/api/search/search-activitypub-video-playlists.ts
@@ -2,7 +2,7 @@
import { expect } from 'chai'
import { wait } from '@peertube/peertube-core-utils'
-import { VideoPlaylistPrivacy } from '@peertube/peertube-models'
+import { HttpStatusCode, VideoPlaylistPrivacy } from '@peertube/peertube-models'
import {
cleanupTests,
createMultipleServers,
@@ -208,6 +208,28 @@ describe('Test ActivityPub playlists search', function () {
expect(body.data).to.have.lengthOf(0)
})
+ it('Should not leak a private playlist via URI search', async function () {
+ const userToken = await servers[0].users.generateUserAndToken('user_private_playlist_search')
+
+ const attributes = {
+ displayName: 'private playlist on server 1',
+ privacy: VideoPlaylistPrivacy.PRIVATE,
+ videoChannelId: servers[0].store.channel.id
+ }
+ const { uuid } = await servers[0].playlists.create({ attributes })
+ const search = servers[0].url + '/video-playlists/' + uuid
+
+ // Force path where we load the video from local database
+ await servers[0].config.updateExistingConfig({ newConfig: { search: { remoteUri: { users: false, anonymous: false } } } })
+
+ await command.searchPlaylists({ search, token: undefined, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
+ await command.searchPlaylists({ search, token: userToken, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
+
+ const body = await command.searchPlaylists({ search, token: servers[0].accessToken })
+ expect(body.total).to.equal(1)
+ expect(body.data[0].uuid).to.equal(uuid)
+ })
+
after(async function () {
await cleanupTests(servers)
})
diff --git a/packages/tests/src/api/search/search-activitypub-videos.ts b/packages/tests/src/api/search/search-activitypub-videos.ts
index 16e6953071..7563616c31 100644
--- a/packages/tests/src/api/search/search-activitypub-videos.ts
+++ b/packages/tests/src/api/search/search-activitypub-videos.ts
@@ -2,7 +2,7 @@
import { expect } from 'chai'
import { wait } from '@peertube/peertube-core-utils'
-import { VideoPrivacy } from '@peertube/peertube-models'
+import { HttpStatusCode, VideoPrivacy } from '@peertube/peertube-models'
import {
cleanupTests,
createMultipleServers,
@@ -190,6 +190,23 @@ describe('Test ActivityPub videos search', function () {
expect(body.data).to.have.lengthOf(0)
})
+ it('Should not leak a private video via URI search', async function () {
+ const userToken = await servers[0].users.generateUserAndToken('user_private_search')
+
+ const { uuid } = await servers[0].videos.upload({ attributes: { name: 'private video', privacy: VideoPrivacy.PRIVATE } })
+ const search = servers[0].url + '/videos/watch/' + uuid
+
+ // Force path where we load the video from local database
+ await servers[0].config.updateExistingConfig({ newConfig: { search: { remoteUri: { users: false, anonymous: false } } } })
+
+ await command.searchVideos({ search, token: undefined, expectedStatus: HttpStatusCode.UNAUTHORIZED_401 })
+ await command.searchVideos({ search, token: userToken, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
+
+ const body = await command.searchVideos({ search, token: servers[0].accessToken })
+ expect(body.total).to.equal(1)
+ expect(body.data[0].uuid).to.equal(uuid)
+ })
+
after(async function () {
await cleanupTests(servers)
})
diff --git a/server/core/controllers/api/search/search-video-playlists.ts b/server/core/controllers/api/search/search-video-playlists.ts
index 941df1d467..9de3b62bf2 100644
--- a/server/core/controllers/api/search/search-video-playlists.ts
+++ b/server/core/controllers/api/search/search-video-playlists.ts
@@ -14,7 +14,15 @@ import { buildMutedForSearchIndex, isSearchIndexSearch, isURISearch } from '@ser
import { getServerActor } from '@server/models/application/application.js'
import { VideoPlaylistModel } from '@server/models/video/video-playlist.js'
import { MVideoPlaylistFullSummary } from '@server/types/models/index.js'
-import { HttpStatusCode, ResultList, VideoPlaylist, VideoPlaylistsSearchQueryAfterSanitize } from '@peertube/peertube-models'
+import {
+ HttpStatusCode,
+ ResultList,
+ UserRight,
+ VideoPlaylist,
+ VideoPlaylistPrivacy,
+ VideoPlaylistsSearchQueryAfterSanitize
+} from '@peertube/peertube-models'
+import { authenticateOrFail } from '../../../middlewares/auth.js'
import {
asyncMiddleware,
openapiOperationDoc,
@@ -25,13 +33,15 @@ import {
videoPlaylistsListSearchValidator,
videoPlaylistsSearchSortValidator
} from '../../../middlewares/index.js'
+import { checkCanManagePlaylist } from '../../../middlewares/validators/videos/video-playlists.js'
import { searchLocalUrl } from './shared/index.js'
const logger = createLogger()
const searchPlaylistsRouter = express.Router()
-searchPlaylistsRouter.get('/video-playlists',
+searchPlaylistsRouter.get(
+ '/video-playlists',
openapiOperationDoc({ operationId: 'searchPlaylists' }),
paginationValidator,
setDefaultPagination,
@@ -52,7 +62,7 @@ function searchVideoPlaylists (req: express.Request, res: express.Response) {
const query = pickSearchPlaylistQuery(req.query)
const search = query.search
- if (isURISearch(search)) return searchVideoPlaylistsURI(search, res)
+ if (isURISearch(search)) return searchVideoPlaylistsURI(search, req, res)
if (isSearchIndexSearch(query)) {
return searchVideoPlaylistsIndex(query, res)
@@ -103,7 +113,7 @@ async function searchVideoPlaylistsDB (query: VideoPlaylistsSearchQueryAfterSani
return res.json(getFormattedObjects(resultList.data, resultList.total))
}
-async function searchVideoPlaylistsURI (search: string, res: express.Response) {
+async function searchVideoPlaylistsURI (search: string, req: express.Request, res: express.Response) {
let videoPlaylist: MVideoPlaylistFullSummary
if (isUserAbleToSearchRemoteURI(res)) {
@@ -118,6 +128,23 @@ async function searchVideoPlaylistsURI (search: string, res: express.Response) {
videoPlaylist = await searchLocalUrl(sanitizeLocalUrl(search), url => VideoPlaylistModel.loadByUrlWithAccountAndChannelSummary(url))
}
+ // The resolved playlist may still be private
+ if (videoPlaylist?.privacy === VideoPlaylistPrivacy.PRIVATE) {
+ if (!await authenticateOrFail({ req, res })) return
+
+ if (
+ !await checkCanManagePlaylist({
+ user: res.locals.oauth.token.User,
+ videoPlaylist,
+ right: UserRight.UPDATE_ANY_VIDEO_PLAYLIST,
+ req,
+ res
+ })
+ ) {
+ return
+ }
+ }
+
return res.json({
total: videoPlaylist ? 1 : 0,
data: videoPlaylist ? [ videoPlaylist.toFormattedJSON() ] : []
@@ -129,5 +156,5 @@ function sanitizeLocalUrl (url: string) {
// Handle alternative channel URLs
return url.replace(new RegExp('^' + WEBSERVER.URL + '/videos/watch/playlist/'), WEBSERVER.URL + '/video-playlists/')
- .replace(new RegExp('^' + WEBSERVER.URL + '/w/p/'), WEBSERVER.URL + '/video-playlists/')
+ .replace(new RegExp('^' + WEBSERVER.URL + '/w/p/'), WEBSERVER.URL + '/video-playlists/')
}
diff --git a/server/core/controllers/api/search/search-videos.ts b/server/core/controllers/api/search/search-videos.ts
index 78d9dd27a5..40159edfbe 100644
--- a/server/core/controllers/api/search/search-videos.ts
+++ b/server/core/controllers/api/search/search-videos.ts
@@ -24,6 +24,7 @@ import {
videosSearchSortValidator,
videosSearchValidator
} from '../../../middlewares/index.js'
+import { checkCanSeeVideo } from '../../../middlewares/validators/shared/videos.js'
import { guessAdditionalAttributesFromQuery } from '../../../models/video/formatter/index.js'
import { VideoModel } from '../../../models/video/video.js'
import { MVideoAccountLightBlacklistAllFiles } from '../../../types/models/index.js'
@@ -57,7 +58,7 @@ function searchVideos (req: express.Request, res: express.Response) {
const search = query.search
if (isURISearch(search)) {
- return searchVideoURI(search, res)
+ return searchVideoURI(search, req, res)
}
if (isSearchIndexSearch(query)) {
@@ -136,7 +137,7 @@ async function searchVideosDB (query: VideosSearchQueryAfterSanitize, req: expre
return res.json(getFormattedObjects(resultList.data, resultList.total, guessAdditionalAttributesFromQuery(query)))
}
-async function searchVideoURI (url: string, res: express.Response) {
+async function searchVideoURI (url: string, req: express.Request, res: express.Response) {
let video: MVideoAccountLightBlacklistAllFiles
// Check if we can fetch a remote video with the URL
@@ -161,6 +162,9 @@ async function searchVideoURI (url: string, res: express.Response) {
video = await searchLocalUrl(sanitizeLocalUrl(url), url => VideoModel.loadByUrlAndPopulateAccountAndFiles(url))
}
+ // The resolved video may still be a local private/internal/password protected
+ if (video && !await checkCanSeeVideo({ req, res, video, paramId: video.uuid })) return
+
return res.json({
total: video ? 1 : 0,
data: video ? [ video.toFormattedJSON() ] : []
diff --git a/server/core/middlewares/auth.ts b/server/core/middlewares/auth.ts
index 522811870c..362f39c5fe 100644
--- a/server/core/middlewares/auth.ts
+++ b/server/core/middlewares/auth.ts
@@ -69,35 +69,47 @@ export function optionalAuthenticate (req: express.Request, res: express.Respons
return next()
}
-export function authenticateOrFail (options: {
+export function authenticatePromise (options: {
req: express.Request
res: express.Response
- errorMessage?: string
- errorStatus?: HttpStatusCodeType
- errorType?: ServerErrorCodeType
}) {
- const { req, res, errorMessage = req.t('Authentication is required'), errorStatus = HttpStatusCode.UNAUTHORIZED_401, errorType } = options
+ const { req, res } = options
return new Promise<boolean>(resolve => {
// Already authenticated? (or tried to)
if (res.locals.oauth?.token.User) return resolve(true)
if (res.locals.authenticated === false || !req.header('authorization')) {
- res.fail({ status: errorStatus, type: errorType, message: errorMessage })
-
return resolve(false)
}
authenticate(req, res, () => {
if (res.locals.oauth?.token.User) return resolve(true)
- res.fail({ status: errorStatus, type: errorType, message: errorMessage })
-
resolve(false)
})
})
}
+export async function authenticateOrFail (options: {
+ req: express.Request
+ res: express.Response
+ errorMessage?: string
+ errorStatus?: HttpStatusCodeType
+ errorType?: ServerErrorCodeType
+}) {
+ const { req, res, errorMessage = req.t('Authentication is required'), errorStatus = HttpStatusCode.UNAUTHORIZED_401, errorType } = options
+
+ const authenticated = await authenticatePromise({ req, res })
+
+ if (!authenticated) {
+ res.fail({ status: errorStatus, type: errorType, message: errorMessage })
+ return false
+ }
+
+ return true
+}
+
// ---------------------------------------------------------------------------
export function authenticateRunnerSocket (socket: Socket, next: (err?: any) => void) {
diff --git a/server/core/middlewares/validators/videos/video-playlists.ts b/server/core/middlewares/validators/videos/video-playlists.ts
index fea1ecd97f..938b8379ab 100644
--- a/server/core/middlewares/validators/videos/video-playlists.ts
+++ b/server/core/middlewares/validators/videos/video-playlists.ts
@@ -507,17 +507,17 @@ function getCommonPlaylistEditAttributes () {
] as (ValidationChain | ExpressPromiseHandler)[]
}
-async function checkCanManagePlaylist (options: {
+export async function checkCanManagePlaylist (options: {
user: MUserAccountId
videoPlaylist: MVideoPlaylistFullSummary
right: UserRightType
req: express.Request
- res: express.Response
+ res: express.Response | null
}) {
const { user, videoPlaylist, right, res, req } = options
if (videoPlaylist.isLocal() === false) {
- res.fail({
+ res?.fail({
status: HttpStatusCode.FORBIDDEN_403,
message: req.t('Cannot manage video playlist of another server.')
})
@@ -544,7 +544,7 @@ async function checkCanManagePlaylist (options: {
}
}
- res.fail({
+ res?.fail({
status: HttpStatusCode.FORBIDDEN_403,
message: req.t('Cannot manage video playlist of another user')
})