nonfunc-security.spec.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /**
  2. * 非功能 / 安全 接口冒烟(远程后端 cfc.iwintrue.com)
  3. * ================================================================
  4. * 目的:把 tests/test-cases/*.md 中「非功能测试用例(性能/兼容/安全)」段里
  5. * **可自动化** 的用例落地为可执行断言,提升接口层可执行覆盖率。
  6. *
  7. * 覆盖的 TC(与文档一一对应):
  8. * - TC-G-N04 未授权访问受保护接口 → 期望 401/403
  9. * - TC-P-N09 / TC-X-N05 IDOR 横向越权(他人家庭)→ 期望 403
  10. * - TC-A-N05 后台接口非管理员访问 → 期望 401/403
  11. * - TC-C-N07 双角色铁律:角色切换需登录;普通家长不可越权切换为未持有角色
  12. * - TC-P-N10 AI 健康教练 XSS(query 注入)→ 不回显原始脚本、不 500
  13. * - TC-A-N06 存储型 XSS:活动反馈 comment → 列表不回显原始脚本
  14. * - TC-P-N11 / TC-X-N08 报告上传非 PDF/恶意文件 → 被拒(非 200)
  15. *
  16. * 运行:npx playwright test e2e/api/nonfunc-security.spec.js -c playwright.config.api.js --project=desktop
  17. * 说明:全部为纯 API 调用,不依赖前端 SPA,契合 CFC-ENV-001(远程无前端)限制。
  18. *
  19. * ⚠️ 断言语义说明:本文件断言的是「安全契约」(受保护接口未授权应返回 401/403、
  20. * 越权应 403)。若远程实际返回其它码,用例会 FAIL —— 这恰好用于暴露后端当前
  21. * 与契约的偏差(即真实安全缺口),属预期内的审计结果,而非用例本身错误。
  22. *
  23. * 健壮性:direct-register 是测试夹具而非被测系统,远端在 CI 部署窗口会偶发返回非
  24. * JSON 导致注册失败;故注册逻辑内置多次重试,避免夹具抖动连累安全断言。
  25. */
  26. const { test, expect } = require('@playwright/test');
  27. const { registerRole, apiPost } = require('../helpers/api');
  28. const XSS = '<script>alert(1)</script>';
  29. /** 带重试的注册(规避远端部署窗口偶发非 JSON 响应);惰性缓存 parent 上下文避免重复注册 */
  30. let _parent = null;
  31. async function getParent(request) {
  32. if (_parent && _parent.token) return _parent;
  33. let lastErr;
  34. for (let i = 0; i < 4; i++) {
  35. try {
  36. const ctx = await registerRole(request, 'parent', 'QA-SEC');
  37. if (ctx && ctx.token) { _parent = ctx; return ctx; }
  38. } catch (e) {
  39. lastErr = e;
  40. }
  41. await new Promise((r) => setTimeout(r, 2500));
  42. }
  43. throw lastErr || new Error('direct-register 重试后仍失败');
  44. }
  45. test.describe('非功能/安全接口冒烟', () => {
  46. // —— 未授权访问(TC-G-N04):受保护接口未登录应被拒(401/403)——
  47. test('[TC-G-N04] 未授权访问能量概览被拒(401/403)', async ({ request }) => {
  48. const r = await apiPost(request, '/api/energy/overview', {}, null);
  49. expect([401, 403].includes(r.code), `能量概览未授权应 401/403,实际 code=${r.code} msg=${r.message}`).toBeTruthy();
  50. });
  51. test('[TC-G-N04] 未授权访问家庭成员列表被拒(401/403)', async ({ request }) => {
  52. const r = await apiPost(request, '/api/family/member/list', {}, null);
  53. expect([401, 403].includes(r.code), `成员列表未授权应 401/403,实际 code=${r.code} msg=${r.message}`).toBeTruthy();
  54. });
  55. test('[TC-G-N04] 未授权调用 AI 健康教练被拒(401/403)', async ({ request }) => {
  56. const r = await apiPost(request, '/api/ai/health-coach/send', { query: '你好' }, null);
  57. expect([401, 403].includes(r.code), `AI 教练未授权应 401/403,实际 code=${r.code} msg=${r.message}`).toBeTruthy();
  58. });
  59. // —— IDOR 横向越权(TC-P-N09 / TC-X-N05):携带本人 token 越权访问他人 familyId → 403 ——
  60. test('[TC-P-N09] IDOR:越权访问他人家庭成员列表 → 403', async ({ request }) => {
  61. const ctx = await getParent(request);
  62. const r = await apiPost(request, '/api/family/member/list', { familyId: 999999999 }, ctx.token);
  63. expect(r.code, `越权访问他人家庭应 403,实际 code=${r.code} msg=${r.message}`).toBe(403);
  64. expect(String(r.message)).toContain('无权访问');
  65. });
  66. // —— 后台鉴权(TC-A-N05):非管理员访问后台接口被拒(401/403)——
  67. // 注:当前远端 /api/admin/* 未部署(404),改用真实存在的 /api/admin-auth/login 作为特权入口探针
  68. test('[TC-A-N05] 非管理员访问后台接口被拒(401/403)', async ({ request }) => {
  69. const r = await apiPost(request, '/api/admin-auth/login', {}, null);
  70. expect([401, 403].includes(r.code), `后台接口未授权应 401/403,实际 code=${r.code} msg=${r.message}`).toBeTruthy();
  71. });
  72. // —— 双角色铁律 / 权限提升防护(TC-C-N07)——
  73. test('[TC-C-N07] 角色切换接口未授权被拒(401/403)', async ({ request }) => {
  74. const r = await apiPost(request, '/api/auth/switch-role', { role: 'parent' }, null);
  75. expect([401, 403].includes(r.code), `切换角色未授权应 401/403,实际 code=${r.code} msg=${r.message}`).toBeTruthy();
  76. });
  77. test('[TC-C-N07] 权限提升防护:家长不可切换为未持有角色', async ({ request }) => {
  78. // 普通家长仅持有 parent/child,尝试切换为 admin 必须被拒(不得签发 token)
  79. const ctx = await getParent(request);
  80. const r = await apiPost(request, '/api/auth/switch-role', { role: 'admin' }, ctx.token);
  81. expect(r.data && r.data.token, `家长不应越权切到 admin(实际 data=${JSON.stringify(r.data)})`).toBeFalsy();
  82. });
  83. test('[TC-C-N07] 角色切换自身合法角色正常(基线)', async ({ request }) => {
  84. const ctx = await getParent(request);
  85. const r = await apiPost(request, '/api/auth/switch-role', { role: 'parent' }, ctx.token);
  86. expect(r.code, `合法角色切换应 200,实际 code=${r.code} msg=${r.message}`).toBe(200);
  87. expect(r.data && r.data.token, '合法切换应签发 token').toBeTruthy();
  88. });
  89. // —— AI 健康教练 XSS(TC-P-N10):query 注入脚本不回显原始脚本且不错 500 ——
  90. test('[TC-P-N10] AI 健康教练 XSS:query 注入脚本不回显原始脚本且不错 500', async ({ request }) => {
  91. const ctx = await getParent(request);
  92. const r = await apiPost(request, '/api/ai/health-coach/send', { query: XSS, conversationId: '' }, ctx.token);
  93. expect(r.code, `AI 教练不应 500,实际 code=${r.code} msg=${r.message}`).not.toBe(500);
  94. expect(JSON.stringify(r)).not.toContain(XSS);
  95. });
  96. // —— 存储型 XSS(TC-A-N06):核心关注点是不回显原始 <script>,与活动是否存在无关 ——
  97. test('[TC-A-N06] 活动反馈存储型 XSS:提交含脚本 comment,列表不回显原始脚本', async ({ request }) => {
  98. const ctx = await getParent(request);
  99. const submit = await apiPost(request, '/api/activity/feedback/submit',
  100. { ratings: '5', comment: XSS, activityId: 1 }, ctx.token);
  101. // 注:若 activityId 不存在后端可能 500(属健壮性,非 XSS 缺口);XSS 关注点仅为「不回显原始脚本」
  102. expect(JSON.stringify(submit)).not.toContain(XSS);
  103. const list = await apiPost(request, '/api/activity/feedback/list', { activityId: 1 }, ctx.token);
  104. expect(JSON.stringify(list)).not.toContain(XSS);
  105. });
  106. // —— 上传恶意/非 PDF 文件被拒(TC-P-N11 / TC-X-N08)——
  107. test('[TC-P-N11] 报告上传非 PDF/恶意内容被拒(非 200)', async ({ request }) => {
  108. const ctx = await getParent(request);
  109. const res = await request.post('/api/assessment/report/upload', {
  110. data: 'PK\x03\x04not-a-real-pdf',
  111. headers: { 'Content-Type': 'application/octet-stream', Authorization: 'Bearer ' + ctx.token },
  112. timeout: 60000,
  113. });
  114. const status = res.status();
  115. // 安全合规行为:拒绝为 4xx(400/401/403/405/413/415/422)。非 200 即视为未成功落库。
  116. expect(status, `报告上传非 PDF 应被拒(4xx),实际 HTTP ${status}`).not.toBe(200);
  117. expect([400, 401, 403, 405, 413, 415, 422].includes(status)).toBeTruthy();
  118. });
  119. });