diff options
| author | Lars Magne Ingebrigtsen | 2012-04-10 03:57:45 +0200 |
|---|---|---|
| committer | Lars Magne Ingebrigtsen | 2012-04-10 03:57:45 +0200 |
| commit | 9ea49b28ab86d5207553d0827e1209276d03cd72 (patch) | |
| tree | e524549c208a3bd69af0a256127463332f1e607c /lisp | |
| parent | 263f20cd0a60e791e14ead267b5aefe7ad3e2dea (diff) | |
| download | emacs-9ea49b28ab86d5207553d0827e1209276d03cd72.tar.gz emacs-9ea49b28ab86d5207553d0827e1209276d03cd72.zip | |
Add a policy list of domains that url.el can set cookies for
* etc/publicsuffix.txt: New file.
* lisp/url/url-cookie.el (url-cookie-two-dot-domains): Remove.
(url-cookie-host-can-set-p): Use `url-domsuf-cookie-allowed-p'
instead of the variable above.
Fixes: debbugs:1401
Diffstat (limited to 'lisp')
| -rw-r--r-- | lisp/url/ChangeLog | 8 | ||||
| -rw-r--r-- | lisp/url/url-cookie.el | 45 | ||||
| -rw-r--r-- | lisp/url/url-domsuf.el | 96 |
3 files changed, 117 insertions, 32 deletions
diff --git a/lisp/url/ChangeLog b/lisp/url/ChangeLog index d6e25188c69..4c9635f8ecf 100644 --- a/lisp/url/ChangeLog +++ b/lisp/url/ChangeLog | |||
| @@ -1,3 +1,11 @@ | |||
| 1 | 2012-04-10 Lars Magne Ingebrigtsen <larsi@gnus.org> | ||
| 2 | |||
| 3 | * url-domsurf.el: New file (bug#1401). | ||
| 4 | |||
| 5 | * url-cookie.el (url-cookie-two-dot-domains): Remove. | ||
| 6 | (url-cookie-host-can-set-p): Use `url-domsuf-cookie-allowed-p' | ||
| 7 | instead of the variable above. | ||
| 8 | |||
| 1 | 2012-03-25 Lars Magne Ingebrigtsen <larsi@gnus.org> | 9 | 2012-03-25 Lars Magne Ingebrigtsen <larsi@gnus.org> |
| 2 | 10 | ||
| 3 | * url-queue.el (url-queue-kill-job): Check whether the buffer has | 11 | * url-queue.el (url-queue-kill-job): Check whether the buffer has |
diff --git a/lisp/url/url-cookie.el b/lisp/url/url-cookie.el index e6ff9bf7dea..aefe8fffd0a 100644 --- a/lisp/url/url-cookie.el +++ b/lisp/url/url-cookie.el | |||
| @@ -25,6 +25,7 @@ | |||
| 25 | 25 | ||
| 26 | (require 'url-util) | 26 | (require 'url-util) |
| 27 | (require 'url-parse) | 27 | (require 'url-parse) |
| 28 | (require 'url-domsuf) | ||
| 28 | 29 | ||
| 29 | (eval-when-compile (require 'cl)) ; defstruct | 30 | (eval-when-compile (require 'cl)) ; defstruct |
| 30 | 31 | ||
| @@ -211,14 +212,6 @@ telling Microsoft that." | |||
| 211 | (concat retval "\r\n") | 212 | (concat retval "\r\n") |
| 212 | ""))) | 213 | ""))) |
| 213 | 214 | ||
| 214 | (defvar url-cookie-two-dot-domains | ||
| 215 | (concat "\\.\\(" | ||
| 216 | (mapconcat 'identity (list "com" "edu" "net" "org" "gov" "mil" "int") | ||
| 217 | "\\|") | ||
| 218 | "\\)$") | ||
| 219 | "A regexp of top level domains that only require two matching | ||
| 220 | '.'s in the domain name in order to set a cookie.") | ||
| 221 | |||
| 222 | (defcustom url-cookie-trusted-urls nil | 215 | (defcustom url-cookie-trusted-urls nil |
| 223 | "A list of regular expressions matching URLs to always accept cookies from." | 216 | "A list of regular expressions matching URLs to always accept cookies from." |
| 224 | :type '(repeat regexp) | 217 | :type '(repeat regexp) |
| @@ -230,30 +223,18 @@ telling Microsoft that." | |||
| 230 | :group 'url-cookie) | 223 | :group 'url-cookie) |
| 231 | 224 | ||
| 232 | (defun url-cookie-host-can-set-p (host domain) | 225 | (defun url-cookie-host-can-set-p (host domain) |
| 233 | (let ((numdots 0) | 226 | (let ((last nil) |
| 234 | (last nil) | 227 | (case-fold-search t)) |
| 235 | (case-fold-search t) | 228 | (if (string= host domain) ; Apparently netscape lets you do this |
| 236 | (mindots 3)) | 229 | t |
| 237 | (while (setq last (string-match "\\." domain last)) | 230 | ;; Remove the dot from wildcard domains before matching. |
| 238 | (setq numdots (1+ numdots) | 231 | (when (eq ?. (aref domain 0)) |
| 239 | last (1+ last))) | 232 | (setq domain (substring domain 1))) |
| 240 | (if (string-match url-cookie-two-dot-domains domain) | 233 | (and (url-domsuf-cookie-allowed-p domain) |
| 241 | (setq mindots 2)) | 234 | ;; Need to check and make sure the host is actually _in_ the |
| 242 | (cond | 235 | ;; domain it wants to set a cookie for though. |
| 243 | ((string= host domain) ; Apparently netscape lets you do this | 236 | (string-match (concat (regexp-quote domain) |
| 244 | t) | 237 | "$") host))))) |
| 245 | ((>= numdots mindots) ; We have enough dots in domain name | ||
| 246 | ;; Need to check and make sure the host is actually _in_ the | ||
| 247 | ;; domain it wants to set a cookie for though. | ||
| 248 | (string-match (concat (regexp-quote | ||
| 249 | ;; Remove the dot from wildcard domains | ||
| 250 | ;; before matching. | ||
| 251 | (if (eq ?. (aref domain 0)) | ||
| 252 | (substring domain 1) | ||
| 253 | domain)) | ||
| 254 | "$") host)) | ||
| 255 | (t | ||
| 256 | nil)))) | ||
| 257 | 238 | ||
| 258 | (defun url-cookie-handle-set-cookie (str) | 239 | (defun url-cookie-handle-set-cookie (str) |
| 259 | (setq url-cookies-changed-since-last-save t) | 240 | (setq url-cookies-changed-since-last-save t) |
diff --git a/lisp/url/url-domsuf.el b/lisp/url/url-domsuf.el new file mode 100644 index 00000000000..3785a1c2fb2 --- /dev/null +++ b/lisp/url/url-domsuf.el | |||
| @@ -0,0 +1,96 @@ | |||
| 1 | ;;; url-domsuf.el --- Say what domain names can have cookies set. | ||
| 2 | |||
| 3 | ;; Copyright (C) 2011 Free Software Foundation, Inc. | ||
| 4 | |||
| 5 | ;; Keywords: comm, data, processes, hypermedia | ||
| 6 | |||
| 7 | ;; This file is part of GNU Emacs. | ||
| 8 | ;; | ||
| 9 | ;; GNU Emacs is free software: you can redistribute it and/or modify | ||
| 10 | ;; it under the terms of the GNU General Public License as published by | ||
| 11 | ;; the Free Software Foundation, either version 3 of the License, or | ||
| 12 | ;; (at your option) any later version. | ||
| 13 | |||
| 14 | ;; GNU Emacs is distributed in the hope that it will be useful, | ||
| 15 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 16 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 17 | ;; GNU General Public License for more details. | ||
| 18 | |||
| 19 | ;; You should have received a copy of the GNU General Public License | ||
| 20 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. | ||
| 21 | |||
| 22 | ;;; Commentary: | ||
| 23 | |||
| 24 | ;; The rules for what domains can have cookies set is defined here: | ||
| 25 | ;; http://publicsuffix.org/list/ | ||
| 26 | |||
| 27 | ;;; Code: | ||
| 28 | |||
| 29 | (defvar url-domsuf-domains nil) | ||
| 30 | |||
| 31 | (defun url-domsuf-parse-file () | ||
| 32 | (with-temp-buffer | ||
| 33 | (insert-file-contents | ||
| 34 | (expand-file-name "publicsuffix.txt" data-directory)) | ||
| 35 | (let ((domains nil) | ||
| 36 | domain exception) | ||
| 37 | (while (not (eobp)) | ||
| 38 | (when (not (looking-at "[/\n\t ]")) | ||
| 39 | ;; !pref.aichi.jp means that it's allowed. | ||
| 40 | (if (not (eq (following-char) ?!)) | ||
| 41 | (setq exception nil) | ||
| 42 | (setq exception t) | ||
| 43 | (forward-char 1)) | ||
| 44 | (setq domain (buffer-substring (point) (line-end-position))) | ||
| 45 | (cond | ||
| 46 | ((string-match "\\`\\*\\." domain) | ||
| 47 | (setq domain (substring domain 2)) | ||
| 48 | (push (cons domain (1+ (length (split-string domain "[.]")))) | ||
| 49 | domains)) | ||
| 50 | (exception | ||
| 51 | (push (cons domain t) domains)) | ||
| 52 | (t | ||
| 53 | (push (cons domain nil) domains)))) | ||
| 54 | (forward-line 1)) | ||
| 55 | (setq url-domsuf-domains (nreverse domains))))) | ||
| 56 | |||
| 57 | (defun url-domsuf-cookie-allowed-p (domain) | ||
| 58 | (unless url-domsuf-domains | ||
| 59 | (url-domsuf-parse-file)) | ||
| 60 | (let* ((allowedp t) | ||
| 61 | (domain-bits (split-string domain "[.]")) | ||
| 62 | (length (length domain-bits)) | ||
| 63 | (upper-domain (mapconcat 'identity (cdr domain-bits) ".")) | ||
| 64 | entry modifier) | ||
| 65 | (dolist (elem url-domsuf-domains) | ||
| 66 | (setq entry (car elem) | ||
| 67 | modifier (cdr elem)) | ||
| 68 | (cond | ||
| 69 | ;; "com" | ||
| 70 | ((and (null modifier) | ||
| 71 | (string= domain entry)) | ||
| 72 | (setq allowedp nil)) | ||
| 73 | ;; "!pref.hokkaido.jp" | ||
| 74 | ((and (eq modifier t) | ||
| 75 | (string= domain entry)) | ||
| 76 | (setq allowedp t)) | ||
| 77 | ;; "*.ar" | ||
| 78 | ((and (numberp modifier) | ||
| 79 | (= length modifier) | ||
| 80 | (string= entry upper-domain)) | ||
| 81 | (setq allowedp nil)))) | ||
| 82 | allowedp)) | ||
| 83 | |||
| 84 | ;; Tests: | ||
| 85 | |||
| 86 | ;; (url-domsuf-cookie-allowed-p "com") => nil | ||
| 87 | ;; (url-domsuf-cookie-allowed-p "foo.bar.ar") => t | ||
| 88 | ;; (url-domsuf-cookie-allowed-p "bar.ar") => nil | ||
| 89 | ;; (url-domsuf-cookie-allowed-p "co.uk") => nil | ||
| 90 | ;; (url-domsuf-cookie-allowed-p "foo.bar.hokkaido.jo") => t | ||
| 91 | ;; (url-domsuf-cookie-allowed-p "bar.hokkaido.jp") => nil | ||
| 92 | ;; (url-domsuf-cookie-allowed-p "pref.hokkaido.jp") => t | ||
| 93 | |||
| 94 | (provide 'url-domsuf) | ||
| 95 | |||
| 96 | ;;; url-domsuf.el ends here | ||