Powerful and easy to use cms to build websites, blogs or ecommerce stores.
HTML backendblogblog-engineblog-platformbloggingcmscontent-managementcontent-management-systemecommerceecommerce-platformno-codepage-builderphpphp-cmsphp7php8self-hostedshopping-cartweb-applicationwebsite-builder
Added csrf protection to all forms and changed all state changing actions to post, vulnerability reported by @KhanMarshaI @0xHamy
givanzad94c4a3f314
diff --git a/admin/controller/content/posts.php b/admin/controller/content/posts.phpindex 4315425..22cb81b 100755--- a/admin/controller/content/posts.php+++ b/admin/controller/content/posts.php@@ -23,7 +23,7 @@namespace Vvveb\Controller\Content;use function Vvveb\__;-use Vvveb\Controller\Base;+use Vvveb\Controller\Listing;use function Vvveb\humanReadable;use Vvveb\Sql\PostSQL;use Vvveb\System\Cache;@@ -31,7 +31,7 @@ use Vvveb\System\Images;use Vvveb\System\User\Admin;use function Vvveb\url;-class Posts extends Base {+class Posts extends Listing {protected $type = 'post';//check for other modules permission like post and editor to enable links like save/delete etc@@ -46,7 +46,7 @@ class Posts extends Base {}function duplicate() {- $post_id = $this->request->post['post_id'] ?? $this->request->get['post_id'] ?? false;+ $post_id = $this->request->post['post_id'] ?? false;if ($post_id) {$this->posts = new PostSQL();@@ -88,9 +88,9 @@ class Posts extends Base {$success = ucfirst($this->type) . __(' duplicated!');$success .= sprintf(' <a href="%s">%s</a>', $url, __('Edit') . " {$this->type}");- $this->view->success[] = $success;- $this->session->set('success', $success);- $this->redirect(['module' => 'content/posts'], [], false);+ $this->view->success['get'] = $success;+ //$this->session->set('success', $success);+ //$this->redirect(['module' => 'content/posts', 'type' => $this->type], [], false);} else {$this->view->errors[] = sprintf(__('Error duplicating %s!'), $this->type);}@@ -101,7 +101,7 @@ class Posts extends Base {}function delete() {- $post_id = $this->request->post['post_id'] ?? $this->request->get['post_id'] ?? false;+ $post_id = $this->request->post['post_id'] ?? false;if ($post_id) {if (is_numeric($post_id)) {@@ -243,8 +243,8 @@ class Posts extends Base {$post['url'] = url($url);$post['edit-url'] = $post['url'];$post['admin-url'] = url(['module' => 'content/posts']) . '&filter[admin_id_text]=' . $post['username'] . ' &filter[admin_id]=' . $post['admin_id'];- $post['delete-url'] = url(['module' => 'content/posts', 'action' => 'delete'] + $url + ['post_id[]' => $post['post_id']]);- $post['duplicate-url'] = url(['module' => 'content/posts', 'action' => 'duplicate'] + $url + ['post_id' => $post['post_id']]);+ $post['delete-url'] = url(['module' => 'content/posts', 'action' => 'delete'] + $url);// + ['post_id[]' => $post['post_id']]);+ $post['duplicate-url'] = url(['module' => 'content/posts', 'action' => 'duplicate'] + $url);// + ['post_id' => $post['post_id']]);$post['view-url'] = url("content/{$this->type}/index", $post + $url + ['host' => $this->global['host']]);$relativeUrl = url("content/{$this->type}/index", $post + $url);$post['design-url'] = url(['module' => 'editor/editor', 'name' => urlencode($post['name'] ?? ''), 'url' => $relativeUrl, 'template' => $template, 'host' => $this->global['host']], false);diff --git a/admin/controller/crud.php b/admin/controller/crud.phpindex ed8ab78..b71b8ab 100755--- a/admin/controller/crud.php+++ b/admin/controller/crud.php@@ -23,6 +23,7 @@namespace Vvveb\Controller;use function Vvveb\__;+use function Vvveb\humanReadable;use Vvveb\System\Traits\Crud as CrudTrait;class Crud extends Base {@@ -35,6 +36,10 @@ class Crud extends Base {$this->view->{$this->type} = $this->data;+ if ($this->data_id && (! $this->data || (! isset($this->data[$this->type . '_id'])))) {+ return $this->notFound(sprintf(__('%s not found!'), humanReadable(__($this->type))));+ }+$admin_path = \Vvveb\adminPath();$controllerPath = $admin_path . 'index.php?module=media/media';$this->view->scanUrl = "$controllerPath&action=scan";diff --git a/admin/controller/product/products.php b/admin/controller/product/products.phpindex 64814bb..efa8933 100755--- a/admin/controller/product/products.php+++ b/admin/controller/product/products.php@@ -23,7 +23,7 @@namespace Vvveb\Controller\Product;use function Vvveb\__;-use Vvveb\Controller\Base;+use Vvveb\Controller\Listing;use function Vvveb\humanReadable;use Vvveb\Sql\ProductSQL;use Vvveb\System\Core\View;@@ -31,7 +31,7 @@ use Vvveb\System\Images;use Vvveb\System\User\Admin;use function Vvveb\url;-class Products extends Base {+class Products extends Listing {protected $type = 'product';//check for other modules permission like post and editor to enable links like save/delete etc@@ -46,7 +46,7 @@ class Products extends Base {}function duplicate() {- $product_id = $this->request->product['product_id'] ?? $this->request->get['product_id'] ?? false;+ $product_id = $this->request->post['product_id'] ?? false;if ($product_id) {$this->products = new ProductSQL();@@ -110,7 +110,7 @@ class Products extends Base {}function delete() {- $product_id = $this->request->post['product_id'] ?? $this->request->get['product_id'] ?? false;+ $product_id = $this->request->post['product_id'];if ($product_id) {if (is_numeric($product_id)) {@@ -184,8 +184,8 @@ class Products extends Base {$template = (isset($product['template']) && $product['template']) ? $product['template'] : $defaultTemplate;$product['url'] = url($url);$product['edit-url'] = url(['module' => 'product/product', 'product_id' => $product['product_id'], 'type' => $product['type']]);- $product['delete-url'] = url(['module' => 'product/products', 'action' => 'delete'] + $url + ['product_id[]' => $product['product_id']]);- $product['duplicate-url'] = url(['module' => 'product/products', 'action' => 'duplicate'] + $url + ['product_id' => $product['product_id']]);+ $product['delete-url'] = url(['module' => 'product/products', 'action' => 'delete'] + $url);// + ['product_id[]' => $product['product_id']]);+ $product['duplicate-url'] = url(['module' => 'product/products', 'action' => 'duplicate'] + $url);// + ['product_id' => $product['product_id']]);$product['view-url'] = url('product/product/index', $product + $url + ['host' => $this->global['host']]);$relativeUrl = url('product/product/index', $product + $url);$product['design-url'] = url(['module' => 'editor/editor', 'name' => urlencode($product['name'] ?? ''), 'url' => $relativeUrl, 'template' => $template, 'host' => $this->global['host'] . $adminPath], false);diff --git a/admin/controller/theme/themes.php b/admin/controller/theme/themes.phpindex d5f5531..0505997 100755--- a/admin/controller/theme/themes.php+++ b/admin/controller/theme/themes.php@@ -29,14 +29,15 @@ use function Vvveb\rcopy;use function Vvveb\rrmdir;use function Vvveb\sanitizeFileName;use function Vvveb\slugify;+use Vvveb\System\CacheManager;use Vvveb\System\Extensions\Themes as ThemesList;use Vvveb\System\Import\Theme;use Vvveb\System\Sites;class Themes extends Base {function duplicate() {- $theme = sanitizeFileName(basename($this->request->get['theme'] ?? ''));- $dest = sanitizeFileName(basename($this->request->get['dest'] ?? ''));+ $theme = sanitizeFileName(basename($this->request->post['theme'] ?? ''));+ $dest = sanitizeFileName(basename($this->request->post['dest'] ?? ''));$newSlug = slugify($dest);$srcDir = DIR_THEMES . $theme;@@ -76,7 +77,7 @@ class Themes extends Base {}function delete() {- $theme = sanitizeFileName(basename($this->request->get['theme'] ?? ''));+ $theme = sanitizeFileName(basename($this->request->post['theme'] ?? ''));if ($theme && is_dir(DIR_THEMES . $theme)) {if (rrmdir(DIR_THEMES . $theme)) {@@ -177,16 +178,20 @@ class Themes extends Base {}function activate() {- $theme = $this->request->get['theme'];+ $theme = $this->request->post['theme'] ?? false;- if (Sites::setTheme($this->global['site_id'], $theme, 'index.html')) {+ if ($theme && Sites::setTheme($this->global['site_id'], $theme, 'index.html')) {$themeName = \Vvveb\humanReadable($theme);$this->themeActivateUrl = \Vvveb\url(['module' => 'theme/themes', 'action'=> 'import', 'theme' => $theme]);$successMessage = sprintf(__('Theme <b>%s</b> was activated!'), $themeName, $this->themeActivateUrl);- $successMessage .= '<a class="btn btn-success btn-sm ms-4" href="' . $this->themeActivateUrl . '">' . __('Import theme content') . '</a>';+ //$successMessage .= '<a class="btn btn-success btn-sm ms-4" href="' . $this->themeActivateUrl . '">' . __('Import theme content') . '</a>';$successMessage .= '<a class="btn btn-outline-primary btn-sm ms-2" target="_blank" href="/">' . __('View website') . '</a>';$this->view->success[] = $successMessage;+ CacheManager::clearObjectCache('site');+ CacheManager::clearFrontend();+ CacheManager::clearCompiledFiles();+ CacheManager::clearPageCache();} else {$error = __('Error activating theme, check config/sites.php write permissions');$this->view->errors[] = $error;diff --git a/admin/controller/user/login.php b/admin/controller/user/login.phpindex 8056c20..120665e 100755--- a/admin/controller/user/login.php+++ b/admin/controller/user/login.php@@ -61,6 +61,10 @@ class Login {return $this->redirect($adminPath);}+ if (! $this->session->get('csrf')) {+ $this->session->set('csrf', Str::random());+ }+//don't load plugins at all if safemode get parameterif (! isset($this->request->get['safemode'])) {$failedPlugins = [];@@ -121,62 +125,70 @@ class Login {$method = $this->request->getMethod();- if (($method == 'post') &&- ($view->errors = $validator->validate($this->request->post)) === true) {- $user = $this->request->post['user'];+ if ($method == 'post') {+ if ( !($csrf = $this->session->get('csrf')) || !($postCsrf = ($this->request->post['csrf'] ?? false)) || ($csrf != $postCsrf) ) {+ $view->errors['get'] = 'Invalid csrf!';+ return;+ //$this->notFound('Invalid csrf!', 403);+ }- $safemode = $this->request->post['safemode'] ?? false;- $flags = [];+ if (($view->errors = $validator->validate($this->request->post)) === true) {+ $user = $this->request->post['user'];- if (strpos($user, '@')) {- $loginData['email'] = $user;- } else {- $loginData['username'] = $user;- }+ $safemode = $this->request->post['safemode'] ?? false;+ $flags = [];+ $loginData = [];- $failedLogin = new Admin_Failed_LoginSQL();- $date = date($this->failedTimeInterval);- $lastIp = $_SERVER['REMOTE_ADDR'] ?? '';- $failedAttemps = $failedLogin->get(['updated_at' => $date, 'status' => 1] + $loginData);+ if (strpos($user, '@')) {+ $loginData['email'] = $user;+ } else {+ $loginData['username'] = $user;+ }- if ($failedAttemps && ($failedAttemps['count'] > $this->failedCount)) {- $view->errors = [__('Too many login attempts, try again in one hour!')];- $failedLogin->logFailed(['last_ip' => $lastIp, 'updated_at' => $date] + $loginData);+ $failedLogin = new Admin_Failed_LoginSQL();+ $date = date($this->failedTimeInterval);+ $lastIp = $_SERVER['REMOTE_ADDR'] ?? '';+ $failedAttemps = $failedLogin->get(['updated_at' => $date, 'status' => 1] + $loginData);- return;- }+ if ($failedAttemps && ($failedAttemps['count'] > $this->failedCount)) {+ $view->errors = [__('Too many login attempts, try again in one hour!')];+ $failedLogin->logFailed(['last_ip' => $lastIp, 'updated_at' => $date] + $loginData);- $loginData['password'] = $this->request->post['password'];+ return;+ }- if ($safemode) {- $flags['safemode'] = true;- }+ $loginData['password'] = $this->request->post['password'];++ if ($safemode) {+ $flags['safemode'] = true;+ }- list($loginData) = Event :: trigger(__CLASS__, __FUNCTION__ , $loginData);+ list($loginData) = Event :: trigger(__CLASS__, __FUNCTION__ , $loginData);- if ($loginData) {- if ($userInfo = Admin::login($loginData, $flags)) {- $view->success[] = __('Login successful!');+ if ($loginData) {+ if ($userInfo = Admin::login($loginData, $flags)) {+ $view->success[] = __('Login successful!');- if (isset($this->request->post['redir']) && $this->request->post['redir'] && $_SERVER['REQUEST_URI'] != $this->request->post['redir']) {- $url = parse_url($this->request->post['redir']);- $this->redirect($url['path'] . '?' . ($url['query'] ?? '') . '#' . ($url['fragment'] ?? ''));- //$this->redirect($this->request->post['redirect']);+ if (isset($this->request->post['redir']) && $this->request->post['redir'] && $_SERVER['REQUEST_URI'] != $this->request->post['redir']) {+ $url = parse_url($this->request->post['redir']);+ $this->redirect($url['path'] . '?' . ($url['query'] ?? '') . '#' . ($url['fragment'] ?? ''));+ //$this->redirect($this->request->post['redirect']);+ } else {+ $this->redirect($adminPath);+ }} else {- $this->redirect($adminPath);+ //user not found or wrong password+ $view->errors = [__('Authentication failed, wrong email or password!')];+ $view->safemode = $safemode;+ $this->session->set('csrf', Str::random());+ //increment failed attempts+ $failedLogin->logFailed(['last_ip' => $lastIp, 'updated_at' => $date] + $loginData);}- } else {- //user not found or wrong password- $view->errors = [__('Authentication failed, wrong email or password!')];- $view->safemode = $safemode;- $this->session->set('csrf', Str::random());- //increment failed attempts- $failedLogin->logFailed(['last_ip' => $lastIp, 'updated_at' => $date] + $loginData);}+ } else {+ //return $this->redirect($adminPath);+ $this->session->set('csrf', Str::random());}- } else {- //return $this->redirect($adminPath);- $this->session->set('csrf', Str::random());}}}diff --git a/admin/template/user/login.tpl b/admin/template/user/login.tplindex 013ba55..d3bfa7f 100755--- a/admin/template/user/login.tpl+++ b/admin/template/user/login.tpl@@ -23,3 +23,6 @@ html|after = <?php } ?>#safemode|addNewAttribute = <?php if (isset($this->safemode) && $this->safemode) echo 'checked';?>import(common.tpl)++//csrf+input[data-v-csrf]|value = <?php echo \Vvveb\session('csrf');?>diff --git a/public/js/vvvebjs/builder.js b/public/js/vvvebjs/builder.jsindex d0b8c65..0351cf0 100755--- a/public/js/vvvebjs/builder.js+++ b/public/js/vvvebjs/builder.js@@ -1851,7 +1851,7 @@ Vvveb.Builder = {// uncomment to use outerHTML, not recommended//let text = selectedEl.outerHTML;- fetch(namespaceUrl + "/translate&action=get", {method: "POST", body: new URLSearchParams({text})})+ fetch(namespaceUrl + "/translate&action=get", {method: "POST", body: new URLSearchParams({text, csrf: document.getElementById('csrf')?.value})}).then((response) => {if (!response.ok) { throw new Error(response) }return response.json()@@ -1873,6 +1873,7 @@ Vvveb.Builder = {let onSave = function(event) {let data = Object.fromEntries(new FormData(document.getElementById("translateForm")));+ data["csrf"] = document.getElementById('csrf')?.value;fetch(namespaceUrl + "/translate&action=save", {method: "POST", body: new URLSearchParams(data)}).then((response) => {@@ -2269,7 +2270,7 @@ Vvveb.Builder = {Vvveb.Builder.loadBlockGroups();}- let data = {type, name, html:element.outerHTML};+ let data = {type, name, html:element.outerHTML, csrf: document.getElementById('csrf')?.value};fetch(saveReusableUrl, {method: "POST", body: new URLSearchParams(data)}).then((response) => {@@ -2298,6 +2299,7 @@ Vvveb.Builder = {}data["elements"] = Vvveb.ChangeManager.getChangedElements();+ data["csrf"] = document.getElementById('csrf')?.value;if (!data["startTemplateUrl"]) {data["html"] = this.getHtml();@@ -3830,7 +3832,7 @@ Vvveb.FileManager = {if (page) {- fetch(deleteFileUrl, {method: "POST", body: new URLSearchParams({file:page.file,post_id})})+ fetch(deleteFileUrl, {method: "POST", body: new URLSearchParams({file:page.file, post_id, csrf: document.getElementById('csrf')?.value})}).then((response) => {if (!response.ok) { return Promise.reject(response); }return response.json()@@ -3886,7 +3888,7 @@ Vvveb.FileManager = {if (page) {- fetch(renameFileUrl, {method: "POST", body: new URLSearchParams({file:page.file, newfile:newfile, name, duplicate, post_id, product_id})})+ fetch(renameFileUrl, {method: "POST", body: new URLSearchParams({file:page.file, newfile:newfile, name, duplicate, post_id, product_id, csrf: document.getElementById('csrf')?.value})}).then((response) => {if (!response.ok) { return Promise.reject(response); }return response.json()@@ -4189,7 +4191,7 @@ Vvveb.Revisions = {headers: {'Content-Type': 'application/x-www-form-urlencoded',},- body: new URLSearchParams({file})+ body: new URLSearchParams({file, csrf: document.getElementById('csrf')?.value})}).then((response) => {if (!response.ok) { throw new Error(response) }@@ -4229,7 +4231,7 @@ Vvveb.Revisions = {headers: {'Content-Type': 'application/x-www-form-urlencoded',},- body: new URLSearchParams({file})+ body: new URLSearchParams({file, csrf: document.getElementById('csrf')?.value})}).then((response) => {if (!response.ok) { throw new Error(response) }diff --git a/system/traits/crud.php b/system/traits/crud.phpindex f54833b..c08fda0 100644--- a/system/traits/crud.php+++ b/system/traits/crud.php@@ -50,10 +50,18 @@ trait Crud {//protected $fullPost;function delete() {+ if (APP == 'admin') {+ $this->checkCsrf();+ }+$type = $this->type;$type_id = $this->type_id ?? "{$type}_id";- $data_id = $this->request->post[$type_id] ?? $this->request->get[$type_id] ?? false;+ if (APP == 'admin') {+ $data_id = $this->request->post[$type_id];+ } else {+ $data_id = $this->request->post[$type_id] ?? $this->request->get[$type_id] ?? false;+ }if ($data_id) {if (is_numeric($data_id)) {@@ -133,12 +141,15 @@ trait Crud {if ($result && isset($result[$type])) {$successMessage = humanReadable(__($type)) . __(' saved!');- $this->view->success[] = $successMessage;-- $this->session->set('success', $successMessage);+ $this->view->success['get'] = $successMessage;+ $this->session->set('success', ['get' => $successMessage]);if (! $this->data_id && $this->redirect) {- $this->redirect(['module' => "$module/$controller", $type_id => $result[$type]]);+ if ($this->redirect === true) {+ $this->redirect(['module' => "$module/$controller", $type_id => $result[$type], 'success' => $successMessage]);+ } else {+ $this->redirect($this->redirect . "?$type_id={$result[$type]}&success=$successMessage");+ }}} else {$this->view->errors[] = __('Error saving!');diff --git a/system/traits/listing.php b/system/traits/listing.phpindex 3fa8f14..d0edbe0 100644--- a/system/traits/listing.php+++ b/system/traits/listing.php@@ -42,6 +42,8 @@ trait Listing {protected $modelName;+ protected $userFilter = false;+protected $options = [];function init() {@@ -56,7 +58,11 @@ trait Listing {$type = $this->type;$type_id = $this->type_id ?? "{$type}_id";- $data_id = $this->request->post[$type_id] ?? $this->request->get[$type_id] ?? false;+ if (APP == 'admin') {+ $data_id = $this->request->post[$type_id];+ } else {+ $data_id = $this->request->post[$type_id] ?? $this->request->get[$type_id] ?? false;+ }if ($data_id) {if (is_numeric($data_id)) {@@ -83,6 +89,8 @@ trait Listing {}}+ unset($this->request->get[$type_id]);+return $this->index();}@@ -111,13 +119,16 @@ trait Listing {'limit' => $limit,//'type' => $this->type,] + $this->global + $this->filter + $this->request->get;- unset($options['user_id']);++ if (! $this->userFilter) {+ unset($options['user_id']);+ }if ($this->data_id && ($id = $this->request->get[$this->data_id] ?? false)) {$options[$this->data_id] = $id;}- $results = $model->getAll($options);+ $results = $model->getAll($this->options + $options);if (isset($results[$type])) {foreach ($results[$type] as $id => &$row) {