]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/WwwAttach.pm
wwwattach: prevent deep-linking via Referer match
[public-inbox.git] / lib / PublicInbox / WwwAttach.pm
1 # Copyright (C) 2016-2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 # For retrieving attachments from messages in the WWW interface
5 package PublicInbox::WwwAttach; # internal package
6 use strict;
7 use parent qw(PublicInbox::GzipFilter);
8 use bytes (); # only for bytes::length
9 use PublicInbox::EmlContentFoo qw(parse_content_type);
10 use PublicInbox::Eml;
11
12 sub referer_match ($) {
13         my ($ctx) = @_;
14         my $env = $ctx->{env};
15         my $referer = $env->{HTTP_REFERER} // '';
16         return 1 if $referer eq ''; # no referer is always OK for wget/curl
17
18         # prevent deep-linking from other domains on some browsers (Firefox)
19         # n.b.: $ctx->{-inbox}->base_url($env) with INBOX_URL won't work
20         # with dillo, we can only match "$url_scheme://$HTTP_HOST/" without
21         # path components
22         my $base_url = $env->{'psgi.url_scheme'} . '://' .
23                         ($env->{HTTP_HOST} //
24                          "$env->{SERVER_NAME}:$env->{SERVER_PORT}") . '/';
25         index($referer, $base_url) == 0;
26 }
27
28 sub get_attach_i { # ->each_part callback
29         my ($part, $depth, $idx) = @{$_[0]};
30         my $ctx = $_[1];
31         return if $idx ne $ctx->{idx}; # [0-9]+(?:\.[0-9]+)+
32         my $res = $ctx->{res};
33         $res->[0] = 200;
34         my $ct = $part->content_type;
35         $ct = parse_content_type($ct) if $ct;
36
37         if ($ct && (($ct->{type} || '') eq 'text')) {
38                 # display all text as text/plain:
39                 my $cset = $ct->{attributes}->{charset};
40                 if ($cset && ($cset =~ /\A[a-zA-Z0-9_\-]+\z/)) {
41                         $res->[1]->[1] .= qq(; charset=$cset);
42                 }
43                 $ctx->{gz} = PublicInbox::GzipFilter::gz_or_noop($res->[1],
44                                                                 $ctx->{env});
45                 $part = $ctx->zflush($part->body);
46         } else { # TODO: allow user to configure safe types
47                 if (referer_match($ctx)) {
48                         $res->[1]->[1] = 'application/octet-stream';
49                         $part = $part->body;
50                 } else {
51                         $res->[0] = 403;
52                         $res->[1]->[1] = 'text/plain';
53                         $part = "Deep-linking prevented\n";
54                 }
55         }
56         push @{$res->[1]}, 'Content-Length', bytes::length($part);
57         $res->[2]->[0] = $part;
58 }
59
60 sub async_eml { # for async_blob_cb
61         my ($ctx, $eml) = @_;
62         eval { $eml->each_part(\&get_attach_i, $ctx, 1) };
63         if ($@) {
64                 $ctx->{res}->[0] = 500;
65                 warn "E: $@";
66         }
67 }
68
69 sub async_next {
70         my ($http) = @_;
71         my $ctx = $http->{forward} or return; # client aborted
72         # finally, we call the user-supplied callback
73         eval { $ctx->{wcb}->($ctx->{res}) };
74         warn "E: $@" if $@;
75 }
76
77 sub scan_attach ($) { # public-inbox-httpd only
78         my ($ctx) = @_;
79         $ctx->{env}->{'psgix.io'}->{forward} = $ctx;
80         $ctx->smsg_blob($ctx->{smsg});
81 }
82
83 # /$LISTNAME/$MESSAGE_ID/$IDX-$FILENAME
84 sub get_attach ($$$) {
85         my ($ctx, $idx, $fn) = @_;
86         $ctx->{res} = [ 404, [ 'Content-Type' => 'text/plain' ],
87                                 [ "Not found\n" ] ];
88         $ctx->{idx} = $idx;
89         bless $ctx, __PACKAGE__;
90         my $eml;
91         if ($ctx->{smsg} = $ctx->{-inbox}->smsg_by_mid($ctx->{mid})) {
92                 return sub { # public-inbox-httpd-only
93                         $ctx->{wcb} = $_[0];
94                         scan_attach($ctx);
95                 } if $ctx->{env}->{'pi-httpd.async'};
96                 # generic PSGI:
97                 $eml = $ctx->{-inbox}->smsg_eml($ctx->{smsg});
98         } elsif (!$ctx->{-inbox}->over) {
99                 if (my $bref = $ctx->{-inbox}->msg_by_mid($ctx->{mid})) {
100                         $eml = PublicInbox::Eml->new($bref);
101                 }
102         }
103         $eml->each_part(\&get_attach_i, $ctx, 1) if $eml;
104         $ctx->{res}
105 }
106
107 1;