Azure Function App 中的 SQL 注入(inject)保护错误

标签 sql azure azure-functions sql-injection

我有 Azure Function App 函数,用于从应用程序到 SQL 数据库的 API 调用。我正在尝试使用参数化查询来保护数据库免受SQL注入(inject)的侵害。我已经能够在 SELECT 和 INSERT 等查询语句上执行此操作,但在使用 DELETE 语句时遇到了麻烦。我不断收到 500 响应,并显示错误消息必须声明标量变量“@agencyId”。我正在声明该变量,所以我不确定它为什么这么说。有人有想法或更好的方法吗?

const userName1 = process.env["DB_USERNAME"];  
const password1 = process.env["DB_PASSWORD"];  
const server1 = process.env["DB_SERVER"];  
const database1 = process.env["DB_NAME"];  
  
module.exports = async function (context, req) {  
    context.log('JavaScript HTTP trigger function processed a request.');  
  
    if (!req.body || !req.body.AGENCY_ID) {  
        context.res = {  
            status: 400,  
            body: "Please provide a valid AGENCY_ID in the request body."  
        };  
        return;  
    }  
  
      const parameters = [  
        { name: 'agencyId', sqlType: sql.Int, value: req.body.AGENCY_ID }  
    ];  
    const query = 'DELETE FROM Agency_Defs WHERE AGENCY_ID = @agencyId;';  

  
    var dbConfig = {  
        server: server1,  
        database: database1,  
        user: userName1,  
        requestTimeout: 600000,  
        password: password1,  
        port: 1433,  
        options: {  
            encrypt: true  
        }  
    };  
  
    try {  
        await sql.connect(dbConfig);  
        const result = await sql.query(query, parameters);  
  
        context.res = {  
            body: result  
        };  
    } catch (error) {  
        context.res = {  
            status: 500,  
            body: error.message  
        };  
    }  
}  

最佳答案

使用此代码删除 Items SQL Server

const  sql = require('mssql');
const  config = {
user:  'sampath125',
password:  'Ra@80muravi',
server:  'sampath234',
database:  'sampath',
};
module.exports = async  function  (context,  req)  {
try  {
await  sql.connect(config);
const  {  agencyId  } = req.body;
if (!agencyId) {
context.res = {
status:  400,
body:  'Please provide the agencyId in the request body.',
};
return;
}
const  query = `DELETE FROM Agency_Defs WHERE AGENCY_ID = @agencyId`;
const  request = new  sql.Request();
request.input('agencyId',  sql.Int,  agencyId);
const  result = await  request.query(query);
await  sql.close();
context.res = {
status:  200,
body:  `Deleted ${result.rowsAffected[0]} records successfully!`,
};
}  catch (err) 
{
context.res = {
status:  500,
body:  err.message,
};
}
};

enter image description here

enter image description here

enter image description here

使用 MSSQL Server:

enter image description here

使用 Azure SQL Server:

enter image description here

关于Azure Function App 中的 SQL 注入(inject)保护错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76775161/

相关文章:

azure-functions - 天蓝色函数 : how to received data via chunked encoding

c# - 为什么我的 Azure Function App 时间戳少了一个小时

sql - 数据源和数据集的区别

MySQL创建一对多表,下面的最佳选择是什么?

azure - 如何使用 terraform 将多个 secret 添加到 azure key Vault

azure - Azure 应用服务上托管的 Multi-Tenancy SaaS 应用中的自定义域

html - 需要帮助让 oracle "SORT BY"以我想要的方式工作

java - 查询后如何匹配用户名和密码

powershell - Azure 凭据尚未设置或已过期

azure - 与 2017 年相比,如何在同一项目下使用 2 种不同语言(C#、node.js)创建 Azure Functions?