6

ローカル SQL Express データベースの情報を更新するために作成したこの HTTP ハンドラーがあります。

ユーザーが相対 URI パス "/../../file.zip" をクエリ文字列として使用することが可能であり、制限された領域外のファイルをダウンロードできることに気付きました。

サイトはまだ公開されていないので、現時点ではセキュリティ上の問題ではありませんが、このようなことは本当に防止したいと考えています。

入力クエリから「..」を削除する単純な string.replace 行を追加しました。

これを確保するためにここで他にすべきことはありますか?

public void ProcessRequest(HttpContext context)
{
    string filesPath = "C:/Downloads/";
    string fileName = context.Request.QueryString["filename"];
    fileName = fileName.Replace("'", "''").Replace("..", "").Replace("/", "").Replace("\\", "");

    if (!string.IsNullOrEmpty(fileName) && File.Exists(filesPath + fileName))
    {
        context.Response.ContentType = "application/octet-stream";
        context.Response.AddHeader("Content-Disposition", string.Format("attachment; filename=\"{0}\"", fileName));
        context.Response.WriteFile(filesPath + fileName);
        //Do work to update SQL database here
    }
    else
    {
        context.Response.ContentType = "text/plain";
        context.Response.Write(filesPath + fileName + " Invalid filename");
    }
}
4

2 に答える 2