python / python/cpython

email.message.get_filename() returning not a filename

オープン
#101,932 コメント 3 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

docs stdlib topic-email type-bug
主要言語
Python
スター
77.2k
フォーク
36k
PR マージ指標
PR 指標を取得中

説明

Bug report

https://docs.python.org/3/library/cgi.html says that the cgi mudule is deprecated and "The FieldStorage class can typically be replaced with ... the email.message module or multipart for POST and PUT."

An HTML form with a file input field must be transmitted as multipart/form-data. Every input field corresponds to a part of that multipart content. The only way to distinguish a file input from another input is via the filename. A file input comes with a filename, while other inputs don't.

The obvious way to check the filename of a part is via the get_filename() function. But its documentation says: "If the header does not have a filename parameter, this method falls back to looking for the name parameter on the Content-Type header." That leaves the function unusable for checking whether a filename is set, because every HTML input field has a name attribute, and therefore the function returns a supposed filename for all input fields, no matter whether they are files or not.

As a workaround, the result of the get_content_disposition() function can be parsed for a filename attribute, but that's unnecessarily complicated and error-prone. That's not what I would expect from a suggested replacement for a module that didn't require any low-level hacking.

For those reasons, the get_filename() function should only return the filename, not the input name. Or the email.message module shouldn't be suggested as a replacement for the FieldStorage class.

Your environment

Currently working with python 3.9 on an Apache webserver

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

email.message モジュールと get_filename() のドキュメントから始め、filename パラメーターと name パラメーターの動作を cgi.FieldStorage の置き換えに関するガイダンスと比較します。filename がある multipart/form-data パートとない multipart/form-data パートについて期待される動作を確認し、その後、get_filename() が入力名をファイル名として返さないように、該当する動作とテストを更新します。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
backend
issue の種類
バグ
難易度
3/5
見積もり時間
1〜2日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
42/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。