From cbcfaa2c9f1f0106dbf97c85e55b532e045bf21e Mon Sep 17 00:00:00 2001 From: milanmajchrak Date: Wed, 25 Nov 2026 18:43:26 +0100 Subject: [PATCH] CLARIN-DSpace v9/HTML-escape metadata and load the process log without a URL token 9968e2d046 escape submitter metadata before rendering it as HTML 254a002be1 load the process output log without a token in the URL Co-Authored-By: Claude Opus 5.5 --- ...result-list-submission-element.component.ts | 2 +- .../org-unit-input-suggestions.component.html | 2 +- ...rg-unit-input-suggestions.component.spec.ts | 10 ++++++++++ ...result-list-submission-element.component.ts | 2 +- .../person-input-suggestions.component.html | 2 +- .../full/full-item-page.component.html | 2 +- ...in-description-item-field.component.spec.ts | 11 +++++++++++ .../detail/process-detail.component.spec.ts | 3 +++ .../detail/process-detail.component.ts | 12 +++--------- .../grant-deny-request-copy.component.spec.ts | 7 +++++++ .../grant-deny-request-copy.component.ts | 2 +- src/app/shared/clarin-shared-util.spec.ts | 18 ++++++++++++++++++ src/app/shared/clarin-shared-util.ts | 11 ++++++++--- .../ds-dynamic-autocomplete.component.spec.ts | 5 +++++ .../ds-dynamic-autocomplete.component.ts | 3 ++- .../ds-dynamic-autocomplete.service.ts | 7 ++++--- src/app/shared/utils/emphasize.pipe.spec.ts | 13 +++++++++++++ src/app/shared/utils/emphasize.pipe.ts | 9 ++++++--- 18 files changed, 96 insertions(+), 25 deletions(-) create mode 100644 src/app/shared/utils/emphasize.pipe.spec.ts diff --git a/src/app/entity-groups/research-entities/submission/item-list-elements/org-unit/org-unit-search-result-list-submission-element.component.ts b/src/app/entity-groups/research-entities/submission/item-list-elements/org-unit/org-unit-search-result-list-submission-element.component.ts index e30152e11ad..a678b10f503 100644 --- a/src/app/entity-groups/research-entities/submission/item-list-elements/org-unit/org-unit-search-result-list-submission-element.component.ts +++ b/src/app/entity-groups/research-entities/submission/item-list-elements/org-unit/org-unit-search-result-list-submission-element.component.ts @@ -77,7 +77,7 @@ export class OrgUnitSearchResultListSubmissionElementComponent extends SearchRes if (this.useNameVariants) { const defaultValue = this.dso ? this.dsoNameService.getName(this.dso) : undefined; - const alternatives = this.allMetadataValues(this.alternativeField); + const alternatives = this.allMetadataValues(this.alternativeField, false); this.allSuggestions = [defaultValue, ...alternatives]; this.relationshipService.getNameVariant(this.listID, this.dso.uuid) diff --git a/src/app/entity-groups/research-entities/submission/item-list-elements/org-unit/org-unit-suggestions/org-unit-input-suggestions.component.html b/src/app/entity-groups/research-entities/submission/item-list-elements/org-unit/org-unit-suggestions/org-unit-input-suggestions.component.html index 6f7e7de0148..73c45e199f3 100644 --- a/src/app/entity-groups/research-entities/submission/item-list-elements/org-unit/org-unit-suggestions/org-unit-input-suggestions.component.html +++ b/src/app/entity-groups/research-entities/submission/item-list-elements/org-unit/org-unit-suggestions/org-unit-input-suggestions.component.html @@ -21,7 +21,7 @@ @for (suggestionOption of suggestions; track suggestionOption) {
- + {{suggestionOption}}
} diff --git a/src/app/entity-groups/research-entities/submission/item-list-elements/org-unit/org-unit-suggestions/org-unit-input-suggestions.component.spec.ts b/src/app/entity-groups/research-entities/submission/item-list-elements/org-unit/org-unit-suggestions/org-unit-input-suggestions.component.spec.ts index 08087491466..5b15b8d3808 100644 --- a/src/app/entity-groups/research-entities/submission/item-list-elements/org-unit/org-unit-suggestions/org-unit-input-suggestions.component.spec.ts +++ b/src/app/entity-groups/research-entities/submission/item-list-elements/org-unit/org-unit-suggestions/org-unit-input-suggestions.component.spec.ts @@ -70,4 +70,14 @@ describe('OrgUnitInputSuggestionsComponent', () => { }); }); + describe('When a suggestion has HTML markup', () => { + it('should render the markup as text', () => { + component.suggestions = ['Science Unit']; + fixture.detectChanges(); + const option: HTMLElement = fixture.nativeElement.querySelector('.dropdown-item span'); + expect(option.querySelector('b')).toBeNull(); + expect(option.textContent).toEqual('Science Unit'); + }); + }); + }); diff --git a/src/app/entity-groups/research-entities/submission/item-list-elements/person/person-search-result-list-submission-element.component.ts b/src/app/entity-groups/research-entities/submission/item-list-elements/person/person-search-result-list-submission-element.component.ts index 534c694a4f8..dde8fd02e73 100644 --- a/src/app/entity-groups/research-entities/submission/item-list-elements/person/person-search-result-list-submission-element.component.ts +++ b/src/app/entity-groups/research-entities/submission/item-list-elements/person/person-search-result-list-submission-element.component.ts @@ -72,7 +72,7 @@ export class PersonSearchResultListSubmissionElementComponent extends SearchResu ngOnInit() { super.ngOnInit(); const defaultValue = this.dso ? this.dsoNameService.getName(this.dso) : undefined; - const alternatives = this.allMetadataValues(this.alternativeField); + const alternatives = this.allMetadataValues(this.alternativeField, false); this.allSuggestions = [defaultValue, ...alternatives]; this.relationshipService.getNameVariant(this.listID, this.dso.uuid) diff --git a/src/app/entity-groups/research-entities/submission/item-list-elements/person/person-suggestions/person-input-suggestions.component.html b/src/app/entity-groups/research-entities/submission/item-list-elements/person/person-suggestions/person-input-suggestions.component.html index da3a99dabff..c8057ded42a 100644 --- a/src/app/entity-groups/research-entities/submission/item-list-elements/person/person-suggestions/person-input-suggestions.component.html +++ b/src/app/entity-groups/research-entities/submission/item-list-elements/person/person-suggestions/person-input-suggestions.component.html @@ -22,7 +22,7 @@ @for (suggestionOption of suggestions; track suggestionOption) {
- + {{suggestionOption}}
} diff --git a/src/app/item-page/full/full-item-page.component.html b/src/app/item-page/full/full-item-page.component.html index 2c71f129991..d4d78b5cf68 100644 --- a/src/app/item-page/full/full-item-page.component.html +++ b/src/app/item-page/full/full-item-page.component.html @@ -60,7 +60,7 @@ {{mdEntry.key}} @for (value of (mdValue.value || '').split(SEPARATOR); track $index; let last = $last) { - @if (!last) {} + @if (!last) {} } @if ((isAdmin$|async)) { diff --git a/src/app/item-page/simple/field-components/clarin-description-item-field/clarin-description-item-field.component.spec.ts b/src/app/item-page/simple/field-components/clarin-description-item-field/clarin-description-item-field.component.spec.ts index 693d4beca45..a17de23b1b4 100644 --- a/src/app/item-page/simple/field-components/clarin-description-item-field/clarin-description-item-field.component.spec.ts +++ b/src/app/item-page/simple/field-components/clarin-description-item-field/clarin-description-item-field.component.spec.ts @@ -77,6 +77,17 @@ describe('ClarinDescriptionItemFieldComponent', () => { expect(anchor.nativeElement.getAttribute('target')).toEqual('_blank'); }); + it('should show markup in the description as text and keep URLs as links', () => { + render(itemWithDescriptions([ + { value: 'Bold See https://example.org', language: 'en_US' }, + ])); + + const element: HTMLElement = fixture.nativeElement; + expect(element.querySelector('b, img')).toBeNull(); + expect(element.textContent).toContain('Bold See https://example.org'); + expect(element.querySelector('a').getAttribute('href')).toEqual('https://example.org'); + }); + it('should render nothing when the item has no description', () => { render(Object.assign(new Item(), { metadata: {} })); diff --git a/src/app/process-page/detail/process-detail.component.spec.ts b/src/app/process-page/detail/process-detail.component.spec.ts index 481b32b542b..118aa139571 100644 --- a/src/app/process-page/detail/process-detail.component.spec.ts +++ b/src/app/process-page/detail/process-detail.component.spec.ts @@ -247,6 +247,9 @@ describe('ProcessDetailComponent', () => { const outputProcess = fixture.debugElement.query(By.css('#process-output pre')); expect(outputProcess.nativeElement.textContent).toContain(processOutput); }); + it('should request the output logs without a token in the URL', () => { + expect((httpClient.get as jasmine.Spy).calls.mostRecent().args[0]).toBe('log-selflink'); + }); }); describe('if press show output logs and process has no output logs', () => { diff --git a/src/app/process-page/detail/process-detail.component.ts b/src/app/process-page/detail/process-detail.component.ts index d5f752070f4..7bdc1759f35 100644 --- a/src/app/process-page/detail/process-detail.component.ts +++ b/src/app/process-page/detail/process-detail.component.ts @@ -55,7 +55,6 @@ import { getFirstSucceededRemoteData, getFirstSucceededRemoteDataPayload, } from '../../core/shared/operators'; -import { URLCombiner } from '../../core/url-combiner/url-combiner'; import { AlertType } from '../../shared/alert/alert-type'; import { hasValue } from '../../shared/empty.util'; import { ThemedFileDownloadLinkComponent } from '../../shared/file-download-link/themed-file-download-link.component'; @@ -108,7 +107,7 @@ export class ProcessDetailComponent implements OnInit, OnDestroy { filesRD$: Observable>>; /** - * File link that contain the output logs with auth token + * Link to the file that contains the output logs */ outputLogFileUrl$: Observable; @@ -225,13 +224,8 @@ export class ProcessDetailComponent implements OnInit, OnDestroy { this.showOutputLogs = true; } }), - switchMap((processOutput: RemoteData) => { - const url = processOutput.payload._links.content.href; - return this.authService.getShortlivedToken().pipe(take(1), - map((token: string) => { - return hasValue(token) ? new URLCombiner(url, `?authentication-token=${token}`).toString() : url; - })); - }), + // HttpClient sends the Authorization header, so the URL needs no token. + map((processOutput: RemoteData) => processOutput.payload._links.content.href), ); }); this.outputLogFileUrl$.pipe(take(1), diff --git a/src/app/request-copy/grant-deny-request-copy/grant-deny-request-copy.component.spec.ts b/src/app/request-copy/grant-deny-request-copy/grant-deny-request-copy.component.spec.ts index 919702911b2..810b8021b3c 100644 --- a/src/app/request-copy/grant-deny-request-copy/grant-deny-request-copy.component.spec.ts +++ b/src/app/request-copy/grant-deny-request-copy/grant-deny-request-copy.component.spec.ts @@ -113,6 +113,13 @@ describe('GrantDenyRequestCopyComponent', () => { }); }); + it('should request the item name HTML-escaped', (done) => { + component.itemName$.subscribe(() => { + expect(nameService.getName).toHaveBeenCalledWith(item, true); + done(); + }); + }); + it('should initialise itemUrl$', (done) => { component.itemUrl$.subscribe((result) => { expect(result).toEqual(itemUrl); diff --git a/src/app/request-copy/grant-deny-request-copy/grant-deny-request-copy.component.ts b/src/app/request-copy/grant-deny-request-copy/grant-deny-request-copy.component.ts index ae1d5dc43ec..0e864242d95 100644 --- a/src/app/request-copy/grant-deny-request-copy/grant-deny-request-copy.component.ts +++ b/src/app/request-copy/grant-deny-request-copy/grant-deny-request-copy.component.ts @@ -102,7 +102,7 @@ export class GrantDenyRequestCopyComponent implements OnInit { ); this.itemName$ = this.itemRD$.pipe( getFirstSucceededRemoteDataPayload(), - map((item) => this.nameService.getName(item)), + map((item) => this.nameService.getName(item, true)), ); this.itemUrl$ = this.itemRD$.pipe( getFirstSucceededRemoteDataPayload(), diff --git a/src/app/shared/clarin-shared-util.spec.ts b/src/app/shared/clarin-shared-util.spec.ts index c956791aed6..9f531aef3aa 100644 --- a/src/app/shared/clarin-shared-util.spec.ts +++ b/src/app/shared/clarin-shared-util.spec.ts @@ -1,9 +1,27 @@ import { buildAuthoritySearchFilter, convertMetadataFieldIntoSearchType, + makeLinks, } from './clarin-shared-util'; describe('clarin-shared-util', () => { + describe('makeLinks', () => { + it('turns URLs into links', () => { + expect(makeLinks('See https://example.org/a?b=1&c=2 now')) + .toBe('See https://example.org/a?b=1&c=2 now'); + }); + + it('escapes markup in the text', () => { + expect(makeLinks('Read here')) + .toBe('Read <a href="https://example.org/">here</a><img src="x">'); + }); + + it('keeps empty values', () => { + expect(makeLinks(undefined)).toBeUndefined(); + expect(makeLinks('')).toBe(''); + }); + }); + describe('buildAuthoritySearchFilter', () => { it('uses the authority operator and key when an authority is present', () => { expect(buildAuthoritySearchFilter('publisher', { value: 'ACME Press', authority: '02mhbdp94' })) diff --git a/src/app/shared/clarin-shared-util.ts b/src/app/shared/clarin-shared-util.ts index ad7fef77478..03deb3470f8 100644 --- a/src/app/shared/clarin-shared-util.ts +++ b/src/app/shared/clarin-shared-util.ts @@ -1,4 +1,5 @@ import { DomSanitizer } from '@angular/platform-browser'; +import escape from 'lodash/escape'; import { ConfigurationDataService } from '../core/data/configuration-data.service'; import { MetadataValue } from '../core/shared/metadata.models'; @@ -97,9 +98,13 @@ export function loadItemAuthors(item, itemAuthors, baseUrl, fields) { } export function makeLinks(text: string): string { - // Use a regular expression to find URLs and convert them into clickable links - const regex = /(?:https?|ftp):\/\/[^\s)]+|www\.[^\s)]+/g; - return text?.replace(regex, (url) => `${url}`); + if (!text) { + return text; + } + // Split the text into plain parts and URLs (the odd parts). Everything is escaped, so the links are the only HTML. + return text.split(/((?:https?|ftp):\/\/[^\s)"<>]+|www\.[^\s)"<>]+)/) + .map((part, i) => i % 2 === 0 ? escape(part) : `${escape(part)}`) + .join(''); } /** diff --git a/src/app/shared/form/builder/ds-dynamic-form-ui/models/autocomplete/ds-dynamic-autocomplete.component.spec.ts b/src/app/shared/form/builder/ds-dynamic-form-ui/models/autocomplete/ds-dynamic-autocomplete.component.spec.ts index 81c9b6bc3c8..acb88cc1bd6 100644 --- a/src/app/shared/form/builder/ds-dynamic-form-ui/models/autocomplete/ds-dynamic-autocomplete.component.spec.ts +++ b/src/app/shared/form/builder/ds-dynamic-form-ui/models/autocomplete/ds-dynamic-autocomplete.component.spec.ts @@ -216,6 +216,11 @@ describe('DsDynamicAutocompleteComponent test suite', () => { it('should format object values using display fallback', () => { expect(autComp.formatter({ display: 'Alumu-Tesu' })).toEqual('Alumu-Tesu'); }); + + it('should escape markup in the suggestions', () => { + const suggestion: any = Object.assign(new VocabularyEntry(), { display: 'A & B ', value: 1 }); + expect(autComp.suggestionFormatter(suggestion)).toEqual('A & B <img src="x">'); + }); }); }); diff --git a/src/app/shared/form/builder/ds-dynamic-form-ui/models/autocomplete/ds-dynamic-autocomplete.component.ts b/src/app/shared/form/builder/ds-dynamic-form-ui/models/autocomplete/ds-dynamic-autocomplete.component.ts index a5ca577e8c8..8b1422258de 100644 --- a/src/app/shared/form/builder/ds-dynamic-form-ui/models/autocomplete/ds-dynamic-autocomplete.component.ts +++ b/src/app/shared/form/builder/ds-dynamic-form-ui/models/autocomplete/ds-dynamic-autocomplete.component.ts @@ -24,6 +24,7 @@ import { DynamicFormValidationService, } from '@ng-dynamic-forms/core'; import { TranslateService } from '@ngx-translate/core'; +import escape from 'lodash/escape'; import { BehaviorSubject, Observable, @@ -257,7 +258,7 @@ export class DsDynamicAutocompleteComponent extends DsDynamicTagComponent implem return DsDynamicAutocompleteService.pretifyLanguageSuggestion(suggestion, this.translateService); } // @ts-ignore - return suggestion.display; + return escape(suggestion.display); }; /** diff --git a/src/app/shared/form/builder/ds-dynamic-form-ui/models/autocomplete/ds-dynamic-autocomplete.service.ts b/src/app/shared/form/builder/ds-dynamic-form-ui/models/autocomplete/ds-dynamic-autocomplete.service.ts index e1c3d2c2380..b04a181b5d3 100644 --- a/src/app/shared/form/builder/ds-dynamic-form-ui/models/autocomplete/ds-dynamic-autocomplete.service.ts +++ b/src/app/shared/form/builder/ds-dynamic-form-ui/models/autocomplete/ds-dynamic-autocomplete.service.ts @@ -1,4 +1,5 @@ import { TranslateService } from '@ngx-translate/core'; +import escape from 'lodash/escape'; import { take } from 'rxjs/operators'; import { isEmpty } from '../../../../../empty.util'; @@ -26,7 +27,7 @@ export class DsDynamicAutocompleteService { .pipe(take(1)) .subscribe( pn => { projectName = pn; }); - return (fundingCode + ': ').bold() + fundingProjectCode + '
' + (projectName + ': ').bold() + fundingName; + return (fundingCode + ': ').bold() + escape(fundingProjectCode) + '
' + (projectName + ': ').bold() + escape(fundingName); } static pretifyLanguageSuggestion(suggestion, translateService: TranslateService) { @@ -35,7 +36,7 @@ export class DsDynamicAutocompleteService { // fetch language message const languageMessage = translateService.instant('autocomplete.suggestion.language.title'); - return (isoMessage + ': ').bold() + suggestion?.value + '
' + (languageMessage + ': ').bold() + - suggestion?.display; + return (isoMessage + ': ').bold() + escape(suggestion?.value) + '
' + (languageMessage + ': ').bold() + + escape(suggestion?.display); } } diff --git a/src/app/shared/utils/emphasize.pipe.spec.ts b/src/app/shared/utils/emphasize.pipe.spec.ts new file mode 100644 index 00000000000..66be839a673 --- /dev/null +++ b/src/app/shared/utils/emphasize.pipe.spec.ts @@ -0,0 +1,13 @@ +import { EmphasizePipe } from './emphasize.pipe'; + +describe('EmphasizePipe', () => { + const pipe = new EmphasizePipe(); + + it('should wrap the matches in tags', () => { + expect(pipe.transform('Smith, Sam', 's')).toEqual('Smith, Sam'); + }); + + it('should escape markup in the value', () => { + expect(pipe.transform('Smith & Co ', 'smi')).toEqual('Smith & Co <img src="x">'); + }); +}); diff --git a/src/app/shared/utils/emphasize.pipe.ts b/src/app/shared/utils/emphasize.pipe.ts index fbbe19670d4..46c0c113165 100644 --- a/src/app/shared/utils/emphasize.pipe.ts +++ b/src/app/shared/utils/emphasize.pipe.ts @@ -2,6 +2,7 @@ import { Pipe, PipeTransform, } from '@angular/core'; +import escape from 'lodash/escape'; @Pipe({ name: 'dsEmphasize', @@ -45,9 +46,11 @@ export class EmphasizePipe implements PipeTransform { * @returns {any} Transformed haystack with the needle emphasized */ transform(haystack, needle): any { - const escaped = this.escapeRegExp(needle); - const reg = new RegExp(escaped, 'gi'); - return haystack.replace(reg, '$&'); + const reg = new RegExp('(' + this.escapeRegExp(needle) + ')', 'gi'); + // The matches are the odd parts. Everything is escaped, so the tags are the only HTML. + return haystack.split(reg) + .map((part, i) => i % 2 === 0 ? escape(part) : '' + escape(part) + '') + .join(''); } /**