没有htmlspecialchars的$ _SERVER [' PHP_self']表单中的请求是hackable

时间:2016-11-19 15:35:45

标签: javascript php html mysql security

如果我在constructor(private cdRef:ChangeDetectorRef) {} animDetails: string; animDone(event:any) { console.log('Ended!'); this.animDetails = 'I am done!'; this.cdRef.detectChanges(); } test.php制作表单,则可能会被黑客攻击。我知道form action ="<?php echo $_SERVER[PHP_self]"?>可以保护它。但如果我使用htmlspecialchars()代替。它没有被黑客攻击。这有什么区别?

如果我编辑url test.php / blah blah,那么它会在源代码中成为表单操作,但在<form action ="test.php">时它不会发生。这不是一回事吗? form action="test.php"test.php

1 个答案:

答案 0 :(得分:1)

<form action ="test.php">分配给HTML时,此代码是静态的,无法在服务器端进行操作。如果您分配全局$_SERVER['PHP_SELF'],则页面中的此条目是动态的,取决于地址栏中找到的用户URL,使他们可以像这样操作此代码:

如果这个世界是一个安全可靠的居住地......

浏览器网址: http://www.test.com/html/form.php

<form action="/html/index.php" method="post">

现在是现实世界......

浏览器网址: http://test.com/form.php/%22%3E%3Cscript%3Ealert('xss')%3C /script%3E%3Chack%22

<form action="/html/index.php/"><script>alert('xss')</script><hack"" method="post">

这可以通过围绕$_SERVER['PHP_SELF']内部htmlspecialchars()内部功能来避免,该功能可以有效地清理脚本,使其如下所示:

<form action="/html/index.php/&quot;&gt;&lt;script&gt;alert('xss')&lt;/script&gt;&lt;hack&quot;" method="post">
相关问题