1

クエリ文字列パラメーターを受け入れて画像を動的に操作するイメージングライブラリがあり、正規表現を使用してパラメーターを解析し、渡された画像で実行するメソッドを決定します。

他のすべてのフィルターでは、プロセッサーが明確なパターンの一致にのみ作用しようとするように非常に厳密な式を使用していますが、1つの方法「ウォーターマーク」では、式が大きすぎて乱雑であるため、式を小さな部分に分割します。それは独自のものであり、可能な一致のそれぞれがオプションであるためです。

私の心配は、最初の一致を行う親の正規表現watermark=[^&]*があまりにも緩く、XXS攻撃にさらされることです。

これを行うためのより良い方法は何でしょうか?単に弾丸を噛んで1つの巨大な表現を作成する必要がありますか、それともより良い代替手段がありますか?

私が解析している文字列の例:

yourimage?watermark = test | color-fff | size-36 | style-italic | opacity-80 | position-30-150 | shadow-true | font-arial

私の表現:

/// <summary>
/// The regular expression to search strings for.
/// </summary>
private static readonly Regex QueryRegex = new Regex(@"watermark=[^&]*",
                                                     RegexOptions.Compiled);

/// <summary>
/// The regular expression to search strings for the text attribute.
/// </summary>
private static readonly Regex TextRegex = 
                                  new Regex(@"text-[^/:?#\[\]@!$&'()*%\|,;=]+",
                                            RegexOptions.Compiled);

/// <summary>
/// The regular expression to search strings for the position attribute.
/// </summary>
private static readonly Regex PositionRegex = new Regex(@"position-\d+-\d+",
                                                        RegexOptions.Compiled);

/// <summary>
/// The regular expression to search strings for the color attribute.
/// </summary>
private static readonly Regex ColorRegex = 
                                      new Regex(@"color-([0-9a-fA-F]{3}){1,2}",
                                                RegexOptions.Compiled);

/// <summary>
/// The regular expression to search strings for the fontsize attribute.
/// </summary>
private static readonly Regex FontSizeRegex = new Regex(@"size-\d{1,3}",
                                                        RegexOptions.Compiled);

/// <summary>
/// The regular expression to search strings for the fontstyle attribute.
/// </summary>
private static readonly Regex FontStyleRegex = 
                 new Regex(@"style-(bold|italic|regular|strikeout|underline)",
                           RegexOptions.Compiled);

/// <summary>
/// The regular expression to search strings for the font family attribute.
/// </summary>
private static readonly Regex FontFamilyRegex = 
                              new Regex(@"font-[^/:?#\[\]@!$&'()*%\|,;=0-9]+",
                                        RegexOptions.Compiled);

/// <summary>
/// The regular expression to search strings for the opacity attribute.
/// </summary>
private static readonly Regex OpacityRegex = 
                                new Regex(@"opacity-(?:100|[1-9]?[0-9])",
                                          RegexOptions.Compiled);

/// <summary>
/// The regular expression to search strings for the shadow attribute.
/// </summary>
private static readonly Regex ShadowRegex = new Regex(@"shadow-true",
                                                      RegexOptions.Compiled);
4

1 に答える 1

2

1つの正規表現を使用するか複数の正規表現を使用するかはまったく関係ありません。重要なのは、脆弱性を開く可能性のある方法で入力を使用する前に、入力を適切にチェックすることです。何らかの方法で入力を使用する前にこれらの正規表現をすべて適用する限り、すべてを1つのステップで実行するよりも脆弱ではありません。

この場合、複数正規表現のアプローチが明らかに優れていると思います。単一の正規表現は非常に複雑で混乱を招きます(特にパラメーターが可変の順序で来る可能性がある場合)。コードが明確であるほど、セキュリティの問題につながる間違いを犯す可能性が低くなります。

私はあなたの正規表現に本質的に悪いことは何も見ていませんが、特殊文字の大規模なリストを禁止するのではなく、おそらく許可したいことを積極的に述べる必要があるというCapiléに同意します。

ただし、最終的な答えは、コードでの使用方法によって異なります。

于 2013-02-21T13:59:27.037 に答える