Repository navigation
Conversation
Expand NoRoute guards beyond /api /v1 /assets so unregistered /metrics, relay prefixes, health probes, and /dashboard/billing return JSON 404 instead of index.html. Keep /dashboard and /dashboard/* console routes as SPA so login redirects still work.
WalkthroughThe web router now classifies operational paths separately from SPA routes, relays 404 responses for non-SPA paths, preserves themed SPA fallback behavior, and adds tests for classification and metrics handling. ChangesWeb router non-SPA handling
Estimated code review effort: 3 (Moderate) | ~20 minutes Sequence Diagram(s)sequenceDiagram
participant HTTPClient
participant NoRoute
participant isNonSPARequestPath
participant RelayNotFound
participant ThemeAssets
HTTPClient->>NoRoute: request unmatched path
NoRoute->>isNonSPARequestPath: classify request URI
alt non-SPA path
isNonSPARequestPath-->>NoRoute: true
NoRoute->>RelayNotFound: relay 404 response
else SPA-capable path
isNonSPARequestPath-->>NoRoute: false
NoRoute->>ThemeAssets: select theme index
ThemeAssets-->>HTTPClient: return index.html
end
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@router/web_router_nonspa_test.go`:
- Around line 8-9: Update the theme-dependent SPA route tests in the affected
fixture to use assert-based route checks, explicitly configure the expected
theme, and register t.Cleanup to restore the prior theme after each test.
Replace the permissive alternative-theme assertion with an exact comparison
against the expected index payload.
In `@router/web-router.go`:
- Around line 90-91: Update the non-SPA classification in the web router to pass
c.Request.URL.Path instead of c.Request.RequestURI to isNonSPARequestPath,
preserving JSON 404 handling for unregistered backend and operations paths. Add
a test covering an encoded API prefix such as /%61pi/missing and verify it does
not fall back to HTML.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 363babe8-0a40-4867-867a-30f36c919507
📒 Files selected for processing (2)
router/web-router.gorouter/web_router_nonspa_test.go
| "github.com/gin-gonic/gin" | ||
| "github.com/stretchr/testify/require" |
There was a problem hiding this comment.
📐 Maintainability & Code Quality | 🟠 Major | ⚡ Quick win
🧩 Analysis chain
🏁 Script executed:
#!/bin/bash
# Locate the repository's theme setter/fixture pattern and existing assert usage.
rg -n -C3 --type=go '\b(SetTheme|GetTheme)\s*\(' common router
rg -n -C2 --type=go 'testify/assert|assert\.(Equal|True|NotContains)' routerRepository: QuantumNous/new-api
Length of output: 3135
🏁 Script executed:
#!/bin/bash
set -euo pipefail
printf '\n== router/web_router_nonspa_test.go ==\n'
sed -n '1,220p' router/web_router_nonspa_test.go
printf '\n== common/constants.go ==\n'
sed -n '1,120p' common/constants.go
printf '\n== router/web_router.go ==\n'
sed -n '80,115p' router/web_router.goRepository: QuantumNous/new-api
Length of output: 5396
🏁 Script executed:
#!/bin/bash
set -euo pipefail
rg -n -C2 --type=go 'testify/assert|assert\.(Equal|True|False|Contains|Empty|NotEmpty|Nil|NotNil)' router/web_router_nonspa_test.go router/*.goRepository: QuantumNous/new-api
Length of output: 938
🏁 Script executed:
#!/bin/bash
set -euo pipefail
sed -n '1,180p' router/web_router_nonspa_test.goRepository: QuantumNous/new-api
Length of output: 1993
🏁 Script executed:
#!/bin/bash
set -euo pipefail
sed -n '1,160p' router/web-router.goRepository: QuantumNous/new-api
Length of output: 2808
Make the theme-dependent SPA assertion deterministic
Use assert for the route cases, set the theme explicitly in the fixture and restore it with t.Cleanup, then assert the exact expected index payload instead of accepting either theme.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@router/web_router_nonspa_test.go` around lines 8 - 9, Update the
theme-dependent SPA route tests in the affected fixture to use assert-based
route checks, explicitly configure the expected theme, and register t.Cleanup to
restore the prior theme after each test. Replace the permissive
alternative-theme assertion with an exact comparison against the expected index
payload.
Source: Coding guidelines
| if isNonSPARequestPath(c.Request.RequestURI) { | ||
| // 未注册的后端/运维路径返回 JSON 404,禁止回退 HTML。 |
There was a problem hiding this comment.
🎯 Functional Correctness | 🟠 Major | ⚡ Quick win
🧩 Analysis chain
🏁 Script executed:
#!/bin/bash
# Confirm whether the router overrides Gin's default decoded-path behavior.
rg -n -C3 --type=go '\b(UseRawPath|UnescapePathValues|RemoveExtraSlash)\b' .Repository: QuantumNous/new-api
Length of output: 157
🏁 Script executed:
#!/bin/bash
set -euo pipefail
# Inspect the router file and path classifier.
git ls-files 'router/web-router.go' '*/*.go' | sed -n '1,120p'
echo '--- router/web-router.go (around cited lines) ---'
nl -ba router/web-router.go | sed -n '1,180p'
echo '--- search isNonSPARequestPath ---'
rg -n -C3 'isNonSPARequestPath|RequestURI|URL.Path|UseRawPath|UnescapePathValues|RemoveExtraSlash' router .Repository: QuantumNous/new-api
Length of output: 3172
🏁 Script executed:
#!/bin/bash
set -euo pipefail
echo '--- router/web-router.go (around cited lines) ---'
sed -n '1,220p' router/web-router.go
echo '--- search isNonSPARequestPath ---'
rg -n -C3 'isNonSPARequestPath|RequestURI|URL.Path|UseRawPath|UnescapePathValues|RemoveExtraSlash' router .Repository: QuantumNous/new-api
Length of output: 50375
🏁 Script executed:
#!/bin/bash
set -euo pipefail
python3 - <<'PY'
from urllib.parse import urlparse, unquote
u = urlparse("http://example.com/%61pi/missing?x=1")
print("python parsed path:", u.path)
print("python raw path:", u.path.encode())
print("python unquoted:", unquote(u.path))
PY
cat <<'EOF' >/tmp/urlcheck.go
package main
import (
"fmt"
"net/http"
)
func main() {
req, err := http.NewRequest("GET", "http://example.com/%61pi/missing?x=1", nil)
if err != nil { panic(err) }
fmt.Println("go URL.Path:", req.URL.Path)
fmt.Println("go URL.RawPath:", req.URL.RawPath)
fmt.Println("go RequestURI:", req.RequestURI)
}
EOF
go run /tmp/urlcheck.goRepository: QuantumNous/new-api
Length of output: 330
🏁 Script executed:
#!/bin/bash
set -euo pipefail
python3 - <<'PY'
from urllib.parse import urlparse, unquote
u = urlparse("http://example.com/%61pi/missing?x=1")
print("python parsed path:", u.path)
print("python unquoted:", unquote(u.path))
PY
cat <<'EOF' >/tmp/urlcheck.go
package main
import (
"fmt"
"net/http"
)
func main() {
req, err := http.NewRequest("GET", "http://example.com/%61pi/missing?x=1", nil)
if err != nil { panic(err) }
fmt.Println("go URL.Path:", req.URL.Path)
fmt.Println("go URL.RawPath:", req.URL.RawPath)
fmt.Println("go RequestURI:", req.RequestURI)
}
EOF
go run /tmp/urlcheck.goRepository: QuantumNous/new-api
Length of output: 295
Classify against c.Request.URL.Path, not RequestURI. RequestURI stays raw, so an encoded API prefix like /%61pi/missing can skip the non-SPA check and fall back to HTML instead of JSON 404. Add coverage for the encoded-prefix case. router/web-router.go:90-91
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@router/web-router.go` around lines 90 - 91, Update the non-SPA classification
in the web router to pass c.Request.URL.Path instead of c.Request.RequestURI to
isNonSPARequestPath, preserving JSON 404 handling for unregistered backend and
operations paths. Add a test covering an encoded API prefix such as
/%61pi/missing and verify it does not fall back to HTML.
|
CodeRabbit notes are mainly test fixture / path-decoding edge cases. No functional change in this pass; open for maintainer review. |
移植自上游 PR QuantumNous#6299。NoRoute 只排除 /v1、/api、/assets,导致 /metrics、/kling 等未注册路径被 index.html 伪装成 200 HTML。 改为 isNonSPARequestPath 白名单前缀判定:API/Relay/运维路径返回 JSON 404,/dashboard/billing 之外的 /dashboard 仍走 SPA。 与原 PR 差异:nonSPAPathPrefixes 增补本 fork 的 /seedance(火山 ARK 视频入口);未移植未被调用的 ThemeAssets.Available()(死代码)。 补路径判定与 /metrics 不返回 HTML 的路由测试。 Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
Withdrawing this contribution. No further action needed from maintainers. Closing and deleting the head branch. |
Summary
Third small PR from the withdrawn hardening effort: SPA NoRoute hygiene.
Embedded frontend
NoRoutepreviously only excluded/api,/v1, and/assets. Unregistered ops/relay paths such as/metricscould fall through toindex.htmlwith HTTP 200, which breaks monitoring and masks missing routes.This PR:
/metrics, relay prefixes, health probes,/dashboard/billing, …)./dashboardand/dashboard/{section}as SPA routes (console), while still treating OpenAI-compatible/dashboard/billingas non-SPA.Test plan
go test ./router -run 'NonSPA|Metrics'GET /metrics→ non-HTML 404 when metrics disabledGET /dashboard/overviewstill serves SPA after login redirectNotes
FRONTEND_MODEwork (separate PR later).Summary by CodeRabbit
Bug Fixes
Tests