クエリ文字列パラメーターを受け入れて画像を動的に操作するイメージングライブラリがあり、正規表現を使用してパラメーターを解析し、渡された画像で実行するメソッドを決定します。
他のすべてのフィルターでは、プロセッサーが明確なパターンの一致にのみ作用しようとするように非常に厳密な式を使用していますが、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);