跨站点脚本过滤器阻止了我使用python-cgi脚本将javascript变量保存到mysqldb的尝试
cross-site scripting filter blocking my attempt to save javascript variable to mysql db using python cgi script
我的主要目标是使用python-cgi脚本将javascript变量保存到mysql-db中。这是js变量的内容:
<li style="width: 300px; background-color: white; list-style-image: url(http://champ/images/plus.gif); ">Costco
<input style="float: right; display: none; " type="image" src="/images/next_level.png" alt="Submit button" onclick="newLevel(this)">
<input style="float: right; margin-right: 7px; display: none; " type="image" src="/images/b_add.png" alt="Submit button" onclick="addItem(this)">
<input style="float: right; margin-right: 7px; display: none; " type="image" src="/images/b_edit.png" alt="Submit button" onclick="editItem(this)">
<input style="float: right; margin-right: 7px; display: none; " type="image" src="/images/b_drop.png" alt="Submit button" onclick="delItem(this)">
<ul style="display: none; ">
<li style="width: 300px; background-color: white; cursor: default; list-style-image: none; ">Cheese
<input style="float:right;" type="image" src="/images/next_level.png" alt="Submit button" onclick="newLevel(this)">
<input style="float:right; margin-right: 7px;" type="image" src="/images/b_add.png" alt="Submit button" onclick="addItem(this)">
<input style="float:right; margin-right: 7px" type="image" src="/images/b_edit.png" alt="Submit button" onclick="editItem(this)">
<input style="float:right; margin-right: 7px" type="image" src="/images/b_drop.png" alt="Submit button" onclick="delItem(this)">
</li>
<li style="width: 300px; background-color: white; cursor: default; list-style-image: none; ">Milk
<input style="float:right;" type="image" src="/images/next_level.png" alt="Submit button" onclick="newLevel(this)">
<input style="float:right; margin-right: 7px;" type="image" src="/images/b_add.png" alt="Submit button" onclick="addItem(this)">
<input style="float:right; margin-right: 7px" type="image" src="/images/b_edit.png" alt="Submit button" onclick="editItem(this)">
<input style="float:right; margin-right: 7px" type="image" src="/images/b_drop.png" alt="Submit button" onclick="delItem(this)">
</li>
<li style="width: 300px; cursor: default; list-style-image: none; ">Pretzels
<input style="float:right;" type="image" src="/images/next_level.png" alt="Submit button" onclick="newLevel(this)">
<input style="float:right; margin-right: 7px;" type="image" src="/images/b_add.png" alt="Submit button" onclick="addItem(this)">
<input style="float:right; margin-right: 7px" type="image" src="/images/b_edit.png" alt="Submit button" onclick="editItem(this)">
<input style="float:right; margin-right: 7px" type="image" src="/images/b_drop.png" alt="Submit button" onclick="delItem(this)">
</li>
</ul>
</li>
<li style="width: 300px; background-color: white; list-style-image: url(http://champ/images/plus.gif); ">Walmart
<input style="float: right; display: none; " type="image" src="/images/next_level.png" alt="Submit button" onclick="newLevel(this)">
<input style="float: right; margin-right: 7px; display: none; " type="image" src="/images/b_add.png" alt="Submit button" onclick="addItem(this)">
<input style="float: right; margin-right: 7px; display: none; " type="image" src="/images/b_edit.png" alt="Submit button" onclick="editItem(this)">
<input style="float: right; margin-right: 7px; display: none; " type="image" src="/images/b_drop.png" alt="Submit button" onclick="delItem(this)">
<ul style="display: none; ">
<li style="width: 300px; background-color: white; cursor: default; list-style-image: none; ">Bread
<input style="float:right;" type="image" src="/images/next_level.png" alt="Submit button" onclick="newLevel(this)">
<input style="float:right; margin-right: 7px;" type="image" src="/images/b_add.png" alt="Submit button" onclick="addItem(this)">
<input style="float:right; margin-right: 7px" type="image" src="/images/b_edit.png" alt="Submit button" onclick="editItem(this)">
<input style="float:right; margin-right: 7px" type="image" src="/images/b_drop.png" alt="Submit button" onclick="delItem(this)">
</li><li style="width: 300px; background-color: white; cursor: default; list-style-image: none; ">Bannanas
<input style="float:right;" type="image" src="/images/next_level.png" alt="Submit button" onclick="newLevel(this)">
<input style="float:right; margin-right: 7px;" type="image" src="/images/b_add.png" alt="Submit button" onclick="addItem(this)">
<input style="float:right; margin-right: 7px" type="image" src="/images/b_edit.png" alt="Submit button" onclick="editItem(this)">
<input style="float:right; margin-right: 7px" type="image" src="/images/b_drop.png" alt="Submit button" onclick="delItem(this)">
</li><li style="width: 300px; background-color: white; cursor: default; list-style-image: none; ">Cereal
<input style="float:right;" type="image" src="/images/next_level.png" alt="Submit button" onclick="newLevel(this)">
<input style="float:right; margin-right: 7px;" type="image" src="/images/b_add.png" alt="Submit button" onclick="addItem(this)">
<input style="float:right; margin-right: 7px" type="image" src="/images/b_edit.png" alt="Submit button" onclick="editItem(this)">
<input style="float:right; margin-right: 7px" type="image" src="/images/b_drop.png" alt="Submit button" onclick="delItem(this)">
</li><li style="width: 300px; background-color: white; cursor: default; list-style-image: none; ">Tortillas
<input style="float:right;" type="image" src="/images/next_level.png" alt="Submit button" onclick="newLevel(this)">
<input style="float:right; margin-right: 7px;" type="image" src="/images/b_add.png" alt="Submit button" onclick="addItem(this)">
<input style="float:right; margin-right: 7px" type="image" src="/images/b_edit.png" alt="Submit button" onclick="editItem(this)">
<input style="float:right; margin-right: 7px" type="image" src="/images/b_drop.png" alt="Submit button" onclick="delItem(this)">
正如你所看到的,我正试图将我的购物清单保存到数据库中。正如我所理解的跨站点脚本过滤器问题,当您通过http-post方法发送js,然后通过http响应返回相同的代码时,就会出现问题。也许我误解了这个问题,因为我在这段代码中没有看到任何js,是吗?
因此,我希望将这个js变量的内容写入一个隐藏的输入表单元素,并提交表单:
function saveList()
{
var list = $('ul').html();
document.getElementById("html_passed").value = list;
document.forms["editsForm"].submit();
}
以下是表单定义:
<form name="editsForm" action="/cgi-bin/todo2.py" method="post">
...
<input id="html_passed" type="hidden" name="html_passed">
...
</form>
然后在执行python-cgi脚本(todo2.py)时读取此表单元素的值:
form = cgi.FieldStorage()
if form.keys() != []:
if 'html_passed' in form.keys():
然后通过MySQLdb将这段代码放在MySQLdb中。然而,当我尝试用form = cgi.FieldStorage()
读取表单内容时,我在webkit控制台中收到以下错误:
Refused to execute a JavaScript script. Source code of script found within request.
所以,我有点麻烦。我发现了一些关于跨站点脚本工作方法的信息,但我不知道如何在python中发送非标准HTTP标头,也不知道是否可以这样做。我看到它可以用header("X-XSS-Protection: 0");
在php中完成。但如果有其他方法可以解决这个问题,我宁愿不去惹麻烦。任何想法都将不胜感激。谢谢
我决定加强我的javascript/jquery技能。我发现,确实是onclicks导致了这个问题。因此,作为一种变通方法,我在提交表格之前删除了它们:
function rmOnClicks()
{
console.log('entered rmOnClicks()')
$('li').children().each(function(index)
{
$(this).removeAttr("onclick");
//alert($(this).text());
});
}
然后,当我用从数据库中回忆数据时,我又把它们加了回来
function addOnClicks()
{
console.log('entered rmOnClicks()')
$('li').children().each(function(index)
{
var src = $(this).attr('src');
if (src == "/images/b_drop.png")
{
$(this).removeAttr("onclick");
$(this).attr('onclick','delItem(this)')
}
else if (src == "/images/b_edit.png")
{
$(this).removeAttr("onclick");
$(this).attr('onclick','editItem(this)')
}
else if (src == "/images/b_add.png")
{
$(this).removeAttr("onclick");
$(this).attr('onclick','addItem(this)')
}
else if (src == "/images/next_level.png")
{
$(this).removeAttr("onclick");
$(this).attr('onclick','newLevel(this)')
}
});
var list = $('ul').html();
console.log(list);
}
这有点草率,但我认为它相当健壮。如果你还有其他想法,我还是很想听听。谢谢
相关文章:
- Java脚本将URL转换为已保存的URL时出错
- 如何使用谷歌脚本将html表单数据保存到谷歌电子表格中
- jquery ajax到php脚本不保存数据
- 谷歌电子表格脚本:如何保存“;永远”;
- 跨站点脚本过滤器阻止了我使用python-cgi脚本将javascript变量保存到mysqldb的尝试
- 使用表单输入中的咖啡脚本编辑保存的值
- 使用java脚本通过网页保存Excel文件
- 将 PHP 数据从mysql_fetch_array保存为脚本变量
- 将所有脚本保存在一个文件中是否是一种好的做法
- 将 PHP 变量保存到函数中的脚本变量
- Photoshop Javascript 脚本保存和关闭文档
- 从“选项”弹出窗口中选择一个值,然后将其保存到 Chrome.storage,以便在后台脚本中用作值
- CRM 2011 在线 - 保存或退出错误消息“<函数>匿名(容器,脚本内容,id)”
- 组合两个脚本以在所有浏览器上保存Google图表时出现问题
- 保存PDF“;斑点”;在谷歌脚本中
- 我怎么能"保存“;变量客户端,这样我就可以在其他java脚本事件中使用它
- 如何使用java脚本保存/下载mp3 DataURL对象
- 我如何将此脚本保存到本地存储
- 尝试使用绘图脚本保存画布
- 将数据从图像映射器脚本保存到 mysql 数据库