Skip to content

Commit 7484007

Browse files
committed
fix: only allows PUT, PATCH, DELETE when Method Spoofing
1 parent 0c13bbd commit 7484007

2 files changed

Lines changed: 40 additions & 1 deletion

File tree

system/CodeIgniter.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -983,7 +983,10 @@ public function spoofRequestMethod()
983983
return;
984984
}
985985

986-
$this->request = $this->request->setMethod($method);
986+
// Only allows PUT, PATCH, DELETE
987+
if (in_array(strtoupper($method), ['PUT', 'PATCH', 'DELETE'], true)) {
988+
$this->request = $this->request->setMethod($method);
989+
}
987990
}
988991

989992
/**

tests/system/CodeIgniterTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,4 +444,40 @@ public function testRunCLIRoute()
444444

445445
$this->assertStringContainsString('Method Not Allowed', $output);
446446
}
447+
448+
public function testSpoofRequestMethodCanUsePUT()
449+
{
450+
$_SERVER['argv'] = ['index.php'];
451+
$_SERVER['argc'] = 1;
452+
453+
$_SERVER['REQUEST_URI'] = '/';
454+
$_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.1';
455+
$_SERVER['REQUEST_METHOD'] = 'POST';
456+
457+
$_POST['_method'] = 'PUT';
458+
459+
ob_start();
460+
$this->codeigniter->useSafeOutput(true)->run();
461+
ob_get_clean();
462+
463+
$this->assertSame('put', Services::request()->getMethod());
464+
}
465+
466+
public function testSpoofRequestMethodCannotUseGET()
467+
{
468+
$_SERVER['argv'] = ['index.php'];
469+
$_SERVER['argc'] = 1;
470+
471+
$_SERVER['REQUEST_URI'] = '/';
472+
$_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.1';
473+
$_SERVER['REQUEST_METHOD'] = 'POST';
474+
475+
$_POST['_method'] = 'GET';
476+
477+
ob_start();
478+
$this->codeigniter->useSafeOutput(true)->run();
479+
ob_get_clean();
480+
481+
$this->assertSame('post', Services::request()->getMethod());
482+
}
447483
}

0 commit comments

Comments
 (0)