0Pricing
Flask Academy · Lezione

Autoescaping e filtro safe

Mantenga la sicurezza XSS e disabiliti la protezione solo quando è certo.

Autoescaping e filtro safe è una lezione Flask Academy gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Flask Academy, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Flask Academy include 4 lezioni in totale.

Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.

The Danger of Raw HTML

If you print user text straight into a page, a sneaky input could inject scripts. This attack is called XSS, and Flask defends against it.

Autoescaping Is On

By default Jinja2 turns on autoescaping for HTML templates. Dangerous characters get neutralized before they reach the browser.

What Escaping Does

Escaping converts characters like the less-than sign into safe entities. The browser then shows them as text, never as live markup.

<b>hi</b>  becomes  &lt;b&gt;hi&lt;/b&gt;

Why Text Stays Text

Because tags are escaped, a comment like a fake bold tag shows up as plain text. The page renders it literally instead of running it.

When You Trust the HTML

Sometimes you truly want HTML rendered, like a value you built yourself. For that you can opt out with the safe filter.

{{ trusted_html|safe }}

How safe Works

The safe filter tells Jinja2 this value is already trusted, so do not escape it. The markup then renders as real HTML.

Only on Trusted Data

Use safe only on content you control. Marking user input safe reopens the very XSS hole autoescaping was closing.

Marking Safe in Python

You can also flag trusted HTML in your view with Markup. A Markup string skips escaping automatically when rendered.

from markupsafe import Markup
snippet = Markup('<b>Hi</b>')

Disabling Escaping Locally

To skip escaping for a whole region, wrap it in an autoescape block set to false. Use this rarely and with great care.

{% autoescape false %}{{ html }}{% endautoescape %}

The escape Filter

You can also force escaping with the escape filter, or its short alias e. It is handy when autoescaping is off for a block.

{{ value|e }}

Safe by Default

The big idea: Flask is secure out of the box. You only relax escaping deliberately, never by accident, which keeps users protected.

Quick Check

You print untrusted user input in a template. What does autoescaping do with HTML tags in it?

Recap

You learned Flask autoescapes output to block XSS, and that the safe filter opts out only for HTML you fully trust. Stay secure! 🔒

Domande Frequenti

La lezione «Autoescaping e filtro safe» è gratuita?

Sì — il testo completo di «Autoescaping e filtro safe» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Flask Academy, passa a CoddyKit PRO. Il corso Flask Academy include 4 lezioni in totale.

Cosa imparerò in «Autoescaping e filtro safe»?

Mantenga la sicurezza XSS e disabiliti la protezione solo quando è certo. Eserciti Flask Academy con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.

Ho bisogno di esperienza per iniziare Flask Academy?

Non è richiesta alcuna esperienza precedente. Flask Academy su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.

Quanto tempo richiede la lezione «Autoescaping e filtro safe»?

La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.

Posso scrivere ed eseguire codice in questa lezione Flask Academy?

Sì. Ogni lezione Flask Academy include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.

Tutte le lezioni di questo corso

  1. render_template e la cartella templates
  2. Passare dati a un template
  3. Cicli e condizioni in Jinja2
  4. Autoescaping e filtro safe
← Torna a Flask Academy