模板对象注入¶
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
直接将用户控制的对象用作模板引擎的参数可能会允许攻击者执行本地文件读取甚至远程代码执行。
建议¶
避免将用户控制的对象用作模板引擎的参数。相反,使用模板所需的特定属性显式构建对象。
示例¶
在下面的示例中,服务器使用用户控制的`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
});
});
参考文献¶
blog.shoebpatel.com: NodeJS 应用中的秘密参数、LFR 和潜在的 RCE.
cwe.mitre.org: CWE-73:文件名或路径的外部控制
常见弱点枚举:CWE-73.
常见弱点枚举:CWE-94.