Commit Graph
41 Commits
Author SHA1 Message Date
naibaandcloudcode e8dabf5bc6 feat(auth): add PAT auth, scoped REST/MCP access, CSRF, and tenant isolation
Introduce Personal Access Tokens (nzp_*) as a stateless auth path alongside
JWT, gated per-endpoint by a scope middleware (nezha:{resource}:{verb}) with
fail-closed empty-scope defaults and a server-id whitelist. Self-management
endpoints (profile, api-tokens, oauth2 bind, refresh-token) explicitly reject
PATs to block privilege-escalation chains. A revoke registry tears down active
long-lived connections (terminal, fm, ws, transfer, mcp) the moment a PAT is
deleted, with a tombstone closing the revoke->register race.

Add an MCP endpoint that proxies tool calls (exec, fs read/write/delete,
transfer) to agents over gRPC, guarded by origin/DNS-rebinding checks, a
per-token rate limiter, audit logging, and a kill switch. Serialize all
sends through the IOStream wrapper to honour grpc-go's concurrency contract.

Add CSRF double-submit protection on unsafe cookie-authenticated methods,
exempting authenticated PAT requests by context identity (not a forgeable
Authorization header). Apply visibility/whitelist filtering consistently
across list, get-by-id, and mutate paths to enforce tenant isolation.

Migrate legacy mcp:* scopes: rewrite read/exec to nezha:* equivalents and
drop dangerous write/delete/wildcard grants.

Co-authored-by: cloudcode <cloudcode@users.noreply.github.com>
2026-05-30 15:56:44 +00:00
naibaandcloudcode e05700c2f0 feat(jwt): server-side session table with keyId + obfuscated uid claims
Replace the {user_id, ip} claim pair with {keyId, uid}:
- keyId is a 32-byte random id that points to a row in the new
  jwt_sessions table holding the real user id, bound IP, UA hash,
  TokenVersion and expiry.
- uid is the user id encoded through pkg/idcodec; mismatch between
  claim uid and session.UserID trips WAF block on the caller IP.
- identityHandler now rejects unknown/revoked/expired sessions, IP
  drift and stale TokenVersion. Refresh updates session.ExpiresAt.

User.TokenVersion bumps on password change and revokes outstanding
sessions, so a leaked JWT secret alone is no longer enough to forge
a token. JWTSession rows are GC'd every 10 minutes (expired + grace
or revoked >24h). OAuth2 callback shares the same issue path.

Includes regression tests for happy path, mismatched claim uid,
revoked session, TokenVersion bump, IP drift and unknown keyId.

Co-authored-by: cloudcode <cloudcode@users.noreply.github.com>
2026-05-26 03:51:05 +00:00
naibaandnaiba/CloudCode fb120074ac chore(security): pin JWT algorithm + SameSite and harden OAuth2 state cookie
- Set GinJWTMiddleware.SigningAlgorithm to "HS256" explicitly so a future
  library default change (or an alg:none confusion attempt) cannot weaken
  token validation. This matches the current gin-jwt default, so behaviour
  is unchanged.
- Set CookieSameSite to Lax: same as the modern-browser default, but
  pinned so server-side intent is clear and CSRF on cross-site POST is
  blocked while top-level GET (OAuth callback) still works.
- Move the nz-o2s OAuth2 state cookie into writeOauth2StateCookie and
  set HttpOnly=true. The frontend does not read this cookie, so HttpOnly
  is strictly an XSS-hardening win with no behaviour change.

