Add strict_proxy option to proxy links to images

Default true.
This commit is contained in:
Cadence Ember 2022-09-08 00:16:24 +12:00
parent 3505925f7e
commit 78399a3474
Signed by untrusted user: cadence
GPG key ID: BC1C2C61CF521B17
2 changed files with 29 additions and 9 deletions

View file

@ -20,7 +20,8 @@
(canonical_origin . "") (canonical_origin . "")
(debug . "false") (debug . "false")
(instance_is_official . "false") ; please don't turn this on, or you will make me very upset (instance_is_official . "false") ; please don't turn this on, or you will make me very upset
(port . "10416"))) (port . "10416")
(strict_proxy . "true")))
(define config (define config
(make-hasheq (make-hasheq

View file

@ -30,9 +30,9 @@
(aside (@ (role "region") (class "portable-infobox pi-theme-wikia pi-layout-default")) (aside (@ (role "region") (class "portable-infobox pi-theme-wikia pi-layout-default"))
(h2 (@ (class "pi-item pi-title") (data-source "title")) (h2 (@ (class "pi-item pi-title") (data-source "title"))
"Infobox Title") "Infobox Title")
(figure (@ (class "pi-item pi-image") (data-sourec "image")) (figure (@ (class "pi-item pi-image") (data-source "image"))
(a (@ (href "https://static.wiki.nocookie.net/nice-image.png") (class "image image-thumbnail") (title "")) (a (@ (href "https://static.wikia.nocookie.net/nice-image.png") (class "image image-thumbnail") (title ""))
(img (@ (src "https://static.wiki.nocookie.net/nice-image-thumbnail.png") (class "pi-image-thumbnail"))))) (img (@ (src "https://static.wikia.nocookie.net/nice-image-thumbnail.png") (class "pi-image-thumbnail")))))
(div (@ (class "pi-item pi-data") (data-source "description")) (div (@ (class "pi-item pi-data") (data-source "description"))
(h3 (@ (class "pi-data-label")) (h3 (@ (class "pi-data-label"))
"Description") "Description")
@ -64,7 +64,7 @@
(check-equal? (preprocess-html-wiki "<figure class=\"thumb tright\" style=\"width: 150px\"><a class=\"image\"><img></a><noscript><a><img></a></noscript><figcaption class=\"thumbcaption\"> <p class=\"caption\">Caption text.</p></figcaption></figure>") (check-equal? (preprocess-html-wiki "<figure class=\"thumb tright\" style=\"width: 150px\"><a class=\"image\"><img></a><noscript><a><img></a></noscript><figcaption class=\"thumbcaption\"> <p class=\"caption\">Caption text.</p></figcaption></figure>")
"<figure class=\"thumb tright\" style=\"width: 150px\"><a class=\"image\"><img></a><noscript><a><img></a></noscript><figcaption class=\"thumbcaption\"><span class=\"caption\">Caption text.</span></figcaption></figure>")) "<figure class=\"thumb tright\" style=\"width: 150px\"><a class=\"image\"><img></a><noscript><a><img></a></noscript><figcaption class=\"thumbcaption\"><span class=\"caption\">Caption text.</span></figcaption></figure>"))
(define (update-tree-wiki tree wikiname) (define (update-tree-wiki tree wikiname #:strict-proxy? strict-proxy?)
(update-tree (update-tree
(λ (element element-type attributes children) (λ (element element-type attributes children)
;; replace whole element? ;; replace whole element?
@ -142,7 +142,7 @@
(curry u (curry u
(λ (v) (and (eq? element-type 'a) (λ (v) (and (eq? element-type 'a)
(has-class? "image" v))) (has-class? "image" v)))
(λ (v) (dict-update attributes 'rel (λ (s) (λ (v) (dict-update v 'rel (λ (s)
(list (string-append (car s) " noreferrer"))) (list (string-append (car s) " noreferrer")))
'("")))) '(""))))
; proxy images from inline styles ; proxy images from inline styles
@ -154,6 +154,13 @@
"url(" "url("
(u-proxy-url url) (u-proxy-url url)
")"))))) ")")))))
; and also their links, if strict-proxy is set
(curry u
(λ (v)
(and strict-proxy?
(eq? element-type 'a)
(has-class? "image-thumbnail" v)))
(λ (v) (attribute-maybe-update 'href u-proxy-url v)))
; proxy images from src attributes ; proxy images from src attributes
(curry attribute-maybe-update 'src u-proxy-url) (curry attribute-maybe-update 'src u-proxy-url)
; don't lazyload images ; don't lazyload images
@ -176,7 +183,7 @@
children))])) children))]))
tree)) tree))
(module+ test (module+ test
(define transformed (update-tree-wiki wiki-document "test")) (define transformed (update-tree-wiki wiki-document "test" #:strict-proxy? #t))
; check that wikilinks are changed to be local ; check that wikilinks are changed to be local
(check-equal? (get-attribute 'href (bits->attributes (check-equal? (get-attribute 'href (bits->attributes
((query-selector ((query-selector
@ -201,7 +208,19 @@
(check-equal? (let* ([alternative ((query-selector (λ (t a c) (has-class? "iframe-alternative" a)) transformed))] (check-equal? (let* ([alternative ((query-selector (λ (t a c) (has-class? "iframe-alternative" a)) transformed))]
[link ((query-selector (λ (t a c) (eq? t 'a)) alternative))]) [link ((query-selector (λ (t a c) (eq? t 'a)) alternative))])
(get-attribute 'href (bits->attributes link))) (get-attribute 'href (bits->attributes link)))
"https://example.com/iframe-src")) "https://example.com/iframe-src")
; check that images are proxied
(check-equal? (get-attribute 'src (bits->attributes
((query-selector
(λ (t a c) (eq? t 'img))
transformed))))
"/proxy?dest=https%3A%2F%2Fstatic.wikia.nocookie.net%2Fnice-image-thumbnail.png")
; check that links to images are proxied
(check-equal? (get-attribute 'href (bits->attributes
((query-selector
(λ (t a c) (and (eq? t 'a) (has-class? "image-thumbnail" a)))
transformed))))
"/proxy?dest=https%3A%2F%2Fstatic.wikia.nocookie.net%2Fnice-image.png"))
(define (page-wiki req) (define (page-wiki req)
(define wikiname (path/param-path (first (url-path (request-uri req))))) (define wikiname (path/param-path (first (url-path (request-uri req)))))
@ -230,7 +249,7 @@
(next-dispatcher) (next-dispatcher)
(response-handler (response-handler
(define body (define body
(generate-wiki-page source-url wikiname title (update-tree-wiki page wikiname))) (generate-wiki-page source-url wikiname title (update-tree-wiki page wikiname #:strict-proxy? (config-true? 'strict_proxy))))
(define redirect-msg ((query-selector (attribute-selector 'class "redirectMsg") body))) (define redirect-msg ((query-selector (attribute-selector 'class "redirectMsg") body)))
(define headers (if redirect-msg (define headers (if redirect-msg
(let* ([dest (get-attribute 'href (bits->attributes ((query-selector (λ (t a c) (eq? t 'a)) redirect-msg))))] (let* ([dest (get-attribute 'href (bits->attributes ((query-selector (λ (t a c) (eq? t 'a)) redirect-msg))))]