Skip to content

feat: sanitizer bypass method #18277

Closed
Closed

Description

@liamdebeasi

Feature Request

Ionic version:

[x] 4.x

Describe the Feature Request

We recently introduced a basic sanitizer which should help Ionic users avoid unintentional XSS vulnerabilities in their applications. Similar to Angular's sanitizer, there exists methods to bypass the sanitize method if a user wants to do something particularly complex that the sanitizer would typically not allow.

Because our sanitizer does not allow every possible combination of markup + styling, it would be good to give users the option to bypass the sanitizer in the event that a) they are certain there are no XSS vulnerabilities (or at least, they know they are responsible for ensuring their code is safe) and b) want to do something complex that the sanitizer can't reliably make sanitary.

Describe Preferred Solution

I would expect the sanitizer to be bypassed if it received an IonicSafeCode as input. Here is how it would look for ion-alert:

const alert = await alertController.create({
  header: 'Alert',
  subHeader: 'Subtitle',
  message: new IonicSafeCode('<a href="https://ionicframework.com" target="_blank">Hello World!</a>')
  buttons: ['Cancel', 'Open Modal', 'Delete']
});

Then the sanitizer method would need to check to see if the content it is receiving is of type IonicSafeCode and allow it to pass through.

(I'm open to other class names. IonicSafeCode was the first one that came to mind)

Additional Context

See: https://angular.io/api/platform-browser/DomSanitizer

Activity

  1. added
    package: core
    type: feature request
    on May 14, 2019
  2. self-assigned this
    on May 14, 2019
  3. miqmago commented on May 15, 2019

    @miqmago

    Coool feature!! Maybe there is something I can't see right now, but why not allowing SafeHtml as type? So it would be easy to be achieved with something like this:

    const alert = await alertController.create({
      header: 'Alert',
      subHeader: 'Subtitle',
      message: this.domSanitizer.bypassSecurityTrustHtml('<ion-icon name="warning"></ion-icon></div>'),
      buttons: ['Cancel', 'Open Modal', 'Delete']
    });

    Also found some customer asking for icons in header, so wouldn't it be nice to have also in header and subHeader?

    Finally I don't know if it's related, will it be available for toasts too?

  4. liamdebeasi commented on May 15, 2019

    @liamdebeasi
    ContributorAuthor

    this.domSanitizer.bypassSecurityTrustHtml is something that is Angular-specific and will not work with other frameworks. There is likely some research to be done into how IonicSafeCode will interact with Angular's own sanitizer.

    Regarding the icons in header, this discussion is best suited for a separate issue.

    There is already a issue open for HTML content with toasts: #18204

  5. cumpstey commented on Jan 22, 2020

    @cumpstey

    I spent a while trying to debug before I eventually came across this ticket.

    I found that html in the message property wasn't sanitised in 4.0.0-beta7, and was in 4.11.7.

    I need to use html with inline styles in the message property, as I'm showing KaTeX markup in the alert and that includes inline styles for element positioning. My fairly fragile workaround, while waiting for a better solution as above, is direct manipulation of the dom. I hope it's useful for anyone else who finds this ticket while waiting for the better solution described above.

    this.alertController.create({
      header: 'My header',
      message: '',
    })
    .then(alert => {
      alert.getElementsByClassName('alert-message').item(0).innerHTML = '<p style="color:red;">My message</p>';
      return alert.present();
    });
    
  6. liamdebeasi commented on Feb 12, 2020

    @liamdebeasi
    ContributorAuthor

    Hi everyone,

    I am working on this feature and would love some feedback. Here is a dev build you can use to try it out: 5.0.0-dev.202002121951.779ed34.

    npm i @ionic/[email protected]
    npm i @ionic/[email protected]

    Usage on a per-instance basis:

    Angular:

    import { IonicSafeString } from '@ionic/angular';
    
    ...
    
    async openAlert() {
      const alert = await this.alertController.create({
          header: 'Alert',
          subHeader: 'Subtitle',
          message: new IonicSafeString('<img src="x" onerror="alert("Hello!");">'),
          buttons: ['OK']
    });
    
        await alert.present();
      }

    React:

    import { IonicSafeString } from '@ionic/react';
    
    ...
    
    const Home: React.FC = () => {
      const [showAlert1, setShowAlert1] = useState(false);
      
      return (
        <IonPage>
          <IonHeader>
            <IonToolbar>
              <IonTitle>Blank</IonTitle>
            </IonToolbar>
          </IonHeader>
          <IonContent>
            <IonButton onClick={() => setShowAlert1(true)} expand="block">Show Alert 1</IonButton>
            <IonAlert
              isOpen={showAlert1}
              onDidDismiss={() => setShowAlert1(false)}
              header={'Alert'}
              subHeader={'Subtitle'}
              message={new IonicSafeString('<img src="x" onerror="alert(document.cookie);">')}
              buttons={['OK']}
            />
          </IonContent>
        </IonPage>
      );
    };

    Disabling sanitizing across your entire app:

    Angular
    app.module.ts

    ...
    IonicModule.forRoot({
      sanitizerEnabled: false
    })
    ...

    React
    App.tsx

    import { setupConfig } from '@ionic/react';
    
    setupConfig({
      sanitizerEnabled: false
    });
  7. added this to the 5.1.0 milestone on Feb 24, 2020
  8. liamdebeasi commented on Apr 27, 2020

    @liamdebeasi
    ContributorAuthor

    This has been resolved via #20457 and will be in an upcoming release of Ionic Framework.

  9. ionitron-bot commented on May 27, 2020

    @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.

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

Metadata

Metadata

Assignees

Labels

package: coretype: feature request

Type

No type

Fields

No fields configured for issues without a type.

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions