forked from dgl/paste.sh
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.psgi
More file actions
158 lines (134 loc) · 4.11 KB
/
app.psgi
File metadata and controls
158 lines (134 loc) · 4.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package PasteSh;
use JSON;
use Plack::Request;
use Scalar::Util qw(blessed);
use Tie::LevelDB;
use Web::Simple;
tie my %data, 'Tie::LevelDB', 'db';
my $serverauth = do {
open my $fh, "<", "serverauth" or die "serverauth: $!";
(<$fh> =~ /(.*)/)[0];
};
my @common_headers = (
'Strict-Transport-Security' => 'max-age=31536000',
'Server' => 'paste.sh/' . do { chomp(my $t = `git describe --always`); $t });
sub _error {
my($message, $code) = @_;
return [ $code,
[ 'Content-Type' => 'text/plain', @common_headers ],
[ $message . "\n" ] ];
}
sub dispatch_request {
my($self, $env) = @_;
sub (/) {
my($self) = @_;
redispatch_to '/index';
},
sub (GET + /cryptojs/*.*) {
my($self, $file) = @_;
if($file =~ /[^a-z0-9-.]/ || !-f "crypto-js/src/$file") {
return _error('Not found', 404);
}
open my $fh, "<", "crypto-js/src/$file" or return;
return [ 200,
[ 'Content-type', 'text/javascript', @common_headers ],
[ <$fh> ] ];
},
sub (GET + /favicon.ico) {
my($self) = @_;
open my $fh, "<", "favicon.ico" or return;
return [ 200,
[ 'Content-type', 'image/x-icon', @common_headers ],
[ <$fh> ] ];
},
sub (GET + /new + ?id=) {
my($self, $id) = @_;
if($data{$id}) {
return _error('Already exists', 409);
}
return [ 200, [ 'Content-type', 'text/plain', @common_headers ],
[ map chr 32 + rand 96, 1 .. 8 ] ];
},
sub (GET + /* + .txt) {
my($self, $path) = @_;
my $data = exists $data{$path} ? eval { decode_json $data{$path} } : undef;
if(!$data) {
return _error('Not found', 404);
}
my $content = $data->{content};
$content =~ s/\G(.{65})/$1\n/g;
return [ 200, [
'Content-type' => 'text/plain',
@common_headers
], [
$data->{serverkey} . "\n" . $content . "\n"
] ];
},
sub (GET + /**) {
my($self, $path) = @_;
my $req = Plack::Request->new($env);
my $cookie = $req->cookies->{pasteauth};
my $data = exists $data{$path} ? eval { decode_json $data{$path} } : undef;
if(!$data) {
return _error('Not found', 404);
}
open my $fh, "<", "paste.html" or die $!;
my $template = join "", <$fh>;
my $public = $path =~ /^p.{8}/;
$template =~ s/\{\{encrypted\}\}/$public ? "" : "encrypted"/e;
$template =~ s/\{\{content\}\}/$data->{content}/;
$template =~ s/\{\{serverkey\}\}/
to_json($data->{serverkey} || "", { allow_nonref => 1 })/e;
$template =~ s/\{\{editable\}\}/
($cookie && $data->{cookie} && $cookie eq $data->{cookie})
|| $path eq 'index'/e;
return [ 200, [
'Content-type' => 'text/html; charset=UTF-8',
(!$public && $path =~ /.{8}/) ? ('X-Robots-Tag' => 'noindex') : (),
@common_headers
], [ $template ] ];
},
sub (PUT + /**) {
my($self, $path) = @_;
my $req = Plack::Request->new($env);
my $content = $req->content;
my $cookie = $req->cookies->{pasteauth};
my $sauth = $req->header('X-Server-Auth');
if($sauth) {
if($sauth ne $serverauth) {
return _error("Invalid auth", 403);
}
# Authed clients can write anything (for updating about, etc).
} elsif(length($path) < 8 || length($path) > 12 || $path =~ m{[^A-Za-z0-9_-]}) {
return _error("Invalid path", 400);
}
if(!$sauth && exists $data{$path}) {
if(my $cur = eval { decode_json $data{$path} }) {
if(!$cur->{cookie} || $cur->{cookie} ne $cookie) {
return _error("Invalid cookie", 403);
}
}
}
$content =~ s/[\r\n]//g;
if($content =~ m{[^A-Za-z0-9/+=]}) {
return _error("Content contains non-base64", 403);
}
if(length $content > (640 * 1024)) {
return _error("Content too large", 413);
}
my $serverkey = $req->header('X-Server-Key');
$data{$path} = encode_json {
content => $content,
cookie => $cookie,
timestamp => time,
serverkey => $serverkey,
};
[ 200,
[ 'Content-type', 'text/plain', @common_headers ],
[ "Saved " . length($content) . " bytes.\n" ] ]
},
sub () {
_error("Not found", 404);
}
}
__PACKAGE__->run_if_script;