Netscape cookies.txt

From Just Solve the File Format Problem
Revision as of 22:57, 19 September 2022 by KlfJoat (Talk | contribs)

Jump to: navigation, search
File Format
Name Netscape cookies.txt
Ontology


In the past, the Netscape browser was so dominant that many tools emulated its cookie file format. The browser market moved on, but the cookies.txt file format remains in use by fundamental and popular tools such as curl, wget, and youtube-dl, as well as many others.

Contents

File format

Officially, the first line of the file must be one of the following:

  • # HTTP Cookie File
  • # Netscape HTTP Cookie File

Fields are separated by tab characters (\t or \009 or 0x09).

Lines are separated by the newline format in use by the running operating system. That means CRLF (\r\n) for Windows and LF (\n) for Unix-like systems such as Linux, macOS, FreeBSD, etc.

The 7 fields are as follows.

Human-Friendly Schema
Field Name Type Example Value Notes
host string example.com Hostname that owns the cookie
subdomains boolean string FALSE Include subdomains (old attempt at SameSite)
path string / Pathname that owns the cookie at the host
isSecure boolean string TRUE Send/receive cookie over HTTPS only.
expiry number 1663611142 Cookie expiration in standard Unix timestamp format
name string cookiename Cookie name
value string cookievalue Cookie value

Example file

# HTTP Cookie File
example.com	FALSE	/	TRUE	1663611142	cookiename	cookievalue
example.net	FALSE	/	FALSE	1125326700	cookiename	cookievalue
example.org	TRUE	/	FALSE	1000210440	cookiename	cookievalue
example.com	FALSE	/a/	FALSE	1596693600	cookiename	cookievalue


Extract cookies to Netscape cookies.txt format

This is a common need so extensions exist for Chrome and Firefox.

Code snippet to extract cookies from Mozilla Firefox to Netscape cookies.txt format

It may be useful to extract your own cookies by running a script. This is a small excerpt of a larger script, so it will not run alone as-is. This portion will take a Firefox cookie database, convert it to cookies.txt format, then echo it to stdout. The work before this snippet plus capturing the output are beyond the scope of this page.

Because of format changes, some data type conversions must be performed during the extract. This is done in the case statements.

This snippet of code is not compatible with the Multi-Account Container Extension or the Facebook Container Extension. Multiple cookies that match the same host/path/name tuple might be output. Depending on the website and the way the cookies.txt file is parsed by the tool you're using, you may find inconsistent, unexpected, or contradictory behavior.

Prerequisites:

  • A Bourne-compatible shell with support for here documents, such as sh, csh, tcsh, ksh, bash, and zsh to name a few.
  • The sqlite3 binary.
echo "# HTTP Cookie File"
sqlite3 -separator '	' "cookies.sqlite" <<- EOF
.mode tabs
.header off
select host,
case substr(host,1,1)='.' when 0 then 'FALSE' else 'TRUE' end,
path,
case isSecure when 0 then 'FALSE' else 'TRUE' end,
expiry,
name,
value
from moz_cookies;
EOF

Firefox locks the cookies.sqlite file while running. If you want to run the above script you must either exit Firefox or copy the file to a temporary location.

Notes

  • The yt-dlp project supports pulling cookies from the browser natively with --cookies-from-browser as well as the legacy cookies.txt format.


Links

Personal tools
Namespaces

Variants
Actions
Navigation
Toolbox