Skip to content

Commit ac284b1

Browse files
author
YuChengKai
committed
finish safety translate
1 parent c92ef57 commit ac284b1

File tree

2 files changed

+157
-16
lines changed

2 files changed

+157
-16
lines changed

Safety/safety-cn.md

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@
1818

1919
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
2020

21-
#### XSS
21+
## XSS
2222

2323
> **跨网站指令码**(英语:Cross-site scripting,通常简称为:XSS)是一种网站应用程式的安全漏洞攻击,是[代码注入](https://www.wikiwand.com/zh-hans/%E4%BB%A3%E7%A2%BC%E6%B3%A8%E5%85%A5)的一种。它允许恶意使用者将程式码注入到网页上,其他使用者在观看网页时就会受到影响。这类攻击通常包含了 HTML 以及使用者端脚本语言。
2424
2525
XSS 分为三种:反射型,存储型和 DOM-based
2626

27-
##### 如何攻击
27+
### 如何攻击
2828

2929
XSS 通过修改 HTML 节点或者执行 JS 代码来攻击网站。
3030

@@ -39,7 +39,7 @@ XSS 通过修改 HTML 节点或者执行 JS 代码来攻击网站。
3939

4040
也有另一种场景,比如写了一篇包含攻击代码 `<script>alert(1)</script>` 的文章,那么可能浏览文章的用户都会被攻击到。这种攻击类型是存储型攻击,也可以说是 DOM-based 攻击,并且这种攻击打击面更广。
4141

42-
##### 如何防御
42+
### 如何防御
4343

4444
最普遍的做法是转义输入输出的内容,对于引号,尖括号,斜杠进行转义
4545

@@ -74,7 +74,7 @@ console.log(html);
7474

7575
以上示例使用了 `js-xss` 来实现。可以看到在输出中保留了 `h1` 标签且过滤了 `script` 标签
7676

77-
##### CSP
77+
### CSP
7878

7979
> 内容安全策略 ([CSP](https://developer.mozilla.org/en-US/docs/Glossary/CSP)) 是一个额外的安全层,用于检测并削弱某些特定类型的攻击,包括跨站脚本 ([XSS](https://developer.mozilla.org/en-US/docs/Glossary/XSS)) 和数据注入攻击等。无论是数据盗取、网站内容污染还是散发恶意软件,这些攻击都是主要的手段。
8080
@@ -102,13 +102,13 @@ console.log(html);
102102

103103
更多属性可以查看 [这里](https://content-security-policy.com/)
104104

105-
#### CSRF
105+
## CSRF
106106

107107
> **跨站请求伪造**(英语:Cross-site request forgery),也被称为 **one-click attack** 或者 **session riding**,通常缩写为 **CSRF** 或者 **XSRF**, 是一种挟制用户在当前已登录的Web应用程序上执行非本意的操作的攻击方法。[[1\]](https://www.wikiwand.com/zh/%E8%B7%A8%E7%AB%99%E8%AF%B7%E6%B1%82%E4%BC%AA%E9%80%A0#citenoteRistic1)[跨網站指令碼](https://www.wikiwand.com/zh/%E8%B7%A8%E7%B6%B2%E7%AB%99%E6%8C%87%E4%BB%A4%E7%A2%BC)(XSS)相比,**XSS** 利用的是用户对指定网站的信任,CSRF 利用的是网站对用户网页浏览器的信任。
108108
109109
简单点说,CSRF 就是利用用户的登录态发起恶意请求。
110110

111-
##### 如何攻击
111+
### 如何攻击
112112

113113
假设网站中有一个通过 Get 请求提交用户评论的接口,那么攻击者就可以在钓鱼网站中加入一个图片,图片的地址就是评论接口
114114

@@ -124,7 +124,7 @@ console.log(html);
124124
</form>
125125
```
126126

127-
##### 如何防御
127+
### 如何防御
128128

129129
防范 CSRF 可以遵循以下几种规则:
130130

@@ -133,23 +133,23 @@ console.log(html);
133133
3. 阻止第三方网站请求接口
134134
4. 请求时附带验证信息,比如验证码或者 token
135135

136-
###### SameSite
136+
#### SameSite
137137

138138
可以对 Cookie 设置 `SameSite` 属性。该属性设置 Cookie 不随着跨域请求发送,该属性可以很大程度减少 CSRF 的攻击,但是该属性目前并不是所有浏览器都兼容。
139139

140-
###### 验证 Referer
140+
#### 验证 Referer
141141

142142
对于需要防范 CSRF 的请求,我们可以通过验证 Referer 来判断该请求是否为第三方网站发起的。
143143

144-
###### Token
144+
#### Token
145145

146146
服务器下发一个随机 Token(算法不能复杂),每次发起请求时将 Token 携带上,服务器验证 Token 是否有效。
147147

148-
#### 密码安全
148+
## 密码安全
149149

150150
密码安全虽然大多是后端的事情,但是作为一名优秀的前端程序员也需要熟悉这方面的知识。
151151

152-
##### 加盐
152+
### 加盐
153153

154154
对于密码存储来说,必然是不能明文存储在数据库中的,否则一旦数据库泄露,会对用户造成很大的损失。并且不建议只对密码单纯通过加密算法加密,因为存在彩虹表的关系。
155155

@@ -161,7 +161,3 @@ sha256(sha1(md5(salt + password + slat)))
161161
```
162162

163163
但是加盐并不能阻止别人盗取账号,只能确保即使数据库泄露,也不会暴露用户的真实密码。一旦攻击者得到了用户的账号,可以通过暴力破解的方式破解密码。对于这种情况,通常使用验证码增加延时或者限制尝试次数的方式。并且一旦用户输入了错误的密码,也不能直接提示用户输错密码,而应该提示账号或密码错误。
164-
165-
##### 前端加密
166-
167-
虽然前端加密对于安全防护来说意义不大,但是在遇到中间人攻击的情况下,可以避免明文密码被第三方获取。

Safety/safety-en.md

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
## XSS
2+
3+
> **Cross-site scripting**(Cross-site scripting in English, often abbreviated as XSS)is one kind of security vulnerabilities attack of web applications , and is a kind of [code input](https://www.wikiwand.com/zh-hans/%E4%BB%A3%E7%A2%BC%E6%B3%A8%E5%85%A5)It allows malicious users to input code into web pages, and other users are affected when they browse web pages. Such attacks often include HTML and consumer-side scripting languages.
4+
5+
XSS is divided into three types: reflective type, storage type, and DOM-based type
6+
7+
### How to attack
8+
9+
XSS attacks websites by modifying HTML nodes or run JS code.
10+
11+
For example, get some parameters through the URL
12+
13+
```html
14+
<!-- http://www.domain.com?name=<script>alert(1)</script> -->
15+
<div>{{name}}</div>
16+
```
17+
18+
The URL input above may change the HTML into `<div><script>alert(1)</script></div>` so that there is an extra executable script out of the page. This type of attack is a reflection attack, or DOM-based attack
19+
20+
There is also another scenario. For example, if you write an article that contains the attack code `<script>alert(1)</script>`, then users who may be browsing the article will be attacked. This type of attack is a store attack, which can also be called a DOM-based attack.
21+
22+
### How to defend
23+
24+
The most common practice is to escape the input and output, escape the quotes, angle brackets, and slashes.
25+
26+
```js
27+
function escape(str) {
28+
str = str.replace(/&/g, "&amp;");
29+
str = str.replace(/</g, "&lt;");
30+
str = str.replace(/>/g, "&gt;");
31+
str = str.replace(/"/g, "&quto;");
32+
str = str.replace(/'/g, "&#39;");
33+
str = str.replace(/`/g, "&#96;");
34+
str = str.replace(/\//g, "&#x2F;");
35+
return str
36+
}
37+
```
38+
39+
The attack code `<script>alert(1)</script>` can be changed by escaping
40+
41+
```js
42+
// -> &lt;script&gt;alert(1)&lt;&#x2F;script&gt;
43+
escape('<script>alert(1)</script>')
44+
```
45+
46+
For displaying rich text, all characters cannot be escaped by the above method, because this will filter out the required format. This kind of situation usually adopts the method of the white list to filter, certainly can also pass the black list to filter, but consider the too many labels and attribute that need to filter, it is more recommended to use the white list way.
47+
48+
```js
49+
var xss = require("xss");
50+
var html = xss('<h1 id="title">XSS Demo</h1><script>alert("xss");</script>');
51+
// -> <h1>XSS Demo</h1>&lt;script&gt;alert("xss");&lt;/script&gt;
52+
console.log(html);
53+
```
54+
55+
The above example uses `js-xss` to implement. You can see that the `h1` tag is preserved in the output and the `script` tag is filtered.
56+
57+
### CSP
58+
59+
The Content Security Policy ([CSP] (https://developer.mozilla.org/en-US/docs/Glossary/CSP)) is an additional layer of security that detects and undermines certain types of attacks, including Cross-site scripting ([XSS] (https://developer.mozilla.org/en-US/docs/Glossary/XSS)) and data injection attacks. Whether it's data theft, website content contamination or malware, these attacks are the primary means.
60+
61+
We can minimize XSS attacks with CSP. CSP is also essentially whitelisted, which stipulates that browsers can only execute code from a specific source.
62+
63+
You can usually enable the CSP with the `Content-Security-Policy` in the HTTP Header.
64+
65+
- Only allow loading of self-site resource
66+
67+
```http
68+
Content-Security-Policy: default-src ‘self’
69+
```
70+
71+
- Only allow loading HTTPS protocol pictures
72+
73+
```http
74+
Content-Security-Policy: img-src https://*
75+
```
76+
77+
- Allow loading of any source frame
78+
79+
```http
80+
Content-Security-Policy: child-src 'none'
81+
```
82+
83+
More attributes can be viewed at [here] (https://content-security-policy.com/)
84+
85+
## CSRF
86+
87+
> **Cross-site request forgery (English: Cross-site request forgery), also known as **one-click attack** or **session riding**, usually abbreviated as **CSRF** or **XSRF** is an attack method that forces users to perform unintended operations on currently logged-in web applications. [[1\]](https://www.wikiwand.com/zh/%E8%B7%A8%E7%AB%99%E8%AF%B7%E6%B1%82%E4%BC%AA%E9%80%A0#citenoteRistic1) Follow [cross-site script](https://www.wikiwand.com/zh/%E8%B7%A8%E7%B6%B2%E7%AB%99%E6%8C%87%E4%BB%A4%E7%A2%BC) (XSS) Compared to **XSS**, users trust the designated website and CSRF uses the website's trust in the user's web browser.
88+
89+
To put it simply, CSRF uses the login state of the user to initiate a malicious request.
90+
91+
### How to attack
92+
93+
Assume that there is an interface on the site that submits user comments through a Get request. The attacker can then add a picture to the phishing site. The address of the picture is the comment interface.
94+
95+
```html
96+
<img src="http://www.domain.com/xxx?comment='attack'"/>
97+
```
98+
99+
If the interface is submitted by the Post, it is relatively troublesome, you need to use the form to submit the interface.
100+
101+
```html
102+
<form action="http://www.domain.com/xxx" id="CSRF" method="post">
103+
<input name="comment" value="attack" type="hidden">
104+
</form>
105+
```
106+
107+
### How to defend
108+
109+
There are several rules for defending against CSRF:
110+
111+
1. Get request does not modify the data
112+
2. Do not allow third-party websites to access user cookies
113+
3. Block third-party website request interfaces
114+
4. Request verification information, such as verification code or token
115+
116+
#### SameSite
117+
118+
The `SameSite` attribute can be set on cookies. This attribute sets the cookie not to be sent along with cross-domain requests. This attribute can greatly reduce the CSRF attack, but this attribute is currently not compatible with all browsers.
119+
120+
#### Verify Referer
121+
122+
For requests that need protection against CSRF, we can verify the Referer to determine if the request was initiated by a third-party website.
123+
124+
#### Token
125+
126+
The server sends a random Token (the algorithm cannot be complex). The Token is carried on each request, and the server verifies that the Token is valid.
127+
128+
## Password security
129+
130+
Although password security is mostly a back-end thing, as a good front-end programmer, you need to be familiar with this knowledge.
131+
132+
### Add salt
133+
134+
For password storage, it must be stored in the database in the clear, otherwise, once the database is leaked, it will cause great losses to the user. And it is not recommended to encrypt the password only by the encryption algorithm because of the rainbow table relationship.
135+
136+
It is usually necessary to add salt to the password and then perform several encryptions with different encryption algorithms.
137+
138+
It is often necessary to add a salt to the password and then encrypt it several times with different encryption algorithms.
139+
140+
```js
141+
// Adding salt means adding a string to the original password and increasing the length of the original password.
142+
sha256(sha1(md5(salt + password + slat)))
143+
```
144+
145+
But adding salt does not prevent others from stealing accounts. It only ensures that even if the database is compromised, the user's real password will not be exposed. Once the attacker gets the user's account, the password can be cracked by brute force. In this case, a verification code is usually used to increase the delay or limit the number of attempts. And once the user enters the wrong password, the user cannot directly prompt the user to enter the wrong password, but should prompt the account or password to be incorrect.

0 commit comments

Comments
 (0)