"""Markup written by a model and rendered in somebody's browser. The uploads route already serves SVG under a sandbox CSP. This is the belt to that brace: what a model hands back is checked before it is stored, because "the model would not do that" is not a security control. Run: DATABASE_URL=sqlite:///:memory: PYTHONPATH=backend python -m unittest discover -s backend/tests """ import os os.environ.setdefault("DATABASE_URL", "sqlite:///:memory:") import unittest from app.services import illustrate def svg(inner: str = '', attrs: str = '') -> str: return (f'{inner}') class WhatIsAccepted(unittest.TestCase): def test_a_plain_diagram_passes(self): self.assertTrue(illustrate.check(svg())) def test_shapes_text_and_markers_pass(self): self.assertTrue(illustrate.check(svg( '' 'Day 3'))) def test_a_path_may_contain_the_letters_of_a_command(self): # `d` is full of letters; the remote-reference check must not read a # curve as a URL. self.assertTrue(illustrate.check(svg(''))) class WhatIsRefused(unittest.TestCase): def test_script(self): self.assertIsNone(illustrate.check(svg(''))) def test_an_event_handler(self): self.assertIsNone(illustrate.check(svg(attrs='onload="steal()"'))) self.assertIsNone(illustrate.check(svg(''))) def test_anything_that_reaches_outside_the_document(self): self.assertIsNone(illustrate.check(svg(''))) self.assertIsNone(illustrate.check(svg(''))) self.assertIsNone(illustrate.check( svg(''))) def test_foreign_objects_and_frames(self): self.assertIsNone(illustrate.check(svg('hi'))) self.assertIsNone(illustrate.check(svg('