Commit a2262eaf authored by Ad Schellevis's avatar Ad Schellevis

fix mvc sample (csrf protection was broken after a forward)

parent f1bbc919
...@@ -68,35 +68,35 @@ class ControllerBase extends Controller ...@@ -68,35 +68,35 @@ class ControllerBase extends Controller
*/ */
public function beforeExecuteRoute($dispatcher) public function beforeExecuteRoute($dispatcher)
{ {
// Authentication // only handle input validation on first request.
// - use authentication of legacy OPNsense. if (!$dispatcher->wasForwarded()) {
if ($this->session->has("Username") == false) { // Authentication
$this->response->redirect("/", true); // - use authentication of legacy OPNsense.
} if ($this->session->has("Username") == false) {
// check for valid csrf on post requests $this->response->redirect("/", true);
if ($this->request->isPost() && !$this->security->checkToken()) { }
// post without csrf, exit. // check for valid csrf on post requests
return false; if ($this->request->isPost() && !$this->security->checkToken()) {
} // post without csrf, exit.
return false;
}
// REST type calls should be implemented by inheriting ApiControllerBase. // REST type calls should be implemented by inheriting ApiControllerBase.
// because we don't check for csrf on these methods, we want to make sure these aren't used. // because we don't check for csrf on these methods, we want to make sure these aren't used.
if ($this->request->isHead() || if ($this->request->isHead() ||
$this->request->isPut() || $this->request->isPut() ||
$this->request->isDelete() || $this->request->isDelete() ||
$this->request->isPatch() || $this->request->isPatch() ||
$this->request->isOptions()) { $this->request->isOptions()) {
throw new \Exception('request type not supported'); throw new \Exception('request type not supported');
}
} }
// include csrf for GET requests. // include csrf for volt view rendering.
if ($this->request->isGet()) { $this->view->setVars([
// inject csrf information 'csrf_tokenKey' => $this->security->getTokenKey(),
$this->view->setVars([ 'csrf_token' => $this->security->getToken()
'csrf_tokenKey' => $this->security->getTokenKey(), ]);
'csrf_token' => $this->security->getToken()
]);
}
// Execute before every found action // Execute before every found action
$this->view->setVar('lang', $this->getTranslator()); $this->view->setVar('lang', $this->getTranslator());
......
...@@ -41,8 +41,8 @@ class IndexController extends ApiControllerBase ...@@ -41,8 +41,8 @@ class IndexController extends ApiControllerBase
*/ */
public function indexAction() public function indexAction()
{ {
if ($this->request->hasPut("message")) { if ($this->request->hasPost("message")) {
$message = $this->request->getPut("message"); $message = $this->request->getPost("message");
} else { } else {
$message = " " ; $message = " " ;
} }
......
...@@ -19,14 +19,13 @@ API call result : <div id="msgid"></div> ...@@ -19,14 +19,13 @@ API call result : <div id="msgid"></div>
$( "#restcall" ).click( function() { $( "#restcall" ).click( function() {
$.ajax({ $.ajax({
type: "PUT", type: "POST",
url: "/api/sample/", url: "/api/sample/",
success: function(data){ success: function(data){
$("#msgid").html( data.message ); $("#msgid").html( data.message );
}, },
data:{message:$("#msg").val()} data:{message:$("#msg").val()}
}); });
}); });
</script> </script>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment