From 78399a3474d23289d7e4872e4f5a6d1ebf7eb13e Mon Sep 17 00:00:00 2001 From: Cadence Ember Date: Thu, 8 Sep 2022 00:16:24 +1200 Subject: [PATCH] Add strict_proxy option to proxy links to images Default true. --- src/config.rkt | 3 ++- src/page-wiki.rkt | 35 +++++++++++++++++++++++++++-------- 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/src/config.rkt b/src/config.rkt index 38433f9..4dc133e 100644 --- a/src/config.rkt +++ b/src/config.rkt @@ -20,7 +20,8 @@ (canonical_origin . "") (debug . "false") (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 (make-hasheq diff --git a/src/page-wiki.rkt b/src/page-wiki.rkt index f09900f..c770abc 100644 --- a/src/page-wiki.rkt +++ b/src/page-wiki.rkt @@ -30,9 +30,9 @@ (aside (@ (role "region") (class "portable-infobox pi-theme-wikia pi-layout-default")) (h2 (@ (class "pi-item pi-title") (data-source "title")) "Infobox Title") - (figure (@ (class "pi-item pi-image") (data-sourec "image")) - (a (@ (href "https://static.wiki.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"))))) + (figure (@ (class "pi-item pi-image") (data-source "image")) + (a (@ (href "https://static.wikia.nocookie.net/nice-image.png") (class "image image-thumbnail") (title "")) + (img (@ (src "https://static.wikia.nocookie.net/nice-image-thumbnail.png") (class "pi-image-thumbnail"))))) (div (@ (class "pi-item pi-data") (data-source "description")) (h3 (@ (class "pi-data-label")) "Description") @@ -64,7 +64,7 @@ (check-equal? (preprocess-html-wiki "

Caption text.

") "
Caption text.
")) -(define (update-tree-wiki tree wikiname) +(define (update-tree-wiki tree wikiname #:strict-proxy? strict-proxy?) (update-tree (λ (element element-type attributes children) ;; replace whole element? @@ -142,7 +142,7 @@ (curry u (λ (v) (and (eq? element-type 'a) (has-class? "image" v))) - (λ (v) (dict-update attributes 'rel (λ (s) + (λ (v) (dict-update v 'rel (λ (s) (list (string-append (car s) " noreferrer"))) '("")))) ; proxy images from inline styles @@ -154,6 +154,13 @@ "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 (curry attribute-maybe-update 'src u-proxy-url) ; don't lazyload images @@ -176,7 +183,7 @@ children))])) tree)) (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-equal? (get-attribute 'href (bits->attributes ((query-selector @@ -201,7 +208,19 @@ (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))]) (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 wikiname (path/param-path (first (url-path (request-uri req))))) @@ -230,7 +249,7 @@ (next-dispatcher) (response-handler (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 headers (if redirect-msg (let* ([dest (get-attribute 'href (bits->attributes ((query-selector (λ (t a c) (eq? t 'a)) redirect-msg))))]