validate command's name (#235)

This commit is contained in:
Roman Volosovskyi 2016-10-14 09:44:38 +03:00
parent 5a68b1d646
commit ac199ba2fa
4 changed files with 37 additions and 21 deletions

View File

@ -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]]

View File

@ -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))

View File

@ -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])?"]

View File

@ -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))