Skip to content

Fix #6: require authentication in test/offline mode too - #13

Open
xiaoyao-666-coder wants to merge 2 commits into
zhengdaodeguL:mainfrom
xiaoyao-666-coder:fix/issue-6-anon-access
Open

xiaoyao-666-coder wants to merge 2 commits into
zhengdaodeguL:mainfrom
xiaoyao-666-coder:fix/issue-6-anon-access

Conversation

@xiaoyao-666-coder

Copy link
Copy Markdown

Closes #6.

test/offline 模式下无 Cookie 也能调用受保护接口并读取他人会话,有无效 Cookie 反而 401,行为正好倒挂。根因:_require_access 只在 production/local 或“带了 Cookie”时才 401;_require_service_access 在非生产模式直接放行。

  • _require_access 改为所有模式下无有效凭证一律 401;_require_service_access 在非生产模式也鉴权(登录用户会话;生产模式仍仅 service token,行为不变)。
  • 没有留匿名开关:/api/health、/api/ready 保持公开已够探活,前端和测试都走注册/登录拿 Cookie。
  • 更新了把匿名行为写成预期的旧测试和 7 个依赖匿名访问的测试(只补登录步骤),新增 backend/tests/test_anon_access.py(12 个测试)与 conftest 的 login_client helper。

验证:MEDGUIDE_MODE=test python -m pytest -q backend/tests 533 passed(基线 521 + 12 新);前端未改动。

行为变更请注意:test/offline/local 模式现在也要先登录才能调受保护接口;部署 smoke 测试只用 health/ready 不受影响。

- _require_access now returns 401 without valid credentials in every
  mode (test/offline used to let anonymous requests through while
  rejecting invalid cookies); _require_service_access also
  authenticates outside production (logged-in user in test/offline,
  service token only in production, unchanged).
- No anonymous-mode switch was added: /api/health and /api/ready
  stay public for smoke tests, and clients/tests authenticate via
  register/login. Update the regression test that enshrined the
  anonymous behaviour and the tests that relied on anonymous access.
- Add backend/tests/test_anon_access.py (12 tests) and a login_client
  helper in conftest.py.

Closes zhengdaodeguL#6.
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.

[Bug][高] test/offline 模式下受保护接口可匿名访问

1 participant