Merge pull request #347 from status-im/feature/command-name-validation-#235
Command name validation (#235)
Former-commit-id: d188b59803
This commit is contained in:
commit
d40125cd79
|
@ -9,7 +9,8 @@
|
|||
[status-im.utils.types :refer [json->clj]]
|
||||
[status-im.commands.utils :refer [reg-handler]]
|
||||
[status-im.constants :refer [console-chat-id wallet-chat-id]]
|
||||
[taoensso.timbre :as log]))
|
||||
[taoensso.timbre :as log]
|
||||
[status-im.utils.homoglyph :as h]))
|
||||
|
||||
(def commands-js "commands.js")
|
||||
|
||||
|
@ -36,8 +37,8 @@
|
|||
(dispatch [::validate-hash identity (slurp "resources/commands.js")])
|
||||
#_(http-get (s/join "/" [url commands-js])
|
||||
|
||||
#(dispatch [::validate-hash identity %])
|
||||
#(dispatch [::loading-failed! identity ::file-was-not-found])))))
|
||||
#(dispatch [::validate-hash identity %])
|
||||
#(dispatch [::loading-failed! identity ::file-was-not-found])))))
|
||||
|
||||
(defn dispatch-loaded!
|
||||
[db [identity file]]
|
||||
|
@ -75,17 +76,27 @@
|
|||
(map (fn [[k v]] [k (assoc v :type as)]))
|
||||
(into {})))
|
||||
|
||||
(defn filter-forbidden-names [id commands]
|
||||
(->> commands
|
||||
(remove (fn [[n]]
|
||||
(and
|
||||
(not (= console-chat-id id))
|
||||
(h/matches (name n) "password"))))
|
||||
(into {})))
|
||||
|
||||
(defn add-commands
|
||||
[db [id _ {:keys [commands responses autorun] :as data}]]
|
||||
(-> db
|
||||
(update-in [id :commands] merge (mark-as :command commands))
|
||||
(update-in [id :responses] merge (mark-as :response responses))
|
||||
(assoc-in [id :commands-loaded] true)
|
||||
(assoc-in [id :autorun] autorun)))
|
||||
[db [id _ {:keys [commands responses autorun]}]]
|
||||
(let [commands' (filter-forbidden-names id commands)
|
||||
responses' (filter-forbidden-names id responses)]
|
||||
(-> db
|
||||
(update-in [id :commands] merge (mark-as :command commands'))
|
||||
(update-in [id :responses] merge (mark-as :response responses'))
|
||||
(assoc-in [id :commands-loaded] true)
|
||||
(assoc-in [id :autorun] autorun))))
|
||||
|
||||
(defn save-commands-js!
|
||||
[_ [id file]]
|
||||
(commands/save {:chat-id id :file file}))
|
||||
(commands/save {:chat-id id :file file}))
|
||||
|
||||
(defn loading-failed!
|
||||
[db [id reason details]]
|
||||
|
|
|
@ -2,17 +2,16 @@
|
|||
(:require [cljs.spec :as s]
|
||||
[status-im.utils.phone-number :refer [valid-mobile-number?]]
|
||||
[status-im.constants :refer [console-chat-id wallet-chat-id]]
|
||||
[clojure.string :as str]))
|
||||
|
||||
(def homoglyph-finder (js/require "homoglyph-finder"))
|
||||
[clojure.string :as str]
|
||||
[status-im.utils.homoglyph :as h]))
|
||||
|
||||
(defn not-illegal-name? [username]
|
||||
(let [username (some-> username (str/trim))]
|
||||
(and (not (.isMatches homoglyph-finder username console-chat-id))
|
||||
(not (.isMatches homoglyph-finder username wallet-chat-id)))))
|
||||
(and (not (h/matches username console-chat-id))
|
||||
(not (h/matches username wallet-chat-id)))))
|
||||
|
||||
(s/def ::not-empty-string (s/and string? not-empty))
|
||||
(s/def ::not-illegal-name not-illegal-name?)
|
||||
|
||||
(s/def ::name (s/and ::not-empty-string
|
||||
::not-illegal-name))
|
||||
::not-illegal-name))
|
||||
|
|
|
@ -1,14 +1,13 @@
|
|||
(ns status-im.profile.validations
|
||||
(:require [cljs.spec :as s]
|
||||
[status-im.constants :refer [console-chat-id wallet-chat-id]]
|
||||
[clojure.string :as str]))
|
||||
|
||||
(def homoglyph-finder (js/require "homoglyph-finder"))
|
||||
[clojure.string :as str]
|
||||
[status-im.utils.homoglyph :as h]))
|
||||
|
||||
(defn correct-name? [username]
|
||||
(let [username (some-> username (str/trim))]
|
||||
(and (not (.isMatches homoglyph-finder username console-chat-id))
|
||||
(not (.isMatches homoglyph-finder username wallet-chat-id)))))
|
||||
(and (not (h/matches username console-chat-id))
|
||||
(not (h/matches username wallet-chat-id)))))
|
||||
|
||||
(defn correct-email? [email]
|
||||
(let [pattern #"[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?"]
|
||||
|
|
|
@ -0,0 +1,7 @@
|
|||
(ns status-im.utils.homoglyph
|
||||
(:require [status-im.utils.utils :as u]))
|
||||
|
||||
(def homoglyph-finder (u/require "homoglyph-finder"))
|
||||
|
||||
(defn matches [s1 s2]
|
||||
(.isMatches homoglyph-finder s1 s2))
|
Loading…
Reference in New Issue