twikoo-func 1.7.23 → 1.7.24

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.js CHANGED
@@ -35,6 +35,7 @@ const {
35
35
  getConfig,
36
36
  getConfigForAdmin,
37
37
  validate,
38
+ validateClientFields,
38
39
  checkCommentOwnership,
39
40
  isValidEmail
40
41
  } = require('./utils')
@@ -87,6 +88,8 @@ exports.main = async (event, context) => {
87
88
  let res = {}
88
89
  try {
89
90
  protect()
91
+ // 统一校验客户端字段类型,防止查询操作符对象注入数据库查询条件
92
+ validateClientFields(event)
90
93
  await readConfig()
91
94
  switch (event.event) {
92
95
  case 'GET_FUNC_VERSION':
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "twikoo-func",
3
- "version": "1.7.23",
3
+ "version": "1.7.24",
4
4
  "description": "A simple comment system.",
5
5
  "author": "imaegoo <hello@imaegoo.com> (https://github.com/imaegoo)",
6
6
  "license": "MIT",
package/utils/index.js CHANGED
@@ -469,9 +469,33 @@ const fn = {
469
469
  }
470
470
  }
471
471
  },
472
+ // 客户端字段类型校验,防止 NoSQL 查询条件注入。
473
+ // 以下字段会直接参与数据库查询或作为评论归属标识,
474
+ // 如果传入对象,会被数据库解释为查询操作符(如 $ne、$gt),
475
+ // 导致越权删除评论、绕过评论可见性限制等问题。
476
+ // 允许字段缺省或为 null(兼容首次匿名请求),有值时必须是字符串。
477
+ validateClientFields (event = {}) {
478
+ const stringFields = ['accessToken', 'id', 'url', 'pid', 'rid']
479
+ for (const field of stringFields) {
480
+ const value = event[field]
481
+ if (value !== undefined && value !== null && typeof value !== 'string') {
482
+ throw new Error(`参数"${field}"必须是字符串`)
483
+ }
484
+ }
485
+ if (event.urls !== undefined && event.urls !== null) {
486
+ if (!Array.isArray(event.urls) || event.urls.some((url) => typeof url !== 'string')) {
487
+ throw new Error('参数"urls"必须是字符串数组')
488
+ }
489
+ }
490
+ },
472
491
  // 校验评论归属:确认评论存在且属于当前用户
473
492
  async checkCommentOwnership (id, uid, getComment) {
474
493
  fn.validate({ id }, ['id'])
494
+ // 兜底校验:id 必须是字符串,防止查询操作符对象注入 _id 条件,
495
+ // 绕过归属校验后批量删除评论
496
+ if (typeof id !== 'string') {
497
+ throw new Error('参数"id"必须是字符串')
498
+ }
475
499
  const comment = await getComment(id)
476
500
  if (!comment) {
477
501
  throw new Error('评论不存在')