security-best-practices
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseSecurity Best Practices
安全最佳实践
Overview
概述
This skill provides a description of how to identify the language and frameworks used by the current context, and then to load information from this skill's references directory about the security best practices for this language and or frameworks.
This information, if present, can be used to write new secure by default code, or to passively detect major issues within existing code, or (if requested by the user) provide a vulnerability report and suggest fixes.
本技能介绍如何识别当前场景中使用的语言和框架,然后从本技能的参考目录中加载该语言和/或框架的安全最佳实践相关信息。
如果存在此类信息,可用于编写默认安全的新代码,或被动检测现有代码中的重大问题,或(在用户请求时)提供漏洞报告并提出修复建议。
Workflow
工作流程
The initial step for this skill is to identify ALL languages and ALL frameworks which you are being asked to use or already exist in the scope of the project you are working in. Focus on the primary core frameworks. Often you will want to identify both frontend and backend languages and frameworks.
Then check this skill's references directory to see if there are any relevant documentation for the language and or frameworks. Make sure you read ALL reference files which relate to the specific framework or language. The format of the filenames is . You should also check if there is a which is agnostic to the framework you may be using.
<language>-<framework>-<stack>-security.md<language>-general-<stack>-security.mdIf working on a web application which includes a frontend and a backend, make sure you have checked for reference documents for BOTH the frontend and backend!
If you are asked to make a web app which will include both a frontend and backend, but the frontend framework is not specified, also check out . It is important that you understand how to secure both the frontend and backend.
javascript-general-web-frontend-security.mdIf no relevant information is available in the skill's references directory, think a little bit about what you know about the language, the framework, and all well known security best practices for it. If you are unsure you can try to search online for documentation on security best practices.
From there it can operate in a few ways.
-
The primary mode is to just use the information to write secure by default code from this point forward. This is useful for starting a new project or when writing new code.
-
The secondary mode is to passively detect vulnerabilities while working in the project and writing code for the user. Critical or very important vulnerabilities or major issues going against security guidance can be flagged and the user can be told about them. This passive mode should focus on the largest impact vulnerabilities and secure defaults.
-
The user can ask for a security report or to improve the security of the codebase. In this case a full report should be produced describe anyways the project fails to follow security best practices guidance. The report should be prioritized and have clear sections of severity and urgency. Then offer to start working on fixes for these issues. See #fixes below.
本技能的初始步骤是识别所有要求使用的、或已存在于当前项目范围内的语言和框架。重点关注核心主框架。通常你需要同时识别前端和后端的语言与框架。
然后检查本技能的参考目录,查看是否有该语言和/或框架的相关文档。确保阅读所有与特定框架或语言相关的参考文件。文件名格式为。你还应检查是否存在与框架无关的文件。
<language>-<framework>-<stack>-security.md<language>-general-<stack>-security.md如果处理的是包含前端和后端的Web应用,请确保同时检查前端和后端的参考文档!
如果用户要求开发包含前后端的Web应用,但未指定前端框架,也请查看。了解如何同时保障前端和后端的安全性至关重要。
javascript-general-web-frontend-security.md如果参考目录中没有相关信息,请结合你对该语言、框架的了解,以及其广为人知的安全最佳实践进行判断。如果用户要求生成报告,需告知用户暂无具体指导(但你仍可生成报告,或明确检测关键漏洞)。
从这里开始,技能可通过以下几种方式运行:
-
主要模式:使用上述信息编写默认安全的新代码。这适用于启动新项目或编写新代码的场景。
-
次要模式:在项目开发和为用户编写代码时,被动检测漏洞。对于违反安全指南的严重或非常重要的漏洞或重大问题,可标记出来并告知用户。此被动模式应聚焦于影响范围最大的漏洞和安全默认配置。
-
用户可能要求提供安全报告或改进代码库的安全性。在这种情况下,需生成完整报告,说明项目未遵循安全最佳实践的所有方面。报告应按优先级划分,明确区分漏洞的严重程度和紧急性。然后提出开始修复这些问题的建议。详见下方#修复建议部分。
Workflow Decision Tree
工作流程决策树
- If the language/framework is unclear, inspect the repo to determine it and list your evidence.
- If matching guidance exists in , load only the relevant files and follow their instructions.
references/ - If no matching guidance exists, consider if you know any well known security best practices for the chosen language and or frameworks, but if asked to generate a report, let the user know that concrete guidance is not available (you can still generate the report or detect for sure critical vulnerabilities)
- 如果语言/框架不明确,检查代码仓库以确定,并列出判断依据。
- 如果目录中有匹配的指导文档,仅加载相关文件并遵循其说明。
references/ - 如果没有匹配的指导文档,考虑你是否了解该语言和/或框架的广为人知的安全最佳实践;但如果用户要求生成报告,需告知用户暂无具体指导(你仍可生成报告,或明确检测关键漏洞)
Overrides
覆盖规则
While these references contain the security best practices for languages and frameworks, customers may have cases where they need to bypass or override these practices. Pay attention to specific rules and instructions in the project's documentation and prompt files which may require you to override certain best practices. When overriding a best practice, you MAY report it to the user, but do not fight with them. If a security best practice needs to be bypassed / ignored for some project specific reason, you can also suggest to add documentation about this to the project so it is clear why the best practice is not being followed and to follow that bypass in the future.
虽然这些参考文档包含语言和框架的安全最佳实践,但客户可能有需要绕过或覆盖这些实践的场景。请注意项目文档和提示文件中可能要求你覆盖某些最佳实践的特定规则和说明。当覆盖某一最佳实践时,你可以向用户报告,但不要与其争执。如果因某些项目特定原因需要绕过/忽略安全最佳实践,你也可以建议在项目中添加相关文档,明确说明不遵循该最佳实践的原因,并在未来遵循此绕过规则。
Report Format
报告格式
When producing a report, you should write the report as a markdown file in or some other location if provided by the user. You can ask the user where they would like the report to be written to.
security_best_practices_report.mdThe report should have a short executive summary at the top.
The report should be clearly delineated into multiple sections based on severity of the vulnerability. The report should focus on the most critical findings as these have the highest impact for the user. All findings should be noted with an numeric ID to make them easier to reference.
For critical findings include a one sentence impact statement.
Once the report is written, also report it to the user directly, although you may be less verbose. You can offer to explain any of the findings or the reasons behind the security best practices guidance if the user wants more info on any findings.
Important: When referencing code in the report, make sure to find and include line numbers for the code you are referencing.
After you write the report file, summarize the findings to the user.
Also tell the user where the final report was written to
生成报告时,应将其写入或用户指定的其他位置。你可以询问用户希望将报告保存到何处。
security_best_practices_report.md报告顶部应有简短的执行摘要。
报告应根据漏洞的严重程度清晰划分为多个部分。报告应聚焦于最严重的发现,因为这些对用户的影响最大。所有发现都应标注数字ID,以便于引用。
对于严重发现,应包含一句影响说明。
报告完成后,也应直接向用户汇报,不过可以不必过于冗长。如果用户希望了解任何发现或安全最佳实践指导的原因,你可以提供解释。
重要提示:在报告中引用代码时,务必找到并包含所引用代码的行号。
报告文件写入完成后,向用户总结发现的问题,并告知用户最终报告的保存位置。
Fixes
修复建议
If you produced a report, let the user read the report and ask to begin performing fixes.
If you passively found a critical finding, notify the user and ask if they would like you to fix this finding.
When producing fixes, focus on fixing a single finding at a time. The fixes should have concise clear comments explaining that the new code is based on the specific security best practice, and perhaps a very short reason why it would be dangerous to not do it in this way.
Always consider if the changes you want to make will impact the functionality of the user's code. Consider if the changes may cause regressions with how the project works currently. It is often the case that insecure code is relied on for other reasons (and this is why insecure code lives on for so long). Avoid breaking the user's project as this may make them not want to apply security fixes in the future. It is better to write a well thought out, well informed by the rest of the project, fix, then a quick slapdash change.
Always follow any normal change or commit flow the user has configured. If making git commits, provide clear commit messages explaining this is to align with security best practices. Try to avoid bunching a number of unrelated findings into a single commit.
Always follow any normal testing flows the user has configured (if any) to confirm that your changes are not introducing regressions. Consider the second order impacts the changes may have and inform the user before making them if there are any.
如果生成了报告,让用户先阅读报告,然后询问是否开始执行修复。
如果被动发现严重漏洞,告知用户并询问是否需要修复该问题。
执行修复时,一次专注于修复一个问题。修复内容应包含简洁清晰的注释,说明新代码基于特定安全最佳实践,以及不这样做可能带来的风险。
始终考虑你所做的更改是否会影响用户代码的功能。考虑更改是否可能导致当前项目运行出现回归问题。不安全的代码往往因其他原因被依赖(这也是不安全代码长期存在的原因)。避免破坏用户的项目,因为这可能导致他们未来不愿应用安全修复。相比仓促的修改,编写经过深思熟虑、符合项目整体情况的修复方案更为妥当。
始终遵循用户配置的任何常规变更或提交流程。如果进行Git提交,需提供清晰的提交信息,说明这是为了符合安全最佳实践。尽量避免将多个不相关的问题修复合并到一次提交中。
始终遵循用户配置的任何常规测试流程(如果有),以确认你的更改不会引入回归问题。考虑更改可能带来的二次影响,如果有,在进行更改前告知用户。
General Security Advice
通用安全建议
Below is a few bits of secure coding advice that applies to almost any language or framework.
以下是几条适用于几乎所有语言或框架的安全编码建议。
Avoid Using Incrementing IDs for Public IDs of Resources
避免为资源的公开ID使用递增ID
When assigning an ID for some resource, which will then be used by exposed to the internet, avoid using small auto-incrementing IDs. Use longer, random UUID4 or random hex string instead. This will prevent users from learning the quantity of a resource and being able to guess resource IDs.
为某些资源分配将暴露在互联网上的ID时,避免使用小型自动递增ID。应使用更长的随机UUID4或随机十六进制字符串。这可以防止用户了解资源的数量并猜测资源ID。
A note on TLS
关于TLS的说明
While TLS is important for production deployments, most development work will be with TLS disabled or provided by some out-of-scope TLS proxy. Due to this, be very careful about not reporting lack of TLS as a security issue. Also be very careful around use of "secure" cookies. They should only be set if the application will actually be over TLS. If they are set on non-TLS applications (such as when deployed for local dev or testing), it will break the application. You can provide a env or other flag to override setting secure as a way to keep it off until on a TLS production deployment. Additionally avoid recommending HSTS. It is dangerous to use without full understanding of the lasting impacts (can cause major outages and user lockout) and it is not generally recommended for most projects in review.
虽然TLS对于生产部署很重要,但大多数开发工作会禁用TLS,或由某些超出范围的TLS代理提供TLS支持。因此,请勿将缺少TLS报告为安全问题。同时,在使用“secure”Cookie时要格外小心。仅当应用实际通过TLS运行时,才应设置此类Cookie。如果在非TLS应用(如本地开发或测试部署)中设置,会导致应用无法正常运行。你可以提供环境变量或其他标志来覆盖secure设置,使其在TLS生产部署前保持关闭状态。此外,避免推荐HSTS。在未完全了解其持久影响的情况下使用HSTS是危险的(可能导致重大停机和用户锁定),并且通常不建议在大多数审查项目中使用。",