From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-Status: No, score=-4.0 required=3.0 tests=ALL_TRUSTED,BAYES_00 shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id 7D3061FA12; Mon, 23 Nov 2020 14:15:35 +0000 (UTC) Date: Mon, 23 Nov 2020 14:15:35 +0000 From: Eric Wong To: meta@public-inbox.org Cc: Leah Neukirchen Subject: [PATCH v2] wwwattach: prevent deep-linking via Referer match Message-ID: <20201123141535.GA15950@dcvr> References: <87mtztx9sr.fsf@vuxu.org> <20201107203913.GA8294@dcvr> <87imagyap9.fsf@vuxu.org> <20201108074917.GA29704@dcvr> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20201108074917.GA29704@dcvr> List-Id: This prevents ` --- Deployed on public-inbox.org (http://ou63pmih66umazou.onion/) but not others. lib/PublicInbox/WwwAttach.pm | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/lib/PublicInbox/WwwAttach.pm b/lib/PublicInbox/WwwAttach.pm index 0b2cda90..09c66d02 100644 --- a/lib/PublicInbox/WwwAttach.pm +++ b/lib/PublicInbox/WwwAttach.pm @@ -9,6 +9,22 @@ use bytes (); # only for bytes::length use PublicInbox::EmlContentFoo qw(parse_content_type); use PublicInbox::Eml; +sub referer_match ($) { + my ($ctx) = @_; + my $env = $ctx->{env}; + my $referer = $env->{HTTP_REFERER} // ''; + return 1 if $referer eq ''; # no referer is always OK for wget/curl + + # prevent deep-linking from other domains on some browsers (Firefox) + # n.b.: $ctx->{-inbox}->base_url($env) with INBOX_URL won't work + # with dillo, we can only match "$url_scheme://$HTTP_HOST/" without + # path components + my $base_url = $env->{'psgi.url_scheme'} . '://' . + ($env->{HTTP_HOST} // + "$env->{SERVER_NAME}:$env->{SERVER_PORT}") . '/'; + index($referer, $base_url) == 0; +} + sub get_attach_i { # ->each_part callback my ($part, $depth, $idx) = @{$_[0]}; my $ctx = $_[1]; @@ -28,8 +44,14 @@ sub get_attach_i { # ->each_part callback $ctx->{env}); $part = $ctx->zflush($part->body); } else { # TODO: allow user to configure safe types - $res->[1]->[1] = 'application/octet-stream'; - $part = $part->body; + if (referer_match($ctx)) { + $res->[1]->[1] = 'application/octet-stream'; + $part = $part->body; + } else { + $res->[0] = 403; + $res->[1]->[1] = 'text/plain'; + $part = "Deep-linking prevented\n"; + } } push @{$res->[1]}, 'Content-Length', bytes::length($part); $res->[2]->[0] = $part;