hypernova-php icon indicating copy to clipboard operation
hypernova-php copied to clipboard

xss injection

Open espretto opened this issue 4 years ago • 2 comments

Hi, The getFallbackHTML method does not escape the json for html here. It should escape the json before sprintf-ing it into html tags just like its js counterpart here.

I'd be happy to contribute if PRs are welcome.

espretto avatar Feb 01 '21 08:02 espretto

Hi, thanks for the feedback. After thinking about this for a sec, you are likely correct, since the json-encoded data is put into an HTML comment block, which could be ended by malicious stuff in the data.

However I don't agree with the js counterpart's approach; brute force replacing all & and > in the input with the corresponding html entities isn't a palatable solution. I could envision scenarios where I have those in my props and I don't want them to turn into htmlentities.

Happy to think through solutions here if you want to provide a test case.

roippi avatar Feb 05 '21 22:02 roippi

The client-side decoder expects server-rendered JSON to be encoded. The client first reverses the conversion and only then parses to JSON. The server should mirror the decoder. Example:

str_replace(['&', '>'], ['&', '>'], json_encode(data));

Unfortunately, there's no flag available to make htmlspecialchars do this. The other special chars are not needed because only > can terminate an html comment (spec). Let's say the above expression is returned by a function hypernova_encode, here is how it compares to json_encode:

$data = ['xss' => 'comment--><script>window.close()</script>'];

sprintf('<script type="application/json"><!--%s--></script>', json_encode($data));
// <script type="application/json"><!--{"xss":"comment--><script>window.close()</script>"}--></script>

sprintf('<script type="application/json"><!--%s--></script>', hypernova_encode($data));
// <script type="application/json"><!--{"xss":"comment--&gt;<script-&gt;window.close()</script-&gt;"}--></script>

espretto avatar Feb 08 '21 21:02 espretto