【发布时间】:2014-05-10 05:58:27
【问题描述】:
感谢您阅读本文。 我担心 Web 环境中的安全防御,并且在输入验证方面遇到了很多困难。
这里的上下文是一个网络环境,用户输入用户名和密码,通过 POST 表单传递到文件 verify.php
我有以下代码,我想在让用户输入进入应用程序之前验证用户输入。但我的问题是:是否可以从这个用户的输入中注入代码,实际上会重写应用程序本身?
skip to "REAL QUESTION HERE" if you don't have much time.
/* I have left some questions along the code in commentary,
if you feel you can answer these it would be greatly appreciated too. */
所有这些安全概念都在融化我的大脑。
<?php
//Only sources provided by the server are allowed?
/*is it relevant to use this because I read that
you have to use webkits for cross-platform performences
and that they are not supported in many cases. */
Content-Security-Policy:
default-src 'self';
script-src 'self';
style-src 'self';
connect-src 'self';
media-src 'self';
object-src 'self';
frame-src 'self'
//prevent javascript from accessing cookies?
//I will only use $_SESSION variables.
ini_set('session.cookie_httponly', 1 );
ini_set('session.cookie_secure', 1 );
session_start();
session_regenerate_id(); /*is it mandatory if I plan to use a generated
random value using strong entropy to make
a login token?*/
include('functions.php');//weak point?
//for further validation use
$usernameSafe = '';
$passwordSafe = '';
//check if the values exists.
if (isset($_POST['username']) && isset($_POST['password'])) {
//validation of user input before letting it access the app.
//weak point?
if (isValidInput($_POST['username']) && isValidInput($_POST['password'])) {
$formUn = $_POST['username'];//weak point?
$formPw = $_POST['password'];//here too?
}
}
//@@@@@@@@@@@@@ REAL QUESTION HERE @@@@@@@@@@@@@@
/*BYPASS_IDEA the input could be :
validUserName']) || 1 == 1 ) { inject php code here } }/*
//1 == 1 is used to bypass the condition.
/*two bracket to close the two 'if' statements involved
and end the app' then also escape evrything after it
with a block commentary char left unclosed */
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//from the php file 'functions.php'
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
function isValidInput($string) {
$isValid = FALSE;
if ($string && is_string($string)) {
/*can it protect against xss attack using characters
like < or any coding tags? */
if (mb_detect_encoding($string, 'UTF-8', TRUE) != FALSE &&
ctype_alnum($string)) {
//Is this how buffer attacks are prevented?
if (strlen($string) <= INPUT_LENGHT)
$isValid = TRUE;
}
}
return $isValid;
}
?>
最终,如果可能的话,这可以在 isset() 事件中实现吗?
【问题讨论】:
-
对于 1 个问题可能有点过分,但仍然是一个不错的问题
标签: php security validation code-injection