{"record":{"id":"e7bdb73510de8588","repo":"jackwener/OpenCLI","slug":"linkedin-cookie-lookup-returned-malformed-payload-e7bdb7","errorCode":null,"errorMessage":"LinkedIn cookie lookup returned malformed payload","messagePattern":"LinkedIn cookie lookup returned malformed payload","errorType":"exception","errorClass":"CommandExecutionError","httpStatus":null,"severity":"error","filePath":"clis/linkedin/shared.js","lineNumber":128,"sourceCode":"\nexport function parseLimit(value, fallback, max) {\n  if (value === undefined || value === null || value === '') return fallback;\n  const parsed = Number(value);\n  if (!Number.isInteger(parsed) || parsed < 1 || parsed > max) {\n    throw new ArgumentError(`--limit must be an integer between 1 and ${max}`);\n  }\n  return parsed;\n}\n\nexport async function requireLinkedInCookie(page, context) {\n  let cookies;\n  try {\n    cookies = await page.getCookies({ url: 'https://www.linkedin.com' });\n  } catch (error) {\n    throw new CommandExecutionError(`LinkedIn cookie lookup failed: ${error?.message || error}`);\n  }\n  if (!Array.isArray(cookies)) {\n    throw new CommandExecutionError('LinkedIn cookie lookup returned malformed payload');\n  }\n  const jsession = cookies.find((c) => c.name === 'JSESSIONID')?.value;\n  if (!jsession) {\n    throw new AuthRequiredError(LINKEDIN_DOMAIN, `${context} requires an active signed-in LinkedIn browser session.`);\n  }\n  return jsession.replace(/^\"|\"$/g, '');\n}\n\nexport function buildAuthProbeScript() {\n  return String.raw`(() => {\n    const text = [\n      window.location.href || '',\n      document.title || '',\n      document.body ? (document.body.innerText || '').slice(0, 4000) : '',\n    ].join('\\n');\n    return /linkedin\\.com\\/(?:login|checkpoint|authwall|uas)/i.test(text)\n      || /\\b(sign in|log in|join linkedin|captcha|verification required)\\b/i.test(text)\n      || /(请登录|登录领英|安全验证)/.test(text);","sourceCodeStart":110,"sourceCodeEnd":146,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/linkedin/shared.js#L110-L146","documentation":"After fetching cookies, requireLinkedInCookie asserts the result is an Array. If page.getCookies resolves to something other than an array (null, object, undefined), it throws CommandExecutionError indicating a malformed payload. This is a defensive check against unexpected driver/protocol behavior.","triggerScenarios":"The automation driver returns null or an object instead of an array from getCookies — e.g. a mocked/stubbed page in tests, a custom CDP client with an unexpected response shape, or an incompatible driver version.","commonSituations":"Unit tests injecting fake page objects whose getCookies returns objects; wrapper libraries that reshape the cookies response; partial protocol responses on flaky connections.","solutions":["Ensure the page object's getCookies returns a real array of cookie objects.","If using a mock in tests, make it return `[]` at minimum instead of null/undefined.","Update or fix the driver/client so the cookies response matches the documented shape.","Log the actual value returned by getCookies to identify what is reshaping it."],"exampleFix":"// before\nconst fakePage = { getCookies: async () => ({ cookies: [] }) };\nawait csrf({ page: fakePage });\n// after\nconst fakePage = { getCookies: async () => [] };\nawait csrf({ page: fakePage });","handlingStrategy":"type-guard","validationCode":"const cookies = await page.getCookies({ url: 'https://www.linkedin.com' });\nif (!Array.isArray(cookies)) throw new Error('Driver returned non-array cookies; check driver version or mocks');","typeGuard":"const isCookieArray = (v) => Array.isArray(v) && v.every((c) => typeof c?.name === 'string');","tryCatchPattern":"try {\n  const token = await requireLinkedInCookie(page, 'csrf');\n} catch (e) {\n  if (e instanceof CommandExecutionError && e.message.includes('malformed payload')) {\n    console.error('getCookies returned a non-array; inspect your page driver/mocks.');\n  } else throw e;\n}","preventionTips":["Return plain arrays from any mocked getCookies in tests.","Keep the automation driver at a version whose getCookies resolves to Cookie[].","Wrap exotic CDP clients so responses are normalized to arrays.","Log the typeof/shape of getCookies output when debugging."],"tags":["browser-automation","malformed-response","cookie-lookup","linkedin"],"backgroundTag":"unexpected-response-shape","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}