You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
bug: IonToast with IonicSafeString display 'undefined' #27118
I have searched for existing issues that already report this problem, without success.
Ionic Framework Version
v7.x
Current Behavior
When I try to display an IonToast with a message containing an IonicSafeString, the text "undefined" is displayed inside the ion-toast, instead of the contents of the IonicSafeString
Expected Behavior
The IonToast must display the contents of the IonicSafeString
Steps to Reproduce
Create an IonToast with the use of ToastController
In message property set a IonicSafeString with 'Hello!'
Present the IonToast and see the message 'undefined' in it
Thanks for the report. The problem here is you do not have innerHTMLTemplatesEnabled enabled in your Ionic config. In Ionic 7 we disabled the custom innerHTML functionality by default, so developers need to first enable this functionality before they can use it. This is noted in the Ionic 7 Breaking Changes Guide.
To fix this, set innerHTMLTemplatesEnabled: true in IonicModule.forRoot in app.module.ts. Adding this resolved the issue on my end. I am going to close this, but let me know if you have any questions.
IonicSafeString only has the desired result when innerHTMLTemplatesEnabled: true which was the default in Ionic v6. In other words, the innerHTML usage was enabled by default in Ionic v6 even if you were not passing in custom HTML or using IonicSafeString.
IonicSafeString allows you to bypass Ionic's sanitizer to dangerously append custom HTML content using innerHTML. However, if this innerHTML functionality is disabled then IonicSafeString will have no effect.
However, if I enable innerHTMLTemplates, then it seems like I don't need to use IonicSafeString to bypass Ionic's sanitizer. It is bypassed by default.
When innerHTMLTemplatesEnabled is true any content passed to the message property will be interpreted as HTML.
When innerHTMLTemplatesEnabled is false any content passed to the message property will be interpreted as a string.
The sanitizer in this case only applies when innerHTMLTemplatesEnabled is true since content is interpreted as custom HTML. Using IonicSafeString lets you bypass the sanitizer step, but content will still be interpreted as HTML.
Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of Ionic, please create a new issue and ensure the template is fully filled out.
Activity
liamdebeasi commentedon Apr 5, 2023
Thanks for the report. The problem here is you do not have
innerHTMLTemplatesEnabledenabled in your Ionic config. In Ionic 7 we disabled the custominnerHTMLfunctionality by default, so developers need to first enable this functionality before they can use it. This is noted in the Ionic 7 Breaking Changes Guide.To fix this, set
innerHTMLTemplatesEnabled: trueinIonicModule.forRootinapp.module.ts. Adding this resolved the issue on my end. I am going to close this, but let me know if you have any questions.liamdebeasi commentedon Apr 5, 2023
Not a problem, and glad the issue is resolved!
bkleineibst commentedon Apr 11, 2023
The documentation is not really clear on this. It states that you can disable this on a case-by-case basis using IonicSafeString, after mentioning the global config option. (https://ionicframework.com/docs/techniques/security#bypassing-the-sanitizer-on-a-case-by-case-basis) However, as pointed out, this doesn't work.
Shouldn't this option still be available, as opposed to enabling it for all innerHTML, which is unsafe?
liamdebeasi commentedon Apr 11, 2023
IonicSafeStringonly has the desired result wheninnerHTMLTemplatesEnabled: truewhich was the default in Ionic v6. In other words, theinnerHTMLusage was enabled by default in Ionic v6 even if you were not passing in custom HTML or usingIonicSafeString.IonicSafeStringallows you to bypass Ionic's sanitizer to dangerously append custom HTML content usinginnerHTML. However, if thisinnerHTMLfunctionality is disabled thenIonicSafeStringwill have no effect.I updated the docs in ionic-team/ionic-docs#2906 to make this clearer.
bkleineibst commentedon Apr 11, 2023
Thanks @liamdebeasi.
However, if I enable innerHTMLTemplates, then it seems like I don't need to use IonicSafeString to bypass Ionic's sanitizer. It is bypassed by default.
liamdebeasi commentedon Apr 11, 2023
Do you have a reproduction case?
I am testing this with the latest Ionic 7 and an alert: https://codepen.io/liamdebeasi/pen/mdzerZe
The "script" tag is being removed when the alert is presented.
bkleineibst commentedon Apr 11, 2023
Maybe it is the case that the sanitizer is more selective.
I have a string with "
<br/>" in it. https://codepen.io/bkleineibst/pen/JjmYboqIf leaves the plain text if it innerHTMLTemplates is not enabled, and replaces with line breaks if it is.
liamdebeasi commentedon Apr 11, 2023
That is the intended behavior.
When
innerHTMLTemplatesEnabledistrueany content passed to themessageproperty will be interpreted as HTML.When
innerHTMLTemplatesEnabledisfalseany content passed to themessageproperty will be interpreted as a string.The sanitizer in this case only applies when
innerHTMLTemplatesEnabledistruesince content is interpreted as custom HTML. UsingIonicSafeStringlets you bypass the sanitizer step, but content will still be interpreted as HTML.ionitron-bot commentedon May 11, 2023
Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of Ionic, please create a new issue and ensure the template is fully filled out.