|
2 | 2 |
|
3 | 3 | import os, flask, xml.etree.ElementTree as ET
|
4 | 4 | from markupsafe import escape
|
| 5 | +from lxml import etree |
5 | 6 |
|
6 | 7 | app = flask.Flask(__name__)
|
7 | 8 |
|
8 | 9 | @app.route("/login", methods=["POST"])
|
9 | 10 | def login():
|
| 11 | + username = flask.request.form.get("user", "") |
10 | 12 | try:
|
11 |
| - tree = ET.parse("users.xml") |
| 13 | + tree = etree.parse("users.xml") |
12 | 14 | root = tree.getroot()
|
13 |
| - except Exception: |
14 |
| - return "Error reading users.xml" |
| 15 | + except Exception as e: |
| 16 | + return f"Error reading XML: {e}" |
15 | 17 |
|
16 |
| - username = flask.request.form.get("user", "") |
17 |
| - |
18 |
| - # XPATH INJECTION VULNERABILITY HERE ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓ |
19 |
| - user = root.find(f".//user[username='{username}']") # UNSAFE! |
20 |
| - |
21 |
| - if user is not None: |
22 |
| - teacher = user.findtext("is_teacher", default="no") |
23 |
| - if teacher in ("yes", "true", "1"): |
24 |
| - return f"<p>Logged in as a teacher!<br/>{open('/flag').read()}</p>" |
25 |
| - else: |
26 |
| - return "Logged in … but not as a teacher. <a href='/'>Try again?</a>" |
27 |
| - |
28 |
| - return "No such user… <a href='/'>Try again?</a>" |
| 18 | + query = f"//user[username/text()='{username}' and is_teacher/text()='yes']" |
| 19 | + try: |
| 20 | + results = root.xpath(query) |
| 21 | + except Exception as e: |
| 22 | + return f"XPath error: {e}" |
| 23 | + |
| 24 | + if results: |
| 25 | + return f"<p>Logged in as a teacher!<br/>{open('/flag').read()}</p>" |
| 26 | + else: |
| 27 | + return "Login failed. <a href='/'>Try again?</a>" |
29 | 28 |
|
30 | 29 | @app.route("/add", methods=["POST"])
|
31 | 30 | def add():
|
|
0 commit comments