【问题标题】:Using innerHTML, and what are security concerns?使用innerHTML,什么是安全问题?
【发布时间】:2023-03-13 17:36:01
【问题描述】:

有人告诉我在我的 JS 代码中使用 innerHTML 很危险。 我确实明白为什么,但是有人可以告诉我这两种使用方式之间是否有区别?

第一:

const onePersonArticle = create('article');
onePersonArticle.className = 'one-person';
const content = ` <label class='info-labels' for="name">NAME: </label><label id='name' class='data-labels'>${member.name}</label>
    <label class='info-labels' for="phone">PHONE: </label><label id='phone' class='data-labels'>${member.phone}</label>
    <label class='info-labels' for="code-wars">CODEWARS: </label><label id='code-wars' class='data-labels'>${member.cwb} - ${member.cwa}</label>
    <label class='info-labels' for="free-code-camp">FREECODECAMP: </label><label id='free-code-camp' class='data-labels'>${member.fccb} - ${member.fcca}</label>
    <label class='info-labels' for="notes">NOTES: </label><label id='notes' class='data-labels'>${member.notes}</label>
    <span class="person-buttons"><button type="button" name="button" class="button delete-button">⌘</button>
    <button type="button" name="button" class="button edit-button">✎</button></span>`;
onePersonArticle.innerHTML = content;

第二个:

const onePersonArticle = create('article');
onePersonArticle.className = 'one-person';
onePersonArticle.innerHTML =
  ` <label class='info-labels' for="name">NAME: </label><label id='name' class='data-labels'>${member.name}</label>
    <label class='info-labels' for="phone">PHONE: </label><label id='phone' class='data-labels'>${member.phone}</label>
    <label class='info-labels' for="code-wars">CODEWARS: </label><label id='code-wars' class='data-labels'>${member.cwb} - ${member.cwa}</label>
    <label class='info-labels' for="free-code-camp">FREECODECAMP: </label><label id='free-code-camp' class='data-labels'>${member.fccb} - ${member.fcca}</label>
    <label class='info-labels' for="notes">NOTES: </label><label id='notes' class='data-labels'>${member.notes}</label>
    <span class="person-buttons"><button type="button" name="button" class="button delete-button">⌘</button>
    <button type="button" name="button" class="button edit-button">✎</button></span>`;
container.appendChild(onePersonArticle);

现在,如果这两种方式都容易受到代码注入的攻击,是否还有其他方法可以将 HTML 标记以与将字符串相同的方式实现到 HTML 页面中?

【问题讨论】:

标签: javascript html security xss innerhtml


【解决方案1】:

如果您完全控制正在使用的字符串的所有方面,那么两者都是安全的。

话虽如此,一般来说,.innerHTML 用于将 HTML 的小片段插入并解析到文档中,而不是大字符串(如您在此处所看到的),因为它成为支持的噩梦。

当您有大量内容时,例如您在此处显示的内容,HTML &lt;template&gt; 和 JavaScript document.importNode() 可能更可取,因为您有一种更简单的方式将内容作为一系列注入DOM 节点通过 API 比一堆字符串连接更容易操作。这允许您动态创建元素,然后您可以使用.textContent 而不是.innerHTML 填充它们,这消除了安全问题,因为文本不会被解析为 HTML。

【讨论】:

  • 同意,但说两者都是安全的有点误导。很可能问题中的两个示例都容易受到 XSS 的攻击。
  • @GaborLengyel 这没有误导,因为我说只有当你完全控制字符串时它们都是安全的。
  • @ScottMarcus 我理解,但是对于一般不理解 XSS 或漏洞的人来说,你说没关系,因为上面的代码很可能是易受攻击的。 “完全控制字符串的所有方面”太模糊了,对不了解安全的开发人员来说意义不大......我并不是要冒犯或争论,严格来说你的答案非常正确,我刚刚工作有很多开发人员,这是我的经验。 :)
猜你喜欢
  • 1970-01-01
  • 2011-05-24
  • 1970-01-01
  • 1970-01-01
  • 2014-09-28
  • 2015-05-27
  • 2011-04-08
  • 2010-10-02
  • 2018-05-03
相关资源
最近更新 更多