2011-03-03 18:29:20 +01:00
|
|
|
/* ============================================================
|
|
|
|
* QupZilla - WebKit based browser
|
2014-01-11 16:11:42 +01:00
|
|
|
* Copyright (C) 2010-2014 David Rosca <nowrep@gmail.com>
|
2011-03-03 18:29:20 +01:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
* ============================================================ */
|
2011-03-02 16:57:41 +01:00
|
|
|
#include "cookiejar.h"
|
2012-01-21 23:19:38 +01:00
|
|
|
#include "mainapplication.h"
|
2014-03-09 21:51:42 +01:00
|
|
|
#include "datapaths.h"
|
|
|
|
#include "autosaver.h"
|
2012-01-11 21:58:25 +01:00
|
|
|
#include "settings.h"
|
2013-02-26 15:48:47 +01:00
|
|
|
#include "qztools.h"
|
2012-02-29 18:33:50 +01:00
|
|
|
|
2012-12-20 14:45:35 +01:00
|
|
|
#include <QNetworkCookie>
|
2012-12-21 16:25:00 +01:00
|
|
|
#include <QWebSettings>
|
2012-02-29 18:33:50 +01:00
|
|
|
#include <QDateTime>
|
2012-07-13 16:43:47 +02:00
|
|
|
|
2011-11-05 15:04:29 +01:00
|
|
|
//#define COOKIE_DEBUG
|
2011-03-02 16:57:41 +01:00
|
|
|
|
2014-03-09 21:51:42 +01:00
|
|
|
CookieJar::CookieJar(QObject* parent)
|
2011-11-05 15:04:29 +01:00
|
|
|
: QNetworkCookieJar(parent)
|
2014-03-06 16:12:36 +01:00
|
|
|
, m_autoSaver(0)
|
2011-03-02 16:57:41 +01:00
|
|
|
{
|
2014-03-06 16:12:36 +01:00
|
|
|
m_autoSaver = new AutoSaver(this);
|
|
|
|
connect(m_autoSaver, SIGNAL(save()), this, SLOT(saveCookies()));
|
2014-03-09 21:51:42 +01:00
|
|
|
|
|
|
|
loadSettings();
|
|
|
|
restoreCookies();
|
2014-03-06 16:12:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
CookieJar::~CookieJar()
|
|
|
|
{
|
|
|
|
m_autoSaver->saveIfNecessary();
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CookieJar::loadSettings()
|
|
|
|
{
|
2012-01-11 21:58:25 +01:00
|
|
|
Settings settings;
|
2012-02-17 18:55:34 +01:00
|
|
|
settings.beginGroup("Cookie-Settings");
|
2011-11-05 15:04:29 +01:00
|
|
|
m_allowCookies = settings.value("allowCookies", true).toBool();
|
2012-07-13 16:43:47 +02:00
|
|
|
m_blockThirdParty = settings.value("allowCookiesFromVisitedDomainOnly", false).toBool();
|
2011-11-05 15:04:29 +01:00
|
|
|
m_filterTrackingCookie = settings.value("filterTrackingCookie", false).toBool();
|
2011-03-02 16:57:41 +01:00
|
|
|
m_deleteOnClose = settings.value("deleteCookiesOnClose", false).toBool();
|
2012-02-17 18:55:34 +01:00
|
|
|
m_whitelist = settings.value("whitelist", QStringList()).toStringList();
|
|
|
|
m_blacklist = settings.value("blacklist", QStringList()).toStringList();
|
|
|
|
settings.endGroup();
|
2012-12-21 16:25:00 +01:00
|
|
|
|
2013-01-20 12:10:28 +01:00
|
|
|
#if QTWEBKIT_FROM_2_3
|
2014-03-06 16:12:36 +01:00
|
|
|
QWebSettings::globalSettings()->setThirdPartyCookiePolicy(m_blockThirdParty ?
|
2012-12-21 16:38:44 +01:00
|
|
|
QWebSettings::AlwaysBlockThirdPartyCookies :
|
|
|
|
QWebSettings::AlwaysAllowThirdPartyCookies);
|
2012-12-21 16:25:00 +01:00
|
|
|
#endif
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CookieJar::setAllowCookies(bool allow)
|
|
|
|
{
|
|
|
|
m_allowCookies = allow;
|
|
|
|
}
|
|
|
|
|
2012-07-13 16:43:47 +02:00
|
|
|
bool CookieJar::setCookiesFromUrl(const QList<QNetworkCookie> &cookieList, const QUrl &url)
|
|
|
|
{
|
2013-02-01 14:45:42 +01:00
|
|
|
QList<QNetworkCookie> newList;
|
2012-07-13 16:43:47 +02:00
|
|
|
|
2013-03-06 09:05:41 +01:00
|
|
|
foreach (QNetworkCookie cookie, cookieList) {
|
2013-02-01 14:45:42 +01:00
|
|
|
// If cookie domain is empty, set it to url.host()
|
|
|
|
if (cookie.domain().isEmpty()) {
|
|
|
|
cookie.setDomain(url.host());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!rejectCookie(url.host(), cookie)) {
|
|
|
|
newList.append(cookie);
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
}
|
2012-02-17 18:55:34 +01:00
|
|
|
|
2014-03-06 16:12:36 +01:00
|
|
|
bool added = QNetworkCookieJar::setCookiesFromUrl(newList, url);
|
2013-01-30 16:04:05 +01:00
|
|
|
|
2014-03-06 16:12:36 +01:00
|
|
|
if (added) {
|
|
|
|
m_autoSaver->changeOcurred();
|
2013-01-30 16:04:05 +01:00
|
|
|
}
|
2013-01-30 18:49:35 +01:00
|
|
|
|
2014-03-06 16:12:36 +01:00
|
|
|
return added;
|
|
|
|
}
|
2011-03-02 16:57:41 +01:00
|
|
|
|
2014-03-06 16:12:36 +01:00
|
|
|
QList<QNetworkCookie> CookieJar::allCookies() const
|
|
|
|
{
|
|
|
|
return QNetworkCookieJar::allCookies();
|
|
|
|
}
|
2012-02-17 18:55:34 +01:00
|
|
|
|
2014-03-06 16:12:36 +01:00
|
|
|
void CookieJar::setAllCookies(const QList<QNetworkCookie> &cookieList)
|
|
|
|
{
|
|
|
|
m_autoSaver->changeOcurred();
|
|
|
|
QNetworkCookieJar::setAllCookies(cookieList);
|
|
|
|
}
|
2011-03-02 16:57:41 +01:00
|
|
|
|
2014-03-06 16:12:36 +01:00
|
|
|
void CookieJar::clearCookies()
|
|
|
|
{
|
|
|
|
setAllCookies(QList<QNetworkCookie>());
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CookieJar::restoreCookies()
|
|
|
|
{
|
2014-03-10 00:47:07 +01:00
|
|
|
if (mApp->isPrivate()) {
|
2011-03-02 16:57:41 +01:00
|
|
|
return;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2012-06-26 11:49:39 +02:00
|
|
|
|
2014-03-09 22:17:13 +01:00
|
|
|
const QString cookiesFile = DataPaths::currentProfilePath() + QLatin1String("/cookies.dat");
|
2011-03-02 16:57:41 +01:00
|
|
|
QDateTime now = QDateTime::currentDateTime();
|
|
|
|
|
|
|
|
QList<QNetworkCookie> restoredCookies;
|
2014-03-06 16:12:36 +01:00
|
|
|
QFile file(cookiesFile);
|
2011-03-02 16:57:41 +01:00
|
|
|
file.open(QIODevice::ReadOnly);
|
|
|
|
QDataStream stream(&file);
|
|
|
|
int count;
|
|
|
|
|
|
|
|
stream >> count;
|
2011-11-06 17:01:23 +01:00
|
|
|
for (int i = 0; i < count; i++) {
|
2011-03-02 16:57:41 +01:00
|
|
|
QByteArray rawForm;
|
|
|
|
stream >> rawForm;
|
2012-02-17 18:55:34 +01:00
|
|
|
const QList<QNetworkCookie> &cookieList = QNetworkCookie::parseCookies(rawForm);
|
|
|
|
if (cookieList.isEmpty()) {
|
2011-03-02 16:57:41 +01:00
|
|
|
continue;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2012-02-17 18:55:34 +01:00
|
|
|
|
2013-12-30 13:43:48 +01:00
|
|
|
const QNetworkCookie cookie = cookieList.at(0);
|
2012-02-17 18:55:34 +01:00
|
|
|
|
|
|
|
if (cookie.expirationDate() < now) {
|
2011-03-02 16:57:41 +01:00
|
|
|
continue;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2012-02-17 18:55:34 +01:00
|
|
|
restoredCookies.append(cookie);
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
file.close();
|
2014-03-06 16:12:36 +01:00
|
|
|
QNetworkCookieJar::setAllCookies(restoredCookies);
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
2014-03-06 16:12:36 +01:00
|
|
|
void CookieJar::saveCookies()
|
2012-01-12 20:10:51 +01:00
|
|
|
{
|
2014-03-10 00:47:07 +01:00
|
|
|
if (mApp->isPrivate()) {
|
2014-03-06 16:12:36 +01:00
|
|
|
return;
|
|
|
|
}
|
2012-01-12 20:10:51 +01:00
|
|
|
|
2014-03-06 16:12:36 +01:00
|
|
|
QList<QNetworkCookie> cookies = allCookies();
|
|
|
|
|
|
|
|
if (m_deleteOnClose) {
|
|
|
|
// If we are deleting cookies on close, save only whitelisted cookies
|
|
|
|
cookies.clear();
|
|
|
|
QList<QNetworkCookie> aCookies = allCookies();
|
|
|
|
|
|
|
|
foreach (const QNetworkCookie &cookie, aCookies) {
|
|
|
|
if (listMatchesDomain(m_whitelist, cookie.domain())) {
|
|
|
|
cookies.append(cookie);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-09 22:17:13 +01:00
|
|
|
QFile file(DataPaths::currentProfilePath() + QLatin1String("/cookies.dat"));
|
2014-03-06 16:12:36 +01:00
|
|
|
file.open(QIODevice::WriteOnly);
|
|
|
|
QDataStream stream(&file);
|
|
|
|
int count = cookies.count();
|
|
|
|
|
|
|
|
stream << count;
|
|
|
|
for (int i = 0; i < count; i++) {
|
|
|
|
const QNetworkCookie cookie = cookies.at(i);
|
|
|
|
|
|
|
|
if (cookie.isSessionCookie()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
stream << cookie.toRawForm();
|
|
|
|
}
|
|
|
|
|
|
|
|
file.close();
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
2014-03-06 16:12:36 +01:00
|
|
|
bool CookieJar::rejectCookie(const QString &domain, const QNetworkCookie &cookie) const
|
2011-03-02 16:57:41 +01:00
|
|
|
{
|
2014-03-06 16:12:36 +01:00
|
|
|
Q_UNUSED(domain)
|
|
|
|
|
|
|
|
const QString cookieDomain = cookie.domain();
|
|
|
|
|
|
|
|
if (!m_allowCookies) {
|
|
|
|
bool result = listMatchesDomain(m_whitelist, cookieDomain);
|
|
|
|
if (!result) {
|
|
|
|
#ifdef COOKIE_DEBUG
|
|
|
|
qDebug() << "not in whitelist" << cookie;
|
|
|
|
#endif
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_allowCookies) {
|
|
|
|
bool result = listMatchesDomain(m_blacklist, cookieDomain);
|
|
|
|
if (result) {
|
|
|
|
#ifdef COOKIE_DEBUG
|
|
|
|
qDebug() << "found in blacklist" << cookie;
|
|
|
|
#endif
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This feature is now natively in QtWebKit 2.3
|
|
|
|
#if QTWEBKIT_TO_2_3
|
|
|
|
if (m_blockThirdParty) {
|
|
|
|
bool result = matchDomain(cookieDomain, domain);
|
|
|
|
if (!result) {
|
|
|
|
#ifdef COOKIE_DEBUG
|
|
|
|
qDebug() << "purged for domain mismatch" << cookie << cookieDomain << domain;
|
|
|
|
#endif
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (m_filterTrackingCookie && cookie.name().startsWith("__utm")) {
|
|
|
|
#ifdef COOKIE_DEBUG
|
|
|
|
qDebug() << "purged as tracking " << cookie;
|
|
|
|
#endif
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
2013-02-01 14:45:42 +01:00
|
|
|
|
2014-03-06 16:12:36 +01:00
|
|
|
bool CookieJar::matchDomain(QString cookieDomain, QString siteDomain) const
|
2013-02-01 14:45:42 +01:00
|
|
|
{
|
|
|
|
// According to RFC 6265
|
|
|
|
|
|
|
|
// Remove leading dot
|
|
|
|
if (cookieDomain.startsWith(QLatin1Char('.'))) {
|
|
|
|
cookieDomain = cookieDomain.mid(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (siteDomain.startsWith(QLatin1Char('.'))) {
|
|
|
|
siteDomain = siteDomain.mid(1);
|
|
|
|
}
|
|
|
|
|
2013-02-26 15:48:47 +01:00
|
|
|
return QzTools::matchDomain(cookieDomain, siteDomain);
|
2013-02-01 14:45:42 +01:00
|
|
|
}
|
|
|
|
|
2014-03-06 16:12:36 +01:00
|
|
|
bool CookieJar::listMatchesDomain(const QStringList &list, const QString &cookieDomain) const
|
2013-02-01 14:45:42 +01:00
|
|
|
{
|
2013-03-06 09:05:41 +01:00
|
|
|
foreach (const QString &d, list) {
|
2013-02-01 14:45:42 +01:00
|
|
|
if (matchDomain(d, cookieDomain)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|