网络安全
网站包含几种不同类型的信息。其中一些是非敏感的,例如公共页面上显示的副本。其中一些是敏感的,例如客户用户名、密码和银行信息,或内部算法和私有产品信息。
¥Websites contain several different types of information. Some of it is non-sensitive, for example the copy shown on the public pages. Some of it is sensitive, for example customer usernames, passwords, and banking information, or internal algorithms and private product information.
敏感信息需要受到保护,这是网络安全的重点。如果该信息落入坏人之手,它可以用于:
¥Sensitive information needs to be protected, and that is the focus of web security. If that information fell into the wrong hands, it could be used to:
- 与竞争对手共享信息会使公司处于竞争劣势。
- 禁用或劫持他们的服务,再次导致他们的操作出现严重问题。
- 将客户的 privacy 置于风险之中,使他们容易受到分析、定位、数据丢失、身份被盗,甚至经济损失。
现代浏览器已经具有多种功能来保护用户的网络安全,但开发者还需要使用最佳实践并谨慎编写代码以确保其网站的安全。即使代码中的简单错误也可能导致漏洞,不良行为者可以利用这些漏洞窃取数据并获得对服务的未经授权的控制。
¥Modern browsers already have several features to protect users' security on the web, but developers also need to use best practices and code carefully to ensure that their websites are secure. Even simple bugs in your code can result in vulnerabilities that bad actors can exploit to steal data and gain unauthorized control over services.
本文介绍了网络安全,包括帮助你了解网站漏洞的概念信息以及如何保护它们的实用指南。
¥This article provides an introduction to web security, including conceptual information to help you understand website vulnerabilities and practical guides on how to secure them.
安全与隐私的关系
¥Relationship between security and privacy
安全和隐私是截然不同但密切相关的主题。了解两者之间的差异以及它们之间的关系是值得的。
¥Security and privacy are distinct yet closely related topics. It is worth knowing the differences between the two and how they relate.
- 安全是保护私有数据和系统免受未经授权的访问的行为。这包括公司(内部)数据以及用户和合作伙伴(外部)数据。
- 隐私是指让用户控制如何收集、存储和使用他们的数据,同时确保不会不负责任地使用数据。例如,你应该让用户知道你正在从他们那里收集哪些数据、将与哪些方共享这些数据以及如何使用这些数据。必须让用户有机会同意你的隐私政策,访问你存储的数据,并在他们选择时删除它。
良好的安全性对于良好的隐私至关重要。你可以遵循我们的 网络隐私 指南中列出的所有建议,但如果你的网站不安全并且攻击者无论如何都可以窃取数据,那么诚信行事和制定健全的隐私政策都是徒劳的。
¥Good security is essential for good privacy. You could follow all the advice listed in our Privacy on the web guide, but acting with integrity and having a robust privacy policy are futile if your site is not secure and attackers can just steal data anyway.
浏览器提供的安全功能
¥Security features provided by browsers
Web 浏览器遵循严格的安全模型,该模型强制对内容、浏览器与服务器之间的连接以及数据传输实现强大的安全性。本节介绍支持此模型的功能。
¥Web browsers follow a strict security model that enforces strong security for content, connections between the browser and the server, and data transportation. This section looks at the features that underpin this model.
同源策略和 CORS
¥Same-origin policy and CORS
同源策略 是 Web 的基本安全机制,它限制从一个 origin 加载的文档或脚本如何与来自另一个来源的资源交互。它有助于隔离潜在的恶意文档,减少可能的攻击媒介。
¥Same-origin policy is a fundamental security mechanism of the web that restricts how a document or a script loaded from one origin can interact with a resource from another origin. It helps isolate potentially malicious documents, reducing possible attack vectors.
一般来说,来自一个来源的文件不能向其他来源提出请求。这是有道理的,因为你不希望网站能够相互干扰并访问未经授权的数据。
¥In general, documents from one origin cannot make requests to other origins. This makes sense because you don't want sites to be able to interfere with one another and access unauthorized data.
但是,在某些情况下,你可能希望放宽此限制;例如,如果你有多个相互交互的网站,你可以允许它们使用 fetch()
相互请求资源。这可以使用 跨域资源共享 (CORS) 来实现,跨域资源共享 (CORS) 是一种基于 HTTP 标头的机制,允许服务器指示除其自身之外的任何来源(域、方案或端口),浏览器应允许从中加载资源。
¥However, you might want to relax this restriction in some circumstances; for example, if you have multiple websites that interact with each other, you may allow them to request resources from one another using fetch()
. This can be permitted using Cross-Origin Resource Sharing (CORS), an HTTP-header-based mechanism that allows a server to indicate any origins (domain, scheme, or port) other than its own from which a browser should permit loading resources.
HTTP 通信模型
¥HTTP model for communication
HTTP 协议由 Web 浏览器和服务器用于相互通信、请求资源、提供响应(例如,提供请求的资源或详细说明请求失败的原因)以及为该通信提供安全功能。
¥The HTTP protocol is used by web browsers and servers to communicate with one another, request resources, provide responses (for example, providing a requested resource or detailing why a request failed), and provide security features for that communication.
传输层安全性 (TLS) 通过在网络传输过程中加密数据来提供安全性和隐私性,并且是 HTTPS 协议背后的技术。TLS 有利于隐私,因为它可以阻止第三方拦截传输的数据并恶意使用它。
¥Transport Layer Security (TLS) provides security and privacy by encrypting data during transport over the network and is the technology behind the HTTPS protocol. TLS is good for privacy because it stops third parties from being able to intercept transmitted data and use it maliciously.
所有浏览器都默认要求使用 HTTPS;实际上已经是这样了,因为如果没有这个协议,你就无法在网络上做很多事情。
¥All browsers are moving towards requiring HTTPS by default; this is practically the case already because you can't do much on the web without this protocol.
相关话题:
¥Related topics:
- 传输层安全性(TLS)
-
TLS 协议是使两个联网应用或设备能够私密且稳健地交换信息的标准。使用 TLS 的应用可以选择其安全参数,这会对数据的安全性和可靠性产生重大影响。
- HTTP 严格传输安全
-
Strict-Transport-Security
HTTP 标头让网站指定只能使用 HTTPS 访问它。 - 证书透明度
-
证书透明度 (CT) 是一个开放框架,旨在防止和监控证书错误颁发。新颁发的证书是 'logged',用于公开运行,通常是独立的 CT 日志。这些提供已颁发 TLS 证书的附加式、加密保证记录。
- 混合内容
-
包含使用 cleartext HTTP 获取的内容的 HTTPS 页面称为混合内容页面。像这样的页面仅部分加密,使嗅探器和中间人攻击者可以访问未加密的内容。
- 弱签名算法
-
signing 和 digital certificate 中使用的哈希算法的强度是证书安全性的关键要素。某些签名算法已知较弱,应在适当的时候避免使用。
安全上下文和功能权限
¥Secure contexts and feature permissions
浏览器以不同的方式控制 "强大的功能" 的使用。这些 "强大的功能" 包括在网站上生成系统通知、使用用户的网络摄像头访问媒体流、操纵系统 GPU 以及使用网络支付。如果站点可以不受限制地使用控制此类功能的 API,恶意开发者可能会尝试执行以下操作:
¥Browsers control the usage of "powerful features" in different ways. These "powerful features" include generating system notifications on a website, using a user's webcam to get access to a media stream, manipulating the system GPU, and using web payments. If a site could just use the APIs that control such features without restriction, malicious developers could attempt to do the following:
- 用不需要的通知和其他 UI 功能惹恼用户。
- 在没有警告的情况下打开他们的网络摄像头以监视他们。
- 堵塞他们的浏览器/系统以发起 Denial of Service (DoS) 攻击。
- 窃取数据或金钱。
这些 "强大的功能" 的控制方式如下:
¥These "powerful features" are controlled in the following ways:
-
仅在 安全上下文 中允许使用此类功能。安全上下文是
window
或worker
,有合理的信心认为内容已安全交付(通过 HTTPS/TLS)。在安全上下文中,与不安全上下文进行通信的潜力是有限的。安全上下文还有助于防止 中间人攻击者 访问强大的功能。 要查看仅在安全环境中可用的 Web 平台功能列表,请参阅 仅限于安全上下文的功能。 - 这些功能的使用受到用户权限系统的控制:用户必须明确选择提供对此类功能的访问权限,这意味着它们不能自动使用。用户权限请求会自动发生,你可以使用 权限 API 查询 API 权限的状态。
- 其他几种浏览器功能只能用于响应用户操作(例如单击按钮),这意味着需要从适当的事件处理程序内部调用它们。这称为瞬态激活。请参阅 由用户激活控制的功能 了解更多信息。
高级安全注意事项
¥High-level security considerations
服务器端和客户端需要考虑 Web 安全的许多方面。本节主要关注客户端安全注意事项。你可以在 网站安全(我们的 服务器端网站编程 学习模块的一部分)找到从服务器端角度来看的有用的安全性摘要,其中还包括需要注意的常见攻击的描述。
¥There are many aspects of web security that need to be thought about on the server- and client-side. This section focuses mainly on client-side security considerations. You can find a useful summary of security from a server-side perspective, which also includes descriptions of common attacks to watch out for, at Website security (part of our Server-side website programming learning module).
负责任地存储客户端数据
¥Store client-side data responsibly
负责任地处理数据主要是为了减少 第三方 cookie 的使用,并小心存储和与他们共享的数据。传统上,Web 开发者使用 cookie 来存储各种数据,攻击者很容易利用这种倾向。因此,浏览器已开始限制你可以使用跨站点 cookie 执行的操作,目的是在将来完全删除对它们的访问。
¥Handling data responsibly is largely concerned with cutting down on third-party cookie usage and being careful about the data you store and share with them. Traditionally, web developers have used cookies to store all kinds of data, and it has been easy for attackers to exploit this tendency. As a result, browsers have started to limit what you can do with cross-site cookies, with the aim of removing access to them altogether in the future.
你应该通过限制你依赖的跟踪活动数量和/或通过其他方式实现所需信息的持久性来准备删除跨站点 cookie。有关详细信息,请参阅 从第三方 cookie 转换 和 替换第三方 cookie。
¥You should prepare for the removal of cross-site cookies by limiting the amount of tracking activities you rely on and/or by implementing the persistence of the desired information in other ways. See Transitioning from third-party cookies and Replacing third-party cookies for more information.
保护用户身份并管理登录
¥Protect user identity and manage logins
实现涉及数据收集的安全解决方案时,尤其是数据敏感(例如登录凭据)时,使用知名解决方案是有意义的。例如,任何受人尊敬的服务器端框架都将具有内置功能来防止常见漏洞。你还可以考虑使用专门的产品来实现你的目的,例如身份提供商解决方案或安全的在线调查提供商。
¥When implementing a secure solution that involves data collection, particularly if the data is sensitive such as log-in credentials, it makes sense to use a reputable solution. For example, any respectable server-side framework will have built-in features to protect against common vulnerabilities. You could also consider using a specialized product for your purpose, for example an identity provider solution or a secure online survey provider.
如果你想推出自己的解决方案来收集用户数据,请确保你了解所有方面和要求。聘请经验丰富的服务器端开发者和/或安全工程师来实现系统,并确保对其进行彻底的测试。使用多重身份验证 (MFA) 提供更好的保护。考虑使用专用 API(例如 网页认证 或 联合凭证管理)来简化应用的客户端。
¥If you want to roll your own solution for collecting user data, make sure you understand all aspects and requirements. Hire an experienced server-side developer and/or security engineer to implement the system, and ensure it is tested thoroughly. Use multi-factor authentication (MFA) to provide better protection. Consider using a dedicated API such as Web Authentication or Federated Credential Management to streamline the client-side of the app.
以下是提供安全登录的一些其他提示:
¥Here are some other tips for providing secure logins:
- 收集用户登录信息时,请强制使用强密码,以便你的用户的账户详细信息不会被轻易猜到。弱密码是安全漏洞的主要原因之一。此外,鼓励你的用户使用密码管理器,这样他们就可以使用更复杂的密码,无需担心记住密码,也不会因写下密码而产生安全风险。另请参阅我们关于 密码不安全 的文章。
- 你还应该对用户进行有关网络钓鱼的教育。网络钓鱼是向用户发送消息(例如电子邮件或短信)的行为,其中包含看似用户每天使用的网站的链接,但实际上并非如此。该链接附带一条消息,旨在诱骗用户在网站上输入用户名和密码,以便其被窃取,然后被攻击者用于恶意目的。
注意:一些网络钓鱼网站可能非常复杂,很难与真实网站区分开来。因此,你应该教育你的用户不要信任电子邮件和短信中的随机链接。如果他们收到类似 "紧急,你需要立即登录才能解决问题" 的消息,他们应该直接在新选项卡中转到该站点并尝试直接登录,而不是单击消息中的链接。或者他们可以给你打调用或发送电子邮件讨论他们收到的消息。
¥Note: Some phishing sites can be very sophisticated and hard to distinguish from a real website. You should therefore educate your users to not trust random links in emails and SMS messages. If they receive a message along the lines of "Urgent, you need to log in now to resolve an issue", they should go to the site directly in a new tab and try logging in directly rather than clicking the link in the message. Or they could phone or email you to discuss the message they received.
- 使用 速率限制 防止对登录页面进行暴力攻击,在一定次数的不成功尝试后锁定账户,以及 验证码挑战。
- 使用唯一的 会话 ID 管理用户登录会话,并在用户不活动一段时间后自动注销。
不要在 URL 查询字符串中包含敏感数据
¥Don't include sensitive data in URL query strings
一般来说,你不应该使用 在 URL 查询字符串中包含敏感数据,因为如果第三方拦截 URL(例如,通过 Referer
HTTP 标头),他们可能会窃取该信息。更严重的是,这些 URL 可以被公共网络爬虫、HTTP 代理和存档工具(如 互联网档案馆)编入索引,这意味着你的敏感数据可能会保留在可公开访问的资源上。
¥As a general rule, you shouldn't include sensitive data in URL query strings because if a third party intercepts the URL (for example, via the Referer
HTTP header), they could steal that information. Even more serious is the fact that these URLs can be indexed by public web crawlers, HTTP proxies, and archiving tools such as the internet archive, meaning that your sensitive data could persist on publicly accessible resources.
使用 POST
请求而不是 GET
请求可以避免这些问题。我们的文章 Referer 标头策略:隐私和安全问题 更详细地描述了与 Referer
标头相关的隐私和安全风险,并提供了减轻这些风险的建议。
¥Use POST
requests rather than GET
requests to avoid these issues. Our article Referer header policy: Privacy and security concerns describes in more detail the privacy and security risks associated with the Referer
header, and offers advice on mitigating those risks.
注意:避免通过
GET
请求传输 URL 中的敏感数据也有助于防范 cross-site request forgery 和 重放攻击。¥Note: Steering away from transmitting sensitive data in URLs via
GET
requests can also help protect against cross-site request forgery and replay attacks.
执行使用政策
¥Enforce usage policies
考虑使用 Web 平台功能(如 内容安全政策 (CSP) 和 权限策略)在你的网站上强制执行一组功能和资源使用规则,使引入漏洞变得更加困难。
¥Consider using web platform features like Content Security Policy (CSP) and Permissions Policy to enforce a set of feature and resource usage rules on your website that make it harder to introduce vulnerabilities.
CSP 允许你添加一层安全性,例如,允许仅从特定的受信任来源加载图片或脚本。这有助于检测和减轻某些类型的攻击,包括跨站点脚本攻击 (XSS) 和数据注入攻击。这些攻击涉及一系列恶意活动,包括数据盗窃、网站篡改和恶意软件传播。
¥CSP allows you to add a layer of security by, for example, allowing images or scripts to be loaded only from specific trusted origins. This helps to detect and mitigate certain types of attacks, including Cross-Site Scripting (XSS) and data injection attacks. These attacks involve a range of malicious activities, including data theft, site defacement, and distribution of malware.
权限策略的工作方式类似,只不过它更关注允许或阻止对特定 "强大的功能" (如前面提到的) 的访问。
¥Permissions policy works in a similar way, except that it is more concerned with allowing or blocking access to specific "powerful features" (as mentioned earlier).
注意:此类政策对于帮助确保网站安全非常有用,尤其是当你在网站上使用大量第三方代码时。但是,请记住,如果你阻止使用第三方脚本所依赖的功能,则最终可能会破坏站点的功能。
¥Note: Such policies are very useful to help keep sites secure, especially when you are using a lot of third-party code on your site. However, keep in mind that if you block usage of a feature that a third-party script relies on to work, you may end up breaking your site's functionality.
维护数据完整性
¥Maintain data integrity
接着上一节,当你允许在你的网站上使用功能和资源时,你应该尝试确保资源未被篡改。
¥Following on from the previous section, when you allow feature and resource usage on your site, you should try to ensure that resources have not been tampered with.
相关话题:
¥Related topics:
- 子资源完整性
-
子资源完整性 (SRI) 是一项安全功能,使浏览器能够验证它们获取的资源(例如,从 CDN)是否在没有意外操作的情况下交付。它的工作原理是允许你提供所获取的资源必须匹配的加密哈希。
- HTTP 访问控制允许来源
-
Access-Control-Allow-Origin
响应标头指示是否可以与来自给定 origin 的请求代码共享该响应。 - HTTP X-内容类型-选项
-
X-Content-Type-Options
响应标头是服务器用来指示Content-Type
标头中公布的 MIME 类型 不应更改且必须遵循的标记。此标头是一种选择退出 MIME 类型嗅探 的方法,或者换句话说,指定 MIME 类型是故意配置的。
清理表单输入
¥Sanitize form input
作为一般规则,不要信任用户在表单中输入的任何内容。在线填写表格既复杂又繁琐,用户很容易输入不正确的数据或格式错误的数据。此外,恶意人员擅长在表单字段中输入特定的可执行代码字符串(例如,SQL 或 JavaScript)。如果你在处理此类输入时不小心,它们可能会在你的网站上执行有害代码或删除你的数据库。有关如何发生这种情况的一个很好的示例,请参阅 SQL 注入。
¥As a general rule, don't trust anything that users enter into forms. Filling out forms online is complicated and tedious, and it is easy for users to enter incorrect data or data in the wrong format. In addition, malicious folks are skilled in the art of entering specific strings of executable code into form fields (for example, SQL or JavaScript). If you're not careful about handling such inputs, they could either execute harmful code on your site or delete your databases. See SQL injection for a good example of how this could happen.
为了防止这种情况,你应该彻底清理表单中输入的数据:
¥To protect against this, you should thoroughly sanitize data entered into your forms:
- 你应该实现客户端验证,以便在用户以错误格式输入数据时通知他们。你可以使用内置的 HTML 表单验证功能来完成此操作,也可以编写自己的验证代码。请参阅 客户端表单验证 了解更多信息。
- 在应用 UI 中显示用户输入时,你应该使用输出编码,以安全地显示与用户输入完全相同的数据,并避免将其作为代码执行。请参阅 输出编码 了解更多信息。
你不能仅依靠客户端验证来确保安全性 - 它应该与服务器端验证相结合。客户端验证通过提供即时验证反馈来增强用户体验,而无需等待往返服务器。但是,恶意方很容易绕过客户端验证(例如,通过关闭浏览器中的 JavaScript 来绕过基于 JavaScript 的验证)。
¥You can't rely on client-side validation alone for security — it should be combined with server-side validation. Client-side validation enhances the user experience by providing instant validation feedback without having to wait for a round trip to the server. However, client-side validation is easy for a malicious party to bypass (for example, by turning off JavaScript in the browser to bypass JavaScript-based validation).
任何信誉良好的服务器端框架都会提供验证表单提交的功能。此外,常见的最佳实践是转义构成可执行语法一部分的任何特殊字符,从而使任何输入的代码不再可执行并被视为纯文本。
¥Any reputable server-side framework will provide functionality for validating form submissions. In addition, a common best practice is to escape any special characters that form part of executable syntax, thereby making any entered code no longer executable and treated as plain text.
防止点击劫持
¥Protect against clickjacking
在 clickjacking 攻击中,用户被欺骗单击执行与用户预期不同的操作的 UI 元素,通常会导致用户的密钥信息被传递给恶意的第三方。此风险是嵌入的第三方内容所固有的,因此请确保你信任嵌入到你网站的内容。此外,请注意点击劫持可以与网络钓鱼技术相结合。你可以在上一节 保护用户身份并管理登录 中阅读有关网络钓鱼的信息。
¥In a clickjacking attack, a user is fooled into clicking a UI element that performs an action different from what the user expects, often resulting in the user's confidential information being passed to a malicious third party. This risk is inherent in embedded third-party content, so make sure you trust what is being embedded into your site. Additionally, be aware that clickjacking can be combined with phishing techniques. You can read about phishing in the previous section Protect user identity and manage logins.
以下功能可以帮助防止点击劫持:
¥The following features can help guard against clickjacking:
- HTTP X 帧选项
-
X-Frame-Options
HTTP 响应标头可用于指示是否应允许浏览器以<frame>
、<iframe>
、<embed>
或<object>
呈现页面。网站可以利用这一点来避免 clickjacking 攻击,确保其内容不会嵌入到其他网站中。 - CSP:frame-ancestors
-
HTTP
Content-Security-Policy
(CSP)frame-ancestors
指令指定可以使用<frame>
、<iframe>
、<object>
或<embed>
嵌入页面的有效父级。
实际安全实现指南
¥Practical security implementation guides
要获得在网站上有效实现安全功能的全面说明,并确保你遵循最佳实践,请参阅我们的 实际安全实现指南 集。
¥To get comprehensive instructions for implementing security features effectively on websites and to ensure you're following best practices, see our set of Practical security implementation guides.
其中一些指南与 HTTP 观测站 工具直接相关。Observatory 对网站进行安全审核,并提供等级和分数以及修复其发现的安全问题的建议。这些指南解释了如何解决 MDN Observatory 测试中出现的问题:该工具链接到每个问题的相关指南,帮助你找到有效的解决方案。有趣的是,Mozilla 的内部开发团队在实现网站时使用此指南来确保应用安全最佳实践。
¥Some of these guides are directly related to the HTTP Observatory tool. Observatory performs security audits on a website and provides a grade and score along with recommendations for fixing the security issues it finds. These guides explain how to resolve issues surfaced by the MDN Observatory tests: the tool links to the relevant guide for each issue, helping guide you towards an effective resolution. Interestingly, Mozilla's internal developer teams use this guidance when implementing websites to ensure that security best practices are applied.
也可以看看
¥See also