{"record":{"id":"66ebe19535b7a482","repo":"XTLS/Xray-core","slug":"insufficient-length-of-packet","errorCode":null,"errorMessage":"insufficient length of packet.","messagePattern":"insufficient length of packet\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"proxy/socks/protocol.go","lineNumber":346,"sourceCode":"\n\treturn buf.WriteAllBytes(writer, buffer.Bytes(), nil)\n}\n\nfunc writeSocks4Response(writer io.Writer, errCode byte, address net.Address, port net.Port) error {\n\tbuffer := buf.StackNew()\n\tdefer buffer.Release()\n\n\tcommon.Must(buffer.WriteByte(0x00))\n\tcommon.Must(buffer.WriteByte(errCode))\n\tportBytes := buffer.Extend(2)\n\tbinary.BigEndian.PutUint16(portBytes, port.Value())\n\tcommon.Must2(buffer.Write(address.IP()))\n\treturn buf.WriteAllBytes(writer, buffer.Bytes(), nil)\n}\n\nfunc DecodeUDPPacket(packet *buf.Buffer) (*protocol.RequestHeader, error) {\n\tif packet.Len() < 5 {\n\t\treturn nil, errors.New(\"insufficient length of packet.\")\n\t}\n\trequest := &protocol.RequestHeader{\n\t\tVersion: socks5Version,\n\t\tCommand: protocol.RequestCommandUDP,\n\t}\n\n\t// packet[0] and packet[1] are reserved\n\tif packet.Byte(2) != 0 /* fragments */ {\n\t\treturn nil, errors.New(\"discarding fragmented payload.\")\n\t}\n\n\tpacket.Advance(3)\n\n\taddr, port, err := addrParser.ReadAddressPort(nil, packet)\n\tif err != nil {\n\t\treturn nil, errors.New(\"failed to read UDP header\").Base(err)\n\t}\n\trequest.Address = addr","sourceCodeStart":328,"sourceCodeEnd":364,"githubUrl":"https://github.com/XTLS/Xray-core/blob/7d214f8b094f75322fa3990f8aadad1c912f24f5/proxy/socks/protocol.go#L328-L364","documentation":"Thrown by DecodeUDPPacket when decoding a SOCKS5 UDP datagram whose total length is less than 5 bytes. A valid SOCKS5 UDP header needs at minimum the 2 reserved bytes, 1 fragment byte, plus at least 2 bytes for address/port encoding. The library rejects anything shorter because the header cannot possibly be parsed.","triggerScenarios":"Calling DecodeUDPPacket on a *buf.Buffer whose Len() < 5: e.g. an empty UDP datagram, a 1-4 byte keepalive/health-check probe, or a truncated packet delivered by handleUDPPayload on the SOCKS inbound UDP path.","commonSituations":"Port scanners or monitoring systems sending tiny UDP probes to the SOCKS UDP port; a client with a broken/fragmented UDP encoder; MTU-truncation on the path between client and inbound.","solutions":["If you control the client, verify it prepends the 10+ byte SOCKS5 UDP header (RSV/FRAG + ATYP + addr + port) before payload.","Inspect the raw datagram length before decoding and ignore sub-5-byte datagrams as noise.","Check for path truncation (VPN/QUIC/UDP-relay MTU issues) if legitimate traffic keeps failing.","Grep logs for the source address of these packets — most are scanners, safe to drop."],"exampleFix":"// before\nheader, err := DecodeUDPPacket(packet)\nif err != nil { return err }\n\n// after\nif packet.Len() < 5 {\n\t// ignore tiny/empty datagrams (scanners, keepalives)\n\tpacket.Release()\n\tcontinue\n}\nheader, err := DecodeUDPPacket(packet)","handlingStrategy":"validation","validationCode":"if packet.Len() < 5 {\n\t// too small to contain RSV+FRAG+ATYP+port; drop it\n\tpacket.Release()\n\tcontinue\n}","typeGuard":null,"tryCatchPattern":"if _, err := socks.DecodeUDPPacket(packet); err != nil {\n\tif strings.HasPrefix(err.Error(), \"insufficient length\") {\n\t\tcontinue // noise, not fatal\n\t}\n\treturn err\n}","preventionTips":["Length-check UDP datagrams before protocol decoding.","Firewall or ignore sub-header-length UDP probes on exposed inbounds.","Keep client SOCKS5 UDP encoders spec-compliant."],"tags":["socks","socks5","udp","protocol","packet-validation"],"backgroundTag":null,"analyzedSha":"7d214f8b094f75322fa3990f8aadad1c912f24f5","analyzedAt":"2026-08-15T14:26:24.325Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}