{"id":"09e75ef415ffc20c","repo":"websockets/ws","slug":"unexpected-end-of-input","errorCode":null,"errorMessage":"Unexpected end of input","messagePattern":"Unexpected end of input","errorType":"exception","errorClass":"SyntaxError","httpStatus":null,"severity":"error","filePath":"lib/extension.js","lineNumber":151,"sourceCode":"          mustUnescape = false;\n        }\n        push(params, paramName, value);\n        if (code === 0x2c) {\n          push(offers, extensionName, params);\n          params = Object.create(null);\n          extensionName = undefined;\n        }\n\n        paramName = undefined;\n        start = end = -1;\n      } else {\n        throw new SyntaxError(`Unexpected character at index ${i}`);\n      }\n    }\n  }\n\n  if (start === -1 || inQuotes || code === 0x20 || code === 0x09) {\n    throw new SyntaxError('Unexpected end of input');\n  }\n\n  if (end === -1) end = i;\n  const token = header.slice(start, end);\n  if (extensionName === undefined) {\n    push(offers, token, params);\n  } else {\n    if (paramName === undefined) {\n      push(params, token, true);\n    } else if (mustUnescape) {\n      push(params, paramName, token.replace(/\\\\/g, ''));\n    } else {\n      push(params, paramName, token);\n    }\n    push(offers, extensionName, params);\n  }\n\n  return offers;","sourceCodeStart":133,"sourceCodeEnd":169,"githubUrl":"https://github.com/websockets/ws/blob/ae1de54330cef77e487548890fabfeb9aae1d83d/lib/extension.js#L133-L169","documentation":"Thrown by extension.parse() at end-of-input (extension.js:150-151) when the final state is invalid: either start is -1 (the header ended right after a separator with no trailing token), inQuotes is still true (an unclosed double-quoted string), or the last character was whitespace (trailing space/tab). All three indicate the header is truncated or incomplete per RFC 6455 §9.1.","triggerScenarios":"extension.parse() is called with a header that ends prematurely: 'permessage-deflate;' (trailing separator, start is -1 at end), 'permessage-deflate; x=\"abc' (unclosed quote, inQuotes is true), or 'permessage-deflate ' (trailing whitespace, last char was space).","commonSituations":"A truncated Sec-WebSocket-Extensions header from a network read that cut off mid-value. A developer's string-building code forgets to append the closing quote or final token. A header value that was trimmed or corrupted by a proxy.","solutions":["Check whether the header ends with a separator (';'/',') and remove the trailing separator, or append the missing token.","If a quoted value is unterminated, add the closing '\"'.","Remove trailing whitespace from the header value.","If the truncation comes from the network/peer, investigate packet fragmentation or proxy modification."],"exampleFix":"// before\nconst offers = WebSocket.extension.parse('permessage-deflate; x=\"abc');\n\n// after — close the quoted string\nconst offers = WebSocket.extension.parse('permessage-deflate; x=\"abc\"');","handlingStrategy":"try-catch","validationCode":"// Reject truncated headers: trailing separators, trailing whitespace, unclosed quotes\nfunction isCompleteHeader(header) {\n  const trimmed = header.trim();\n  if (/[;,]$/.test(trimmed)) return false;\n  if (trimmed.endsWith(' ') || trimmed.endsWith('\\t')) return false;\n  const quoteCount = (trimmed.match(/\"/g) || []).length;\n  if (quoteCount % 2 !== 0) return false;\n  return true;\n}\n\nif (isCompleteHeader(header)) {\n  const offers = WebSocket.extension.parse(header);\n}","typeGuard":null,"tryCatchPattern":"try {\n  const offers = WebSocket.extension.parse(header);\n} catch (err) {\n  if (err instanceof SyntaxError && err.message === 'Unexpected end of input') {\n    console.warn('Truncated extensions header:', header);\n    return;\n  }\n  throw err;\n}","preventionTips":["Trim trailing whitespace and separators from header values before parsing.","Ensure all quoted strings are closed with a matching '\"'.","Avoid constructing headers by concatenating fragments that may be empty.","Catch the 'Unexpected end of input' SyntaxError specifically for truncated headers."],"tags":["websocket","extension","handshake","header-parsing","rfc6455"],"analyzedSha":"ae1de54330cef77e487548890fabfeb9aae1d83d","analyzedAt":"2026-08-03T19:11:18.437Z","schemaVersion":2}