diff --git a/forum/forum.py b/forum/forum.py index 30fa4f0..1b950a3 100644 --- a/forum/forum.py +++ b/forum/forum.py @@ -5,6 +5,7 @@ Le sujet fixe « Bulletin de la semaine du Vercors » est toujours épinglé en """ import hashlib import os +import re import secrets import threading @@ -299,6 +300,8 @@ class Forum: s = (s or "").strip() if not s or len(s) > maxi: raise Invalide("%s vide ou trop long" % nom) + if re.search(r"[\x00-\x08\x0b\x0c\x0e-\x1f\x7f]", s): + raise Invalide("%s : caractères de contrôle refusés" % nom) return s def _nouveau_sujet(self, user, titre, corps, ip, status="publie", station=None, prets=(), format_="texte", diff --git a/forum/markdown_sur.py b/forum/markdown_sur.py index fdcfa98..52e7fb4 100644 --- a/forum/markdown_sur.py +++ b/forum/markdown_sur.py @@ -10,16 +10,19 @@ import re CODE = re.compile(r"`([^`\n]+)`") LIEN = re.compile(r"\[([^\]\n]+)\]\(([^)\s]+)\)") URL_OK = re.compile(r"^https?://[^\s\x00]+$", re.I) -GRAS = re.compile(r"\*\*(?=\S)(.+?)(?<=\S)\*\*") -ITALIQUE = re.compile(r"(? HTML en ligne (le code est protégé des autres règles).""" + t = t[:LIGNE_MAX * 6] # texte échappé : une ligne de LIGNE_MAX caractères au plus codes = [] def garder(m): @@ -37,7 +40,11 @@ def _en_ligne(t): t = LIEN.sub(lien, t) t = GRAS.sub(r"\1", t) t = ITALIQUE.sub(lambda m: "%s" % (m.group(1) or m.group(2)), t) - return re.sub("\x00(\\d+)\x00", lambda m: codes[int(m.group(1))], t) + def remettre(m): + i = int(m.group(1)) + return codes[i] if i < len(codes) else m.group(0) + + return re.sub("\x00(\\d+)\x00", remettre, t) def _cellules(ligne): @@ -59,7 +66,9 @@ def _alignements(sep): def rendu(texte): """Markdown -> HTML sûr.""" + texte = CONTROLE.sub("", texte or "") # \x00 sert de marqueur interne lignes = html.escape((texte or "").replace("\r\n", "\n").replace("\r", "\n"), quote=True).split("\n") + lignes = [x[:LIGNE_MAX * 6] for x in lignes] # lignes très longues coupées : temps de calcul borné out, para, i = [], [], 0 def fermer_para(): @@ -74,10 +83,10 @@ def rendu(texte): i += 1 continue m = TITRE.match(l) - if m: + if m and m.group(2).rstrip().rstrip("#").rstrip(): fermer_para() n = len(m.group(1)) + 1 # # -> h2 (le h1 est le titre du sujet) - out.append("%s" % (n, _en_ligne(m.group(2)), n)) + out.append("%s" % (n, _en_ligne(m.group(2).rstrip().rstrip("#").rstrip()), n)) i += 1 continue if "|" in l and i + 1 < len(lignes) and SEPARATEUR.match(lignes[i + 1]) and "|" in lignes[i + 1]: diff --git a/forum/server.py b/forum/server.py index 75de117..99d9e79 100644 --- a/forum/server.py +++ b/forum/server.py @@ -1206,6 +1206,8 @@ class Handler(BaseHTTPRequestHandler): d.get("station", ""), d.get("prevenir") == "1") return self.envoyer(200, json.dumps({"id": bid, "heure": heure_fr(maj)}, ensure_ascii=False), "application/json; charset=utf-8") if chemin == "/apercu-md": # aperçu instantané : même rendu que l'affichage + if not roles.peut(f.roles_de(user), "creer_sujet"): + raise Refus("publication réservée") return self.envoyer(200, markdown_sur.rendu(d.get("corps", "")[:CORPS_MAX])) if chemin == "/brouillon/supprimer": f.supprimer_brouillon(user, entier(d.get("brouillon", ""), 0)) diff --git a/forum/tests/test_publier.py b/forum/tests/test_publier.py index d2f8b0f..08faf10 100644 --- a/forum/tests/test_publier.py +++ b/forum/tests/test_publier.py @@ -37,6 +37,28 @@ class TestMarkdown(unittest.TestCase): self.assertIn("
  1. a
  2. b
", h) self.assertIn("**pas gras**", markdown_sur.rendu("`**pas gras**`")) + def test_caracteres_de_controle(self): + h = markdown_sur.rendu("a \x005\x00 `c` \x000\x00 b\x07") + self.assertNotIn("\x00", h) + self.assertIn("c", h) + self.assertEqual(markdown_sur._en_ligne("\x009\x00"), "\x009\x00") # marqueur inconnu : pas d'IndexError + f = base.Forum() + u = f.fixer_proprietaire("theo", "Théo") + for titre, corps in (("t\x00", "c"), ("t", "a\x005\x00")): + with self.assertRaises(Invalide): + f.creer_sujet(u, titre, corps) + f.creer_sujet(u, "t", "ligne\r\nsuite\ttab") # retours à la ligne et tabulations restent permis + + def test_entrees_pieges_rapides(self): + import time + for piege in ("# " + " " * 20000 + "x", "#" * 3 + " " + "#" * 20000 + " x", "**" + "a " * 20000, + "*a" * 20000, "_a " * 20000, "| " * 20000 + "\n|" + "-|" * 20000, "[" * 20000 + "](" * 20000): + debut = time.perf_counter() + markdown_sur.rendu(piege) + self.assertLess(time.perf_counter() - debut, 1.0, piege[:20]) + self.assertIn("

Titre

", markdown_sur.rendu("# Titre ## ")) + self.assertNotIn("

", markdown_sur.rendu("# ###")) + def test_tableaux(self): h = markdown_sur.rendu("| Station | Tmin | Note |\n| --- | ---: | :-: |\n| Gêve | -4,2 | **gel** |\n| x | 1 |") self.assertIn("", h) @@ -136,6 +158,8 @@ class TestPublier(base.BaseHTTP): self.assertNotIn("