Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9205037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:51:24+00:00 2026-06-17T23:51:24+00:00

I built a tool that takes arbitrary HTML, collects all the classes and ids

  • 0

I built a tool that takes arbitrary HTML, collects all the classes and ids and outputs them back into the page. I am concerned about security. I had been using HTML Purifier to filter the input, but I need to support HTML5, which HTML Purifier does not.

This is the gist of the tool:

$html=$_POST['html'];
$doc = new DOMDocument();
$doc->loadHTML($html);
$xpath = new DOMXpath($doc);
$elements = $xpath->query("//body");

foreach ($elements as $element) { 
    $nodes = $element->childNodes;     
    $output=write_selectors($nodes);
}

function write_selectors($nodes){
    foreach($nodes as $node){
        $node->getAttribute('id');
        .
        .
        .
        $node->getAttribute('class');
        .
        .
        .
    }
    .
    .
    .   
    return 'string containing all classes and ids in the document' 
}
.
.
.
echo htmlentities($output, ENT_QUOTES); 

My questions are:

It seems like it would be possible for someone to put a string like this into the tool: '<div '); do_bad_stuff( 'ha_ha_ha' and that $doc->loadHTML($html); would end up saying $doc->loadHTML('<div '); do_bad_stuff( 'ha_ha_ha');

It seems like DOMDocument just errors when I try to input funny business like that, but should I be doing something to protect against malicious inputs? If not, why not?

Secondarily, is htmlentities enough to sanitize the output?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T23:51:26+00:00Added an answer on June 17, 2026 at 11:51 pm
    1. No,it will never do that. You have $html in a variable and sent that variable directly into the function.

    2. Yes, personally I would use htmlspecialchars, but htmlentities is fine to protect you from XSS.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got an little canvas tool I've built that cut up images into little
Does anyone know of a free tool, similar to what is built into Visual
I'm creating a page that has some dynamically generated images built from data that
I have built a C# .net web service that takes a complex type as
We've got a little tool that I built where you can edit a jQuery
I have a small deploy tool that I'm upgrading. The tool takes a version
can anyone suggest me a plugin/tool that can perform a code formatting at build
im using the fantastic eric4 ide to code python, it's got a tool built
Are there any scripts out there, or have any of you built a tool,
I am having trouble with a site administration tool I built, It allows me

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.