[fix #5612 #5613] improve contact code validation

- fix contact code validation for universal links
- go to profile when following universal link pointing to
user own contact code
- fix contact code validation for add contact field

spec for validating public-key has been changed to a
regexp that only accept valid normalized public-key

Signed-off-by: Eric Dvorsak <eric@dvorsak.fr>
This commit is contained in:
Eric Dvorsak 2018-08-23 23:52:30 +02:00
parent 11199c13ee
commit 67a86f57e9
No known key found for this signature in database
GPG Key ID: 932AC1CE5F05DE0C
6 changed files with 30 additions and 33 deletions

View File

@ -4,13 +4,14 @@
[cljs.spec.alpha :as spec]
[clojure.string :as string]))
(defn validate-pub-key [whisper-identity {:keys [address public-key]}]
(cond
(string/blank? whisper-identity)
(i18n/label :t/use-valid-contact-code)
(#{(hex/normalize-hex address) (hex/normalize-hex public-key)}
(hex/normalize-hex whisper-identity))
(i18n/label :t/can-not-add-yourself)
(defn own-whisper-identity?
[{{:keys [public-key]} :account/account} whisper-identity]
(= whisper-identity public-key))
(defn validate-pub-key [db whisper-identity]
(cond
(not (spec/valid? :global/public-key whisper-identity))
(i18n/label :t/use-valid-contact-code)))
(i18n/label :t/use-valid-contact-code)
(own-whisper-identity? db whisper-identity)
(i18n/label :t/can-not-add-yourself)))

View File

@ -16,7 +16,7 @@
(handlers/register-handler-fx
:new-chat/set-new-identity
(fn [{{:keys [web3 network network-status] :as db} :db} [_ new-identity]]
(let [new-identity-error (db/validate-pub-key new-identity (:account/account db))]
(let [new-identity-error (db/validate-pub-key db new-identity)]
(if (stateofus/is-valid-name? new-identity)
(let [network (get-in db [:account/account :networks network])
chain (ethereum/network->chain-keyword network)]

View File

@ -55,7 +55,7 @@
(fn [{:keys [db] :as cofx} [_ _ contact-identity]]
(let [current-account (:account/account db)
fx {:db (assoc db :contacts/new-identity contact-identity)}
validation-result (new-chat.db/validate-pub-key contact-identity current-account)]
validation-result (new-chat.db/validate-pub-key db contact-identity)]
(if (some? validation-result)
(utils/show-popup (i18n/label :t/unable-to-read-this-code) validation-result #(re-frame/dispatch [:navigate-to-clean :home]))
(handlers-macro/merge-fx cofx

View File

@ -4,22 +4,9 @@
[status-im.js-dependencies :as dependencies]
[status-im.utils.ethereum.core :as ethereum]))
(defn hex-string? [s]
(let [s' (if (string/starts-with? s "0x")
(subs s 2)
s)]
(boolean (re-matches #"(?i)[0-9a-f]+" s'))))
(defn valid-length? [identity]
(let [length (count identity)]
(and
(hex-string? identity)
(or
(and (= 128 length) (not (string/includes? identity "0x")))
(and (= 130 length) (string/starts-with? identity "0x"))
(and (= 132 length) (string/starts-with? identity "0x04"))
(ethereum/address? identity)))))
(defn valid-public-key? [s]
(boolean (re-matches #"0x04[0-9a-f]{128}" s)))
(spec/def :global/not-empty-string (spec/and string? not-empty))
(spec/def :global/public-key (spec/and :global/not-empty-string valid-length?))
(spec/def :global/address ethereum/address?)
(spec/def :global/public-key (spec/and :global/not-empty-string valid-public-key?))
(spec/def :global/address ethereum/address?)

View File

@ -6,7 +6,10 @@
[status-im.chat.events :as chat.events]
[status-im.models.account :as models.account]
[status-im.ui.components.list-selection :as list-selection]
[status-im.ui.components.react :as react]))
[status-im.ui.components.react :as react]
[cljs.spec.alpha :as spec]
[status-im.ui.screens.navigation :as navigation]
[status-im.ui.screens.add-new.new-chat.db :as new-chat.db]))
(def public-chat-regex #".*/chat/public/(.*)$")
(def profile-regex #".*/user/(.*)$")
@ -37,9 +40,11 @@
(log/info "universal-links: handling public chat " public-chat)
(chat.events/create-new-public-chat public-chat cofx))
(defn handle-view-profile [profile-id cofx]
(defn handle-view-profile [profile-id {:keys [db] :as cofx}]
(log/info "universal-links: handling view profile" profile-id)
(chat.events/show-profile profile-id true cofx))
(if (new-chat.db/own-whisper-identity? db profile-id)
(navigation/navigate-to-cofx :my-profile nil cofx)
(chat.events/show-profile profile-id true cofx)))
(defn handle-not-found [full-url]
(log/info "universal-links: no handler for " full-url))
@ -74,7 +79,7 @@
(match-url url public-chat-regex)
(handle-public-chat (match-url url public-chat-regex) cofx)
(match-url url profile-regex)
(spec/valid? :global/public-key (match-url url profile-regex))
(handle-view-profile (match-url url profile-regex) cofx)
(match-url url browse-regex)

View File

@ -29,10 +29,14 @@
{:db db}))))))
(testing "a user profile link"
(testing "it loads the profile"
(let [actual (links/handle-url "status-im://user/profile-id"
(let [actual (links/handle-url "status-im://user/0x04fbce10971e1cd7253b98c7b7e54de3729ca57ce41a2bfb0d1c4e0a26f72c4b6913c3487fa1b4bb86125770f1743fb4459da05c1cbe31d938814cfaf36e252073"
{:db db})]
(is (= "profile-id" (get-in actual [:db :contacts/identity])))
(is (= "0x04fbce10971e1cd7253b98c7b7e54de3729ca57ce41a2bfb0d1c4e0a26f72c4b6913c3487fa1b4bb86125770f1743fb4459da05c1cbe31d938814cfaf36e252073" (get-in actual [:db :contacts/identity])))
(is (= :profile (get-in actual [:db :view-id]))))))
(testing "if does nothing because the link is invalid"
(is (= (links/handle-url "status-im://user/CONTACTCODE"
{:db db})
nil)))
(testing "a not found url"
(testing "it does nothing"
(is (nil? (links/handle-url "status-im://not-existing"