source: github/bin/modcss.php @ db52218

HEADcourier-fixdev-browser-capabilitiespdorelease-0.6release-0.7release-0.8
Last change on this file since db52218 was db52218, checked in by thomascube <thomas@…>, 4 years ago

Improve security of modcss.php by setting timeouts and more sanity checks

  • Property mode set to 100644
File size: 3.1 KB
Line 
1<?php
2
3/*
4 +-----------------------------------------------------------------------+
5 | bin/modcss.php                                                        |
6 |                                                                       |
7 | This file is part of the RoundCube Webmail client                     |
8 | Copyright (C) 2007-2009, RoundCube Dev. - Switzerland                 |
9 | Licensed under the GNU GPL                                            |
10 |                                                                       |
11 | PURPOSE:                                                              |
12 |   Modify CSS source from a URL                                        |
13 |                                                                       |
14 +-----------------------------------------------------------------------+
15 | Author: Thomas Bruederli <roundcube@gmail.com>                        |
16 +-----------------------------------------------------------------------+
17
18 $Id$
19
20*/
21
22define('INSTALL_PATH', realpath(dirname(__FILE__) . '/..') . '/');
23require INSTALL_PATH . 'program/include/iniset.php';
24
25$RCMAIL = rcmail::get_instance();
26
27$source = '';
28$error  = 'Requires a valid user session and source url';
29
30if (empty($RCMAIL->user->ID)) {
31    header('HTTP/1.1 403 Forbidden');
32    echo $error;
33    exit;
34}
35
36$url = preg_replace('![^a-z0-9:./\-_?$&=%]!i', '', $_GET['u']);
37if ($url === null) {
38    header('HTTP/1.1 403 Forbidden');
39    echo $error;
40    exit;
41}
42
43$a_uri = parse_url($url);
44$port  = $a_uri['port'] ? $a_uri['port'] : 80;
45$host  = $a_uri['host'];
46$path  = $a_uri['path'] . ($a_uri['query'] ? '?'.$a_uri['query'] : '');
47
48// don't allow any other connections than http(s)
49if (strtolower(substr($a_uri['scheme'], 0, 4)) != 'http') {
50    header('HTTP/1.1 403 Forbidden');
51    echo "Invalid URL";
52    exit;
53}
54
55// try to open socket connection
56if (!($fp = fsockopen($host, $port, $errno, $error, 15))) {
57    header('HTTP/1.1 500 Internal Server Error');
58    echo $error;
59    exit;
60}
61
62// set timeout for socket
63stream_set_timeout($fp, 30);
64
65// send request
66$out  = "GET $path HTTP/1.0\r\n";
67$out .= "Host: $host\r\n";
68$out .= "Connection: Close\r\n\r\n";
69fwrite($fp, $out);
70
71// read response
72$header = true;
73$headers = array();
74while (!feof($fp)) {
75    $line = trim(fgets($fp, 4048));
76
77    if ($header) {
78        if (preg_match('/^HTTP\/1\..\s+(\d+)/', $line, $regs)
79            && intval($regs[1]) != 200) {
80            break;
81        }
82        else if (empty($line)) {
83            $header = false;
84        }
85        else {
86            list($key, $value) = explode(': ', $line);
87            $headers[strtolower($key)] = $value;
88        }
89    }
90    else {
91        $source .= "$line\n";
92    }
93}
94fclose($fp);
95
96// check content-type header and mod styles
97$mimetype = strtolower($headers['content-type']);
98if (!empty($source) && in_array($mimetype, array('text/css','text/plain'))) {
99    header('Content-Type: text/css');
100    echo rcmail_mod_css_styles($source, preg_replace('/[^a-z0-9]/i', '', $_GET['c']));
101    exit;
102}
103else
104    $error = "Invalid response returned by server";
105
106header('HTTP/1.0 404 Not Found');
107echo $error;
108exit;
Note: See TracBrowser for help on using the repository browser.