منع حقن XML وLDAP
وسّع دفاعاتك ضد الحقن لتتجاوز SQL والأوامر وتشمل XML (XXE) وLDAP. تعلّم كيف تفسد المدخلات غير الموثوقة عمل هذه المفسرات وكيفية تحييدها.
منع حقن XML وLDAP درس مجاني في Secure Coding & OWASP Top 10 for Backend على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في 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 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
الأسئلة الشائعة
هل درس «منع حقن XML وLDAP» مجاني؟
نعم — نص درس «منع حقن XML وLDAP» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة 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 مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Secure Coding & OWASP Top 10 for Backend؟
لا تُشترط خبرة سابقة. Secure Coding & OWASP Top 10 for Backend على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.
كم من الوقت يستغرق درس «منع حقن XML وLDAP»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Secure Coding & OWASP Top 10 for Backend هذا؟
نعم. كل درس في Secure Coding & OWASP Top 10 for Backend يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.