{"record":{"id":"6780620255566aa0","repo":"nats-io/nats-server","slug":"s-parser-error-state-d-i-d-proto-s","errorCode":null,"errorMessage":"%s parser ERROR, state=%d, i=%d: proto='%s...'","messagePattern":"(.+?) parser ERROR, state=(.+?), i=(.+?): proto='(.+?)\\.\\.\\.'","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"server/parser.go","lineNumber":1255,"sourceCode":"\t\t\t}\n\t\t\tc.msgBuf = make([]byte, lrem, c.pa.size+LEN_CR_LF)\n\t\t\tcopy(c.msgBuf, buf[c.as:])\n\t\t} else {\n\t\t\tc.msgBuf = c.scratch[len(c.argBuf):len(c.argBuf)]\n\t\t\tc.msgBuf = append(c.msgBuf, (buf[c.as:])...)\n\t\t}\n\t}\n\n\treturn nil\n\nauthErr:\n\tc.authViolation()\n\treturn ErrAuthentication\n\nparseErr:\n\tc.sendErr(\"Unknown Protocol Operation\")\n\tsnip := protoSnippet(i, PROTO_SNIPPET_SIZE, buf)\n\terr := fmt.Errorf(\"%s parser ERROR, state=%d, i=%d: proto='%s...'\", c.kindString(), c.state, i, snip)\n\treturn err\n}\n\nfunc protoSnippet(start, max int, buf []byte) string {\n\tstop := start + max\n\tbufSize := len(buf)\n\tif start >= bufSize {\n\t\treturn `\"\"`\n\t}\n\tif stop > bufSize {\n\t\tstop = bufSize - 1\n\t}\n\treturn fmt.Sprintf(\"%q\", buf[start:stop])\n}\n\n// Check if the length of buffer `arg` is over the max control line limit `mcl`.\n// If so, an error is sent to the client and the connection is closed.\n// The error ErrMaxControlLine is returned.","sourceCodeStart":1237,"sourceCodeEnd":1273,"githubUrl":"https://github.com/nats-io/nats-server/blob/3a66a489d262bf89b71a71c955c94920394532f3/server/parser.go#L1237-L1273","documentation":"The server's protocol parser hit an unknown protocol operation while in a given lexer/parser state. It builds an error with the connection kind, parser state, byte offset, and a snippet of the offending proto text via errorf(). Primarily exercised by parser tests and conf/fuzz.go's Fuzz entry, which feeds random data to Parse and expects errors like this.","triggerScenarios":"A client (or fuzz corpus) sends bytes that do not match any known NATS protocol operation, causing the parser's parseErr path in server/parser.go.","commonSituations":"Non-NATS clients pointing at a NATS port, TLS traffic sent to a plaintext port (binary garbage), corrupted/framed payloads, or fuzz testing with malformed input.","solutions":["Ensure clients use a NATS client library speaking the NATS protocol.","Check for TLS/plaintext mismatch: enable TLS on the server side or connect with tls:// to plain ports correctly.","Inspect the logged proto snippet to identify the sending client and the garbage bytes.","If seen in fuzzing, minimize the input with the corpus entry that produced it."],"exampleFix":"// before: connecting with raw HTTP to NATS port\ncurl http://localhost:4222/\n// after: use a NATS client\nnc, _ := nats.Connect(\"nats://localhost:4222\")","handlingStrategy":"try-catch","validationCode":"// Before connecting a custom client, ensure the first bytes are a known NATS op\nif !regexp.MustCompile(`^(CONNECT|PUB|SUB|UNSUB|MSG|PING|PONG|INFO|PLUS|ERR)\\b`).Match(buf) {\n    return errors.New(\"not a NATS protocol operation\")\n}","typeGuard":"func isKnownProtoOp(b []byte) bool {\n    for _, op := range []string{\"CONNECT \", \"PUB \", \"SUB \", \"UNSUB \", \"PING\", \"PONG\", \"INFO \", \"MSG \", \"+OK\", \"-ERR\"} {\n        if bytes.HasPrefix(b, []byte(op)) { return true }\n    }\n    return false\n}","tryCatchPattern":"if _, err := nats.Parse(string(data)); err != nil {\n    // parse errors on garbage input are expected; log snippet and drop connection\n    log.Printf(\"dropping client %s: %v\", conn.RemoteAddr(), err)\n    return\n}","preventionTips":["Use official NATS client libraries, not hand-rolled protocol writers.","Match TLS settings: don't send TLS to plaintext ports or vice versa.","Don't point HTTP/other-protocol clients at NATS ports."],"tags":["protocol","parser","fuzzing"],"backgroundTag":"unknown-protocol-operation","analyzedSha":"3a66a489d262bf89b71a71c955c94920394532f3","analyzedAt":"2026-09-02T04:41:54.247Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}