Browse Source

Restore the safe URL check for content preview links (#1389)

Co-authored-by: claude[bot] <claude[bot]@users.noreply.github.com>
pull/1390/head
github-actions[bot] 4 days ago
committed by GitHub
parent
commit
1c8a367541
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 57
      frontend/src/app/features/content/shared/preview-button.component.spec.ts
  2. 5
      frontend/src/app/features/content/shared/preview-button.component.ts
  3. 5
      frontend/src/app/shared/state/_test-helpers.ts

57
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<typeof vi.spyOn>;
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,<script>alert(1)</script>'].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;
}

5
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');
}

5
frontend/src/app/shared/state/_test-helpers.ts

@ -46,10 +46,11 @@ type SchemaValues = {
fieldsInLists?: string[];
fieldsInReferences?: string[];
fieldRules?: FieldRuleDto[];
previewUrls?: Record<string, string>;
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',

Loading…
Cancel
Save