Prevención de inyección XML y LDAP
Amplíe la defensa contra inyecciones más allá de SQL y los comandos, hasta XML (XXE) y LDAP. Aprenda cómo las entradas no confiables corrompen estos intérpretes y cómo neutralizarlas.
Prevención de inyección XML y LDAP es una lección gratuita de Secure Coding & OWASP Top 10 for Backend en CoddyKit. Esta es la lección 4 de 4. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de Secure Coding & OWASP Top 10 for Backend, y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de Secure Coding & OWASP Top 10 for Backend incluye 4 lecciones en total.
Partes de esta lección aún no han sido traducidas y se muestran en inglés.
Injection Beyond SQL
You have seen SQL, command, and XSS injection. Any interpreter that mixes untrusted input with structure is at risk. Two often-missed backend targets are XML parsers and LDAP directories.
What Is XXE
XML External Entity (XXE) injection abuses XML parsers that resolve external entities. An attacker defines an entity that reads a local file or hits an internal URL.
<!DOCTYPE x [ <!ENTITY xxe SYSTEM "file:///etc/passwd"> ]>
<data>&xxe;</data>What XXE Can Do
- Read sensitive files from the server
- Perform server-side request forgery to internal services
- Cause denial of service via entity expansion
All from a parser feature most apps never need.
Disable External Entities
The core fix is to configure the parser to not resolve external entities or DTDs. This single setting closes XXE entirely.
factory.setFeature('http://apache.org/xml/features/disallow-doctype-decl', true);
factory.setExpandEntityReferences(false);Prefer Safer Formats
Where possible, accept JSON instead of XML — it has no entity concept and no equivalent attack. If you must take XML, lock the parser down first.
What Is LDAP Injection
LDAP directories are queried with filter strings. If user input is concatenated into a filter, an attacker can alter the query logic — the LDAP analog of SQL injection.
// vulnerable: input goes straight into the filter
filter = '(uid=' + username + ')'An LDAP Bypass
Submitting * or admin)(&) as a username can turn a precise filter into one that matches many entries or always succeeds, bypassing authentication.
Escape LDAP Special Characters
Neutralize the special characters * ( ) \ NUL by escaping them before they enter a filter.
def escape(s):
out = ''
for ch in s:
if ch in '*()\\':
out += '\\' + format(ord(ch), '02x')
else:
out += ch
return outUse Parameterized APIs
Best of all, use directory APIs that accept inputs as parameters rather than building filter strings by hand — the same principle that makes prepared SQL statements safe.
The Common Defense
Across SQL, command, XML, and LDAP the rule is identical: never let untrusted input change the structure of a query or document. Separate code from data with parameterization, escaping, or safe parser config.
Allow-List Validation
Add a layer of defense by validating input against an allow-list of acceptable values before it reaches any interpreter. Rejecting unexpected characters or formats early shrinks the attack surface for every injection class at once.
import re
if not re.fullmatch(r'[a-zA-Z0-9_.-]+', username):
raise ValueError('invalid username')Quick Check
Test your injection defense.
Recap
You extended injection defense to XML and LDAP:
- XXE abuses external entities — disable DTDs and entity resolution
- LDAP injection manipulates filters — escape special characters or use parameterized APIs
- The universal rule: keep untrusted data out of structure
Preguntas frecuentes
¿La lección «Prevención de inyección XML y LDAP» es gratis?
Sí — el texto completo de «Prevención de inyección XML y LDAP» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de Secure Coding & OWASP Top 10 for Backend, actualiza a CoddyKit PRO. El curso de Secure Coding & OWASP Top 10 for Backend incluye 4 lecciones en total.
¿Qué aprenderé en «Prevención de inyección XML y LDAP»?
Amplíe la defensa contra inyecciones más allá de SQL y los comandos, hasta XML (XXE) y LDAP. Aprenda cómo las entradas no confiables corrompen estos intérpretes y cómo neutralizarlas. Practicas Secure Coding & OWASP Top 10 for Backend con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.
¿Necesito experiencia previa para empezar Secure Coding & OWASP Top 10 for Backend?
No se requiere experiencia previa. Secure Coding & OWASP Top 10 for Backend en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 4 de 4.
¿Cuánto tiempo toma la lección «Prevención de inyección XML y LDAP»?
La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.
¿Puedo escribir y ejecutar código en esta lección de Secure Coding & OWASP Top 10 for Backend?
Sí. Cada lección de Secure Coding & OWASP Top 10 for Backend incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.
Todas las lecciones de este curso
- Prevención de la inyección SQL
- Inyección de comandos y código
- Cross-Site Scripting (XSS) en el backend
- Prevención de inyección XML y LDAP