Self-hosted homelab infrastructure visualizer — interactive network diagram with live status monitoring
TypeScript diagramhomelabhomelab-infrastructure-observabilitynetworkproxmox-veself-hostedzigbeezwavezwave-js
fix(docs): stop the frontmatter regex going quadratic on an open block
Pouzorf0b5175155bb
diff --git a/backend/app/services/doc_tree.py b/backend/app/services/doc_tree.pyindex 138f5cf..f52d7b6 100644--- a/backend/app/services/doc_tree.py+++ b/backend/app/services/doc_tree.py@@ -24,7 +24,11 @@ from app.db.models import Document# A document is addressed by id; the slug is for readable URLs and export# filenames, so it only has to be filesystem- and URL-safe._SLUG_STRIP = re.compile(r"[^a-z0-9]+")-_FRONTMATTER = re.compile(r"\A---\s*\n(.*?)\n---\s*(?:\n|\Z)", re.DOTALL)+# `[ \t]*`, not `\s*`: `\s` matches the newline the fence line ends with, and an+# alternative that can be reached two ways makes the match quadratic on a body+# that opens with `---` and never closes it — the shape a half-typed document+# has for as long as the user is typing it.+_FRONTMATTER = re.compile(r"\A---[ \t]*\n(.*?)\n---[ \t]*(?:\n|\Z)", re.DOTALL)DOCUMENT_KINDS = frozenset({"device", "node", "design", "page", "folder"})diff --git a/backend/tests/test_doc_tree.py b/backend/tests/test_doc_tree.pyindex 6264ae3..f547289 100644--- a/backend/tests/test_doc_tree.py+++ b/backend/tests/test_doc_tree.py@@ -84,6 +84,26 @@ def test_parse_frontmatter_of_an_empty_body_is_empty():assert doc_tree.parse_frontmatter("") == {}+def test_parse_frontmatter_keeps_a_trailing_space_on_the_fence():+ assert doc_tree.parse_frontmatter("--- \ntitle: NAS\n--- \n") == {"title": "NAS"}+++def test_parse_frontmatter_of_an_unclosed_block_does_not_go_quadratic():+ """A body that opens a block and never closes it must still be linear.++ The shape a document has while the block is being typed, and the input the+ scanner named: `---\n` then many repetitions of `\n `. With `\s*` around the+ fences the closing alternative was reachable two ways and each added line+ multiplied the backtracking.+ """+ import time++ body = "---\n" + "\n " * 40_000+ started = time.perf_counter()+ assert doc_tree.parse_frontmatter(body) == {}+ assert time.perf_counter() - started < 1.0++def test_tags_accepts_a_list_or_a_comma_string():assert doc_tree.tags_from({"tags": ["a", " b "]}) == ["a", "b"]assert doc_tree.tags_from({"tags": "a, b"}) == ["a", "b"]diff --git a/frontend/src/documentation/__tests__/frontmatter.test.ts b/frontend/src/documentation/__tests__/frontmatter.test.tsindex 7827f53..1725fe7 100644--- a/frontend/src/documentation/__tests__/frontmatter.test.ts+++ b/frontend/src/documentation/__tests__/frontmatter.test.ts@@ -36,6 +36,20 @@ describe('parseFrontmatter', () => {expect(parseFrontmatter('---\n- a\n- b\n---\nbody').data).toEqual({})})+ it('keeps a trailing space on the fence line', () => {+ expect(parseFrontmatter('--- \ntitle: NAS\n--- \n').data).toEqual({ title: 'NAS' })+ })++ it('stays linear on a block that was opened and never closed', () => {+ // What a document looks like while the block is being typed. With `\s*`+ // around the fences the closing alternative was reachable two ways, and+ // each added line multiplied the backtracking.+ const body = `---\n${'\n '.repeat(40_000)}`+ const started = performance.now()+ expect(parseFrontmatter(body).data).toEqual({})+ expect(performance.now() - started).toBeLessThan(1000)+ })+it('handles CRLF line endings', () => {expect(parseFrontmatter('---\r\ntitle: NAS\r\n---\r\nbody').data).toEqual({ title: 'NAS' })})diff --git a/frontend/src/documentation/frontmatter.ts b/frontend/src/documentation/frontmatter.tsindex 5a099d9..4056107 100644--- a/frontend/src/documentation/frontmatter.ts+++ b/frontend/src/documentation/frontmatter.ts@@ -9,7 +9,11 @@ import yaml from 'js-yaml'* block parses again.*/-const BLOCK = /^---\s*\r?\n([\s\S]*?)\r?\n---\s*(?:\r?\n|$)/+// `[ \t]*`, not `\s*`: `\s` matches the newline the fence line ends with, so the+// same position is reachable two ways and the match goes quadratic on a body+// that opens with `---` and never closes it — which is what a document looks+// like for as long as the user is still typing the block.+const BLOCK = /^---[ \t]*\r?\n([\s\S]*?)\r?\n---[ \t]*(?:\r?\n|$)/export interface Frontmatter {data: Record<string, unknown>