From fb5aef4d14228942573eb61034ba990f0dd0be91 Mon Sep 17 00:00:00 2001 From: Isaries Date: Sat, 14 Nov 2026 11:29:06 +0800 Subject: [PATCH] fix(ProjectService): Inject asset paths into HTML attributes whose file names have parentheses replaceAssetPaths left a relative file name with parentheses relative when it appeared in HTML, such as , because the name class of its regex has no parentheses. The browser then resolves the name against the page URL and the image is broken, for students and in the authoring editor alike. After the existing replacement, quoted src, href and poster attribute values and quoted CSS url() values are tested whole against the file name pattern already used for standalone values, and get the prefix when they match. The closing quote must match the opening one, so a value cannot run into the next string. Both raw HTML and HTML escaped inside a JSON string are handled. --- src/app/services/projectService.spec.ts | 67 +++++++++++++++++++++ src/assets/wise5/services/projectService.ts | 18 +++++- 2 files changed, 83 insertions(+), 2 deletions(-) diff --git a/src/app/services/projectService.spec.ts b/src/app/services/projectService.spec.ts index 6dfbb48375d..45d301d326f 100644 --- a/src/app/services/projectService.spec.ts +++ b/src/app/services/projectService.spec.ts @@ -197,9 +197,76 @@ function injectAssetPaths() { const content = { src: '/curriculum/1/assets/a (1).png' }; expect(service.injectAssetPaths(content).src).toEqual('/curriculum/1/assets/a (1).png'); }); + it('should inject a src with parentheses as multiple choice authoring writes it', () => { + expectHtmlInjected( + 'image (1).png', + `image (1).png` + ); + }); + it('should inject an upper-case, single-quoted attribute with spaces around =', () => { + expectHtmlInjected( + "", + `` + ); + }); + it('should inject href, poster and source src with parentheses', () => { + expectHtmlInjected( + 'doc', + `doc` + + `` + ); + }); + it('should inject quoted CSS url() values with parentheses', () => { + expectHtmlInjected( + `
` + + '', + `
` + + `` + ); + }); + it('should inject an attribute that follows a newline', () => { + expectHtmlInjected( + '', + `` + ); + }); + it('should inject a src with CJK brackets', () => { + expectHtmlInjected( + '', + `` + ); + }); + it('should not change absolute URLs, data URIs or other attributes in HTML', () => { + const html = + 'a' + + ''; + expectHtmlInjected(html, html); + }); + it('should not change prose or an unquoted url() with parentheses', () => { + const html = '

(see) a (1).png

'; + expectHtmlInjected(html, html); + }); + it('should not inject twice with a root-relative base URL', () => { + (configService.getConfigParam as jasmine.Spy).and.returnValue('/curriculum/12345/'); + const html = 'b'; + const once = service.replaceAssetPaths(html); + expect(once).toEqual( + '' + + 'b' + ); + expect(service.replaceAssetPaths(once)).toEqual(once); + expect(service.injectAssetPaths(service.injectAssetPaths({ html })).html).toEqual(once); + }); }); } +function expectHtmlInjected(html: string, expected: string): void { + expect(service.replaceAssetPaths(html)).toEqual(expected); + expect(service.injectAssetPaths({ html }).html).toEqual(expected); +} + function shouldRetrieveProjectWhenConfigProjectURLIsValid() { it('should retrieve project when Config.projectURL is valid', () => { spyOn(configService, 'getConfigParam').withArgs('projectURL').and.returnValue(projectURL); diff --git a/src/assets/wise5/services/projectService.ts b/src/assets/wise5/services/projectService.ts index 55aa647da4f..ecd9f47ad5d 100644 --- a/src/assets/wise5/services/projectService.ts +++ b/src/assets/wise5/services/projectService.ts @@ -22,8 +22,9 @@ import { ProjectLocale } from '../../../app/domain/projectLocale'; import { Language } from '../../../app/domain/language'; import { ThemeSettings } from '../common/ThemeSettings'; -// Wider than the name class in replaceAssetPaths because nothing surrounds a standalone value for -// a match to run into. Sentence punctuation is excluded so prose ending in a file name is not +// Tested against a whole value: a standalone string, or a quoted attribute or url() value in +// replaceAssetPaths. Wider than the name class of the regex there because nothing around a whole +// value can be swallowed. Sentence punctuation is excluded so prose ending in a file name is not // mistaken for one. const RELATIVE_ASSET_FILE_NAME = new RegExp( '^(?!/)[a-zA-Z0-9@%&;()._/ \\-' + @@ -33,6 +34,12 @@ const RELATIVE_ASSET_FILE_NAME = new RegExp( 'i' ); +// A quoted URL attribute or CSS url() value. The matching closing quote bounds the value, so a +// name with parentheses can be tested whole without running into the next string. \\[nrt] covers +// whitespace that JSON.stringify has escaped. +const QUOTED_ASSET_REFERENCE = + /((?:\s|\\[nrt])(?:src|href|poster)\s*=\s*|url\(\s*)(\\?["'])(.*?)\2/gi; + @Injectable() export class ProjectService { achievements: any = []; @@ -484,6 +491,13 @@ export class ProjectService { return delimiter + contentBaseURL + 'assets/' + rest; } ); + contentString = contentString.replace( + QUOTED_ASSET_REFERENCE, + (matchedString, lead: string, quote: string, value: string) => + RELATIVE_ASSET_FILE_NAME.test(value) + ? lead + quote + contentBaseURL + 'assets/' + value + quote + : matchedString + ); } return contentString; }