From aa77d7ba9e31921e5a66ab9459a8be86b7b83537 Mon Sep 17 00:00:00 2001 From: Andelf Date: Mon, 29 Nov 2021 23:34:49 +0800 Subject: [PATCH] fix: avoid cyclical refs and deep refs Fixes #3305 --- src/main/frontend/components/block.cljs | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/main/frontend/components/block.cljs b/src/main/frontend/components/block.cljs index abf88d5f8..c32457a7f 100644 --- a/src/main/frontend/components/block.cljs +++ b/src/main/frontend/components/block.cljs @@ -89,6 +89,7 @@ ;; TODO: dynamic (defonce max-blocks-per-page 200) +(defonce max-depth-of-links 5) (defonce *blocks-container-id (atom 0)) ;; TODO: @@ -857,8 +858,12 @@ (let [{:keys [url label title metadata full_text]} link] (match url ["Block_ref" id] - (let [label* (if (seq (mldoc/plain->text label)) label nil)] - (block-reference (assoc config :reference? true) id label*)) + (let [label* (if (seq (mldoc/plain->text label)) label nil) + {:keys [link-depth]} config + link-depth (or link-depth 0)] + (if (> link-depth max-depth-of-links) + [:p.warning.text-sm "Block ref nesting is too deep"] + (block-reference (assoc config :reference? true :link-depth (inc link-depth)) id label*))) ["Page_ref" page] (let [format (get-in config [:block :block/format])] @@ -1200,16 +1205,21 @@ (ui/tweet-embed id)))) (= name "embed") - (let [a (first arguments)] + (let [a (first arguments) + {:keys [link-depth]} config + link-depth (or link-depth 0)] (cond (nil? a) ; empty embed nil + (> link-depth max-depth-of-links) + [:p.warning.text-sm "Embed depth is too deep"] + (and (string/starts-with? a "[[") (string/ends-with? a "]]")) (let [page-name (text/get-page-name a)] (when-not (string/blank? page-name) - (page-embed config page-name))) + (page-embed (assoc config :link-depth (inc link-depth)) page-name))) (and (string/starts-with? a "((") (string/ends-with? a "))")) @@ -1220,7 +1230,7 @@ (let [s (string/trim s)] (and (util/uuid-string? s) (uuid s))))] - (block-embed config id))) + (block-embed (assoc config :link-depth (inc link-depth)) id))) :else ;TODO: maybe collections? nil))