XTLS/Xray-core · error

Invalid base64 string

Error message

Invalid base64 string

What it means

Thrown by ParseNoise when a noise entry of type "base64" cannot be decoded. Xray first translates URL-safe characters and strips '=' padding via a replacer, then uses RawURLEncoding; input that still fails (invalid characters, bad length) raises this error with the decode cause chained. Build-time only, like the other noise errors.

Source

Thrown at infra/conf/freedom.go:228

			return nil, errors.New("rand lengthMin or lengthMax cannot be 0")
		}

	case "str":
		// user input string
		NConfig.Packet = []byte(noise.Packet)

	case "hex":
		// user input hex
		NConfig.Packet, err = hex.DecodeString(noise.Packet)
		if err != nil {
			return nil, errors.New("Invalid hex string").Base(err)
		}

	case "base64":
		// user input base64
		NConfig.Packet, err = base64.RawURLEncoding.DecodeString(strings.NewReplacer("+", "-", "/", "_", "=", "").Replace(noise.Packet))
		if err != nil {
			return nil, errors.New("Invalid base64 string").Base(err)
		}

	default:
		return nil, errors.New("Invalid packet, only rand/str/hex/base64 are supported")
	}

	if noise.Delay != nil {
		NConfig.DelayMin = uint64(noise.Delay.From)
		NConfig.DelayMax = uint64(noise.Delay.To)
	}
	switch strings.ToLower(noise.ApplyTo) {
	case "", "ip", "all":
		NConfig.ApplyTo = "ip"
	case "ipv4":
		NConfig.ApplyTo = "ipv4"
	case "ipv6":
		NConfig.ApplyTo = "ipv6"
	default:

View on GitHub (pinned to 7d214f8b09)

Solutions

  1. Use clean base64 (standard or URL-safe; '=' padding is stripped automatically), e.g. "aGVsbG8".
  2. Trim whitespace/newlines before embedding the value.
  3. For raw ASCII payloads use type "str" instead of base64.

Example fix

// before
{"type": "base64", "packet": "hello world!!"}

// after
{"type": "base64", "packet": "aGVsbG8="}
Defensive patterns

Strategy: validation

Validate before calling

if noise.Type == "base64" {
    s := strings.NewReplacer("+", "-", "/", "_", "=", "").Replace(strings.TrimSpace(noise.Packet))
    if _, err := base64.RawURLEncoding.DecodeString(s); err != nil {
        return fmt.Errorf("noise packet is not valid base64: %w", err)
    }
}

Prevention

When it happens

Trigger: {"type": "base64", "packet": "aGVsbG8="} fails only if padding handling leaves an invalid length; in practice triggers on non-base64 characters or whitespace, e.g. "hello world!!" or truncated input. Mixed standard/URL-safe alphabets are tolerated by the replacer.

Common situations: Pasting text with spaces or newlines; truncated base64 from manual copying; assuming arbitrary strings are valid base64.

Related errors


AI-assisted analysis of XTLS/Xray-core@7d214f8b09 (2026-08-15). Data as JSON: /api/errors/3316ad73b36bd0b6. Report an issue: GitHub.