Commit f11f271
committed
Fix exponential DOM issue in unwrap
This fixes an issue in the current implementation where if `sanitize` is
called on an HTML with deep nested unallowed elements it creates an
exponential number of new elements and can eventually exhaust the
available memory.
For example, if you have this HTML:
<x><y><z>blabla</z></y></x>
The code first hits `<x>` and inserts its `.innerHTML` after it:
<x><y><z>blabla</z></y></x>
<y><z>blabla</z></y>
Then it hits the inner `<y>`, and same thing:
<x><y><z>blabla</z></y>
<z>blabla</z></x>
<y><z>blabla</z></y>
Then the inner `<z>`:
<x><y><z>blabla</z>
blabla</y>
<z>blabla</z></x>
<y><z>blabla</z></y>
Then the first `<z>`’s copy, etc.
At the end, there are 7 nodes in the document (2^3-1).
If you raise the depth from 3 to 20, you get 2^20-1=1M nodes.
Note that there is also a performance issue as `.innerHTML` serializes
the whole DOM and then `insertAdjacentHTML` unserializes it.
This commit fixes both the exponential issue and the
serialization/unserialization.1 parent 86cd196 commit f11f271
1 file changed
Lines changed: 14 additions & 12 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
254 | 254 | | |
255 | 255 | | |
256 | 256 | | |
257 | | - | |
| 257 | + | |
258 | 258 | | |
259 | 259 | | |
260 | 260 | | |
261 | 261 | | |
262 | | - | |
263 | | - | |
264 | | - | |
265 | | - | |
266 | | - | |
267 | | - | |
268 | | - | |
269 | | - | |
270 | | - | |
271 | | - | |
272 | | - | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
273 | 273 | | |
| 274 | + | |
| 275 | + | |
274 | 276 | | |
275 | 277 | | |
276 | 278 | | |
| |||
0 commit comments