ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/perl/confirm-paste
Revision: 1.13
Committed: Fri Dec 23 22:38:29 2022 UTC (16 months, 1 week ago) by root
Branch: MAIN
CVS Tags: HEAD
Changes since 1.12: +27 -3 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 sf-exg 1.1 #! perl
2    
3 root 1.5 =head1 NAME
4    
5 sf-exg 1.10 confirm-paste - ask for confirmation before pasting control characters
6 root 1.5
7 root 1.6 =head1 DESCRIPTION
8 root 1.5
9 root 1.9 Displays a confirmation dialog when a paste containing control characters
10 root 1.13 is detected. The user can choose C<y> to either paste a sanitized variant
11     where all control characters are removed, C<p> to paste the string
12     unmodified or C<n> to drop the paste request completely.
13 root 1.9
14     This is mostly meant as a defense-in-depth mechanism to protect against
15 sf-exg 1.10 the common web browser bug of you selecting some text but the browser
16 root 1.9 pasting a completely different text, which has some attack potential.
17    
18     It can also be useful to prevent you from accidentally pasting large
19     amounts of text.
20 root 1.5
21 root 1.13 =head2 DETAILS
22    
23     If a string containing unicode control characters (specifically U+0000 ..
24     U+001F currrently) is pasted into the terminal, this extension will ask
25     whether it should be pasted. Strings without control characters get pasted
26     without prompt.
27    
28     When a sanitized version is pasted (choice C<y>), then contiguous
29     sequences of those control characters will be replaced by a single spaces.
30    
31     The exact detection and sanitization algorithm is subject to change in
32     future versions.
33    
34 root 1.5 =cut
35    
36 sf-exg 1.1 sub msg {
37     my ($self, $msg) = @_;
38    
39 sf-exg 1.3 $self->{overlay} = $self->overlay (0, -1, $self->ncol, 2, urxvt::OVERLAY_RSTYLE, 0);
40 sf-exg 1.1 $self->{overlay}->set (0, 0, $msg);
41     }
42    
43     sub on_tt_paste {
44     my ($self, $str) = @_;
45    
46 sf-exg 1.12 my $count = ($str =~ tr/\x00-\x1f//)
47 root 1.9 or return;
48 sf-exg 1.1
49     $self->{paste} = \$str;
50 root 1.13 $self->msg ("Pasting $count control characters, continue? (y/p/n)");
51 root 1.9
52 sf-exg 1.4 my $preview = substr $self->locale_decode ($str), 0, $self->ncol;
53     $preview =~ s/\n/\\n/g;
54 root 1.8 $preview =~ s/([\x00-\x1f\x80-\x9f])/sprintf "\\x%02x", ord $1/ge;
55 root 1.9
56 sf-exg 1.4 $self->{overlay}->set (0, 1, $self->special_encode ($preview));
57 sf-exg 1.1 $self->enable (key_press => \&key_press);
58    
59     1
60     }
61    
62     sub leave {
63     my ($self) = @_;
64    
65     $self->{paste} = undef;
66     delete $self->{overlay};
67     $self->disable ("key_press");
68     }
69    
70     sub key_press {
71     my ($self, $event, $keysym, $string) = @_;
72    
73 root 1.13 my $paste = delete $self->{paste};
74    
75 sf-exg 1.1 if ($keysym == 121) { # y
76 root 1.13 my $paste = $$paste;
77     $paste =~ s/[\x00-\x1f]+/ /g;
78     $self->tt_paste ($paste);
79     $self->leave;
80     } elsif ($keysym == 112) { # p
81     $self->tt_paste ($$paste);
82 sf-exg 1.1 $self->leave;
83     } elsif ($keysym == 110) { # n
84     $self->leave;
85     }
86    
87 root 1.13 $self->{paste} = $paste;
88    
89 sf-exg 1.1 1
90     }