虽然许多开发人员可能认为这些漏洞属于“常识”,但绝大多数新引入的安全弱点源于诸如跨站脚本(XSS)、SQL 注入和跨站请求伪造(CSRF)等漏洞。 通过遵循安全编码实践,可以缓解这些漏洞,例如使用参数化查询、输入验证,以及避免硬编码敏感数据。 GitHub Copilot 可以帮助检测和解决这些问题。
注意
虽然 Copilot对话 可以帮助发现一些常见安全漏洞并提供修复方案,但不应依赖 Copilot 进行全面的安全分析。 使用 code scanning 将更彻底地确保代码安全。 有关如何设置 code scanning 的详细信息,请参阅“配置代码扫描的默认设置”。
示例方案
下面的 JavaScript 代码存在潜在的 XSS 漏洞,如果未对 name 参数在页面显示前进行正确的清理,可能被利用。
function displayName(name) {
const nameElement = document.getElementById('name-display');
nameElement.innerHTML = `Showing results for "${name}"`
}
示例提示
可以请求 Copilot对话 分析代码中的常见安全漏洞,并提供解释及修复方案。
Analyze this code for potential security vulnerabilities and suggest fixes.
function displayName(name) {
const nameElement = document.getElementById('name-display');
nameElement.innerHTML = `Showing results for "${name}"`
}
Analyze this code for potential security vulnerabilities and suggest fixes.
Analyze this code for potential security vulnerabilities and suggest fixes.
示例响应
注意
以下响应是示例。 Copilot对话 的回答是不确定的,因此你可能会得到与这里所显示的不同的回答。
Copilot 会给出漏洞解释以及修复代码的建议。
function displayName(name) {
const nameElement = document.getElementById('name-display');
nameElement.textContent = `Showing results for "${name}"`;
}
其他阅读材料
- GitHub Copilot 对话助手的提示设计
- 使用 GitHub Copilot 的最佳做法
-
[AUTOTITLE](/code-security/code-scanning/introduction-to-code-scanning/about-code-scanning)