Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 44108d9

Browse files
committed
added travis
1 parent 980c4ed commit 44108d9

File tree

2 files changed

+83
-1
lines changed

2 files changed

+83
-1
lines changed

.travis.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
language: node_js
2+
node_js:
3+
- 0.10
4+
git:
5+
depth: 1
6+
branches:
7+
only:
8+
- master

README.md

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,76 @@
11
# 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, "&lt;")
27+
.replace(/>/g, "&gt;")
28+
.replace(/'/g, "&#39;")
29+
.replace(/"/g, "&quot;");
30+
}
31+
32+
// you might be the same dev that does this too
33+
function unescape(s) {
34+
return s.replace(/&amp;/g, "&")
35+
.replace(/&lt;/g, "<")
36+
.replace(/&gt;/g, ">")
37+
.replace(/&#39;/g, "'")
38+
.replace(/&quot;/g, '"');
39+
}
40+
41+
// guess what we have here ?
42+
unescape('&amp;lt;');
43+
44+
// now guess this XSS too ...
45+
unescape('&amp;lt;script&amp;gt;alert("yo")&amp;lt;/script&amp;gt;');
46+
47+
48+
```
49+
50+
The last example will produce `<script>alert("yo")</script>` instead of the expected `&lt;script&gt;alert("yo")&lt;/script&gt;`.
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 `&amp;` 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, "&lt;")
66+
.replace(/>/g, "&gt;")
67+
.replace(/'/g, "&#39;")
68+
.replace(/"/g, "&quot;")
69+
.replace(/&/g, "&amp;");
70+
}
71+
72+
escape('<'); // &amp;lt; instead of &lt;
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

Comments
 (0)