|
1 | 1 | # html-escaper
|
2 |
| -A module to escape/unescape common problematic entities done the right way. |
| 2 | +A simple module to escape/unescape common problematic entities. |
| 3 | + |
| 4 | + |
| 5 | +### How |
| 6 | +This package is available in npm so `npm install html-escaper` is all you need to do, using eventually the global flag too. |
| 7 | + |
| 8 | +Once the module is present |
| 9 | +```js |
| 10 | +var html = require('html-escaper'); |
| 11 | + |
| 12 | +// two basic methods |
| 13 | +html.escape('string'); |
| 14 | +html.unescape('escaped string'); |
| 15 | +``` |
| 16 | + |
| 17 | + |
| 18 | +### Why |
| 19 | +there is basically one rule only: do not **ever** replace one char after another if you are transforming a string into another. |
| 20 | + |
| 21 | +```js |
| 22 | +// WARNING: THIS IS WRONG |
| 23 | +// if you are that kind of dev that does this |
| 24 | +function escape(s) { |
| 25 | + return s.replace(/&/g, "&") |
| 26 | + .replace(/</g, "<") |
| 27 | + .replace(/>/g, ">") |
| 28 | + .replace(/'/g, "'") |
| 29 | + .replace(/"/g, """); |
| 30 | +} |
| 31 | + |
| 32 | +// you might be the same dev that does this too |
| 33 | +function unescape(s) { |
| 34 | + return s.replace(/&/g, "&") |
| 35 | + .replace(/</g, "<") |
| 36 | + .replace(/>/g, ">") |
| 37 | + .replace(/'/g, "'") |
| 38 | + .replace(/"/g, '"'); |
| 39 | +} |
| 40 | + |
| 41 | +// guess what we have here ? |
| 42 | +unescape('&lt;'); |
| 43 | + |
| 44 | +// now guess this XSS too ... |
| 45 | +unescape('&lt;script&gt;alert("yo")&lt;/script&gt;'); |
| 46 | + |
| 47 | + |
| 48 | +``` |
| 49 | + |
| 50 | +The last example will produce `<script>alert("yo")</script>` instead of the expected `<script>alert("yo")</script>`. |
| 51 | + |
| 52 | +Nothing like this could possibly happen if we grab all chars at once and either ways. |
| 53 | +It's just a fortunate case that after swapping `&` with `&` no other replace will be affected, but it's not portable and universally a bad practice. |
| 54 | + |
| 55 | +Grab all chars at once, no excuses! |
| 56 | + |
| 57 | + |
| 58 | + |
| 59 | +**more details** |
| 60 | +As somebody might think it's an `unescape` issue only, it's not. Being an anti-pattern with side effects works both ways. |
| 61 | + |
| 62 | +As example, changing the order of the replacement in escaping would produce the unexpected: |
| 63 | +```js |
| 64 | +function escape(s) { |
| 65 | + return s.replace(/</g, "<") |
| 66 | + .replace(/>/g, ">") |
| 67 | + .replace(/'/g, "'") |
| 68 | + .replace(/"/g, """) |
| 69 | + .replace(/&/g, "&"); |
| 70 | +} |
| 71 | + |
| 72 | +escape('<'); // &lt; instead of < |
| 73 | +``` |
| 74 | +If we do not want to code with the fear that the order wasn't perfect or that our order in either escaping or unescaping is different from the order another method or function used, if we understand the issue and we agree it's potentially a disaster prone approach, if we add the fact in this case creating 4 RegExp objects each time and invoking 4 times `.replace` trough the `String.prototype` is also potentially slower than creating one function only holding one object, or holding the function too, we should agree there is not absolutely any valid reason to keep proposing a char-by-char implementation. |
| 75 | + |
| 76 | +We have proofs this approach can fail already so ... why should we risk? Just avoid and grab all chars at once or simply use this tiny utility. |
0 commit comments