首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >XSS过滤器以避免表单注入匹配它不应该匹配的字符串

XSS过滤器以避免表单注入匹配它不应该匹配的字符串
EN

Stack Overflow用户
提问于 2018-01-23 10:41:26
回答 1查看 388关注 0票数 0

我使用这个Grails 3插件对用户输入的XSS进行消毒。它使用正则表达式过滤掉不需要的内容。其中一个表达方式是:

代码语言:javascript
运行
复制
<(.*?)form(.*?)>(.*?)</(.*?)form(.*?)>

它的目的是消除注入的恶意形式。“守则”写道:

代码语言:javascript
运行
复制
// Avoid any form injection with <...form ...> ... </form ...> tag

然而,这一表述过于宽泛。例如,它匹配以下无害字符串:

代码语言:javascript
运行
复制
<p>Refactoring is the disciplined process of improving design qualities without changing the external behaviour of the code. To refactor a big piece of code means to apply small transformation that keep the behavior unchanged. When refactoring, the code should work every 5-7 minutes. It's not refactoring if you can't run the code for hours or days.</p><p><br></p><p>In this session, we will take a deep dive into the refactoring transformations. I will demonstrate:</p><p>&nbsp; &nbsp;how to pick the next transformation</p><p>&nbsp; &nbsp;how small the transformations are</p><p>&nbsp; &nbsp;how to use tools to make refactoring faster and</p><p>&nbsp; &nbsp;how local transformations lead to unexpected improvements in design</p>

问题是:,去掉表单的更好的正则表达式是什么?当然,像上面这样的字符串应该保持原样。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-01-23 11:08:06

作为免责声明,我们一般不应该使用regex来过滤嵌套的HTML内容(实际上也包括任何HTML)内容。但是,由于OP似乎使用的是一个工具来实现这一点,所以可能没有一个简单的解决办法。

下面的模式似乎有效,并且只在<form>标记上触发:

代码语言:javascript
运行
复制
<([^<>]*)form([^<>]*)>(.*?)<\/([^<>]*)form([^<>]*)>

我对您原来的模式所做的主要更改是使标签内的匹配尽可能不贪婪和谨慎。您的示例文本是一个很好的文本,因为它包含了单词transformations,它与您的原始模式导致错误的结果。

演示

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48399676

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档