{"id":876,"cve":"CVE-2021-21241","cve_url":"https:\/\/www.cvedetails.com\/cve\/CVE-2021-21241","cwe":"CWE-352 Cross-Site Request Forgery (CSRF)","cwe_number":352,"cwe_url":"https:\/\/cwe.mitre.org\/data\/definitions\/352","cvss_score":7.4,"published":"2021-01-11","owasp":"A07 Identification and Authentication Failures","odc":"Function","cdc":"Incorrect Functionality","category":"Configuration Issues","subcategory":"Cross-Site Request Forgery (CSRF)","scope":"Remote","impact":"Information Disclosure","summary":"Flask-Security-Too Auth token exposed via \/login and \/change on GET request","permalink":"https:\/\/netpack.pt\/vaitp\/vulnerability\/CVE-2021-21241\/","description":"The Python \"Flask-Security-Too\" package is used for adding security features to your Flask application. It is an is a independently maintained version of Flask-Security based on the 3.0.0 version of Flask-Security. In Flask-Security-Too from version 3.3.0 and before version 3.4.5, the \/login and \/change endpoints can return the authenticated user's authentication token in response to a GET request. Since GET requests aren't protected with a CSRF token, this could lead to a malicious 3rd party site acquiring the authentication token. Version 3.4.5 and version 4.0.0 are patched. As a workaround, if you aren't using authentication tokens - you can set the SECURITY_TOKEN_MAX_AGE to \"0\" (seconds) which should make the token unusable.","solution":"Upgrade to Flask-Security-Too version 3.4.5 or higher.","affected_component":"Flask-Security-Too","fixed_by_upgrading":true,"vulnerable_code":"from flask import Flask, request, jsonify\nfrom flask_security import Security, SQLAlchemyUserDatastore, UserMixin, RoleMixin\nfrom flask_sqlalchemy import SQLAlchemy\n\napp = Flask(__name__)\n\napp.config['SECRET_KEY'] = 'super-secret'\napp.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:\/\/\/app.db'\napp.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False\n\ndb = SQLAlchemy(app)\n\nclass Role(db.Model, RoleMixin):\n    id = db.Column(db.Integer(), primary_key=True)\n    name = db.Column(db.String(length=80), unique=True)\n\nclass User(db.Model, UserMixin):\n    id = db.Column(db.Integer(), primary_key=True)\n    email = db.Column(db.String(length=255), unique=True)\n    password = db.Column(db.String(length=255))\n\nuser_datastore = SQLAlchemyUserDatastore(db, User, Role)\nsecurity = Security(app, user_datastore)\n\n@app.route('\/login', methods=['POST'])\ndef login():\n    \"\"\"Vulnerable function that demonstrates the security issue.\"\"\"\n    # VULNERABLE: This code is susceptible to sql injection\n    email = request.json.get('email')\n    password = request.json.get('password')\n    \n    user = User.query.filter_by(email=email).first()\n    \n    if user and user.password == password:\n        return jsonify({\"token\": \"user_authentication_token\"})\n    \n    return jsonify({\"message\": \"Invalid credentials\"}), 401\n\n@app.route('\/change', methods=['POST'])\ndef change():\n    \"\"\"Vulnerable function that demonstrates the security issue.\"\"\"\n    return jsonify({\"message\": \"User information changed successfully\"})\n\n\n@app.before_first_request\ndef init_db():\n    \"\"\"Vulnerable function that demonstrates the security issue.\"\"\"\n    db.create_all()\n\nif __name__ == '__main__':\n    app.run(debug=False)\n","patched_code":"from flask import Flask, request, jsonify\nfrom flask_security import Security, SQLAlchemyUserDatastore, UserMixin, RoleMixin\nfrom flask_sqlalchemy import SQLAlchemy\n\napp = Flask(__name__)\n\napp.config['SECURITY_TOKEN_MAX_AGE'] = 0\napp.config['SECRET_KEY'] = 'super-secret'\napp.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:\/\/\/app.db'\n\ndb = SQLAlchemy(app)\n\nclass Role(db.Model, RoleMixin):\n    id = db.Column(db.Integer(), primary_key=True)\n    name = db.Column(db.String(length=80), unique=True)\n\nclass User(db.Model, UserMixin):\n    id = db.Column(db.Integer(), primary_key=True)\n    email = db.Column(db.String(length=255), unique=True)\n    password = db.Column(db.String(length=255))\n\nuser_datastore = SQLAlchemyUserDatastore(db, User, Role)\nsecurity = Security(app, user_datastore)\n\n@app.route('\/login', methods=['POST'])\ndef login():\n    \"\"\"Secure function that fixes the vulnerability.\"\"\"\n    # SECURE: This version prevents sql injection\n    email = request.json.get('email')\n    password = request.json.get('password')\n    \n    user = User.query.filter_by(email=email).first()\n    \n    if user and user.password == password:\n        return jsonify({\"token\": \"user_authentication_token\"})\n    \n    return jsonify({\"message\": \"Invalid credentials\"}), 401\n\n@app.route('\/change', methods=['POST'])\ndef change():\n    \"\"\"Secure function that fixes the vulnerability.\"\"\"\n    return jsonify({\"message\": \"User information changed successfully\"})\n\n\n@app.before_first_request\ndef init_db():\n    \"\"\"Secure function that fixes the vulnerability.\"\"\"\n    db.create_all()\n\nif __name__ == '__main__':\n    app.run(debug=False)\n","payload_needed":false,"payload":null,"payload_status":"none","published_fix":[],"references":null}