url-cookie.el 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. ;;; url-cookie.el --- URL cookie support
  2. ;; Copyright (C) 1996-1999, 2004-2012 Free Software Foundation, Inc.
  3. ;; Keywords: comm, data, processes, hypermedia
  4. ;; This file is part of GNU Emacs.
  5. ;;
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;;; Code:
  18. (require 'url-util)
  19. (require 'url-parse)
  20. (eval-when-compile (require 'cl)) ; defstruct
  21. (defgroup url-cookie nil
  22. "URL cookies."
  23. :prefix "url-"
  24. :prefix "url-cookie-"
  25. :group 'url)
  26. ;; A cookie is stored internally as a vector of 7 slots
  27. ;; [ url-cookie NAME VALUE EXPIRES LOCALPART DOMAIN SECURE ]
  28. (defstruct (url-cookie
  29. (:constructor url-cookie-create)
  30. (:copier nil)
  31. (:type vector)
  32. :named)
  33. name value expires localpart domain secure)
  34. (defvar url-cookie-storage nil "Where cookies are stored.")
  35. (defvar url-cookie-secure-storage nil "Where secure cookies are stored.")
  36. (defcustom url-cookie-file nil
  37. "File where cookies are stored on disk."
  38. :type '(choice (const :tag "Default" :value nil) file)
  39. :group 'url-file
  40. :group 'url-cookie)
  41. (defcustom url-cookie-confirmation nil
  42. "If non-nil, confirmation by the user is required to accept HTTP cookies."
  43. :type 'boolean
  44. :group 'url-cookie)
  45. (defcustom url-cookie-multiple-line nil
  46. "If nil, HTTP requests put all cookies for the server on one line.
  47. Some web servers, such as http://www.hotmail.com/, only accept cookies
  48. when they are on one line. This is broken behavior, but just try
  49. telling Microsoft that."
  50. :type 'boolean
  51. :group 'url-cookie)
  52. (defvar url-cookies-changed-since-last-save nil
  53. "Whether the cookies list has changed since the last save operation.")
  54. (defun url-cookie-parse-file (&optional fname)
  55. "Load FNAME, default `url-cookie-file'."
  56. ;; It's completely normal for the cookies file not to exist yet.
  57. (load (or fname url-cookie-file) t t))
  58. (defun url-cookie-clean-up (&optional secure)
  59. (let ((var (if secure 'url-cookie-secure-storage 'url-cookie-storage))
  60. new new-cookies)
  61. (dolist (cur (symbol-value var))
  62. (setq new-cookies nil)
  63. (dolist (cur-cookie (cdr cur))
  64. (or (not (url-cookie-p cur-cookie))
  65. (url-cookie-expired-p cur-cookie)
  66. (null (url-cookie-expires cur-cookie))
  67. (setq new-cookies (cons cur-cookie new-cookies))))
  68. (when new-cookies
  69. (setcdr cur new-cookies)
  70. (setq new (cons cur new))))
  71. (set var new)))
  72. (defun url-cookie-write-file (&optional fname)
  73. (when url-cookies-changed-since-last-save
  74. (or fname (setq fname (expand-file-name url-cookie-file)))
  75. (if (condition-case nil
  76. (progn
  77. (url-make-private-file fname)
  78. nil)
  79. (error t))
  80. (message "Error accessing cookie file `%s'" fname)
  81. (url-cookie-clean-up)
  82. (url-cookie-clean-up t)
  83. (with-temp-buffer
  84. (insert ";; Emacs-W3 HTTP cookies file\n"
  85. ";; Automatically generated file!!! DO NOT EDIT!!!\n\n"
  86. "(setq url-cookie-storage\n '")
  87. (pp url-cookie-storage (current-buffer))
  88. (insert ")\n(setq url-cookie-secure-storage\n '")
  89. (pp url-cookie-secure-storage (current-buffer))
  90. (insert ")\n")
  91. (insert " \n;; Local Variables:\n"
  92. ";; version-control: never\n"
  93. ";; no-byte-compile: t\n"
  94. ";; End:\n")
  95. (set (make-local-variable 'version-control) 'never)
  96. (write-file fname))
  97. (setq url-cookies-changed-since-last-save nil))))
  98. (defun url-cookie-store (name value &optional expires domain localpart secure)
  99. "Store a cookie."
  100. (let ((storage (if secure url-cookie-secure-storage url-cookie-storage))
  101. tmp found-domain)
  102. ;; First, look for a matching domain.
  103. (if (setq found-domain (assoc domain storage))
  104. ;; Need to either stick the new cookie in existing domain storage
  105. ;; or possibly replace an existing cookie if the names match.
  106. (unless (dolist (cur (setq storage (cdr found-domain)) tmp)
  107. (and (equal localpart (url-cookie-localpart cur))
  108. (equal name (url-cookie-name cur))
  109. (progn
  110. (setf (url-cookie-expires cur) expires)
  111. (setf (url-cookie-value cur) value)
  112. (setq tmp t))))
  113. ;; New cookie.
  114. (setcdr found-domain (cons
  115. (url-cookie-create :name name
  116. :value value
  117. :expires expires
  118. :domain domain
  119. :localpart localpart
  120. :secure secure)
  121. (cdr found-domain))))
  122. ;; Need to add a new top-level domain.
  123. (setq tmp (url-cookie-create :name name
  124. :value value
  125. :expires expires
  126. :domain domain
  127. :localpart localpart
  128. :secure secure))
  129. (cond (storage
  130. (setcdr storage (cons (list domain tmp) (cdr storage))))
  131. (secure
  132. (setq url-cookie-secure-storage (list (list domain tmp))))
  133. (t
  134. (setq url-cookie-storage (list (list domain tmp))))))))
  135. (defun url-cookie-expired-p (cookie)
  136. "Return non-nil if COOKIE is expired."
  137. (let ((exp (url-cookie-expires cookie)))
  138. (and (> (length exp) 0)
  139. (> (float-time) (float-time (date-to-time exp))))))
  140. (defun url-cookie-retrieve (host &optional localpart secure)
  141. "Retrieve all cookies for a specified HOST and LOCALPART."
  142. (let ((storage (if secure
  143. (append url-cookie-secure-storage url-cookie-storage)
  144. url-cookie-storage))
  145. (case-fold-search t)
  146. cookies retval localpart-match)
  147. (dolist (cur storage)
  148. (setq cookies (cdr cur))
  149. (if (and (car cur)
  150. (string-match
  151. (concat "^.*"
  152. (regexp-quote
  153. ;; Remove the dot from wildcard domains
  154. ;; before matching.
  155. (if (eq ?. (aref (car cur) 0))
  156. (substring (car cur) 1)
  157. (car cur)))
  158. "$") host))
  159. ;; The domains match - a possible hit!
  160. (dolist (cur cookies)
  161. (and (if (and (stringp
  162. (setq localpart-match (url-cookie-localpart cur)))
  163. (stringp localpart))
  164. (string-match (concat "^" (regexp-quote localpart-match))
  165. localpart)
  166. (equal localpart localpart-match))
  167. (not (url-cookie-expired-p cur))
  168. (setq retval (cons cur retval))))))
  169. retval))
  170. (defun url-cookie-generate-header-lines (host localpart secure)
  171. (let ((cookies (url-cookie-retrieve host localpart secure))
  172. retval chunk)
  173. ;; Have to sort this for sending most specific cookies first.
  174. (setq cookies (and cookies
  175. (sort cookies
  176. (lambda (x y)
  177. (> (length (url-cookie-localpart x))
  178. (length (url-cookie-localpart y)))))))
  179. (dolist (cur cookies)
  180. (setq chunk (format "%s=%s" (url-cookie-name cur) (url-cookie-value cur))
  181. retval (if (and url-cookie-multiple-line
  182. (< 80 (+ (length retval) (length chunk) 4)))
  183. (concat retval "\r\nCookie: " chunk)
  184. (if retval
  185. (concat retval "; " chunk)
  186. (concat "Cookie: " chunk)))))
  187. (if retval
  188. (concat retval "\r\n")
  189. "")))
  190. (defvar url-cookie-two-dot-domains
  191. (concat "\\.\\("
  192. (mapconcat 'identity (list "com" "edu" "net" "org" "gov" "mil" "int")
  193. "\\|")
  194. "\\)$")
  195. "A regexp of top level domains that only require two matching
  196. '.'s in the domain name in order to set a cookie.")
  197. (defcustom url-cookie-trusted-urls nil
  198. "A list of regular expressions matching URLs to always accept cookies from."
  199. :type '(repeat regexp)
  200. :group 'url-cookie)
  201. (defcustom url-cookie-untrusted-urls nil
  202. "A list of regular expressions matching URLs to never accept cookies from."
  203. :type '(repeat regexp)
  204. :group 'url-cookie)
  205. (defun url-cookie-host-can-set-p (host domain)
  206. (let ((numdots 0)
  207. (last nil)
  208. (case-fold-search t)
  209. (mindots 3))
  210. (while (setq last (string-match "\\." domain last))
  211. (setq numdots (1+ numdots)
  212. last (1+ last)))
  213. (if (string-match url-cookie-two-dot-domains domain)
  214. (setq mindots 2))
  215. (cond
  216. ((string= host domain) ; Apparently netscape lets you do this
  217. t)
  218. ((>= numdots mindots) ; We have enough dots in domain name
  219. ;; Need to check and make sure the host is actually _in_ the
  220. ;; domain it wants to set a cookie for though.
  221. (string-match (concat (regexp-quote
  222. ;; Remove the dot from wildcard domains
  223. ;; before matching.
  224. (if (eq ?. (aref domain 0))
  225. (substring domain 1)
  226. domain))
  227. "$") host))
  228. (t
  229. nil))))
  230. (defun url-cookie-handle-set-cookie (str)
  231. (setq url-cookies-changed-since-last-save t)
  232. (let* ((args (url-parse-args str t))
  233. (case-fold-search t)
  234. (secure (and (assoc-string "secure" args t) t))
  235. (domain (or (cdr-safe (assoc-string "domain" args t))
  236. (url-host url-current-object)))
  237. (current-url (url-view-url t))
  238. (trusted url-cookie-trusted-urls)
  239. (untrusted url-cookie-untrusted-urls)
  240. (expires (cdr-safe (assoc-string "expires" args t)))
  241. (localpart (or (cdr-safe (assoc-string "path" args t))
  242. (file-name-directory
  243. (url-filename url-current-object))))
  244. (rest nil))
  245. (dolist (this args)
  246. (or (member (downcase (car this)) '("secure" "domain" "expires" "path"))
  247. (setq rest (cons this rest))))
  248. ;; Sometimes we get dates that the timezone package cannot handle very
  249. ;; gracefully - take care of this here, instead of in url-cookie-expired-p
  250. ;; to speed things up.
  251. (and expires
  252. (string-match
  253. (concat "^[^,]+, +\\(..\\)-\\(...\\)-\\(..\\) +"
  254. "\\(..:..:..\\) +\\[*\\([^\]]+\\)\\]*$")
  255. expires)
  256. (setq expires (concat (match-string 1 expires) " "
  257. (match-string 2 expires) " "
  258. (match-string 3 expires) " "
  259. (match-string 4 expires) " ["
  260. (match-string 5 expires) "]")))
  261. ;; This one is for older Emacs/XEmacs variants that don't
  262. ;; understand this format without tenths of a second in it.
  263. ;; Wednesday, 30-Dec-2037 16:00:00 GMT
  264. ;; - vs -
  265. ;; Wednesday, 30-Dec-2037 16:00:00.00 GMT
  266. (and expires
  267. (string-match
  268. "\\([0-9]+\\)-\\([A-Za-z]+\\)-\\([0-9]+\\)[ \t]+\\([0-9]+:[0-9]+:[0-9]+\\)\\(\\.[0-9]+\\)*[ \t]+\\([-+a-zA-Z0-9]+\\)"
  269. expires)
  270. (setq expires (concat (match-string 1 expires) "-" ; day
  271. (match-string 2 expires) "-" ; month
  272. (match-string 3 expires) " " ; year
  273. (match-string 4 expires) ".00 " ; hour:minutes:seconds
  274. (match-string 6 expires)))) ":" ; timezone
  275. (while (consp trusted)
  276. (if (string-match (car trusted) current-url)
  277. (setq trusted (- (match-end 0) (match-beginning 0)))
  278. (pop trusted)))
  279. (while (consp untrusted)
  280. (if (string-match (car untrusted) current-url)
  281. (setq untrusted (- (match-end 0) (match-beginning 0)))
  282. (pop untrusted)))
  283. (and trusted untrusted
  284. ;; Choose the more specific match.
  285. (set (if (> trusted untrusted) 'untrusted 'trusted) nil))
  286. (cond
  287. (untrusted
  288. ;; The site was explicitly marked as untrusted by the user.
  289. nil)
  290. ((or (eq url-privacy-level 'paranoid)
  291. (and (listp url-privacy-level) (memq 'cookies url-privacy-level)))
  292. ;; User never wants cookies.
  293. nil)
  294. ((and url-cookie-confirmation
  295. (not trusted)
  296. (save-window-excursion
  297. (with-output-to-temp-buffer "*Cookie Warning*"
  298. (dolist (x rest)
  299. (princ (format "%s - %s" (car x) (cdr x)))))
  300. (prog1
  301. (not (funcall url-confirmation-func
  302. (format "Allow %s to set these cookies? "
  303. (url-host url-current-object))))
  304. (if (get-buffer "*Cookie Warning*")
  305. (kill-buffer "*Cookie Warning*")))))
  306. ;; User wants to be asked, and declined.
  307. nil)
  308. ((url-cookie-host-can-set-p (url-host url-current-object) domain)
  309. ;; Cookie is accepted by the user, and passes our security checks.
  310. (dolist (cur rest)
  311. (url-cookie-store (car cur) (cdr cur) expires domain localpart secure)))
  312. (t
  313. (url-lazy-message "%s tried to set a cookie for domain %s - rejected."
  314. (url-host url-current-object) domain)))))
  315. (defvar url-cookie-timer nil)
  316. (defcustom url-cookie-save-interval 3600
  317. "The number of seconds between automatic saves of cookies.
  318. Default is 1 hour. Note that if you change this variable outside of
  319. the `customize' interface after `url-do-setup' has been run, you need
  320. to run the `url-cookie-setup-save-timer' function manually."
  321. :set #'(lambda (var val)
  322. (set-default var val)
  323. (if (bound-and-true-p url-setup-done)
  324. (url-cookie-setup-save-timer)))
  325. :type 'integer
  326. :group 'url-cookie)
  327. (defun url-cookie-setup-save-timer ()
  328. "Reset the cookie saver timer."
  329. (interactive)
  330. (ignore-errors (cancel-timer url-cookie-timer))
  331. (setq url-cookie-timer nil)
  332. (if url-cookie-save-interval
  333. (setq url-cookie-timer (run-at-time url-cookie-save-interval
  334. url-cookie-save-interval
  335. #'url-cookie-write-file))))
  336. (provide 'url-cookie)
  337. ;;; url-cookie.el ends here