feat(big-question): 中职/高职组「综合大题」在线作答 + 封堵 auth 成绩泄露三通道

## 一、需求变更:中职/高职组 Word 文件上传题 → 在线「综合大题」

按 2026-09-21 需求变更,中职组 / 高职组取消 Word 文件上传题,改为在线作答
综合大题(1 道大题 × 3 个小问 × 每小问 10 分 = 大题 30 分)。

- 新增表(迁移 023/024)
  · `big_questions`        —— 题库(题干富文本含表格 + 三小问提示语 + 评分口径)
  · `big_question_answers` —— 作答(抽题快照 + 三小问答案 + 按小问得分)
  两份迁移同时提供 `.sql`(生产直跑)与 `.js`(本地/CI 幂等执行)。
- 新增 model:`app/model/bigQuestion.js`、`app/model/bigQuestionAnswer.js`
- 新增 service:`app/service/bigQuestion.js`(抽题 / 保存答案 / 评分核心逻辑)
- 考生端接口(`app/controller/exam.js`)
  · `GET  /api/exam/:group/big-question` —— 首次抽题并落库题干快照,
    后续一律返回同一道题(**幂等**,防止同一考生拿到不同题干引发纠纷)
  · `POST /api/exam/:group/big-answer`   —— 保存三小问答案,带考试窗口门控
- 管理端接口(`app/controller/admin.js`)
  · 题库 CRUD:`/api/admin/big-questions/:group[/:id]`
  · 作答查证:`GET /api/admin/big-answers/:group`(考生 → 抽到的题 → 三小问 → 提交时间)
  · 按小问评分:`PUT /api/admin/big-answers/:group/:uid/scores`
- 服务端封堵越界数据:`app/controller/exam.js` 对中职/高职组的 Word 上传接口
  一并拒绝(前端入口已移除,但直调 API 仍可绕过)。

**所有新增读写仅落在新增的两张表,不触碰任何既有表结构。**

## 二、安全修复(铁律 15 成绩泄露 —— auth 通道缺口)

原有成绩发布门控只覆盖 4 条通道,遗漏了 auth 的**三条同源通道**:三处都直接
回传 `toJSON()`,在 `score_published_<group> = false`(未发布)时照样下发成绩,
而同期 `GET /api/score` 正确返回 `null`。

- `POST /api/auth/login`         —— 实测未发布态回传 `score`
- `GET  /api/auth/current-user`  —— 全字段 `toJSON`,连带下发分项
- `POST /api/auth/refresh`       —— 全字段 `toJSON`,且前端 `doSilentRefresh()`
  会把响应 `user` 写入 `localStorage` 与 Pinia,等效于登录通道;
  实测泄漏 `score` + `exam_score` + `word_score` + `ppt_score` 共 4 个字段

修复方式:新增 `AuthService.toClientUser()` 作为**唯一出口**,三处全部收敛,
禁止各自直接 `toJSON`。口径:
- 该组已发布 → 保留 `score`;未发布 / 查询异常 → 剔除 `score`(异常按未发布,安全默认)
- `exam_score` / `word_score` / `ppt_score` **任何情况下都不下发**
  (前端不使用;保留会形成"总分虽隐藏、分项可加总"的旁路)

门禁 `scripts/verify_score_leak_all_channels.py` 相应从 14 项扩到 20 项,
新增通道⑤/⑥/⑦断言(含"发布后 score 必须恢复"的**正向对照**,防止
"永不回传"式过度封堵造成假绿),并经变异注入自检证明断言可证伪。

## 三、移除已弃用的 Word 模板

中职 / 高职各 3 个历史 Word 模板(`app/public/templates/{zhongzhi,gaozhi}/*.docx`)
随需求一并移除;目录保留 `.gitkeep` 以维持路径存在。

---
验证:G10 铁律15 门禁 20/20 PASS(含变异自检,EXIT=0)、G9 铁律14 门禁 22/22 PASS、
窗口哨兵残留自检 6/6 PASS。前端配套改动见前端仓库提交 1bfe007。
This commit is contained in:
2026-09-21 11:57:58 +08:00
parent f281d206fb
commit 8ef18da395
19 changed files with 1527 additions and 9 deletions

View File

