GitHub Copilot 可帮助开发人员识别和解决 SQL 代码和应用程序层查询中的常见安全风险。 它可以帮助检测漏洞,例如 SQL 注入、过度公开的数据和不安全模式,特别是针对那些安全背景不强的开发人员,并在开发过程中提供切合实际的建议,增强其安全意识。
开始
确保已连接到数据库,并使用 MSSQL 扩展打开活动编辑器窗口。 通过此连接, @mssql 聊天参与者可以了解数据库环境的上下文,从而提供准确的上下文感知建议。 如果没有数据库连接,聊天参与者将没有架构或数据上下文来提供有意义的响应。
以下示例使用 AdventureWorksLT2022 示例数据库,可以从 Microsoft SQL Server 示例和社区项目 主页下载该数据库。
为了获得最佳结果,请调整表和架构名称以匹配自己的环境。
确保聊天包含 @mssql 前缀。 例如,键入 @mssql 后跟问题或提示。 这可确保聊天参与者了解你请求与 SQL 相关的帮助。
使用 GitHub Copilot 检测和修复安全风险
GitHub Copilot 可帮助开发人员在开发过程早期检测和修复常见的安全漏洞,然后再到达生产环境。 无论你使用的是原始 SQL、ORM 还是存储过程,GitHub Copilot 都可以识别不安全模式,解释潜在风险,并根据数据库上下文建议更安全的替代方案。 这对于不专门从事安全性但需要遵循安全编码做法的开发人员尤其有用。
下面是通过聊天参与者可以询问的常见用例和示例。
SQL 注入检测
SQL 注入是数据库应用程序中最常见的和危险的安全漏洞之一。 GitHub Copilot 可帮助识别未参数化的查询、字符串内插问题以及动态 SQL 的滥用,同时建议为上下文定制的更安全的参数化替代项。
Python 中的 SQLAlchemy 示例
I'm working with SQLAlchemy in Python for my current database `SalesLT` schema. Check the following `SQLAlchemy` query for potential security risks, such as SQL injection, over-fetching, or performance issues. If applicable, suggest improvements using parameterized queries, connection pooling, and other secure `SQL Server` practices to ensure performance and security.
query = f"SELECT * FROM SalesLT.Customer WHERE LastName = '{user_input}'"
result = engine.execute(query).fetchall()
JavaScript SQL 示例
Analyze the following JavaScript SQL query for potential security vulnerabilities. Identify risks such as SQL injection, over-fetching, and poor authentication practices. Explain why this query is insecure and provide a secure alternative.
const query = `SELECT * FROM Users WHERE Username = '${username}' AND Password = '${password}'`;
SQL 注入攻击模拟
Using my current database, simulate a SQL injection attack for the `SalesLT.uspGetCustomerOrderHistory` stored procedure and suggest fixes.
查看存储过程示例
Review the stored procedure `SalesLT.uspGetCustomerOrderHistory` in my current database for potential SQL injection vulnerabilities. Explain how unparameterized or improperly validated inputs could be exploited and recommend secure coding practices.
识别安全问题示例
Review the `SalesLT.uspGetCustomerOrderHistory_Insecure` stored procedure. Identify any potential security issues in the implementation and then provide a revised version of the stored procedure that addresses these concerns without explicitly listing security best practices.
You can use the following T-SQL to create the stored procedure:
CREATE OR ALTER PROCEDURE [SalesLT].[uspGetCustomerOrderHistory_Insecure]
@CustomerID NVARCHAR (50)
AS
BEGIN
DECLARE @SQL AS NVARCHAR (MAX) = N'SELECT *
FROM SalesLT.SalesOrderHeader
WHERE CustomerID = ' + @CustomerID + ';';
EXECUTE (@SQL);
END
GO
常规安全建议
除了 SQL 注入之外,许多数据库应用程序默认公开敏感数据或使用不安全的配置。 GitHub Copilot 提供有关加密连接、屏蔽或保护个人数据以及跨多个开发堆栈的安全身份验证和授权最佳做法的指南。
敏感数据存储示例
Recommend secure methods for storing sensitive data in the `SalesLT.Address` table.
屏蔽个人数据示例
What are the best strategies or built-in features in my database for masking personal data in the `SalesLT.Customer` table?
在 Entity Framework Core 示例中强制加密
How can I configure my connection string in Entity Framework Core to enforce encryption and avoid exposing credentials?
Node.js 身份验证示例中的 Microsoft Entra ID
In a Prisma or Node.js environment, how can I securely use Microsoft Entra ID authentication or managed identity with SQL Server instead of storing passwords?
建议使用 SQL Server 选项来保护数据示例
What SQL Server options should I enable or verify (for example, Always Encrypted, Transparent Data Encryption) to protect customer data when using ORMs like Sequelize or EF Core?
共享您的体验
为了帮助我们优化和改进 MSSQL 扩展的 GitHub Copilot,请使用以下 GitHub 问题模板提交反馈: GitHub Copilot 反馈
提交反馈时,请考虑包括:
测试方案 – 告诉我们你关注哪些领域,例如架构创建、查询生成、安全性、本地化。
效果良好 - 描述任何感觉流畅、有用或超出预期的体验。
问题或漏洞 – 包括任何问题、不一致或令人困惑的行为。 屏幕截图或屏幕录制特别有用。
改进建议 - 分享改进可用性、扩大覆盖范围或增强 GitHub Copilot 响应的想法。