How to prevent possible problems with requiring user textedit content?

I am in the process of creating a PHP generated webpage that allows a user to make changes to a form’s textedit element. The content will be “required” into a web-page!

Only HTML elements will be allowed.

Possible rejection reasons are the content contains any of the following:

  1. <?
  2. ?>
  3. <script
  4. </script>
  5. " style"

Is there anything else worthy of rejection?


I’m assuming by “textedit element” you mean “textarea element”.

Many modern browsers have default security built in and enabled.
But not all.

If you are planning to output user supplied input you are venturing on very thin ice.

As a simple contrived example, try entering this into to the textarea.

header("X-XSS-Protection: 0");
// header("Content-Security-Policy: 0");

ini_set('display_errors', 'true');

$test_input = "";

  if(!empty($_POST['test_input'])) {
    $test_input = trim($_POST['test_input']);
<html lang="en">
<title>Textarea Sanitization test</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<style type="text/css">
<h1>Textarea Sanitization test</h1>
<form action=""; method="POST">
 <textarea id="test_input" name="test_input" rows="10" cols="50"></textarea>
 <input type="submit" value="Submit" />
 <div><?php echo htmlspecialchars($test_input); ?></div>
 <div><?php echo strip_tags($test_input); ?></div>
 <h2>unsanitized html</h2>
 <div><?php echo $test_input; ?></div>

I’d consider the IFRAME tag because they can be used maliciously, as with SCRIPT. If you need editors to add 3rd party widgets that use IFRAME’s (like vide/map embeds) then use a separate field.

1 Like

Regret the delay it was due to a broken arm and the doctor reckoned I could not use the desktop for three months!

Is the following sufficient to sanatize users input?

  declare(strict_types=1); // PHP 7 specific
  ini_set('error_log', 'ERROR-LOG.php');

  header("X-XSS-Protection: 1");
  header("Content-Security-Policy: 1");

  # function fred($var='NOT SPECIFIED') {echo '<pre>'.var_dump($var) .'</pre>'; }

function getSanitizedInput( & $problem='')
  $problem = '{background-color:#f0f0f0; color:#000;}';

  $result = '';
    if( isset($_POST['test_input']) ):
      $result = ' ' .$_POST['test_input'];
      $result = strtolower($result);
      if( strpos( $result, '<script') 
          strpos( $result, '<?php') 
          strpos( $result, '<style') 
          strpos( $result, '<frame') 
          strpos( $result, '<iframe') 
        $result  = "PROBLEM: \n\t" . strip_tags( $_POST['test_input'] );
        $problem = '.problem {background-color:#ff0; color:#f00; font-weight:700;}';
        $result = trim($_POST['test_input']);

  return $result;
}// func getSanitizedInput();

$test_input = getSanitizedInput( $problem );

<html lang="en">
<title>Textarea Sanitization test</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<style type="text/css">
  body {background-color: #f9f9f9;}
  dl dd {margin-left:0px;}
  <?= $problem; ?>
  .test_input {width:88%; height: 12em;} 
  .lstn {list-style-type:none;}
  .lh2  {line-height: 1.42;}
  .ooo  {margin:0; padding:0;}
  .red  {color:red;}
  .tac  {text-align:center;} .tal {text-align: left;}

<h1>Textarea Sanitization test</h1>
<dl class="ooo">
  <dt> Try these (should produce an error): </dt>
    <ul class="lstn lh2">
      <li><?= htmlentities('<SCRIPT>alert("gotcha")</script>'); ?></li>
      <li><?= htmlentities('<STYLE'); ?></li>
      <li><?= htmlentities('<?'); ?></li>
      <li><?= htmlentities('<FRAME'); ?></li>
      <li><?= htmlentities('<iframe'); ?></li>

<div class="tac" >
  <?php $_POST = []; ?>

    onsubmit="return validateForm()" 
    <fieldset class="tal">
      <label>Please input body script (without &lt;body&gt; or &lt;/body&gt;)</label>
        class="problem  test_input" 
        name="test_input"><?= $test_input; ?></textarea>
      <input type="submit" value="Submit" />

<?php if($test_input || strip_tags($test_input) ): ?>
  <div class="tal">
    <div>  <?= htmlspecialchars($test_input); ?> </div>

    <div> <?= strip_tags($test_input); ?> </div>

    <h2>unsanitized html</h2>
    <div> <?= $test_input; ?> </div>

<?php else: echo '<b class="red">YES WE HAVE NO $test_input</b>'; endif ?>  


##Is there anything else I may have forgotten?

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.