我有一个成像库,它接受查询字符串参数来动态操作图像,我使用正则表达式来解析参数以确定在传递的图像上运行哪些方法。
对于我的所有其他过滤器,我有非常严格的表达式,以确保处理器只会尝试对明确的模式匹配采取行动,但对于一种方法“水印”,表达式被分解成更小的部分,因为它太大而且太乱而无法使用它是自己的,因为每个可能的匹配都是可选的。
我担心的是,watermark=[^&]*
进行初始匹配的父 Regex 过于宽松,会让我面临 XXS 攻击。
有什么更好的方法来做到这一点?我应该硬着头皮创造一个巨大的表情还是有更好的选择?
我正在解析的示例字符串:
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);