Skip to content

Commit a5ca8fa

Browse files
committed
Disable access to unsafe.php for blacklisted hosts.
A better fix that uses an env var is coming in #23. This is a temporary fix to close the hole. Fixes #22
1 parent 811337b commit a5ca8fa

1 file changed

Lines changed: 56 additions & 26 deletions

File tree

src/eval/unsafe.php

Lines changed: 56 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,68 @@
11
<?php
2-
// Turn off errors since eval will throw them on invalid syntax
3-
$inString = @ini_set('log_errors', false);
4-
$token = @ini_set('display_errors', true);
2+
// Turn off errors since eval will throw them on invalid syntax
3+
$inString = @ini_set('log_errors', false);
4+
$token = @ini_set('display_errors', true);
55

6-
// CORS support
7-
header("Access-Control-Allow-Origin: *");
8-
header("Content-type: application/json");
6+
// CORS support
7+
header("Access-Control-Allow-Origin: *");
8+
header("Content-type: application/json");
99

10-
$code = $_POST['code'];
10+
if (isRequestFromBlacklistedHost()) {
11+
echo getJsonOutput(array(
12+
'result' => 'Tried to access unsafe eval from a blacklisted host',
13+
'error' => ''
14+
));
15+
exit();
16+
}
1117

12-
// Remove error prone snippets
13-
$toRemove = array("<?php", "?>", "<?");
18+
$code = $_POST['code'];
1419

15-
$code = str_replace($toRemove, "", $code);
20+
// Remove error prone snippets
21+
$toRemove = array("<?php", "?>", "<?");
1622

17-
// Simple output buffering to capture
18-
// error messages and send them to the user
19-
ob_start();
23+
$code = str_replace($toRemove, "", $code);
2024

21-
eval($code);
22-
$result = ob_get_clean();
23-
$error = error_get_last();
25+
// Simple output buffering to capture
26+
// error messages and send them to the user
27+
ob_start();
2428

25-
echo getJsonOutput(array(
26-
'result' => $result,
27-
'error' => $error
28-
));
29+
eval($code);
30+
$result = ob_get_clean();
31+
$error = error_get_last();
2932

30-
@ini_set('display_errors', $token);
31-
@ini_set('log_errors', $inString);
33+
echo getJsonOutput(array(
34+
'result' => $result,
35+
'error' => $error
36+
));
3237

33-
function getJsonOutput($options) {
34-
$result = $options['result'];
35-
$error = $options['error'];
36-
return json_encode(array("result" => $result, "error" => $error));
38+
@ini_set('display_errors', $token);
39+
@ini_set('log_errors', $inString);
40+
41+
function getJsonOutput($options) {
42+
$result = $options['result'];
43+
$error = $options['error'];
44+
return json_encode(array("result" => $result, "error" => $error));
45+
}
46+
47+
function isRequestFromBlacklistedHost() {
48+
// Prevents unsafe access on hosting providers (#22)
49+
$blacklistedHosts = array(
50+
'cloudcontrolled',
51+
'herokuapp'
52+
);
53+
54+
$isFromBlacklistedHost = false;
55+
56+
if (isset($_SERVER['HTTP_ORIGIN'])) {
57+
$origin = $_SERVER['HTTP_ORIGIN'];
58+
59+
foreach ($blacklistedHosts as $host) {
60+
if (strpos($origin, $host) !== false) {
61+
$isFromBlacklistedHost = true;
3762
}
63+
}
64+
}
65+
66+
return $isFromBlacklistedHost;
67+
}
3868
?>

0 commit comments

Comments
 (0)