Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,12 @@ go test -run TestRealityEndToEnd -v ./... # needs xray on PATH
key when it is enabled. The key is the reason to use `-passphrase` for
anything that leaves the machine; keep the passphrase somewhere other than the
backup directory.
- The admin API refuses state-changing requests that another site could have
caused. It authenticates with HTTP Basic, and browsers attach those credentials
to any request for the origin no matter which page started it, so without this
a form on any website could delete a user or revoke a REALITY UUID. Requests
arriving with a foreign `Origin` or `Sec-Fetch-Site: cross-site` are refused;
requests with no `Origin` are left alone, so `curl` and scripts keep working.
- `/health` is the only endpoint reachable without admin credentials, and it
returns `{"status":"ok"}` with no uptime or version: on a public port it is
the one thing a scanner can read, so it says nothing. `/metrics`,
Expand Down
145 changes: 145 additions & 0 deletions csrf_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
package main

import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"
)

// The admin API uses HTTP Basic, and a browser attaches those credentials to any
// request for the origin -- including one a page on another site started. These
// tests hold the actual route that deletes a VLESS UUID, which is the worst case
// for an attacker: no request body, no unusual method, nothing a cross-site form
// cannot produce.
func csrfTestServer(t *testing.T) (*ProxyServer, *httptest.ResponseRecorder, string) {
t.Helper()

cfg := DefaultConfig()
cfg.AuthEnabled = false
s := NewProxyServer(cfg)
s.store = newTestStore(t)
row, err := s.store.CreateVlessClient("11111111-2222-3333-4444-555555555555", "victim")
if err != nil {
t.Fatalf("seed: %v", err)
}
return s, nil, fmt.Sprintf("%d", row.ID)
}

func csrfRequest(t *testing.T, s *ProxyServer, method, path string, headers map[string]string) *httptest.ResponseRecorder {
t.Helper()
req := httptest.NewRequest(method, path, strings.NewReader("{}"))
req.RemoteAddr = "127.0.0.1:5000"
req.Host = "admin.example.com"
for k, v := range headers {
req.Header.Set(k, v)
}
rec := httptest.NewRecorder()
s.adminHandler().ServeHTTP(rec, req)
return rec
}

func TestCSRFRefusesCrossSiteStateChanges(t *testing.T) {
s, _, id := csrfTestServer(t)
path := "/api/vless-clients/" + id

// What a page on another site can produce: a simple request, no preflight,
// no body. Browsers attach the admin's Basic credentials to it.
rec := csrfRequest(t, s, http.MethodDelete, path, map[string]string{
"Origin": "https://evil.example",
})
if rec.Code != http.StatusForbidden {
t.Errorf("DELETE with a foreign Origin = %d, want 403: %s", rec.Code, rec.Body)
}

// Sec-Fetch-Site catches the same thing even when Origin is stripped.
rec = csrfRequest(t, s, http.MethodDelete, path, map[string]string{
"Sec-Fetch-Site": "cross-site",
})
if rec.Code != http.StatusForbidden {
t.Errorf("DELETE with Sec-Fetch-Site: cross-site = %d, want 403: %s", rec.Code, rec.Body)
}

// The user must still exist: the request was refused, not partly honoured.
clients, err := s.store.ListVlessClients()
if err != nil || len(clients) != 1 {
t.Errorf("the refused request still deleted the client: %+v, %v", clients, err)
}
}

func TestCSRFAllowsSameOriginAndNonBrowser(t *testing.T) {
// What matters is that the request got past the CSRF layer and reached the
// route, which may then refuse it for its own reasons -- REALITY being off
// in this server, for one. Anything but 403 means the check let it by.
cases := []struct {
name string
headers map[string]string
}{
{"same origin, as the dashboard sends it", map[string]string{
"Origin": "https://admin.example.com",
"Sec-Fetch-Site": "same-origin",
}},
{"no origin at all, as curl sends it", nil},
{"same-site navigation", map[string]string{"Sec-Fetch-Site": "same-site"}},
{"a direct link opened by hand", map[string]string{"Sec-Fetch-Site": "none"}},
}

for _, tc := range cases {
server, _, id := csrfTestServer(t)
rec := csrfRequest(t, server, http.MethodDelete, "/api/vless-clients/"+id, tc.headers)
if rec.Code == http.StatusForbidden {
t.Errorf("%s: refused as cross-site, want it to reach the route", tc.name)
}
}
}

