sub2api icon indicating copy to clipboard operation
sub2api copied to clipboard

我的sub2api 登不上了,一登录进后台就秒过期退出登录

Open mofeiss opened this issue 4 months ago • 11 comments

刚刚自动更新到最新版以后就出现这个问题.回退部署到老版本也不行,一进后台就被秒踢.

mofeiss avatar Jul 16 '26 15:07 mofeiss

I encountered the same issue and confirmed that it is related to the session binding feature introduced in v0.1.158.

Environment

  • Sub2API v0.1.158
  • Docker deployment
  • Cloudflare proxy
  • server.trusted_proxies is not configured
  • session_binding_enabled uses its default value of true

Request flow

  1. POST /api/v1/auth/login returns 200.
  2. The subsequent GET /api/v1/auth/me returns 401 with error code SESSION_BINDING_MISMATCH.
  3. After the session family is revoked, POST /api/v1/auth/refresh also returns 401, with the log message Refresh token not found, possible reuse attack.

Root cause

SessionBindingContext obtains the client IP through ip.GetTrustedClientIP(c) and combines it with the User-Agent to generate a session fingerprint. When the service is behind Cloudflare and server.trusted_proxies is empty, the backend may use the Cloudflare edge node IP instead of the actual client IP. If the login request and subsequent requests pass through different edge nodes, the fingerprint changes and the newly issued session is immediately revoked.

The startup log also contains this warning:

Warning: server.trusted_proxies is empty in release mode; client IP trust chain is disabled

Temporary workaround

Disable session binding:

INSERT INTO settings (key, value, updated_at)
VALUES ('session_binding_enabled', 'false', NOW())
ON CONFLICT (key)
DO UPDATE SET value = EXCLUDED.value, updated_at = EXCLUDED.updated_at;

After disabling it and restarting the service, I verified the complete flow through the public domain:

  • Login: 200
  • /auth/me: 200
  • Token refresh: 200
  • /auth/me with the new access token: 200

aomsir avatar Jul 16 '26 18:07 aomsir

我遇到了同样的问题,并确认这与 v0.1.158 中引入的会话绑定功能有关。

Environment

  • Sub2API v0.1.158
  • Docker 部署
  • Cloudflare 代理
  • server.trusted_proxies 没有配置
  • session_binding_enabled 使用其默认值 true

Request flow

  1. POST /api/v1/auth/login 返回 200。
  2. 随后的 GET /api/v1/auth/me 返回 401,错误代码为 SESSION_BINDING_MISMATCH。
  3. After the session family is revoked, POST /api/v1/auth/refresh also returns 401, with the log message Refresh token not found, possible reuse attack.

Root cause

SessionBindingContext obtains the client IP through ip.GetTrustedClientIP(c) and combines it with the User-Agent to generate a session fingerprint. When the service is behind Cloudflare and server.trusted_proxies is empty, the backend may use the Cloudflare edge node IP instead of the actual client IP. If the login request and subsequent requests pass through different edge nodes, the fingerprint changes and the newly issued session is immediately revoked. 

The startup log also contains this warning: 

Warning: server.trusted_proxies is empty in release mode; client IP trust chain is disabled

Temporary workaround

Disable session binding: 

INSERT INTO settings (key, value, updated_at) VALUES ('session_binding_enabled', 'false', NOW()) ON CONFLICT (key) DO UPDATE SET value = EXCLUDED.value, updated_at = EXCLUDED.updated_at; 禁用后重启服务,我通过公共域验证了完整流程:

  • 登录:200
  • /auth/me:200
  • 令牌刷新:200
  • /auth/me with the new access token: 200

You didn’t just fix the bug, you saved my sanity. My hair thanks you. 🫡

mofeiss avatar Jul 16 '26 18:07 mofeiss

+1

nicemkbk avatar Jul 17 '26 01:07 nicemkbk

我也遇到了这个问题
可以测试一下 这个方法 只能临时使用

在Postgres 的settings表 按照它的格式 添加一条session_binding_enabled=false

xbc001 avatar Jul 17 '26 06:07 xbc001

这傻X项目。。。VIBE之后真的自己检查过么

b1ghawk119 avatar Jul 17 '26 07:07 b1ghawk119

I had the same problem, and I was able to fix it by following your workaround. Thanks!

iadail avatar Jul 17 '26 08:07 iadail

真是傻逼吧 每一个版本都有这种鬼问题

HeLongaa avatar Jul 17 '26 08:07 HeLongaa

一直退出我还以为是我机器出啦问题 修一下吧

agboreyam255-dev avatar Jul 17 '26 09:07 agboreyam255-dev

在系统设置中 安全与认证 会话 IP/UA 绑定 关闭就可以了 ,默认是开启的

cranux avatar Jul 17 '26 10:07 cranux

容器部署的直接执行这个命令就行 docker exec -i sub2api-postgres psql -U sub2api -d sub2api -v ON_ERROR_STOP=1 <<'SQL' INSERT INTO settings (key, value, updated_at) VALUES ('session_binding_enabled', 'false', NOW()) ON CONFLICT (key) DO UPDATE SET value = EXCLUDED.value, updated_at = EXCLUDED.updated_at;

SELECT key, value, updated_at FROM settings WHERE key = 'session_binding_enabled'; SQL

1754164240 avatar Jul 18 '26 03:07 1754164240

Confirmed the same issue on Sub2API v0.1.160 in a Docker deployment behind Cloudflare.

Reproduction:

  1. POST /api/v1/auth/login returns HTTP 200.
  2. The immediately following GET /api/v1/auth/me returns HTTP 401 with SESSION_BINDING_MISMATCH.
  3. The refresh token is then rejected because the session family has already been revoked.

Temporarily disabling session IP/UA binding restored the complete login, /auth/me, token refresh, and post-refresh authentication flow.

PR #4463 matches this failure mode and provides a safer fallback when no trusted proxy chain is configured. Please consider updating/rebasing and merging #4463.

turingcat avatar Jul 18 '26 04:07 turingcat