Skip to content

fix: replace hardcoded JWT and AES encryption keys with env placeholders#543

Open
saaa99999999 wants to merge 289 commits into
didi:developfrom
saaa99999999:fix/hardcoded-credentials
Open

fix: replace hardcoded JWT and AES encryption keys with env placeholders#543
saaa99999999 wants to merge 289 commits into
didi:developfrom
saaa99999999:fix/hardcoded-credentials

Conversation

@saaa99999999

Copy link
Copy Markdown

1. 硬编码 JWT 签名密钥 (CWE-798)

仓库中提交了3个 .env 文件,其中 JWT 签名密钥都是同一个可预测的值。

server/.env / server/.env.development / server/.env.production:

修复前:

XIAOJU_SURVEY_JWT_SECRET=xiaojuSurveyJwtSecret

修复后:

XIAOJU_SURVEY_JWT_SECRET=change-me-in-production

这个密钥在 auth.service.ts 中同时用于签名和验证 JWT token:

  • server/src/modules/auth/services/auth.service.ts:16: sign({ _id, username }, secret, signOptions) 用它对 token 签名
  • server/src/modules/auth/services/auth.service.ts:27: verify(token, secret) 用它验证 token

拿到仓库代码的人可以直接使用 xiaojuSurveyJwtSecret 伪造任意用户的 token,绕过 authentication guard。

import jwt
token = jwt.encode({'username': 'admin', '_id': 'anything'}, 'xiaojuSurveyJwtSecret', algorithm='HS256')
# 这个 token 能通过所有需要认证的 API

2. 硬编码 AES 加密密钥 (CWE-798)

同样在 3 个 .env 文件中,AES 加密密钥也是硬编码的。

server/.env / server/.env.development / server/.env.production:

修复前:

XIAOJU_SURVEY_RESPONSE_AES_ENCRYPT_SECRET_KEY=dataAesEncryptSecretKey

修复后:

XIAOJU_SURVEY_RESPONSE_AES_ENCRYPT_SECRET_KEY=change-me-in-production

密钥用途:

  • server/src/app.module.ts:onModuleInit(): 传入 ResponseSecurityPlugin,作为存储在内存中的 secretKey
  • server/src/securityPlugin/responseSecurityPlugin/index.ts:encryptResponseData() / decryptResponseData(): 对问卷回答中的敏感数据(手机号/身份证/地址/邮箱/性别)加密和解密
  • server/src/securityPlugin/responseSecurityPlugin/utils.ts:encryptData() / decryptData(): 使用 CryptoJS.AES 加解密

拿到这个密钥的攻击者可以解密所有问卷回答数据,包含用户手机号、身份证号、地址等 PII。

3. 错误日志泄露 JWT 密钥 (CWE-532)

server/src/modules/auth/controllers/auth.controller.ts:138-143:

修复前:

throw new Error(
    'generateToken erro:' +
      error.message +
      this.configService.get<string>('XIAOJU_SURVEY_JWT_SECRET') +
      this.configService.get<string>('XIAOJU_SURVEY_JWT_EXPIRES_IN'),
);

修复后:

throw new Error(
    'generateToken erro:' + error.message,
);

登录失败时的错误消息会把 JWT 密钥和过期时间拼接进去,泄露在日志或响应中。


共计修复 3 个问题,涉及 4 个文件。

sudoooooo and others added 30 commits April 1, 2024 22:49
* feat: nps评分功能

* feat: nps样式添加

* feat: 添加nps评分icon

* feat: 基于修改建议修改nps评分组件

* feat: 将自定义类移入至设置器
* fix: 修复评分和nps题型非必填提示没有填写的问题
* fix: 使用添加key的方法修复必填提示
* feat: 增加拖拽添加题目效果

* feat: 手动实现题型的预览效果

* feat: 优化预览体验
* feat: 补充file模块的单测

* fix: 安装types依赖
skique and others added 26 commits May 28, 2025 19:53
* fix: 下线数据上报

* fix: 下线数据上报
* 修改配置环境

* 添加AI生成问卷入口

* 创建AI智能生成问卷页面

* AI生成问卷前端代码优化

* AI生成问卷页面分文件编写

* 修复列表页TextImport问题

* AI api 初始调试完成

* 前端页面显示AI生成结果

* Text to Schema 应用预览

* 把ai-generate页面中的东西转接到列表页AIGenerate组件下

* 实现创建问卷的功能啦!!!

* AI生成问卷页面功能丰富

* AI生成标签

* 删除多余的ai-generate页面文件

* 实现AI打字输出

* 停止生成功能完成

* 标题处AI标签样式、重新生成、万能公式内容微调

* AI loading 和生成的时候禁止用户输入,AI生成东西的时候用户视角也会跟随移动

* 隐藏AI模型KEY MongoDB数据库URL
Co-authored-by: luch <32321690+luch1994@users.noreply.github.com>
@skique skique changed the base branch from main to develop May 27, 2026 14:44
@skique

skique commented May 27, 2026

Copy link
Copy Markdown
Collaborator

@saaa99999999 hello,请阅读贡献指南,用develop分支作为pr的基准分支

@codecov

codecov Bot commented Jun 14, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 65.03906% with 537 lines in your changes missing coverage. Please review.
⚠️ Please upload report for BASE (develop@9afd1d1). Learn more about missing BASE report.

Files with missing lines Patch % Lines
.../modules/channel/controllers/channel.controller.ts 0.00% 88 Missing and 2 partials ⚠️
...rc/modules/survey/controllers/survey.controller.ts 30.55% 68 Missing and 7 partials ⚠️
...er/src/modules/channel/services/channel.service.ts 0.00% 44 Missing and 2 partials ⚠️
.../src/modules/survey/services/surveyMeta.service.ts 54.54% 38 Missing and 2 partials ⚠️
...dules/survey/controllers/ai-generate.controller.ts 0.00% 27 Missing and 1 partial ⚠️
...odules/appManager/__tests__/appManager.e2e-spec.ts 0.00% 26 Missing ⚠️
...ules/workspace/controllers/workspace.controller.ts 44.18% 23 Missing and 1 partial ⚠️
...odules/survey/controllers/recycleBin.controller.ts 0.00% 19 Missing ⚠️
server/src/modules/survey/utils/index.ts 34.48% 19 Missing ⚠️
...dules/survey/controllers/surveyGroup.controller.ts 77.63% 17 Missing ⚠️
... and 30 more
Additional details and impacted files
@@            Coverage Diff             @@
##             develop     #543   +/-   ##
==========================================
  Coverage           ?   79.81%           
==========================================
  Files              ?      145           
  Lines              ?     3765           
  Branches           ?      489           
==========================================
  Hits               ?     3005           
  Misses             ?      732           
  Partials           ?       28           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@skique

skique commented Jun 17, 2026

Copy link
Copy Markdown
Collaborator

@saaa99999999 你好,切到develop后有冲突哦,麻烦解决一下冲突再合入吧

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.