【问题标题】:lint errors blocking did some research but not successfullint 错误阻止做了一些研究,但没有成功
【发布时间】:2018-03-03 10:02:43
【问题描述】:
  • 我是 tslint 和 typescript 的新手。
  • 我正在尝试修复此错误。
  • 你能告诉我如何解决它吗?
  • 我做了一些研究,但找不到解决方案。
  • 在下面提供代码。

(no-inner-html) app/components/sports.ts[717, 9]:使用 html() 函数将字符串写入 innerHTML 是不安全的:$('.tubing').html(health.标题)

if (health.title == "waterS") {
      let that = this;
      let recordPlayersWeight = {};
      this.futureParrot = [];
      this.pastParrot = [];
      this.peacockService.getResponse(health.url, 'get', null)
        .subscribe(recordPlayersWeight => {
            this.gridData = recordPlayersWeight.waterDtos;
            that._recordPlayersWeightSource.recordPlayersWeight(this.gridData);
          },
          err => {
          }
        );
    }

    that.window = $("#TigerwatersPopup");
    that.window.kendoWindow({
      width: "60%",
      title: false,
      visible: false,
      resizable: false,
      actions: [],
      draggable: false,
      modal: true,
      open: function() {
        $("html, body").css("overflow", "hidden");
        that.isVisible = true;
        $('.tubing').html(health.title);

【问题讨论】:

标签: javascript jquery angularjs typescript tslint


【解决方案1】:

当您使用 jQuery 的 .html() 函数时,可能会不小心将“不安全”元素添加到您的页面,例如 iframe 标签或源标签。如果您添加的 html 是由恶意用户提交的(例如,作为查询的一部分或在表单中输入的数据),那么您就是在为他们提供攻击您的方法。这是一种代码注入攻击(见https://en.wikipedia.org/wiki/Code_injection)。

在您的情况下,您似乎只是想将纯文本放入 .tubing 元素中,所以这样做更安全

$('.tubing').text( health.title );

它的作用是“转义”任何 HTML 特殊字符,例如 < 或 >,以便它们显示为文本而不是被视为元素。这是安全的,可以防止攻击者注入代码,并且不会触发 lint 规则。

【讨论】:

  • 感谢您的回复...我又遇到了一个错误,您能告诉我如何解决它 无法在构造函数中声明属性“waterService”
  • 这不在您发布的代码中,所以我无能为力。也许再问一个问题。您收到的错误应该会提示您导致问题的文件和行。
  • 嘿它发生在这一行构造函数(公共水服务:WATERService)你能帮我吗
猜你喜欢
  • 2013-04-07
  • 1970-01-01
  • 2018-07-16
  • 2015-06-20
  • 1970-01-01
  • 1970-01-01
  • 2014-06-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多