Saltar al contenido
uencoder

URL Codificación vs HTML Codificación

Understand el difference entre percent-codificación para URLs y entity codificación para HTML, y cuando a usar cada.

问题

You son building un web página ese displays usuario entrada en both HTML content y un URL, y you necesitar a know que codificación a aplicar donde.

Requisitos previos

  • Basic HTML knowledge
  • Basic URL structure knowledge

Pasos

  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.

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

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

    entrada
    user input in both contexts
    输出
    no injection possible

Resultado esperado

Usuario entrada es safely rendered en both HTML content y URL attributes sin enabling XSS o breaking links.

Errores comunes

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.

Solución de problemas指引

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

Límites de seguridad

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

Herramientas relacionadas

参考链接