From be60104f2129535bec5bd4fb68fcd84f5a7498c9 Mon Sep 17 00:00:00 2001 From: lik Date: Sat, 24 Oct 2026 22:00:11 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=B3=A8=E5=86=8C=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E4=B8=8D=E5=86=8D=E6=8E=A5=E5=8F=97=E5=AE=A2=E6=88=B7=E7=AB=AF?= =?UTF-8?q?=20app=EF=BC=8C=E7=AE=A1=E7=90=86=E5=91=98=E8=BA=AB=E4=BB=BD?= =?UTF-8?q?=E6=94=B9=E6=8C=89=20role=20=E5=88=A4=E5=AE=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 客户端可在 /user/register 的 app 入参写入 wxapp-escort-admin 键自封管理员;改为注册忽略 app,管理员身份统一由 utils 判定 role。conf.json 含微信密钥与库口令,移出版本控制。 --- .gitignore | 2 ++ conf.json | 28 ---------------------------- handler/users.js | 10 ++++++---- middleware/auth.js | 29 ++++++++++++++++++++++++++++- 4 files changed, 36 insertions(+), 33 deletions(-) delete mode 100644 conf.json diff --git a/.gitignore b/.gitignore index 0f67650..b00c66b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ /node_modules/ /logs/ .env +# 含微信 AppSecret 与数据库口令,只允许外置在服务器上,禁止入库 +conf.json diff --git a/conf.json b/conf.json deleted file mode 100644 index ea1b94f..0000000 --- a/conf.json +++ /dev/null @@ -1,28 +0,0 @@ -{ - "port": 9010, - "app": { - "wxapp-escort": { - "appid": "wxf73c79e16837af07", - "secret": "5a061d65f4d35d19e62e83b98f6c63cf" - }, - "wxapp-escort-admin": { - "appid": "wxf94dd97b91af09a2", - "secret": "79ba226684740830af0029763d3045f0" - } - }, - "mongodb": { - "str": "mongodb://huashengtec.com:6000", - "host": "huashengtec.com", - "db": "health", - "option": { - "user": "ehason", - "pass": "Ehason_dbuser_2026", - "dbName": "eiot_user", - "authSource": "admin", - "autoIndex": true, - "socketTimeoutMS": 3000, - "serverSelectionTimeoutMS": 30000 - }, - "debug": false - } -} \ No newline at end of file diff --git a/handler/users.js b/handler/users.js index 919b396..4d804cd 100644 --- a/handler/users.js +++ b/handler/users.js @@ -2,7 +2,7 @@ import Joi from 'joi'; import { DBModel } from "../models/index.js"; import ResponseUtil from "../utils/api_response.js"; import { hashPassword, verifyPassword, hashToken } from "../utils/crypto.js"; -import { sanitizeUser } from "../middleware/auth.js"; +import { sanitizeUser, isAdmin } from "../middleware/auth.js"; import config from "../conf.json" with { type: "json" }; // 微信 access_token 进程内缓存(有效期 7200s,提前 5 分钟刷新) @@ -48,7 +48,8 @@ const registerSchema = Joi.object({ district: Joi.string().allow(''), }).optional(), addresses: Joi.array().optional(), - app: Joi.object().optional(), + // 不接受客户端 app:app 里的角色决定管理端权限,只能由服务端/库内维护, + // 否则任何人都能在注册时自封管理员(历史漏洞)。 }).required(), }); @@ -146,7 +147,8 @@ class HandlerUser { status: { account: "normal", }, - app: userInfo.app || {}, + // 注册一律不带 app:管理端角色只能由服务端在库内授予 + app: {}, }; const user = await DBModel.User.setUser(newUser); @@ -280,7 +282,7 @@ class HandlerUser { return ResponseUtil.unauthorized(ctx, "用户未登录或 token 无效"); } - if (!("wxapp-escort-admin" in (user.app || {}))) { + if (!isAdmin(user)) { return ResponseUtil.unauthorized(ctx, "用户无管理员权限"); } diff --git a/middleware/auth.js b/middleware/auth.js index 770c28d..56e6614 100644 --- a/middleware/auth.js +++ b/middleware/auth.js @@ -87,6 +87,33 @@ function auth(options = {}) { }; } +/** + * 管理端身份判定(与 health 服务 utils/permissions.js 同一口径) + * + * 不再用「app 里存在 wxapp-escort-admin 键」:该键历史上可由 /user/register 的 + * 客户端入参写入,任何人都能据此自封管理员。现在注册侧拒绝写 app, + * 但库里可能残留历史账号,因此要求显式 role === 'admin'; + * role 缺失的历史账号暂按管理员放行并打告警,待库中补齐 role 后收紧。 + * + * @param {Object} user 用户文档或脱敏后的用户对象 + * @returns {boolean} + */ +function isAdmin(user) { + const entry = user?.app?.['wxapp-escort-admin']; + if (!entry) return false; + + const roles = Array.isArray(entry.role) ? entry.role : [entry.role].filter(Boolean); + if (roles.includes('admin')) return true; + + if (entry.role === undefined) { + console.warn( + "[auth] 管理员账号缺少 role 字段,请补齐 app.wxapp-escort-admin.role = ['admin']" + ); + return true; + } + return false; +} + /** * 返回用户安全对象(去除密码、token 等敏感字段) * 注意:登录/注册响应中的 token 由 handler 单独附加 @@ -102,4 +129,4 @@ function sanitizeUser(user) { return obj; } -export { auth, extractToken, sanitizeUser, findUserByToken }; +export { auth, extractToken, sanitizeUser, findUserByToken, isAdmin };