【问题标题】:how to properly secure form - flat file database如何正确保护表单 - 平面文件数据库
【发布时间】:2012-01-06 07:25:47
【问题描述】:

我的表单将用户输入(输入+文本区域)保存到平面文件数据库中。 我在 Google 上找到了很多关于如何创建平面文件数据库的示例,但没有人正确地涵盖了一些关于如何正确保护表单免受 XSS 和其他恶意攻击的基础知识。

我知道最好的方法是拥有(例如:)一个 SQL 数据库……但事实并非如此。

到目前为止我知道(这可能是错误的!如果是,请纠正我):

  • 最好使用 .php 文件来存储数据(在 <?php ...data... ?> 内)而不是 .txt 文件
  • 如果可能的话,将带有deny from all 的 .htaccess 放到数据库文件夹中
  • 在提交之前通过 php 验证您的输入和文本区域。 (但是具体怎么做???我的意思是……最好的方法是什么?
  • 正确验证您的字段 (php)(究竟如何...有些做法仅适用于 sql 数据库,不适用于 ffdb...
  • 我看起来像 mysql_real_escape_string 但对 ffdb 来说已经足够了

你的想法是什么? 感谢您的帮助

【问题讨论】:

  • 第一个听起来很奇怪。您在哪里找到该建议?
  • 你有什么理由想要一个平面文件数据库吗?如果只是因为你不能拥有 MySQL(或 postgreSQL 或 ...),你是否在使用 SQLite 时大吃一惊?它将数据存储在您可以保存在您想要的任何文件夹中的文件中,并且在 PHP5 中默认启用
  • 谢谢@Carlos ...实际上我听说过很多 SQLite 但从来没有时间去探索它...我只是感兴趣:拥有一小段代码 例如:聊天室或留下一个用于创建小型 FFDB 的评论表单...特别是在无法访问 SQL 数据库的情况下。在这种情况下,“外面”没有多少简单的信息可用......

标签: php javascript security flat-file


【解决方案1】:

不知道你从哪里得到的,但是通过使用

  • .php 文件来存储数据(内部)而不是 .txt 文件

您可以确定它会允许任何人进行任何他们想要的攻击,

  • 从数据库文件夹中删除一个拒绝的 .htaccess

完全没有意义,

所以,看来唯一的问题是

  • 如何正确保护表单免受 XSS 影响

使用htmlspecialchars()解决了

这是我很久以前在遥远的星系中编写的此类脚本的示例...
如果有什么不清楚的地方,请随时询问。

<?php
if ($_SERVER['REQUEST_METHOD']=='POST') { 
  // iterating over POST data
  foreach($_POST as $key => $value) { 
    //first we are doing non-destructive modifications
    //in case we will need to show the data back in the form on error
    $value = trim($value); 
    if (get_magic_quotes_gpc()) $value = stripslashes($value); 
    $value = htmlspecialchars($value,ENT_QUOTES); 
    $_POST[$key] = $value; 
    //here go "destructive" modifications, specific to the storage format
    $value = str_replace("\r","",$value);
    $value = str_replace("\n","<br>",$value);
    $value = str_replace("|","&brvbar;",$value);
    $msg[$key] = $value;
  } 
  //various validations
  $err=''; 
  if (!$msg['name']) $err.="You forgot to introduce yourself<br>"; 
  if (!$msg['notes']) $err.="You forgot to leave a comment!<br>"; 
  //and so on
  //...
  // if no errors - writing to the file
  if (!$err) { 
    $s  = $msg['name']."|".$msg['email']."|".$msg['notes']."|".time()."\n"; 
    $fp = fopen("gbook.txt","a"); 
    fwrite($fp,$s); 
    fclose($fp); 
    //and then redirect
    Header("Location: ".$_SERVER['PHP_SELF']); 
    exit; 
  } 
  //otherwise - show the filled form
} else { 
  //if it was not a POST request
  //we have to fill variables used in form
  $_POST['name'] = $_POST['email'] = $_POST['notes'] =''; 
} 
?> 
<html> 
<head></head> 
<body> 
<? if ($err): ?><font color=red><b><?=$err?></b></font><? endif ?> 
<form method="POST">
Name: <input type="text" name="name" value="<?=$_POST['name']?>"><br> 
Email: <input type="text" name="email" value="<?=$_POST['email']?>"><br> 
Notes: <textarea rows="3" cols="30" name="notes"><?=$_POST['notes']?></textarea><br> 
<input type="submit" name="submit"> 
</form> 
</body> 
</html>

它将产生一种所谓的管道分隔格式,像这样

name1|email1|comment
name2|email2|comment

你可以使用 file()+explode() 读取它

【讨论】:

  • 好吧,如果他使用var_export() 将数据写入这些文件,那将是安全的。
  • 是的,我不得不承认这是真的。尽管以代码的形式存储数据无论如何都是最不明智的解决方案
  • @Col.Shrapnel 你打错了,hmlspecialchars() > htmlspecialchars() :-)
【解决方案2】:

如果您将内容存储在文本文件中,则无需担心转义字符串。

您可以将其过滤为恶意 HTML,但这取决于您对内容的处理方式。

确保文件位于公共目录之外的文件夹中或受deny from all htaccess 技巧保护,并确保同时使用文件锁定以防止其被覆盖。

此外,如果您正在寻找一个好的平面文件数据库,请查看 Flintstone,它是我编写的键/值数据库存储,可能对您有用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多