Inline Javascript vs External - Is one more secure than the other?

时间:2018-03-23 00:24:07

标签: javascript

A question that I've wondered about every now and again over the years but never really bothered to find a definitive answer to.

We know it's possible to manipulate both inline and external scripts via the console, but are there any small security benefits of one over the other?

Is it a bit harder to manipulate one of them or are they both equally vulnerable?

1 个答案:

答案 0 :(得分:0)

内联JavaScript的可能问题

内联JavaScript的主要缺点是Cross-site Scripting的潜力。从本质上讲,受信任的网站可以无意中呈现代码(在本例中为JavaScript),可以执行任何数量的恶意行为。可以找到内联JavaScript安全问题的简单示例here

内容安全政策

当外部化JavaScript时,您可以建立一个Content Security Policy,它信任从您的网站(以及其他网站,如果需要)加载的外部JavaScript,并阻止所有内联JavaScript执行。从本质上讲,您确定所有外部加载的资源都是安全的,如果任何JavaScript尝试内联执行,那么阻止尝试。

其他步骤

然而,应该指出的是,应该建立保障措施作为"清洁"的第一道防线。传入和传出文本。应该假设任何用户输入,传入或传出,可能是恶意的。相应地计划。

以下链接提供了更多信息: