{"record":{"id":"64b141e29886115c","repo":"nats-io/nats-server","slug":"processheaderpub-parse-error-q","errorCode":null,"errorMessage":"processHeaderPub Parse Error: %q","messagePattern":"processHeaderPub Parse Error: %q","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"server/client.go","lineNumber":2919,"sourceCode":"\n\tc.pa.arg = arg\n\tswitch len(args) {\n\tcase 3:\n\t\tc.pa.subject = args[0]\n\t\tc.pa.reply = nil\n\t\tc.pa.hdr = parseSize(args[1])\n\t\tc.pa.size = parseSize(args[2])\n\t\tc.pa.hdb = args[1]\n\t\tc.pa.szb = args[2]\n\tcase 4:\n\t\tc.pa.subject = args[0]\n\t\tc.pa.reply = args[1]\n\t\tc.pa.hdr = parseSize(args[2])\n\t\tc.pa.size = parseSize(args[3])\n\t\tc.pa.hdb = args[2]\n\t\tc.pa.szb = args[3]\n\tdefault:\n\t\treturn fmt.Errorf(\"processHeaderPub Parse Error: %q\", arg)\n\t}\n\tif c.pa.hdr < 0 {\n\t\treturn fmt.Errorf(\"processHeaderPub Bad or Missing Header Size: %q\", arg)\n\t}\n\t// If number overruns an int64, parseSize() will have returned a negative value\n\tif c.pa.size < 0 {\n\t\treturn fmt.Errorf(\"processHeaderPub Bad or Missing Total Size: %q\", arg)\n\t}\n\tif c.pa.hdr > c.pa.size {\n\t\treturn fmt.Errorf(\"processHeaderPub Header Size larger then TotalSize: %q\", arg)\n\t}\n\tmaxPayload := atomic.LoadInt32(&c.mpay)\n\t// Use int64() to avoid int32 overrun...\n\tif maxPayload != jwt.NoLimit && int64(c.pa.size) > int64(maxPayload) {\n\t\t// If we are given the remaining read buffer (since we do blind reads\n\t\t// we may have the beginning of the message header/payload), we will\n\t\t// look for the tracing header and if found, we will generate a\n\t\t// trace event with the max payload ingress error.","sourceCodeStart":2901,"sourceCodeEnd":2937,"githubUrl":"https://github.com/nats-io/nats-server/blob/3a66a489d262bf89b71a71c955c94920394532f3/server/client.go#L2901-L2937","documentation":"The server failed to parse a HPUB protocol message: after the opcode, exactly four arguments (subject, reply, header-size, body-size) are expected, and the received argument count did not match, so the header-publish line is malformed. The server returns this error and will close the connection, since the protocol stream is now ambiguous.","triggerScenarios":"Sending a 'HPUB' line to the server whose token count != 4 (e.g. missing header/size fields, an empty reply token with a stray space creating wrong arg count, or trailing/extra tokens); typically from a hand-rolled or buggy client implementation.","commonSituations":"Custom NATS clients implementing headers incorrectly (HPUB <subject> <reply> <hdr> <size> vs the no-reply form HPUB <subject> <hdr> <size> mishandled); newline/CRLF handling bugs that merge two protocol lines; fuzzing or test tooling sending truncated HPUB lines.","solutions":["Send the correct HPUB form: with reply 'HPUB <subject> <reply> <hdr_size> <total_size>'; without reply 'HPUB <subject> <hdr_size> <total_size>' (still 4 tokens after opcode handling — verify against the protocol spec your server version implements).","Use an official NATS client library instead of hand-crafting protocol frames.","Check that sizes are decimal integers and the CRLF-terminated line carries exactly the expected fields, with the payload of exactly hdr+size bytes following.","Capture the raw bytes on the wire (tcpdump/Wireshark) to see the actual malformed line and correct the producer."],"exampleFix":"// before\nconn.Write([]byte(\"HPUB foo 10\\r\\nNATS/1.0\\r\\nX: 1\\r\\n\\r\\nbody\")) // missing size arg\n// after\nconn.Write([]byte(\"HPUB foo 10 14\\r\\nNATS/1.0\\r\\nX: 1\\r\\n\\r\\nbody\"))","handlingStrategy":"try-catch","validationCode":"func validateHPUB(subject, reply string, hdr, size int) error {\n    if subject == \"\" || hdr < 0 || size < 0 { return fmt.Errorf(\"bad HPUB args\") }\n    return nil\n}","typeGuard":null,"tryCatchPattern":"if err := conn.Flush(); err != nil {\n    if strings.Contains(err.Error(), \"processHeaderPub Parse Error\") {\n        // malformed HPUB: reconnect with a compliant client library\n        log.Printf(\"HPUB frame malformed, rebuilding protocol writer: %v\", err)\n    }\n}","preventionTips":["Use an official NATS client library; do not hand-write HPUB frames.\n","Verify header/total sizes count bytes of header-block+payload exactly.\n","Always terminate protocol lines with CRLF.\n","Test header publishing against a local server before production."],"tags":["nats","protocol","hpub","headers"],"backgroundTag":"nats-protocol-parse-error","analyzedSha":"3a66a489d262bf89b71a71c955c94920394532f3","analyzedAt":"2026-09-02T04:41:54.247Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}