转义 urldecode 转义混淆

发布于 2024-10-20 15:16:27 字数 3043 浏览 0 评论 0原文

我有一个 TinyMCE 编辑器 - 其内容需要通过 AJAX 上传。

显然,由于 AJAX 发送参数,它们需要通过 javascripts escape() 函数进行转义,这样就不会破坏 AJAX 参数。这些字段是在 PHP 端进行 mysql_real_escape_string 编辑的,我只需要对 AJAX 参数进行转义。

不幸的是,当我将链接和图像添加到编辑器然后提交时,图像和链接的 URL 如下所示:

http://localhost:8888/%22../img/miscimages/hwo-american.gif/%22

在向用户显示内容的页面(产品视图页面)上,数据库中的字符串是运行urldecode()以消除所有%22和其他转义字符。此页面上没有 javascript,全部由 PHP 生成,因此使用 urldecode() 而不是 unescape()。有什么办法解决这个问题吗?

代码:

AJAX发送

function update(){
    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    }else{// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            var response = xmlhttp.responseText;
            alert(response);
            window.location = 'admin-products.php';
        }
    }
    var prodID=document.getElementById("editchoice").value;
    var edittitle=escape(document.getElementById("editname").value);
    var editcategory=document.getElementById("editcat").value;
    var editcontent = escape(tinyMCE.get('editcontent').getContent());
    var parameters= "prodID="+prodID+"&title="+edittitle+"&content="+editcontent+"&cat="+editcategory;
    xmlhttp.open("POST", "../scr/editProduct.php", true);
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlhttp.send(parameters);
}

PHP数据库插入

$prodID = $_POST['prodID'];
$title = urldecode(mysql_escape_string($_POST['title']));
$content = urldecode(mysql_escape_string($_POST['content']));
$category = $_POST['cat'];

echo $prodID . $title . $content . $category;

mysql_query("UPDATE product SET title='$title', content='$content', category_id='$category' WHERE id='$prodID'") or die("ERROR: ".mysql_error());

PHP显示在页面上

/* Get Product from Alias */
$alias = $_GET['name'];
$product_selectProd = mysql_query("SELECT * FROM product WHERE alias='$alias'") or die("ERROR: ". mysql_error());

/* Sort Query Vars */
while($product_arrayProd = mysql_fetch_array($product_selectProd)){
    $product_category = $product_arrayProd['category_id'];
    $product_title = urldecode($product_arrayProd['title']);
    $product_text = urldecode($product_arrayProd['content']);
    $product_image = $product_arrayProd['main_image'];
    $product_sub_image = $product_arrayProd['sub_image'];

    /* Build the Product List */
    $productDetail .= "<img src='$product_image' width='350' height='240' class='prod_image_left' /><img src='$category_image' width='350' height='240' class='prod_image_right' />";
    $productDetail .= "<p>&nbsp;</p><h1>Fiddes $product_title</h1><hr />";
    $productDetail .= "$product_text";
}

I've got a TinyMCE editor - the contents of which need to be uploaded by AJAX.

Obviously because of the AJAX sending the parameters, they need to be escaped via javascripts escape() function, this is so it doesn't break the AJAX parameters. The fields are mysql_real_escape_string'ed at the PHP side, I only need the escape for the AJAX parameters.

Unfortunately, when I add links and images into the editor and then submit, the URLs to the images and links appear like this:

http://localhost:8888/%22../img/miscimages/hwo-american.gif/%22

On the page where the contents are displayed to the user (the product view page), the string from the database is run through urldecode() to get rid of all the %22 and other escaped characters. There is no javascript on this page, it's all generated with PHP, hence the urldecode() and not unescape(). Is there any way around this?

CODE:

AJAX Send

function update(){
    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    }else{// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            var response = xmlhttp.responseText;
            alert(response);
            window.location = 'admin-products.php';
        }
    }
    var prodID=document.getElementById("editchoice").value;
    var edittitle=escape(document.getElementById("editname").value);
    var editcategory=document.getElementById("editcat").value;
    var editcontent = escape(tinyMCE.get('editcontent').getContent());
    var parameters= "prodID="+prodID+"&title="+edittitle+"&content="+editcontent+"&cat="+editcategory;
    xmlhttp.open("POST", "../scr/editProduct.php", true);
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlhttp.send(parameters);
}

PHP Database Insert

