-
Notifications
You must be signed in to change notification settings - Fork 7
/
torpaste.py
executable file
·304 lines (249 loc) · 8.13 KB
/
torpaste.py
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#!bin/python
# -*- coding: utf-8 -*-
import importlib
import time
from datetime import datetime
from os import getenv
import sys
import logic
from subprocess import check_output
from flask import Flask
from flask import Response
from flask import redirect
from flask import render_template
from flask import request
app = Flask(__name__)
# Calculate Software Version
VERSION = check_output(["git", "describe"]).decode("utf-8").replace("\n", "")
# Compatible Backends List
COMPATIBLE_BACKENDS = [
"filesystem",
"azure_storage",
"aws_s3",
]
# Available list of paste visibilities
# public: can be viewed by all, is listed in /list
# unlisted: can be viewed by all, is not listed in /list ("hidden")
AVAILABLE_VISIBILITIES = ["public", "unlisted"]
@app.route('/')
def index():
return render_template(
"index.html",
config=config,
version=VERSION,
page="main"
)
@app.route("/new", methods=["GET", "POST"])
def new_paste():
if request.method == "GET":
return render_template(
"index.html",
config=config,
version=VERSION,
page="new"
)
else:
if (request.form['content']):
status, message = logic.create_new_paste(
request.form['content'],
request.form,
config
)
if (status == "ERROR"):
return Response(
render_template(
"index.html",
config=config,
version=VERSION,
page="new",
error=message
),
400
)
if (status == "OK"):
return redirect("/view/" + message)
else:
return Response(
render_template(
"index.html",
config=config,
version=VERSION,
error="Please enter some text to include in the paste.",
page="new"
),
400
)
@app.route("/view/<pasteid>")
def view_paste(pasteid):
status, data, code = logic.view_existing_paste(pasteid, config)
if (status == "ERROR"):
return Response(
render_template(
"index.html",
config=config,
version=VERSION,
error=data,
page="new"
),
code
)
if (status == "OK"):
paste_date = datetime.fromtimestamp(
int(
data[1]
) + time.altzone + 3600).strftime("%H:%M:%S %d/%m/%Y")
paste_size = logic.format_size(len(data[0].encode('utf-8')))
if (status == "WARNING"):
paste_date = "Not available."
return Response(
render_template(
"view.html",
content=data[0],
date=paste_date,
size=paste_size,
pid=pasteid,
config=config,
version=VERSION,
page="view"
),
200
)
@app.route("/raw/<pasteid>")
def raw_paste(pasteid):
status, data, code = logic.view_existing_paste(pasteid, config)
if (status == "ERROR" and code >= 500):
return Response(data, code, mimetype="text/plain")
if (status == "ERROR"):
return Response("No such paste", code, mimetype="text/plain")
return Response(data[0], mimetype="text/plain")
@app.route("/list")
def list():
listFilters = {"visibility": "public"}
defaultFilters = {"visibility": "public"}
status, data, code = logic.get_paste_listing(
config,
listFilters,
defaultFilters
)
if (status == "ERROR"):
return Response(
render_template(
"index.html",
config=config,
version=VERSION,
page="new",
error=data
),
code
)
return Response(
render_template(
"list.html",
pastes=data,
config=config,
version=VERSION,
page="list"
)
)
@app.route("/about")
def about_tor_paste():
return render_template(
"about.html",
config=config,
version=VERSION,
page="about"
)
@app.after_request
def additional_headers(response):
response.headers["X-Frame-Options"] = "DENY"
response.headers["X-Xss-Protection"] = "1; mode=block"
response.headers["X-Content-Type-Options"] = "nosniff"
response.headers["X-Powered-By"] = "Tor Paste " + VERSION
response.headers["Content-Security-Policy"] = "default-src 'none'; "
response.headers["Content-Security-Policy"] += "img-src 'self'; "
response.headers["Content-Security-Policy"] += "style-src 'self'; "
if (config['CSP_REPORT_URI']):
response.headers["Content-Security-Policy"] += "report-uri "
response.headers["Content-Security-Policy"] += config['CSP_REPORT_URI']
return response
# Required Initialization Code
# necessary for local modules import (backends, exceptions)
sys.path.append('.')
def load_config():
"""
This method reads all configuration variables from environment variables
and put them in a dictionary, which is then returned. Environment
variables are used for convenience when using Docker (simply add
an -e "TP_SOME_CONFIG_VAR=value" to docker run to modify the default
configuration)
:return: the configuration dictionary
"""
# Web App <title>
WEBSITE_TITLE = getenv("TP_WEBSITE_TITLE") or "Tor Paste"
# Backend Used
BACKEND = getenv("TP_BACKEND") or "filesystem"
if BACKEND in COMPATIBLE_BACKENDS:
b = importlib.import_module('backends.'+BACKEND)
else:
print(
"Configured backend (" + BACKEND + ") is not compatible with " +
"current version."
)
exit(1)
# Maximum Paste Size
MAX_PASTE_SIZE = getenv("TP_PASTE_MAX_SIZE") or "1 P"
if MAX_PASTE_SIZE[0] == "0":
MAX_PASTE_SIZE = "1 P"
MAX_PASTE_SIZE = MAX_PASTE_SIZE.split(" ")
try:
AMOUNT = int(MAX_PASTE_SIZE[0])
UNIT = MAX_PASTE_SIZE[1]
except Exception:
print("Invalid TP_PASTE_MAX_SIZE: " + " ".join(MAX_PASTE_SIZE))
exit(1)
orders = ["B", "k", "M", "G", "T", "P"]
if UNIT not in orders:
print("Invalid Unit Size: " + UNIT)
try:
MAX_PASTE_SIZE = AMOUNT * 1024**orders.index(UNIT)
except Exception:
print("An unknown error occured while determining max paste size.")
exit(1)
# Disable the paste listing feature
PASTE_LIST_ACTIVE = getenv("TP_PASTE_LIST_ACTIVE") or True
if PASTE_LIST_ACTIVE in ["False", "false", 0, "0"]:
PASTE_LIST_ACTIVE = False
# Content Security Policy Handling
CSP_REPORT_URI = getenv("TP_CSP_REPORT_URI") or False
# control the enabled paste visibilities:
# public = can be opened by anyone and listed in /list
# unlisted = can be opened by anyone, but are not listed in /list
visibilityEnv = "TP_ENABLED_PASTE_VISIBILITIES"
ENABLED_PASTE_VISIBILITIES = getenv(visibilityEnv) or 'public'
ENABLED_PASTE_VISIBILITIES = ENABLED_PASTE_VISIBILITIES.replace(' ', '')
ENABLED_PASTE_VISIBILITIES = ENABLED_PASTE_VISIBILITIES.split(',')
# remove any potential whitespace
ENABLED_PASTE_VISIBILITIES = [visibility
for visibility in ENABLED_PASTE_VISIBILITIES
if visibility in AVAILABLE_VISIBILITIES]
if len(ENABLED_PASTE_VISIBILITIES) == 0:
print("No valid visibilities found for pastes.")
exit(1)
return {
"MAX_PASTE_SIZE": MAX_PASTE_SIZE,
"WEBSITE_TITLE": WEBSITE_TITLE,
"PASTE_LIST_ACTIVE": PASTE_LIST_ACTIVE,
"CSP_REPORT_URI": CSP_REPORT_URI,
"ENABLED_PASTE_VISIBILITIES": ENABLED_PASTE_VISIBILITIES,
"b": b
}
config = load_config()
b = config['b']
# Initialize Backend
try:
b.initialize_backend()
except Exception:
print("Failed to initialize backend")
exit(1)
if __name__ == '__main__':
app.run(host="0.0.0.0")