XPath Injection
Concept
Vulnerable Scenario
Example Code (Vulnerable)
from flask import Flask, request
from lxml import etree
app = Flask(__name__)
@app.route('/search', methods=['GET'])
def search_products():
category = request.args.get('category')
xml_file = 'products.xml'
# Load the XML file
tree = etree.parse(xml_file)
# Construct the XPath query
query = f"//product[category='{category}']"
# Execute the XPath query
results = tree.xpath(query)
# Process the results
products = []
for product in results:
name = product.find('name').text
price = product.find('price').text
products.append({'name': name, 'price': price})
return {'products': products}Explanation
Prevention
Example Code (Secure)
Conclusion
Semgrep Rule
Last updated