From ff944ab672bfe39261470cfbd54e372998139301 Mon Sep 17 00:00:00 2001 From: "usehoplite[bot]" <288093033+usehoplite[bot]@users.noreply.github.com> Date: Thu, 3 Sep 2026 04:48:18 +0000 Subject: [PATCH 1/2] =?UTF-8?q?fix(auth):=20=E5=90=8E=E7=AB=AF=E4=B8=8D?= =?UTF-8?q?=E5=8F=AF=E8=BE=BE=E6=97=B6=E7=99=BB=E5=BD=95=E9=A1=B5=E6=98=BE?= =?UTF-8?q?=E7=A4=BA=E5=8F=8B=E5=A5=BD=E9=94=99=E8=AF=AF=E8=80=8C=E9=9D=9E?= =?UTF-8?q?=E5=8E=9F=E5=A7=8B=E5=BC=95=E6=93=8E=E6=8A=A5=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit /api 响应体为空或非 JSON(后端宕机、网关 502)时,response.json() 抛出 SyntaxError 并被直接展示给用户(Failed to execute 'json' on 'Response'...)。 改为安全解析:无法解析 JSON 时给出可读的中文提示,checkAuth 不再把后端 不可达误判为首装流程。 Co-authored-by: DSUK <2285740204@qq.com> --- src/js/store.js | 46 +++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 41 insertions(+), 5 deletions(-) diff --git a/src/js/store.js b/src/js/store.js index 4c0ac290..fd9d5043 100644 --- a/src/js/store.js +++ b/src/js/store.js @@ -741,6 +741,27 @@ const persistAskAIOpen = (open) => { } }; +// 网关/反向代理出错时响应体可能为空或非 JSON,response.json() 会抛引擎级 +// SyntaxError;向用户展示原始英文报错没有意义,统一转成可读提示。 +const readJsonSafely = async (response) => { + try { + return await response.json(); + } catch { + return null; + } +}; + +const toFriendlyAuthError = (error) => { + const message = String(error?.message || ''); + if ( + message.toLowerCase().includes('failed to fetch') || + message.toLowerCase().includes('unexpected end of json input') + ) { + return '无法连接服务器,请检查网络后重试'; + } + return message || '网络验证失败'; +}; + const useStore = create((set, get) => ({ // --- 1. 认证状态 --- isAuthenticated: false, @@ -1024,8 +1045,8 @@ const useStore = create((set, get) => ({ // 1. 优先检查当前 Session 是否已认证;显式退出后不再信任残留 Cookie。 if (!explicitlyLoggedOut) { const sessionRes = await fetch('/api/auth/session'); - const { authenticated } = await sessionRes.json(); - if (authenticated) { + const sessionData = await readJsonSafely(sessionRes); + if (sessionData?.authenticated) { clearPendingAuthProvider(); set({ isAuthenticated: true, showLoginModal: false, isCheckingAuth: false }); return true; @@ -1034,7 +1055,13 @@ const useStore = create((set, get) => ({ // 2. 如果 Session 不存在,再检查基本配置并尝试自动登录 const res = await fetch('/api/auth/check-password'); - const { hasPassword, isDemoMode } = await res.json(); + const checkData = await readJsonSafely(res); + if (checkData === null) { + // 后端不可达或响应非 JSON:回到登录页,避免误判为首次安装 + set({ showLoginModal: true }); + return false; + } + const { hasPassword, isDemoMode } = checkData; set({ isDemoMode }); if (isDemoMode) { @@ -1080,7 +1107,16 @@ const useStore = create((set, get) => ({ body: JSON.stringify(requestBody), }); - const result = await response.json(); + const result = await readJsonSafely(response); + + if (result === null) { + const errorMsg = response.ok ? '服务器响应异常,请稍后重试' : '无法连接服务器,请检查网络后重试'; + set({ loginError: errorMsg }); + if (!silent) { + toastManager.error(errorMsg); + } + return false; + } if (response.status === 429) { const errorMsg = result.error || '登录过于频繁,请稍后再试'; @@ -1132,7 +1168,7 @@ const useStore = create((set, get) => ({ return false; } } catch (error) { - const catchMsg = error.message || '网络验证失败'; + const catchMsg = toFriendlyAuthError(error); set({ loginError: catchMsg }); if (!silent) { toastManager.error(catchMsg); From e51075f4389e90a4fce870c2e8efb21e6aca2004 Mon Sep 17 00:00:00 2001 From: "usehoplite[bot]" <288093033+usehoplite[bot]@users.noreply.github.com> Date: Thu, 3 Sep 2026 05:18:50 +0000 Subject: [PATCH 2/2] =?UTF-8?q?fix(ci):=20=E4=BF=AE=E5=A4=8D=20backend=20?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=E4=B8=8E=E4=BE=9D=E8=B5=96=E5=AE=A1=E8=AE=A1?= =?UTF-8?q?=E5=A4=B1=E8=B4=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - TestGetSubscriptionUsage 硬编码 2026-08 周期日期,跨月后 CycleWindow 计算的当前周期不再匹配种子数据(cycle used=0)。改为运行时用与生产 代码一致的 CycleWindow 计算周期边界,断言语义不变。 - golang.org/x/crypto v0.54.0 存在 govulncheck 上报的 2 个漏洞 (ssh.Dial 可达),升级到修复版本 v0.56.0。 Co-authored-by: DSUK <2285740204@qq.com> --- backend-go/go.mod | 4 ++-- backend-go/go.sum | 4 ++-- backend-go/internal/subscription/service_test.go | 6 +++++- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/backend-go/go.mod b/backend-go/go.mod index 5aec9742..79517470 100644 --- a/backend-go/go.mod +++ b/backend-go/go.mod @@ -11,7 +11,8 @@ require ( github.com/robfig/cron/v3 v3.0.1 github.com/shirou/gopsutil/v4 v4.26.6 github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e - golang.org/x/crypto v0.54.0 + golang.org/x/crypto v0.56.0 + golang.org/x/net v0.57.0 gopkg.in/yaml.v3 v3.0.1 modernc.org/sqlite v1.53.0 ) @@ -38,7 +39,6 @@ require ( github.com/x448/float16 v0.8.4 // indirect github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78 // indirect github.com/yusufpapurcu/wmi v1.2.4 // indirect - golang.org/x/net v0.57.0 // indirect golang.org/x/sync v0.22.0 // indirect golang.org/x/sys v0.47.0 // indirect golang.org/x/tools v0.47.0 // indirect diff --git a/backend-go/go.sum b/backend-go/go.sum index 3c9de20f..0a571061 100644 --- a/backend-go/go.sum +++ b/backend-go/go.sum @@ -77,8 +77,8 @@ github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78 h1:ilQV1hzziu+LLM3zU github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78/go.mod h1:aL8wCCfTfSfmXjznFBSZNN13rSJjlIOI1fUNAtF7rmI= github.com/yusufpapurcu/wmi v1.2.4 h1:zFUKzehAFReQwLys1b/iSMl+JQGSCSjtVqQn9bBrPo0= github.com/yusufpapurcu/wmi v1.2.4/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0= -golang.org/x/crypto v0.54.0 h1:YLIA59K4fiNzHzjnZt2tUJQjQtUWfWbeHBqKtk3eScw= -golang.org/x/crypto v0.54.0/go.mod h1:KWL8ny2AZdGR2cWmzeHrp2azQPGogOv+HeQaVEXC2dk= +golang.org/x/crypto v0.56.0 h1:GUh5Ii4J5jtcseSMiRqr1jXCNHoxjeV9Fmekc2oLy6Y= +golang.org/x/crypto v0.56.0/go.mod h1:OMW5y6CY9l38uPLmxU6l6pwcXp1obtLo3e6gT7gQR2I= golang.org/x/mod v0.37.0 h1:vF1DjpVEshcIqoEaauuHebaLk1O1forxjxBaVn884JQ= golang.org/x/mod v0.37.0/go.mod h1:m8S8VeM9r4dzDwjrKO0a1sZP3YjeMamRRlD+fmR2Q/0= golang.org/x/net v0.57.0 h1:K5+3DljvIuDG9/Jv9rvyMywYNFCQ9RSUY6OOTTkT+tE= diff --git a/backend-go/internal/subscription/service_test.go b/backend-go/internal/subscription/service_test.go index 5363c3c1..b3bf1bff 100644 --- a/backend-go/internal/subscription/service_test.go +++ b/backend-go/internal/subscription/service_test.go @@ -14,6 +14,7 @@ import ( "github.com/iwvw/api-monitor/backend-go/internal/config" "github.com/iwvw/api-monitor/backend-go/internal/secure" + "github.com/iwvw/api-monitor/backend-go/internal/subscriptionledger" "gopkg.in/yaml.v3" _ "modernc.org/sqlite" ) @@ -2254,8 +2255,11 @@ func TestGetSubscriptionUsage(t *testing.T) { VALUES('sub-u','sub_default_nodes','plan-u','UsageSub','tok-u','uuid-u','pwd-u',1,'2026-08-01 00:00:00','panel')`); err != nil { t.Fatal(err) } + // 用与生产代码一致的 CycleWindow 计算「当前周期」,避免硬编码日期 + // 在跨月后使种子数据不再属于当前周期(cycle used 变为 0)。 + cycleStart, cycleEnd := subscriptionledger.CycleWindow(time.Now().UTC(), "monthly", 1, "2026-08-01 00:00:00") if _, err := db.Exec(`INSERT INTO subscription_usage_cycles(subscription_id,cycle_start,cycle_end,upload_bytes,download_bytes,updated_at) - VALUES('sub-u','2026-08-01T00:00:00Z','2026-09-01T00:00:00Z',10,20,datetime('now'))`); err != nil { + VALUES('sub-u',?,?,10,20,datetime('now'))`, cycleStart, cycleEnd); err != nil { t.Fatal(err) } hour := time.Now().UTC().Truncate(time.Hour).Format(time.RFC3339)