Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions src/material/icon/icon.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(`
<svg>
<filter id="blur">
<feGaussianBlur in="SourceGraphic" stdDeviation="5" />
</filter>

<circle cx="170" cy="60" r="50" fill="green" filter="url('#blur')" />
</svg>
`),
);

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(`
<svg>
<mask id="cover">
<circle cx="170" cy="60" r="50" fill="white" />
</mask>

<circle cx="170" cy="60" r="50" fill="green" mask="url('#cover')" />
</svg>
`),
);

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',
Expand Down
16 changes: 15 additions & 1 deletion src/material/icon/icon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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:
*
Expand Down Expand Up @@ -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}')`);
});
});
}
Expand Down
Loading