Allow modules to override CSRF protection.

Useful for Web APIs and all other kinds of things.

API changes:
	- Added public CHTTPSock::GetURI() method
	- Added public CModule::ValidateWebRequestCSRFCheck() method
	- Made CWebSock::GetCSRFCheck() method public so it can be accessed
	  from CModule
	- Added public CWebSock::ValidateCSRFCheck() method

Other changes:
	- Added a Sample Web API module (modules/samplewebapi.cpp) and a
	  simple web form with no CSRF check.

Implements feature request #1180.
This commit is contained in:
Latchezar Tzvetkoff
2016-07-15 18:01:41 +03:00
committed by lol768
parent 222ae86fcc
commit a9a7f17910
8 changed files with 111 additions and 3 deletions
+1
View File
@@ -83,6 +83,7 @@ class CHTTPSock : public CSocket {
const CString& GetPass() const;
const CString& GetParamString() const;
const CString& GetContentType() const;
const CString& GetURI() const;
const CString& GetURIPrefix() const;
bool IsPost() const;
// !Getters
+6
View File
@@ -477,6 +477,12 @@ class CModule {
*/
virtual bool OnWebRequest(CWebSock& WebSock, const CString& sPageName,
CTemplate& Tmpl);
/** If ValidateWebRequestCSRFCheck returned false, a CSRF error will be printed.
* @param WebSock The active request.
* @param sPageName The name of the page that has been requested.
* @return You MUST return true if the CSRF token is valid.
*/
virtual bool ValidateWebRequestCSRFCheck(CWebSock& WebSock, const CString& sPageName);
/** Registers a sub page for the sidebar.
* @param spSubPage The SubPage instance.
*/
+3 -1
View File
@@ -178,6 +178,9 @@ class CWebSock : public CHTTPSock {
static void FinishUserSessions(const CUser& User);
CString GetCSRFCheck();
bool ValidateCSRFCheck(const CString& sURI);
protected:
using CHTTPSock::PrintErrorPage;
@@ -186,7 +189,6 @@ class CWebSock : public CHTTPSock {
VCString GetDirs(CModule* pModule, bool bIsTemplate);
void SetPaths(CModule* pModule, bool bIsTemplate = false);
void SetVars();
CString GetCSRFCheck();
private:
EPageReqResult OnPageRequestInternal(const CString& sURI,
+21
View File
@@ -0,0 +1,21 @@
<? INC Header.tmpl ?>
<form method="post" action="<? VAR URIPrefix TOP ?><? VAR ModPath ?>">
<div class="section">
<h3>Sample Web API</h3>
<div class="sectionbg">
<div class="sectionbody">
<div class="subsection full">
<div class="inputlabel">Text:</div>
<textarea name="text" cols="70" rows="5" class="monospace"></textarea>
<br /><span class="info">Sample text that will be returned plain on submit/API request.</span>
</div>
<div class="subsection submitline">
<input type="submit" name="submit" value="Submit" />
</div>
</div>
</div>
</div>
</form>
<? INC Footer.tmpl ?>
+56
View File
@@ -0,0 +1,56 @@
/*
* Copyright (C) 2004-2016 ZNC, see the NOTICE file for details.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <znc/IRCNetwork.h>
class CSampleWebAPIMod : public CModule {
public:
MODCONSTRUCTOR(CSampleWebAPIMod) {}
~CSampleWebAPIMod() override {}
bool OnWebRequest(CWebSock& WebSock, const CString& sPageName,
CTemplate& Tmpl) override {
if (sPageName != "index") {
// only accept requests to index
return false;
}
if (WebSock.IsPost()) {
// print the text we just recieved
CString text = WebSock.GetRawParam("text", true);
WebSock.PrintHeader(text.length(), "text/plain; charset=UTF-8");
WebSock.Write(text);
WebSock.Close(Csock::CLT_AFTERWRITE);
return false;
}
return true;
}
bool ValidateWebRequestCSRFCheck(CWebSock& WebSock,
const CString& sPageName) override {
return true;
}
};
template <>
void TModInfo<CSampleWebAPIMod>(CModInfo& Info) {
Info.AddType(CModInfo::UserModule);
Info.SetWikiPage("samplewebapi");
}
GLOBALMODULEDEFS(CSampleWebAPIMod, "Sample Web API module.")
+2
View File
@@ -536,6 +536,8 @@ const CString& CHTTPSock::GetContentType() const { return m_sContentType; }
const CString& CHTTPSock::GetParamString() const { return m_sPostData; }
const CString& CHTTPSock::GetURI() const { return m_sURI; }
const CString& CHTTPSock::GetURIPrefix() const { return m_sURIPrefix; }
bool CHTTPSock::HasParam(const CString& sName, bool bPost) const {
+4
View File
@@ -594,6 +594,10 @@ bool CModule::OnWebRequest(CWebSock& WebSock, const CString& sPageName,
CTemplate& Tmpl) {
return false;
}
bool CModule::ValidateWebRequestCSRFCheck(CWebSock& WebSock,
const CString& sPageName) {
return WebSock.ValidateCSRFCheck(WebSock.GetURI());
}
bool CModule::OnEmbeddedWebRequest(CWebSock& WebSock, const CString& sPageName,
CTemplate& Tmpl) {
return false;
+18 -2
View File
@@ -655,8 +655,8 @@ CWebSock::EPageReqResult CWebSock::OnPageRequestInternal(const CString& sURI,
// 1. they obviously know the password,
// 2. it's easier to automate some tasks e.g. user creation, without need to
// care about cookies and csrf
if (IsPost() && !m_bBasicAuth &&
GetParam("_CSRF_Check") != GetCSRFCheck() && sURI != "/login") {
if (IsPost() && !m_bBasicAuth && !sURI.StartsWith("/mods/") &&
!ValidateCSRFCheck(sURI)) {
DEBUG("Expected _CSRF_Check: " << GetCSRFCheck());
DEBUG("Actual _CSRF_Check: " << GetParam("_CSRF_Check"));
PrintErrorPage(
@@ -803,6 +803,18 @@ CWebSock::EPageReqResult CWebSock::OnPageRequestInternal(const CString& sURI,
if (!pModule) return PAGE_NOTFOUND;
// Pass CSRF check to module.
if (IsPost() && !m_bBasicAuth &&
!pModule->ValidateWebRequestCSRFCheck(*this, m_sPage)) {
DEBUG("Expected _CSRF_Check: " << GetCSRFCheck());
DEBUG("Actual _CSRF_Check: " << GetParam("_CSRF_Check"));
PrintErrorPage(
403, "Access denied",
"POST requests need to send "
"a secret token to prevent cross-site request forgery attacks.");
return PAGE_DONE;
}
m_Template["ModPath"] = pModule->GetWebPath();
m_Template["ModFilesPath"] = pModule->GetWebFilesPath();
@@ -969,6 +981,10 @@ CString CWebSock::GetCSRFCheck() {
return pSession->GetId().MD5();
}
bool CWebSock::ValidateCSRFCheck(const CString& sURI) {
return sURI == "/login" || GetParam("_CSRF_Check") == GetCSRFCheck();
}
bool CWebSock::OnLogin(const CString& sUser, const CString& sPass,
bool bBasic) {
DEBUG("=================== CWebSock::OnLogin(), basic auth? "