JWT Cookie HttpOnly/Secure are intentionally left default for now: the
frontend reads \`!!document.cookie\` to display login state and many
deployments terminate TLS at an upstream proxy — flipping those would
require a coordinated frontend change.

Co-authored-by: naiba/CloudCode <hi+cloudcode@nai.ba>
2026-05-18 15:17:59 +00:00
naibaandnaiba/CloudCode de9ab16c5a fix: 恢复被误删的 AuthCodeURL 调用,修复编译失败
上次提交误删了 url := o2conf.AuthCodeURL(...) 导致编译失败。
同时添加注释说明 Cookie Secure 属性动态设置的原因。

Co-authored-by: naiba/CloudCode <hi+cloudcode@nai.ba>
2026-03-01 04:05:53 +00:00
naibaandnaiba/CloudCode 16c4a6c9f3 Fix: 设置Cookie Secure属性增强安全性
- 根据运行环境动态设置Secure属性
- 开发环境兼容HTTP,生产环境强制HTTPS
- 解决CodeQL扫描发现的安全漏洞

Co-authored-by: naiba/CloudCode <hi+cloudcode@nai.ba>
2026-03-01 03:49:20 +00:00
naiba 3b8d2f3736 fix: oauth2 login 2025-10-09 22:00:08 +08:00
naiba 235ee05c91 feat: upgrade frontend 2025-03-15 22:31:16 +08:00
UUBulb c0fee50776 fix: possible redirect url inconsistency (#1003) 2025-02-24 20:53:11 +08:00
UUBulb 3d591fae78 fix: struct tag (#932)
* fix: struct tag

* fix: struct tag

* fix: strings conversion

* do not write anything after redirection

* fix table

* fix select
2024-12-31 23:09:58 +08:00
UUBulb 2d03c61f38 fix: oauth2 redirect url not consistent (#930)
* fix: oauth2 redirect url not consistent

* only use one redirect uri

* feat: allow to disable password authentication

* generate translation template

* update error

* redirect

* query
2024-12-31 22:29:29 +08:00
naiba 8b9afbf39c feat: 绑定 oauth2 2024-12-28 23:50:59 +08:00
naiba 37c87bbc07 🎉 init swaggo 2024-10-19 23:14:53 +08:00
UUBulb 911bce71ff refactor: ddns (#414)
* refactor ddns

* update webhook
2024-08-24 11:11:06 +08:00
Tao Chen 3364ab9892 添加OIDC支持 (#387)
* add general OIDC

* use "github.com/coreos/go-oidc/v3/oidc" to simplify oidc config

* fix: check if https by X-Forwarded-Proto

* recovery config.yaml
2024-07-13 12:51:59 +08:00
naiba 3596e443fa refactor: imporve code style 2024-06-23 16:15:08 +08:00
Akkia c92e4999bc 支持cloudflare access OIDC认证 (#354) 2024-05-03 08:47:53 +08:00
naiba 15f4816e1f 🔒️ more secure token generation 2022-12-16 23:34:14 +08:00
Akkia 280003eed5 fix: Gitee login error 2022-10-14 01:31:00 +08:00
ysicing bfe8b7e741 feat(oauth2): add gitea oauth2 support
add gitea oauth2 support

Signed-off-by: ysicing <i@ysicing.me>
2022-10-12 19:34:15 +08:00
naiba 9a3c31154f batch update server tag 2022-09-17 00:08:27 +08:00
naiba 0f99a30c4c custon dashboard template [no ci] 2022-06-03 09:45:11 +08:00
naiba d58eb460b2 dashboard v0.13.20 Login with Gitlab、Jihulab 2022-05-26 20:00:20 +08:00
naiba 43d6a01a39 🚸 imporve redirect page 2022-04-30 21:39:25 +08:00
naiba a9277b156e 🔖 dashboard v0.13.0 Globalization 2022-04-30 00:48:39 +08:00
naiba 5e97dbff22 🔖 dashboard v0.12.25 refactor oauth2 login 2022-04-26 09:20:32 +08:00
Akkia a282383b0d fix some typo 2022-04-10 20:28:22 +08:00
naiba 5bdfc0e052 theme-mdui by @MikoyChinese 2022-01-09 11:54:14 +08:00
naiba c4257ad105 🐛 修复修改服务器导致 Agent 离线 2021-07-25 23:50:08 +08:00
naiba f7e9ea72dd 任意时间区间(月流量)统计,无视重启~ 2021-07-14 23:53:37 +08:00
naiba 93c47434ce 🏷️ 增加对 f2fs 的统计 2021-05-04 10:22:38 +08:00
naiba ca75e811ca 🐛 修复修改配置后重置为GitHub登录的问题 2021-05-02 17:21:16 +08:00
naiba 8185af768d ♻️ 重构部分代码,打印调试信息 2021-04-20 19:30:34 +08:00
naiba 8062e346d5 接入 Gitee 登录,v0.4.11 之前的用户需要更新配置文件 2021-03-02 23:08:40 +08:00
naiba 56e61f2fc1 feat: 后台服务器备注 close #72 2021-01-20 19:24:59 +08:00
naiba 29cee9865e 🔊 v0.3.0 计划任务(定期备份等场景) 2021-01-19 09:59:04 +08:00
naiba eae55c8b85 修复 go mod 不兼容 2020-12-13 00:43:25 +08:00
naiba 8d2933ac21 一键脚本 2020-11-29 22:17:40 +08:00
naiba a4611beaec 🚚 from p14yground to naiba 2020-11-11 10:07:45 +08:00
奶爸 024a11bcd0 多管理员 2020-03-22 22:28:25 +08:00
奶爸 f2080ed795 持久化Token 2019-12-20 23:58:09 +08:00
奶爸 f2f840de4a Web 服务 2019-12-08 16:59:58 +08:00