Skip to content

Commit 81ea05b

Browse files
committed
Added support for X-Proxy-Cookie
1 parent 2943452 commit 81ea05b

File tree

2 files changed

+73
-37
lines changed

2 files changed

+73
-37
lines changed

README.md

Lines changed: 70 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ This is such a script.
1111

1212

1313

14+
15+
1416
Installation
1517
---
1618

@@ -42,41 +44,9 @@ And then for example add a `proxy.php` like this to your web application:
4244

4345
```
4446

45-
Security
46-
---
47-
48-
The whitelist array can contain any number of these criterias:
49-
50-
- Exact paths
51-
`['http://example.com/api/specific-method']`
52-
- Array with single regex key
53-
`['regex' => '%^http://example.com/api/%']`
54-
- Array with any [parse_url](http://php.net/manual/en/function.parse-url.php) components to match
55-
`['host' => 'example.com']`
56-
`['host' => 'example.com', 'scheme' => 'https']`
57-
58-
The requested URL must match at least one of the whitelisted criterias to be accepted, otherwise a 403 will be returned. An empty whitelist will accept any requests.
59-
60-
**An example using all types**
61-
62-
``` PHP
63-
<?php
64-
65-
require 'vendor/autoload.php';
66-
67-
CrossOriginProxy::proxy([
68-
// Exact matching
69-
['http://www.yr.no/place/Sweden/Stockholm/Stockholm/forecast.xml'],
7047

71-
// URL component matching
72-
['host' => 'localhost'],
73-
['host' => 'example.com', 'scheme' => 'http'],
7448

75-
// Regex matching
76-
['regex' => '%^http://www.yr.no/place/Norway/%'],
77-
]);
7849

79-
```
8050

8151
Usage
8252
---
@@ -89,7 +59,10 @@ On the client-side, when performing cross-origin requests:
8959
All parameters and HTTP headers (except `Cookie`, `Host` and `X-Proxy-URL`) will be used to recreate the request and performed server-side by the proxy. When complete it will mirror the response, including headers, and return it to the client-side script more or less as if it had been called directly.
9060

9161

92-
Using jQuery
62+
63+
64+
65+
Usage with jQuery
9366
---
9467

9568
**Basic GET request**
@@ -99,7 +72,20 @@ $.ajax({
9972
url: 'proxy.php',
10073
cache: false,
10174
headers: {
102-
'X-Proxy-URL': 'http://api.example.com/some/path',
75+
'X-Proxy-URL': 'http://example.com/api/method',
76+
},
77+
})
78+
```
79+
80+
**Basic GET request with cookie**
81+
82+
``` JAVASCRIPT
83+
$.ajax({
84+
url: 'proxy.php',
85+
cache: false,
86+
headers: {
87+
'X-Proxy-URL': 'http://example.com/api/method',
88+
'X-Proxy-Cookie': 'jsessionid=AS348AF929FK219CKA9FK3B79870H;',
10389
},
10490
})
10591
```
@@ -140,6 +126,54 @@ $.ajax({
140126

141127
When using `cache:false` jQuery adds a `_` GET parameter to the URL with the current timestamp to prevent the browser from returning a cached response. This happens *before* the `ajaxSend` event, so in the above case, if you had set `cache:false`, that `_` parameter would just be "moved" to the `X-Proxy-URL` header and no longer have any effect. So instead, leave `cache` at its default value `true`, and add the parameter manually to the proxy url instead.
142128

143-
**More?**
129+
*Some more examples can be found in [test/index.html](test/index.html).*
130+
131+
132+
133+
Security
134+
---
135+
136+
The hostname of the referer is checked, but can be easily spoofed, so the whitelist array should be put to good use. Fill it with any number of the following types of criterias:
137+
138+
- Exact paths
139+
`['http://example.com/api/specific-method']`
140+
- Array with single regex key
141+
`['regex' => '%^http://example.com/api/%']`
142+
- Array with any [parse_url](http://php.net/manual/en/function.parse-url.php) components to match
143+
`['host' => 'example.com']`
144+
`['host' => 'example.com', 'scheme' => 'https']`
145+
146+
The requested URL must match at least one of the whitelisted criterias to be accepted, otherwise a 403 will be returned. The whitelist can also be set to an empty array to allow any URLs.
147+
148+
**Example**
149+
150+
``` PHP
151+
<?php
152+
153+
require 'vendor/autoload.php';
154+
155+
CrossOriginProxy::proxy([
156+
157+
// URL component matching
158+
['host' => 'localhost'],
159+
['host' => 'example.com', 'scheme' => 'https'],
160+
161+
// Exact matching
162+
['http://www.yr.no/place/Sweden/Stockholm/Stockholm/forecast.xml'],
163+
164+
// Regex matching
165+
['regex' => '%^http://www.yr.no/place/Norway/%'],
166+
167+
]);
168+
169+
```
170+
171+
Cookies
172+
---
173+
174+
Cookies sent to the proxy will be ignored, since the browser will send the ones meant for the domain of the proxy, and not the cookies meant for the proxied resource. Don't want stuff to leak!
175+
176+
If a request requires a certain cookie set, for example a session id, you can set the `X-Proxy-Cookie` header which is then used as `Cookie` header by the proxy.
177+
178+
X-Proxy-Cookie: jsessionid=AS348AF929FK219CKA9FK3B79870H;
144179

145-
Some more examples can be found in [test/index.html](test/index.html).

proxy.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
$headers = getallheaders();
1919
$method = __('REQUEST_METHOD', $_SERVER);
2020
$url = __('X-Proxy-URL', $headers);
21-
21+
$cookie = __('X-Proxy-Cookie', $headers);
2222

2323
// Check that we have a URL
2424
if( ! $url)
@@ -40,6 +40,8 @@
4040
// Remove ignored headers and prepare the rest for resending
4141
$ignore = ['Cookie', 'Host', 'X-Proxy-URL'];
4242
$headers = array_diff_key($headers, array_flip($ignore));
43+
if($cookie)
44+
$headers['Cookie'] = $cookie;
4345
foreach($headers as $key => &$value)
4446
$value = "$key: $value";
4547

0 commit comments

Comments
 (0)