chromiumembedded / cef

Chromium Embedded Framework (CEF). A simple framework for embedding Chromium-based browsers in other applications.
https://bitbucket.org/chromiumembedded/cef/
Other
3.38k stars 467 forks source link

Cookies created when using Alloy bootstrap not available when switching to Chrome bootstrap #3721

Closed EamonHetherton closed 5 months ago

EamonHetherton commented 5 months ago

I'm using CEF via the downstream project CefSharp which had previously used the Alloy bootstrap but is switching to the Chrome bootstrap by default in v126. As a result of this, cookies that existed prior to switching are no longer available.

To Reproduce Steps to reproduce the behavior:

  1. Initialize CEF using Alloy bootstrap with a clean cache_path
  2. Create a cookie (or visit site that sets a cookie)
  3. Restart CEF now using Chrome bootstrap with same cache_path
  4. No cookies exist

Expected behavior Either cookies available by default after switching to use Chrome bootstrap or an option provided to migrate them from Alloy to Chrome.

Versions:

Additional context (https://github.com/cefsharp/CefSharp/issues/4847)

It was suggested by another user on the CefSharp issue that it is due to the Chrome bootstrap using an encryption key that is stored in the Local State file, but the Alloy bootstrap uses a key from LocalPrefs.json

magreenblatt commented 5 months ago

We don't plan to migrate cache directories automatically. CEF doesn't know what Profile-specific directory you were using previously, and not all files/directories are applicable between Chrome and Alloy bootstraps.

Your application can copy some files before calling CefInitialize. For example, it's fine to copy the Local Storage and Session Storage folders between Alloy and Chrome cache directories.

I have not tried copying Cookies or Cookies-journal files. Both Local State and LocalPrefs.json are JSON files, so you can copy values between them as needed (and test/verify that it works as you expect).

EamonHetherton commented 5 months ago

We don't plan to migrate cache directories automatically. CEF doesn't know what Profile-specific directory you were using previously, and not all files/directories are applicable between Chrome and Alloy bootstraps.

I am using the same cache_path that I used for the Alloy bootstrap when switching to the Chrome bootstrap. I don't need chromium to detect and auto migrate, I'm happy to be explicit and provide whatever information is required to migrate the cookies.

I have tried copying the Local Storage, Session Storage and Network folders to a sub folder called Default which appears to be where the ChromeBootstrap would create these folders and then copied the "os_crypt" block that contains and "encrypted_key" field from the LocalPrefs.json file created by the AlloyBootstrap to the "Local State" file created by the Chrome bootstrap but that was not enough. Is there somewhere that I can reference to better understand the folder structure differences between the two?

Even with log level at Verbose the log file does not indicate a problem loading the cookies :(

magreenblatt commented 5 months ago

copied the "os_crypt" block that contains and "encrypted_key" field from the LocalPrefs.json file created by the AlloyBootstrap to the "Local State" file created by the Chrome bootstrap but that was not enough

It might be better to extract the cookies using the CefCookieManager API while running with the Alloy bootstrap, and then use the same API to import the cookies after restarting (or installing the new app version) with the Chrome bootstrap.

Is there somewhere that I can reference to better understand the folder structure differences between the two?

It's probably easiest just to run cefclient with Alloy (--disable-chrome-runtime) and Chrome bootstraps with different --cache-path values, and compare the resulting directory structures. Many of the contents will be the same, with Chrome creating some additional files and folders. As you've noticed, Chrome uses a "Default" subdirectory under cache-path whereas Alloy writes everything in the cache-path directory.

EamonHetherton commented 5 months ago

It might be better to extract the cookies using the CefCookieManager API while running with the Alloy bootstrap, and then use the same API to import the cookies after restarting (or installing the new app version) with the Chrome bootstrap.

I considered that but I wanted it to be transparent to the user and due to the restriction of only being able to initialise CEF once per process I decided to explore other options first.

Anyway, I've made some progress. Not sure what I had done before that did not work but I was able to migrate my cookies this time by doing the following:

1) Run the Alloy bootstrap and ensure some cookies exist 2) Create a Default directory in the cache_path folder 3) Copy the Local Storage, Session Storage and Network folders to the sub folder Default (maybe should copy some other folders too, have not investigated further yet) 4) rename LocalPrefs.json in cache_path to Local State (this is essentially setting up the correct os_crypt.encrypted_key value that can be used to decrypt the cookie values)