{"record":{"id":"edacc26e4f9be470","repo":"BigPizzaV3/CodexPlusPlus","slug":"page-capturescreenshot-returned-invalid-png-data","errorCode":null,"errorMessage":"Page.captureScreenshot returned invalid PNG data","messagePattern":"Page\\.captureScreenshot returned invalid PNG data","errorType":"exception","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"crates/codex-plus-core/src/bridge.rs","lineNumber":211,"sourceCode":"    output_path: &Path,\n) -> anyhow::Result<u64> {\n    let response = send_cdp_command(\n        websocket_url,\n        \"Page.captureScreenshot\",\n        capture_screenshot_params(),\n    )\n    .await?;\n    let encoded = response\n        .get(\"result\")\n        .and_then(|result| result.get(\"data\"))\n        .and_then(Value::as_str)\n        .filter(|data| !data.is_empty())\n        .ok_or_else(|| anyhow::anyhow!(\"Page.captureScreenshot returned no image data\"))?;\n    let bytes = base64::engine::general_purpose::STANDARD\n        .decode(encoded)\n        .context(\"failed to decode screenshot PNG\")?;\n    if !bytes.starts_with(&[137, 80, 78, 71, 13, 10, 26, 10]) {\n        bail!(\"Page.captureScreenshot returned invalid PNG data\");\n    }\n    crate::settings::atomic_write(output_path, &bytes)\n        .with_context(|| format!(\"failed to save screenshot {}\", output_path.display()))?;\n    Ok(bytes.len() as u64)\n}\n\npub async fn run_periodic_evaluations<F>(\n    websocket_url: &str,\n    period: Duration,\n    mut next_expression: F,\n) -> anyhow::Result<()>\nwhere\n    F: FnMut() -> anyhow::Result<Option<String>>,\n{\n    let socket = connect_cdp_websocket(websocket_url).await?;\n    let mut session = CdpSession::new(socket);\n    let mut interval = tokio::time::interval(period);\n    loop {","sourceCodeStart":193,"sourceCodeEnd":229,"githubUrl":"https://github.com/BigPizzaV3/CodexPlusPlus/blob/b1ed92e5e4a2d74095d4b8db5af43cef7acba9c6/crates/codex-plus-core/src/bridge.rs#L193-L229","documentation":"After Page.captureScreenshot succeeds, the bridge base64-decodes result.data and verifies the decoded bytes start with the 8-byte PNG signature (\\x89PNG\\r\\n\\x1a\\n). If the decoded payload does not begin with the magic bytes, the bridge assumes the browser returned corrupt or non-PNG data and bails with this error, refusing to write a broken file to output_path.","triggerScenarios":"A CDP endpoint returns captureScreenshot data that decodes but is not PNG — e.g. an older/nonstandard browser, a remote-debugging proxy re-encoding the image, or mocked/intercepted responses in embedded/patched Chromium builds.","commonSituations":"Pointing the bridge at a non-Chromium CDP implementation that ignores the format:'png' parameter; a middlebox or instrumentation layer altering the response payload; memory pressure in the target causing truncated frames.","solutions":["Verify the target browser is Chromium-based and honors Page.captureScreenshot format:'png'","Retry the capture — transient renderer issues can yield bad frames","Capture the decoded payload prefix and inspect it (e.g. is it JPEG \\xFF\\xD8? then the format param is being ignored)","Remove any proxy/instrumentation between the bridge and the CDP websocket and retry"],"exampleFix":"// before\nlet bytes = base64::engine::general_purpose::STANDARD.decode(encoded)?;\n// after (diagnose unexpected format)\nlet bytes = ...decode...;\nassert!(bytes.starts_with(&[137,80,78,71,13,10,26,10]), \"first bytes: {:02x?}\", &bytes[..8.min(bytes.len())]);","handlingStrategy":"validation","validationCode":"const PNG_MAGIC: [u8; 8] = [0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A];\n// after decoding the base64 payload yourself:\nif !bytes.starts_with(&PNG_MAGIC) { eprintln!(\"non-PNG payload, first bytes: {:02x?}\", &bytes[..bytes.len().min(8)]); }","typeGuard":"fn is_png(bytes: &[u8]) -> bool {\n    bytes.starts_with(&[137, 80, 78, 71, 13, 10, 26, 10])\n}","tryCatchPattern":"match capture_page_screenshot(&ws, &path).await {\n    Ok(len) => println!(\"saved {len} bytes\"),\n    Err(e) if e.to_string().contains(\"invalid PNG data\") => {\n        eprintln!(\"target returned non-PNG data; check browser/format support\");\n    }\n    Err(e) => return Err(e),\n}","preventionTips":["Verify the CDP target is a Chromium build honoring format:'png'","Strip proxies/instrumentation that could re-encode capture responses","Validate the PNG magic before writing files to avoid corrupt artifacts"],"tags":["cdp","screenshot","png","devtools"],"backgroundTag":"unexpected-api-response-shape","analyzedSha":"b1ed92e5e4a2d74095d4b8db5af43cef7acba9c6","analyzedAt":"2026-09-19T23:35:21.129Z","contentChangedAt":"2026-09-19T23:35:21.129Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}