Fix sharing of cookie jars with private browsing

Fixes #1219
This commit is contained in:
Florian Bruhin 2016-06-08 16:34:42 +02:00
parent 530721522a
commit 483a5f8103
7 changed files with 37 additions and 8 deletions

View File

@ -90,6 +90,7 @@ Fixed
- Fix for tab indicators getting lost when moving tabs - Fix for tab indicators getting lost when moving tabs
- Fixed handling of backspace in number hinting mode - Fixed handling of backspace in number hinting mode
- Fixed `FileNotFoundError` when starting in some cases on old Qt versions - Fixed `FileNotFoundError` when starting in some cases on old Qt versions
- Fixed sharing of cookies between tabs when `private-browsing` is enabled
v0.6.2 v0.6.2
------ ------

View File

@ -426,7 +426,9 @@ def _init_modules(args, crash_handler):
proxy.init() proxy.init()
log.init.debug("Initializing cookies...") log.init.debug("Initializing cookies...")
cookie_jar = cookies.CookieJar(qApp) cookie_jar = cookies.CookieJar(qApp)
ram_cookie_jar = cookies.RAMCookieJar(qApp)
objreg.register('cookie-jar', cookie_jar) objreg.register('cookie-jar', cookie_jar)
objreg.register('ram-cookie-jar', ram_cookie_jar)
log.init.debug("Initializing cache...") log.init.debug("Initializing cache...")
diskcache = cache.DiskCache(standarddir.cache(), parent=qApp) diskcache = cache.DiskCache(standarddir.cache(), parent=qApp)
objreg.register('cache', diskcache) objreg.register('cache', diskcache)

View File

@ -31,7 +31,6 @@ from PyQt5.QtNetwork import (QNetworkAccessManager, QNetworkReply, QSslError,
from qutebrowser.config import config from qutebrowser.config import config
from qutebrowser.utils import (message, log, usertypes, utils, objreg, qtutils, from qutebrowser.utils import (message, log, usertypes, utils, objreg, qtutils,
urlutils, debug) urlutils, debug)
from qutebrowser.browser import cookies
from qutebrowser.browser.network import qutescheme, networkreply from qutebrowser.browser.network import qutescheme, networkreply
from qutebrowser.browser.network import filescheme from qutebrowser.browser.network import filescheme
@ -184,15 +183,15 @@ class NetworkManager(QNetworkAccessManager):
private: Whether we're currently in private browsing mode. private: Whether we're currently in private browsing mode.
""" """
if private: if private:
cookie_jar = cookies.RAMCookieJar(self) cookie_jar = objreg.get('ram-cookie-jar')
self.setCookieJar(cookie_jar)
else: else:
# We have a shared cookie jar - we restore its parent so we don't
# take ownership of it.
app = QCoreApplication.instance()
cookie_jar = objreg.get('cookie-jar') cookie_jar = objreg.get('cookie-jar')
self.setCookieJar(cookie_jar)
cookie_jar.setParent(app) # We have a shared cookie jar - we restore its parent so we don't
# take ownership of it.
self.setCookieJar(cookie_jar)
app = QCoreApplication.instance()
cookie_jar.setParent(app)
def _set_cache(self): def _set_cache(self):
"""Set the cache of the NetworkManager correctly. """Set the cache of the NetworkManager correctly.

View File

@ -455,3 +455,12 @@ Feature: Various utility commands.
When I run :set-cmd-text :message-i "Hello World" When I run :set-cmd-text :message-i "Hello World"
And I run :command-accept And I run :command-accept
Then the message "Hello World" should be shown Then the message "Hello World" should be shown
## https://github.com/The-Compiler/qutebrowser/issues/1219
Scenario: Sharing cookies with private browsing
When I set general -> private-browsing to true
And I open cookies/set?qute-test=42 without waiting
And I wait until cookies is loaded
And I open cookies in a new tab
Then the cookie qute-test should be set to 42

View File

@ -18,6 +18,7 @@
# along with qutebrowser. If not, see <http://www.gnu.org/licenses/>. # along with qutebrowser. If not, see <http://www.gnu.org/licenses/>.
import sys import sys
import json
import os.path import os.path
import subprocess import subprocess
@ -60,3 +61,15 @@ def update_documentation():
def pdfjs_available(): def pdfjs_available():
if not pdfjs.is_available(): if not pdfjs.is_available():
pytest.skip("No pdfjs installation found.") pytest.skip("No pdfjs installation found.")
@bdd.then(bdd.parsers.parse('the cookie {name} should be set to {value}'))
def check_cookie(quteproc, name, value):
"""Check if a given cookie is set correctly.
This assumes we're on the httpbin cookies page.
"""
content = quteproc.get_content()
data = json.loads(content)
print(data)
assert data['cookies'][name] == value

View File

@ -70,6 +70,7 @@ class Request(testprocess.Line):
[http.client.UNAUTHORIZED, http.client.OK], [http.client.UNAUTHORIZED, http.client.OK],
'/redirect-to': [http.client.FOUND], '/redirect-to': [http.client.FOUND],
'/status/404': [http.client.NOT_FOUND], '/status/404': [http.client.NOT_FOUND],
'/cookies/set': [http.client.FOUND],
} }
sanitized = QUrl('http://localhost' + self.path).path() # Remove ?foo sanitized = QUrl('http://localhost' + self.path).path() # Remove ?foo

View File

@ -34,6 +34,7 @@ import pytest
import helpers.stubs as stubsmod import helpers.stubs as stubsmod
from qutebrowser.config import config from qutebrowser.config import config
from qutebrowser.utils import objreg from qutebrowser.utils import objreg
from qutebrowser.browser import cookies
from PyQt5.QtCore import QEvent, QSize, Qt from PyQt5.QtCore import QEvent, QSize, Qt
from PyQt5.QtWidgets import QWidget, QHBoxLayout, QVBoxLayout from PyQt5.QtWidgets import QWidget, QHBoxLayout, QVBoxLayout
@ -281,11 +282,14 @@ def fake_keyevent_factory():
def cookiejar_and_cache(stubs): def cookiejar_and_cache(stubs):
"""Fixture providing a fake cookie jar and cache.""" """Fixture providing a fake cookie jar and cache."""
jar = QNetworkCookieJar() jar = QNetworkCookieJar()
ram_jar = cookies.RAMCookieJar()
cache = stubs.FakeNetworkCache() cache = stubs.FakeNetworkCache()
objreg.register('cookie-jar', jar) objreg.register('cookie-jar', jar)
objreg.register('ram-cookie-jar', ram_jar)
objreg.register('cache', cache) objreg.register('cache', cache)
yield yield
objreg.delete('cookie-jar') objreg.delete('cookie-jar')
objreg.delete('ram-cookie-jar')
objreg.delete('cache') objreg.delete('cache')