1

您好我正在尝试在 Cloud Armor 安全策略中设置一条规则,以阻止将主机设置为 IP 地址的请求。这应该是相当直截了当的,除了我在尝试应用规则时在 Web 控制台中遇到错误。

这是自定义规则的 RE2 代码:

!has(request.headers["Host"]) ||
request.headers["Host"] == "" ||
request.headers["Host"].matches('(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)')

我们获取 Host 标头,然后对 Host 标头字符串使用 .matches()。正则表达式是相当标准的东西,但我不明白为什么控制台会出现这种错误消息:

1:34: token recognition error at: ''(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.'
3:82: extraneous input '3' expecting {'}', ',', IDENTIFIER}
3:84: mismatched input '(' expecting {<EOF>, 'in', '==', '!=', '<', '<=', '>=', '>', '&&', '||', '[', '{', '.', '-', '?', '+', '*', '/', '%%'}
3:28: token recognition error at: '|2'
3:40: token recognition error at: '|['
3:58: token recognition error at: '')'
3:81: expected a qualified name
3:81: undeclared reference to '*error*' (in container '')
1:1: ERROR: Cloud Armor rule matcher expression:1:33: token recognition error at: ''(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.'

我一直在这里使用文档

4

1 回答 1

2

嗯,看来我不得不\..

因此,自定义规则现在是:

!has(request.headers["Host"]) ||
request.headers["Host"] == "" ||
request.headers["Host"].matches('(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)')
于 2021-08-11T13:30:37.633 回答