diff --git a/src/material/icon/icon.spec.ts b/src/material/icon/icon.spec.ts index 73ea5b43c953..a294413cc4d2 100644 --- a/src/material/icon/icon.spec.ts +++ b/src/material/icon/icon.spec.ts @@ -1038,6 +1038,64 @@ describe('MatIcon', () => { ); }); + it('should not allow the path to terminate the `url()` string', () => { + fakePath = `/$fake-path'),url('https://$fake-host/collect`; + iconRegistry.addSvgIconLiteral( + 'fido', + trustHtml(` + + + + + + + + `), + ); + + const fixture = TestBed.createComponent(IconFromSvgName); + fixture.componentInstance.iconName = 'fido'; + fixture.changeDetectorRef.markForCheck(); + fixture.detectChanges(); + const circle = fixture.nativeElement.querySelector('mat-icon svg circle'); + const value = circle.getAttribute('filter')!; + + // A quote in the path must not be able to close the string and append another `url()` + // token, since `filter` accepts a list of references. Stripping the escape sequences has to + // leave a single quoted URL with no quotes of its own. + expect(value.replace(/\\./g, '')).toMatch(/^url\('[^']*'\)$/); + expect(value).toContain(`\\'`); + }); + + it('should escape backslashes in the path when prefixing the `url()` references', () => { + fakePath = `/$fake-path\\?q='x`; + iconRegistry.addSvgIconLiteral( + 'fido', + trustHtml(` + + + + + + + + `), + ); + + const fixture = TestBed.createComponent(IconFromSvgName); + fixture.componentInstance.iconName = 'fido'; + fixture.changeDetectorRef.markForCheck(); + fixture.detectChanges(); + const circle = fixture.nativeElement.querySelector('mat-icon svg circle'); + const value = circle.getAttribute('mask')!; + + // A backslash must not be able to escape the closing quote of the string. + expect(value.replace(/\\./g, '')).toMatch(/^url\('[^']*'\)$/); + expect(value).toContain(`\\\\`); + expect(value).toContain(`\\'`); + expect(value.endsWith(`#cover')`)).toBe(true); + }); + it('should use latest path when prefixing the `url()` references', () => { iconRegistry.addSvgIconLiteral( 'fido', diff --git a/src/material/icon/icon.ts b/src/material/icon/icon.ts index 7b1c24e88290..fdcab09616f2 100644 --- a/src/material/icon/icon.ts +++ b/src/material/icon/icon.ts @@ -95,6 +95,15 @@ const funcIriAttributeSelector = funcIriAttributes.map(attr => `[${attr}]`).join /** Regex that can be used to extract the id out of a FuncIRI. */ const funcIriPattern = /^url\(['"]?#(.*?)['"]?\)$/; +/** + * Escapes a value so that it can be interpolated into a single-quoted CSS string without being + * able to terminate it. Tabs and newlines are removed by the URL parser, so a backslash and a + * single quote are the only characters that need to be escaped here. + */ +function escapeCssStringValue(value: string): string { + return value.replace(/[\\']/g, '\\$&'); +} + /** * Component to display an icon. It can be used in the following ways: * @@ -408,11 +417,16 @@ export class MatIcon implements OnInit, AfterViewChecked, OnDestroy { // Prefix it with a dot segment so it stays on the current origin without changing the resolved // path. const normalizedPath = path.startsWith('//') ? `/.${path}` : path; + // The path is interpolated into a quoted CSS string below. Escape it so that a path + // containing a literal quote cannot close that string and append further URL tokens to the + // attribute, which some of these attributes (e.g. `mask` and `filter`) accept as a list. + const escapedPath = escapeCssStringValue(normalizedPath); if (elements) { elements.forEach((attrs, element) => { attrs.forEach(attr => { - element.setAttribute(attr.name, `url('${normalizedPath}#${attr.value}')`); + const escapedId = escapeCssStringValue(attr.value); + element.setAttribute(attr.name, `url('${escapedPath}#${escapedId}')`); }); }); }