Skip to content

Fix CookiePartitionKeyConverter to support string deserialization#3075

Merged
kblok merged 5 commits intomasterfrom
copilot/fix-cookie-partition-key-deserialization
Jan 8, 2026
Merged

Fix CookiePartitionKeyConverter to support string deserialization#3075
kblok merged 5 commits intomasterfrom
copilot/fix-cookie-partition-key-deserialization

Conversation

Copy link
Contributor

Copilot AI commented Jan 5, 2026

The CookiePartitionKeyConverter only handled object format from CDP, preventing round-trip serialization when users save cookies to files and reload them.

Changes

CookiePartitionKeyConverter.Read()

  • Check token type: return string directly if JsonTokenType.String, otherwise extract topLevelSite from object
  • Supports both CDP object format {"topLevelSite": "...", "hasCrossSiteAncestor": false} and string format "https://example.com"

CookiePartitionKeyConverter.Write()

  • Write as string value instead of object for simpler serialization
  • Handle null values explicitly

Tests

  • Added CookiePartitionKeyConverterTests.cs covering round-trip serialization, both input formats, null handling, and arrays

Example

// Serialize cookies to file
var cookies = await page.GetCookiesAsync();
File.WriteAllText("cookies.json", JsonSerializer.Serialize(cookies));

// Deserialize and restore (now works)
var loaded = JsonSerializer.Deserialize<CookieParam[]>(
    File.ReadAllText("cookies.json"));
await page.SetCookieAsync(loaded);

Aligns with upstream puppeteer's partitionKey?: CookiePartitionKey | string type.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • https://storage.googleapis.com/chrome-for-testing-public/138.0.7204.101/linux64/chrome-linux64.zip
    • Triggering command: /usr/share/dotnet/dotnet /usr/share/dotnet/dotnet exec --runtimeconfig /home/REDACTED/work/puppeteer-sharp/puppeteer-sharp/lib/PuppeteerSharp.Tests/bin/Release/net8.0/PuppeteerSharp.Tests.runtimeconfig.json --depsfile /home/REDACTED/work/puppeteer-sharp/puppeteer-sharp/lib/PuppeteerSharp.Tests/bin/Release/net8.0/PuppeteerSharp.Tests.deps.json /home/REDACTED/work/puppeteer-sharp/puppeteer-sharp/lib/PuppeteerSharp.Tests/bin/Release/net8.0/testhost.dll --port 40463 --endpoint 127.0.0.1:040463 --role client --parentprocessid 5724 --telemetryoptedin false xtTests.cs (http block)
    • Triggering command: /usr/share/dotnet/dotnet /usr/share/dotnet/dotnet exec --runtimeconfig /home/REDACTED/work/puppeteer-sharp/puppeteer-sharp/lib/PuppeteerSharp.Tests/bin/Release/net8.0/PuppeteerSharp.Tests.runtimeconfig.json --depsfile /home/REDACTED/work/puppeteer-sharp/puppeteer-sharp/lib/PuppeteerSharp.Tests/bin/Release/net8.0/PuppeteerSharp.Tests.deps.json /home/REDACTED/work/puppeteer-sharp/puppeteer-sharp/lib/PuppeteerSharp.Tests/bin/Release/net8.0/testhost.dll --port 36163 --endpoint 127.0.0.1:036163 --role client --parentprocessid 6440 --telemetryoptedin false Tests.cs (http block)
    • Triggering command: /usr/share/dotnet/dotnet /usr/share/dotnet/dotnet exec --runtimeconfig /home/REDACTED/work/puppeteer-sharp/puppeteer-sharp/lib/PuppeteerSharp.Tests/bin/Release/net8.0/PuppeteerSharp.Tests.runtimeconfig.json --depsfile /home/REDACTED/work/puppeteer-sharp/puppeteer-sharp/lib/PuppeteerSharp.Tests/bin/Release/net8.0/PuppeteerSharp.Tests.deps.json /home/REDACTED/work/puppeteer-sharp/puppeteer-sharp/lib/PuppeteerSharp.Tests/bin/Release/net8.0/testhost.dll --port 46605 --endpoint 127.0.0.1:046605 --role client --parentprocessid 7136 --telemetryoptedin false ests.cs (http block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

This section details on the original issue you should resolve

<issue_title>CookiePartitionKeyConverter doesn't support deserialization of string properly</issue_title>
<issue_description>### Description

The current CookiePartitionKeyConverter doesn't currently support deserialization if the key is already a string. Essentially, trying to serialize these back out to a file, and then read back in again doesn't currently work, because the current writer writes as a string, and the reader will attempt to read an object. This currently works in puppeteer, because it's expecting an object from CDP, but it should still be able to deserialize if the cookies are offloaded to a file/string.

Two simple functions:

private async Task TryLoadCookiesAsync()
{
    if (!File.Exists(_cookieFile))
        return;

    try
    {
        using var fs = File.OpenRead(_cookieFile);
        var currentCookies = JsonSerializer.Deserialize<CookieParam[]>(fs);
        await _page.SetCookieAsync(currentCookies);
    }
    catch (Exception ex)
    {
        _log.Warning(ex, "Exception loading cookies.");
    }
}

private async Task TrySaveCookiesAsync()
{
    try
    {
        // Attempt to get cookies from browser:
        var cookies = await _page.GetCookiesAsync();

        using var fs = File.Create(_cookieFile);
        await JsonSerializer.SerializeAsync(fs, cookies);
    }
    catch (Exception ex)
    {
        _log.Warning(ex, "Exception saving cookies");
    }
}

Expected behavior:

TryLoadCookiesAsync() to be able to be loaded.

Actual behavior:

Throws an exception "The node must be of type 'JsonObject'".

Versions

  • Which version of PuppeteerSharp are you using? 20.0.0.5
  • Which .NET runtime and version are you targeting? .NET 8</issue_description>

Comments on the Issue (you are @copilot in this section)

Custom agent used: Bidi migration
Implement bidi code


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

…erialization

Co-authored-by: kblok <2198466+kblok@users.noreply.github.com>
@kblok kblok marked this pull request as ready for review January 5, 2026 13:12
Copilot AI review requested due to automatic review settings January 5, 2026 13:12
@kblok kblok changed the title [WIP] Fix deserialization issue in CookiePartitionKeyConverter Fix deserialization issue in CookiePartitionKeyConverter Jan 5, 2026
Copilot AI changed the title Fix deserialization issue in CookiePartitionKeyConverter Fix CookiePartitionKeyConverter to support string deserialization Jan 5, 2026
Copilot AI requested a review from kblok January 5, 2026 13:15
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CookiePartitionKeyConverter doesn't support deserialization of string properly

3 participants