बीटा Shoulder बीटा में है — परिणाम कभी-कभी गलत हो सकते हैं। आपकी प्रतिक्रिया तय करती है कि हम आगे क्या ठीक करें। प्रतिक्रिया साझा करें
📄

Improper Restriction of XML External Entity Reference

🛡️ 3 नियम इसे पहचानते हैं

Improper Restriction of XML External Entity Reference

The product processes an XML document that can contain XML entities with URIs that resolve to documents outside of the intended sphere of control, causing the product to embed incorrect documents into its output.

XML External Entity (XXE) attacks exploit features of XML parsers to read local files, perform server-side request forgery, or cause denial of service.

व्यापकता
मध्यम
3 भाषाएँ कवर की गईं
प्रभाव
उच्च
3 उच्च गंभीरता वाले नियम
रोकथाम
प्रलेखित
3 फिक्स उदाहरण
2 रोकथाम
2 रोकथाम

इस भेद्यता को कैसे ठीक करें

3 Shoulder डिटेक्शन नियमों पर आधारित XML External Entity (XXE) के लिए रोकथाम रणनीतियाँ।

XML External Entity (XXE) Injection HIGH

Go's encoding/xml is safe by default; reject XML with DOCTYPE declarations as defense in depth

+28 -15 go
  package main
  
  import (
-     "encoding/xml"
-     "io/ioutil"
-     "net/http"
- )
- 
- type Data struct {
-     XMLName xml.Name `xml:"data"`
-     Value   string   `xml:"value"`
- }
- 
- func handler(w http.ResponseWriter, r *http.Request) {
-     body, _ := ioutil.ReadAll(r.Body)
-     // Potentially vulnerable: parsing untrusted XML without DOCTYPE check
-     var data Data
-     xml.Unmarshal(body, &data)
+     "bytes"
+     "encoding/xml"
+     "errors"
+     "io/ioutil"
+     "net/http"
+ )
+ 
+ type Data struct {
+     XMLName xml.Name `xml:"data"`
+     Value   string   `xml:"value"`
+ }
+ 
+ func safeXMLUnmarshal(body []byte, v interface{}) error {
+     // Defense in depth: reject XML with DOCTYPE declarations
+     if bytes.Contains(body, []byte("<!DOCTYPE")) ||
+         bytes.Contains(body, []byte("<!ENTITY")) {
+         return errors.New("DOCTYPE/ENTITY declarations not allowed")
+     }
+     return xml.Unmarshal(body, v)
+ }
+ 
+ func handler(w http.ResponseWriter, r *http.Request) {
+     body, _ := ioutil.ReadAll(r.Body)
+     var data Data
+     if err := safeXMLUnmarshal(body, &data); err != nil {
+         http.Error(w, "Invalid XML", 400)
+         return
+     }
      w.Write([]byte(data.Value))
  }
  
XML External Entity (XXE) Injection HIGH

Disable external entity processing in XML parsers or use JSON instead of XML

+15 -7 javascript
  const express = require('express');
- const libxmljs = require('libxmljs');
- const app = express();
- 
- app.post('/parse', (req, res) => {
-   const xmlContent = req.body.xml;
-   const doc = libxmljs.parseXml(xmlContent);
-   res.json({ root: doc.root().name() });
+ const { XMLParser } = require('fast-xml-parser');
+ const app = express();
+ 
+ const parser = new XMLParser({
+   processEntities: false,
+   allowBooleanAttributes: true,
+ });
+ 
+ app.post('/parse', (req, res) => {
+   try {
+     const result = parser.parse(req.body.xml);
+     res.json(result);
+   } catch (e) {
+     res.status(400).json({ error: 'Invalid XML' });
+   }
  });
  
XML External Entity (XXE) Injection HIGH

Use defusedxml instead of standard XML parsers for untrusted input

+10 -7 python
- from lxml import etree
- from flask import request
- 
- @app.route('/api/xml', methods=['POST'])
- def parse_xml():
-     root = etree.fromstring(request.data)
-     return {'name': root.find('name').text}
+ import defusedxml.ElementTree as ET
+ from flask import request, jsonify
+ 
+ @app.route('/api/xml', methods=['POST'])
+ def parse_xml():
+     try:
+         root = ET.fromstring(request.data)
+         return jsonify({'name': root.find('name').text})
+     except ET.ParseError:
+         return jsonify({'error': 'Invalid XML'}), 400
  

मुख्य अभ्यास

  • Use denial of service
3 पहचान
3 पहचान

अपने कोड में भेद्यताएँ खोजें

Improper Restriction of XML External Entity Reference पैटर्न के लिए अपने कोडबेस को स्कैन करने के लिए Shoulder का उपयोग करें। 3 नियम.

टर्मिनल
# Scan with Shoulder CLI
npx @shoulderdev/cli trust --cwe=611

# Or scan entire project
npx @shoulderdev/cli trust .
4 चेतावनी संकेत
4 चेतावनी संकेत

कोड समीक्षा में किन बातों पर ध्यान दें

ये पैटर्न संभावित Improper Restriction of XML External Entity Reference भेद्यताओं का संकेत देते हैं। कोड समीक्षा और सुरक्षा ऑडिट के दौरान इन्हें देखें।

🟠
unsafe XML parsing that could allow XML External Entity (XXE) attacks javascript-xxe
🟠
XML parsing with external entity processing enabled python-xxe
🔍

अपने कोडबेस को इसके लिए स्कैन करें: Improper Restriction of XML External Entity Reference

Shoulder CLI आपके पूरे कोडबेस में भेद्य पैटर्न खोजता है।