【问题标题】:unwanted <p><span><div> tags are being inserted into database不需要的 <p><span><div> 标签被插入到数据库中
【发布时间】:2012-09-30 21:52:34
【问题描述】:

我正在使用表单将文本插入 MySQL 数据库。

当用户在表单中手动键入文本时,结果完美地插入到数据库中。

但是,如果用户从另一个网页复制和粘贴文本,则会有隐藏的 p 标签与文本一起发送到数据库。标签在表单本身中是不可见的,但在提交时它们仍会发送到数据库。

如果我随后使用 MySQL SELECT 语句在网页上显示结果,则会显示不需要的标签,它们会破坏我的网页布局!

因此,当我从另一个网页复制和粘贴文本时,我只需要知道如何阻止不需要的“p”“span”和“div”标签插入到我的 MySQL 数据库中。

有问题的网络表单是我正在构建的内容管理系统的一部分。从用户的角度来看,我需要表单是防弹的。现实情况是,用户很可能会从其他网站复制和粘贴文本,也可能从 word 文档中复制和粘贴文本,我需要确保在复制时不会将不需要的“p”“span”和“div”标签插入数据库并从第三方来源粘贴。

这是我的表单代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Untitled</title>
<script type="text/javascript" src="http://www.achcreative.net/ckeditor/ckeditor.js"></script>
<link href="../elite.css" rel="stylesheet" type="text/css" />
</head>
<body>

<!--Begin Main Menu -->
<?php include("includes/menu.inc.php"); ?>
<!--End Main Menu -->

<h2 class="subheaderh2">Insert New News Entry</h2>  

<form method="post" action="insert_news.php">
<input name="publish" type="hidden" id="publish" value="publish" />  
<table>
<tr><td><p>News Title:</p></td></tr>
<tr><td><input name="newstitle" type="text" size="43" id="newstitle"></td></tr>
<tr><td><p>News Article:</p></td></tr>
<tr><td><textarea name="newsarticle" cols="40" rows="10" id="newsarticle"></textarea>

<script type="text/javascript">
//<![CDATA[

// Replace the <textarea id="editor"> with an CKEditor
// instance, using default configurations.
CKEDITOR.replace( 'newsarticle', 
    {
        toolbar :
        [
            [ 'Bold', 'Italic', '-', 'NumberedList', 'BulletedList', '-', 'Link', 'Unlink' ],
        ]
    });
//]]>
</script>
</td></tr>
<tr><td height="30" colspan="2"><input type="submit" value="Submit"></td></tr>  
</table></form>
<p><a href="news_results.php">Return</a></p>
</body>
</html>

这是我的表单处理脚本代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Untitled</title>
</head>

<body>
<h2 class="subheaderh2">News Entry Results</h2>

<?php
// create short variable names
$newstitle=$_POST['newstitle'];
$newsarticle=$_POST['newsarticle'];
$publish=$_POST['publish'];

if (!$newstitle || !$newsarticle)
{
 echo '<p>You have not entered all the required details.<br />'
      .'Please go back and try again.</p>'
      .'<p><a href="javascript:history.go(-1)">Return</a></p>';
 exit;
}

if (!get_magic_quotes_gpc())
{
$newstitle = addslashes($newstitle);
$newsarticle = addslashes($newsarticle);
}

$time = date("l jS F Y - g:iA");

// connect to the database
include('../connect-db.php');

/* Create the prepared statement */
if ($stmt = $mysqli->prepare("INSERT INTO news (id, newstitle, newsarticle, date, archive) values (NULL, ?, ?, NOW(), ?)")) {

/* Bind our params */
$stmt->bind_param('sss', $newstitle, $newsarticle, $publish);

/* Set our params */
$newstitle=$_POST['newstitle'];
$newsarticle=$_POST['newsarticle'];
$publish=$_POST['publish'];

/* Execute the prepared Statement */
$stmt->execute();

/* Echo results */
echo "{$newstitle}";
echo "<br />{$newsarticle}";
echo "Inserted into database on: ";
echo "$time";
echo "<br />";
echo "<br />";
echo '<a href="news_results.php">view results</a>';

/* Close the statement */
$stmt->close(); 
}
else {
/* Error */
printf("Prepared Statement Error: %s\n", $mysqli->error);
}

