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(onboarding): drop the duplicate protocol when a full URL is pasted (#100712)

posthog[bot]e386b30b01a4
diff --git a/frontend/src/lib/components/AuthorizedUrlList/authorizedUrlListLogic.test.ts b/frontend/src/lib/components/AuthorizedUrlList/authorizedUrlListLogic.test.ts
index af172c331b03..0e70a7f3eeb7 100644
--- a/frontend/src/lib/components/AuthorizedUrlList/authorizedUrlListLogic.test.ts
+++ b/frontend/src/lib/components/AuthorizedUrlList/authorizedUrlListLogic.test.ts
@@ -11,6 +11,7 @@ import { initKeaTests } from '~/test/init'
import {
AuthorizedUrlListType,
+ NEW_URL,
SuggestedDomain,
appEditorUrl,
authorizedUrlListLogic,
@@ -119,6 +120,40 @@ describe('the authorized urls list logic', () => {
})
})
+ // The form opens prefilled with `https://`, so a pasted full URL doubles the protocol
+ test.each([
+ ['https://www.example.com', 'https://www.example.com'],
+ ['http://localhost:3000', 'http://localhost:3000'],
+ ])('keeps a single protocol when "%s" is pasted over the prefilled one', async (pasted, expected) => {
+ await expectLogic(logic, () => {
+ logic.actions.newUrl()
+ logic.actions.setProposedUrlValue('url', `${NEW_URL}${pasted}`)
+ }).toFinishAllListeners()
+
+ await expectLogic(logic).toMatchValues({
+ proposedUrl: { url: expected },
+ proposedUrlHasErrors: false,
+ })
+ })
+
+ it('repairs a saved URL that already has two protocols when it is edited', async () => {
+ // An earlier test leaves `api.update` mocked with a promise it never resolves
+ const update = jest.spyOn(api, 'update').mockResolvedValue({})
+
+ await expectLogic(logic, () => {
+ logic.actions.setAuthorizedUrls(['https://https://www.example.com'])
+ logic.actions.setEditUrlIndex(0)
+ }).toFinishAllListeners()
+
+ await expectLogic(logic, () => {
+ logic.actions.submitProposedUrl()
+ }).toFinishAllListeners()
+
+ expect(update).toHaveBeenCalledWith(`api/environments/${MOCK_TEAM_ID}`, {
+ app_urls: ['https://www.example.com'],
+ })
+ })
+
it('allows an unchanged URL when editing', async () => {
await expectLogic(logic, () => {
logic.actions.setAuthorizedUrls(['https://example.com'])
@@ -288,6 +323,10 @@ describe('the authorized urls list logic', () => {
proposedUrl: 'capacitor://localhost',
validityMessage: undefined,
},
+ {
+ proposedUrl: 'https://https://www.example.com',
+ validityMessage: "Please enter a valid domain (URLs with a path aren't allowed)",
+ },
]
testCases.forEach((testCase) => {
diff --git a/frontend/src/lib/components/AuthorizedUrlList/authorizedUrlListLogic.ts b/frontend/src/lib/components/AuthorizedUrlList/authorizedUrlListLogic.ts
index e32272aedebd..d5996bb4f1a0 100644
--- a/frontend/src/lib/components/AuthorizedUrlList/authorizedUrlListLogic.ts
+++ b/frontend/src/lib/components/AuthorizedUrlList/authorizedUrlListLogic.ts
@@ -67,6 +67,14 @@ export function hasWildcardInPort(input: unknown): boolean {
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[],
@@ -625,6 +633,15 @@ export const authorizedUrlListLogic = kea<authorizedUrlListLogicType>([
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) {
diff --git a/frontend/src/lib/utils/url.ts b/frontend/src/lib/utils/url.ts
index d8558f53140c..42beffe7b713 100644
--- a/frontend/src/lib/utils/url.ts
+++ b/frontend/src/lib/utils/url.ts
@@ -137,16 +137,14 @@ export function stripHTTP(url: string): string {
export function isDomain(url: string | URL): boolean {
try {
const parsedUrl = typeof url === 'string' ? new URL(url) : url
- if (parsedUrl.protocol.includes('http') && (!parsedUrl.pathname || parsedUrl.pathname === '/')) {
- return true
- }
- if (!parsedUrl.pathname.replace(/^\/\//, '').includes('/')) {
- return true
- }
+ // Trailing slashes do not make a path, but everything else does, including the `//host`
+ // path that a doubled protocol such as `https://https://example.com` parses into.
+ // The match is start-anchored, because an end-anchored one retries at every slash and
+ // takes quadratic time on a long run of slashes.
+ return parsedUrl.pathname === '' || /^\/+$/.test(parsedUrl.pathname)
} catch {
return false
}
- return false
}
export function isURL(input: any): boolean {