2024-05-23 09:42:14 +00:00
|
|
|
// SPDX-FileCopyrightText: 2024 John Livingston <https://www.john-livingston.fr/>
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2023-05-24 13:09:56 +00:00
|
|
|
import type { RegisterServerOptions } from '@peertube/peertube-types'
|
2023-05-24 14:09:55 +00:00
|
|
|
import type { LiveChatJSONLDAttributeV1, PeertubeXMPPServerInfos } from './types'
|
2023-04-20 16:28:08 +00:00
|
|
|
import { URL } from 'url'
|
|
|
|
|
2023-05-24 13:09:56 +00:00
|
|
|
/**
|
|
|
|
* Use this function for incoming remote informations.
|
|
|
|
* It will sanitize them, by checking everything is ok.
|
|
|
|
* It can also migrate from old format to the new one.
|
|
|
|
*
|
|
|
|
* This function can be used when informations are incoming,
|
|
|
|
* or when reading stored information (to automatically migrate them)
|
|
|
|
*
|
2023-05-31 14:19:45 +00:00
|
|
|
* @param options server options
|
2023-05-24 13:09:56 +00:00
|
|
|
* @param chatInfos remote chat informations
|
2023-05-31 14:19:45 +00:00
|
|
|
* @param referenceUrl optional url string. If given, we must check that urls are on the same domain, to avoid spoofing.
|
2023-05-24 13:09:56 +00:00
|
|
|
* @returns a sanitized version of the remote chat informations
|
|
|
|
*/
|
2023-05-31 14:19:45 +00:00
|
|
|
function sanitizePeertubeLiveChatInfos (
|
|
|
|
options: RegisterServerOptions,
|
|
|
|
chatInfos: any,
|
|
|
|
referenceUrl?: string
|
|
|
|
): LiveChatJSONLDAttributeV1 {
|
2023-04-20 16:28:08 +00:00
|
|
|
if (chatInfos === false) { return false }
|
|
|
|
if (typeof chatInfos !== 'object') { return false }
|
|
|
|
|
|
|
|
if (chatInfos.type !== 'xmpp') { return false }
|
|
|
|
if ((typeof chatInfos.jid) !== 'string') { return false }
|
|
|
|
|
2023-05-24 13:09:56 +00:00
|
|
|
if (!('xmppserver' in chatInfos)) {
|
|
|
|
// V0 format, migrating on the fly to v1.
|
2023-05-31 14:19:45 +00:00
|
|
|
return _sanitizePeertubeLiveChatInfosV0(options, chatInfos, referenceUrl)
|
2023-04-20 16:28:08 +00:00
|
|
|
}
|
|
|
|
|
2023-05-24 13:09:56 +00:00
|
|
|
if (!chatInfos.xmppserver || (typeof chatInfos.xmppserver !== 'object')) {
|
|
|
|
return false
|
|
|
|
}
|
2023-05-31 14:19:45 +00:00
|
|
|
const xmppserver = sanitizePeertubeLiveChatServerInfos(options, chatInfos.xmppserver, referenceUrl)
|
2023-05-24 14:09:55 +00:00
|
|
|
if (!xmppserver) { return false }
|
|
|
|
|
2024-06-06 16:04:17 +00:00
|
|
|
let customEmojisUrl: string | undefined
|
|
|
|
if (('customEmojisUrl' in chatInfos) && chatInfos.customEmojisUrl) {
|
|
|
|
customEmojisUrl = sanitizeCustomEmojisUrl(options, chatInfos.customEmojisUrl, referenceUrl)
|
|
|
|
}
|
|
|
|
|
2023-05-24 14:09:55 +00:00
|
|
|
const r: LiveChatJSONLDAttributeV1 = {
|
|
|
|
type: chatInfos.type,
|
|
|
|
jid: chatInfos.jid,
|
2024-06-06 16:04:17 +00:00
|
|
|
xmppserver,
|
|
|
|
customEmojisUrl
|
2023-05-24 14:09:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2023-05-31 14:19:45 +00:00
|
|
|
/**
|
|
|
|
* Use this function for incoming remote server informations.
|
|
|
|
* It will sanitize them, by checking everything is ok.
|
|
|
|
* It can also migrate from old format to the new one.
|
|
|
|
*
|
|
|
|
* This function can be used when informations are incoming,
|
|
|
|
* or when reading stored information (to automatically migrate them)
|
|
|
|
* @param options server options
|
|
|
|
* @param xmppserver remote server information
|
|
|
|
* @param referenceUrl optional url string. If given, we must check that urls are on the same domain, to avoid spoofing.
|
|
|
|
* @returns a sanitized version of remote server information (or false if not valid)
|
|
|
|
*/
|
2023-05-24 14:09:55 +00:00
|
|
|
function sanitizePeertubeLiveChatServerInfos (
|
2023-05-31 14:19:45 +00:00
|
|
|
options: RegisterServerOptions, xmppserver: any, referenceUrl?: string
|
2023-05-24 14:09:55 +00:00
|
|
|
): PeertubeXMPPServerInfos | false {
|
|
|
|
if (!xmppserver || (typeof xmppserver !== 'object')) {
|
|
|
|
return false
|
|
|
|
}
|
2023-04-20 16:28:08 +00:00
|
|
|
|
2023-05-31 14:19:45 +00:00
|
|
|
let checkHost: undefined | string
|
|
|
|
if (referenceUrl) {
|
|
|
|
checkHost = _readReferenceUrl(referenceUrl)
|
|
|
|
if (!checkHost) {
|
|
|
|
options.peertubeHelpers.logger.error(
|
|
|
|
'sanitizePeertubeLiveChatServerInfos: got an invalid referenceUrl: ' + referenceUrl
|
|
|
|
)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-24 13:09:56 +00:00
|
|
|
if ((typeof xmppserver.host) !== 'string') { return false }
|
2023-05-31 14:19:45 +00:00
|
|
|
const host = _validateHost(xmppserver.host, checkHost)
|
2023-05-24 13:09:56 +00:00
|
|
|
if (!host) { return false }
|
|
|
|
if ((typeof xmppserver.muc) !== 'string') { return false }
|
2023-05-31 14:19:45 +00:00
|
|
|
const muc = _validateHost(xmppserver.muc, checkHost)
|
2023-05-24 13:09:56 +00:00
|
|
|
if (!muc) { return false }
|
2023-04-20 16:28:08 +00:00
|
|
|
|
2023-05-24 14:09:55 +00:00
|
|
|
const r: PeertubeXMPPServerInfos = {
|
|
|
|
host,
|
|
|
|
muc
|
2023-05-24 13:09:56 +00:00
|
|
|
}
|
|
|
|
|
2024-04-18 13:42:06 +00:00
|
|
|
// This comes with livechat >= 9.0.0, can be absent.
|
|
|
|
const external = _validateHost(xmppserver.external, checkHost)
|
|
|
|
if (external) {
|
|
|
|
r.external = external
|
|
|
|
}
|
|
|
|
|
2023-05-24 13:09:56 +00:00
|
|
|
if (xmppserver.directs2s) {
|
|
|
|
if ((typeof xmppserver.directs2s) === 'object') {
|
|
|
|
const port = xmppserver.directs2s.port
|
|
|
|
if ((typeof port === 'string') && /^\d+$/.test(port)) {
|
2023-05-24 14:09:55 +00:00
|
|
|
r.directs2s = {
|
2023-05-24 13:09:56 +00:00
|
|
|
port
|
|
|
|
}
|
2023-05-19 10:52:52 +00:00
|
|
|
}
|
2023-05-24 13:09:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (xmppserver.websockets2s) {
|
|
|
|
if ((typeof xmppserver.websockets2s) === 'object') {
|
|
|
|
const url = xmppserver.websockets2s.url
|
|
|
|
if ((typeof url === 'string') && _validUrl(url, {
|
|
|
|
noSearchParams: true,
|
2023-05-31 14:19:45 +00:00
|
|
|
protocol: 'ws.',
|
|
|
|
domain: checkHost
|
2023-05-24 13:09:56 +00:00
|
|
|
})) {
|
2023-05-24 14:09:55 +00:00
|
|
|
r.websockets2s = {
|
2023-05-24 13:09:56 +00:00
|
|
|
url
|
|
|
|
}
|
2023-05-19 10:52:52 +00:00
|
|
|
}
|
|
|
|
}
|
2023-05-24 13:09:56 +00:00
|
|
|
}
|
|
|
|
if (xmppserver.anonymous) {
|
2023-05-31 14:19:45 +00:00
|
|
|
const virtualhost = _validateHost(xmppserver.anonymous.virtualhost, checkHost)
|
2023-05-24 13:09:56 +00:00
|
|
|
if (virtualhost) {
|
2023-05-24 14:09:55 +00:00
|
|
|
r.anonymous = {
|
2023-05-24 13:09:56 +00:00
|
|
|
virtualhost
|
|
|
|
}
|
2023-05-19 10:52:52 +00:00
|
|
|
|
2023-05-24 13:09:56 +00:00
|
|
|
const bosh = xmppserver.anonymous.bosh
|
|
|
|
if ((typeof bosh === 'string') && _validUrl(bosh, {
|
|
|
|
noSearchParams: true,
|
2023-05-31 14:19:45 +00:00
|
|
|
protocol: 'http.',
|
|
|
|
domain: checkHost
|
2023-05-24 13:09:56 +00:00
|
|
|
})) {
|
2023-05-24 14:09:55 +00:00
|
|
|
r.anonymous.bosh = bosh
|
2023-05-19 10:52:52 +00:00
|
|
|
}
|
|
|
|
|
2023-05-24 13:09:56 +00:00
|
|
|
const websocket = xmppserver.anonymous.websocket
|
|
|
|
if ((typeof websocket === 'string') && _validUrl(websocket, {
|
|
|
|
noSearchParams: true,
|
2023-05-31 14:19:45 +00:00
|
|
|
protocol: 'ws.',
|
|
|
|
domain: checkHost
|
2023-05-24 13:09:56 +00:00
|
|
|
})) {
|
2023-05-24 14:09:55 +00:00
|
|
|
r.anonymous.websocket = websocket
|
2023-05-24 13:09:56 +00:00
|
|
|
}
|
2023-05-04 17:14:23 +00:00
|
|
|
}
|
2023-04-20 16:28:08 +00:00
|
|
|
}
|
2023-05-24 13:09:56 +00:00
|
|
|
|
2023-04-20 16:28:08 +00:00
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2024-06-06 16:04:17 +00:00
|
|
|
/**
|
|
|
|
* Use this function for incoming custom emojis definition url.
|
|
|
|
* It will sanitize them, by checking everything is ok.
|
|
|
|
*
|
|
|
|
* @param options server options
|
|
|
|
* @param customEmojisUrl the value to test.
|
|
|
|
* @param referenceUrl optional url string. If given, we must check that urls are on the same domain, to avoid spoofing.
|
|
|
|
* @returns the url if valid, else undefined.
|
|
|
|
*/
|
|
|
|
function sanitizeCustomEmojisUrl (
|
|
|
|
options: RegisterServerOptions,
|
|
|
|
customEmojisUrl: any,
|
|
|
|
referenceUrl?: string
|
|
|
|
): string | undefined {
|
|
|
|
let checkHost: undefined | string
|
|
|
|
|
|
|
|
if (referenceUrl) {
|
|
|
|
checkHost = _readReferenceUrl(referenceUrl)
|
|
|
|
if (!checkHost) {
|
|
|
|
options.peertubeHelpers.logger.error(
|
|
|
|
'sanitizeCustomEmojisUrl: got an invalid referenceUrl: ' + referenceUrl
|
|
|
|
)
|
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((typeof customEmojisUrl) !== 'string') { return undefined }
|
|
|
|
|
|
|
|
if (
|
|
|
|
!_validUrl(customEmojisUrl, {
|
|
|
|
noSearchParams: true,
|
|
|
|
protocol: 'http.',
|
|
|
|
domain: checkHost
|
|
|
|
})
|
|
|
|
) {
|
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
|
|
|
|
// No further verification. The frontend must use this url carefully (should only get JSON data).
|
|
|
|
return customEmojisUrl
|
|
|
|
}
|
|
|
|
|
2023-04-20 16:28:08 +00:00
|
|
|
interface URLConstraints {
|
|
|
|
protocol: 'http.' | 'ws.'
|
|
|
|
noSearchParams: boolean
|
2023-05-31 14:19:45 +00:00
|
|
|
domain?: string
|
2023-04-20 16:28:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function _validUrl (s: string, constraints: URLConstraints): boolean {
|
|
|
|
if ((typeof s) !== 'string') { return false }
|
|
|
|
if (s === '') { return false }
|
|
|
|
let url: URL
|
|
|
|
try {
|
|
|
|
url = new URL(s)
|
|
|
|
} catch (_err) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if (constraints.protocol) {
|
|
|
|
if (constraints.protocol === 'http.') {
|
2023-04-21 14:33:17 +00:00
|
|
|
if (url.protocol !== 'https:' && url.protocol !== 'http:') {
|
2023-04-20 16:28:08 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
} else if (constraints.protocol === 'ws.') {
|
2023-04-21 14:33:17 +00:00
|
|
|
if (url.protocol !== 'wss:' && url.protocol !== 'ws:') {
|
2023-04-20 16:28:08 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (constraints.noSearchParams) {
|
|
|
|
if (url.search !== '') {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-31 14:19:45 +00:00
|
|
|
if (constraints.domain) {
|
|
|
|
if (url.hostname !== constraints.domain) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-20 16:28:08 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2023-05-31 14:19:45 +00:00
|
|
|
function _validateHost (s: any, mustBeSubDomainOf?: string): false | string {
|
2023-05-19 10:52:52 +00:00
|
|
|
try {
|
2023-05-24 13:09:56 +00:00
|
|
|
if (typeof s !== 'string') { return false }
|
2023-05-19 10:52:52 +00:00
|
|
|
if (s.includes('/')) { return false }
|
|
|
|
const url = new URL('http://' + s)
|
2023-05-31 14:19:45 +00:00
|
|
|
const hostname = url.hostname
|
|
|
|
if (mustBeSubDomainOf && hostname !== mustBeSubDomainOf) {
|
|
|
|
const parts = hostname.split('.')
|
|
|
|
if (parts.length <= 2) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
parts.shift()
|
|
|
|
if (parts.join('.') !== mustBeSubDomainOf) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return hostname
|
2023-05-19 10:52:52 +00:00
|
|
|
} catch (_err) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-31 14:19:45 +00:00
|
|
|
function _readReferenceUrl (s: any): undefined | string {
|
|
|
|
try {
|
|
|
|
if (typeof s !== 'string') { return undefined }
|
|
|
|
if (!s.startsWith('https://') && !s.startsWith('http://')) {
|
|
|
|
s = 'http://' + s
|
|
|
|
}
|
|
|
|
const url = new URL(s)
|
|
|
|
const host = url.hostname
|
|
|
|
// Just to avoid some basic spoofing, we must check that host is not simply something like "com".
|
|
|
|
// We will check if there is at least one dot. This test is not perfect, but can limit spoofing cases.
|
|
|
|
if (!host.includes('.')) { return undefined }
|
|
|
|
return host
|
|
|
|
} catch (_err) {
|
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function _sanitizePeertubeLiveChatInfosV0 (
|
|
|
|
options: RegisterServerOptions, chatInfos: any, referenceUrl?: string
|
|
|
|
): LiveChatJSONLDAttributeV1 {
|
2023-05-24 13:09:56 +00:00
|
|
|
const logger = options.peertubeHelpers.logger
|
|
|
|
logger.debug('We are have to migrate data from the old JSONLD format')
|
|
|
|
|
|
|
|
if (chatInfos === false) { return false }
|
|
|
|
if (typeof chatInfos !== 'object') { return false }
|
|
|
|
|
|
|
|
if (chatInfos.type !== 'xmpp') { return false }
|
|
|
|
if ((typeof chatInfos.jid) !== 'string') { return false }
|
|
|
|
|
|
|
|
// no link? invalid! dropping all.
|
|
|
|
if (!Array.isArray(chatInfos.links)) { return false }
|
|
|
|
|
2023-05-31 14:19:45 +00:00
|
|
|
let checkHost: undefined | string
|
|
|
|
if (referenceUrl) {
|
|
|
|
checkHost = _readReferenceUrl(referenceUrl)
|
|
|
|
if (!checkHost) {
|
|
|
|
options.peertubeHelpers.logger.error(
|
|
|
|
'_sanitizePeertubeLiveChatInfosV0: got an invalid referenceUrl: ' + referenceUrl
|
|
|
|
)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const muc = _validateHost(chatInfos.jid.split('@')[1], checkHost)
|
2023-05-24 13:09:56 +00:00
|
|
|
if (!muc) { return false }
|
|
|
|
if (!muc.startsWith('room.')) {
|
|
|
|
logger.error('We expected old format host to begin with "room.". Discarding.')
|
|
|
|
return false
|
|
|
|
}
|
2023-05-31 14:19:45 +00:00
|
|
|
const host = _validateHost(muc.replace(/^room\./, ''), checkHost)
|
2023-05-24 13:09:56 +00:00
|
|
|
if (!host) { return false }
|
|
|
|
|
|
|
|
const r: LiveChatJSONLDAttributeV1 = {
|
|
|
|
type: chatInfos.type,
|
|
|
|
jid: chatInfos.jid,
|
|
|
|
xmppserver: {
|
|
|
|
host,
|
|
|
|
muc
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const link of chatInfos.links) {
|
|
|
|
if ((typeof link) !== 'object') { continue }
|
|
|
|
if (['xmpp-bosh-anonymous', 'xmpp-websocket-anonymous'].includes(link.type)) {
|
|
|
|
if ((typeof link.jid) !== 'string') { continue }
|
|
|
|
if ((typeof link.url) !== 'string') { continue }
|
|
|
|
|
|
|
|
if (
|
|
|
|
!_validUrl(link.url, {
|
|
|
|
noSearchParams: true,
|
2023-05-31 14:19:45 +00:00
|
|
|
protocol: link.type === 'xmpp-websocket-anonymous' ? 'ws.' : 'http.',
|
|
|
|
domain: checkHost
|
2023-05-24 13:09:56 +00:00
|
|
|
})
|
|
|
|
) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!r.xmppserver.anonymous) {
|
|
|
|
r.xmppserver.anonymous = {
|
|
|
|
virtualhost: link.jid
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (link.type === 'xmpp-bosh-anonymous') {
|
|
|
|
r.xmppserver.anonymous.bosh = link.url
|
|
|
|
} else if (link.type === 'xmpp-websocket-anonymous') {
|
|
|
|
r.xmppserver.anonymous.websocket = link.url
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2023-05-24 14:09:55 +00:00
|
|
|
function sanitizeXMPPHost (options: RegisterServerOptions, host: any): false | string {
|
|
|
|
return _validateHost(host)
|
|
|
|
}
|
|
|
|
|
|
|
|
function sanitizeXMPPHostFromInstanceUrl (_options: RegisterServerOptions, s: any): false | string {
|
|
|
|
try {
|
|
|
|
if (typeof s !== 'string') { return false }
|
|
|
|
const url = new URL(s)
|
|
|
|
return url.hostname
|
|
|
|
} catch (_err) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-20 16:28:08 +00:00
|
|
|
export {
|
2023-05-24 14:09:55 +00:00
|
|
|
sanitizePeertubeLiveChatInfos,
|
|
|
|
sanitizePeertubeLiveChatServerInfos,
|
|
|
|
sanitizeXMPPHost,
|
|
|
|
sanitizeXMPPHostFromInstanceUrl
|
2023-04-20 16:28:08 +00:00
|
|
|
}
|