/* close our connection */
$mysqli->close();

?>

</body>
</html>

在此先感谢

问候

安德鲁

【问题讨论】:

  • 打印前不使用htmlspecialchars吗?无论如何,我认为 strip_tags 会很有用。
  • 这与数据库无关。不需要的标签存在于用户提交的数据中。您将这些数据放入 mysql 的事实是无关紧要的。如果您正在写入文件,或者只是显示输出,它们就会出现。

标签: php mysql configuration ckeditor


【解决方案1】:

我想指出您的代码易受 XSS 攻击。

现在回到你的问题:

您可能使用 html 编辑器。尝试在使用 javascript 和 onsubmit 属性提交之前删除不需要的标签。您可以使用以下正则表达式去除标签:

value_of_editor.replace(/<[^>]+>/g,'');

还要确保在向客户端发送 html 之前不要输出原始 html 而是转义 html。

更新: 没有必要将转义消息放入数据库 - 我认为这只是浪费数据长度。而且您应该始终检查您向客户端输出的内容。

【讨论】:

  • 嗨 Zaffy .....你能给我建议或指出正确的方向,以获得避免 XSS 攻击的最佳实践
  • @user1710099: OWASP XSS 是权威指南。
  • @user1710099 很简单。只需过滤所有发送给客户端的数据,并在输出用户输入的数据时更加精确。不要相信自己的数据库!看到这个owasp.org/index.php/Cross-site_Scripting_%28XSS%29
  • 呵呵@Zaffy:勉强击败你到 owasp!
  • @JordanArseno 不。你只是更快。我喜欢知道安全的人:)
【解决方案2】:

CKEditor 提供了大量影响内容最终输出的配置选项。

如果您不希望在将某些内容粘贴到编辑器时包含 HTML 标记,您可以强制粘贴操作仅是文本,这将去除 HTML 标记。

config.forcePasteAsPlainText = true;

如果您可以包含从另一个网页复制并粘贴到编辑器中的有问题内容的示例,将会很有帮助。包括以下三个部分。

1) 被复制的网页部分。

2) 该网页中被复制部分的源代码。

3) 粘贴操作后CKEditor内容的源码。

要在编辑器中查看源代码,您需要暂时将“源”按钮重新添加到工具栏中:

CKEDITOR.replace( 'newsarticle', 
    {
        toolbar :
        [
            [ 'Source','Bold', 'Italic', '-', 'NumberedList', 'BulletedList', '-', 'Link', 'Unlink' ],
        ]
    });

粘贴操作后,点击源按钮,复制粘贴的内容。这将使您能够准确地看到正在发生的事情。

配置选项列表可在此处获得:
CKEditor 3 JavaScript API Documentation Namespace CKEDITOR.config

【讨论】:

  • 感谢大家的专业知识!....您在这里为我提供了大量信息...非常感谢 :0) 我会尽快发布我修改后的脚本。跨度>
【解决方案3】:

您应该使用函数 strip_tags 来从字符串中去除(明显的)标签和可能有害的代码。 (无论是html还是php代码)

$foo = strip_tags('<b>code with html</b>'); // $foo will be "code with html"

【讨论】:

  • 嗨 Ahmet....谢谢您的建议和意见。 strip_tags 似乎是一个很好的解决方案。我会试试这个并报告
【解决方案4】:

您可以对需要插入数据库的文本使用strip_tags() 函数。

Here's the reference

【讨论】:

  • 不是strip_tag,而是strip_tags
猜你喜欢
  • 1970-01-01
  • 2011-04-03
  • 1970-01-01
  • 1970-01-01
  • 2018-11-10
  • 1970-01-01
  • 2013-07-21
  • 1970-01-01
相关资源
最近更新 更多