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

fix[angular-gen2]: embed and custom code block and text block angular sanitizing html warning #3809

Merged

Conversation

sidmohanty11
Copy link
Contributor

@sidmohanty11 sidmohanty11 commented Jan 2, 2025

Description

Uses https://angular.dev/api/platform-browser/DomSanitizer to mark text as safe html to use inside innerHTML. As text is already sanitized from the HTML editor we should be able to bypass it safely. This only shows up in the dev server and we don't receive these warnings in a prod build.

Fixes Embed and Custom Code block wrapping them inside sanitizer.bypassSecurityTrustHtml so that they allow embedding of iframe

Fixes #3793

Jira
https://builder-io.atlassian.net/browse/ENG-7950
https://builder-io.atlassian.net/browse/ENG-7901

Screenshot
Before
Screenshot 2025-01-02 at 6 43 43 PM
Screenshot 2025-01-02 at 6 43 50 PM
After
Screenshot 2025-01-02 at 6 42 52 PM
Screenshot 2025-01-02 at 6 45 39 PM

@sidmohanty11 sidmohanty11 requested review from samijaber and a team as code owners January 2, 2025 13:06
Copy link

changeset-bot bot commented Jan 2, 2025

🦋 Changeset detected

Latest commit: 076db35

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@builder.io/sdk-angular Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link

nx-cloud bot commented Jan 2, 2025

View your CI Pipeline Execution ↗ for commit 076db35.

Command Status Duration Result
nx test @e2e/sveltekit ✅ Succeeded 7m 30s View ↗
nx test @e2e/qwik-city ✅ Succeeded 7m 22s View ↗
nx test @e2e/nextjs-sdk-next-app ✅ Succeeded 7m 34s View ↗
nx test @e2e/nuxt ✅ Succeeded 7m 3s View ↗
nx test @e2e/react-sdk-next-pages ✅ Succeeded 6m 7s View ↗
nx test @e2e/gen1-remix ✅ Succeeded 6m 19s View ↗
nx test @e2e/react-native ✅ Succeeded 5m 22s View ↗
nx test @e2e/gen1-next15-app ✅ Succeeded 4m 53s View ↗
Additional runs (34) ✅ Succeeded ... View ↗

☁️ Nx Cloud last updated this comment at 2025-01-15 05:37:01 UTC

@samijaber
Copy link
Contributor

Have these angular tests been failing sporadically before this PR?

@sidmohanty11
Copy link
Contributor Author

@samijaber no these are related to changes I've made here. Turns out the text component is triggering ngAfterViewChecked if the parent dom element has a mouseenter event listener calling the getter frequently when we hover and sanitizer.bypassSecurityTrustHtml(processedText) is creating a new object everytime its called resulting in non-interactivity. This I think should be fixed from #3812 because it removes the getter and the text will only update when its updated from the block itself

if (variableName) {
code = code.replace(
`[innerHTML]="${variableName}"`,
`[innerHTML]="sanitizer.bypassSecurityTrustHtml(${variableName})"`
Copy link
Contributor Author

@sidmohanty11 sidmohanty11 Jan 10, 2025

Choose a reason for hiding this comment

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

@samijaber this was needed for Embed and Custom Code block as well so I extended this PR to fix them too

Copy link
Contributor

Choose a reason for hiding this comment

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

long-term I think this should be baked into mitosis: the angular generator should always use sanitizer.bypassSecurityTrustHtml for innerHTML (maybe with a flag to force-disable the sanitizer if needed)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

great idea, i've created a ticket for it

Comment on lines 722 to 728
code: {
post: (code) => {
if (
code.includes('selector: "builder-text"') ||
code.includes('selector: "builder-embed"') ||
code.includes('selector: "custom-code"')
) {
Copy link
Contributor

Choose a reason for hiding this comment

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

We now pass json as the 2nd arg for cost plugins: https://github.com/BuilderIO/mitosis/blob/f3fabea5d912662e5e6454fd0f9088cb98c72f65/packages/core/src/modules/plugins.ts#L44-L84

This lets you do cleaner checks, like ['Text', 'Embed', 'CustomCode'].includes(json.name)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done!

if (variableName) {
code = code.replace(
`[innerHTML]="${variableName}"`,
`[innerHTML]="sanitizer.bypassSecurityTrustHtml(${variableName})"`
Copy link
Contributor

Choose a reason for hiding this comment

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

long-term I think this should be baked into mitosis: the angular generator should always use sanitizer.bypassSecurityTrustHtml for innerHTML (maybe with a flag to force-disable the sanitizer if needed)

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.

Angular Gen2 - HTML sanitization warning
2 participants