aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar Brendan Taylor <whateley@gmail.com>2010-12-22 21:08:18 -0700
committerGravatar Brendan Taylor <whateley@gmail.com>2010-12-22 21:08:18 -0700
commit186668a01883127447f1b6042ea84c8f3cd7536b (patch)
tree8efe0ade69c5630997709d85f3eda602abf8d124
parent6ade80cd01d0473dc1268d35a9db89dc03b1b9e9 (diff)
add cookie whitelisting, document BLACKLIST_COOKIE and WHITELIST_COOKIE events
-rw-r--r--README10
-rw-r--r--examples/data/plugins/cookies.py44
2 files changed, 44 insertions, 10 deletions
diff --git a/README b/README
index e0d9dc9..35492da 100644
--- a/README
+++ b/README
@@ -719,7 +719,6 @@ Events have this format:
be a unix-timestamp or empty
* `EVENT [uzbl_instance_name] DELETE_COOKIE domain path name value scheme expire`:
When a cookie was deleted. arguments as ADD_COOKIE
-
Events/requests which the EM and its plugins listens for
@@ -794,6 +793,15 @@ Events/requests which the EM and its plugins listens for
`<index>` is `+`, advance the cursor by one character, and if it is `-`,
move the cursor back by one character.
* `START_COMPLETION`: TODO explain completion
+* `BLACKLIST_COOKIE`: add a rule for blacklisting cookies
+ - `request BLACKLIST_COOKIE <component> <regexp>`: Blacklist cookies where
+ `<component>` matches `<regexp>`. `<component>` is one of `domain`,
+ `path`, `name`, `value`, `scheme` or `expires`.
+* `WHITELIST_COOKIE`: add a rule for whitelisting cookies (if any whitelist is
+ set then only cookies that are whitelisted cookies will be used)
+ - `request WHITELIST_COOKIE <component> <regexp>`: Whitelist cookies where
+ `<component>` matches `<regexp>`. `<component>` is one of `domain`,
+ `path`, `name`, `value`, `scheme` or `expires`.
### COMMAND LINE ARGUMENTS
diff --git a/examples/data/plugins/cookies.py b/examples/data/plugins/cookies.py
index 8fb9b32..387e820 100644
--- a/examples/data/plugins/cookies.py
+++ b/examples/data/plugins/cookies.py
@@ -4,6 +4,7 @@
from collections import defaultdict
import os, re
+# these are symbolic names for the components of the cookie tuple
symbolic = {'domain': 0, 'path':1, 'name':2, 'value':3, 'scheme':4, 'expires':5}
_splitquoted = re.compile("( |\\\".*?\\\"|'.*?')")
@@ -90,11 +91,22 @@ xdg_data_home = os.environ.get('XDG_DATA_HOME', os.path.join(os.environ['HOME'],
DefaultStore = TextStore(os.path.join(xdg_data_home, 'uzbl/cookies.txt'))
SessionStore = TextStore(os.path.join(xdg_data_home, 'uzbl/session-cookies.txt'))
-def accept_cookie(uzbl, cookie):
- for component, match in uzbl.cookie_blacklist:
+def match_list(_list, cookie):
+ for component, match in _list:
if match(cookie[component]) is not None:
- return False
- return True
+ return True
+ return False
+
+# accept a cookie only when:
+# a. there is no whitelist and the cookie is in the blacklist
+# b. the cookie is in the whitelist and not in the blacklist
+def accept_cookie(uzbl, cookie):
+ if uzbl.cookie_whitelist:
+ if match_list(uzbl.cookie_whitelist, cookie):
+ return not match_list(uzbl.cookie_blacklist, cookie)
+ return False
+
+ return not match_list(uzbl.cookie_blacklist, cookie)
def expires_with_session(uzbl, cookie):
return cookie[5] == ''
@@ -114,9 +126,10 @@ def add_cookie(uzbl, cookie):
if accept_cookie(uzbl, splitted):
for u in get_recipents(uzbl):
u.send('add_cookie %s' % cookie)
-
+
get_store(uzbl, expires_with_session(uzbl, splitted)).add_cookie(cookie, splitted)
else:
+ logger.debug('cookie %r is blacklisted' % splitted)
uzbl.send('delete_cookie %s' % cookie)
def delete_cookie(uzbl, cookie):
@@ -130,21 +143,34 @@ def delete_cookie(uzbl, cookie):
for store in set([get_store(uzbl, session) for session in (True, False)]):
store.delete_cookie(cookie, splitted)
-def blacklist(uzbl, arg):
+# add a cookie matcher to a whitelist or a blacklist.
+# a matcher is a (component, re) tuple that matches a cookie when the
+# "component" part of the cookie matches the regular expression "re".
+# "component" is one of the keys defined in the variable "symbolic" above,
+# or the index of a component of a cookie tuple.
+def add_cookie_matcher(_list, arg):
component, regexp = splitquoted(arg)
try:
component = symbolic[component]
except KeyError:
component = int(component)
assert component <= 5
- uzbl.cookie_blacklist.append((component, re.compile(regexp).match))
+ _list.append((component, re.compile(regexp).match))
+
+def blacklist(uzbl, arg):
+ add_cookie_matcher(uzbl.cookie_blacklist, arg)
+
+def whitelist(uzbl, arg):
+ add_cookie_matcher(uzbl.cookie_whitelist, arg)
def init(uzbl):
connect_dict(uzbl, {
'ADD_COOKIE': add_cookie,
'DELETE_COOKIE': delete_cookie,
- 'BLACKLIST_COOKIE': blacklist
+ 'BLACKLIST_COOKIE': blacklist,
+ 'WHITELIST_COOKIE': whitelist
})
export_dict(uzbl, {
- 'cookie_blacklist' : []
+ 'cookie_blacklist' : [],
+ 'cookie_whitelist' : []
})