-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.py
More file actions
112 lines (94 loc) · 3.22 KB
/
helpers.py
File metadata and controls
112 lines (94 loc) · 3.22 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
"""
Contains helping function.
"""
import subprocess
import re
import html.entities
import config
import shutil
import os
# https://stackoverflow.com/questions/25027122/break-the-function-after-certain-time
class TimeoutException(Exception):
"""Custom exception class"""
pass
def timeout_handler(signum, frame):
"""# Custom signal handler"""
raise TimeoutException
class Interpreter(object):
"""
Class to securely run code fed to us.
"""
def __init__(self, language):
self.firejail_dir = config.FIREJAIL_DIR
self.input_file = self.firejail_dir + config.INPUT_FILE
self.command = config.FIREJAIL_COMMAND + language["command"]
self.output = ""
def run(self, code):
"""
Runs code and returns the output
"""
self.create_input(code)
try:
output_bytes = subprocess.check_output(self.command, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as err:
output_bytes = err.output
output = str(output_bytes, "utf-8")
output = "".join(output.split("\x07")[1:])
# if you take off, everything breaks, even if it makes no sense
# if you print output, it gives you what you expect
# however, if you, in a terminal, evaluate output, the string is different
# I don't know why this happens
self.output = output
clean_up()
def create_input(self, code=""):
"""
Creates temporary file and fills it with the code to be executed
"""
with open(self.input_file, "w+") as tmp_file:
tmp_file.write(code)
def clean_up():
"""
Kills every firejailed process, cleans firejail_dir folder
"""
command = ["firejail", "--list"]
kill = ["kill", "-9"]
pids = [int(s[:s.find(":")]) for s in str(subprocess.check_output(command), "utf-8")\
.split("\n")[:-1]]
for pid in pids:
# os.kill sometimes doesn't work
# os.kill(PID, signal.SIGTERM)
try:
subprocess.check_call(kill + [str(pid)])
except subprocess.CalledProcessError:
pass
# delete everything in firejail_dir
for the_file in os.listdir(config.FIREJAIL_DIR):
file_path = os.path.join(config.FIREJAIL_DIR, the_file)
if os.path.isfile(file_path):
os.unlink(file_path)
elif os.path.isdir(file_path):
shutil.rmtree(file_path)
def unescape(text):
"""
Removes HTML or XML character references and entities from a text string.
@param text The HTML (or XML) source text.
@return The plain text, as a Unicode string, if necessary."""
def fixup(match):
"""Auxiliary function"""
text = match.group(0)
if text[:2] == "&#":
# character reference
try:
if text[:3] == "&#x":
return chr(int(text[3:-1], 16))
return chr(int(text[2:-1]))
except ValueError:
pass
else:
# named entity
try:
text = chr(html.entities.name2codepoint[text[1:-1]])
except KeyError:
pass
return text # leave as is
return re.sub(r"&#?\w+;", fixup, text)