【问题标题】:How to secure my NodeJS code - no plain password and no text file if possible如何保护我的 NodeJS 代码 - 如果可能,没有纯密码和文本文件
【发布时间】: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


【解决方案1】:

好吧回答,如果连环境变量都可以暴露,最好的方法大概是使用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()
})

很高兴回答您的问题。

【讨论】:

    猜你喜欢
    • 2011-01-16
    • 1970-01-01
    • 1970-01-01
    • 2023-02-25
    • 1970-01-01
    • 2016-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多