我有一个网站,用户可以使用html设置描述。我希望他们能够使用所有的html功能,但不包括js。有没有办法在标签内禁用所有javascript?请注意,我是指包括onclick
等所有javascript。如果没有,是否有某种正则表达式可以用来过滤所有xss攻击?
我有一个网站,用户可以使用html设置描述。我希望他们能够使用所有的html功能,但不包括js。有没有办法在标签内禁用所有javascript?请注意,我是指包括onclick
等所有javascript。如果没有,是否有某种正则表达式可以用来过滤所有xss攻击?
您可以使用内容安全策略(CSP)来保护IT技术相关内容,但配置可能有些繁琐。此外,它并不是100%安全的,因为它取决于客户端(浏览器)。
you could load all you scripts externally and block all inlined scripts. This can be done with CSP 1 - the browser support is pretty good for CSP 1.
Policy would looks something like this:
Content-Security-Policy: script-src 'self';
and/or you could whitelist your inline scripts blocks and all others will be blocked. You need CSP 2 for that, the browser support is lower for that one. The browser support is more limited, e.g. no IE
examples of the policy:
Content-Security-Policy: script-src 'nonce-123'
allow executing <script nonce="123">safemethod()</script>
, block others
Content-Security-Policy: script-src 'sha256-07123e1f482356c415f684407a3b8723e10b2cbbc0b8fcd6282c49d37c9c1abc'
allow executing the script that has the sha 256 hash (hash of the content) of 07123e1f482356c415f684407a3b8723e10b2cbbc0b8fcd6282c49d37c9c1abc
, block others.
tidy
或Perl中的HTML::Parser
)进行过滤,并仅允许通过已经明确确认为安全的标签和属性。