aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--doc/misc/eww.texi19
-rw-r--r--etc/NEWS6
-rw-r--r--lisp/net/shr.el45
-rw-r--r--test/lisp/net/shr-tests.el13
4 files changed, 75 insertions, 8 deletions
diff --git a/doc/misc/eww.texi b/doc/misc/eww.texi
index 315b4b0194d..b8821cbc299 100644
--- a/doc/misc/eww.texi
+++ b/doc/misc/eww.texi
@@ -217,9 +217,22 @@ in an external browser by customizing
217@findex url-cookie-list 217@findex url-cookie-list
218@kindex C 218@kindex C
219@cindex Cookies 219@cindex Cookies
220 EWW handles cookies through the @ref{Top, url package, ,url}. 220 EWW handles cookies through the @ref{Top, url package, ,url}
221You can list existing cookies with @kbd{C} (@code{url-cookie-list}). 221package. You can list existing cookies with @kbd{C}
222For details about the Cookie handling @xref{Cookies,,,url}. 222(@code{url-cookie-list}). For details about the Cookie handling
223@xref{Cookies,,,url}.
224
225@vindex shr-cookie-policy
226 Many @acronym{HTML} pages have images embedded in them, and EWW will
227download most these by default. When fetching images, cookies can be
228sent and received, and these can be used to track users. To control
229when to send cookies when retrieving these images, the
230@code{shr-cookie-policy} variable can be used. The default value,
231@code{same-origin}, means that EWW will only send cookies when
232fetching images that originate from the same source as the
233@acronym{HTML} page. @code{nil} means ``never send cookies when
234retrieving these images'' and @code{t} means ``always send cookies
235when retrieving these images''.
223 236
224@vindex eww-header-line-format 237@vindex eww-header-line-format
225@cindex Header 238@cindex Header
diff --git a/etc/NEWS b/etc/NEWS
index 3f38f9f4a12..50956f4082c 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -1118,6 +1118,12 @@ The variable to use instead to alter text to be sent is now
1118** eww/shr 1118** eww/shr
1119 1119
1120+++ 1120+++
1121*** The new variable 'shr-cookie-policy' can be used to control when
1122to use cookies when fetching embedded images. The default is to use
1123them when the images are from the same domain as the main HTML
1124document.
1125
1126+++
1121*** The 'eww' command can now create a new EWW buffer. 1127*** The 'eww' command can now create a new EWW buffer.
1122Invoking the command with a prefix argument will cause it to create a 1128Invoking the command with a prefix argument will cause it to create a
1123new EWW buffer for the URL instead of reusing the default one. 1129new EWW buffer for the URL instead of reusing the default one.
diff --git a/lisp/net/shr.el b/lisp/net/shr.el
index 2e4f7fa5c61..63988d01c88 100644
--- a/lisp/net/shr.el
+++ b/lisp/net/shr.el
@@ -39,6 +39,7 @@
39(require 'svg) 39(require 'svg)
40(require 'image) 40(require 'image)
41(require 'puny) 41(require 'puny)
42(require 'url-cookie)
42(require 'text-property-search) 43(require 'text-property-search)
43 44
44(defgroup shr nil 45(defgroup shr nil
@@ -111,6 +112,16 @@ Alternative suggestions are:
111 :version "24.4" 112 :version "24.4"
112 :type 'string) 113 :type 'string)
113 114
115(defcustom shr-cookie-policy 'same-origin
116 "When to use cookies when fetching dependent data like images.
117If t, always use cookies. If nil, never use cookies. If
118`same-origin', use cookies if the dependent data comes from the
119same domain as the main data."
120 :type '(choice (const :tag "Always use cookies" t)
121 (const :tag "Never use cookies" nil)
122 (const :tag "Use cookies for same domain" same-origin))
123 :version "27.1")
124
114(define-obsolete-variable-alias 'shr-external-browser 125(define-obsolete-variable-alias 'shr-external-browser
115 'browse-url-secondary-browser-function "27.1") 126 'browse-url-secondary-browser-function "27.1")
116 127
@@ -333,7 +344,7 @@ called."
333 ;; Remove common tracking junk from the URL. 344 ;; Remove common tracking junk from the URL.
334 (funcall cont (replace-regexp-in-string 345 (funcall cont (replace-regexp-in-string
335 ".utm_.*" "" destination))))) 346 ".utm_.*" "" destination)))))
336 nil t)) 347 nil t t))
337 348
338(defun shr-probe-and-copy-url (url) 349(defun shr-probe-and-copy-url (url)
339 "Copy the URL under point to the kill ring. 350 "Copy the URL under point to the kill ring.
@@ -427,7 +438,7 @@ the URL of the image to the kill buffer instead."
427 (message "Inserting %s..." url) 438 (message "Inserting %s..." url)
428 (url-retrieve url 'shr-image-fetched 439 (url-retrieve url 'shr-image-fetched
429 (list (current-buffer) (1- (point)) (point-marker)) 440 (list (current-buffer) (1- (point)) (point-marker))
430 t t)))) 441 t))))
431 442
432(defun shr-zoom-image () 443(defun shr-zoom-image ()
433 "Toggle the image size. 444 "Toggle the image size.
@@ -985,8 +996,7 @@ the mouse click event."
985 (if (not url) 996 (if (not url)
986 (message "No link under point") 997 (message "No link under point")
987 (url-retrieve (shr-encode-url url) 998 (url-retrieve (shr-encode-url url)
988 'shr-store-contents (list url directory) 999 'shr-store-contents (list url directory)))))
989 nil t))))
990 1000
991(defun shr-store-contents (status url directory) 1001(defun shr-store-contents (status url directory)
992 (unless (plist-get status :error) 1002 (unless (plist-get status :error)
@@ -1658,7 +1668,8 @@ The preference is a float determined from `shr-prefer-media-type'."
1658 (shr-encode-url url) 'shr-image-fetched 1668 (shr-encode-url url) 'shr-image-fetched
1659 (list (current-buffer) start (set-marker (make-marker) (point)) 1669 (list (current-buffer) start (set-marker (make-marker) (point))
1660 (list :width width :height height)) 1670 (list :width width :height height))
1661 t t))) 1671 t
1672 (not (shr--use-cookies-p url shr-base)))))
1662 (when (zerop shr-table-depth) ;; We are not in a table. 1673 (when (zerop shr-table-depth) ;; We are not in a table.
1663 (put-text-property start (point) 'keymap shr-image-map) 1674 (put-text-property start (point) 'keymap shr-image-map)
1664 (put-text-property start (point) 'shr-alt alt) 1675 (put-text-property start (point) 'shr-alt alt)
@@ -1669,6 +1680,30 @@ The preference is a float determined from `shr-prefer-media-type'."
1669 (shr-fill-text 1680 (shr-fill-text
1670 (or (dom-attr dom 'title) alt)))))))) 1681 (or (dom-attr dom 'title) alt))))))))
1671 1682
1683(defun shr--use-cookies-p (url base)
1684 "Say whether to use cookies when fetching URL (typically an image).
1685BASE is the URL of the HTML being rendered."
1686 (cond
1687 ((null base)
1688 ;; Disallow cookies if we don't know what the base is.
1689 nil)
1690 ((eq shr-cookie-policy 'same-origin)
1691 (let ((url-host (url-host (url-generic-parse-url url)))
1692 (base-host (split-string
1693 (url-host (url-generic-parse-url (car base)))
1694 "\\.")))
1695 ;; We allow cookies if it's for any of the sibling domains (that
1696 ;; we're allowed to set cookies for). Determine that by going
1697 ;; "upwards" in the base domain name.
1698 (cl-loop while base-host
1699 when (url-cookie-host-can-set-p
1700 url-host (mapconcat #'identity base-host "."))
1701 return t
1702 do (pop base-host)
1703 finally (return nil))))
1704 (t
1705 shr-cookie-policy)))
1706
1672(defun shr--preferred-image (dom) 1707(defun shr--preferred-image (dom)
1673 (let ((srcset (dom-attr dom 'srcset)) 1708 (let ((srcset (dom-attr dom 'srcset))
1674 (frame-width (frame-pixel-width)) 1709 (frame-width (frame-pixel-width))
diff --git a/test/lisp/net/shr-tests.el b/test/lisp/net/shr-tests.el
index dd820e2d9f4..c3be36439e0 100644
--- a/test/lisp/net/shr-tests.el
+++ b/test/lisp/net/shr-tests.el
@@ -53,6 +53,19 @@
53 (unless (equal (car result) (cdr result)) 53 (unless (equal (car result) (cdr result))
54 (should (not (list name (car result) (cdr result)))))))) 54 (should (not (list name (car result) (cdr result))))))))
55 55
56(ert-deftest use-cookies ()
57 (let ((shr-cookie-policy 'same-origin))
58 (should
59 (shr--use-cookies-p "http://images.fsf.org" '("http://www.fsf.org")))
60 (should
61 (shr--use-cookies-p "http://www.fsf.org" '("https://www.fsf.org")))
62 (should
63 (shr--use-cookies-p "http://www.fsf.org" '("https://www.fsf.org")))
64 (should
65 (shr--use-cookies-p "http://www.fsf.org" '("http://fsf.org")))
66 (should-not
67 (shr--use-cookies-p "http://www.gnu.org" '("http://www.fsf.org")))))
68
56(require 'shr) 69(require 'shr)
57 70
58;;; shr-stream-tests.el ends here 71;;; shr-stream-tests.el ends here