Skip to content

Commit 4a145a5

Browse files
authored
fix: 违禁词检测增加对昵称检测 (#354)
* fix: 违禁词检测增加对昵称检测 * fix: 优化安全域名判断 * fix: bump version
1 parent 08d5c9a commit 4a145a5

File tree

10 files changed

+47
-24
lines changed

10 files changed

+47
-24
lines changed

docs/.vuepress/theme/layouts/Layout.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
<!-- Twikoo -->
1515
<div id="twikoo"></div>
16-
<script src="https://cdn.jsdelivr.net/npm/twikoo@1.5.4/dist/twikoo.all.min.js" ref="twikooJs"></script>
16+
<script src="https://cdn.jsdelivr.net/npm/twikoo@1.5.5/dist/twikoo.all.min.js" ref="twikooJs"></script>
1717
</div>
1818
</template>
1919
</ParentLayout>

docs/quick-start.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ exports.main = require('twikoo-func').main
4444
8. 创建完成后,点击“twikoo"进入云函数详情页,进入“函数代码”标签,点击“文件 - 新建文件”,输入 `package.json`,回车
4545
9. 复制以下代码、粘贴到代码框中,点击“保存并安装依赖”
4646
``` json
47-
{ "dependencies": { "twikoo-func": "1.5.4" } }
47+
{ "dependencies": { "twikoo-func": "1.5.5" } }
4848
```
4949

5050
### 命令行部署
@@ -175,7 +175,7 @@ twikoo:
175175

176176
``` html
177177
<div id="tcomment"></div>
178-
<script src="https://cdn.jsdelivr.net/npm/twikoo@1.5.4/dist/twikoo.all.min.js"></script>
178+
<script src="https://cdn.jsdelivr.net/npm/twikoo@1.5.5/dist/twikoo.all.min.js"></script>
179179
<script>
180180
twikoo.init({
181181
envId: '您的环境id', // 腾讯云环境填 envId;Vercel 环境填地址(https://xxx.vercel.app)
@@ -193,7 +193,7 @@ twikoo.init({
193193

194194
请参考爆米兔前端静态资源库 [https://cdn.baomitu.com/twikoo](https://cdn.baomitu.com/twikoo)
195195

196-
引入的 CDN 链接替换为如下即可:`https://lib.baomitu.com/twikoo/1.5.4/twikoo.all.min.js`
196+
引入的 CDN 链接替换为如下即可:`https://lib.baomitu.com/twikoo/1.5.5/twikoo.all.min.js`
197197

198198
## 开启管理面板
199199

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "twikoo",
3-
"version": "1.5.4",
3+
"version": "1.5.5",
44
"description": "A simple comment system based on Tencent CloudBase (tcb).",
55
"keywords": ["twikoojs", "comment", "comment-system", "cloudbase", "vercel"],
66
"author": "imaegoo <hello@imaegoo.com> (https://github.com/imaegoo)",

src/function/twikoo/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1123,7 +1123,7 @@ async function parse (comment) {
11231123
comment: DOMPurify.sanitize(comment.comment, { FORBID_TAGS: ['style'], FORBID_ATTR: ['style'] }),
11241124
pid: comment.pid ? comment.pid : comment.rid,
11251125
rid: comment.rid,
1126-
isSpam: isAdminUser ? false : preCheckSpam(comment.comment),
1126+
isSpam: isAdminUser ? false : preCheckSpam(comment),
11271127
created: timestamp,
11281128
updated: timestamp
11291129
}
@@ -1171,7 +1171,7 @@ async function limitFilter () {
11711171
}
11721172

11731173
// 预垃圾评论检测
1174-
function preCheckSpam (comment) {
1174+
function preCheckSpam ({ comment, nick }) {
11751175
// 长度限制
11761176
let limitLength = parseInt(config.LIMIT_LENGTH)
11771177
if (Number.isNaN(limitLength)) limitLength = 500
@@ -1185,7 +1185,7 @@ function preCheckSpam (comment) {
11851185
} else if (config.FORBIDDEN_WORDS) {
11861186
// 违禁词检测
11871187
for (const forbiddenWord of config.FORBIDDEN_WORDS.split(',')) {
1188-
if (comment.indexOf(forbiddenWord.trim()) !== -1) {
1188+
if (comment.indexOf(forbiddenWord.trim()) !== -1 || nick.indexOf(forbiddenWord.trim()) !== -1) {
11891189
console.log('包含违禁词,直接标记为垃圾评论~')
11901190
return true
11911191
}

src/function/twikoo/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "twikoo-func",
3-
"version": "1.5.4",
3+
"version": "1.5.5",
44
"description": "A simple comment system based on Tencent CloudBase (tcb).",
55
"author": "imaegoo <hello@imaegoo.com> (https://github.com/imaegoo)",
66
"license": "MIT",

src/js/utils/i18n/i18n.js

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,17 @@ const imageBedServices = [
8888
'smms'
8989
].map(s => `"${s}"`)
9090

91+
const defaultGravatar = [
92+
'404',
93+
'mp',
94+
'identicon',
95+
'monsterid',
96+
'wavatar',
97+
'retro',
98+
'robohash',
99+
'blank'
100+
].map(s => `"${s}"`)
101+
91102
/**
92103
* 把所有语言翻译放在同一对象下可以减小打包 js 的体积 (~17kb)
93104
*
@@ -298,16 +309,16 @@ export default {
298309
'Comment placeholder. Use <br> to start a newline. Default: empty.'
299310
],
300311
[S.ACI + '_CORS_ALLOW_ORIGIN']: [
301-
'Vercel 安全域名,防止环境被盗用,请注意设置后将无法在本地(localhost)加载评论,默认为空',
302-
'Vercel 安全域名,防止環境被盜用,請注意設置後將無法在本地(localhost)加載評論,默認為空',
303-
'Vercel 安全域名,防止環境被盜用,請注意設置後將無法在本地(localhost)加載評論,默認為空',
304-
'Vercel security domain name to prevent the environment from being stolen, please note that after setting, you will not be able to load comments locally (localhost), the default is empty'
312+
'Vercel CORS 安全域名,注意:错误设置会导致无法加载,默认为空,格式为 https://blog.example.com',
313+
'Vercel CORS 安全域名,注意:错误设置会导致无法加載,默認為空,格式为 https://blog.example.com',
314+
'Vercel CORS 安全域名,注意:错误设置会导致无法加載,默認為空,格式为 https://blog.example.com',
315+
'Vercel CORS allow origin, note: incorrect settings can cause loading failure. Default: blank, format: https://blog.example.com'
305316
],
306317
[S.ACI + '_DEFAULT_GRAVATAR']: [
307-
'默认的头像显示。默认值为 "identicon",可选: 404、mp、identicon、monsterid、wavatar、retro、robohash、blank',
308-
'預設的頭像顯示。預設值為 "identicon",可選: 404、mp、identicon、monsterid、wavatar、retro、robohash、blank',
309-
'預設的頭像顯示。預設值為 "identicon",可選: 404、mp、identicon、monsterid、wavatar、retro、robohash、blank',
310-
'Avatar placeholder. Default: "identicon". Choose from: 404, mp, identicon, monsterid, wavatar, retro, robohash, blank.'
318+
`默认的头像显示。默认值为 "identicon",可选:${defaultGravatar.join('、')}`,
319+
`預設的頭像顯示。預設值為 "identicon",可選:${defaultGravatar.join('、')}`,
320+
`預設的頭像顯示。預設值為 "identicon",可選:${defaultGravatar.join('、')}`,
321+
`Avatar placeholder. Default: "identicon". Choose from: ${defaultGravatar.join(', ')}`
311322
],
312323
[S.ACI + '_EMOTION_CDN']: [
313324
'表情 CDN,默认为:https://cdn.jsdelivr.net/gh/imaegoo/emotion/owo.json',

src/vercel-min/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{ "dependencies": { "twikoo-vercel": "1.5.4" } }
1+
{ "dependencies": { "twikoo-vercel": "1.5.5" } }

src/vercel/api/index.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ module.exports = async (requestArg, responseArg) => {
163163
function allowCors () {
164164
if (request.headers.origin) {
165165
response.setHeader('Access-Control-Allow-Credentials', true)
166-
response.setHeader('Access-Control-Allow-Origin', config.CORS_ALLOW_ORIGIN || request.headers.origin)
166+
response.setHeader('Access-Control-Allow-Origin', getAllowedOrigin())
167167
response.setHeader('Access-Control-Allow-Methods', 'POST')
168168
response.setHeader(
169169
'Access-Control-Allow-Headers',
@@ -172,6 +172,18 @@ function allowCors () {
172172
}
173173
}
174174

175+
function getAllowedOrigin () {
176+
const localhostRegex = /^https?:\/\/(localhost|127\.0\.0\.1|0\.0\.0\.0)(:\d{1,5})?$/
177+
if (localhostRegex.test(request.headers.origin)) {
178+
return request.headers.origin
179+
} else if (config.CORS_ALLOW_ORIGIN) {
180+
// 许多用户设置安全域名时,喜欢带结尾的 "/",必须处理掉
181+
return config.CORS_ALLOW_ORIGIN.replace(/\/$/, '')
182+
} else {
183+
return request.headers.origin
184+
}
185+
}
186+
175187
function anonymousSignIn () {
176188
if (request.body) {
177189
if (request.body.accessToken) {
@@ -1133,7 +1145,7 @@ async function parse (comment) {
11331145
comment: DOMPurify.sanitize(comment.comment, { FORBID_TAGS: ['style'], FORBID_ATTR: ['style'] }),
11341146
pid: comment.pid ? comment.pid : comment.rid,
11351147
rid: comment.rid,
1136-
isSpam: isAdminUser ? false : preCheckSpam(comment.comment),
1148+
isSpam: isAdminUser ? false : preCheckSpam(comment),
11371149
created: timestamp,
11381150
updated: timestamp
11391151
}
@@ -1177,7 +1189,7 @@ async function limitFilter () {
11771189
}
11781190

11791191
// 预垃圾评论检测
1180-
function preCheckSpam (comment) {
1192+
function preCheckSpam ({ comment, nick }) {
11811193
// 长度限制
11821194
let limitLength = parseInt(config.LIMIT_LENGTH)
11831195
if (Number.isNaN(limitLength)) limitLength = 500
@@ -1191,7 +1203,7 @@ function preCheckSpam (comment) {
11911203
} else if (config.FORBIDDEN_WORDS) {
11921204
// 违禁词检测
11931205
for (const forbiddenWord of config.FORBIDDEN_WORDS.split(',')) {
1194-
if (comment.indexOf(forbiddenWord.trim()) !== -1) {
1206+
if (comment.indexOf(forbiddenWord.trim()) !== -1 || nick.indexOf(forbiddenWord.trim()) !== -1) {
11951207
console.log('包含违禁词,直接标记为垃圾评论~')
11961208
return true
11971209
}

src/vercel/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "twikoo-vercel",
3-
"version": "1.5.4",
3+
"version": "1.5.5",
44
"description": "A simple comment system based on Tencent CloudBase (tcb).",
55
"author": "imaegoo <hello@imaegoo.com> (https://github.com/imaegoo)",
66
"license": "MIT",

src/version.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
const version = '1.5.4'
1+
const version = '1.5.5'
22

33
export { version }

0 commit comments

Comments
 (0)