简体   繁体   English

AWS WAF Regexp 问题与前瞻

[英]AWS WAF Regexp issue with lookahead

I am building a regexp for AWS WAF using a negative lookahead.我正在使用负前瞻为 AWS WAF 构建正则表达式。

joe(?!(ann|jen))

However, I've got back the following error from WAF console但是,我从 WAF 控制台返回了以下错误

WAFInvalidParameterException: Error reason: The parameter contains formatting that is not valid., field: REGEX_PATTERN_SET, parameter: joe(?!(ann|jen))

It seems like the AWS WAF does not support this kind of regexp.似乎 AWS WAF 不支持这种正则表达式。 I've found this blog https://aws.amazon.com/about-aws/whats-new/2017/10/aws-waf-now-supports-regular-expressions-regex/我发现这个博客https://aws.amazon.com/about-aws/whats-new/2017/10/aws-waf-now-supports-regular-expressions-regex/

Is there anyone having similar issue?有没有人有类似的问题? can you share how to fix it?你能分享一下如何解决吗?

Since negative lookaheads are unsupported, I broke mine out into several expressions that cover all cases.由于不支持否定前瞻,我将我的表达分解为涵盖所有情况的几个表达式。 WAF lets you specify multiple expressions. WAF 允许您指定多个表达式。 It uses logical OR matching, so only one of them has to match.它使用逻辑 OR 匹配,因此只有其中之一必须匹配。 Using the example in the question, the solution could be...使用问题中的示例,解决方案可能是...

joe[^aj]
joea[^n]
joean[^n]
joej[^e]
joeje[^n]

joe matches, unless he's followed by an a or a j . joe匹配,除非他后跟aj Then he's suspicious, so we go on to the next rule.然后他很可疑,所以我们继续下一个规则。 If that a is followed by an n , the we're still suspicious, so we go on to the next rule.如果a后跟n ,我们仍然怀疑,所以我们继续下一个规则。 We repeat that process until we've decided whether or not the entire word is joeann or joejen我们重复这个过程,直到我们决定整个词是joeann还是joejen


My particular use case was URI matching.我的特定用例是 URI 匹配。 I wanted to throttle requests to an entire directory, except for one subdirectory (and all its subdirectories).我想限制对整个目录的请求,除了一个子目录(及其所有子目录)。

Say we want to throttle /my/dir but not anything in /my/dir/safe .假设我们想限制/my/dir但不想限制/my/dir/safe任何内容。 We would do it like so...我们会这样做...

^/my/dir/?$
^/my/dir/[^s]
^/my/dir/s[^a]
^/my/dir/sa[^f]
^/my/dir/saf[^e]
^/my/dir/safe[^/]

We follow the same process of identifying each letter in sequence.我们遵循相同的过程来依次识别每个字母。

"You can't start with S. Ok, you can start with S, but you can't also have an A. Ok ok, I'll let it slide, but you cannot have an F too. Ok fine, your persistent, but..." “你不能以 S 开头。好吧,你可以以 S 开头,但你不能也有 A。好吧,我让它滑,但你不能也有 F。好吧,你的执着, 但...”

Notice we have to include a rule for the trailing slash / .请注意,我们必须为尾部斜杠/包含一条规则。 This covers the optional slash in /my/dir/safe/ and all subdirectories such as /my/dir/safe/whatever .这包括/my/dir/safe/的可选斜杠和所有子目录,例如/my/dir/safe/whatever

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM