Ik ben bezig aan een klein projectje, waarbij gebruikers een URL kunnen invullen in een form veld. Deze wordt met een AJAX POST request naar de server gestuurd. Daar wordt de URL geïnspecteerd en geladen met cURL, zoals hieronder getoond.
Nou vraag ik me af of ik de zaak zo voldoende heb dichtgetimmerd. Ik haal de url eerst door een filter (die controleert op sql injectie etc) en accepteer alleen URL's die:
Verder hoor ik graag andere ervaringen over het gebruik van cURL met door de gebruiker ingegeven URL's
PHP:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
| <?php //Load generic user input filter require_once("class.inputfilter_clean.php"); $filter = new InputFilter(); //Clean input URL, and check if it's an http address function filter_url($url) { global $filter; $url = $filter->process($url); $url_parsed = parse_url($url); if(isset($url_parsed)) { $scheme = $url_parsed['scheme']; if(isset($scheme) && $scheme=="http") { return $url; } } die("Must use http protocol"); } //Load URL with cURL, following redirects if necessary function curl_redirect_exec($ch, &$redirects, $curlopt_header = false) { //Include headers in response, and load response as a string variable curl_setopt($ch, CURLOPT_HEADER, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //Load the webpage $data = curl_exec($ch); //Die if result is anything other than text/html $info = curl_getinfo($ch, CURLINFO_CONTENT_TYPE); if(substr($info,0,9) != "text/html") die("Could not read URL"); //Handle specific http return codes $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); if(isset($http_code) { //handle redirects if ($http_code == 301 || $http_code == 302) { //find redirect url list($header) = explode("\r\n\r\n", $data, 2); $matches = array(); preg_match('/(Location:|URI:)(.*?)\n/', $header, $matches); $url = filter_url(trim(array_pop($matches))); //execute this function on redirect url curl_setopt($ch, CURLOPT_URL, $url); $redirects++; return curl_redirect_exec($ch, $redirects); } //handle html if ($http_code == 200) { if ($curlopt_header) return $data; else { list(,$body) = explode("\r\n\r\n", $data, 2); return $body; } } } //Failure (received 404 or no http code at all) die("Invalid URL"); } //Clean URL $url = filter_url($_POST['url']); //Start cURL, with cleaned URL $ch = curl_init($url); //Load URL, redirect if needed, and get resulting HTML $html = curl_redirect_exec($ch); //Stop cURL curl_close($ch); //Parse HTML, store in database, etc //... ?> |
Nou vraag ik me af of ik de zaak zo voldoende heb dichtgetimmerd. Ik haal de url eerst door een filter (die controleert op sql injectie etc) en accepteer alleen URL's die:
- Kunnen worden geparsed door PHP
- Beginnen met 'http://'
- (na laden) verwijzen naar een text/html document
- (na laden) http code 301, 302 of 200 geven
Verder hoor ik graag andere ervaringen over het gebruik van cURL met door de gebruiker ingegeven URL's