2

CAT.NET コード分析でコード (C#、デスクトップ アプリケーション) を分析していますが、ファイル名を処理するときに「ファイル パスをファイル システム ルーチンに渡す前にサニタイズしてください」というメッセージが表示されます。私が理解していないのは、ファイル名が有効であることを確認するために、次を使用することです。

void SomeMethod(String filename)
{
    filename = System.IO.Path.GetFullPath(filename);
    // ... Do stuff
}

無効なファイル名の問題を解決するのは「魔法の解決策」ではないでしょうか? ここで似たようなものを読みました(最初の回答)が、私の場合、ローカルファイルのみを扱っているので、非常に基本的なものです...

では、なぜこのメッセージが表示されるのでしょうか?また、表示されないようにするにはどうすればよいでしょうか?

4

2 に答える 2

1

I know this is an old question, but I've just come across something that may be helpful specifically related to the CAT.Net error message.

In a blog post about the CAT.Net Data Flow Rules, they have this to say about the FileCanonicalizationRule:

Description

User input used in the file handling routines can potentially lead to File Canonicalization vulnerability. Code is particularly susceptible to canonicalization issues if it makes any decisions based on the name of a resource that is passed to the program as input. Files, paths, and URLs are resource types that are vulnerable to canonicalization because in each case there are many different ways to represent the same name.

Resolution

Sanitize the file path prior to passing it to file handling routines. Use Path.GetInvalidFileNameChars or Path.GetInvalidPathChars to get the invalid characters and remove them from the input. More information can be found at http://msdn.microsoft.com/en-us/library/system.io.path.getinvalidfilenamechars.aspx.

So, they suggest that you use Path.GetInvalidFileNameChars and Path.GetInvalidPathChars to validate your paths.

Note that their suggestion is to remove the invalid characters. While this will indeed make the path valid, it may cause unexpected behaviour for the user. As the comments on this question/answer suggest it's probably better to quit early and tell the user that their path is invalid, rather than doing something unexpected with their input (like removing bad characters and using the modified version).

于 2012-03-21T19:24:55.677 に答える
0

ファイル名がユーザーからのものである場合、「../../../../etc/passwd」のようなものである可能性があります。エラー メッセージは、取得できないようにサニタイズする必要があることを示しています。想定されていないディレクトリに。

于 2009-12-31T07:50:09.823 に答える