Skip to content

Commit 41f71c6

Browse files
author
Mark Scherer
committed
Fix autoPostRedirect()
1 parent 05ca38b commit 41f71c6

File tree

2 files changed

+67
-29
lines changed

2 files changed

+67
-29
lines changed

src/Controller/Component/CommonComponent.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,10 @@ public function currentUrl($asString = false) {
155155
*
156156
* @param mixed $whereTo URL
157157
* @param bool $allowSelf if redirect to the same controller/action (url) is allowed
158-
* @param int|null $status
158+
* @param int $status
159159
* @return \Cake\Network\Response
160160
*/
161-
public function autoRedirect($whereTo, $allowSelf = false, $status = null) {
161+
public function autoRedirect($whereTo, $allowSelf = false, $status = 302) {
162162
if ($allowSelf || $this->Controller->referer(null, true) !== '/' . $this->Controller->request->url) {
163163
return $this->Controller->redirect($this->Controller->referer($whereTo, true), $status);
164164
}
@@ -200,7 +200,7 @@ public function autoPostRedirect($whereTo, $conditionalAutoRedirect = true, $sta
200200
$referer = Router::parse($referer);
201201
}
202202

203-
if (!$conditionalAutoRedirect || empty($this->Controller->autoRedirectActions) || is_array($referer) && !empty($referer['action'])) {
203+
if ($conditionalAutoRedirect && !empty($this->Controller->autoRedirectActions) && is_array($referer) && !empty($referer['action'])) {
204204
// Be sure that controller offset exists, otherwise you
205205
// will run into problems, if you use url rewriting.
206206
$refererController = null;
@@ -211,12 +211,13 @@ public function autoPostRedirect($whereTo, $conditionalAutoRedirect = true, $sta
211211
if (!isset($this->Controller->autoRedirectActions)) {
212212
$this->Controller->autoRedirectActions = [];
213213
}
214+
214215
foreach ($this->Controller->autoRedirectActions as $action) {
215216
list($controller, $action) = pluginSplit($action);
216-
if (!empty($controller) && $refererController !== '*' && $refererController != $controller) {
217+
if (!empty($controller) && $refererController !== '*' && $refererController !== $controller) {
217218
continue;
218219
}
219-
if (empty($controller) && $refererController != $this->Controller->request->params['controller']) {
220+
if (empty($controller) && $refererController !== $this->Controller->request->params['controller']) {
220221
continue;
221222
}
222223
if (!in_array($referer['action'], $this->Controller->autoRedirectActions, true)) {
@@ -229,15 +230,15 @@ public function autoPostRedirect($whereTo, $conditionalAutoRedirect = true, $sta
229230
}
230231

231232
/**
232-
* Automatically add missing url parts of the current url including
233+
* Automatically add missing URL parts of the current URL including
233234
* - querystring (especially for 3.x then)
234235
* - passed params
235236
*
236237
* @param mixed|null $url
237238
* @param int|null $status
238239
* @return \Cake\Network\Response
239240
*/
240-
public function completeRedirect($url = null, $status = null) {
241+
public function completeRedirect($url = null, $status = 302) {
241242
if ($url === null) {
242243
$url = $this->Controller->request->params;
243244
unset($url['pass']);

tests/TestCase/Controller/Component/CommonComponentTest.php

Lines changed: 59 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,25 @@
1212
*/
1313
class CommonComponentTest extends TestCase {
1414

15-
//public $fixtures = array('core.sessions', 'plugin.tools.tools_users', 'plugin.tools.roles');
16-
15+
/**
16+
* @return void
17+
*/
1718
public function setUp() {
1819
parent::setUp();
1920

2021
Configure::write('App.namespace', 'TestApp');
22+
Configure::write('App.fullBaseUrl', 'http://localhost');
2123

22-
$this->Controller = new CommonComponentTestController(new Request('/test'));
24+
$this->request = new Request('/my_controller/foo');
25+
$this->request->params['controller'] = 'MyController';
26+
$this->request->params['action'] = 'foo';
27+
$this->Controller = new CommonComponentTestController($this->request);
2328
$this->Controller->startupProcess();
2429
}
2530

31+
/**
32+
* @return void
33+
*/
2634
public function tearDown() {
2735
parent::tearDown();
2836

@@ -31,8 +39,6 @@ public function tearDown() {
3139
}
3240

3341
/**
34-
* CommonComponentTest::testLoadComponent()
35-
*
3642
* @return void
3743
*/
3844
public function testLoadComponent() {
@@ -70,8 +76,6 @@ public function testLoadComponent() {
7076
}
7177

7278
/**
73-
* CommonComponentTest::testGetParams()
74-
*
7579
* @return void
7680
*/
7781
public function testGetParams() {
@@ -83,8 +87,6 @@ public function testGetParams() {
8387
}
8488

8589
/**
86-
* CommonComponentTest::testGetDefaultUrlParams()
87-
*
8890
* @return void
8991
*/
9092
public function testGetDefaultUrlParams() {
@@ -106,8 +108,6 @@ public function testCurrentUrl() {
106108
}
107109

108110
/**
109-
* CommonComponentTest::testIsForeignReferer()
110-
*
111111
* @return void
112112
*/
113113
public function testIsForeignReferer() {
@@ -125,39 +125,69 @@ public function testIsForeignReferer() {
125125
}
126126

127127
/**
128-
* CommonComponentTest::testAutoRedirect()
129-
*
130128
* @return void
131129
*/
132130
public function testPostRedirect() {
133131
$is = $this->Controller->Common->postRedirect(['action' => 'foo']);
134132
$is = $this->Controller->response->header();
135-
$this->assertSame('/foo', $is['Location']);
133+
$this->assertSame('http://localhost/foo', $is['Location']);
136134
$this->assertSame(302, $this->Controller->response->statusCode());
137135
}
138136

139137
/**
140-
* CommonComponentTest::testAutoRedirect()
141-
*
142138
* @return void
143139
*/
144140
public function testAutoRedirect() {
145141
$is = $this->Controller->Common->autoRedirect(['action' => 'foo']);
146142
$is = $this->Controller->response->header();
147-
$this->assertSame('/foo', $is['Location']);
148-
$this->assertSame(200, $this->Controller->response->statusCode());
143+
$this->assertSame('http://localhost/foo', $is['Location']);
144+
$this->assertSame(302, $this->Controller->response->statusCode());
149145
}
150146

151147
/**
152-
* CommonComponentTest::testAutoRedirect()
153-
*
154148
* @return void
155149
*/
156150
public function testAutoRedirectReferer() {
151+
$this->request->env('HTTP_REFERER', 'http://localhost/my_controller/some-referer-action');
152+
157153
$is = $this->Controller->Common->autoRedirect(['action' => 'foo'], true);
158154
$is = $this->Controller->response->header();
159-
$this->assertSame('/foo', $is['Location']);
160-
$this->assertSame(200, $this->Controller->response->statusCode());
155+
$this->assertSame('http://localhost/my_controller/some-referer-action', $is['Location']);
156+
$this->assertSame(302, $this->Controller->response->statusCode());
157+
}
158+
159+
/**
160+
* @return void
161+
*/
162+
public function testAutoPostRedirect() {
163+
$is = $this->Controller->Common->autoPostRedirect(['action' => 'foo'], true);
164+
$is = $this->Controller->response->header();
165+
$this->assertSame('http://localhost/foo', $is['Location']);
166+
$this->assertSame(302, $this->Controller->response->statusCode());
167+
}
168+
169+
/**
170+
* @return void
171+
*/
172+
public function testAutoPostRedirectReferer() {
173+
$this->request->env('HTTP_REFERER', 'http://localhost/my_controller/allowed');
174+
175+
$is = $this->Controller->Common->autoPostRedirect(['controller' => 'MyController', 'action' => 'foo'], true);
176+
$is = $this->Controller->response->header();
177+
$this->assertSame('http://localhost/my_controller/allowed', $is['Location']);
178+
$this->assertSame(302, $this->Controller->response->statusCode());
179+
}
180+
181+
/**
182+
* @return void
183+
*/
184+
public function testAutoPostRedirectRefererNotWhitelisted() {
185+
$this->request->env('HTTP_REFERER', 'http://localhost/my_controller/wrong');
186+
187+
$is = $this->Controller->Common->autoPostRedirect(['controller' => 'MyController', 'action' => 'foo'], true);
188+
$is = $this->Controller->response->header();
189+
$this->assertSame('http://localhost/my_controller/foo', $is['Location']);
190+
$this->assertSame(302, $this->Controller->response->statusCode());
161191
}
162192

163193
}
@@ -167,9 +197,16 @@ public function testAutoRedirectReferer() {
167197
*/
168198
class CommonComponentTestController extends Controller {
169199

200+
public $name = 'MyController';
201+
170202
/**
171203
* @var array
172204
*/
173205
public $components = ['Tools.Common'];
174206

207+
/**
208+
* @var array
209+
*/
210+
public $autoRedirectActions = ['allowed'];
211+
175212
}

0 commit comments

Comments
 (0)