@@ -1659,6 +1659,214 @@ class AdminController extends Controller {
ctx.body = ctx.helper.error(500, '重置测试数据失败');
}
}
// ==================== 综合大题(中职组 / 高职组) ====================
//
// 2026-09-21 需求变更配套管理端能力:
// · 题库 CRUD(题干富文本含表格 + 三小问提示语 + 每小问 10 分 / 大题 30 分)
// · 作答查证(考生 → 抽到的题目快照 → 三小问答案 → 提交时间),用于避免考生纠纷
// · 按小问评分(合计分经既有唯一评分路径写入 contestants.word_score)
//
// 全部读写仅落在新增的 big_questions / big_question_answers,不触碰任何既有表。
/**
* 大题题库列表
* GET /api/admin/big-questions/:group
*/
async listBigQuestions() {
const { ctx, service } = this;
if (!this._ensureAdmin()) return;
try {
const { group } = ctx.params;
if (!this._validateGroup(group)) return;
if (!service.bigQuestion.isEnabledGroup(group)) {
ctx.body = ctx.helper.error(400, '该组别未启用综合大题');
return;
}
const list = await service.bigQuestion.listQuestions(group);
ctx.body = ctx.helper.success({
list,
total: list.length,
scorePerSub: 10,
subCount: service.bigQuestion.subQuestionCount(),
maxChars: service.bigQuestion.answerMaxChars(),
}, '获取大题题库成功');
} catch (err) {
ctx.logger.error('获取大题题库失败:', err);
ctx.body = ctx.helper.error(500, '获取大题题库失败');
}
}
/**
* 新增大题
* POST /api/admin/big-questions/:group
* Body: { title, description, sub_prompt_1..3, score_per_sub, sort_order, status }
*/
async createBigQuestion() {
const { ctx, service } = this;
if (!this._ensureAdmin()) return;
try {
const { group } = ctx.params;
if (!this._validateGroup(group)) return;
const result = await service.bigQuestion.createQuestion(group, ctx.request.body || {});
if (!result.ok) {
ctx.body = ctx.helper.error(400, result.message);
return;
}
ctx.body = ctx.helper.success({
id: result.record.id,
questionUid: result.record.question_uid,
}, '大题已新增');
} catch (err) {
ctx.logger.error('新增大题失败:', err);
ctx.body = ctx.helper.error(500, '新增大题失败');
}
}
/**
* 更新大题
* PUT /api/admin/big-questions/:group/:id
*/
async updateBigQuestion() {
const { ctx, service } = this;
if (!this._ensureAdmin()) return;
try {
const { group, id } = ctx.params;
if (!this._validateGroup(group)) return;
const qid = parseInt(id, 10);
if (isNaN(qid)) {
ctx.body = ctx.helper.error(400, '大题 ID 非法');
return;
}
const result = await service.bigQuestion.updateQuestion(group, qid, ctx.request.body || {});
if (!result.ok) {
ctx.body = ctx.helper.error(result.code === 'NOT_FOUND' ? 404 : 400, result.message);
return;
}
ctx.body = ctx.helper.success({ id: result.record.id }, '大题已更新');
} catch (err) {
ctx.logger.error('更新大题失败:', err);
ctx.body = ctx.helper.error(500, '更新大题失败');
}
}
/**
* 删除大题(已被考生抽取过的大题拒绝删除,引导改为「停用」)
* DELETE /api/admin/big-questions/:group/:id
*/
async deleteBigQuestion() {
const { ctx, service } = this;
if (!this._ensureAdmin()) return;
try {
const { group, id } = ctx.params;
if (!this._validateGroup(group)) return;
const qid = parseInt(id, 10);
if (isNaN(qid)) {
ctx.body = ctx.helper.error(400, '大题 ID 非法');
return;
}
const result = await service.bigQuestion.deleteQuestion(group, qid);
if (!result.ok) {
ctx.body = ctx.helper.error(result.code === 'NOT_FOUND' ? 404 : 409, result.message);
return;
}
ctx.body = ctx.helper.success({ deleted: 1 }, '大题已删除');
} catch (err) {
ctx.logger.error('删除大题失败:', err);
ctx.body = ctx.helper.error(500, '删除大题失败');
}
}
/**
* 大题作答查证:按组返回「考生 → 抽到的题目(快照)→ 三小问答案 → 提交时间 → 得分」
* GET /api/admin/big-answers/:group
*
* 含该组全部考生(未抽题者 drawn=false),便于核对缺考/未作答。
* 题目取自**题干快照**而非当前题库,确保考生当时所见可举证。
*/
async getBigAnswers() {
const { ctx, service } = this;
if (!this._ensureAdmin()) return;
try {
const { group } = ctx.params;
if (!this._validateGroup(group)) return;
if (!service.bigQuestion.isEnabledGroup(group)) {
ctx.body = ctx.helper.error(400, '该组别未启用综合大题');
return;
}
const list = await service.bigQuestion.listAnswersForAdmin(group);
ctx.body = ctx.helper.success({
list,
total: list.length,
drawnCount: list.filter(x => x.drawn).length,
}, '获取大题作答成功');
} catch (err) {
ctx.logger.error('获取大题作答失败:', err);
ctx.body = ctx.helper.error(500, '获取大题作答失败');
}
}
/**
* 按小问评分(三小问各 0-10,合计 0-30)
* PUT /api/admin/big-answers/:group/:uid/scores
* Body: { score_1, score_2, score_3 }
*
* 合计分经 service.admin.updateScores(既有唯一评分路径)同步 contestants.word_score /
* contestants.score / accounts,保证与「成绩发布门控」及总分公式完全一致,
* 不引入第二条写成绩的代码通路。
*/
async setBigAnswerScores() {
const { ctx, service } = this;
if (!this._ensureAdmin()) return;
try {
const { group, uid } = ctx.params;
if (!this._validateGroup(group)) return;
const aggregationUid = parseInt(uid, 10);
if (isNaN(aggregationUid)) {
ctx.body = ctx.helper.error(400, '考生聚合 ID 非法');
return;
}
const body = ctx.request.body || {};
const result = await service.bigQuestion.setSubScores(group, aggregationUid, {
score_1: body.score_1,
score_2: body.score_2,
score_3: body.score_3,
});
if (!result.ok) {
ctx.body = ctx.helper.error(result.code === 'NOT_FOUND' ? 404 : 400, result.message);
return;
}
ctx.body = ctx.helper.success({
total: result.total,
perSubScore: result.perSubScore,
}, `大题评分已保存(合计 ${result.total} 分)`);
} catch (err) {
ctx.logger.error('大题评分失败:', err);
ctx.body = ctx.helper.error(500, '大题评分失败');
}
}
}
module.exports = AdminController;

