question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Extensible Sanitizer

See original GitHub issue

🚀 feature request

Relevant Package

@angular/core

this line and file are of key interest: https://github.com/angular/angular/blob/486cade596a57b7c146ae3aa269d69d7d828a6ab/packages/core/src/sanitization/html_sanitizer.ts#L95

Description

The ability to trust a custom whitelist of HTML tags, attributes, and values with a DOM sanitizer, without bypassing the whole sanitizer.

This would allow preservation of id, style, data attributes, and other common attributes. These attributes are useful in a variety of use cases for many Angular developers. This issue is a common cause of many GitHub issues, StackOverflow questions, and other indicators that there is a real problem with demand for a secure and flexible solution.

Related SO 1 Related SO 2 Tutorial on a best practice approach to doing what I am requesting this would close a bunch of github issues and resolve many SO questions.

Describe the solution you’d like

I would like to be able to specify three things to allow keys and values to be sanitized in a SecurityContext.HTML.

This could be implemented as an options argument to DomSanitizer.sanitize, or it could be implemented as a custom SecurityContext, but I will use options object in my description below:

    trustedFirstParagraph$ = this.translateService
        .translate('some-translation-key')
        .pipe(map(s => this.domSanitizer.sanitize(SecurityContext.HTML, s, options)));

Here, options can be an object with any of three keys:

const options = {
  trustAttributeKeyExpression: /some-regex-to-trust/,
  trustAttributeValueExpression: /some-value-to-trust/,
  trustAttribute: (el, key, value): boolean | string[] => {
      // arbitrary logic that can return a boolean
      // or [string, string] sanitized/transformed [key, value].
  }
}

trustAttributeKeyExpression and trustAttributeValueExpression must both match if both are specified. To implement an OR operation, and other more complex algorithms, a developer can use trustAttribute.

Describe alternatives you’ve considered

  1. hiding data inside the class attribute and parsing it
  2. using a span or div inside my element which is visually hidden but which I can access through my component dom ref and parse out the value
  3. de-DRYing my code and having component-specific content across n components instead of being able to leverage a generic service

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:52
  • Comments:7

github_iconTop GitHub Comments

7reactions
Ruud-cbcommented, Aug 4, 2021

Please put this a bit higher on the to-do list, top-listed stackoverflow questions suggest using bypassSecurityTrustHtml as a solution! Thus leaving a lot of applications open for XSS attacks…

5reactions
RichardMcSorleycommented, Nov 5, 2020

Agreed this would be a much need feature. I’m unable to use data attributes for my markdown to HTML project. So without this I’m blocked.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Urgo Extensible : Adhesive Disinfectant Precut Bandage x30
Type: Precut Bandage ; Expiration Date: N/A ; Active Ingredients: Rapid Wound Dressing ; MPN: Does not apply ; Set Includes: Rapid Wound...
Read more >
Amazon Basics Extendable Window Squeegee with Spray ...
Window squeegee for safely and conveniently cleaning indoor and outdoor windows; Includes a window squeegee with a telescopic aluminum handle that extends ...
Read more >
HoMedics UV-Clean Phone Sanitizer with Logo - 4AllPromos
This expandable sanitizer is easily stored away when collapsed so you can have it with your wherever you go - in your car,...
Read more >
Long Handle Extensible Dust Cleaner Limpieza Hogar ...
Long handle Extensible Dust Cleaner limpieza hogar Microfiber Cleaning Brush for Household Sofa Gap Furniture Dust Remover. Extra 2% Off. 5 orders.
Read more >
Invest in Sanitizers | Fuchsia
Currently Fuchsia uses several sanitizers to detect memory safety bugs: AddressSanitizer (ASan) detects instances of out-of-bounds access, use ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found