Skip to content

Commit 715173b

Browse files
davidpandersonlfield
authored andcommitted
web: fix various vulnerabilities
- Send all cookies as HttpOnly (don't let Javascript see them) - If page sending cookie is HTTPS, make the cookie secure - Often we have something like: $name = get_str('name'); if (!lookup($name)) { error_page("can't find $name"); } Can't do this; it can be exploited for XSS attacks. Just say 'Can't find file' or whatever - Don't show database error messages
1 parent fa038fa commit 715173b

11 files changed

+120
-19
lines changed

html/inc/consent.inc

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ function consent_to_a_policy(
4848
function check_user_consent($user, $consent_name) {
4949
list($checkct, $ctid) = check_consent_type($consent_name);
5050
if ($checkct) {
51-
$consent_result = BoincLatestConsent::lookup("userid={$user->id} AND consent_type_id=$ctid AND consent_flag=1");
51+
$consent_result = BoincLatestConsent::lookup(
52+
"userid=$user->id AND consent_type_id=$ctid AND consent_flag=1"
53+
);
5254
if ($consent_result) {
5355
return TRUE;
5456
}
@@ -64,7 +66,8 @@ function check_user_consent($user, $consent_name) {
6466
// If the boolean is FALSE, the integer returned is -1.
6567
//
6668
function check_consent_type($name, $checkenabled=TRUE) {
67-
$ct = BoincConsentType::lookup("shortname = '{$name}'");
69+
$name = BoincDb::escape_string($name);
70+
$ct = BoincConsentType::lookup("shortname = '$name'");
6871
if ($ct and ( !$checkenabled or ($ct->enabled)) ) {
6972
return array(TRUE, $ct->id);
7073
}

html/inc/prefs_util.inc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ function check_venue($x) {
3232
if ($x == "home") return;
3333
if ($x == "work") return;
3434
if ($x == "school") return;
35-
error_page(tra("bad venue: %1", $x));
35+
error_page("bad venue");
3636
}
3737

3838
function check_subset($x) {
3939
if ($x == "global") return;
4040
if ($x == "project") return;
41-
error_page(tra("bad subset: %1", $x));
41+
error_page("bad subset");
4242
}
4343

4444
abstract class PREF {
@@ -271,7 +271,7 @@ class PREF_CONSENT extends PREF {
271271
(!$cr) ) {
272272
$rc = consent_to_a_policy($user, $consent_type_id, $flag, 0, 'Webform', time());
273273
if (!$rc) {
274-
error_page(tra("Database error:").BoincDb::error());
274+
error_page("Database error");
275275
}
276276
}
277277
}
@@ -288,7 +288,7 @@ class PREF_CONSENT extends PREF {
288288

289289
$rc = consent_to_a_policy($user, $consent_type_id, $this->default, 0, 'Webform');
290290
if (!$rc) {
291-
error_page(tra("Database error:").BoincDb::error());
291+
error_page("Database error");
292292
}
293293
}
294294

html/inc/user_util.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ function validate_post_make_user() {
206206
$team = BoincTeam::lookup_id($teamid);
207207
$clone_user = BoincUser::lookup_id($team->userid);
208208
if (!$clone_user) {
209-
error_page("User $userid not found");
209+
error_page("User $team->userid not found");
210210
}
211211
$project_prefs = $clone_user->project_prefs;
212212
} else {

html/inc/util.inc

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,11 @@ function send_cookie($name, $value, $permanent, $ops=false) {
157157
$path .= "_ops/";
158158
}
159159
$expire = $permanent?time()+3600*24*365:0;
160-
setcookie($name, $value, $expire, $path);
160+
setcookie($name, $value, $expire, $path,
161+
'',
162+
is_https(), // if this page is secure, make cookie secure
163+
true // httponly; no JS access
164+
);
161165
}
162166

163167
function clear_cookie($name, $ops=false) {
@@ -878,10 +882,7 @@ function strip_bbcode($string){
878882
}
879883

880884
function current_url() {
881-
$url = "http";
882-
if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == "on") {
883-
$url .= "s";
884-
}
885+
$url = is_https()?'https':'http';
885886
$url .= "://";
886887
$url .= $_SERVER['SERVER_NAME'];
887888
$url .= ":".$_SERVER['SERVER_PORT'];
@@ -1098,7 +1099,7 @@ function do_download($path,$name="") {
10981099
function redirect_to_secure_url() {
10991100
if (defined('SECURE_URL_BASE')
11001101
&& strstr(SECURE_URL_BASE, "https://")
1101-
&& empty($_SERVER['HTTPS'])
1102+
&& !is_https()
11021103
) {
11031104
Header("Location: https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
11041105
exit;

html/inc/util_basic.inc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,4 +185,12 @@ function dtime() {
185185
return microtime(true);
186186
}
187187

188+
// is $x a valid file (or dir) name?
189+
//
190+
function is_valid_filename($x) {
191+
if (htmlspecialchars($x) != $x) return false;
192+
if (strstr($x, '/')) return false;
193+
return true;
194+
}
195+
188196
?>

html/user/am_set_host_info.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
// You should have received a copy of the GNU Lesser General Public License
1717
// along with BOINC. If not, see <http://www.gnu.org/licenses/>.
1818

19+
// Handler for RPC to change the venue of a host
20+
1921
require_once("../inc/boinc_db.inc");
2022
require_once("../inc/xml.inc");
2123

html/user/manage_app.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,6 @@ function batches_action($app) {
226226
case "batches_action":
227227
batches_action($app); break;
228228
default:
229-
error_page("unknown action $action");
229+
error_page("unknown action");
230230
}
231231
?>

html/user/manage_project.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ function handle_add_action() {
208208
case 'edit_action':
209209
handle_edit_action(); break;
210210
default:
211-
error_page("unknown action: $action");
211+
error_page("unknown action");
212212
}
213213

214214
?>

html/user/sandbox.php

Lines changed: 89 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,32 @@ function list_files($user, $err_msg) {
5353
</form>
5454
<hr>
5555
";
56-
if (strcmp($err_msg,"")!=0){
57-
echo "<p>$err_msg<hr>";
56+
57+
form_start('sandbox.php', 'post');
58+
form_input_hidden('action', 'add_file');
59+
form_input_text('Name', 'name');
60+
form_input_textarea('Contents', 'contents');
61+
form_submit('OK');
62+
form_end();
63+
echo "
64+
<hr>
65+
<h3>Get web file</h3>
66+
";
67+
form_start('sandbox.php', 'post');
68+
form_input_hidden('action', 'get_file');
69+
form_input_text('URL', 'url');
70+
form_submit('OK');
71+
form_end();
72+
page_tail();
73+
}
74+
75+
function list_files($user) {
76+
$dir = sandbox_dir($user);
77+
if (!is_dir($dir)) error_page("Can't open sandbox directory");
78+
page_head("File sandbox");
79+
$notice = htmlspecialchars(get_str('notice', true));
80+
if ($notice) {
81+
echo "<p>$notice<hr>";
5882
}
5983
$files = array();
6084
while (($f = readdir($d)) !== false) {
@@ -138,8 +162,53 @@ function upload_file($user) {
138162
list_files($user, $notice);
139163
}
140164

165+
<<<<<<< HEAD
166+
=======
167+
function add_file($user) {
168+
$dir = sandbox_dir($user);
169+
$name = post_str('name');
170+
if (!is_valid_filename($name)) {
171+
error_page('bad filename');
172+
}
173+
if (!$name) error_page('No name given');
174+
if (file_exists("$dir/$name")) {
175+
error_page("file $name exists");
176+
}
177+
$contents = post_str('contents');
178+
$contents = str_replace("\r\n", "\n", $contents);
179+
file_put_contents("$dir/$name", $contents);
180+
181+
[$md5, $size] = get_file_info("$dir/$name");
182+
write_info_file("$dir/.md5/$name", $md5, $size);
183+
184+
$notice = "Added file <strong>$name</strong> ($size bytes)";
185+
header(sprintf('Location: sandbox.php?notice=%s', urlencode($notice)));
186+
}
187+
188+
function get_file($user) {
189+
$dir = sandbox_dir($user);
190+
$url = post_str('url');
191+
if (filter_var($url, FILTER_VALIDATE_URL) === FALSE) {
192+
error_page('Not a valid URL');
193+
}
194+
$fname = basename($url);
195+
$path = "$dir/$fname";
196+
if (file_exists($path)) {
197+
error_page("File $fname exists; delete it first.");
198+
}
199+
copy($url, $path);
200+
$notice = "Fetched file from <strong>$url</strong><br/>";
201+
header(sprintf('Location: sandbox.php?notice=%s', urlencode($notice)));
202+
}
203+
204+
// delete a sandbox file.
205+
//
206+
>>>>>>> c2defb6df6 (web: fix various vulnerabilities)
141207
function delete_file($user) {
142208
$name = get_str('name');
209+
if (!is_valid_filename($name)) {
210+
error_page('bad filename');
211+
}
143212
$dir = sandbox_dir($user);
144213
list($error, $size, $md5) = sandbox_parse_link_file("$dir/$name");
145214
if ($error) {
@@ -163,6 +232,9 @@ function delete_file($user) {
163232
}
164233
function download_file($user) {
165234
$name = get_str('name');
235+
if (!is_valid_filename($name)) {
236+
error_page('bad filename');
237+
}
166238
$dir = sandbox_dir($user);
167239
list($err, $size, $md5) = sandbox_parse_link_file("$dir/$name");
168240
if ($err) {
@@ -176,11 +248,21 @@ function download_file($user) {
176248
}
177249
function view_file($user) {
178250
$name = get_str('name');
251+
if (!is_valid_filename($name)) {
252+
error_page('bad filename');
253+
}
179254
$dir = sandbox_dir($user);
255+
<<<<<<< HEAD
180256
list($error, $size, $md5) = sandbox_parse_link_file("$dir/$name");
181257
if ($error) error_page("no such link file");
182258
$p = sandbox_physical_path($user, $md5);
183259
if (!is_file($p)) error_page("no such physical file");
260+
=======
261+
$path = "$dir/$name";
262+
if (!is_file($path)) {
263+
error_page("no such file");
264+
}
265+
>>>>>>> c2defb6df6 (web: fix various vulnerabilities)
184266
echo "<pre>\n";
185267
readfile($p);
186268
echo "</pre>\n";
@@ -198,7 +280,12 @@ function view_file($user) {
198280
case 'delete_file': delete_file($user); break;
199281
case 'download_file': download_file($user); break;
200282
case 'view_file': view_file($user); break;
283+
<<<<<<< HEAD
201284
default: error_page("no such action: $action");
285+
=======
286+
case 'add_form': add_form($user); break;
287+
default: error_page("no such action: ".htmlspecialchars($action));
288+
>>>>>>> c2defb6df6 (web: fix various vulnerabilities)
202289
}
203290

204291
?>

html/user/team_forum.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ function show_forum($team) {
205205
require_founder_login($user, $team);
206206
remove($team);
207207
} else if ($cmd != "") {
208-
error_page("unknown command $cmd");
208+
error_page("unknown command ".htmlspecialchars($cmd));
209209
} else {
210210
show_forum($team);
211211
}

0 commit comments

Comments
 (0)