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 的誤用,同時建議針對您的內容量身打造更安全的參數化替代方案。
SQLAlchemy in Python 範例
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?
Microsoft Entra ID 在 Node.js 認證範例中
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 回應的想法。