jackwener/OpenCLI · error · CommandExecutionError
LinkedIn cookie lookup returned malformed payload
Error message
LinkedIn cookie lookup returned malformed payload
What it means
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.
Source
Thrown at clis/linkedin/shared.js:128
export function parseLimit(value, fallback, max) {
if (value === undefined || value === null || value === '') return fallback;
const parsed = Number(value);
if (!Number.isInteger(parsed) || parsed < 1 || parsed > max) {
throw new ArgumentError(`--limit must be an integer between 1 and ${max}`);
}
return parsed;
}
export async function requireLinkedInCookie(page, context) {
let cookies;
try {
cookies = await page.getCookies({ url: 'https://www.linkedin.com' });
} catch (error) {
throw new CommandExecutionError(`LinkedIn cookie lookup failed: ${error?.message || error}`);
}
if (!Array.isArray(cookies)) {
throw new CommandExecutionError('LinkedIn cookie lookup returned malformed payload');
}
const jsession = cookies.find((c) => c.name === 'JSESSIONID')?.value;
if (!jsession) {
throw new AuthRequiredError(LINKEDIN_DOMAIN, `${context} requires an active signed-in LinkedIn browser session.`);
}
return jsession.replace(/^"|"$/g, '');
}
export function buildAuthProbeScript() {
return String.raw`(() => {
const text = [
window.location.href || '',
document.title || '',
document.body ? (document.body.innerText || '').slice(0, 4000) : '',
].join('\n');
return /linkedin\.com\/(?:login|checkpoint|authwall|uas)/i.test(text)
|| /\b(sign in|log in|join linkedin|captcha|verification required)\b/i.test(text)
|| /(请登录|登录领英|安全验证)/.test(text);View on GitHub (pinned to 49907e53dc)
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.
Example fix
// before
const fakePage = { getCookies: async () => ({ cookies: [] }) };
await csrf({ page: fakePage });
// after
const fakePage = { getCookies: async () => [] };
await csrf({ page: fakePage }); Defensive patterns
Strategy: type-guard
Validate before calling
const cookies = await page.getCookies({ url: 'https://www.linkedin.com' });
if (!Array.isArray(cookies)) throw new Error('Driver returned non-array cookies; check driver version or mocks'); Type guard
const isCookieArray = (v) => Array.isArray(v) && v.every((c) => typeof c?.name === 'string');
Try / catch
try {
const token = await requireLinkedInCookie(page, 'csrf');
} catch (e) {
if (e instanceof CommandExecutionError && e.message.includes('malformed payload')) {
console.error('getCookies returned a non-array; inspect your page driver/mocks.');
} else throw e;
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: Unit tests injecting fake page objects whose getCookies returns objects; wrapper libraries that reshape the cookies response; partial protocol responses on flaky connections.
Understand the failure class
- Parsing and encoding errors: unexpected token, malformed input — why parsers reject input and how to find the real culprit.
Related errors
- LinkedIn cookie lookup failed: ${error?.message || error}
- Sales Navigator messaging threads API returned malformed pay
- Sales Navigator messaging threads API returned malformed thr
- ${label} returned an unexpected response
- ${label} returned malformed response
AI-assisted analysis of jackwener/OpenCLI@49907e53dc (2026-08-29).
Data as JSON: /api/errors/e7bdb73510de8588.
Report an issue: GitHub.