{"record":{"id":"bb3a7d0510d073c9","repo":"gorilla/websocket","slug":"websocket-invalid-compression-level","errorCode":null,"errorMessage":"websocket: invalid compression level","messagePattern":"websocket: invalid compression level","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"conn.go","lineNumber":1227,"sourceCode":"// Deprecated: Use the NetConn method.\nfunc (c *Conn) UnderlyingConn() net.Conn {\n\treturn c.conn\n}\n\n// EnableWriteCompression enables and disables write compression of\n// subsequent text and binary messages. This function is a noop if\n// compression was not negotiated with the peer.\nfunc (c *Conn) EnableWriteCompression(enable bool) {\n\tc.enableWriteCompression = enable\n}\n\n// SetCompressionLevel sets the flate compression level for subsequent text and\n// binary messages. This function is a noop if compression was not negotiated\n// with the peer. See the compress/flate package for a description of\n// compression levels.\nfunc (c *Conn) SetCompressionLevel(level int) error {\n\tif !isValidCompressionLevel(level) {\n\t\treturn errors.New(\"websocket: invalid compression level\")\n\t}\n\tc.compressionLevel = level\n\treturn nil\n}\n\n// FormatCloseMessage formats closeCode and text as a WebSocket close message.\n// An empty message is returned for code CloseNoStatusReceived.\nfunc FormatCloseMessage(closeCode int, text string) []byte {\n\tif closeCode == CloseNoStatusReceived {\n\t\t// Return empty message because it's illegal to send\n\t\t// CloseNoStatusReceived. Return non-nil value in case application\n\t\t// checks for nil.\n\t\treturn []byte{}\n\t}\n\tbuf := make([]byte, 2+len(text))\n\tbinary.BigEndian.PutUint16(buf, uint16(closeCode))\n\tcopy(buf[2:], text)\n\treturn buf","sourceCodeStart":1209,"sourceCodeEnd":1245,"githubUrl":"https://github.com/gorilla/websocket/blob/e064f32e3674d9d79a8fd417b5bc06fa5c6cad8f/conn.go#L1209-L1245","documentation":"SetCompressionLevel validates the flate compression level (must be between flate.BestSpeed=1 and flate.BestCompression=9, per isValidCompressionLevel) and returns this error when the given level is out of range. The connection's compression level is left unchanged.","triggerScenarios":"Calling Conn.SetCompressionLevel with a value outside 1-9 (e.g. 0, 10, or flate.NoCompression=0, which is rejected).","commonSituations":"Passing flate.NoCompression (0) or flate.HuffmanOnly (-2) assuming all compress/flate constants are valid; off-by-one level values from configuration.","solutions":["Pass a level between flate.BestSpeed (1) and flate.BestCompression (9)","Use flate.DefaultCompression (-1) if you want the default, which resolves to a valid internal level","Clamp user/config-supplied compression levels before calling SetCompressionLevel","Remember the call is a no-op if compression was never negotiated via the subprotocol handshake"],"exampleFix":"// before\nc.SetCompressionLevel(flate.NoCompression) // 0 -> error\n// after\nc.SetCompressionLevel(flate.BestSpeed) // 1-9 accepted","handlingStrategy":"validation","validationCode":"func validCompressionLevel(level int) bool {\n    return level >= flate.BestSpeed && level <= flate.BestCompression // 1..9\n}\nif validCompressionLevel(level) {\n    conn.SetCompressionLevel(level)\n}","typeGuard":"func isValidLevel(l int) bool { return l >= 1 && l <= 9 }","tryCatchPattern":null,"preventionTips":["Clamp config-supplied levels to 1-9 before calling","Remember flate.NoCompression (0) is rejected here","Compression must be negotiated in the handshake or the call is a no-op"],"tags":["websocket","compression","validation"],"backgroundTag":"invalid-parameter-value","analyzedSha":"e064f32e3674d9d79a8fd417b5bc06fa5c6cad8f","analyzedAt":"2026-08-31T12:40:58.222Z","schemaVersion":2},"datasetVersion":"2026-08-31T14:17:45.589Z"}