Skip to content

fix(auth): role claims, expiring demo tokens and visible demo-auth fallback - #13

Merged
Jerry-CodeHub merged 1 commit into
masterfrom
fix/auth-hardening
Sep 23, 2026
Merged

Jerry-CodeHub merged 1 commit into
masterfrom
fix/auth-hardening

Conversation

@Jerry-CodeHub

Copy link
Copy Markdown
Owner

背景

审计 2026-09-22 H-4 + M-1 + M-2 + L-2/L-5/L-8。09-20 审计后鉴权链的形式完整性已修复,本轮把剩余风险从「文档声明」升级为「机制性防线」:

  • H-4:生产构建未配 UMI_APP_API_BASE 时静默落入任意凭证放行(fail-open 不可见)
  • M-1:canSeeAdmin 用用户名黑名单——接真实后端后任何用户登录即 admin,CurrentUser 无角色接入点
  • M-2:演示 token ${name}-demo-token 可预测、永不过期;mock 实现形似现成后端参考代码,搬进真实后端即成万能凭证

改动

  • 构建期醒目 banner:生产构建且未配后端时打印不可忽略的警告(可见的 fail-open;不做 build fail 以保住 clone 即 build 的开箱体验)
  • role 白名单式权限:CurrentUser/UserInfo 增加 role,canSeeAdmin = initialState?.role === 'admin';注释明示真实后端必须由服务端返回角色声明
  • 演示 token 重构(demo/demoToken.ts,auth 与 mock 共用):明文 JSON 载荷 {name, role, ts} + 7 天过期 + 时钟回拨防御;mock 文件头加「禁止搬进真实后端」警告
  • 401 整页跳转(window.location.assign):重置内存中残留的 initialState,避免并发 401 重复 push;PUBLIC_PATH 兼容子路径
  • 登录的 localStorage 写入失败(隐私模式/配额满)给出可见提示而非静默吞掉(新 locale 键 login.storageError)
  • 演示 userService 对 localStorage 数据做形状校验,损坏时回落静态种子
  • typings.d.ts:CESIUM_ION_TOKEN / UMI_APP_API_BASE 改为 string | undefined(诚实类型)

验证(headless 浏览器,静态构建)

完整权限链路实测:未登录守卫重定向 → admin 登录 + /table 可访问 → 登出回登录页 → dontHaveAccess(role=user)登录后访问 /table 落 403 且管理菜单项被过滤;零 console 错误。pnpm check 53 用例全绿(含过期/伪造/未来时间戳 token 拒绝用例)。

破坏性说明

演示 token 形状变更会让已登录的旧会话失效(旧 -demo-token 形状不再被接受)——对演示站点是预期行为(重新登录即可)。canSeeAdmin 语义变严:未返回 role 的真实后端接入者会全部失去 admin——这是有意为之(白名单式),README/SECURITY.md 已声明接入要求。

@coderabbitai

coderabbitai Bot commented Sep 23, 2026

Copy link
Copy Markdown

Warning

Review limit reached

Next included review available in 58 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Learn how review limits work.

Review configuration:

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Advanced

Run ID: 12b4f872-da3f-4938-8746-659232864723

📥 Commits

Reviewing files that changed from the base of the PR and between 8e537c4 and f82b4d7.

📒 Files selected for processing (15)
  • config/config.ts
  • mock/userAPI.ts
  • src/access.ts
  • src/layouts/RightContent.tsx
  • src/locales/en-US.ts
  • src/locales/zh-CN.ts
  • src/pages/Login/index.tsx
  • src/services/auth.test.ts
  • src/services/auth.ts
  • src/services/demo/demoToken.ts
  • src/services/demo/userService.ts
  • src/typings.d.ts
  • src/utils/Auth/userInfo.ts
  • src/utils/requestConfig.test.ts
  • src/utils/requestConfig.ts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@cloudflare-workers-and-pages

Copy link
Copy Markdown

Deploying umi-react-admin with  Cloudflare Pages  Cloudflare Pages

Latest commit: f82b4d7
Status: ✅  Deploy successful!
Preview URL: https://90509491.umi-react-admin.pages.dev
Branch Preview URL: https://fix-auth-hardening.umi-react-admin.pages.dev

View logs

@Jerry-CodeHub
Jerry-CodeHub merged commit 26b7922 into master Sep 23, 2026
5 checks passed
@Jerry-CodeHub
Jerry-CodeHub deleted the fix/auth-hardening branch September 23, 2026 00:15
…llback

- access.ts switches to allowlist: canSeeAdmin requires role==='admin'
  from the issuer (demo token payload or real backend), instead of a
  username blacklist that made every logged-in user an admin
- demo tokens are now self-describing payloads (name, role, ts) with a
  7-day TTL and clock-rollback guard, shared by services/auth.ts and
  mock/userAPI.ts via demo/demoToken.ts; the mock file now carries a
  do-not-port-to-production warning since its token convention is a
  universal credential if copied into a real backend
- production builds without UMI_APP_API_BASE now print an unmissable
  banner that the auth is a demo stub (visible fail-open, no hard build
  failure to keep the clone-and-build experience)
- 401 now does a full-page redirect (resets in-memory initialState,
  avoids duplicate pushes on concurrent 401s), PUBLIC_PATH-aware
- login surfaces localStorage failures instead of swallowing them
  behind the empty catch; locales gain login.storageError
- demo userService validates stored JSON shape and falls back to the
  static seed when localStorage is corrupted
- typings: CESIUM_ION_TOKEN / UMI_APP_API_BASE are string | undefined
  (they are optional define injections)

verified headless against a static build: guard redirect, admin login
and table access, logout, and dontHaveAccess landing on 403 with the
admin menu entries filtered out
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.

1 participant