来自 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/