Skip to content

security: prevent path traversal in LOAD DATA LOCAL INFILE#1247

Closed
dfgvaetyj3456356-hash wants to merge 1 commit into
PyMySQL:mainfrom
dfgvaetyj3456356-hash:security/fix-load-local-infile-path-traversal
Closed

security: prevent path traversal in LOAD DATA LOCAL INFILE#1247
dfgvaetyj3456356-hash wants to merge 1 commit into
PyMySQL:mainfrom
dfgvaetyj3456356-hash:security/fix-load-local-infile-path-traversal

Conversation

@dfgvaetyj3456356-hash

Copy link
Copy Markdown

This PR fixes a path traversal vulnerability in LOAD DATA LOCAL INFILE handling.

CWE: CWE-22 (Path Traversal)
File: pymysql/connections.py

When local_infile=True is set on a connection, a malicious or compromised MySQL server can send a LOAD LOCAL INFILE request packet containing an arbitrary filename (e.g., ../../../etc/passwd). The client blindly opens the file and streams its contents back to the server.

Fix:

  • In LoadLocalFile.init(), resolve the server-provided filename to an absolute path using os.path.realpath()
  • Reject filenames that fall outside the current working directory
  • Uses the existing CR_LOAD_DATA_LOCAL_INFILE_REALPATH_FAIL error code (which was defined but never referenced in the codebase)
  • Added tests verifying path traversal and absolute path rejection

Note: The constant CR_LOAD_DATA_LOCAL_INFILE_REALPATH_FAIL = 2069 was already defined in pymysql/constants/CR.py, strongly suggesting that realpath validation was planned but never implemented. This PR completes that security hardening.

@methane

methane commented May 29, 2026

Copy link
Copy Markdown
Member

ja: まず初めに、信頼できないMySQLサーバーに接続するべきではありません。また、 local_infile=Fase を使用すれば、MySQLサーバーがローカルファイルを読み込むことを防止できます。
en: First of all, you should not connect to an untrusted MySQL server. Also, using local_infile=Fase can prevent the MySQL server from loading local files.

ja: LOAD LOCAL INFILE をより安全にする改良を考える余地はありますが、提案された設計は良くありません。データファイルがカレントディレクトリの外にある場合に後方互換性がなくなりますし、カレントディレクトリの下に盗まれたら問題のあるファイルが存在しないという保証もないからです。
en: There is room for improvement to make LOAD LOCAL INFILE more secure, but the proposed design is not good. It would break backward compatibility when the data file is outside the current directory, and there is no guarantee that a problematic file won't exist if it is stolen under the current directory.

ja: 簡単かつ高価的な対策として、 args に指定された文字列以外を対象とした LOAD LOCAL パケットをエラーにするアイデアがあります。後方互換性のために、この動作はデフォルトではなく load_infile="only_args" のようなオプションで有効にすることができるでしょう。
en: As a simple and effective countermeasure, there is an idea to make LOAD LOCAL packets that target anything other than the string specified in args an error. For backward compatibility, this behavior could be enabled with an option like load_infile="only_args" rather than being the default.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants