I know from this question that Firefox 3.0 and up stores its cookies in an SQLite database. My question is: can you access this database from other desktop programs in such a way that you could add a cookie?
I realize this has security implications. However, I do not want to read them at all. I want to be able to set one cookie if possible. I don't even want to overwrite a cookie. I just want to add it if it isn't there already. This is sort of a personal project I'm working on for fun.
This question is mostly language agnostic. I would prefer a solution in C#, but proof of concept in any language will suffice.
Extra credit: It would be cool to set the same cookie in Internet Explorer, too
For FF3, you can access the cookies.sqlite file with any SQLite wrapper - however, check whether FF is running - it may be write-locking the file (not tested).
The database contains this:
TABLE moz_cookies (
id INTEGER PRIMARY KEY,
name TEXT,
value TEXT,
host TEXT,
path TEXT,
expiry INTEGER,
lastAccessed INTEGER,
isSecure INTEGER,
isHttpOnly INTEGER
)
Not sure about the primary key, it looks like it is a unix timestamp of when the cookie was created; expiry and lastAccessed are also unix timestamps, the rest is self-explanatory.
Try an INSERT INTO moz_cookies
and see if FF becomes immediately aware of the new cookie or if it requires a restart.
I know this question is really old, but I had the same problem and never really found a complete sample of code (though the answers on this page pointed me in the right direction). HTH!
public static void ClearFirefoxCookies()
{
int procCount = Process.GetProcessesByName("firefox").Length;
if (procCount > 0)
throw new ApplicationException(string.Format("There are {0} instances of Firefox still running", procCount));
try
{
using (SQLiteConnection conn = new SQLiteConnection("Data Source=" + GetFirefoxCookiesFileName()))
{
conn.Open();
SQLiteCommand command = conn.CreateCommand();
command.CommandText = "delete from moz_cookies";
int count = command.ExecuteNonQuery();
}
}
catch (SQLiteException ex)
{
if (!(ex.ErrorCode == SQLiteErrorCode.Busy || ex.ErrorCode == SQLiteErrorCode.Locked))
throw new ApplicationException("The Firefox cookies.sqlite file is locked");
}
}
private static string GetFirefoxCookiesFileName()
{
string path = System.IO.Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData), @"Mozilla\Firefox\Profiles");
if (!System.IO.Directory.Exists(path))
throw new ApplicationException("Firefox profiles folder not found");
string[] fileNames = System.IO.Directory.GetFiles(path, "cookies.sqlite", System.IO.SearchOption.AllDirectories);
if (fileNames.Length != 1 || !System.IO.File.Exists(fileNames[0]))
throw new ApplicationException("Firefox cookies.sqlite file not found");
return fileNames[0];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With