代码之家  ›  专栏  ›  技术社区  ›  alex

在将服务器变量回显到页面时,我是否覆盖了所有安全基础?

  •  1
  • alex  · 技术社区  · 16 年前

    <form method="post" action="<?php echo strip_tags($_SERVER['REQUEST_URI']); ?>">
    

    striptags()

    http://www.mysite.com/page-with-form.php?bla="><script src="http://www.nasty.com/super-nasty.js"></script><a href="#
    

    我是否涵盖了所有基础,以防止XSS攻击,还是应该使用更白名单的方法,比如只允许字母数字字符、正斜杠、问号、等号、括号等的正则表达式?

    非常感谢。

    5 回复  |  直到 15 年前
        1
  •  6
  •   Gumbo    16 年前

    使用 htmlspecialchars 而不是 strip_tags .

        2
  •  3
  •   Community CDub    4 年前

    https://www.rfc-editor.org/rfc/rfc3986#section-4.2

    relative-ref  = relative-part [ "?" query ] [ "#" fragment ]
    
          relative-part = "//" authority path-abempty
                        / path-absolute
                        / path-noscheme
                        / path-empty
    

        3
  •  2
  •   random    16 年前

    $_SERVER["PHP_SELF"]
    

    正如VolkerK在评论中指出的那样,甚至 PHP_SELF 易受攻击,您可以根据以下内容编写自己的小变量 并分解出您知道不是页面一部分的URI的其余部分。大致如下:

    $file_ext = '.php'; //knowing what file extension your URI is
    $page_on = $_SERVER["PHP_SELF"]; //grab this page, with all that junk
    $page_huh = explode($file_ext, $page_on); //blow it apart based on file ext
    $page_on = $page_huh[0].$file_ext; //attach the leg back onto the URI
    
    echo $page_on;
    
        4
  •  0
  •   maxyfc    16 年前

    如果你 striptags() 只去掉标签(“<”和“>”之间的字符,包括尖括号),有人仍然可以注入javascript:

    http://www.mysite.com/page-with-form.php?bla=" onsubmit="return function(){ /*nasty code here*/ }()" style="
    

    更好地将HTML、Javascript和CSS中所有可能的元字符(即尖括号、圆括号、大括号、分号、双引号、单引号等)列入白名单。

        5
  •  0
  •   Xavura Xavura    16 年前

    如果你想让表单自行提交,只需将操作留空即可,例如。

    <form action="" method="POST">
    ...
    </form>