mirror of
https://github.com/Buriburizaem0n/nezha_domains.git
synced 2026-11-04 16:40:21 +00:00
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.
104 lines
3.1 KiB
Go
104 lines
3.1 KiB
Go
//go:build !agentcompat
|
|
|
|
package rpc
|
|
|
|
import (
|
|
"net/http"
|
|
"testing"
|
|
|
|
"github.com/nezhahq/nezha/model"
|
|
"github.com/nezhahq/nezha/pkg/agentcompatcontract"
|
|
)
|
|
|
|
func TestPrepareNATCapabilityDefaultPreservesHeaderAsOrdinaryRequestData(t *testing.T) {
|
|
// Given
|
|
installNATCredentialTestGate(t)
|
|
request := &http.Request{Header: make(http.Header)}
|
|
request.Header.Set(agentcompatcontract.IOStreamCapabilityHeader, "ordinary-data")
|
|
request.Header.Set("Authorization", natTestForeignRandom)
|
|
|
|
// When
|
|
lease, err := prepareNATCapability(request, &model.NAT{Common: model.Common{ID: 91}, ServerID: 81})
|
|
|
|
// Then
|
|
if err != nil {
|
|
t.Fatalf("default NAT capability hook returned error: %v", err)
|
|
}
|
|
if lease.active {
|
|
t.Fatal("default NAT capability hook unexpectedly activated")
|
|
}
|
|
if got := request.Header.Get(agentcompatcontract.IOStreamCapabilityHeader); got != "ordinary-data" {
|
|
t.Fatalf("default NAT capability hook changed header to %q", got)
|
|
}
|
|
if got := request.Header.Get("Authorization"); got != natTestForeignRandom {
|
|
t.Fatalf("default NAT capability hook changed foreign Authorization to %q", got)
|
|
}
|
|
}
|
|
|
|
func TestPrepareNATCapabilityDefaultStripsDashboardCredentials(t *testing.T) {
|
|
// Given
|
|
installNATCredentialTestGate(t)
|
|
for name, credential := range map[string]string{
|
|
"dashboard jwt": natTestDashboardJWT,
|
|
"expired dashboard jwt": natTestDashboardExpiredJWT,
|
|
"dashboard api token": natTestDashboardAPIToken,
|
|
} {
|
|
t.Run(name, func(t *testing.T) {
|
|
request := &http.Request{Header: make(http.Header)}
|
|
request.Header.Set("Authorization", credential)
|
|
|
|
// When
|
|
lease, err := prepareNATCapability(request, &model.NAT{Common: model.Common{ID: 91}, ServerID: 81})
|
|
|
|
// Then
|
|
if err != nil {
|
|
t.Fatalf("default NAT capability hook returned error: %v", err)
|
|
}
|
|
if lease.active {
|
|
t.Fatal("default NAT capability hook unexpectedly activated")
|
|
}
|
|
if got := request.Header.Get("Authorization"); got != "" {
|
|
t.Fatalf("default NAT capability hook retained dashboard credential %q", got)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestPrepareNATCapabilityDefaultKeepsForeignAndMissingAuthorization(t *testing.T) {
|
|
// Given
|
|
installNATCredentialTestGate(t)
|
|
for name, values := range map[string][]string{
|
|
"foreign jwt": {natTestForeignJWT},
|
|
"foreign random": {natTestForeignRandom},
|
|
"empty value": {""},
|
|
"missing header": nil,
|
|
} {
|
|
t.Run(name, func(t *testing.T) {
|
|
request := &http.Request{Header: make(http.Header)}
|
|
for _, value := range values {
|
|
request.Header.Set("Authorization", value)
|
|
}
|
|
|
|
// When
|
|
lease, err := prepareNATCapability(request, &model.NAT{Common: model.Common{ID: 91}, ServerID: 81})
|
|
|
|
// Then
|
|
if err != nil {
|
|
t.Fatalf("default NAT capability hook returned error: %v", err)
|
|
}
|
|
if lease.active {
|
|
t.Fatal("default NAT capability hook unexpectedly activated")
|
|
}
|
|
got := request.Header.Values("Authorization")
|
|
if len(got) != len(values) {
|
|
t.Fatalf("default NAT capability hook changed Authorization to %q, want %q", got, values)
|
|
}
|
|
for i := range values {
|
|
if got[i] != values[i] {
|
|
t.Fatalf("default NAT capability hook changed Authorization to %q, want %q", got, values)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|