]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Hval.pm
hval: routines for attribute escaping
[public-inbox.git] / lib / PublicInbox / Hval.pm
1 # Copyright (C) 2014-2018 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 #
4 # represents a header value in various forms.  Used for HTML generation
5 # in our web interface(s)
6 package PublicInbox::Hval;
7 use strict;
8 use warnings;
9 use Encode qw(find_encoding);
10 use PublicInbox::MID qw/mid_clean mid_escape/;
11 use base qw/Exporter/;
12 our @EXPORT_OK = qw/ascii_html obfuscate_addrs to_filename src_escape
13                 to_attr from_attr/;
14 my $enc_ascii = find_encoding('us-ascii');
15
16 sub new {
17         my ($class, $raw, $href) = @_;
18
19         # we never care about trailing whitespace
20         $raw =~ s/\s*\z//;
21         bless {
22                 raw => $raw,
23                 href => defined $href ? $href : $raw,
24         }, $class;
25 }
26
27 sub new_msgid {
28         my ($class, $msgid) = @_;
29         $class->new($msgid, mid_escape($msgid));
30 }
31
32 sub new_oneline {
33         my ($class, $raw) = @_;
34         $raw = '' unless defined $raw;
35         $raw =~ tr/\t\n / /s; # squeeze spaces
36         $raw =~ tr/\r//d; # kill CR
37         $class->new($raw);
38 }
39
40 # some of these overrides are standard C escapes so they're
41 # easy-to-understand when rendered.
42 my %escape_sequence = (
43         "\x00" => '\\0', # NUL
44         "\x07" => '\\a', # bell
45         "\x08" => '\\b', # backspace
46         "\x09" => "\t", # obvious to show as-is
47         "\x0a" => "\n", # obvious to show as-is
48         "\x0b" => '\\v', # vertical tab
49         "\x0c" => '\\f', # form feed
50         "\x0d" => '\\r', # carriage ret (not preceding \n)
51         "\x1b" => '^[', # ASCII escape (mutt seems to escape this way)
52         "\x7f" => '\\x7f', # DEL
53 );
54
55 my %xhtml_map = (
56         '"' => '&#34;',
57         '&' => '&#38;',
58         "'" => '&#39;',
59         '<' => '&lt;',
60         '>' => '&gt;',
61 );
62
63 $xhtml_map{chr($_)} = sprintf('\\x%02x', $_) for (0..31);
64 %xhtml_map = (%xhtml_map, %escape_sequence);
65
66 sub src_escape ($) {
67         $_[0] =~ s/\r\n/\n/sg;
68         $_[0] =~ s/([\x7f\x00-\x1f])/$xhtml_map{$1}/sge;
69         $_[0] = $enc_ascii->encode($_[0], Encode::HTMLCREF);
70 }
71
72 sub ascii_html {
73         my ($s) = @_;
74         $s =~ s/\r\n/\n/sg; # fixup bad line endings
75         $s =~ s/([<>&'"\x7f\x00-\x1f])/$xhtml_map{$1}/sge;
76         $enc_ascii->encode($s, Encode::HTMLCREF);
77 }
78
79 sub as_html { ascii_html($_[0]->{raw}) }
80
81 sub raw {
82         if (defined $_[1]) {
83                 $_[0]->{raw} = $_[1];
84         } else {
85                 $_[0]->{raw};
86         }
87 }
88
89 sub prurl {
90         my ($env, $u) = @_;
91         index($u, '//') == 0 ? "$env->{'psgi.url_scheme'}:$u" : $u;
92 }
93
94 # for misguided people who believe in this stuff, give them a
95 # substitution for '.'
96 # &#8228; &#183; and &#890; were also candidates:
97 #   https://public-inbox.org/meta/20170615015250.GA6484@starla/
98 # However, &#8226; was chosen to make copy+paste errors more obvious
99 sub obfuscate_addrs ($$;$) {
100         my $ibx = $_[0];
101         my $repl = $_[2] || '&#8226;';
102         my $re = $ibx->{-no_obfuscate_re}; # regex of domains
103         my $addrs = $ibx->{-no_obfuscate}; # { adddress => 1 }
104         $_[1] =~ s/(([\w\.\+=\-]+)\@([\w\-]+\.[\w\.\-]+))/
105                 my ($addr, $user, $domain) = ($1, $2, $3);
106                 if ($addrs->{$addr} || ((defined $re && $domain =~ $re))) {
107                         $addr;
108                 } else {
109                         $domain =~ s!([^\.]+)\.!$1$repl!;
110                         $user . '@' . $domain
111                 }
112                 /sge;
113 }
114
115 # like format_sanitized_subject in git.git pretty.c with '%f' format string
116 sub to_filename ($) {
117         my ($s, undef) = split(/\n/, $_[0]);
118         $s =~ s/[^A-Za-z0-9_\.]+/-/g;
119         $s =~ tr/././s;
120         $s =~ s/[\.\-]+\z//;
121         $s =~ s/\A[\.\-]+//;
122         $s
123 }
124
125 # convert a filename (or any string) to HTML attribute
126
127 my %ESCAPES = map { chr($_) => sprintf('::%02x', $_) } (0..255);
128 $ESCAPES{'/'} = ':'; # common
129
130 sub to_attr ($) {
131         my ($str) = @_;
132
133         # git would never do this to us:
134         return if index($str, '//') >= 0;
135
136         my $first = '';
137         if ($str =~ s/\A([^A-Ya-z])//ms) { # start with a letter
138                   $first = sprintf('Z%02x', ord($1));
139         }
140         $str =~ s/([^A-Za-z0-9_\.\-])/$ESCAPES{$1}/egms;
141         $first . $str;
142 }
143
144 # reverse the result of to_attr
145 sub from_attr ($) {
146         my ($str) = @_;
147         my $first = '';
148         if ($str =~ s/\AZ([a-f0-9]{2})//ms) {
149                 $first = chr(hex($1));
150         }
151         $str =~ s!::([a-f0-9]{2})!chr(hex($1))!egms;
152         $str =~ tr!:!/!;
153         $first . $str;
154 }
155
156 1;