Skip to content

Commit b0c4fd6

Browse files
committedDec 12, 2014
Settings: Sanitize value for multiline terminator tokens
1 parent f114fc7 commit b0c4fd6

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed
 

Diff for: ‎src/settings.cpp

+28-6
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,32 @@ Settings & Settings::operator = (const Settings &other)
6363
}
6464

6565

66+
std::string Settings::sanitizeName(const std::string &name)
67+
{
68+
std::string n(name);
69+
70+
for (const char *s = "\t\n\v\f\r\b =\"{}#"; *s; s++)
71+
n.erase(std::remove(n.begin(), n.end(), *s), n.end());
72+
73+
return n;
74+
}
75+
76+
77+
std::string Settings::sanitizeValue(const std::string &value)
78+
{
79+
std::string v(value);
80+
size_t p = 0;
81+
82+
if (v.substr(0, 3) == "\"\"\"")
83+
v.erase(0, 3);
84+
85+
while ((p = v.find("\n\"\"\"")) != std::string::npos)
86+
v.erase(p, 4);
87+
88+
return v;
89+
}
90+
91+
6692
std::string Settings::getMultiline(std::istream &is, size_t *num_lines)
6793
{
6894
size_t lines = 1;
@@ -683,19 +709,15 @@ void Settings::setEntry(const std::string &name, const void *data,
683709
{
684710
Settings *old_group = NULL;
685711

686-
// Strip any potentially dangerous characters from the name (note the value
687-
// has no such restrictions)
688-
std::string n(name);
689-
for (const char *s = "\t\n\v\f\r\b =\""; *s; s++)
690-
n.erase(std::remove(n.begin(), n.end(), *s), n.end());
712+
std::string n = sanitizeName(name);
691713

692714
{
693715
JMutexAutoLock lock(m_mutex);
694716

695717
SettingsEntry &entry = set_default ? m_defaults[n] : m_settings[n];
696718
old_group = entry.group;
697719

698-
entry.value = set_group ? "" : *(const std::string *)data;
720+
entry.value = set_group ? "" : sanitizeValue(*(const std::string *)data);
699721
entry.group = set_group ? *(Settings **)data : NULL;
700722
entry.is_group = set_group;
701723
}

Diff for: ‎src/settings.h

+2
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ class Settings {
112112
bool updateConfigObject(std::istream &is, std::ostream &os,
113113
const std::string &end, u32 tab_depth=0);
114114

115+
static std::string sanitizeName(const std::string &name);
116+
static std::string sanitizeValue(const std::string &value);
115117
static std::string getMultiline(std::istream &is, size_t *num_lines=NULL);
116118
static void printEntry(std::ostream &os, const std::string &name,
117119
const SettingsEntry &entry, u32 tab_depth=0);

0 commit comments

Comments
 (0)
Please sign in to comment.