cefsharp / CefSharp

.NET (WPF and Windows Forms) bindings for the Chromium Embedded Framework
http://cefsharp.github.io/
Other
9.76k stars 2.92k forks source link

v126.x (Chrome bootstrap) unable to load cookies from prior version (Alloy bootstrap) #4847

Open EamonHetherton opened 2 weeks ago

EamonHetherton commented 2 weeks ago

Is there an existing issue for this?

CefSharp Version

126.2.70

Operating System

Windows 11

Architecture

x64

.Net Version

net 8.0

Implementation

WinForms

Reproduction Steps

1) Initialize CEF with a clean CachePath on a prior version of CefSharp.WinForms.NETCore (e.g. I have tested various versions from 100.0.140-125.0.210) and either create a cookie manually or visit a site that sets a cookie. 2) update to 126.2.70 and using the same CachePath the cookie is no longer available.

Expected behavior

I would have expected that the cookie created in a prior version to still be available. I have just tested the process of upgrading the following versions and each time the cookies were still intact in the next version until I hit v126.2.70.

Actual behavior

no cookies appear to be in the cookie store immediately after upgrading to v126.2.70

Regression?

yes this looks like it worked in prior versions. it appears at least since 100.0.140 the cookies have been forward compatible until we reached v126.2.70

amaitland commented 2 weeks ago

Thanks for the detailed testing 👍 It's important to remember that CefSharp is just a wrapper around the Chromium Embedded Framework(CEF).

Try switching back to the Alloy bootstrap to see if that makes a difference. Refer to https://github.com/cefsharp/CefSharp/issues/4835#issuecomment-2183620313

Ultimately this will need to be raised at https://github.com/chromiumembedded/cef/issues

EamonHetherton commented 2 weeks ago

Thanks for the prompt reply, settings.ChromeRuntime = false; does appear to fix the issue with cookies still being loadable after the update. I'll try to dig deeper into the root cause.

EugeneSunrise commented 2 weeks ago

Thanks for the prompt reply, settings.ChromeRuntime = false; does appear to fix the issue with cookies still being loadable after the update. I'll try to dig deeper into the root cause.

I had a similar problem, the point is that when we use chrome bootstrap(ChromeRuntume = true, since 125 ver.) cookies are encrypted with a key that is stored in the Local State file, but if we use alloy bootstrap cookies are encrypted with a key from LocalPrefs.json

amaitland commented 2 weeks ago

Thanks for the prompt reply, settings.ChromeRuntime = false; does appear to fix the issue with cookies still being loadable after the update. I'll try to dig deeper into the root cause.

Please raise an issue on the https://github.com/chromiumembedded/cef/issues (post a link back here for reference).

We'll need to see if CEF can handle this case gracefully.

EamonHetherton commented 2 weeks ago

https://github.com/chromiumembedded/cef/issues/3721

EamonHetherton commented 2 weeks ago

We don't plan to migrate cache directories automatically https://github.com/chromiumembedded/cef/issues/3721#issuecomment-2187539681 :(

I thought about trying to auto detect the first run with the Chrome bootstrap and initialise the Alloy bootstrap first and extract all the cookies, then load Chrome bootstrap and insert the cookies but can only initialise CEF once per process so that becomes significantly more difficult

EamonHetherton commented 2 weeks ago

some success: https://github.com/chromiumembedded/cef/issues/3721#issuecomment-2187818342

Still not sure what to use to detect "first run" of Chrome bootstrap (probably the existence of LocalPrefs.json and not Local State file at this stage), but I think following the steps outlined in that link might work for me. Will also need to consider what other files/folders should be cleaned up as part of the Alloy->Chrome bootstrap migration.

Any thoughts on what else may need migrating?

EamonHetherton commented 2 weeks ago

Work in progress migration, works for me YMMV :)

public static class Alloy_To_Chrome_Migration
{
    private static readonly string[] FoldersToMigrate = ["Cache", "Code Cache", "DawnGraphiteCache", "DawnWebGPUCache", "GPUCache", "Local Storage", "Network", "Session Storage", "Shared Dictionary"];
    private static readonly string[] FilesToMigrate = ["LOCK", "LOG", "Visited Links"];
    private const string AlloyStateFilename = "LocalPrefs.json";
    private const string ChromeStateFileName = "Local State";
    public static void Execute(CefSettings settings)
    {
        var cache_path = settings.CachePath;
        try
        {
            string alloyStateFile = Path.Combine(cache_path, AlloyStateFilename);
            string chromeStateFile = Path.Combine(cache_path, ChromeStateFileName);
            if (settings.ChromeRuntime && File.Exists(alloyStateFile) && !File.Exists(chromeStateFile))
            {
                File.Move(alloyStateFile, chromeStateFile);

                var defaultDir = Path.Combine(cache_path, "Default");
                Directory.CreateDirectory(defaultDir);

                foreach (var migrationFolderName in FoldersToMigrate)
                {
                    var migrationFolder = Path.Combine(cache_path, migrationFolderName);
                    if (Directory.Exists(migrationFolder))
                    {
                        Directory.Move(migrationFolder, Path.Combine(defaultDir, migrationFolderName));
                    }
                }
                foreach (var migrationFileName in FilesToMigrate)
                {
                    var migrationFile = Path.Combine(cache_path, migrationFileName);
                    if (File.Exists(migrationFile))
                    {
                        File.Move(migrationFile, Path.Combine(defaultDir, migrationFileName));
                    }
                }
            }
        }
        catch (Exception)
        {
        }
    }
}