func TestCSRFLeavesReadsAlone(t *testing.T) {
s, _, _ := csrfTestServer(t)

// A cross-site GET is harmless here and must not start failing: nothing on
// this API changes state on a read.
rec := csrfRequest(t, s, http.MethodGet, "/api/vless-clients", map[string]string{
"Origin": "https://evil.example",
})
if rec.Code != http.StatusOK {
t.Fatalf("cross-origin GET = %d, want 200: %s", rec.Code, rec.Body)
}
var listing struct {
Clients []VlessClient `json:"clients"`
}
if err := json.Unmarshal(rec.Body.Bytes(), &listing); err != nil {
t.Fatalf("unmarshal: %v", err)
}
if len(listing.Clients) != 1 {
t.Errorf("clients = %d, want 1", len(listing.Clients))
}
}

// The whole point of the change, exercised the way the attack would arrive: an
// admin's browser, carrying Basic credentials, hitting a mutating route that a
// page on another site caused.
func TestCSRFAttackCannotReachAStateChangingRoute(t *testing.T) {
s, _, id := csrfTestServer(t)

// What the browser sends for a cross-site form POST: no Origin suppressed,
// no preflight, simple headers only.
req := httptest.NewRequest(http.MethodPost, "/api/vless-clients/"+id+"/reset", nil)
req.Host = "admin.example.com"
req.RemoteAddr = "203.0.113.7:44444"
req.Header.Set("Sec-Fetch-Site", "cross-site")
req.Header.Set("Sec-Fetch-Mode", "cors")
req.Header.Set("Origin", "https://attacker.example")
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")

rec := httptest.NewRecorder()
s.adminHandler().ServeHTTP(rec, req)

if rec.Code != http.StatusForbidden {
t.Fatalf("a cross-site request reached the route: %d %s", rec.Code, rec.Body)
}
if strings.Contains(rec.Body.String(), "xray") {
t.Error("the request was handled by the REALITY route instead of being refused")
}
}
45 changes: 45 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"log/slog"
"net"
"net/http"
"net/url"
"os"
"os/signal"
"path/filepath"
Expand Down Expand Up @@ -1175,6 +1176,49 @@ func basicAuth(user, pass string) func(http.Handler) http.Handler {
}
}

// sameOriginOnly refuses state-changing requests that a page on another site
// could have caused.
//
// The admin API is authenticated with HTTP Basic, and browsers attach Basic
// credentials to any request for that origin regardless of which page started
// it. That makes every mutating route here a target: a form on any website can
// reset a user's data usage or revoke a VLESS UUID, and the browser will
// cheerfully attach the admin's credentials on the way.
//
// The test is the one the browser makes easy. Origin is sent on every cross-site
// state-changing request, and on same-origin ones, so a mutating request that
// arrives with no Origin at all did not come from a browser page and is left
// alone -- that keeps curl and the API working. Sec-Fetch-Site, which modern
// browsers send on every request, is the belt to that pair of braces.
func sameOriginOnly(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet, http.MethodHead, http.MethodOptions:
next.ServeHTTP(w, r)
return
}

// Sec-Fetch-Site is the coarse signal: it names a different site, not a
// different origin, so it is only trusted to rule requests out. The
// Origin check below is what actually decides, and a request from
// another subdomain is settled there rather than here.
if site := r.Header.Get("Sec-Fetch-Site"); site == "cross-site" {
http.Error(w, "cross-site request refused", http.StatusForbidden)
return
}

if origin := r.Header.Get("Origin"); origin != "" {
u, err := url.Parse(origin)
if err != nil || u.Host == "" || !strings.EqualFold(u.Host, r.Host) {
http.Error(w, "cross-site request refused", http.StatusForbidden)
return
}
}

next.ServeHTTP(w, r)
})
}

// ─── Rate Limiter ──────────────────────────────────────────────────────────

type rateLimiter struct {
Expand Down Expand Up @@ -2069,6 +2113,7 @@ func (s *ProxyServer) adminHandler() http.Handler {
s.limiter = newRateLimiterSet(s.cfg.RateLimitRPS)
handler = rateLimit(s.limiter)(handler)
}
handler = sameOriginOnly(handler)
if s.cfg.AdminEnabled && s.cfg.AdminUser != "" && s.cfg.AdminPass != "" {
handler = basicAuth(s.cfg.AdminUser, s.cfg.AdminPass)(handler)
}
Expand Down
Loading