diff --git a/core-web/libs/portlets/edit-ema/portlet/src/lib/dot-ema-shell/dot-ema-shell.component.spec.ts b/core-web/libs/portlets/edit-ema/portlet/src/lib/dot-ema-shell/dot-ema-shell.component.spec.ts index 779763800be..595fe258c96 100644 --- a/core-web/libs/portlets/edit-ema/portlet/src/lib/dot-ema-shell/dot-ema-shell.component.spec.ts +++ b/core-web/libs/portlets/edit-ema/portlet/src/lib/dot-ema-shell/dot-ema-shell.component.spec.ts @@ -312,6 +312,75 @@ describe('DotEmaShellComponent', () => { expect(spyStoreLoadPage).toHaveBeenCalledWith(INITIAL_PAGE_PARAMS); }); + describe('Sanitize url when called loadPageAsset', () => { + it('should sanitize when url is index', () => { + const spyloadPageAsset = jest.spyOn(store, 'loadPageAsset'); + const spyLocation = jest.spyOn(location, 'go'); + + const params = { + ...INITIAL_PAGE_PARAMS, + url: '/index' + }; + + overrideRouteSnashot( + activatedRoute, + SNAPSHOT_MOCK({ queryParams: params, data: UVE_CONFIG_MOCK(BASIC_OPTIONS) }) + ); + + spectator.detectChanges(); + expect(spyloadPageAsset).toHaveBeenCalledWith({ ...params, url: 'index' }); + expect(spyLocation).toHaveBeenCalledWith( + '/?language_id=1&url=index&variantName=DEFAULT&com.dotmarketing.persona.id=modes.persona.no.persona&editorMode=edit' + ); + }); + + it('should sanitize when url is nested', () => { + const spyloadPageAsset = jest.spyOn(store, 'loadPageAsset'); + + const spyLocation = jest.spyOn(location, 'go'); + + const params = { + ...INITIAL_PAGE_PARAMS, + url: '/some-url/some-nested-url/' + }; + + overrideRouteSnashot( + activatedRoute, + SNAPSHOT_MOCK({ queryParams: params, data: UVE_CONFIG_MOCK(BASIC_OPTIONS) }) + ); + + spectator.detectChanges(); + expect(spyloadPageAsset).toHaveBeenCalledWith({ + ...params, + url: 'some-url/some-nested-url' + }); + expect(spyLocation).toHaveBeenCalledWith( + '/?language_id=1&url=some-url%2Fsome-nested-url&variantName=DEFAULT&com.dotmarketing.persona.id=modes.persona.no.persona&editorMode=edit' + ); + }); + + it('should sanitize when url is nested and ends in index', () => { + const spyloadPageAsset = jest.spyOn(store, 'loadPageAsset'); + const spyLocation = jest.spyOn(location, 'go'); + + const params = { + ...INITIAL_PAGE_PARAMS, + url: '/some-url/index' + }; + + overrideRouteSnashot( + activatedRoute, + SNAPSHOT_MOCK({ queryParams: params, data: UVE_CONFIG_MOCK(BASIC_OPTIONS) }) + ); + + spectator.detectChanges(); + expect(spyloadPageAsset).toHaveBeenCalledWith({ ...params, url: 'some-url/' }); + expect(spyLocation).toHaveBeenCalledWith( + '/?language_id=1&url=some-url%2F&variantName=DEFAULT&com.dotmarketing.persona.id=modes.persona.no.persona&editorMode=edit' + ); + }); + }); + it('should patch viewParams with empty object when the editorMode is edit', () => { const patchViewParamsSpy = jest.spyOn(store, 'patchViewParams'); const params = { diff --git a/core-web/libs/portlets/edit-ema/portlet/src/lib/dot-ema-shell/dot-ema-shell.component.ts b/core-web/libs/portlets/edit-ema/portlet/src/lib/dot-ema-shell/dot-ema-shell.component.ts index 1cf3dcaa173..d7efc720603 100644 --- a/core-web/libs/portlets/edit-ema/portlet/src/lib/dot-ema-shell/dot-ema-shell.component.ts +++ b/core-web/libs/portlets/edit-ema/portlet/src/lib/dot-ema-shell/dot-ema-shell.component.ts @@ -39,6 +39,7 @@ import { checkClientHostAccess, getAllowedPageParams, getTargetUrl, + sanitizeURL, shouldNavigate } from '../utils'; @@ -209,6 +210,9 @@ export class DotEmaShellComponent implements OnInit { const params = getAllowedPageParams(queryParams); const validHost = checkClientHostAccess(params.clientHost, allowedDevURLs); + //Sanitize the url + params.url = sanitizeURL(params.url); + if (!validHost) { delete params.clientHost; } diff --git a/core-web/libs/portlets/edit-ema/portlet/src/lib/services/guards/edit-ema.guard.spec.ts b/core-web/libs/portlets/edit-ema/portlet/src/lib/services/guards/edit-ema.guard.spec.ts index 5c6848a3d25..06ca4f69d79 100644 --- a/core-web/libs/portlets/edit-ema/portlet/src/lib/services/guards/edit-ema.guard.spec.ts +++ b/core-web/libs/portlets/edit-ema/portlet/src/lib/services/guards/edit-ema.guard.spec.ts @@ -93,28 +93,7 @@ describe('EditEmaGuard', () => { expect(didEnteredPortlet).toBe(true); }); - it('should navigate to "edit-page" and sanitize url', () => { - const route: ActivatedRouteSnapshot = { - firstChild: { - url: [{ path: 'content' }] - }, - queryParams: { url: '/some-url/with-index/index' } - // eslint-disable-next-line @typescript-eslint/no-explicit-any - } as any; - - TestBed.runInInjectionContext(() => editEmaGuard(route, state) as Observable); - - expect(router.navigate).toHaveBeenCalledWith(['/edit-page/content'], { - queryParams: { - 'com.dotmarketing.persona.id': 'modes.persona.no.persona', - language_id: 1, - url: 'some-url/with-index/' - }, - replaceUrl: true - }); - }); - - it('should navigate to "edit-page" and sanitize url when the url is "/"', () => { + it('should navigate to "edit-page" with url as "index" when the initial url queryParam is "/"', () => { const route: ActivatedRouteSnapshot = { firstChild: { url: [{ path: 'content' }] diff --git a/core-web/libs/portlets/edit-ema/portlet/src/lib/services/guards/edit-ema.guard.ts b/core-web/libs/portlets/edit-ema/portlet/src/lib/services/guards/edit-ema.guard.ts index 14821c3be01..b786936878e 100644 --- a/core-web/libs/portlets/edit-ema/portlet/src/lib/services/guards/edit-ema.guard.ts +++ b/core-web/libs/portlets/edit-ema/portlet/src/lib/services/guards/edit-ema.guard.ts @@ -2,7 +2,6 @@ import { inject } from '@angular/core'; import { ActivatedRouteSnapshot, CanActivateFn, Params, Router } from '@angular/router'; import { DEFAULT_PERSONA } from '../../shared/consts'; -import { sanitizeURL } from '../../utils'; type EmaQueryParams = { url: string; @@ -44,19 +43,11 @@ function confirmQueryParams(queryParams: Params): { return acc; } - // Handle URL parameter special cases - if (key === 'url') { - if (queryParams[key] !== 'index' && queryParams[key].endsWith('/index')) { - acc[key] = sanitizeURL(queryParams[key]); - acc.missing = true; - } - - if (queryParams[key] === '/') { - acc[key] = 'index'; - acc.missing = true; + if (key === 'url' && queryParams[key] === '/') { + acc[key] = 'index'; + acc.missing = true; - return acc; - } + return acc; } return acc; diff --git a/core-web/libs/portlets/edit-ema/portlet/src/lib/store/features/load/withLoad.ts b/core-web/libs/portlets/edit-ema/portlet/src/lib/store/features/load/withLoad.ts index 6e4cdaad302..c93a358ff28 100644 --- a/core-web/libs/portlets/edit-ema/portlet/src/lib/store/features/load/withLoad.ts +++ b/core-web/libs/portlets/edit-ema/portlet/src/lib/store/features/load/withLoad.ts @@ -81,6 +81,7 @@ export function withLoad() { return of(pageAsset); } + // Maybe we can use retryWhen() instead of this navigate. const url = vanityUrl.forwardTo.replace('/', ''); router.navigate([], { queryParamsHandling: 'merge',