Skip to content

Instantly share code, notes, and snippets.

@msanders
Last active March 13, 2024 01:56
Show Gist options
  • Save msanders/0715f4a1d64190011d7813e57a2e8ffe to your computer and use it in GitHub Desktop.
Save msanders/0715f4a1d64190011d7813e57a2e8ffe to your computer and use it in GitHub Desktop.
Export cookies from a Firefox profile to cookies.txt format

Background

http://fileformats.archiveteam.org/wiki/Netscape_cookies.txt

Installation

mkdir -p ~/bin
curl --proto "=https" --tlsv1.2 https://gist.githubusercontent.com/msanders/0715f4a1d64190011d7813e57a2e8ffe/raw/firefox_export_cookies_txt > ~/bin/firefox_export_cookies_txt
chmod u+x ~/bin/firefox_export_cookies_txt

# https://kb.mozillazine.org/Profile_folder_-_Firefox#Finding_the_profile_folder
~/bin/firefox_export_cookies_txt /path/to/fxprofile

Note that this script should work even when Firefox is running and has locked the cookies.sqlite database.

License

This is made available under the terms of the MIT license. For a copy, see https://opensource.org/licenses/MIT.

#!/bin/sh
# https://gist.github.com/msanders/0715f4a1d64190011d7813e57a2e8ffe
# License: MIT
set -o errexit -o nounset
SCRIPT_NAME="$(basename "$0")"
show_usage() {
cat <<USAGE
Usage: $SCRIPT_NAME <profile>
Export cookies from a Firefox profile as Netscape cookies.txt format.
USAGE
}
main() (
if [ "$#" -ne 1 ]; then
show_usage
exit 1
fi
case "$1" in
-h | --help)
show_usage
exit
esac
profiledir="$(readlink -f "$1")"
dbpath="$profiledir/cookies.sqlite"
# Adopted from
# http://fileformats.archiveteam.org/wiki/Netscape_cookies.txt#Extract_cookies_to_Netscape_cookies.txt_format
printf "# HTTP Cookie File\n\n"
sqlite3 -init /dev/null -tabs -readonly \
"file://$dbpath?immutable=1" <<EOF | sort -t "\t" -k 1,1 -k 6,6
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
)
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment