/
test.php
155 lines (127 loc) · 4.94 KB
/
test.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php ob_start()?>
</ol>
<h2>Заголовок — Title</h2>
<!-- Кодировка → UTF-8 -->
<header data-any="data:title/%$#">
<a href="data:text/html,<img src=x onerror=alert(0)>">
Danger!
</a>
<img alt="Doesn't make sense without "src"">
<a href="#" target="a$#@z"><a href="#" target="_blank">Okay.</a></a>
<p>
Some <i><b>broken</i> nesting</b> & keeping <p>↓
</p>
</header>
<p>Co<u>mm<!</p>-->en</u>t.</p>
--
<meta/>XML</meta>-<script/>way</script>
<p><acronym title="foo!">is deprecated, now <abbr>rules</abbr></acronym>.</p>
<p><acronym>Even if it's aliased – nesting must be <abbr>kept</acronym></abbr>!</p>
<h1 style="background: url('javascript:foo;'); no: protection; ;">for IE 6</h1>
<p><b>What? </img src="/"></b></p>
<p>Proper single <wbr title=5> tag and nesting.</p> <wbr title=5 />
<span title=>Empty</span> <ins title=''>title</ins> <s title="">.</s>
<aside>Here <input data-foo=/> <input data-foo> too.</aside>
<div style="margin: " " " " ' ' ' ';border: up;smth:; font:"
data-onmousemove="alert('Ouch ' + document.cookie + '!')">
Browser decodes entities inside attributes.
<p style="margin: "; igno: re; background: foo">Style discarded.</p>
<p style="margin: '; igno: re; background: foo">Too.</p>
<a style="margin: '; ">Tag discarded.</a>
<!-- multi
</div>
line -->
</div>
<p>< b>ad<b>ad</ b>·<i>ad< /i> <mark>•</mark> <u/style>ad</u>•<s>ad</s data-x></p>
<input data-custom readonly disabled=disabled placeholder=foo>
<ul type=" id='what's =on "=smth going="here foo=">
<li value='>Too <i>broken</i'></i>.</li>
<li
value="
>
<kbd data-foo="b"a'r""" lang='baz"&'''''>duh</kbd>
</li
>
<li value="value='value=""
style="icon: url('; cursor: url(";
background: data:image/png,foo'); font: "foo", bar">
<strong>Unnest <wbr>me</wbr>!</strong>
</li>
</ul>
<article>
<DiV><P>N3STiNG.</p></dIv>
<p>o_O →</p/>
<iMG sRc="https://localhost" LoL> <em></Img>?</em>
<LoL><b stYle="fOnt-SiZE: 18pX; oMG: w00t">ar</b></LoL>
</article>
<!--
<b>Unterminated
comment</b>
<meta>
<?php
$input = ob_get_clean();
$customInput = isset($_REQUEST['input']);
$customInput and $input = trim($_REQUEST['input']);
$expected = $customInput ? $input : trim(file_get_contents(__DIR__.'/tested.html'));
ini_set('display_errors', 'on');
error_reporting(-1);
require_once __DIR__.'/hypersafe.php';
$hs = new HyperSafe;
$hs->lineBreaks = $customInput ? null : "\n";
$hs->keepComments = true;
$ac = &$_REQUEST['autoclose'] and $hs->autoClose = $ac === '1' ? true : $ac;
$output = trim($hs->clean($input));
function esc($str) {
return htmlspecialchars($str, ENT_NOQUOTES, 'utf-8');
}
function unescTags($str) {
return strtr($str, array('<' => '<', '>' => '>'));
}
header('Content-Type: text/html; charset=utf-8');
if ($expected !== $output) {
echo '<h3 style="color: red">Output differs</h3>';
}
?>
<div style="overflow: auto">
<textarea style="width: 49%; float: left; height: 40em" readonly
><?php echo esc($expected)?></textarea>
<textarea style="width: 49%; float: right; height: 40em" readonly
><?php echo esc($output)?></textarea>
</div>
<center>
↑ expected
[ <a href="#" onclick="document.getElementById('_hsinput_').style.display = 'block'; return false">clean yours</a> ]
produced ↑
</center>
<form action="" method="post" style="display: none" id="_hsinput_">
<p>
<textarea style="width: 100%; height: 35em" name="input"
onfocus="this.select(); this.onfocus = null"
><?php echo esc($input)?></textarea>
</p>
<p style="text-align: center">
<label><input type="radio" name="autoclose" value="" checked> don't autoclose tags</label>
<label><input type="radio" name="autoclose" value="eof"> autoclose on EOF</label>
<label><input type="radio" name="autoclose" value="1"> autoclose all tags</label>
</p>
<p style="text-align: center"><input type="submit"></p>
</form>
<h3>Warnings</h3>
<ol>
<?php
foreach ($hs->warnings() as $warning) {
extract($warning);
echo '<li>';
echo '<b>', esc($msg), '</b> ';
if ($opener) { echo "<$opener[tagName] $opener[pos]>"; }
echo '…';
if ($closer) { echo "<$closer[tagName] $closer[pos]>"; }
if ($opener) { echo '<br><kbd>', esc(unescTags($opener['tag'])), '</kbd>'; }
if ($closer) { echo '<br><kbd>', esc(unescTags($closer['tag'])), '</kbd>'; }
echo '</li>';
}
?>
</ol>
<div style="border: 1px solid silver; padding: 1em">
<?php echo $output?>
</div>