Skip to content
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

Add support for trusted types (CSP) #230

Merged

Conversation

tomastrajan
Copy link
Contributor

Hey there!

TLDR; This PR enables apps served with require-trusted-types-for 'script' CSP header to work with ngx-highlightjs.

We (and probably other folks out there) need to be able to use the HTTP Content-Security-Policy (CSP) trusted-types

The gist of it is that the ngx-highlightjs uses innerHTML assignment (with Angular sanitizer which is correct) but still evaluated as a security risk by the CSP. Therefore we have to create a policy to allow consumers to trust the library.

In consumer application then CSP can be enabled for local development and testing using

       "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "options": {
            "browserTarget": "rwc-b2c-components-showcase-rwc:build",
            "headers": {
              "Content-Security-Policy": "trusted-types ngx-highlightjs angular angular#unsafe-bypass angular#bundler; require-trusted-types-for 'script'"
            }
          }
        },

Keep in mind that in the end it is the responsibility of server to set correct CSP headers when serving the application (not responsibility of this lib).

This solution is heavily inspired by Angular own solution
Read more about trusted types and their support in Angular

@MurhafSousli
Copy link
Owner

Thanks a lot!

@MurhafSousli MurhafSousli merged commit a416dc8 into MurhafSousli:master Jul 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants