:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
import {
MakeLogicType,
actions,
afterMount,
connect,
kea,
key,
listeners,
path,
props,
reducers,
selectors,
sharedListeners,
} from 'kea'
import type { BreakPointFunction } from 'kea'
import { forms } from 'kea-forms'
import type { DeepPartial, DeepPartialMap, FieldName, ValidationErrorType } from 'kea-forms'
import { loaders } from 'kea-loaders'
import { encodeParams, urlToAction } from 'kea-router'
import { subscriptions } from 'kea-subscriptions'
import api from 'lib/api'
import {
checkUrlIsAuthorized,
sanitizePossibleWildCardedURL,
} from 'lib/components/AuthorizedUrlList/authorizedUrlMatching'
import { SetupTaskId, globalSetupLogic } from 'lib/components/ProductSetup'
import { copyToClipboard } from 'lib/utils/copyToClipboard'
import { hogql } from 'lib/utils/hogql'
import { addProductIntent } from 'lib/utils/product-intents'
import { isDomain, isURL } from 'lib/utils/url'
import { sceneLogic } from 'scenes/sceneLogic'
import { teamLogic } from 'scenes/teamLogic'
import { urls } from 'scenes/urls'
import { userLogic } from 'scenes/userLogic'
import { ProductIntentContext, ProductKey } from '~/queries/schema/schema-general'
import { ExperimentIdType, ToolbarParams, ToolbarUserIntent } from '~/types'
import type { TeamPublicType, TeamType, UserType } from '../../../types'
export interface ProposeNewUrlFormType {
url: string
}
export enum AuthorizedUrlListType {
TOOLBAR_URLS = 'TOOLBAR_URLS',
RECORDING_DOMAINS = 'RECORDING_DOMAINS',
WEB_ANALYTICS = 'WEB_ANALYTICS',
WEB_EXPERIMENTS = 'WEB_EXPERIMENTS',
}
/**
* Checks if the URL has a wildcard (*) in the port position e.g. http://localhost:*
*/
export function hasWildcardInPort(input: unknown): boolean {
if (!input || typeof input !== 'string') {
return false
}
// This regex matches URLs with a wildcard (*) in the port position
const portWildcardRegex = /^(https?:\/\/[^:/]+):\*(.*)$/
return portWildcardRegex.test(input.trim())
}
/**
* The add URL form starts with `https://` already in the box, so a pasted full URL leaves two
* protocols behind. Drop the leading one, because the protocol the user supplied is the real one.
*/
export function stripDuplicateProtocol(url: string): string {
return url.replace(/^(https?:\/\/)+(?=[a-z][a-z0-9+.-]*:\/\/)/i, '')
}
export const validateProposedUrl = (
proposedUrl: string,
currentUrls: string[],
onlyAllowDomains: boolean = false,
allowWildCards: boolean = true
): string | undefined => {
if (!isURL(proposedUrl)) {
return 'Please enter a valid URL'
}
if (hasWildcardInPort(proposedUrl)) {
return 'Wildcards are not allowed in the port position'
}
if (onlyAllowDomains && !isDomain(sanitizePossibleWildCardedURL(proposedUrl))) {
return "Please enter a valid domain (URLs with a path aren't allowed)"
}
const hasWildCard = proposedUrl.indexOf('*') > -1
if (hasWildCard && allowWildCards === false) {
return 'Wildcards are not allowed'
}
if (
hasWildCard &&
!/^https?:\/\/((\*\.)?localhost|localhost)(:\d+)?$/.test(proposedUrl) && // Allow http://*.localhost and localhost with ports
!proposedUrl.match(/^(.*)\*[^*]*\.[^*]+\.[^*]+$/)
) {
return 'Wildcards can only be used for subdomains'
}
if (currentUrls.indexOf(proposedUrl) > -1) {
return `This ${onlyAllowDomains ? 'domains' : 'URL'} already is registered`
}
return
}
interface BuildToolbarParamsOptions {
actionId?: number | null
experimentId?: ExperimentIdType
productTourId?: string | null
userIntent?: ToolbarUserIntent
toolbarFlagsKey?: string
}
const _buildToolbarUserIntent = (options?: BuildToolbarParamsOptions): ToolbarUserIntent => {
if (options?.userIntent) {
return options.userIntent
}
if (options?.actionId) {
return 'edit-action'
}
if (options?.experimentId) {
return 'edit-experiment'
}
if (options?.productTourId) {
if (options.productTourId !== 'new') {
return 'edit-product-tour'
}
return 'add-product-tour'
}
return 'add-action'
}
function buildToolbarParams(options?: BuildToolbarParamsOptions): ToolbarParams {
return {
userIntent: _buildToolbarUserIntent(options),
uiHost: window.location.origin,
...(options?.actionId ? { actionId: options.actionId } : {}),
...(options?.experimentId ? { experimentId: options.experimentId } : {}),
...(options?.productTourId && options.productTourId !== 'new' ? { productTourId: options.productTourId } : {}),
...(options?.toolbarFlagsKey ? { toolbarFlagsKey: options.toolbarFlagsKey } : {}),
}
}
/** defaultIntent: whether to launch with empty intent (i.e. toolbar mode is default) */
export function appEditorUrl(
appUrl: string,
options?: {
actionId?: number | null
experimentId?: ExperimentIdType
productTourId?: string | null
userIntent?: ToolbarUserIntent
generateOnly?: boolean
toolbarFlagsKey?: string
}
): string {
const params = buildToolbarParams(options) as Record<string, unknown>
// See https://github.com/PostHog/posthog-js/blob/f7119c/src/extensions/toolbar.ts#L52 for where these params
// are passed. `appUrl` is an extra `redirect_to_site` param.
params['appUrl'] = appUrl
params['generateOnly'] = options?.generateOnly
return '/api/user/redirect_to_site/' + encodeParams(params, '?')
}
/**
* Builds a direct toolbar launch URL that navigates to the app with toolbar params in the hash.
* Unlike appEditorUrl which goes through redirect_to_site,
* this constructs the URL client-side so the toolbar uses OAuth for authentication.
*/
export function directToolbarUrl(
appUrl: string,
options?: BuildToolbarParamsOptions & {
token?: string
dataAttributes?: string[]
userEmail?: string
distinctId?: string
}
): string {
const params: Record<string, unknown> = {
action: 'ph_authorize',
token: options?.token,
toolbarVersion: 'toolbar',
instrument: true,
userEmail: options?.userEmail,
distinctId: options?.distinctId,
...buildToolbarParams(options),
dataAttributes: options?.dataAttributes,
}
const state = encodeURIComponent(JSON.stringify(params))
return `${appUrl}#__posthog=${state}`
}
/**
* Schemes allowed to be rendered in the Site preview iframe. That iframe runs with
* `allow-scripts allow-same-origin`, so a `javascript:`/`data:`/`blob:` src would execute in the
* PostHog origin. Only ever frame an http(s) URL the team has explicitly authorized.
*/
const FRAMEABLE_URL_SCHEME = /^https?:\/\//i
export const checkUrlIsSafeToFrame = (url: string, authorizedUrls: string[]): boolean =>
FRAMEABLE_URL_SCHEME.test(url) && checkUrlIsAuthorized(url, authorizedUrls)
export interface SuggestedDomain {
url: string
count: number
}
export const filterNotAuthorizedUrls = (
suggestions: SuggestedDomain[],
authorizedUrls: string[]
): SuggestedDomain[] => {
const suggestedDomains: SuggestedDomain[] = []
suggestions.forEach(({ url, count }) => {
let urlWithoutPath: string
try {
const parsedUrl = sanitizePossibleWildCardedURL(url)
urlWithoutPath = parsedUrl.protocol + '//' + parsedUrl.host
} catch {
// Skip invalid URLs (e.g., "/" or "/billing" paths without a domain)
return
}
// Have we already added this domain?
if (suggestedDomains.some((sd) => sd.url === urlWithoutPath)) {
return
}
if (!checkUrlIsAuthorized(urlWithoutPath, authorizedUrls)) {
suggestedDomains.push({ url: urlWithoutPath, count })
}
})
return suggestedDomains
}
export const NEW_URL = 'https://'
export interface KeyedAppUrl {
url: string
type: 'authorized' | 'suggestion'
originalIndex: number
// how many seen in the last three days
count?: number
}
export interface AuthorizedUrlListLogicProps {
actionId: number | null
experimentId: ExperimentIdType | null
productTourId: string | null
userIntent?: ToolbarUserIntent
type: AuthorizedUrlListType
allowWildCards?: boolean
}
export const defaultAuthorizedUrlProperties = {
actionId: null,
experimentId: null,
productTourId: null,
userIntent: undefined,
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface authorizedUrlListLogicValues {
currentTeam: TeamPublicType | TeamType | null // teamLogic
currentTeamId: number | null // teamLogic
user: UserType | null // userLogic
authorizedUrls: string[]
checkUrlIsAuthorized: (url: string) => boolean
checkUrlIsSafeToFrame: (url: string) => boolean
editUrlIndex: number | null
isAddUrlFormVisible: boolean
isProposedUrlSubmitting: boolean
isProposedUrlValid: boolean
launchUrl: (url: string) => string
onlyAllowDomains: boolean
proposedUrl: ProposeNewUrlFormType
proposedUrlAllErrors: Record<string, any>
proposedUrlChanged: boolean
proposedUrlErrors: DeepPartialMap<ProposeNewUrlFormType, ValidationErrorType>
proposedUrlHasErrors: boolean
proposedUrlManualErrors: Record<string, any>
proposedUrlTouched: boolean
proposedUrlTouches: Record<string, boolean>
proposedUrlValidationErrors: DeepPartialMap<ProposeNewUrlFormType, ValidationErrorType>
showProposedURLForm: boolean
showProposedUrlErrors: boolean
suggestions: any[]
suggestionsLoading: boolean
urlToEdit: string
urlsKeyed: KeyedAppUrl[]
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface authorizedUrlListLogicActions {
updateCurrentTeam: (payload: Partial<TeamType>) => Partial<TeamType> // teamLogic
addUrl: (
url: string,
launch?: boolean
) => {
launch: boolean | undefined
url: string
}
cancelProposingUrl: () => {
value: true
}
copyLaunchCode: () => {
value: true
}
launchAtUrl: (url: string) => {
url: string
}
loadSuggestions: (_: void) => void
loadSuggestionsFailure: (
error: string,
errorObject?: any
) => {
error: string
errorObject?: any
}
loadSuggestionsSuccess: (
suggestions: SuggestedDomain[],
payload?: void
) => {
suggestions: SuggestedDomain[]
payload?: void
}
newUrl: () => {
value: true
}
removeUrl: (index: number) => {
index: number
}
resetProposedUrl: (values?: ProposeNewUrlFormType) => {
values?: ProposeNewUrlFormType
}
setAuthorizedUrls: (authorizedUrls: string[]) => {
authorizedUrls: string[]
}
setEditUrlIndex: (originalIndex: number | null) => {
originalIndex: number | null
}
setProposedUrlManualErrors: (errors: Record<string, any>) => {
errors: Record<string, any>
}
setProposedUrlValue: (
key: FieldName,
value: any
) => {
name: FieldName
value: any
}
setProposedUrlValues: (values: DeepPartial<ProposeNewUrlFormType>) => {
values: DeepPartial<ProposeNewUrlFormType>
}
submitProposedUrl: () => {
value: boolean
}
submitProposedUrlFailure: (
error: Error,
errors: Record<string, any>
) => {
error: Error
errors: Record<string, any>
}
submitProposedUrlRequest: (proposedUrl: ProposeNewUrlFormType) => {
proposedUrl: ProposeNewUrlFormType
}
submitProposedUrlSuccess: (proposedUrl: ProposeNewUrlFormType) => {
proposedUrl: ProposeNewUrlFormType
}
touchProposedUrlField: (key: string) => {
key: string
}
updateUrl: (
index: number,
url: string
) => {
index: number
url: string
}
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface authorizedUrlListLogicMeta {
key: string
sharedListeners: {
saveUrls: (
payload: any,
breakpoint: BreakPointFunction,
action: {
type: string
payload: any
},
previousState: any
) => void | Promise<void>
}
__keaTypeGenInternalSelectorTypes: {
urlToEdit: (authorizedUrls: string[], editUrlIndex: number | null) => string
urlsKeyed: (authorizedUrls: string[], suggestions: any[]) => KeyedAppUrl[]
launchUrl: (
currentTeam: TeamPublicType | TeamType | null,
user: UserType | null,
actionId: number | null,
experimentId: ExperimentIdType | null,
productTourId: string | null,
arg: ToolbarUserIntent | undefined
) => (url: string) => string
isAddUrlFormVisible: (editUrlIndex: number | null) => boolean
onlyAllowDomains: (type: AuthorizedUrlListType) => boolean
checkUrlIsAuthorized: (authorizedUrls: string[]) => (url: string) => boolean
checkUrlIsSafeToFrame: (authorizedUrls: string[]) => (url: string) => boolean
}
}
export type authorizedUrlListLogicType = MakeLogicType<
authorizedUrlListLogicValues,
authorizedUrlListLogicActions,
AuthorizedUrlListLogicProps,
authorizedUrlListLogicMeta
>
export const authorizedUrlListLogic = kea<authorizedUrlListLogicType>([
path((key) => ['lib', 'components', 'AuthorizedUrlList', 'authorizedUrlListLogic', key]),
key((props) => `${props.type}-${props.experimentId}-${props.actionId}-${props.productTourId}`), // Some will be undefined but that's ok, this avoids experiment/action with same ID sharing same store
props({ ...defaultAuthorizedUrlProperties } as AuthorizedUrlListLogicProps),
connect(() => ({
values: [teamLogic, ['currentTeam', 'currentTeamId'], userLogic, ['user']],
actions: [teamLogic, ['updateCurrentTeam']],
})),
actions(() => ({
setAuthorizedUrls: (authorizedUrls: string[]) => ({ authorizedUrls }),
addUrl: (url: string, launch?: boolean) => ({ url, launch }),
newUrl: true,
removeUrl: (index: number) => ({ index }),
updateUrl: (index: number, url: string) => ({ index, url }),
launchAtUrl: (url: string) => ({ url }),
setEditUrlIndex: (originalIndex: number | null) => ({ originalIndex }),
cancelProposingUrl: true,
copyLaunchCode: true,
})),
loaders(({ values }) => ({
suggestions: {
__default: [] as SuggestedDomain[],
loadSuggestions: async (_: void, breakpoint) => {
const query = hogql`
select properties.$current_url, count()
from events
where event = '$pageview'
and timestamp >= now() - interval 3 day
and timestamp <= now()
and properties.$current_url is not null
group by properties.$current_url
order by count() desc
limit 25`
const currentScene = sceneLogic.findMounted()?.values.activeSceneId ?? 'Settings'
let response: Awaited<ReturnType<typeof api.queryHogQL>>
try {
response = await api.queryHogQL(query, {
scene: currentScene,
productKey: 'platform_and_support',
})
} catch {
// Suggestions are advisory. A failed query (server error or network error) leaves the
// list empty and the manual "Fetch suggestions" retry available, so we swallow it here
// instead of letting it surface as an unhandled loader error.
return []
}
breakpoint()
const result = response.results as [string, number][]
if (!result || result.length === 0) {
return []
}
const suggestedDomains = filterNotAuthorizedUrls(
result.map(([url, count]) => ({ url, count })),
values.authorizedUrls
)
return suggestedDomains.slice(0, 20)
},
},
})),
subscriptions(({ props, actions }) => ({
currentTeam: (currentTeam) => {
const urls =
(props.type === AuthorizedUrlListType.RECORDING_DOMAINS
? currentTeam.recording_domains
: currentTeam.app_urls) || []
actions.setAuthorizedUrls(urls.filter(Boolean))
},
})),
afterMount(({ actions }) => {
actions.loadSuggestions()
}),
forms(({ values, actions, props }) => ({
proposedUrl: {
defaults: { url: '' } as ProposeNewUrlFormType,
errors: ({ url }) => ({
// default to allowing wildcards because that was the original behavior
url: validateProposedUrl(
url,
values.authorizedUrls.filter((_, index) => index !== values.editUrlIndex),
values.onlyAllowDomains,
props.allowWildCards ?? true
),
}),
submit: async ({ url }) => {
if (values.editUrlIndex !== null && values.editUrlIndex >= 0) {
actions.updateUrl(values.editUrlIndex, url)
} else {
actions.addUrl(url)
}
},
},
})),
reducers(() => ({
showProposedURLForm: [
false as boolean,
{
newUrl: () => true,
submitProposedUrlSuccess: () => false,
cancelProposingUrl: () => false,
},
],
authorizedUrls: [
[] as string[],
{
setAuthorizedUrls: (_, { authorizedUrls }) => authorizedUrls,
addUrl: (state, { url }) => (url && !state.includes(url) ? state.concat([url]) : state),
updateUrl: (state, { index, url }) => (url ? Object.assign([...state], { [index]: url }) : state),
removeUrl: (state, { index }) => {
const newUrls = [...state]
newUrls.splice(index, 1)
return newUrls
},
},
],
suggestions: [
[],
{
addUrl: (state, { url }) => [...state].filter((sd) => url !== sd.url),
},
],
editUrlIndex: [
null as number | null,
{
setEditUrlIndex: (_, { originalIndex }) => originalIndex,
removeUrl: (editUrlIndex, { index }) =>
editUrlIndex && index < editUrlIndex
? editUrlIndex - 1
: index === editUrlIndex
? null
: editUrlIndex,
newUrl: () => -1,
updateUrl: () => null,
addUrl: () => null,
cancelProposingUrl: () => null,
},
],
})),
sharedListeners(({ values, props }) => ({
saveUrls: async () => {
if (props.type === AuthorizedUrlListType.RECORDING_DOMAINS) {
await teamLogic.asyncActions.updateCurrentTeam({ recording_domains: values.authorizedUrls })
} else {
await teamLogic.asyncActions.updateCurrentTeam({ app_urls: values.authorizedUrls })
}
},
})),
listeners(({ sharedListeners, values, actions, props }) => ({
setEditUrlIndex: () => {
actions.setProposedUrlValue('url', values.urlToEdit)
},
newUrl: () => {
actions.setProposedUrlValue('url', NEW_URL)
},
setProposedUrlValue: ({ value }) => {
if (typeof value !== 'string') {
return
}
const stripped = stripDuplicateProtocol(value)
if (stripped !== value) {
actions.setProposedUrlValue('url', stripped)
}
},
addUrl: async ({ url, launch }) => {
// Await the app_urls PATCH before markTaskAsCompleted to avoid a race on the team PATCH response.
if (props.type === AuthorizedUrlListType.RECORDING_DOMAINS) {
await teamLogic.asyncActions.updateCurrentTeam({ recording_domains: values.authorizedUrls })
} else {
await teamLogic.asyncActions.updateCurrentTeam({ app_urls: values.authorizedUrls })
}
if (launch) {
actions.launchAtUrl(url)
}
globalSetupLogic.findMounted()?.actions.markTaskAsCompleted(SetupTaskId.AddAuthorizedDomain)
},
removeUrl: sharedListeners.saveUrls,
updateUrl: sharedListeners.saveUrls,
launchAtUrl: ({ url }) => {
void addProductIntent({
product_type: ProductKey.TOOLBAR,
intent_context: ProductIntentContext.TOOLBAR_LAUNCHED,
})
window.location.href = values.launchUrl(url)
},
cancelProposingUrl: () => {
actions.resetProposedUrl()
},
submitProposedUrlSuccess: () => {
actions.setEditUrlIndex(null)
actions.resetProposedUrl()
},
copyLaunchCode: async () => {
const params: Record<string, unknown> = {
action: 'ph_authorize',
token: values.currentTeam?.api_token,
toolbarVersion: 'toolbar',
instrument: true,
userEmail: values.user?.email,
distinctId: values.user?.distinct_id,
...buildToolbarParams({
...(props.actionId ? { actionId: props.actionId } : {}),
...(props.experimentId ? { experimentId: props.experimentId } : {}),
}),
dataAttributes: values.currentTeam?.data_attributes,
}
const templateScript = `
if (!window?.posthog) {
console.warn('PostHog must be added to the window object on this page, for this to work. This is normally done in the loaded callback of your posthog init code.')
} else {
window.posthog.loadToolbar(${JSON.stringify(params)})
}
`
await copyToClipboard(templateScript, 'code to paste into the console')
},
})),
selectors({
urlToEdit: [
(s) => [s.authorizedUrls, s.editUrlIndex],
(authorizedUrls: string[], editUrlIndex: number | null) => {
if (editUrlIndex === null || editUrlIndex === -1) {
return NEW_URL
}
return authorizedUrls[editUrlIndex]
},
],
urlsKeyed: [
(s) => [s.authorizedUrls, s.suggestions],
(authorizedUrls: string[], suggestions: any[]): KeyedAppUrl[] => {
const keyedUrls = authorizedUrls
.map((url, index) => ({
url,
type: 'authorized',
originalIndex: index,
}))
.concat(
suggestions.map(({ url, count }, index) => ({
url,
type: 'suggestion',
originalIndex: index,
count,
}))
) as KeyedAppUrl[]
return keyedUrls
},
],
launchUrl: [
(s, p) => [
s.currentTeam,
s.user,
p.actionId,
p.experimentId,
p.productTourId,
p.userIntent ?? (() => undefined),
],
(
currentTeam: null | import('~/types').TeamPublicType | import('~/types').TeamType,
user: null | import('~/types').UserType,
actionId: number | null,
experimentId: ExperimentIdType | null,
productTourId: string | null,
userIntent: ToolbarUserIntent | undefined
) =>
(url: string) => {
const commonOptions = {
token: currentTeam?.api_token,
dataAttributes: currentTeam?.data_attributes,
userEmail: user?.email,
distinctId: user?.distinct_id,
}
if (experimentId) {
return directToolbarUrl(url, { ...commonOptions, experimentId })
}
if (productTourId) {
return directToolbarUrl(url, { ...commonOptions, productTourId, userIntent })
}
return directToolbarUrl(url, { ...commonOptions, actionId })
},
],
isAddUrlFormVisible: [(s) => [s.editUrlIndex], (editUrlIndex: number | null) => editUrlIndex === -1],
onlyAllowDomains: [
(_, p) => [p.type],
(type: AuthorizedUrlListType) => type === AuthorizedUrlListType.RECORDING_DOMAINS,
],
checkUrlIsAuthorized: [
(s) => [s.authorizedUrls],
(authorizedUrls: string[]) => (url: string) => {
return checkUrlIsAuthorized(url, authorizedUrls)
},
],
checkUrlIsSafeToFrame: [
(s) => [s.authorizedUrls],
(authorizedUrls: string[]) => (url: string) => {
return checkUrlIsSafeToFrame(url, authorizedUrls)
},
],
}),
urlToAction(({ actions }) => ({
[urls.toolbarLaunch()]: (_, searchParams) => {
if (searchParams.addNew) {
actions.newUrl()
}
},
})),
])