Skip to content

Commit

Permalink
Sanitize all cURL parameters, not just the message
Browse files Browse the repository at this point in the history
  • Loading branch information
apfelbox committed Jun 27, 2013
1 parent f3a60e1 commit 42a1dca
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
35 changes: 26 additions & 9 deletions src/HipChat/HipChat.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,6 @@ public function get_rooms() {
public function message_room($room_id, $from, $message, $notify = false,
$color = self::COLOR_YELLOW,
$message_format = self::FORMAT_HTML) {

if ((strlen($message) > 0) && ($message[0] === "@"))
{
// prepend message with space, since the first character is a @ and PHP would
// treat this as file upload
// @link http://www.php.net/manual/en/function.curl-setopt.php CURLOPT_POSTFIELDS
$message = ' ' . $message;
}

$args = array(
'room_id' => $room_id,
'from' => $from,
Expand Down Expand Up @@ -237,6 +228,12 @@ public function get_users() {
* @param $post_data Data to send via POST. Leave null for GET request.
*/
public function curl_request($url, $post_data = null) {

if (is_array($post_data))
{
$post_data = array_map(array($this, "sanitizeCurlParameter"), $post_data);
}

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
Expand Down Expand Up @@ -268,6 +265,26 @@ public function curl_request($url, $post_data = null) {
return $response;
}

/**
* Sanitizes the given value as cURL parameter.
*
* The first value may not be a "@". PHP would treat this as a file upload
*
* @link http://www.php.net/manual/en/function.curl-setopt.php CURLOPT_POSTFIELDS
*
* @param string $value
* @return string
*/
private function sanitizeCurlParameter ($value)
{
if ((strlen($value) > 0) && ($value[0] === "@"))
{
$value = ' ' . $value;
}

return $value;
}

/**
* Make an API request using curl
*
Expand Down
2 changes: 1 addition & 1 deletion tests/HipChatPHPTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function testBadApiMethod() {
public function testMentionAtFirstPosition ()
{
$hc = new HipChat\HipChat('hipchat-php-test-token', $this->target);
$hc->message_room(123, 'sender', '@test test');
$hc->message_room(123, '@sender', '@test test');
}

}

0 comments on commit 42a1dca

Please sign in to comment.