-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain_fuzzer.py
More file actions
301 lines (243 loc) · 8.85 KB
/
main_fuzzer.py
File metadata and controls
301 lines (243 loc) · 8.85 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
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
import argparse
import contextlib
import datetime
import os.path
import random
import shutil
import sys
from enum import Enum
from pathlib import Path
from subprocess import CalledProcessError, CompletedProcess, TimeoutExpired, run
from time import time
from typing import Iterator, List
from faker import Faker
from lxml import etree, html
from lxml.html import HtmlElement
@contextlib.contextmanager
def measure_performance(title: str) -> Iterator[None]:
time_start = time()
yield
time_end = time()
time_diff = time_end - time_start
padded_name = f"{title} ".ljust(60, ".")
padded_time = f" {time_diff:0.2f}".rjust(6, ".")
print(f"{padded_name}{padded_time}s", flush=True) # noqa: T201
def rewrite_js_path_to_local(html_text: str) -> str:
tree = html.fromstring(html_text)
for script in tree.xpath("//script[@src]"):
src = script.get("src")
if src.endswith("html2pdf4doc.min.js"):
script.set("src", "html2pdf4doc.min.js")
return str(html.tostring(tree, encoding="unicode"))
class MutationType(str, Enum):
RANDOM = "random"
INCREMENTAL = "incremental"
@classmethod
def all_as_str(cls) -> List[str]:
return [t.value for t in cls]
def mutate_html_content(
tree: HtmlElement, mutation_type: MutationType, mutation_cycle: int
) -> None:
if mutation_type == MutationType.RANDOM:
# Pick a random element.
elems = tree.xpath("//p | //td")
if elems:
for _i in range(25):
node = random.choice(elems)
print("Mutating node:", node.tag, flush=True) # noqa: T201
n_sentences = random.randint(1, 100)
fake = Faker()
extra_text = fake.text(max_nb_chars=10 * n_sentences)
node.text = extra_text
elif mutation_type == MutationType.INCREMENTAL:
elems = tree.xpath('//*[@id="html2pdf4doc_mutate_this"]')
assert len(elems) == 1, (
'Expected element with id="html2pdf4doc_mutate_this" to be found.'
)
filler_element = elems[0]
filler_element.attrib["style"] = f"height: {mutation_cycle}px;"
else:
raise AssertionError("Must not reach here.")
def mutate_and_print(
*,
path_to_input_file: str,
path_to_root: str,
path_to_failed_mutants_dir: str,
cycle: int,
mutation_type: MutationType,
strict_mode_2: bool = False,
) -> bool:
assert os.path.isfile(path_to_input_file), path_to_input_file
assert os.path.isdir(path_to_root), path_to_root
assert 0 <= cycle <= 1000, cycle
if not os.path.abspath(path_to_root):
path_to_root = os.path.abspath(path_to_root)
with open(path_to_input_file, encoding="utf-8") as input_file_:
text = input_file_.read()
# Parse HTML into DOM
tree = html.fromstring(text)
mutate_html_content(tree, mutation_type, cycle)
# Serialize back to HTML
mutated_html = etree.tostring(
tree, pretty_print=False, method="html", encoding="unicode"
)
# Save next to input file
path_to_mut_html = path_to_input_file + ".mut.html"
path_to_mut_pdf = path_to_input_file + ".mut.html.pdf"
with open(path_to_mut_html, "w", encoding="utf-8") as f:
f.write(mutated_html)
print("Wrote mutated file:", path_to_mut_html, flush=True) # noqa: T201
paths_to_print = [(path_to_mut_html, path_to_mut_pdf)]
cmd: List[str] = [
sys.executable,
"-m",
"html2pdf4doc.main",
"print",
"--strict",
]
if strict_mode_2:
cmd.append("--strict2")
for path_to_print_ in paths_to_print:
cmd.append(path_to_print_[0])
cmd.append(path_to_print_[1])
path_to_mut_dir = os.path.dirname(path_to_mut_html)
relative_path_to_mut_html = Path(path_to_mut_dir).relative_to(path_to_root)
path_to_mut_output = os.path.join(
path_to_failed_mutants_dir, relative_path_to_mut_html
)
def copy_files_if_needed() -> None:
if os.path.isdir(path_to_mut_output):
return
Path(path_to_failed_mutants_dir).mkdir(parents=True, exist_ok=True)
shutil.copytree(
"html2pdf4doc",
os.path.join(path_to_failed_mutants_dir, "html2pdf4doc"),
dirs_exist_ok=True,
)
shutil.rmtree(path_to_mut_output, ignore_errors=True)
Path(path_to_mut_output).mkdir(parents=True, exist_ok=True)
shutil.copytree(path_to_root, path_to_mut_output, dirs_exist_ok=True)
def copy_mutated_file() -> None:
relative_path_to_mut_html = Path(path_to_mut_html).relative_to(
path_to_root
)
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
path_to_mut_html_out = os.path.join(
path_to_mut_output,
f"{relative_path_to_mut_html}.{timestamp}.html",
)
shutil.copy(path_to_mut_html, path_to_mut_html_out)
if not os.path.isfile(path_to_mut_pdf):
print( # noqa: T201
f"html2pdf4doc_fuzzer: warning: Mutated PDF is missing: {path_to_mut_pdf}"
)
return
path_to_mut_pdf_out = os.path.join(
path_to_mut_output,
f"{relative_path_to_mut_html}.{timestamp}.pdf",
)
shutil.copy(path_to_mut_pdf, path_to_mut_pdf_out)
print( # noqa: T201
f"Saved failed mutated HTML as:\n"
f"HTML: {path_to_mut_html_out}\n"
f"PDF: {path_to_mut_pdf_out}"
)
with measure_performance(
"html2pdf4doc_fuzzer: printing HTML to PDF using HTML2PDF and Chrome Driver"
):
try:
_: CompletedProcess[bytes] = run(
cmd, capture_output=False, check=True, bufsize=1
)
except CalledProcessError as called_process_error_:
print(called_process_error_) # noqa: T201
copy_files_if_needed()
copy_mutated_file()
return False
except TimeoutExpired:
raise TimeoutError from None
return True
def fuzz_test(
*,
path_to_input_file: str,
path_to_root: str,
path_to_failed_mutants_dir: str,
total_mutations: int = 20,
mutation_type: MutationType = MutationType.RANDOM,
strict_mode_2: bool = False,
) -> None:
success_count, failure_count = 0, 0
for i in range(1, total_mutations + 1):
print( # noqa: T201
f"html2pdf4doc_fuzzer print cycle #{i}/{total_mutations} — "
f"So far: 🟢{success_count} / 🔴{failure_count}",
flush=True,
)
success = mutate_and_print(
path_to_input_file=path_to_input_file,
path_to_root=path_to_root,
path_to_failed_mutants_dir=path_to_failed_mutants_dir,
cycle=i,
mutation_type=mutation_type,
strict_mode_2=strict_mode_2,
)
if success:
success_count += 1
else:
failure_count += 1
assert total_mutations > 0
success_rate_percent = (success_count / total_mutations) * 100
print( # noqa: T201
f"html2pdf4doc_fuzzer: finished {'✅' if failure_count == 0 else '❌'} — "
f"Success rate: {success_count}/{total_mutations} ({success_rate_percent}%)",
flush=True,
)
if failure_count > 0:
sys.exit(1)
def main() -> None:
# To avoid UnicodeEncodeError on Windows when printing emojis.
sys.stdout.reconfigure(encoding="utf-8") # type: ignore[union-attr]
parser = argparse.ArgumentParser()
parser.add_argument("input_file", type=str, help="TODO")
parser.add_argument("root_path", type=str, help="TODO")
parser.add_argument("path_to_failed_mutants_dir", type=str, help="TODO")
parser.add_argument(
"--total-mutations",
type=int,
required=True,
help="An integer between 1 and 1000",
)
parser.add_argument(
"--mutations",
type=str,
choices=MutationType.all_as_str(),
help="Algorithm to use for mutations.",
)
parser.add_argument(
"--strict",
action="store_true",
help="Enables Strict mode (level 1).",
)
parser.add_argument(
"--strict2",
action="store_true",
help="Enables Strict mode (level 2).",
)
args = parser.parse_args()
path_to_input_file = args.input_file
path_to_root = args.root_path
path_to_failed_mutants_dir = args.path_to_failed_mutants_dir
total_mutations = args.total_mutations
assert 1 <= total_mutations <= 1000, total_mutations
mutation_type = MutationType(args.mutations)
strict_mode_2 = args.strict2
fuzz_test(
path_to_input_file=path_to_input_file,
path_to_root=path_to_root,
path_to_failed_mutants_dir=path_to_failed_mutants_dir,
total_mutations=total_mutations,
mutation_type=mutation_type,
strict_mode_2=strict_mode_2,
)
if __name__ == "__main__":
main()