Files
nezha_domains/cmd/dashboard/rpc/nat_capability_default.go
T
xvkong233 40240ed02b fix(nat): strip only dashboard-issued Authorization credentials at NAT ingress
Since 2640b86 (v2.2.11, 2026-07-20) the NAT ingress deleted the entire
Authorization header, breaking self-hosted apps behind the tunnel that
authenticate with their own Bearer tokens (e.g. the Qinglong panel login
loop).

Strip only credentials issued by this dashboard and forward foreign ones:

- controller.IsDashboardCredential classifies an Authorization value as
  dashboard-issued via a signature-only JWT parser derived from the auth
  middleware (same key, HS256 pinned, claims validation deliberately
  skipped so an expired panel token is still recognized and kept out of
  the tunnel) or an api_tokens lookup for PAT-shaped values. Unknown PATs,
  foreign signatures, other schemes and missing values pass through. When
  classification cannot complete (parser not ready, DB nil or error) the
  verdict fails closed (strip) and the log carries a fixed reason only,
  never credential content.
- Both NAT capability variants (default, and agent-compat legacy /
  invalid-capability / post-consume paths) replace the unconditional
  Del("Authorization") with stripDashboardCredential. The rpc package
  receives the classifier through SetNATDashboardCredentialGate, the same
  injection pattern as SetMCPKillSwitchObserver; an unwired gate keeps the
  historical strip-everything behavior.

Tests: controller classification semantics (panel JWT, expired panel JWT
and panel PAT stripped; foreign-signed JWT, unknown nzp_ token, random
strings, non-Bearer schemes and missing header pass through; parser
unavailable, DB nil and DB error fail closed with credential-free logs)
plus rpc unit and flow coverage for both variants (dashboard credentials
never reach the forwarded bytes; foreign and missing Authorization are
forwarded untouched).

Compatibility: no API, database or frontend changes; NAT backends that do
not use Authorization behave exactly as before.
2026-10-01 11:17:45 +00:00

30 lines
932 B
Go

//go:build !agentcompat
package rpc
import (
"net/http"
"github.com/nezhahq/nezha/model"
serviceRPC "github.com/nezhahq/nezha/service/rpc"
)
type natCapabilityLease struct {
active bool
publicationOwned bool
streamLease *serviceRPC.AgentCompatNATStreamLease
access serviceRPC.AgentCompatCapabilityAccess
handle serviceRPC.AgentCompatNATPublishHandle
}
func prepareNATCapability(request *http.Request, _ *model.NAT) (natCapabilityLease, error) {
// The default build has no capability protocol: every request takes the
// legacy NAT path, so any dashboard-issued Authorization value (panel JWT
// or panel PAT) is stripped before the request is tunneled to the agent.
// Foreign Authorization values are ordinary request data and stay put.
stripDashboardCredential(request)
return natCapabilityLease{}, nil
}
func (lease natCapabilityLease) cleanup(*serviceRPC.NezhaHandler) {}