luci-base: protect CBI forms with CSRF tokens
authorJo-Philipp Wich <jow@openwrt.org>
Tue, 6 Oct 2015 16:54:35 +0000 (18:54 +0200)
committerJo-Philipp Wich <jow@openwrt.org>
Tue, 6 Oct 2015 16:54:35 +0000 (18:54 +0200)
Signed-off-by: Jo-Philipp Wich <jow@openwrt.org>
modules/luci-base/luasrc/dispatcher.lua
modules/luci-base/luasrc/view/cbi/header.htm

index 798e3e6ce64d1aac4edeb6edbbe347756c02d39b..a402d023b352d38157756e5f2d6fa6a87816a8ee 100644 (file)
@@ -743,6 +743,15 @@ local function _cbi(self, ...)
        local cbi = require "luci.cbi"
        local tpl = require "luci.template"
        local http = require "luci.http"
+       local disp = require "luci.dispatcher"
+
+       if http.formvalue("cbi.submit") == "1" and
+          http.formvalue("token") ~= disp.context.urltoken.stok
+       then
+               http.status(403, "Forbidden")
+               luci.template.render("csrftoken")
+               return
+       end
 
        local config = self.config or {}
        local maps = cbi.load(self.model, ...)
index 2bddaba61a3e41683f0a23b3027b1e7e527277fc..302df1d2fda4a24f56de05ca352f8b1863055769 100644 (file)
@@ -2,6 +2,7 @@
 <form method="post" name="cbi" action="<%=REQUEST_URI%>" enctype="multipart/form-data" onreset="return cbi_validate_reset(this)" onsubmit="return cbi_validate_form(this, '<%:Some fields are invalid, cannot save values!%>')">
        <div>
                <script type="text/javascript" src="<%=resource%>/cbi.js"></script>
+               <input type="hidden" name="token" value="<%=token%>" />
                <input type="hidden" name="cbi.submit" value="1" />
                <input type="submit" value="<%:Save%>" class="hidden" />
        </div>