2

I'm encountering an issue when trying to embed GitHub Gists in my Angular application. I have a component where I want to display code snippets from GitHub Gists using iframes. However, I'm facing a security-related problem

When I try to embed a Gist using an iframe with a source URL like this:

//COMPONENT
import { Component } from '@angular/core';
import { DomSanitizer, SafeResourceUrl } from '@angular/platform-browser';
@Component({
  selector: 'app-code-block',
  templateUrl: './code-block.component.html',
  styleUrls: ['./code-block.component.scss'],
})
export class CodeBlockComponent {
  constructor(private sanitizer: DomSanitizer) {}

  gistUrl: string =
    'https://gist.github.com/KeanuBarnardd/c058985c88b93f6b7247da50ff927a28';
    
  getSafeUrl(url: string): SafeResourceUrl {
    return this.sanitizer.bypassSecurityTrustResourceUrl(url);
  }
}

// HTML
<iframe [src]="getSafeUrl(gistUrl)" frameborder="0"></iframe>

I receive the following error in my browser console: ERROR Error: NG0904: unsafe value used in a resource URL context

I have attempted to use Angular's DomSanitizer to bypass security, but it doesn't resolve the issue.

I've also tested this in different browsers with the same result.

I've tried the methods given here all of them seem to be outdated. How to embed a GitHub gist in the Angular template?

I've also tried just putting the embedding tag directly into the HTML that doesn't work either as I get an HTML stripping security error.

Any help would be good thanks.

2
  • Have you tried using a pipe as detailed in stackoverflow.com/a/38079724/11487901 ? Commented Nov 9, 2023 at 11:02
  • try this this.sanitizer.bypassSecurityTrustUrl(value) by omitting Resource Commented Nov 10, 2023 at 20:28

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.