diff --git a/frontend/src/app/features/content/shared/preview-button.component.spec.ts b/frontend/src/app/features/content/shared/preview-button.component.spec.ts new file mode 100644 index 000000000..3b91ac32f --- /dev/null +++ b/frontend/src/app/features/content/shared/preview-button.component.spec.ts @@ -0,0 +1,57 @@ +/* + * Squidex Headless CMS + * + * @license + * Copyright (c) Squidex UG (haftungsbeschränkt). All rights reserved. + */ + +import { TestBed } from '@angular/core/testing'; +import { AuthService } from '@app/shared'; +import { createContent } from '../../../shared/services/contents.service.spec'; +import { TestValues } from '../../../shared/state/_test-helpers'; +import { PreviewButtonComponent } from './preview-button.component'; + +describe('PreviewButtonComponent', () => { + let windowOpen: ReturnType; + + beforeEach(() => { + windowOpen = vi.spyOn(window, 'open').mockReturnValue(null); + }); + + afterEach(() => { + windowOpen.mockRestore(); + }); + + it('should open preview url with interpolated values', () => { + const button = createButton('https://squidex.io/preview/${createdBy}'); + + button['follow']('Web'); + + expect(windowOpen).toHaveBeenCalledWith('https://squidex.io/preview/creator12', '_blank'); + }); + + ['javascript:alert(1)', ' JavaScript:alert(1)', 'data:text/html,'].forEach(url => { + it(`should not open unsafe preview url ${url}`, () => { + const button = createButton(url); + + button['follow']('Web'); + + expect(windowOpen).not.toHaveBeenCalled(); + }); + }); +}); + +function createButton(previewUrl: string) { + TestBed.configureTestingModule({ + providers: [ + { provide: AuthService, useValue: TestValues.authService.object }, + ], + }); + + const fixture = TestBed.createComponent(PreviewButtonComponent); + + fixture.componentRef.setInput('content', createContent(12)); + fixture.componentRef.setInput('schema', TestValues.createSchema({ previewUrls: { Web: previewUrl } })); + + return fixture.componentInstance; +} diff --git a/frontend/src/app/features/content/shared/preview-button.component.ts b/frontend/src/app/features/content/shared/preview-button.component.ts index 5daba8df9..90ca397a2 100644 --- a/frontend/src/app/features/content/shared/preview-button.component.ts +++ b/frontend/src/app/features/content/shared/preview-button.component.ts @@ -9,7 +9,7 @@ import { Component, computed, inject, input, linkedSignal } from '@angular/core'; import { Observable } from 'rxjs'; import { take } from 'rxjs/operators'; -import { AuthService, ContentDto, DropdownMenuComponent, interpolate, LocalStoreService, ModalDirective, ModalModel, ModalPlacementDirective, SchemaDto, Settings, TranslatePipe } from '@app/shared'; +import { AuthService, ContentDto, DropdownMenuComponent, interpolate, isSafeUrl, LocalStoreService, ModalDirective, ModalModel, ModalPlacementDirective, SchemaDto, Settings, TranslatePipe } from '@app/shared'; @Component({ selector: 'sqx-preview-button', @@ -85,6 +85,9 @@ export class PreviewButtonComponent { private navigateTo(name: string) { const urlVars = { ...this.content(), ...this.authService.user || {} }; const urlLink = interpolate(this.schema().previewUrls[name], urlVars, 'iv'); + if (!isSafeUrl(urlLink)) { + return; + } window.open(urlLink, '_blank'); } diff --git a/frontend/src/app/shared/state/_test-helpers.ts b/frontend/src/app/shared/state/_test-helpers.ts index acb47c311..f7b55b489 100644 --- a/frontend/src/app/shared/state/_test-helpers.ts +++ b/frontend/src/app/shared/state/_test-helpers.ts @@ -46,10 +46,11 @@ type SchemaValues = { fieldsInLists?: string[]; fieldsInReferences?: string[]; fieldRules?: FieldRuleDto[]; + previewUrls?: Record; properties?: SchemaPropertiesDto; }; -function createSchema({ name, properties, id, fields, fieldsInLists, fieldsInReferences, fieldRules }: SchemaValues = {}) { +function createSchema({ name, properties, id, fields, fieldsInLists, fieldsInReferences, fieldRules, previewUrls }: SchemaValues = {}) { id = id || 1; @@ -66,7 +67,7 @@ function createSchema({ name, properties, id, fields, fieldsInLists, fieldsInRef lastModified: DateTime.now(), lastModifiedBy: 'me', name: name || `schema-name${id}`, - previewUrls: {}, + previewUrls: previewUrls || {}, properties: properties || new SchemaPropertiesDto(), scripts: new SchemaScriptsDto(), type: 'Default',