From ef17eda48489a26518882a113b0dc2df04157da9 Mon Sep 17 00:00:00 2001 From: CryptoManiac Date: Mon, 17 Feb 2014 02:52:24 +0400 Subject: [PATCH] Add -alertnotify=command option --- src/alert.cpp | 14 ++++++++++++++ src/util.cpp | 13 +++++++++++++ src/util.h | 1 + 3 files changed, 28 insertions(+), 0 deletions(-) diff --git a/src/alert.cpp b/src/alert.cpp index a1dc27a..515fbeb 100644 --- a/src/alert.cpp +++ b/src/alert.cpp @@ -236,7 +236,21 @@ bool CAlert::ProcessAlert() mapAlerts.insert(make_pair(GetHash(), *this)); // Notify UI if it applies to me if(AppliesToMe()) + { uiInterface.NotifyAlertChanged(GetHash(), CT_NEW); + std::string strCmd = GetArg("-alertnotify", ""); + if (!strCmd.empty()) + { + // Alert text should be plain ascii coming from a trusted source, but to + // be safe we first strip anything not in safeChars, then add single quotes around + // the whole string before passing it to the shell: + std::string singleQuote("'"); + std::string safeStatus = SanitizeString(strStatusBar); + safeStatus = singleQuote+safeStatus+singleQuote; + boost::replace_all(strCmd, "%s", safeStatus); + boost::thread t(runCommand, strCmd); // thread runs free + } + } } printf("accepted alert %d, AppliesToMe()=%d\n", nID, AppliesToMe()); diff --git a/src/util.cpp b/src/util.cpp index 7f5ec96..dd29324 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -433,6 +433,19 @@ bool ParseMoney(const char* pszIn, int64& nRet) return true; } +// safeChars chosen to allow simple messages/URLs/email addresses, but avoid anything +// even possibly remotely dangerous like & or > +static string safeChars("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890 .,;_/:?@"); +string SanitizeString(const string& str) +{ + string strResult; + for (std::string::size_type i = 0; i < str.size(); i++) + { + if (safeChars.find(str[i]) != std::string::npos) + strResult.push_back(str[i]); + } + return strResult; +} static const signed char phexdigit[256] = { -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, diff --git a/src/util.h b/src/util.h index 4a7592b..8c5f379 100644 --- a/src/util.h +++ b/src/util.h @@ -193,6 +193,7 @@ void ParseString(const std::string& str, char c, std::vector& v); std::string FormatMoney(int64 n, bool fPlus=false); bool ParseMoney(const std::string& str, int64& nRet); bool ParseMoney(const char* pszIn, int64& nRet); +std::string SanitizeString(const std::string& str); std::vector ParseHex(const char* psz); std::vector ParseHex(const std::string& str); bool IsHex(const std::string& str); -- 1.7.1