Repository: yt-dlp/yt-dlp

Issue:
Get session cookies when using --cookies-from-browser
### DO NOT REMOVE OR SKIP THE ISSUE TEMPLATE

- [X] I understand that I will be **blocked** if I remove or skip any mandatory\* field

### Checklist

- [X] I'm reporting a bug unrelated to a specific site
- [X] I've verified that I'm running yt-dlp version **2022.11.11** ([update instructions](https://github.com/yt-dlp/yt-dlp#update)) or later (specify commit)
- [X] I've checked that all provided URLs are playable in a browser with the same IP and same login details
- [X] I've checked that all URLs and arguments with special characters are [properly quoted or escaped](https://github.com/yt-dlp/yt-dlp/wiki/FAQ#video-url-contains-an-ampersand--and-im-getting-some-strange-output-1-2839-or-v-is-not-recognized-as-an-internal-or-external-command)
- [X] I've searched the [bugtracker](https://github.com/yt-dlp/yt-dlp/issues?q=) for similar issues **including closed ones**. DO NOT post duplicates
- [X] I've read the [guidelines for opening an issue](https://github.com/yt-dlp/yt-dlp/blob/master/CONTRIBUTING.md#opening-an-issue)

### Provide a description that is worded well enough to be understood

While investigating #4514 I found a different behavior between `--cookies cookies.txt` and `--cookies-from-browser chrome`.
The first option injected all cookies into the request, while the latter one was missing the session cookies.

Session cookies have `expires=None` and `discard=True`. Taking a look into the codebase I see that session cookies imported from Chrome have `expires=0` and `discard=False`.

https://github.com/yt-dlp/yt-dlp/blob/d7b460d0e5fc710950582baed2e3fc616ed98a80/yt_dlp/cookies.py#L335-L339

Is this intended or possibly a bug?

### Provide verbose output that clearly demonstrates the problem

- [X] Run **your** yt-dlp command with **-vU** flag added (`yt-dlp -vU <your command line>`)
- [X] Copy the WHOLE output (starting with `[debug] Command-line config`) and insert it below

### Complete Verbose Output

```shell
NA
```



Discussion:
Definitely not intended. I thought chrome doesn't write session cookies to disk 🤔. If it does, we should "fix" the cookies similar to https://github.com/yt-dlp/yt-dlp/blob/b96187a9603aaa7ead8ad94ac82433f085ea4895/yt_dlp/utils.py#L1654-L1667
>  I thought chrome doesn't write session cookies to disk 🤔

It does because of the `Continue where you left off` startup option.
Firefox is also affected by this issue: It seems like session cookies are not stored in its `cookies.sqlite` file.

Instead, they are in `sessionstore.jsonlz4` (but that file only exists if Firefox is not running). That's a file in Mozilla's custom `mozLz4` format, which essentially is just the LZ4 block format preceded by a lower-endian 32-bit unsigned int specifying the size of the uncompressed data in bytes. 

Once uncompressed, the `sessionstore` data can be parsed as JSON, and the session cookies found in the top-level key `cookies` of the resulting object.

Resolving patch:
diff --git a/yt_dlp/cookies.py b/yt_dlp/cookies.py
--- a/yt_dlp/cookies.py
+++ b/yt_dlp/cookies.py
@@ -347,6 +347,11 @@ def _process_chrome_cookie(decryptor, host_key, name, value, encrypted_value, pa
         if value is None:
             return is_encrypted, None
 
+    # In chrome, session cookies have expires_utc set to 0
+    # In our cookie-store, cookies that do not expire should have expires set to None
+    if not expires_utc:
+        expires_utc = None
+
     return is_encrypted, http.cookiejar.Cookie(
         version=0, name=name, value=value, port=None, port_specified=False,
         domain=host_key, domain_specified=bool(host_key), domain_initial_dot=host_key.startswith('.'),


Test patch: