Skip to content

Commit 897fd0e

Browse files
committed
Validate the JSON submitted via the feedback mechanism
1 parent dfaa16f commit 897fd0e

1 file changed

Lines changed: 48 additions & 36 deletions

File tree

infra/api.py

Lines changed: 48 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def save(self):
2828
json.dump(self.data, f)
2929

3030
def safe_tag(self, tag):
31-
if tag.lower() in [ "p", "li", "pre", "span" ]:
31+
if isinstance(tag, str) and tag.lower() in [ "p", "li", "pre", "span" ]:
3232
return tag
3333
else:
3434
return "p"
@@ -39,54 +39,66 @@ def typo(self, url, old, new, name, tag="p"):
3939
obj['old'] == old and
4040
obj['new'] == new for obj in self.data):
4141
return
42-
self.data.append({
43-
'id': len(self.data),
44-
'time': time.time(),
45-
'type': 'typo',
46-
'tag': self.safe_tag(tag),
47-
'url': url,
48-
'old': old,
49-
'new': new,
50-
'name': name,
51-
'status': 'new',
52-
})
53-
self.save()
42+
if isinstance(url, str) and isinstance(old, str) and \
43+
isinstance(new, str) and isinstance(name, str):
44+
self.data.append({
45+
'id': len(self.data),
46+
'time': time.time(),
47+
'type': 'typo',
48+
'tag': self.safe_tag(tag),
49+
'url': url,
50+
'old': old,
51+
'new': new,
52+
'name': name,
53+
'status': 'new',
54+
})
55+
self.save()
56+
else:
57+
abort(400, "Invalid JSON object")
5458

5559
def text_comment(self, url, text, comment, name, tag="p"):
5660
if any(obj['type'] == 'comment' and
5761
obj['url'] == url and
5862
obj['text'] == text and
5963
obj['comment'] == comment for obj in self.data):
6064
return
61-
self.data.append({
62-
'id': len(self.data),
63-
'time': time.time(),
64-
'type': 'comment',
65-
'tag': self.safe_tag(tag),
66-
'url': url,
67-
'text': text,
68-
'comment': comment,
69-
'name': name,
70-
'status': 'new',
71-
})
72-
self.save()
65+
if isinstance(url, str) and isinstance(text, str) and \
66+
isinstance(comment, str) and isinstance(name, str):
67+
self.data.append({
68+
'id': len(self.data),
69+
'time': time.time(),
70+
'type': 'comment',
71+
'tag': self.safe_tag(tag),
72+
'url': url,
73+
'text': text,
74+
'comment': comment,
75+
'name': name,
76+
'status': 'new',
77+
})
78+
self.save()
79+
else:
80+
abort(400, "Invalid JSON object")
7381

7482
def chapter_comment(self, url, comment, name, email):
7583
if any(obj['type'] == 'chapter_comment' and
7684
obj['url'] == url and
7785
obj['comment'] == comment for obj in self.data):
7886
return
79-
self.data.append({
80-
'id': len(self.data),
81-
'time': time.time(),
82-
'type': 'chapter_comment',
83-
'url': url,
84-
'comment': comment,
85-
'name': name,
86-
'email': email,
87-
'status': 'new',
88-
})
89-
self.save()
87+
if isinstance(url, str) and isinstance(email, str) and \
88+
isinstance(comment, str) and isinstance(name, str):
89+
self.data.append({
90+
'id': len(self.data),
91+
'time': time.time(),
92+
'type': 'chapter_comment',
93+
'url': url,
94+
'comment': comment,
95+
'name': name,
96+
'email': email,
97+
'status': 'new',
98+
})
99+
self.save()
100+
else:
101+
abort(400, "Invalid JSON object")
90102

91103
def status(self, i):
92104
return self.data[i]["status"]

0 commit comments

Comments
 (0)