Skip to content

bug: IonToast with IonicSafeString display 'undefined' #27118

Closed

Description

Prerequisites

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

  1. Create an IonToast with the use of ToastController
  2. In message property set a IonicSafeString with 'Hello!'
  3. Present the IonToast and see the message 'undefined' in it

Code Reproduction URL

https://stackblitz.com/edit/angular-sxv7cl?file=src/app/example.component.ts

Ionic Info

Ionic:

Ionic CLI : 7.0.0
Ionic Framework : not installed
@angular-devkit/build-angular : 15.2.4
@angular-devkit/schematics : 15.2.4
@angular/cli : 15.2.4
@ionic/angular-toolkit : 7.0.0

Capacitor:

Capacitor CLI : 4.6.2
@capacitor/android : not installed
@capacitor/core : 4.6.2
@capacitor/ios : not installed

Utility:

cordova-res : not installed globally
native-run : 1.7.2

System:

NodeJS : v16.20.0
npm : 8.19.4
OS : Windows 10

Additional Information

With previous versions of Ionic (for example 6) the issue does not occur

Activity

  1. self-assigned this
    on Apr 5, 2023
  2. liamdebeasi commented on Apr 5, 2023

    @liamdebeasi
    Contributor

    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.

  3. removed their assignment
    on Apr 5, 2023
  4. ghost closed this as completedon Apr 5, 2023
  5. liamdebeasi commented on Apr 5, 2023

    @liamdebeasi
    Contributor

    Not a problem, and glad the issue is resolved!

  6. bkleineibst commented on Apr 11, 2023

    @bkleineibst

    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?

  7. liamdebeasi commented on Apr 11, 2023

    @liamdebeasi
    Contributor

    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.

    I updated the docs in ionic-team/ionic-docs#2906 to make this clearer.

  8. bkleineibst commented on Apr 11, 2023

    @bkleineibst

    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.

  9. liamdebeasi commented on Apr 11, 2023

    @liamdebeasi
    Contributor

    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.

  10. bkleineibst commented on Apr 11, 2023

    @bkleineibst

    Maybe it is the case that the sanitizer is more selective.

    I have a string with "<br/>" in it. https://codepen.io/bkleineibst/pen/JjmYboq

    If leaves the plain text if it innerHTMLTemplates is not enabled, and replaces with line breaks if it is.

  11. liamdebeasi commented on Apr 11, 2023

    @liamdebeasi
    Contributor

    That is the intended behavior.

    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.

  12. ionitron-bot commented on May 11, 2023

    @ionitron-bot

    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.

  13. locked and limited conversation to collaborators on May 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions