Prevenção de injeção em XML e LDAP
Amplie a defesa contra injeção para além de SQL e comandos, alcançando XML (XXE) e LDAP. Aprenda como entradas não confiáveis corrompem esses interpretadores e como neutralizá-las.
Prevenção de injeção em XML e LDAP é uma aula grátis de Secure Coding & OWASP Top 10 for Backend no CoddyKit. Esta é a aula 4 de 4. Você pode ler a aula completa abaixo gratuitamente — depois pratica ao vivo no navegador com um editor de código integrado e um tutor de IA 24/7. Faz parte do caminho de aprendizado de Secure Coding & OWASP Top 10 for Backend, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de Secure Coding & OWASP Top 10 for Backend inclui 4 aulas no total.
Partes desta aula ainda não foram traduzidas e aparecem em 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
Perguntas Frequentes
A aula “Prevenção de injeção em XML e LDAP” é grátis?
Sim — o texto completo de “Prevenção de injeção em XML e LDAP” é grátis para ler aqui na web. Para praticá-la interativamente (um editor de código integrado e um tutor de IA 24/7) e desbloquear o restante do curso de Secure Coding & OWASP Top 10 for Backend, atualize para CoddyKit PRO. O curso de Secure Coding & OWASP Top 10 for Backend inclui 4 aulas no total.
O que vou aprender em “Prevenção de injeção em XML e LDAP”?
Amplie a defesa contra injeção para além de SQL e comandos, alcançando XML (XXE) e LDAP. Aprenda como entradas não confiáveis corrompem esses interpretadores e como neutralizá-las. Você pratica Secure Coding & OWASP Top 10 for Backend com código prático que executa diretamente no navegador, e um tutor de IA 24/7 responde suas dúvidas enquanto trabalha na aula.
Preciso ter experiência prévia para começar Secure Coding & OWASP Top 10 for Backend?
Nenhuma experiência prévia é necessária. Secure Coding & OWASP Top 10 for Backend no CoddyKit é estruturado para alunos iniciantes até avançados, então você pode começar aqui ou desde o início e aprender no seu ritmo. Esta é a aula 4 de 4.
Quanto tempo leva a aula “Prevenção de injeção em XML e LDAP”?
A maioria das aulas CoddyKit leva cerca de 5–10 minutos. Cada uma é compacta e interativa, então você faz progresso constante e retoma exatamente de onde parou entre web e app.
Posso escrever e executar código nesta aula de Secure Coding & OWASP Top 10 for Backend?
Sim. Cada aula de Secure Coding & OWASP Top 10 for Backend inclui um editor de código integrado, então você escreve e executa código real direto no navegador e recebe feedback de IA instantaneamente — nenhuma configuração local necessária.
Todas as aulas deste curso
- Prevenção de injeção de SQL
- Injeção de comandos e código
- Cross-Site Scripting (XSS) no back-end
- Prevenção de injeção em XML e LDAP