From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id 9FF8381D7C for ; Fri, 26 Nov 2021 08:52:10 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 8D055157C0 for ; Fri, 26 Nov 2021 08:51:40 +0100 (CET) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [94.136.29.106]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS id D938C157B0 for ; Fri, 26 Nov 2021 08:51:38 +0100 (CET) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id A80A943C19; Fri, 26 Nov 2021 08:51:38 +0100 (CET) Message-ID: <3627c843-b11c-7a8e-1235-9d203558df06@proxmox.com> Date: Fri, 26 Nov 2021 08:51:37 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:95.0) Gecko/20100101 Thunderbird/95.0 Content-Language: en-US To: Thomas Lamprecht , pmg-devel@lists.proxmox.com References: <20211125141441.1383250-1-d.csapak@proxmox.com> <850e6a4c-b370-614e-6b2a-819a06d9fbb8@proxmox.com> From: Dominik Csapak In-Reply-To: <850e6a4c-b370-614e-6b2a-819a06d9fbb8@proxmox.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-SPAM-LEVEL: Spam detection results: 0 AWL 1.187 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment NICE_REPLY_A -1.993 Looks like a legit reply (A) SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [htmlmail.pm] Subject: Re: [pmg-devel] [PATCH pmg-api v2] fix #3734: scrub 'url' from style tags/attributes X-BeenThere: pmg-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Mail Gateway development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 26 Nov 2021 07:52:10 -0000 On 11/25/21 18:26, Thomas Lamprecht wrote: > On 25.11.21 15:14, Dominik Csapak wrote: >> if 'view images' for the quarantine is disabled, it is expected that >> *no* images will be loaded. but in addition to img (src/href/etc.) >> also css can load external images via the 'url' directive >> >> since html scrubber does not parse/iterate over css, we simply remove >> the url+protocol part of those tags/attributes. this technically leaves behind >> invalid css, but the browsers should cope with that. >> (we cannot 'cleanly' remove without much more effort because of quoting) >> >> also we have to scrub the style tags in 'dump_html' since HTML::Scrubber >> does not have a way to modify the *content* of a tag, only the >> attributes... >> > > I found two issues (see inline), I got fully commited followups here, but I did > not push that yet so we can quick check tomorrow if its ok for you and that I > missed nothing else. > >> Signed-off-by: Dominik Csapak >> --- >> changes from v1: >> * replace url with ___ and protocol:// with _ instead of removing >> * move sub out and use the reference >> * always pass $cid_hash and only use it in the function when >> $view_images is set >> * improve comment to show what 'dump_html' does >> >> @thomas: a note to our off-list discussion regarding url-encoding the >> protocol: you *could* do it, but the browser does not recognize it as >> a protocol and interprets it as a relative url, so we're safe on >> this regard > > thx for checking! > >> >> src/PMG/HTMLMail.pm | 31 +++++++++++++++++++++++++++---- >> 1 file changed, 27 insertions(+), 4 deletions(-) >> >> diff --git a/src/PMG/HTMLMail.pm b/src/PMG/HTMLMail.pm >> index b69a596..c5c94bf 100644 >> --- a/src/PMG/HTMLMail.pm >> +++ b/src/PMG/HTMLMail.pm >> @@ -15,8 +15,26 @@ use HTML::Scrubber; >> use PMG::Utils; >> use PMG::MIMEUtils; >> >> +# $value is a ref to a string scalar >> +my sub remove_urls { >> + my ($value) = @_; > > $$value can be undef here, so I added a > return if !defined $$value; > > to avoid a ugly warning like: > > pmgproxy[164923]: Use of uninitialized value in substitution (s///) at /usr/share/perl5/PMG/HTMLMail.pm line 22. > pmgproxy[164923]: Use of uninitialized value in substitution (s///) at /usr/share/perl5/PMG/HTMLMail.pm line 25. > > every time one loads a mail where this happens. make sense > >> -- 8< snip 8< -- > >> @@ -137,7 +159,7 @@ sub getscrubber { >> span => 1, >> src => $viewimages ? qr{^(?!(?:java)?script)}i : 0, >> start => 1, >> - style => 1, >> + style => $viewimages ? 1 : \remove_urls_from_attr, > > this actually does not works as expected, to get the callback functionality we need > to set it at the "rules" one, not here at the "default" one, which really just is the > boolean default for that attribute type, and setting it to an code ref-makes may call > it once but due to returning undef it just plainly disables the tag, which is way more > scrubbing than we want to achieve. > ok so you're right in that it does not work correctly here (idk why i did not catch that), but we can use a sub here the 'default' contains the 'default' rules, else the regexes would also not work what prevents it from working here that it does not like the reference to the function, so making it a my $remove_urls_from_attr = sub {}; and giving '$remove_urls_from_attr' here works as expected. (i'll send a v3) On 11/26/21 08:28, Thomas Lamprecht wrote: > On 25.11.21 15:14, Dominik Csapak wrote: >> if 'view images' for the quarantine is disabled, it is expected that >> *no* images will be loaded. but in addition to img (src/href/etc.) >> also css can load external images via the 'url' directive >> >> since html scrubber does not parse/iterate over css, we simply remove >> the url+protocol part of those tags/attributes. this technically leaves behind >> invalid css, but the browsers should cope with that. >> (we cannot 'cleanly' remove without much more effort because of quoting) >> >> also we have to scrub the style tags in 'dump_html' since HTML::Scrubber >> does not have a way to modify the *content* of a tag, only the >> attributes... >> >> Signed-off-by: Dominik Csapak >> --- >> changes from v1: >> * replace url with ___ and protocol:// with _ instead of removing >> * move sub out and use the reference >> * always pass $cid_hash and only use it in the function when >> $view_images is set >> * improve comment to show what 'dump_html' does >> >> @thomas: a note to our off-list discussion regarding url-encoding the >> protocol: you *could* do it, but the browser does not recognize it as >> a protocol and interprets it as a relative url, so we're safe on >> this regard >> > > Another option: Setting the content security policy: > > For this call we could use: > $resp->header("Content-Security-Policy", "default-src 'self'; style-src 'self' 'unsafe-inline';"); > > Maybe even; > "Content-Security-Policy", "default-src 'none'; style-src 'unsafe-inline';" > > That works out quite well here. > > In the long run the CSP is something we could evaluate in general, at least for API > calls, as only (mostly?) those contain dynamic, sometimes user/foreign controlled input. > > If we would like to set a CSP for everything we'd need something like: > > $resp->header("Content-Security-Policy", "default-src 'self'; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval'; img-src 'self' data:;"); > > to cover it, and ideally switch from 'unsafe-inline' to nonce/sha approach of > whitelisting, but in any case, nothing that we'd want to rush out now. > > Also, doing both is an option, avoiding requests in the first place, so no scary > errors in the browser console, and the CSP, for really just this the *quarantine/content > API call, as safety net.. > yes imho setting the csp is also good, but requires patching http-server too so that we can set the header there..