Skip to content

Commit

Permalink
clj-kondo: check regex error prone double escaping
Browse files Browse the repository at this point in the history
  • Loading branch information
cnrpman authored and tiensonqin committed Mar 30, 2023
1 parent dd86856 commit 770fdc5
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 3 deletions.
4 changes: 3 additions & 1 deletion .clj-kondo/config.edn
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

:linters
{:path-invalid-construct/string-join {:level :info}
:regex-checks/double-escaped-regex {:level :warning}
:aliased-namespace-symbol {:level :warning}
;; Disable until it doesn't trigger false positives on rum/defcontext
:earmuffed-var-not-dynamic {:level :off}
Expand Down Expand Up @@ -113,7 +114,8 @@

:hooks {:analyze-call {rum.core/defc hooks.rum/defc
rum.core/defcs hooks.rum/defcs
clojure.string/join hooks.path-invalid-construct/string-join}}
clojure.string/join hooks.path-invalid-construct/string-join
clojure.string/replace hooks.regex-checks/double-escaped-regex}}
:lint-as {promesa.core/let clojure.core/let
promesa.core/loop clojure.core/loop
promesa.core/recur clojure.core/recur
Expand Down
1 change: 0 additions & 1 deletion .clj-kondo/hooks/path_invalid_construct.clj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
(defn string-join
[{:keys [node]}]
(let [[_ sep-v & _args] (:children node)]
;; (prn :string-join)
(when (and (api/string-node? sep-v)
(= ["/"] (:lines sep-v)))
(api/reg-finding! (assoc (meta node)
Expand Down
15 changes: 15 additions & 0 deletions .clj-kondo/hooks/regex_checks.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
(ns hooks.regex-checks
"This hook try to find out those error-prone double escaping regex expressions"
(:require [clj-kondo.hooks-api :as api]))

(def double-escaped-checker #"\(re-pattern .*\\\\\\\\[\?\#\|\.\^\$\\\+].*\)")

(defn double-escaped-regex
[{:keys [node]}]
(let [[_ _content regex & _args] (:children node)
regex-string (str (api/sexpr regex))]
(when (and (= (api/tag regex) :regex)
(re-matches double-escaped-checker regex-string))
(api/reg-finding! (assoc (meta regex)
:message (str "double slash (\\\\) found in this regular expression followed by a regex special character (, + , * , ? , ^ , $ , | , \\ ), are you mistakenly double escaped a special character? Only escape one-time is required. No escape is required in character class []. (use #_{:clj-kondo/ignore [:regex-checks/double-escaped-regex]} to ignore)")
:type :regex-checks/double-escaped-regex)))))
8 changes: 7 additions & 1 deletion src/main/frontend/util/fs.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@
(defn legacy-dot-file-name-sanity
[page-name]
(when (string? page-name)
;; Bug in original code, but doesn't affect the result
;; https://github.com/logseq/logseq/blob/1519e35e0c8308d8db90b2525bfe7a716c4cdf04/src/main/frontend/util.cljc#L892
#_{:clj-kondo/ignore [:regex-checks/double-escaped-regex]}
(let [normalize (fn [s] (.normalize s "NFC"))
remove-boundary-slashes (fn [s] (when (string? s)
(let [s (if (= \/ (first s))
Expand All @@ -165,9 +168,12 @@
(defn legacy-url-file-name-sanity
[page-name]
(let [url-encode #(some-> % str (js/encodeURIComponent) (.replace "+" "%20"))]
;; Bug in original code, but doesn't affect the result
;; https://github.com/logseq/logseq/blob/1519e35e0c8308d8db90b2525bfe7a716c4cdf04/src/main/frontend/util.cljc#L892
#_{:clj-kondo/ignore [:regex-checks/double-escaped-regex]}
(some-> page-name
gp-util/page-name-sanity
;; for android filesystem compatibility
;; for android filesystem compatibility
(string/replace #"[\\#|%]+" url-encode)
;; Windows reserved path characters
(string/replace #"[:\\*\\?\"<>|]+" url-encode)
Expand Down

0 comments on commit 770fdc5

Please sign in to comment.