fix(xray): confine log paths written under any key case

resolveXrayLogPaths looked the log object up by the exact keys "access" and
"error", but xray-core decodes that object with encoding/json, which falls back
to a case-insensitive field match. "Access": "/tmp/pwn.log" therefore reached
AccessLog untouched and Xray — root, in a standard install — created the file
there, reopening the arbitrary write that GHSA-jm48-m3rr-9hgg closed.

Fold every case variant onto the canonical key before confining it. When both a
canonical key and a variant are present the canonical value wins, so a
"none" cannot be overridden by a smuggled "Access" path.
This commit is contained in:
Sanaei
2026-09-03 18:06:15 +02:00
parent 25d0c06f89
commit f9de0226fe
2 changed files with 105 additions and 1 deletions
+32 -1
View File
@@ -7,6 +7,7 @@ import (
"path"
"path/filepath"
"runtime"
"slices"
"strings"
"sync"
@@ -1034,6 +1035,19 @@ func ensureStatsPolicy(policy json_util.RawMessage) json_util.RawMessage {
return out
}
// caseVariantKeys returns every key of parsed that equals want ignoring case,
// lowest first so the fold is deterministic when several variants are present.
func caseVariantKeys(parsed map[string]any, want string) []string {
var keys []string
for key := range parsed {
if strings.EqualFold(key, want) {
keys = append(keys, key)
}
}
slices.Sort(keys)
return keys
}
func resolveXrayLogPaths(logCfg json_util.RawMessage) json_util.RawMessage {
if len(logCfg) == 0 {
return logCfg
@@ -1044,12 +1058,29 @@ func resolveXrayLogPaths(logCfg json_util.RawMessage) json_util.RawMessage {
}
changed := false
for _, key := range []string{"access", "error"} {
v, ok := parsed[key].(string)
// xray-core decodes this object with encoding/json, whose case-insensitive
// field match makes "Access" reach AccessLog too — fold every variant.
variants := caseVariantKeys(parsed, key)
value, hasValue := parsed[key]
for _, variant := range variants {
if variant == key {
continue
}
if !hasValue {
value, hasValue = parsed[variant], true
}
delete(parsed, variant)
changed = true
}
v, ok := value.(string)
if !ok {
continue
}
trimmed := strings.TrimSpace(v)
if trimmed == "" || strings.EqualFold(trimmed, "none") {
if changed {
parsed[key] = v
}
continue
}
base := path.Base(filepath.ToSlash(trimmed))