Config: Support env XRAY_JSON_STRICT=true (#6053)

https://github.com/XTLS/Xray-core/pull/6053#issuecomment-4363840170

---------

Co-authored-by: RPRX <63339210+RPRX@users.noreply.github.com>
This commit is contained in:
Yury Kastov
2026-05-02 16:11:40 +03:00
committed by GitHub
parent 1d62941bd2
commit bdff2fa72e
4 changed files with 44 additions and 1 deletions
+15 -1
View File
@@ -5,12 +5,22 @@ import (
"io"
"github.com/xtls/xray-core/common/errors"
"github.com/xtls/xray-core/common/platform"
creflect "github.com/xtls/xray-core/common/reflect"
"github.com/xtls/xray-core/core"
"github.com/xtls/xray-core/infra/conf"
"github.com/xtls/xray-core/main/confloader"
)
// UseStrictJSON, when true, makes JSON config decoders skip the custom
// comment-stripping reader and parse input as strict RFC 8259 JSON.
//
// Enabled by setting the env variable xray.json.strict=true (or its normalized
// form XRAY_JSON_STRICT=true). Default false preserves backward-compatible
// behavior for human-edited configs that may contain comments or other
// JSON5/JSONC syntax.
var UseStrictJSON = platform.NewEnvFlag(platform.UseStrictJSON).GetValue(func() string { return "" }) == "true"
func MergeConfigFromFiles(files []*core.ConfigSource) (string, error) {
c, err := mergeConfigs(files)
if err != nil {
@@ -31,7 +41,11 @@ func mergeConfigs(files []*core.ConfigSource) (*conf.Config, error) {
if err != nil {
return nil, errors.New("failed to read config: ", file).Base(err)
}
c, err := ReaderDecoderByFormat[file.Format](r)
decoder := ReaderDecoderByFormat[file.Format]
if file.Format == "json" && UseStrictJSON {
decoder = DecodeJSONConfigStrict
}
c, err := decoder(r)
if err != nil {
return nil, errors.New("failed to decode config: ", file).Base(err)
}