| 
									
										
										
										
											2016-06-20 23:34:14 -07:00
										 |  |  | // #docplaster
 | 
					
						
							|  |  |  | // #docregion
 | 
					
						
							|  |  |  | import { Component } from '@angular/core'; | 
					
						
							| 
									
										
										
										
											2016-09-01 02:08:57 +01:00
										 |  |  | import { DomSanitizer, SafeResourceUrl, SafeUrl } from '@angular/platform-browser'; | 
					
						
							| 
									
										
										
										
											2016-06-20 23:34:14 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | @Component({ | 
					
						
							|  |  |  |   selector: 'bypass-security', | 
					
						
							| 
									
										
										
										
											2016-09-25 18:51:54 -07:00
										 |  |  |   moduleId: module.id, | 
					
						
							|  |  |  |   templateUrl: 'bypass-security.component.html', | 
					
						
							| 
									
										
										
										
											2016-06-20 23:34:14 -07:00
										 |  |  | }) | 
					
						
							|  |  |  | export class BypassSecurityComponent { | 
					
						
							| 
									
										
										
										
											2016-06-30 07:21:55 -07:00
										 |  |  |   dangerousUrl: string; | 
					
						
							|  |  |  |   trustedUrl: SafeUrl; | 
					
						
							|  |  |  |   dangerousVideoUrl: string; | 
					
						
							| 
									
										
										
										
											2016-06-20 23:34:14 -07:00
										 |  |  |   videoUrl: SafeResourceUrl; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // #docregion trust-url
 | 
					
						
							| 
									
										
										
										
											2016-09-01 02:08:57 +01:00
										 |  |  |   constructor(private sanitizer: DomSanitizer) { | 
					
						
							| 
									
										
										
										
											2016-06-30 07:21:55 -07:00
										 |  |  |     // javascript: URLs are dangerous if attacker controlled.
 | 
					
						
							| 
									
										
										
										
											2016-09-14 12:11:53 -04:00
										 |  |  |     // Angular sanitizes them in data binding, but you can
 | 
					
						
							| 
									
										
										
										
											2016-06-30 07:21:55 -07:00
										 |  |  |     // explicitly tell Angular to trust this value:
 | 
					
						
							|  |  |  |     this.dangerousUrl = 'javascript:alert("Hi there")'; | 
					
						
							|  |  |  |     this.trustedUrl = sanitizer.bypassSecurityTrustUrl(this.dangerousUrl); | 
					
						
							| 
									
										
										
										
											2016-06-20 23:34:14 -07:00
										 |  |  |     // #enddocregion trust-url
 | 
					
						
							|  |  |  |     this.updateVideoUrl('PUBnlbjZFAI'); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // #docregion trust-video-url
 | 
					
						
							|  |  |  |   updateVideoUrl(id: string) { | 
					
						
							|  |  |  |     // Appending an ID to a YouTube URL is safe.
 | 
					
						
							| 
									
										
										
										
											2016-06-30 07:21:55 -07:00
										 |  |  |     // Always make sure to construct SafeValue objects as
 | 
					
						
							| 
									
										
										
										
											2016-09-14 12:11:53 -04:00
										 |  |  |     // close as possible to the input data so
 | 
					
						
							| 
									
										
										
										
											2016-06-20 23:34:14 -07:00
										 |  |  |     // that it's easier to check if the value is safe.
 | 
					
						
							| 
									
										
										
										
											2016-06-30 07:21:55 -07:00
										 |  |  |     this.dangerousVideoUrl = 'https://www.youtube.com/embed/' + id; | 
					
						
							| 
									
										
										
										
											2016-06-20 23:34:14 -07:00
										 |  |  |     this.videoUrl = | 
					
						
							| 
									
										
										
										
											2016-06-30 07:21:55 -07:00
										 |  |  |         this.sanitizer.bypassSecurityTrustResourceUrl(this.dangerousVideoUrl); | 
					
						
							| 
									
										
										
										
											2016-06-20 23:34:14 -07:00
										 |  |  |   } | 
					
						
							|  |  |  |   // #enddocregion trust-video-url
 | 
					
						
							|  |  |  | } |