$prodID = $_POST['prodID'];
$title = urldecode(mysql_escape_string($_POST['title']));
$content = urldecode(mysql_escape_string($_POST['content']));
$category = $_POST['cat'];

echo $prodID . $title . $content . $category;

mysql_query("UPDATE product SET title='$title', content='$content', category_id='$category' WHERE id='$prodID'") or die("ERROR: ".mysql_error());

PHP Display on Page

/* Get Product from Alias */
$alias = $_GET['name'];
$product_selectProd = mysql_query("SELECT * FROM product WHERE alias='$alias'") or die("ERROR: ". mysql_error());

/* Sort Query Vars */
while($product_arrayProd = mysql_fetch_array($product_selectProd)){
    $product_category = $product_arrayProd['category_id'];
    $product_title = urldecode($product_arrayProd['title']);
    $product_text = urldecode($product_arrayProd['content']);
    $product_image = $product_arrayProd['main_image'];
    $product_sub_image = $product_arrayProd['sub_image'];

    /* Build the Product List */
    $productDetail .= "<img src='$product_image' width='350' height='240' class='prod_image_left' /><img src='$category_image' width='350' height='240' class='prod_image_right' />";
    $productDetail .= "<p> </p><h1>Fiddes $product_title</h1><hr />";
    $productDetail .= "$product_text";
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

打小就很酷 2024-10-27 15:16:27

来自 MDC: https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/函数#escape_and_unescape_Functions

escape 和 unescape 函数可以
对于非 ASCII 不能正常工作
字符并已被弃用。
在 JavaScript 1.5 及更高版本中,使用
编码 URI、解码 URI、
编码 URIComponent,以及
解码URIComponent。

所以不要使用转义,它已被弃用:)

From MDC: https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Functions#escape_and_unescape_Functions

The escape and unescape functions do
not work properly for non-ASCII
characters and have been deprecated.
In JavaScript 1.5 and later, use
encodeURI, decodeURI,
encodeURIComponent, and
decodeURIComponent.

So don't use escape, it's deprecated :)

诠释孤独 2024-10-27 15:16:27

只是关于数据库插入的注释。

  1. 您不应该使用 urldecode 进行数据库插入。 urlancodes 与数据库无关。对于插入,仅应使用与数据库相关的函数。
    在其他地方执行此操作,而不是在将数据插入数据库时​​执行此操作。 mysql_real_escape_string 应该是插入之前应用于数据的最后一件事。或者你破坏了一切(就像你现在所做的那样)

  2. 一个小问题。使用 mysql_real_escape_string 而不是 mysql_escape_string 稍微好一些。对于一些奇怪的编码,它会覆盖你的背部。

  3. 最可怕的事情。你没有清理数字。因此,您的代码容易受到 sql 注入攻击。你必须要么

    • 以与字符串相同的方式转义它,因为您已经引用了它
    • 或者将其转换为探测器类型,例如使用 intval() 函数
    • 或者摆脱所有转义引用和转换并使用准备好的语句来绑定变量。 (这将需要对代码进行重大更改)

对于别名变量以及您站点中的所有其他动态查询也是如此。你应该尽快修复它。

just a note on your database insert.

  1. you shouldn't use urldecode for the database insert. urlancodes has nothing to do with databases. for the insert only database-related functions should be used.
    Do it somewhere else, not a the time when data being inserted to database. mysql_real_escape_string should be the very last thing applied to the data before insert. Or you broke everything (as you do at the moment)

  2. a minor issue. it's slightly better to use mysql_real_escape_string instead of mysql_escape_string. For some odd encodings it will cover your backs.

  3. Most terrible thing. You're not sanitizing numbers. So, your code is open to sql injection attack. You have to either

    • escape it the same way as strings, as you already quoting it
    • or cast it to prober type, using intval() function for example
    • or get rid of all escaping quoting and casting and use prepared statements to bind variables. (it will require major code changes)

Same for alias variable and, I suppose, all other dynamic queries in your site. You ought to fix it as soon as possible.

街角迷惘 2024-10-27 15:16:27

答案似乎就在 TinyMCE 中,我正在 mysql_real_escape_string 处理该字符串,但 TinyMCE 默认情况下已经为您执行了此操作,因此当 php 取消转义这些字符时,它只是转义它的一个实例,而不是它的所有实例。

It seemed the answer lay in TinyMCE, I was mysql_real_escape_stringing the string but TinyMCE does that already for you by default, so when php was unescaping these characters, it was only escaping one instance of it, instead of all instances of it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文