CodeQL 文档

模板对象注入

ID: js/template-object-injection
Kind: path-problem
Security severity: 9.3
Severity: error
Precision: high
Tags:
   - security
   - external/cwe/cwe-073
   - external/cwe/cwe-094
Query suites:
   - javascript-code-scanning.qls
   - javascript-security-extended.qls
   - javascript-security-and-quality.qls

点击查看 CodeQL 仓库中的查询

直接将用户控制的对象用作模板引擎的参数可能会允许攻击者执行本地文件读取甚至远程代码执行。

建议

避免将用户控制的对象用作模板引擎的参数。相反,使用模板所需的特定属性显式构建对象。

示例

在下面的示例中,服务器使用用户控制的`profile` 对象来渲染`index` 模板。

var app = require('express')();
app.set('view engine', 'hbs');

app.post('/', function (req, res, next) {
    var profile = req.body.profile;
    res.render('index', profile);
});

但是,如果攻击者向`profile` 对象添加`layout` 属性,则服务器将加载`layout` 属性指定的 文件,从而允许攻击者执行本地文件读取。

修复方法是让服务器构建对象,并且只添加模板需要的属性。

var app = require('express')();
app.set('view engine', 'hbs');

app.post('/', function (req, res, next) {
    var profile = req.body.profile;
    res.render('index', {
        name: profile.name,
        location: profile.location
    });
});

参考文献

  • ©GitHub, Inc.
  • 条款
  • 隐私