View File

@@ -309,9 +309,9 @@ class AuthController extends Controller {
role: 'contestant',
}, this.app.config.jwt.secret, this.app.config.jwt.expiresIn);
// 返回用户信息(排除密码)
const userData = contestant.toJSON();
delete userData.password;
// 返回用户信息 —— 统一走 AuthService.toClientUser 出口:
// 剔除密码 + 按「成绩发布门控」决定是否携带 score(铁律15 全通道封堵,2026-09-21)
const userData = await service.auth.toClientUser(contestant);
ctx.body = ctx.helper.success({
token,
@@ -392,7 +392,7 @@ class AuthController extends Controller {
* 获取当前登录用户信息(需认证)
*/
async currentUser() {
const { ctx } = this;
const { ctx, service } = this;
// 从 JWT token 中获取用户信息
const token = ctx.headers.authorization
@@ -426,8 +426,9 @@ class AuthController extends Controller {
return;
}
const userData = contestant.toJSON();
delete userData.password;
// 与 login 共用同一脱敏出口(同源族:两条 auth 通道口径必须一致,
// 且此处原为全字段 toJSON —— 会连带下发 exam_score/word_score/ppt_score)
const userData = await service.auth.toClientUser(contestant);
ctx.body = ctx.helper.success(userData, '获取用户信息成功');
}
@@ -544,7 +545,7 @@ class AuthController extends Controller {
* - 该接口刻意不挂 auth 中间件(因为携带的 token 可能已过期)。
*/
async refresh() {
const { ctx } = this;
const { ctx, service } = this;
const authorization = ctx.headers.authorization || '';
const token = authorization.replace(/^Bearer\s+/i, '') || null;
@@ -597,8 +598,9 @@ class AuthController extends Controller {
: { id: account.id, phone: account.phone, role: 'contestant' };
const newToken = ctx.helper.generateToken(payload, this.app.config.jwt.secret, this.app.config.jwt.expiresIn);
const userData = account.toJSON();
delete userData.password;
// 返回用户信息 —— 统一走 AuthService.toClientUser 出口
// (与 login / currentUser 同源:禁止各自直接 toJSON,否则成绩未发布时 score 会从本通道漏出)
const userData = await service.auth.toClientUser(account);
ctx.body = ctx.helper.success({
token: newToken,

View File

@@ -456,6 +456,17 @@ class ExamController extends Controller {
const { group } = ctx.params;
if (!this._validateGroup(group)) return;
// 🔴 中职组/高职组已取消 Word 文件上传题(改为在线作答「综合大题」)。
// 前端入口已移除,但直调 API 仍可绕过,故在服务端一并封堵,避免出现
// 「界面上没这道题、后台却收到 Word 附件」的越界数据(2026-09-21)。
if (['zhongzhi', 'gaozhi'].includes(group)) {
ctx.body = ctx.helper.error(
400,
'中职组/高职组已取消 Word 文件上传题,请在「综合大题」中在线作答'
);
return;
}
// 获取用户聚合信息
const aggregation = await this._getUserAggregation();
if (!aggregation) {
@@ -609,6 +620,134 @@ class ExamController extends Controller {
}
}
/**
* 获取本次抽中的综合大题(仅中职组 / 高职组)
* GET /api/exam/:group/big-question
*
* 2026-09-21 需求变更:中职/高职取消 Word 文件上传题,改为随机抽取一道大题在线作答。
* 语义:**首次调用抽题并落库题干快照,之后一律返回同一道题**(幂等,防考生纠纷),
* 同时回传已保存的三小问作答,供重新进入考试时恢复。
*
* ⚠️ 不回传任何分数:成绩仅经 GET /api/score 单通道(发布门控后)下发,铁律15。
*/
async bigQuestion() {
const { ctx, service } = this;
try {
const { group } = ctx.params;
if (!this._validateGroup(group)) return;
if (!service.bigQuestion.isEnabledGroup(group)) {
ctx.body = ctx.helper.error(400, '该组别未启用综合大题');
return;
}
const aggregation = await this._getUserAggregation();
if (!aggregation) {
ctx.body = ctx.helper.error(403, '未找到您的报名信息');
return;
}
// 组别归属校验(与 status / questions 同口径,防止跨组越权取题)
if (aggregation.team && aggregation.team.group_name
&& aggregation.team.group_name !== group) {
ctx.body = ctx.helper.error(403, '您不属于该组别');
return;
}
const result = await service.bigQuestion.getForCandidate(
aggregation.aggregation_uid,
group,
(ctx.state.currentUser && ctx.state.currentUser.id) || aggregation.aggregation_uid
);
if (!result.ok) {
// 组别未启用 → 400;无题可抽 → 4004(与「题目未配置」类业务码口径一致)
const code = result.code === 'NO_QUESTION' ? 4004 : 400;
ctx.body = ctx.helper.error(code, result.message);
return;
}
ctx.body = ctx.helper.success({
question: result.question,
answers: result.answers,
submitTime: result.submitTime,
maxChars: result.maxChars,
subCount: result.subCount,
reused: result.reused,
}, '获取综合大题成功');
} catch (err) {
ctx.logger.error('获取综合大题失败:', err);
ctx.body = ctx.helper.error(500, '获取综合大题失败');
}
}
/**
* 保存综合大题三小问作答(仅中职组 / 高职组)
* POST /api/exam/:group/big-answer
* Body: { answer_1, answer_2, answer_3, forceSubmit? }
*
* 每小问上限 2000 字,**后端同步校验**(前端 maxlength 只是体验优化,不可作为唯一防线)。
* forceSubmit 与 submit-all 同语义:仅「倒计时归零的自动提交」传入,允许在考试窗口
* 临界/已关闭时仍落库考生已写内容,避免整卷提交时丢失大题答案。
*/
async saveBigAnswer() {
const { ctx, service } = this;
try {
const { group } = ctx.params;
if (!this._validateGroup(group)) return;
if (!service.bigQuestion.isEnabledGroup(group)) {
ctx.body = ctx.helper.error(400, '该组别未启用综合大题');
return;
}
const body = ctx.request.body || {};
const forceSubmit = body.forceSubmit === true;
// 与逐题保存(submitAnswers)同口径的考试窗口门控;forceSubmit 为自动交卷兜底通道
const examStatus = await service.exam.getExamStatus(group);
if (!examStatus.isActive && !forceSubmit) {
ctx.body = ctx.helper.error(4001, examStatus.message || '考试未在进行中');
return;
}
const aggregation = await this._getUserAggregation();
if (!aggregation) {
ctx.body = ctx.helper.error(403, '未找到您的报名信息');
return;
}
const result = await service.bigQuestion.saveAnswers(
aggregation.aggregation_uid,
group,
{
answer_1: body.answer_1,
answer_2: body.answer_2,
answer_3: body.answer_3,
}
);
if (!result.ok) {
// 超长 → 400(参数非法);未抽题 → 4004(业务前置条件未满足)
const code = result.code === 'NOT_DRAWN' ? 4004 : 400;
ctx.body = ctx.helper.error(code, result.message);
return;
}
ctx.body = ctx.helper.success({
filled: result.filled,
subCount: service.bigQuestion.subQuestionCount(),
maxChars: service.bigQuestion.answerMaxChars(),
savedAt: new Date().toISOString(),
}, '综合大题答案已保存');
} catch (err) {
ctx.logger.error('保存综合大题答案失败:', err);
ctx.body = ctx.helper.error(500, '保存综合大题答案失败');
}
}
/**
* 一体提交:答案 + Word 文件
* POST /api/exam/:group/submit-all