Be sure to validate if actor is allowed to be live on certain domain
This commit is contained in:
@@ -3,6 +3,7 @@ import {
|
|||||||
type $Typed,
|
type $Typed,
|
||||||
type AppBskyActorDefs,
|
type AppBskyActorDefs,
|
||||||
AppBskyEmbedExternal,
|
AppBskyEmbedExternal,
|
||||||
|
AtUri,
|
||||||
} from '@atproto/api'
|
} from '@atproto/api'
|
||||||
import {isAfter, parseISO} from 'date-fns'
|
import {isAfter, parseISO} from 'date-fns'
|
||||||
|
|
||||||
@@ -73,10 +74,15 @@ export function isStatusValidForViewers(
|
|||||||
config: LiveNowConfig,
|
config: LiveNowConfig,
|
||||||
) {
|
) {
|
||||||
if (status.status !== 'app.bsky.actor.status#live') return false
|
if (status.status !== 'app.bsky.actor.status#live') return false
|
||||||
|
if (!status.uri) return false // should not happen, just backwards compat
|
||||||
try {
|
try {
|
||||||
|
const {host: liveDid} = new AtUri(status.uri)
|
||||||
if (AppBskyEmbedExternal.isView(status.embed)) {
|
if (AppBskyEmbedExternal.isView(status.embed)) {
|
||||||
const url = new URL(status.embed.external.uri)
|
const url = new URL(status.embed.external.uri)
|
||||||
return config.allSupportedDomains.has(url.hostname)
|
const exception = config.allowedHostsExceptionsByDid.get(liveDid)
|
||||||
|
const isValidException = exception ? exception.has(url.hostname) : false
|
||||||
|
const isValidForAnyone = config.defaultAllowedHosts.has(url.hostname)
|
||||||
|
return isValidException || isValidForAnyone
|
||||||
} else {
|
} else {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -90,27 +90,34 @@ const DEFAULT_LIVE_ALLOWED_DOMAINS = [
|
|||||||
]
|
]
|
||||||
export type LiveNowConfig = {
|
export type LiveNowConfig = {
|
||||||
allowedDomains: Set<string>
|
allowedDomains: Set<string>
|
||||||
allSupportedDomains: Set<string>
|
defaultAllowedHosts: Set<string>
|
||||||
|
allowedHostsExceptionsByDid: Map<string, Set<string>>
|
||||||
}
|
}
|
||||||
export function useLiveNowConfig(): LiveNowConfig {
|
export function useLiveNowConfig(): LiveNowConfig {
|
||||||
const ctx = useContext(LiveNowContext)
|
const ctx = useContext(LiveNowContext)
|
||||||
const canGoLive = useCanGoLive()
|
const canGoLive = useCanGoLive()
|
||||||
const {currentAccount} = useSession()
|
const {currentAccount} = useSession()
|
||||||
const allVipDomains = new Set(ctx.flatMap(live => live.domains))
|
const defaultAllowedHosts = new Set(DEFAULT_LIVE_ALLOWED_DOMAINS)
|
||||||
const allSupportedDomains = new Set(
|
const allowedHostsExceptionsByDid = new Map<string, Set<string>>()
|
||||||
DEFAULT_LIVE_ALLOWED_DOMAINS.concat(Array.from(allVipDomains)),
|
for (const live of ctx) {
|
||||||
|
allowedHostsExceptionsByDid.set(
|
||||||
|
live.did,
|
||||||
|
new Set(DEFAULT_LIVE_ALLOWED_DOMAINS.concat(live.domains)),
|
||||||
)
|
)
|
||||||
|
}
|
||||||
if (!currentAccount?.did || !canGoLive)
|
if (!currentAccount?.did || !canGoLive)
|
||||||
return {
|
return {
|
||||||
allowedDomains: new Set(),
|
allowedDomains: new Set(),
|
||||||
allSupportedDomains,
|
defaultAllowedHosts,
|
||||||
|
allowedHostsExceptionsByDid,
|
||||||
}
|
}
|
||||||
const vip = ctx.find(live => live.did === currentAccount.did)
|
const vip = ctx.find(live => live.did === currentAccount.did)
|
||||||
return {
|
return {
|
||||||
allowedDomains: new Set(
|
allowedDomains: new Set(
|
||||||
DEFAULT_LIVE_ALLOWED_DOMAINS.concat(vip ? vip.domains : []),
|
DEFAULT_LIVE_ALLOWED_DOMAINS.concat(vip ? vip.domains : []),
|
||||||
),
|
),
|
||||||
allSupportedDomains,
|
defaultAllowedHosts,
|
||||||
|
allowedHostsExceptionsByDid,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user