Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

use new onlyAllow() method in baked code, to ensure 405 responses hav…

…e required Allow header included
  • Loading branch information...
commit 27d83eedfe4b3a3e6bb93a3336f0c3d82ea2255a 1 parent 17ba713
ceeram ceeram authored
12 lib/Cake/Console/Templates/default/actions/controller_actions.ctp
View
@@ -47,10 +47,12 @@
/**
* <?php echo $admin ?>add method
*
+ * @throws MethodNotAllowedException
* @return void
*/
public function <?php echo $admin ?>add() {
- if ($this->request->is('post')) {
+ if ($this->request->data) {
+ $this->request->onlyAllow('post');
$this-><?php echo $currentModelName; ?>->create();
if ($this-><?php echo $currentModelName; ?>->save($this->request->data)) {
<?php if ($wannaUseSession): ?>
@@ -86,6 +88,7 @@
/**
* <?php echo $admin ?>edit method
*
+ * @throws MethodNotAllowedException
* @throws NotFoundException
* @param string $id
* @return void
@@ -95,7 +98,8 @@
if (!$this-><?php echo $currentModelName; ?>->exists()) {
throw new NotFoundException(__('Invalid <?php echo strtolower($singularHumanName); ?>'));
}
- if ($this->request->is('post') || $this->request->is('put')) {
+ if ($this->request->data) {
+ $this->request->onlyAllow('post', 'put');
if ($this-><?php echo $currentModelName; ?>->save($this->request->data)) {
<?php if ($wannaUseSession): ?>
$this->Session->setFlash(__('The <?php echo strtolower($singularHumanName); ?> has been saved'));
@@ -137,9 +141,7 @@
* @return void
*/
public function <?php echo $admin; ?>delete($id = null) {
- if (!$this->request->is('post')) {
- throw new MethodNotAllowedException();
- }
+ $this->request->onlyAllow('post', 'delete');
$this-><?php echo $currentModelName; ?>->id = $id;
if (!$this-><?php echo $currentModelName; ?>->exists()) {
throw new NotFoundException(__('Invalid <?php echo strtolower($singularHumanName); ?>'));
6 lib/Cake/Test/Case/Console/Command/Task/ControllerTaskTest.php
View
@@ -353,7 +353,8 @@ public function testBakeActionsUsingSessions() {
$this->assertContains("\$this->set('bakeArticle', \$this->BakeArticle->read(null, \$id)", $result);
$this->assertContains('function add()', $result);
- $this->assertContains("if (\$this->request->is('post'))", $result);
+ $this->assertContains("if (\$this->request->data)", $result);
+ $this->assertContains("\$this->request->onlyAllow('post')", $result);
$this->assertContains('if ($this->BakeArticle->save($this->request->data))', $result);
$this->assertContains("\$this->Session->setFlash(__('The bake article has been saved'));", $result);
@@ -392,7 +393,8 @@ public function testBakeActionsWithNoSessions() {
$this->assertContains("\$this->set('bakeArticle', \$this->BakeArticle->read(null, \$id)", $result);
$this->assertContains('function add()', $result);
- $this->assertContains("if (\$this->request->is('post'))", $result);
+ $this->assertContains("if (\$this->request->data)", $result);
+ $this->assertContains("\$this->request->onlyAllow('post')", $result);
$this->assertContains('if ($this->BakeArticle->save($this->request->data))', $result);
$this->assertContains("\$this->flash(__('The bake article has been saved.'), array('action' => 'index'))", $result);

8 comments on commit 27d83ee

José Lorenzo Rodríguez
Owner

I'm not sure this is a good change. data can be empty but still be a POST request

José Lorenzo Rodríguez
Owner

Moreover, those methods also accept GET so it would be inaccurate to respond to browser that method only accepts POST

Mark S.
Collaborator

yeah, the delete method makes sense, but the rest is debatable.

ceeram
Collaborator

propose to partial remove and keep in delete: cc92717

Mark S.
Collaborator

@ceeram: i guess you could first check on valid post/delete before actually checking for exists() in the db on delete. so the initial order was good for me.

ceeram
Collaborator

I dont agree, if you get methodnotallowed then change method to same uri suddenly you could get 404
if the resource does not exist, your should always get 404, and only get 405 when it exists but wrong method

Mark Story
Owner

@ceeram I think the change in cc92717 is a good compromise that better communicates how delete() methods should be used, and doesn't tell half truths for add() and edit().

ceeram
Collaborator

cherry-picked the commit to 2.3: abe74ad

Please sign in to comment.
Something went wrong with that request. Please try again.