浏览代码

chore: disallow user idents from clobbering internal idents

If users can clobber internal idents it would create bugs and possibly
security issues for internal idents associated with security.
Also updated docstrings of related fn
Gabriel Horner 6 月之前
父节点
当前提交
90dbdd6cdc
共有 1 个文件被更改,包括 4 次插入2 次删除
  1. 4 2
      deps/db/src/logseq/db/frontend/db_ident.cljc

+ 4 - 2
deps/db/src/logseq/db/frontend/db_ident.cljc

@@ -56,18 +56,20 @@
                      (str id)))
                      (str id)))
          id)))))
          id)))))
 
 
-;; TODO: db ident should obey clojure's rules for keywords
 (defn create-db-ident-from-name
 (defn create-db-ident-from-name
   "Creates a :db/ident for a class or property by sanitizing the given name.
   "Creates a :db/ident for a class or property by sanitizing the given name.
+  The created ident should obey clojure's rules for keywords.
 
 
    NOTE: Only use this when creating a db-ident for a new class/property. Using
    NOTE: Only use this when creating a db-ident for a new class/property. Using
    this in read-only contexts like querying can result in db-ident conflicts"
    this in read-only contexts like querying can result in db-ident conflicts"
   [user-namespace name-string]
   [user-namespace name-string]
   {:pre [(or (keyword? user-namespace) (string? user-namespace)) (string? name-string)]}
   {:pre [(or (keyword? user-namespace) (string? user-namespace)) (string? name-string)]}
+  (assert (not (re-find #"^(logseq|block)(\.|$)" (name user-namespace)))
+          "New ident is not allowed to use an internal namespace")
   (if #?(:org.babashka/nbb (some? js/process)
   (if #?(:org.babashka/nbb (some? js/process)
          :cljs (exists? js/process)
          :cljs (exists? js/process)
          :default false)
          :default false)
-    ;; So that we don't have to change :user.{property|class} in our tests
+    ;; Used for contexts where we want repeatable idents e.g. tests and CLIs
     (keyword user-namespace (-> name-string (string/replace #"[/()]|\s+" "-") (string/replace-first #"^(\d)" "NUM-$1")))
     (keyword user-namespace (-> name-string (string/replace #"[/()]|\s+" "-") (string/replace-first #"^(\d)" "NUM-$1")))
     (keyword user-namespace
     (keyword user-namespace
              (str
              (str