快速入門:安全分析器

GitHub Copilot 協助開發者識別並解決 SQL 程式碼及應用層查詢中的常見安全風險。 它能偵測像是 SQL 注入、資料過度暴露以及不安全模式等漏洞。 沒有強健資安背景的開發者,可以在開發過程中使用 GitHub Copilot 獲得實用且具情境感知的建議。

開始

確保你連上資料庫,並且開啟一個啟用 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 object-relational mapping (ORM) frameworks like Sequelize or EF Core?

分享您的體驗

若要協助我們精簡及改善 MSSQL 延伸模組的 GitHub Copilot,請使用下列 GitHub 問題範本來提交您的意見反應: GitHub Copilot 意見反應

提交意見反應時,請考慮包括:

  • 測試情境:請告訴我們你專注於哪些領域,例如架構建立、查詢產生、安全性、在地化。

  • 有效的方法:描述任何感覺順利、有幫助或超出預期的經驗。

  • 問題或錯誤:包含任何問題、不一致或令人困惑的行為。 螢幕快照或螢幕錄製特別有用。

  • 改進建議:分享提升可用性、擴大覆蓋範圍或強化 GitHub Copilot 回應的點子。