Skip to content

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

Prevent SSRF

Chocobozzz6a1588e3c35c
diff --git a/package.json b/package.json
index f4d58ab79e..1209d98930 100644
--- a/package.json
+++ b/package.json
@@ -200,7 +200,6 @@
"ua-parser-js": "^2.0.10",
"uuid": "^14.0.2",
"validator": "^13.15.35",
- "webfinger.js": "3.0.4",
"winston": "3.19.0",
"ws": "^8.21.3",
"yauzl": "^3.4.0"
diff --git a/packages/tests/src/api/search/search-activitypub-video-channels.ts b/packages/tests/src/api/search/search-activitypub-video-channels.ts
index 8067346034..1b7d446318 100644
--- a/packages/tests/src/api/search/search-activitypub-video-channels.ts
+++ b/packages/tests/src/api/search/search-activitypub-video-channels.ts
@@ -91,7 +91,7 @@ describe('Test ActivityPub video channels search', function () {
]
for (const search of searches) {
- const body = await command.searchChannels({ search })
+ const body = await command.searchChannels({ search, token: servers[0].accessToken })
expect(body.total).to.equal(1)
expect(body.data).to.be.an('array')
diff --git a/packages/tests/src/api/search/search-videos.ts b/packages/tests/src/api/search/search-videos.ts
index 50ba8e0c9f..615c823d1e 100644
--- a/packages/tests/src/api/search/search-videos.ts
+++ b/packages/tests/src/api/search/search-videos.ts
@@ -667,6 +667,6 @@ describe('Test videos search', function () {
})
after(async function () {
- await cleanupTests([ server ])
+ await cleanupTests([ server, remoteServer ])
})
})
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 6345f0a330..9d2a7c656f 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -347,9 +347,6 @@ importers:
validator:
specifier: ^13.15.35
version: 13.15.35
- webfinger.js:
- specifier: 3.0.4
- version: 3.0.4
winston:
specifier: 3.19.0
version: 3.19.0
@@ -11255,9 +11252,6 @@ packages:
puppeteer-core:
optional: true
- resolution: {integrity: sha512-5c15N1n4qCm/jGJjUt32mBdPVlSugLbAztIDNBpuDfukGz2E9NhmXPfLikayn2p3kcgEZsI/UOdOwVpxOr8qJA==}
-
resolution: {integrity: sha512-VwddBukDzu71offAQR975unBIGqfKZpM+8ZX6ySk8nYhVoo5CYaZyzt3YBvYtRtO+aoGlqxPg/B87NGVZ/fu6g==}
engines: {node: '>=12'}
@@ -23133,8 +23127,6 @@ snapshots:
- supports-color
- utf-8-validate
-
diff --git a/server/core/controllers/api/search/search-video-channels.ts b/server/core/controllers/api/search/search-video-channels.ts
index b6cd4db7e6..961be6237c 100644
--- a/server/core/controllers/api/search/search-video-channels.ts
+++ b/server/core/controllers/api/search/search-video-channels.ts
@@ -117,7 +117,12 @@ async function searchVideoChannelURI (search: string, res: express.Response) {
let videoChannel: MChannelAccountDefault
let uri = search
+ const canSearchRemoteURI = isUserAbleToSearchRemoteURI(res)
+
if (!isURISearch(search)) {
+ // Resolving a "name@host" handle requires an outbound webfinger lookup
+ if (!canSearchRemoteURI) return res.json({ total: 0, data: [] })
+
try {
uri = await loadActorUrlOrGetFromWebfinger(search)
} catch (err) {
@@ -127,7 +132,7 @@ async function searchVideoChannelURI (search: string, res: express.Response) {
}
}
- if (isUserAbleToSearchRemoteURI(res)) {
+ if (canSearchRemoteURI) {
try {
const latestUri = await findLatestAPRedirection(uri)
diff --git a/server/core/helpers/custom-jsonld-signature.ts b/server/core/helpers/custom-jsonld-signature.ts
index 0ee5802322..3fb9540125 100644
--- a/server/core/helpers/custom-jsonld-signature.ts
+++ b/server/core/helpers/custom-jsonld-signature.ts
@@ -1,6 +1,14 @@
import jsonld from 'jsonld'
+import { ACTIVITY_STREAMS_CONTEXT } from './jsonld-contexts/activitystreams-context.js'
+import { createLogger } from './logger.js'
+import { doJSONRequest } from './requests.js'
+import { REQUEST_TIMEOUTS } from '../initializers/constants.js'
+
+const logger = createLogger()
const STATIC_CACHE = {
+ 'https://www.w3.org/ns/activitystreams': ACTIVITY_STREAMS_CONTEXT,
+
'https://w3id.org/security/v1': {
'@context': {
id: '@id',
@@ -53,10 +61,8 @@ const STATIC_CACHE = {
}
}
-const localCache = new Map<string, any>()
-
-const nodeDocumentLoader = (jsonld as any).documentLoaders.node()
-
+const REMOTE_CONTEXT_CACHE_MAX_ENTRIES = 100
+const remoteContextCache = new Map<string, any>()
;(jsonld as any).documentLoader = async (url: string) => {
if (url in STATIC_CACHE) {
return {
@@ -66,12 +72,20 @@ const nodeDocumentLoader = (jsonld as any).documentLoaders.node()
}
}
- if (localCache.has(url)) return localCache.get(url)
+ if (remoteContextCache.has(url)) return remoteContextCache.get(url)
+
+ logger.debug('Fetching non-pinned JSON-LD context/document.', { url })
+
+ const { body } = await doJSONRequest<any>(url, {
+ timeout: REQUEST_TIMEOUTS.DEFAULT,
+ bodyKBLimit: 1000,
+ headers: { accept: 'application/ld+json, application/json' }
+ })
- const remoteDoc = await nodeDocumentLoader(url)
+ const remoteDoc = { contextUrl: null, document: body, documentUrl: url }
- if (localCache.size < 100) {
- localCache.set(url, remoteDoc)
+ if (remoteContextCache.size < REMOTE_CONTEXT_CACHE_MAX_ENTRIES) {
+ remoteContextCache.set(url, remoteDoc)
}
return remoteDoc
diff --git a/server/core/helpers/jsonld-contexts/activitystreams-context.ts b/server/core/helpers/jsonld-contexts/activitystreams-context.ts
new file mode 100644
index 0000000000..1387aa53ff
--- /dev/null
+++ b/server/core/helpers/jsonld-contexts/activitystreams-context.ts
@@ -0,0 +1,156 @@
+// Vendored copy of https://www.w3.org/ns/activitystreams, fetched 2026-09-22.
+// Kept offline/pinned so PeerTube's JSON-LD document loader never has to perform
+// a live fetch for this context (see ../custom-jsonld-signature.ts).
+export const ACTIVITY_STREAMS_CONTEXT = {
+ '@context': {
+ '@vocab': '_:',
+ xsd: 'http://www.w3.org/2001/XMLSchema#',
+ as: 'https://www.w3.org/ns/activitystreams#',
+ ldp: 'http://www.w3.org/ns/ldp#',
+ vcard: 'http://www.w3.org/2006/vcard/ns#',
+ id: '@id',
+ type: '@type',
+ Accept: 'as:Accept',
+ Activity: 'as:Activity',
+ IntransitiveActivity: 'as:IntransitiveActivity',
+ Add: 'as:Add',
+ Announce: 'as:Announce',
+ Application: 'as:Application',
+ Arrive: 'as:Arrive',
+ Article: 'as:Article',
+ Audio: 'as:Audio',
+ Block: 'as:Block',
+ Collection: 'as:Collection',
+ CollectionPage: 'as:CollectionPage',
+ Relationship: 'as:Relationship',
+ Create: 'as:Create',
+ Delete: 'as:Delete',
+ Dislike: 'as:Dislike',
+ Document: 'as:Document',
+ Event: 'as:Event',
+ Follow: 'as:Follow',
+ Flag: 'as:Flag',
+ Group: 'as:Group',
+ Ignore: 'as:Ignore',
+ Image: 'as:Image',
+ Invite: 'as:Invite',
+ Join: 'as:Join',
+ Leave: 'as:Leave',
+ Like: 'as:Like',
+ Link: 'as:Link',
+ Mention: 'as:Mention',
+ Note: 'as:Note',
+ Object: 'as:Object',
+ Offer: 'as:Offer',
+ OrderedCollection: 'as:OrderedCollection',
+ OrderedCollectionPage: 'as:OrderedCollectionPage',
+ Organization: 'as:Organization',
+ Page: 'as:Page',
+ Person: 'as:Person',
+ Place: 'as:Place',
+ Profile: 'as:Profile',
+ Question: 'as:Question',
+ Reject: 'as:Reject',
+ Remove: 'as:Remove',
+ Service: 'as:Service',
+ TentativeAccept: 'as:TentativeAccept',
+ TentativeReject: 'as:TentativeReject',
+ Tombstone: 'as:Tombstone',
+ Undo: 'as:Undo',
+ Update: 'as:Update',
+ Video: 'as:Video',
+ View: 'as:View',
+ Listen: 'as:Listen',
+ Read: 'as:Read',
+ Move: 'as:Move',
+ Travel: 'as:Travel',
+ IsFollowing: 'as:IsFollowing',
+ IsFollowedBy: 'as:IsFollowedBy',
+ IsContact: 'as:IsContact',
+ IsMember: 'as:IsMember',
+ subject: { '@id': 'as:subject', '@type': '@id' },
+ relationship: { '@id': 'as:relationship', '@type': '@id' },
+ actor: { '@id': 'as:actor', '@type': '@id' },
+ attributedTo: { '@id': 'as:attributedTo', '@type': '@id' },
+ attachment: { '@id': 'as:attachment', '@type': '@id' },
+ bcc: { '@id': 'as:bcc', '@type': '@id' },
+ bto: { '@id': 'as:bto', '@type': '@id' },
+ cc: { '@id': 'as:cc', '@type': '@id' },
+ context: { '@id': 'as:context', '@type': '@id' },
+ current: { '@id': 'as:current', '@type': '@id' },
+ first: { '@id': 'as:first', '@type': '@id' },
+ generator: { '@id': 'as:generator', '@type': '@id' },
+ icon: { '@id': 'as:icon', '@type': '@id' },
+ image: { '@id': 'as:image', '@type': '@id' },
+ inReplyTo: { '@id': 'as:inReplyTo', '@type': '@id' },
+ items: { '@id': 'as:items', '@type': '@id' },
+ instrument: { '@id': 'as:instrument', '@type': '@id' },
+ orderedItems: { '@id': 'as:items', '@type': '@id', '@container': '@list' },
+ last: { '@id': 'as:last', '@type': '@id' },
+ location: { '@id': 'as:location', '@type': '@id' },
+ next: { '@id': 'as:next', '@type': '@id' },
+ object: { '@id': 'as:object', '@type': '@id' },
+ oneOf: { '@id': 'as:oneOf', '@type': '@id' },
+ anyOf: { '@id': 'as:anyOf', '@type': '@id' },
+ closed: { '@id': 'as:closed', '@type': 'xsd:dateTime' },
+ origin: { '@id': 'as:origin', '@type': '@id' },
+ accuracy: { '@id': 'as:accuracy', '@type': 'xsd:float' },
+ prev: { '@id': 'as:prev', '@type': '@id' },
+ preview: { '@id': 'as:preview', '@type': '@id' },
+ replies: { '@id': 'as:replies', '@type': '@id' },
+ result: { '@id': 'as:result', '@type': '@id' },
+ audience: { '@id': 'as:audience', '@type': '@id' },
+ partOf: { '@id': 'as:partOf', '@type': '@id' },
+ tag: { '@id': 'as:tag', '@type': '@id' },
+ target: { '@id': 'as:target', '@type': '@id' },
+ to: { '@id': 'as:to', '@type': '@id' },
+ url: { '@id': 'as:url', '@type': '@id' },
+ altitude: { '@id': 'as:altitude', '@type': 'xsd:float' },
+ content: 'as:content',
+ contentMap: { '@id': 'as:content', '@container': '@language' },
+ name: 'as:name',
+ nameMap: { '@id': 'as:name', '@container': '@language' },
+ duration: { '@id': 'as:duration', '@type': 'xsd:duration' },
+ endTime: { '@id': 'as:endTime', '@type': 'xsd:dateTime' },
+ height: { '@id': 'as:height', '@type': 'xsd:nonNegativeInteger' },
+ href: { '@id': 'as:href', '@type': '@id' },
+ hreflang: 'as:hreflang',
+ latitude: { '@id': 'as:latitude', '@type': 'xsd:float' },
+ longitude: { '@id': 'as:longitude', '@type': 'xsd:float' },
+ mediaType: 'as:mediaType',
+ published: { '@id': 'as:published', '@type': 'xsd:dateTime' },
+ radius: { '@id': 'as:radius', '@type': 'xsd:float' },
+ rel: 'as:rel',
+ startIndex: { '@id': 'as:startIndex', '@type': 'xsd:nonNegativeInteger' },
+ startTime: { '@id': 'as:startTime', '@type': 'xsd:dateTime' },
+ summary: 'as:summary',
+ summaryMap: { '@id': 'as:summary', '@container': '@language' },
+ totalItems: { '@id': 'as:totalItems', '@type': 'xsd:nonNegativeInteger' },
+ units: 'as:units',
+ updated: { '@id': 'as:updated', '@type': 'xsd:dateTime' },
+ width: { '@id': 'as:width', '@type': 'xsd:nonNegativeInteger' },
+ describes: { '@id': 'as:describes', '@type': '@id' },
+ formerType: { '@id': 'as:formerType', '@type': '@id' },
+ deleted: { '@id': 'as:deleted', '@type': 'xsd:dateTime' },
+ inbox: { '@id': 'ldp:inbox', '@type': '@id' },
+ outbox: { '@id': 'as:outbox', '@type': '@id' },
+ following: { '@id': 'as:following', '@type': '@id' },
+ followers: { '@id': 'as:followers', '@type': '@id' },
+ streams: { '@id': 'as:streams', '@type': '@id' },
+ preferredUsername: 'as:preferredUsername',
+ endpoints: { '@id': 'as:endpoints', '@type': '@id' },
+ uploadMedia: { '@id': 'as:uploadMedia', '@type': '@id' },
+ proxyUrl: { '@id': 'as:proxyUrl', '@type': '@id' },
+ liked: { '@id': 'as:liked', '@type': '@id' },
+ oauthAuthorizationEndpoint: { '@id': 'as:oauthAuthorizationEndpoint', '@type': '@id' },
+ oauthTokenEndpoint: { '@id': 'as:oauthTokenEndpoint', '@type': '@id' },
+ provideClientKey: { '@id': 'as:provideClientKey', '@type': '@id' },
+ signClientKey: { '@id': 'as:signClientKey', '@type': '@id' },
+ sharedInbox: { '@id': 'as:sharedInbox', '@type': '@id' },
+ Public: { '@id': 'as:Public', '@type': '@id' },
+ source: 'as:source',
+ likes: { '@id': 'as:likes', '@type': '@id' },
+ shares: { '@id': 'as:shares', '@type': '@id' },
+ alsoKnownAs: { '@id': 'as:alsoKnownAs', '@type': '@id' }
+ }
+}
diff --git a/server/core/lib/activitypub/actors/webfinger.ts b/server/core/lib/activitypub/actors/webfinger.ts
index 79b2214ff4..18ba0652d9 100644
--- a/server/core/lib/activitypub/actors/webfinger.ts
+++ b/server/core/lib/activitypub/actors/webfinger.ts
@@ -1,18 +1,12 @@
-import { isProdInstance } from '@peertube/peertube-node-utils'
import { isActivityPubUrlValid } from '@server/helpers/custom-validators/activitypub/misc.js'
-import { CONFIG } from '@server/initializers/config.js'
-import { REQUEST_TIMEOUTS, WEBSERVER } from '@server/initializers/constants.js'
+import { doJSONRequest } from '@server/helpers/requests.js'
+import { REMOTE_SCHEME, REQUEST_TIMEOUTS, WEBSERVER } from '@server/initializers/constants.js'
import { ActorModel } from '@server/models/actor/actor.js'
import { MActorFull } from '@server/types/models/index.js'
-import WebFinger from 'webfinger.js'
-// oxlint-disable-next-line @typescript-eslint/no-deprecated
-const webfinger = new WebFinger({
- tls_only: isProdInstance(),
- uri_fallback: false,
- request_timeout: REQUEST_TIMEOUTS.DEFAULT,
- allow_private_addresses: CONFIG.FEDERATION.PREVENT_SSRF === false
-})
+type WebFingerJRD = {
+ links?: { rel?: string, href?: string }[]
+}
export async function loadActorUrlOrGetFromWebfinger (uriArg: string) {
// Handle strings like @[email protected]
@@ -33,14 +27,22 @@ export async function loadActorUrlOrGetFromWebfinger (uriArg: string) {
}
export async function getUrlFromWebfinger (uri: string) {
- const { object } = await webfinger.lookup(uri)
+ const [ name, host ] = uri.split('@')
+ if (!name || !host) throw new Error(`Invalid webfinger address "${uri}"`)
+
+ const url = `${REMOTE_SCHEME.HTTP}://${host}/.well-known/webfinger?resource=${encodeURIComponent('acct:' + uri)}`
+
+ const { body } = await doJSONRequest<WebFingerJRD>(url, {
+ timeout: REQUEST_TIMEOUTS.DEFAULT,
+ headers: { accept: 'application/jrd+json, application/json' }
+ })
- if (Array.isArray(object.links) === false) throw new Error('WebFinger links is not an array.')
+ if (Array.isArray(body.links) === false) throw new Error('WebFinger links is not an array.')
- const selfLink = object.links.find(l => l.rel === 'self')
- if (selfLink === undefined || isActivityPubUrlValid(selfLink.href as string) === false) {
+ const selfLink = body.links.find(l => l.rel === 'self')
+ if (selfLink === undefined || isActivityPubUrlValid(selfLink.href) === false) {
throw new Error('Cannot find self link or href is not a valid URL.')
}
- return selfLink.href as string
+ return selfLink.href
}
diff --git a/server/core/lib/video-pre-import.ts b/server/core/lib/video-pre-import.ts
index 181307d63a..ad482960ba 100644
--- a/server/core/lib/video-pre-import.ts
+++ b/server/core/lib/video-pre-import.ts
@@ -374,7 +374,11 @@ async function processYoutubeSubtitles (options: {
}
async function hasUnicastURLsOnly (youtubeDLInfo: YoutubeDLInfo) {
- const hosts = youtubeDLInfo.urls.map(u => new URL(u).hostname)
+ const urls = youtubeDLInfo.webpageUrl
+ ? [ ...youtubeDLInfo.urls, youtubeDLInfo.webpageUrl ]
+ : youtubeDLInfo.urls
+
+ const hosts = urls.map(u => new URL(u).hostname)
const uniqHosts = new Set(hosts)
for (const h of uniqHosts) {