-
Notifications
You must be signed in to change notification settings - Fork 987
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[FIX #2565] data-store: escape leading tildes in message content #2612
[FIX #2565] data-store: escape leading tildes in message content #2612
Conversation
Hi @siphiuel thanks for your contribution! One of our team members will review shortly - in the meantime are you already part of the conversation on our Riot? I'd like to invite you at https://chat.status.im/#/register and ping me @hutch when you join! Thanks. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add tests to validate this behavior.
Transit is used for JSON parsing." | ||
[{:keys [content] :as message}] | ||
(if (and (not (string/blank? content)) | ||
(string/starts-with? content "~")) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like ~
is not the only special character. Also we probably want to do this generically, not only for this particular field? @
7107f6a
to
b1b53a8
Compare
Hi! Thank you for reviewing! Made the following changes:
I did a force push and some of your comments (the ones regarding items #2 and #3) seem to be gone. Sorry for that. |
Thanks! It's not clear to me if escaped characters will be deserialized correctly. Could we add a round-trip test that would validate the the full serialize / deserialize logic? |
Hi! There's a round-trip test at line #25:
It checks where Clojure data structure survives the conversion in both directions intact. Do you think it is enough? Or should I add more tests of that kind? |
Great I missed it! |
) | ||
(str "~" e) | ||
(walk/walk walk-fn identity e)))] | ||
(walk/walk walk-fn identity message))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could use postwalk
, then no need to use recursion inside walk-fn
.
Something like
(letfn [(walk-fn [e]
(cond->> e
(to-be-escaped? e)
(str escape-char)))]
(walk/postwalk walk-fn message))
But this is just a suggestion
(if (and (string? e) | ||
(first e) | ||
(contains? #{"~" "^" "`"} (first e)) | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please fix trailing parenth https://github.com/bbatsov/clojure-style-guide#gather-trailing-parens
[message] | ||
(letfn [(walk-fn [e] | ||
(if (and (string? e) | ||
(first e) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need for this check, i mean (first e)
(letfn [(walk-fn [e] | ||
(if (and (string? e) | ||
(first e) | ||
(contains? #{"~" "^" "`"} (first e)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please extract this set of characters which should be escaped to a constant.
(first e) | ||
(contains? #{"~" "^" "`"} (first e)) | ||
) | ||
(str "~" e) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please extract magic value ("~"
) to constant
fetching from Realm where Transit is used for JSON parsing." | ||
[message] | ||
(letfn [(walk-fn [e] | ||
(if (and (string? e) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also would be great to extract all these predicates to separate function
|
||
(deftest transit-preparation | ||
(testing "Check if leading Transit special characters are properly escaped with tildes" | ||
(let [data {:to-be-escaped1 "~bad string" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please align values in maps and let
forms
:to-be-escaped3 "`and another bad string" | ||
:no-escaping "no escaping" | ||
:vector-content ["a" "b" "c"]} | ||
prepare-for-transit #'core/prepare-for-transit |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the reason for not using core/prepare-for-transit
directly?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See comments above
Hi, thanks for reviewing! Made the requested changes, details below:
|
I just haven't noticed that function is "private". Yeah it doesn't work in cljs and tbh i see no reason for using
We usually align let-binding values, so it should look like (let [data {:to-be-escaped1 "~bad string"
:to-be-escaped2 "^another bad string"
:to-be-escaped3 "`and another bad string"
:no-escaping "no escaping"
:vector-content ["a" "b" "c"]}
prepared-data (core/prepare-for-transit data)]
...) not (let [data {:to-be-escaped1 "~bad string"
:to-be-escaped2 "^another bad string"
:to-be-escaped3 "`and another bad string"
:no-escaping "no escaping"
:vector-content ["a" "b" "c"]}
prepared-data (core/prepare-for-transit data)]
...) |
Got it, made the changes. |
I can squash if there are no other changes to be done. |
@siphiuel, yes please squash commits |
ebe44a2
to
e307794
Compare
Done. |
Per Transit documentation, leading tildes have to be escaped, otherwise they are treated as tagged values.