Skip to content

: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.

fix(phai): handle relative URLs in agent dashboard links (#61672)

Chris Volzer2c4fdf93e623
diff --git a/ee/hogai/llm.py b/ee/hogai/llm.py
index e39d7cb4debd..b5c77567a3dd 100644
--- a/ee/hogai/llm.py
+++ b/ee/hogai/llm.py
@@ -32,10 +32,12 @@ BILLING_SKIPPED_COUNTER = Counter(
PROJECT_ORG_USER_CONTEXT_PROMPT = """
You are currently in project {{{project_name}}}, which is part of the {{{organization_name}}} organization.
The user's name appears to be {{{user_full_name}}} ({{{user_email}}}). Feel free to use their first name when greeting. DO NOT use this name if it appears possibly fake.
-All PostHog app URLs must use relative paths without a domain (no us.posthog.com, eu.posthog.com, app.posthog.com), and omit the `/project/:id/` prefix. Never include `/-/` in URLs.
+All PostHog app URLs must use root-relative paths starting with `/`, without a domain (no us.posthog.com, eu.posthog.com, app.posthog.com), and omit the `/project/:id/` prefix. Never include `/-/` in URLs. Never use relative paths like `../` or `./` — always start with `/`.
Use Markdown with descriptive anchor text, for example "[Cohorts view](/cohorts)".
Key URL patterns:
+- Dashboard: `/dashboard/<id>`, e.g. `/dashboard/12345`
+- Insights: `/insights/<short_id>`, e.g. `/insights/abc123`
- Settings: `/settings/<section-id>` where section IDs use hyphens, e.g. `/settings/organization-members`, `/settings/environment-replay`, `/settings/user-api-keys`
- Data management: `/data-management/events`, `/data-management/properties`
- Billing: `/organization/billing`
diff --git a/frontend/src/lib/utils/router-utils.test.ts b/frontend/src/lib/utils/router-utils.test.ts
index 1892f32fcf1b..b061c54f52b6 100644
--- a/frontend/src/lib/utils/router-utils.test.ts
+++ b/frontend/src/lib/utils/router-utils.test.ts
@@ -14,6 +14,26 @@ describe('router-utils', () => {
expect(altered).toEqual('/project/phc_gE7SWBNBgFbA4eQ154KPXebyB8KyLJuypR8jg1DSo9Z/replay')
})
+ describe('relative path normalization', () => {
+ it('normalizes ../ prefix to absolute path with project id', () => {
+ expect(addProjectIdIfMissing('../dashboard/1663553', 112509)).toEqual('/project/112509/dashboard/1663553')
+ })
+ it('normalizes multiple ../ prefixes', () => {
+ expect(addProjectIdIfMissing('../../dashboard/1663553', 112509)).toEqual(
+ '/project/112509/dashboard/1663553'
+ )
+ })
+ it('normalizes ./ prefix to absolute path with project id', () => {
+ expect(addProjectIdIfMissing('./insights/abc123', 112509)).toEqual('/project/112509/insights/abc123')
+ })
+ it('normalizes repeated ./ prefixes', () => {
+ expect(addProjectIdIfMissing('././dashboard/1663553', 112509)).toEqual('/project/112509/dashboard/1663553')
+ })
+ it('does not alter normal absolute paths', () => {
+ expect(addProjectIdIfMissing('/dashboard/1663553', 112509)).toEqual('/project/112509/dashboard/1663553')
+ })
+ })
+
describe('stripTrailingSlash', () => {
it('strips a single trailing slash', () => {
expect(stripTrailingSlash('/insights/abc/')).toEqual('/insights/abc')
diff --git a/frontend/src/lib/utils/router-utils.ts b/frontend/src/lib/utils/router-utils.ts
index 852bbf221518..3e6284575f79 100644
--- a/frontend/src/lib/utils/router-utils.ts
+++ b/frontend/src/lib/utils/router-utils.ts
@@ -27,11 +27,20 @@ function isPathWithoutProjectId(path: string): boolean {
return pathsWithoutProjectId.includes(firstPart)
}
+function normalizeRelativePath(path: string): string {
+ const normalized = path.replace(/^(\.\.\/|\.\/)+/, '')
+ return normalized.startsWith('/') ? normalized : `/${normalized}`
+}
+
function addProjectIdUnlessPresent(path: string, teamId?: TeamType['id']): string {
if (path.match(projectIdentifierInUrlRegex)) {
return path
}
+ if (path.startsWith('../') || path.startsWith('./')) {
+ path = normalizeRelativePath(path)
+ }
+
let prefix = ''
try {
prefix = `/project/${teamId ?? getCurrentTeamId()}`