Aller au contenu
uencoder

URL Codage vs HTML Codage

Understand le difference entre percent-codage pour URLs et entity codage pour HTML, et quand en utiliser chaque.

问题

You sont building un web page ce displays utilisateur entrée en both HTML content et un URL, et you besoin en know qui codage en appliquer où.

Prérequis

  • Basic HTML knowledge
  • Basic URL structure knowledge

Étapes

  1. 1. Understand the threat contexts

    URL encoding (percent-encoding) protects query strings from misinterpretation. HTML encoding (entity encoding) protects page content from XSS by neutralizing characters like <, >, and &. They solve different problems in different contexts.

    entrée
    <script>alert(1)</script>
    输出
    HTML: &lt;script&gt;alert(1)&lt;/script&gt;
  2. 2. Apply the correct encoding per context

    When placing user data inside HTML text, use HTML entity encoding. When placing user data inside a URL attribute (href, src), use URL encoding. For data inside a JavaScript string literal, use JavaScript string escaping.

    entrée
    O'Brien
    输出
    HTML: O&#39;Brien | URL: O%27Brien
  3. 3. Test both paths

    Render the page and inspect the DOM. Confirm that no raw < or > appears in HTML and that the URL resolves correctly. Automated scanners can verify that no XSS vector remains.

    entrée
    user input in both contexts
    输出
    no injection possible

Résultat attendu

Utilisateur entrée est safely rendered en both HTML content et URL attributes sans enabling XSS ou breaking links.

Erreurs courantes

Using URL encoding for HTML content
URL encoding does not neutralize < and > for HTML. Always use HTML entity encoding for content displayed in the page.
Forgetting to encode the ampersand in HTML attributes
Encode & as &amp; in HTML attribute values to avoid confusing the parser with entity references.

Dépannage指引

  • If the page shows literal &lt; instead of <, you double-encoded. Decode once and re-encode only for the correct context.
  • Use a framework's built-in escaping rather than manual encoding to avoid context mismatches.

Limites de sécurité

  • Encoding is context-specific. Applying the wrong encoding leaves XSS or injection vulnerabilities open.

Outils associés

参考链接