0Pricing
Secure Coding & OWASP Top 10 for Backend · 课时

防止 XML 与 LDAP 注入

将注入防护从 SQL 和命令扩展到 XML(XXE)与 LDAP。学习不可信输入如何破坏这些解释器,以及如何将其安全中和。

防止 XML 与 LDAP 注入 是 CoddyKit 上的免费 Secure Coding & OWASP Top 10 for Backend 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Secure Coding & OWASP Top 10 for Backend 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Secure Coding & OWASP Top 10 for Backend 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

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 out

Use 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

常见问题解答

「防止 XML 与 LDAP 注入」课时是免费的吗?

是的 — 「防止 XML 与 LDAP 注入」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Secure Coding & OWASP Top 10 for Backend 课程的其余内容,请升级到 CoddyKit PRO。 Secure Coding & OWASP Top 10 for Backend 课程共包含 4 节课。

「防止 XML 与 LDAP 注入」这节课中我会学到什么?

将注入防护从 SQL 和命令扩展到 XML(XXE)与 LDAP。学习不可信输入如何破坏这些解释器,以及如何将其安全中和。 你通过在浏览器中直接运行的动手代码来练习 Secure Coding & OWASP Top 10 for Backend,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Secure Coding & OWASP Top 10 for Backend 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Secure Coding & OWASP Top 10 for Backend 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「防止 XML 与 LDAP 注入」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Secure Coding & OWASP Top 10 for Backend 课中编写并运行代码吗?

能。每节 Secure Coding & OWASP Top 10 for Backend 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 防止 SQL 注入
  2. 命令与代码注入
  3. 后端中的跨站脚本攻击(XSS)
  4. 防止 XML 与 LDAP 注入
← 返回 Secure Coding & OWASP Top 10 for Backend