Skip to content

fix(icon): use SafeResourceUrl in getSvgIconFromUrl #7535

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 11, 2017
Merged
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
2 changes: 1 addition & 1 deletion src/lib/icon/icon-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ export class MatIconRegistry {
return observableOf(cloneSvg(cachedIcon));
}

return RxChain.from(this._loadSvgIconFromConfig(new SvgIconConfig(url)))
return RxChain.from(this._loadSvgIconFromConfig(new SvgIconConfig(safeUrl)))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While we're at it, it might be a good idea to rename the url variable above to something along the lines of sanitizedUrl to avoid confusion in the future.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really the type should enforce passing the right thing around; I want to check with Martin about adding a brand to safe types (since they're empty they match everything)

.call(doOperator, svg => this._cachedIconsByUrl.set(url!, svg))
.call(map, svg => cloneSvg(svg))
.result();
Expand Down
13 changes: 10 additions & 3 deletions src/lib/icon/icon.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {inject, async, TestBed} from '@angular/core/testing';
import {inject, async, fakeAsync, tick, TestBed} from '@angular/core/testing';
import {SafeResourceUrl, DomSanitizer} from '@angular/platform-browser';
import {HttpModule, XHRBackend} from '@angular/http';
import {MockBackend} from '@angular/http/testing';
Expand Down Expand Up @@ -126,7 +126,7 @@ describe('MatIcon', () => {
});

describe('Icons from URLs', () => {
it('should register icon URLs by name', () => {
it('should register icon URLs by name', fakeAsync(() => {
iconRegistry.addSvgIcon('fluffy', trust('cat.svg'));
iconRegistry.addSvgIcon('fido', trust('dog.svg'));

Expand All @@ -153,7 +153,14 @@ describe('MatIcon', () => {
svgElement = verifyAndGetSingleSvgChild(matIconElement);
verifyPathChildElement(svgElement, 'woof');
expect(httpRequestUrls).toEqual(['dog.svg', 'cat.svg']);
});

// Assert that a registered icon can be looked-up by url.
iconRegistry.getSvgIconFromUrl(trust('cat.svg')).subscribe(element => {
verifyPathChildElement(element, 'meow');
});

tick();
}));

it('should throw an error when using an untrusted icon url', () => {
iconRegistry.addSvgIcon('fluffy', 'farm-set-1.svg');
Expand Down