如何保护我的NodeJS代码-如果可能,没有简单的密码和文本文件

问题描述 投票:1回答:1

我具有以下NodeJS代码,其中我已成功使用mssql模块执行存储过程。我使用的是var config = {..,密码:'....',...}部分,其中定义了用户名和密码。

我如何确保以下代码的安全性,即我没有对此代码或任何外部文件进行硬编码或拥有任何密码。想法是使用加密的密码进行连接,然后执行存储过程。

[我看到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);
});
node.js security encryption cryptography password-encryption
1个回答
0
投票

好回答,如果甚至可以暴露环境变量,则最好的方法可能是使用readline。这是Node.js中的内置模块,其作用类似于Python中的prompt()。它将要求用户输入。这样,文件或代码中不会有任何信息泄漏。但是,使用readline的缺点是您每次必须手动输入密码,这在开发过程中可能会很烦人。因此,我建议在部署之前,将密码设置为硬编码,然后稍后再切换到readline方法。

示例代码:

const readline = require('readline');
const rl = readline.createInterface({input: process.stdin, output: process.stdout})
rl.question('Enter you password: ', answer => {
    // Authentication here
    rl.close()
})

很高兴回答您的问题。

© www.soinside.com 2019 - 2024. All rights reserved.