{"record":{"id":"34ea7bc01d954b31","repo":"XTLS/Xray-core","slug":"invalid-port-range-s","errorCode":null,"errorMessage":"invalid port range: {s}","messagePattern":"invalid port range: (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"common/net/port.go","lineNumber":33,"sourceCode":"func PortFromBytes(port []byte) Port {\n\treturn Port(binary.BigEndian.Uint16(port))\n}\n\n// PortFromInt converts an integer to a Port.\n// @error when the integer is not positive or larger then 65535\nfunc PortFromInt(val uint32) (Port, error) {\n\tif val > 65535 {\n\t\treturn Port(0), errors.New(\"invalid port range: \", val)\n\t}\n\treturn Port(val), nil\n}\n\n// PortFromString converts a string to a Port.\n// @error when the string is not an integer or the integral value is a not a valid Port.\nfunc PortFromString(s string) (Port, error) {\n\tval, err := strconv.ParseUint(s, 10, 32)\n\tif err != nil {\n\t\treturn Port(0), errors.New(\"invalid port range: \", s)\n\t}\n\treturn PortFromInt(uint32(val))\n}\n\n// Value return the corresponding uint16 value of a Port.\nfunc (p Port) Value() uint16 {\n\treturn uint16(p)\n}\n\n// String returns the string presentation of a Port.\nfunc (p Port) String() string {\n\treturn strconv.Itoa(int(p))\n}\n\n// FromPort returns the beginning port of this PortRange.\nfunc (p *PortRange) FromPort() Port {\n\treturn Port(p.From)\n}","sourceCodeStart":15,"sourceCodeEnd":51,"githubUrl":"https://github.com/XTLS/Xray-core/blob/7d214f8b094f75322fa3990f8aadad1c912f24f5/common/net/port.go#L15-L51","documentation":"Returned by net.PortFromString when the string is not a parseable base-10 integer that fits uint32, i.e. strconv.ParseUint fails. It is the string-entry counterpart of the port-range check and fires before PortFromInt is even reached.","triggerScenarios":"Calling PortFromString(s) with non-numeric input ('http', '80/tcp', empty string, whitespace, negative sign, or values above 4294967295). Values between 65536 and 4294967295 parse fine here and are then rejected by PortFromInt (error 204).","commonSituations":"Config files where a service name was written instead of a numeric port, trailing spaces or quotes in YAML/JSON values, or environment variables that are empty by default.","solutions":["Use a pure numeric port string ('443', not 'https' or '443 ')","Trim whitespace and reject empty values before calling PortFromString","Add JSON-schema/config validation for port fields at load time"],"exampleFix":"// before\nport, err := net.PortFromString(rawCfg.Port) // \"443/tcp\"\n\n// after\nport, err := net.PortFromString(strings.TrimSpace(rawCfg.Port))\nif err != nil { return fmt.Errorf(\"invalid port %q in config\", rawCfg.Port) }","handlingStrategy":"validation","validationCode":"s = strings.TrimSpace(s)\nif _, err := strconv.ParseUint(s, 10, 16); err != nil {\n    return fmt.Errorf(\"port %q must be 0-65535\", s)\n}","typeGuard":"func isValidPortString(s string) bool { _, err := strconv.ParseUint(strings.TrimSpace(s), 10, 16); return err == nil }","tryCatchPattern":null,"preventionTips":["Use numeric ports in configs, never service names","Trim and reject empty strings before parsing"],"tags":["validation","port","config","parsing"],"backgroundTag":null,"analyzedSha":"7d214f8b094f75322fa3990f8aadad1c912f24f5","analyzedAt":"2026-08-15T14:26:24.325Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}