The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
Preserve hash and query in OAuth connect redirect locations (#24959)
Félix Malfaitc8fe1172d5a6
diff --git a/packages/twenty-server/src/engine/core-modules/application/connection-provider/connection-provider-oauth.controller.ts b/packages/twenty-server/src/engine/core-modules/application/connection-provider/connection-provider-oauth.controller.tsindex c5ffd47102e..8ecb097bcee 100644--- a/packages/twenty-server/src/engine/core-modules/application/connection-provider/connection-provider-oauth.controller.ts+++ b/packages/twenty-server/src/engine/core-modules/application/connection-provider/connection-provider-oauth.controller.ts@@ -15,6 +15,7 @@ import {AuthExceptionCode,} from 'src/engine/core-modules/auth/auth.exception';import { TransientTokenService } from 'src/engine/core-modules/auth/token/services/transient-token.service';+import { parseRelativeUrl } from 'src/engine/core-modules/domain/domain-server-config/utils/parse-relative-url.util';import { WorkspaceDomainsService } from 'src/engine/core-modules/domain/workspace-domains/services/workspace-domains.service';import { GuardRedirectService } from 'src/engine/core-modules/guard-redirect/services/guard-redirect.service';import { TwentyConfigService } from 'src/engine/core-modules/twenty-config/twenty-config.service';@@ -203,13 +204,16 @@ export class ConnectionProviderOAuthController {);}- const pathname =+ const { pathname, searchParams, hash } = parseRelativeUrl(redirectLocation ||- getSettingsPath(SettingsPath.ApplicationDetail, { applicationId });+ getSettingsPath(SettingsPath.ApplicationDetail, { applicationId }),+ );const url = this.workspaceDomainsService.buildWorkspaceURL({workspace,pathname,+ searchParams,+ hash,});// Frontend tab list reads the URL hash to pick the active tab.diff --git a/packages/twenty-server/src/engine/core-modules/auth/controllers/google-apis-auth.controller.ts b/packages/twenty-server/src/engine/core-modules/auth/controllers/google-apis-auth.controller.tsindex 5f34fa1d8f3..5e472aff9a3 100644--- a/packages/twenty-server/src/engine/core-modules/auth/controllers/google-apis-auth.controller.ts+++ b/packages/twenty-server/src/engine/core-modules/auth/controllers/google-apis-auth.controller.ts@@ -23,6 +23,7 @@ import { GoogleAPIsOauthRequestCodeGuard } from 'src/engine/core-modules/auth/guimport { GoogleAPIsService } from 'src/engine/core-modules/auth/services/google-apis.service';import { TransientTokenService } from 'src/engine/core-modules/auth/token/services/transient-token.service';import { APIsOAuthRequest } from 'src/engine/core-modules/auth/types/apis-oauth-request.type';+import { parseRelativeUrl } from 'src/engine/core-modules/domain/domain-server-config/utils/parse-relative-url.util';import { WorkspaceDomainsService } from 'src/engine/core-modules/domain/workspace-domains/services/workspace-domains.service';import { GuardRedirectService } from 'src/engine/core-modules/guard-redirect/services/guard-redirect.service';import { OnboardingService } from 'src/engine/core-modules/onboarding/onboarding.service';@@ -125,15 +126,18 @@ export class GoogleAPIsAuthController {);}- const pathname =+ const { pathname, searchParams, hash } = parseRelativeUrl(redirectLocation ||- getSettingsPath(SettingsPath.AccountsConfiguration, {- connectedAccountId,- });+ getSettingsPath(SettingsPath.AccountsConfiguration, {+ connectedAccountId,+ }),+ );const url = this.workspaceDomainsService.buildWorkspaceURL({workspace,pathname,+ searchParams,+ hash,});return res.redirect(url.toString());diff --git a/packages/twenty-server/src/engine/core-modules/auth/controllers/microsoft-apis-auth.controller.ts b/packages/twenty-server/src/engine/core-modules/auth/controllers/microsoft-apis-auth.controller.tsindex 1adc17d9e3b..ac2aa07bbf8 100644--- a/packages/twenty-server/src/engine/core-modules/auth/controllers/microsoft-apis-auth.controller.ts+++ b/packages/twenty-server/src/engine/core-modules/auth/controllers/microsoft-apis-auth.controller.ts@@ -23,6 +23,7 @@ import { MicrosoftAPIsOauthRequestCodeGuard } from 'src/engine/core-modules/authimport { MicrosoftAPIsService } from 'src/engine/core-modules/auth/services/microsoft-apis.service';import { TransientTokenService } from 'src/engine/core-modules/auth/token/services/transient-token.service';import { APIsOAuthRequest } from 'src/engine/core-modules/auth/types/apis-oauth-request.type';+import { parseRelativeUrl } from 'src/engine/core-modules/domain/domain-server-config/utils/parse-relative-url.util';import { WorkspaceDomainsService } from 'src/engine/core-modules/domain/workspace-domains/services/workspace-domains.service';import { GuardRedirectService } from 'src/engine/core-modules/guard-redirect/services/guard-redirect.service';import { OnboardingService } from 'src/engine/core-modules/onboarding/onboarding.service';@@ -132,15 +133,18 @@ export class MicrosoftAPIsAuthController {);}- const pathname =+ const { pathname, searchParams, hash } = parseRelativeUrl(redirectLocation ||- getSettingsPath(SettingsPath.AccountsConfiguration, {- connectedAccountId,- });+ getSettingsPath(SettingsPath.AccountsConfiguration, {+ connectedAccountId,+ }),+ );const url = this.workspaceDomainsService.buildWorkspaceURL({workspace,pathname,+ searchParams,+ hash,});return res.redirect(url.toString());diff --git a/packages/twenty-server/src/engine/core-modules/domain/domain-server-config/utils/__tests__/parse-relative-url.util.spec.ts b/packages/twenty-server/src/engine/core-modules/domain/domain-server-config/utils/__tests__/parse-relative-url.util.spec.tsnew file mode 100644index 00000000000..9c5d23128b1--- /dev/null+++ b/packages/twenty-server/src/engine/core-modules/domain/domain-server-config/utils/__tests__/parse-relative-url.util.spec.ts@@ -0,0 +1,63 @@+import { buildUrlWithPathnameAndSearchParams } from 'src/engine/core-modules/domain/domain-server-config/utils/build-url-with-pathname-and-search-params.util';+import { parseRelativeUrl } from 'src/engine/core-modules/domain/domain-server-config/utils/parse-relative-url.util';++describe('parseRelativeUrl', () => {+ it('parses a plain pathname', () => {+ expect(parseRelativeUrl('/settings/accounts')).toEqual({+ pathname: '/settings/accounts',+ searchParams: {},+ hash: '',+ });+ });++ it('splits a tab anchor out of the pathname', () => {+ expect(+ parseRelativeUrl(+ '/object/company/46d9f704-ee56-4063-835b-ffb3aea3c544#630fec80-dc2d-400b-b760-f8d9b7402cac',+ ),+ ).toEqual({+ pathname: '/object/company/46d9f704-ee56-4063-835b-ffb3aea3c544',+ searchParams: {},+ hash: '#630fec80-dc2d-400b-b760-f8d9b7402cac',+ });+ });++ it('splits a query string and a hash out of the pathname', () => {+ expect(+ parseRelativeUrl('/objects/companies?viewId=view-1&page=2#tab-1'),+ ).toEqual({+ pathname: '/objects/companies',+ searchParams: { viewId: 'view-1', page: '2' },+ hash: '#tab-1',+ });+ });++ it('keeps only the path, query and hash of an absolute url', () => {+ expect(parseRelativeUrl('https://evil.com/phishing?a=1#target')).toEqual({+ pathname: '/phishing',+ searchParams: { a: '1' },+ hash: '#target',+ });+ });++ it('keeps only the path of a protocol-relative url', () => {+ expect(parseRelativeUrl('//evil.com/phishing')).toEqual({+ pathname: '/phishing',+ searchParams: {},+ hash: '',+ });+ });++ it('rebuilds into a url without percent-encoding the separators', () => {+ const url = buildUrlWithPathnameAndSearchParams({+ baseUrl: new URL('https://acme.twenty.com'),+ ...parseRelativeUrl(+ '/object/company/46d9f704-ee56-4063-835b-ffb3aea3c544#630fec80-dc2d-400b-b760-f8d9b7402cac',+ ),+ });++ expect(url.toString()).toBe(+ 'https://acme.twenty.com/object/company/46d9f704-ee56-4063-835b-ffb3aea3c544#630fec80-dc2d-400b-b760-f8d9b7402cac',+ );+ });+});diff --git a/packages/twenty-server/src/engine/core-modules/domain/domain-server-config/utils/parse-relative-url.util.ts b/packages/twenty-server/src/engine/core-modules/domain/domain-server-config/utils/parse-relative-url.util.tsnew file mode 100644index 00000000000..a4625aadb62--- /dev/null+++ b/packages/twenty-server/src/engine/core-modules/domain/domain-server-config/utils/parse-relative-url.util.ts@@ -0,0 +1,17 @@+// A redirect location coming from the front can carry a query string and a+// tab anchor (e.g. /object/company/<recordId>#<tabId>). Assigning the whole+// string to URL.pathname percent-encodes '?' and '#' into the path, so split+// it into parts the URL API can reassemble. Parsing against a fixed base also+// drops any host smuggled into the value, keeping redirects on our domain.+export const parseRelativeUrl = (relativeUrl: string) => {+ const { pathname, searchParams, hash } = new URL(+ relativeUrl,+ 'http://relative-url.invalid',+ );++ return {+ pathname,+ searchParams: Object.fromEntries(searchParams.entries()),+ hash,+ };+};