Key tampering: what are protective measures if any?
Oct 31, 2018 19:43:21 GMT -8
Peter and ₪» ⅀ ƪ Ƒ «₪ like this
Post by bartlesby on Oct 31, 2018 19:43:21 GMT -8
I'd like to preface by saying this isn't something that has come up for me personally or likely even many other people. Communities can handle bad actors in house. I'm asking more out of curiosity than urgent need. I apologize if it's already been addressed.
I recently made a plugin to add likes to conversations, which utilizes the Message key, and after getting the core work done, it occurred to me just how easy it is for a user to force whatever data into the key that they want. This isn't a huge problem with keys set on users because the information stored there is only pertinent to them but when it comes to keys used to share information across users, it becomes a potential trainwreck.
Now, when I wrote that plugin, I made a serious goof. I caught it before putting it into action but I was embarrassed that I made it at all. The values I was putting into and taking out from the key were going to be displayed-- as html. I'm sure some of you know where that's going.
For everybody else, here's a very abridged example of what I did wrong. First, the code to set the key:
Then the code to display it for all users.
So what's wrong there? The user can change addToKey to whatever they like using the browser console. If instead of "user_name_here", they decide to use "<script>console.log('gotcha!');</script>", suddenly every user is executing potentially malicious script.
That's my dumb mistake and I just stripped the tags using regex before putting anything from the key into the DOM.
I'm sure there are better ways to go about it but I found that the most convenient. Anyway, after worrying about the malicious I started worrying about the annoying. Perhaps somebody filling up the entire key, putting in false information, messing with parsing, etc. That gets to the crux of my question, of course:
How do you secure your keys and how secure can they be? It seems to me you can't prevent somebody from putting in whatever information they like. Can it be worked around and what are your limitations?
I recently made a plugin to add likes to conversations, which utilizes the Message key, and after getting the core work done, it occurred to me just how easy it is for a user to force whatever data into the key that they want. This isn't a huge problem with keys set on users because the information stored there is only pertinent to them but when it comes to keys used to share information across users, it becomes a potential trainwreck.
Now, when I wrote that plugin, I made a serious goof. I caught it before putting it into action but I was embarrassed that I made it at all. The values I was putting into and taking out from the key were going to be displayed-- as html. I'm sure some of you know where that's going.
For everybody else, here's a very abridged example of what I did wrong. First, the code to set the key:
var addToKey = "user_name_here";
pb.plugin.key(key).set({ object_id: 42, value: addToKey });
Then the code to display it for all users.
var keyString = pb.plugin.key(key).get(42); // returns addToKey
var content = "<a href='doesnt_matter'>" + keyString + "</a>";
$(targetElement).html(content);
So what's wrong there? The user can change addToKey to whatever they like using the browser console. If instead of "user_name_here", they decide to use "<script>console.log('gotcha!');</script>", suddenly every user is executing potentially malicious script.
That's my dumb mistake and I just stripped the tags using regex before putting anything from the key into the DOM.
var keyString = pb.plugin.key(key).get(42); // returns addToKey
keyString = keyString.replace(/(<([^>]+)>)/ig,"");
var content = "<a href='doesnt_matter'>" + keyString + "</a>";
$(targetElement).html(content);
I'm sure there are better ways to go about it but I found that the most convenient. Anyway, after worrying about the malicious I started worrying about the annoying. Perhaps somebody filling up the entire key, putting in false information, messing with parsing, etc. That gets to the crux of my question, of course:
How do you secure your keys and how secure can they be? It seems to me you can't prevent somebody from putting in whatever information they like. Can it be worked around and what are your limitations?