The following is the full code of a page that receives a POST request from another page (I don't believe it's too relevant what that other page is, but I'll post it if requested).
Assuming that a request to this page can be made with arbitrary POST data, are there any glaring vulnerabilities?
I'm mainly concerned about the strings being passed to the page via the POST data, but other vulnerabilities would be good as well.
This is being run on an nginx server, the user input is validated client-side but not server-side, there are no db actions performed by the php here.
<!DOCTYPE HTML>
<html>
<head>
<title>Your request has been submitted</title>
<?php include("html-head.php") ?>
</head>
<body class="index">
<?php include_once("header.php") ?>
<?php
if (!isset($_POST['username'])){
die();
} else {
$username = trim(str_replace("@","",$_POST['username']));
}
$hotlist = array('harpies');
function get_ip_address() {
if (!empty($_SERVER['HTTP_CLIENT_IP']) && $this->validate_ip($_SERVER['HTTP_CLIENT_IP']))
return $_SERVER['HTTP_CLIENT_IP'];
if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$iplist = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
foreach ($iplist as $ip) {
if ($this->validate_ip($ip))
return $ip;
}
$logstr = time();
foreach ($_POST as $key => $value) {
$log .="\t".$key."\t".$value."\r\n";
}
}
if (!empty($_SERVER['HTTP_X_FORWARDED']) && $this->validate_ip($_SERVER['HTTP_X_FORWARDED']))
return $_SERVER['HTTP_X_FORWARDED'];
if (!empty($_SERVER['HTTP_X_CLUSTER_CLIENT_IP']) && $this->validate_ip($_SERVER['HTTP_X_CLUSTER_CLIENT_IP']))
return $_SERVER['HTTP_X_CLUSTER_CLIENT_IP'];
if (!empty($_SERVER['HTTP_FORWARDED_FOR']) && $this->validate_ip($_SERVER['HTTP_FORWARDED_FOR']))
return $_SERVER['HTTP_FORWARDED_FOR'];
if (!empty($_SERVER['HTTP_FORWARDED']) && $this->validate_ip($_SERVER['HTTP_FORWARDED']))
return $_SERVER['HTTP_FORWARDED'];
return $_SERVER['REMOTE_ADDR'];
}
$currentURL = explode("/",$_SERVER['REQUEST_URI'])[0];
$errmsg = "";
$trustedUsers = array("user1","user2");
$userIDs = array(
"user1"=>"xxxxxxxxxxxxxxxxxxxxxxxx",
"user2"=>"xxxxxxxxxxxxxxxxxxxxxxxx",
"user3"=>"xxxxxxxxxxxxxxxxxxxxxxxx"
);
$userName = $trustedUsers[array_rand($trustedUsers)];
function rand_color() {
return str_pad(dechex(mt_rand(0, 0xFFF)), 3, '0', STR_PAD_LEFT);
}
$rndCode = strtoupper(rand_color());
$logstr = time('Y-m-d H:i:s');
foreach ($_POST as $key => $value) {
$logstr .= "\t".$key."\t".$value."\r\n";
}
$logstr .= "\tipAddr\t".get_ip_address()."\r\n";
$logstr .= "\trndCode\t".$rndCode."\r\n";
$logstr .= "\ttrustedUsers\t".$userName."\r\n";
$logUrl = "http://example.com/log.php";
$logFields = "str=".$logstr;
$cl = curl_init();
curl_setopt($cl, CURLOPT_URL, $logUrl);
curl_setopt($cl, CURLOPT_POST, 1);
curl_setopt($cl, CURLOPT_POSTFIELDS, $logFields);
$result = curl_exec($cl);
curl_close($cl);
// require_once "Mail.php";
$emailFieldstrings = "";
$emailURL = "https://api.mailgun.net/v2/example.com/messages";
$email_Fields = array(
"from" => "no reply <[email protected]>",
"to" => $username." <".$_POST['email'].">",
"bcc" => "[email protected]",
"subject" => "Action needed to complete your request",
"text" => "Email content.",
"html" => "<html><body>Email content </body> </html>"
);
$emailUser = "api:key-d7ca70c3e32e82108928322ea23bbee8";
foreach($email_Fields as $key=>$value) { $emailFieldstrings .= $key.'='.$value.'&'; }
rtrim($emailFieldstrings, '&');
$cu = curl_init();
curl_setopt($cu,CURLOPT_URL, $emailURL);
curl_setopt($cu,CURLOPT_USERPWD, "api:key-d7ca70c3e32e82108928322ea23bbee8");
curl_setopt($cu,CURLOPT_POST, count($email_Fields));
curl_setopt($cu,CURLOPT_POSTFIELDS, $emailFieldstrings);
$result = curl_exec($cu);
curl_close($cu);
$bit = array(
"ign" => $username,
"email" => $_POST['email'],
"ip" => get_ip_address()
);
$matchlist = array();
$hotlist = array(
"purple" => "purple category",
"red" => "red category",
"orange" => "orange category"
);
$data = "115.186.242.130";
foreach($bit as $desc => $data){
foreach($hotlist as $key => &$value){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.trello.com/1/search?token=xxxx&key=xxxxxx&card_fields=name,url,desc&partial=true&query=label:" . $key . "%20" . urlencode($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close;
$output = json_decode($output, true);
if ($output["cards"]){
foreach ($output["cards"] as $card){
$id = $card['id'];
$matchlist[$id]="[" . $card['name'] . "](" . $card['url'] . ")";
}
}
}}
$dup_warn = "";
foreach ($matchlist as $key => &$value){
$dup_warn = "\n- " . $value;
}
if (count($dup_warn) > 0){
$dup_warn = "Some information from this application matched information from the following other applications:" . $dup_warn;
}
if (strlen($errmsg) > 0){
$details = $dup_warn . "Username Provided: ".$username."\nAreas of play: ".$_POST['areas']."\n Email: ".$_POST['email']."\nG+ Profile: ".$_POST['gplus']."\nIP Address: ".get_ip_address().$errmsg;
$url = "https://api.pushover.net/1/messages.json";
$fields = array(
'token' => urlencode("zzzzzzzzzzzzzzzzzzzzzzzz"),
'user' => urlencode("xzzxzxzzzzzzzzzzzzzzzz"),
'title' => urlencode("Something went wrong with your site"),
'message' => urlencode($details),
);
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');
$ch = curl_init();
}
$trelloCard = $dup_warn;
if (in_array($username, $hotlist)){
$trelloCard .= "--- \xA ** Warning: User on hotlist, consult mods before adding.**";
}
if (isset($_POST['areas'])){
$trelloCard .= "\xA 1. **Areas**: ".$_POST['areas']." ";
}
$trelloCard .= "\xA 2. **Email**: ".$_POST['email']." \xA 3. **Google+ Profile**: [link](".$_POST['gplus'] . ") ";
if(strlen(trim($_POST['referrer']))){
$trelloCard .= "\xA 4. **The following user(s) have been listed as references: " . trim($_POST['referrer'])."**";
}else{
$trelloCard .= "\xA 5. **This application did not list any references.**";
}
$trelloCard .= "\xA \xA ------- \xA \xA 6. Handler: ".$userName."\xA 7. _IP Address: ".get_ip_address()."_ \xA 8. Slack: [invite](https://slack.com/api/users.admin.invite?email=" . $_POST['email'] . "&token=xxxxxxxxxxxx&set_active=true&_attempts=1) \xA 9. Browser: ".$_POST['useragent'];
$url = 'https://api.trello.com/1/cards';
$fields = array(
'key' => urlencode('xxxx'),
'token' => urlencode('xxxxxxxx'),
'name' => urlencode($rndCode." - ".$username),
'due' => date('Y.m.d', strtotime('+1 Week')),
'desc' => urlencode($trelloCard),
'pos' => urlencode('top'),
'idList' => urlencode('xxxxxxxxxxxxxxx'),
'idMembers' => urlencode($userIDs[$userName])
// 'idMembers' => urlencode($userIDs['jimsug'])
);
$fields_string='';
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
$fields_string=rtrim($fields_string, '&');
$ch = curl_init();
curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
$result = curl_exec($ch);
curl_close($ch);
?>
<section id="banner">
<div class="inner">
<header>
<h2>Thanks</h2>
</header>
<p>We've received your request </br>
We'll be in touch
</p>
</div>
<header class="special container">
<p>You will receive an email from us with a <strong>verification</strong> code to send to a <strong>trusted user</strong>.</p>
</header>
</section>
<!-- <article id="main">
</article> -->
<?php include_once("footer.php") ?>
</body>
</html>