javascript - 在 Angular 2+ 中使用 href ="javascript:"是否危险?

标签 javascript html css angular security

来自 the Angular security guide ,有这个:

To block XSS attacks, you must prevent malicious code from entering the DOM (Document Object Model). For example, if attackers can trick you into inserting a tag in the DOM, they can run arbitrary code on your website. The attack isn't limited to tags—many elements and properties in the DOM allow code execution, for example, <img onerror="..."> and <a href="javascript:...">. If attacker-controlled data enters the DOM, expect security vulnerabilities.

我有时使用 <a href="javascript:" (click)="someFunction()">link</a>使用 CSS 选择器 ( a:link) 定位元素,而链接不引用 URI、 anchor 或运行任何代码。

这是一种危险的做法吗?他们并没有真正举例说明如何利用它。

如果这是不好的做法,那么这里是 my dilemma :如果我在导航中有一系列链接,其中一些是 HREF,其中一些绑定(bind)到 JS,但我希望它们看起来都一样并使用相同的 CSS 选择器。有没有一种简单的方法可以做到这一点?

最佳答案

尝试为具有(点击)的链接添加一个空的 href

<a (click)="action()" href>Previous</a>

如果不行,你可以试试

<a href (click)="doSomething(); $event.preventDefault()">Link</a>
<a href (click)="!!doSomething()">Link</a>
<a href (click)="doSomething(); false">Link</a>

关于javascript - 在 Angular 2+ 中使用 href ="javascript:"是否危险?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50862845/

相关文章:

javascript - Jquery 错误地按 id 对 <li> 进行排序

css - 自定义 WP 主题 - 如何更改 CSS 中导航按钮的链接?

javascript - CSS,Jquery - 绝对定位的 div 的宽度返回为 0

html - 样式化结果集

javascript - 如何缓存 JSON 响应?

javascript - 在运行时将字符串附加到 DOM 并在 X 秒后将其删除

javascript - Angular 组件不会显示

java - 将 java 对象从 .jsp 发送到 java

javascript - 如何将php代码输入的常量值转换为html标签形式

javascript - 使用 javascript 添加 float