【发布时间】:2016-06-29 22:16:38
【问题描述】:
我有以下 NodeJS 代码,我成功地使用 mssql 模块执行存储过程。我正在使用 var config = { .., password: '....', ... } 部分来定义用户和密码。
如何使以下代码安全,即我没有硬编码或在此文件或任何外部文件中没有任何密码。想法是使用加密密码建立连接,然后执行存储过程。
我看到 NodeJS 中有一个名为 crypto 的模块,但我想看看如何将它插入我的代码中以摆脱真正的密码(至少)。
http://lollyrock.com/articles/nodejs-encryption/
如果我使用环境变量并直接使用它们来填充密码变量,我看到一些帖子说甚至可以公开环境变量。 Hardcoded mysql user and password in Node.js
感谢任何帮助。
//This computerName is what we'll find in our mssql server to see
//if the server entry exist or not and the stored procedure will take this as a parameter.
var computerName = "some.fake.server.com";
var secProfile = "";
var sql = require('mssql');
var config = {
user: 'dbuser',
//I want to get rid of the following password line from this section.
password: 'secure9ass',
server: 'dbserver.domain.com',
database: 'DBName',
pool: {
max: 10,
min: 0,
idleTimeoutMillis: 30000
}
}
sql.connect(config).then(function(output) {
// Stored Procedure
new sql.Request()
.input("ComputerName", sql.VarChar(100), computerName)
.execute('dbo.getSysStatus_ByName').then(function(recordsets) {
console.dir(recordsets);
}).catch(function(err) {
// ... error checks
console.log('ERROR1::: ' + err)
console.log("----")
console.log(err)
console.log("====")
console.log(recordsets)
console.log("----")
console.log('ERROR2::: '+ sqlOutput);
console.log('ERROR3::: '+ request.parameters.sqlOutput.value);
});
console.log(output);
}).catch(function(err) {
// ... error checks
console.log('ERROR5::: '+ err);
});
【问题讨论】:
-
好吧,那是不可能的。确保很难侵入您的服务器,而不是混淆您的服务器代码。如果您使用加密,那么某处必须有一个密钥。如果您没有密钥,则不是加密,而是编码
-
运行 nodejs 代码的机器有多安全?正确保护机器,您应该能够使用环境变量。
标签: node.js security encryption cryptography password-encryption