Skip to content

[Bug] getResponseBody returns raw bytes without proper base64 encoding for binary content #1903

@Financier-Nuri

Description

@Financier-Nuri

Summary

The CDP method Network.getResponseBody returns raw bytes directly in the response body, but the CDP protocol specification requires binary content to be base64 encoded when base64Encoded is false or for non-text responses.

Steps to Reproduce

  1. Start Lightpanda browser
  2. Navigate to a page that returns binary content (e.g., an image)
  3. Use CDP to call Network.getResponseBody on the image request
  4. Observe that raw bytes are returned instead of base64 encoded content

Expected Behavior

According to CDP protocol spec, Network.getResponseBody should:

  • Return base64 encoded content when base64Encoded: true
  • Return string content for text responses
  • Handle binary content appropriately

Actual Behavior

In src/cdp/domains/network.zig:204-215, the function returns raw bytes:

try cmd.sendResult(.{
    .body = buf.items,
    .base64Encoded = false,
}, .{});

Root Cause Analysis

Location: src/cdp/domains/network.zig:204-215

The code directly returns buf.items (raw bytes) with base64Encoded: false. This violates the CDP specification when:

  1. The response is binary (image, video, etc.)
  2. The content contains non-UTF8 bytes

The CDP protocol states that for binary content, the server should either:

  • Return base64 encoded string with base64Encoded: true
  • Or ensure the content is valid UTF-8 text

Additionally, there's no Content-Type check to determine if the response is binary or text.

Proposed Solution

  1. Check the Content-Type header to determine if response is binary
  2. If binary, encode to base64 and set base64Encoded: true
  3. If text, ensure proper UTF-8 encoding before returning
  4. Add MIME type checking (images, videos, etc. should always be base64)

Code Reference

fn getResponseBody(cmd: anytype) !void {
    // ... existing code ...
    
    // Add MIME type check
    const mime = rh.contentType() orelse "application/octet-stream";
    const is_binary = isBinaryMimeType(mime);
    
    if (is_binary) {
        // Base64 encode the binary content
        const encoded = try base64Encode(buf.items, arena);
        return cmd.sendResult(.{ .body = encoded, .base64Encoded = true }, .{});
    }
    
    return cmd.sendResult(.{ .body = buf.items, .base64Encoded = false }, .{});
}

Environment

  • OS: Linux
  • Version: Lightpanda latest
  • Zig: latest

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions