简体   繁体   English

React.js允许XSS在href中吗?

[英]React.js allows XSS in href?

User generated links in an href tag can look like: href标记中用户生成的链接如下所示:

javascript:(() => {alert('MALICIOUS CODE running on your browser')})();

which i just injected using an input field on a page that does not check on the server, that URLs start with http / https . 我只是使用未在服务器上检查的页面上的输入字段注入的,URL以http / https开头。 On the react-rendered page i could later click on the link, and voila my code is running. 稍后,我可以在react-rendered页面上单击链接,瞧瞧我的代码正在运行。

Why does that work, when it is often stated: 为什么经常这样说呢?

react automatically escapes everything for you. 反应自动为您逃脱一切 ( What does it mean when they say React is XSS protected? ) 当他们说React受到XSS保护时,这是什么意思?

When react documentation points that "Everything" is escaped it just means that if you add some children to a JSX element, "Everything" in that children will be escaped. 当react文档指出“ Everything”已转义时,仅表示如果向JSX元素中添加了一些子级,则该子级中的“ Everything”将被转义。

For example in: 例如在:

const element = (
  <h1>
    Random text
  </h1>
);

Everything inside the h1 will be escaped, so if instead of "Random text" you want to display some user text coming from an input field, you are safe, any malicious script will be escaped. h1内部的所有内容都将被转义,因此,如果您想显示一些来自输入字段的用户文本而不是“随机文本”,那么您可以放心地删除所有恶意脚本。

WARNING: This does not mean you do not have to worry about XSS at all if you are using React. 警告:这并不意味着您在使用React时根本不必担心XSS。

You can still get an unsafe string from that input field (or an external source, as an API, converting a JSON object, etc) and place it on element props, or using dangerouslySetInnerHTML, or directly on html attributes as href, style.. etc where that malicious code will run. 您仍然可以从该输入字段(或作为API的外部源,转换JSON对象等)中获得不安全的字符串,并将其放置在元素props上,或者使用危险地设置InnerHTML,或者直接在html属性上用作href,style。等等,这些恶意代码将在其中运行。

So if you really need to place text on those props is your responsibility to sanitize the data before using it. 因此,如果您确实需要在这些道具上放置文字,则有责任在使用数据之前对数据进行消毒 Same as usually is done in the servers when saving data entered by a user, this data has to be sanitized so that code is not saved in the DB. 与保存用户输入的数据时通常在服务器中执行的操作相同,必须对此数据进行清理,以使代码不会保存在DB中。

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

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