r/PHPhelp • u/0lafe • Jun 25 '25
Sanitizing user submitted HTML to display
Does anyone have any advice on handling user submitted HTML that is intended to be displayed?
I'm working on an application with a minimal wiki section. This includes users submitting small amounts of HTML to be displayed. We allow some basic tags, such as headers, paragraphs, lists, and ideally links. Our input comes from a minimal WYSIWYG editor (tinymce) with some basic client side restriction on input.
I am somewhat new to PHP and have no idea how to handle this. I come from Rails which has a very convenient "sanitize" method for this exact task. Trying to find something similar for PHP all I see is ways to prevent from html from embedding, or stripping certain tags.
Has anyone ran into this problem before, and do you have any recommendations on solutions? Our application is running with very minimal dependencies and no package manager. I'd love to avoid adding anything too large if possible, if only due to the struggle of setting it all up.
6
u/colshrapnel Jun 25 '25
For the love of all good, use markdown in your wiki instead of HTML. It's so much cleaner and easier to use. I am sure tinymce should support it by now. So there wouldn't be any need in HTML validation.
But if you positively need HTML then you heed a thing called HTML purifier (or sanitizer). So you've got to install one, like it or not. And I don't find your imposed limitations fair. You DON'T have "a convenient "sanitize" method in Ruby. Just like there is none in PHP. While compared with Rails, any PHP framework has a component with similar functionality. So you have a choice - either use a framework, just like you did with Ruby, or use a standalone package.
5
u/mindprocessor Jun 25 '25
http://htmlpurifier.org/
2
u/innosu_ Jun 25 '25
strip_tags
1
u/g105b Jun 25 '25
If you loaded the HTML into a DOM Document, you would have full control over the attributes and tags available. It would be slower than strip_tags but you could ensure all attributes are removed, or provide a white list, etc.
2
u/Pechynho Jun 25 '25
Just use this 😇
https://symfony.com/doc/current/html_sanitizer.html
2
u/MateusAzevedo Jun 25 '25
When you say "if only due to the struggle of setting it all up", are you referring to the production server or your local dev environment?
If the former, note that you don't need to setup Composer in your server do be able to deploy your code. Composer can be used locally only, to download packages and setup the autoloader, then you just copy everything to the server.
If the latter, then you'll need to do a bit of work to make libraries work. HTML Purifier, being an older library, will be the easiest to use, just a single
require. symfony/html-sanitizer or a Markdown parser (if you decide to go that route) will require you to either write a bunch ofrequirefor their classes, or write and register your own PSR-4 autoloader.Installing and using Composer is not a hard task at all, so I highly recommend not restricting yourself by a "no manager" requirement.
1
u/phpMartian Jun 25 '25
Setting up composer is super easy.
0
u/AmiAmigo Jun 25 '25
You have written so well…that’s a perfect prompt:
Hope it helps: https://chatgpt.com/share/685b9991-238c-800e-bc7b-de470fc04070
0
u/toramanlis Jun 25 '25
xss protection is the keyword on this. sometimes frameworks have the functionality built-in, other times you can choose a package from github.
as all security practices, you don't code it yourself unless you absolutely have to.