Skip to content

Commit 681ac8d

Browse files
committed
Getting command arguments is now secure
- CKeyValues.load_from_file() and CKeyValues.save_to_file() now require only the file name
1 parent 9b1c3d6 commit 681ac8d

File tree

2 files changed

+30
-14
lines changed

2 files changed

+30
-14
lines changed

src/core/modules/commands/command_wrap_python.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,18 @@ DECLARE_SP_MODULE(command_c)
9797
//-----------------------------------------------------------------------------
9898
// Exposes the CICommand interface.
9999
//-----------------------------------------------------------------------------
100+
class CCommandExt
101+
{
102+
public:
103+
static const char* GetArg(CCommand command, unsigned int iIndex)
104+
{
105+
if ((int) iIndex >= command.ArgC())
106+
BOOST_RAISE_EXCEPTION(PyExc_IndexError, "Index out of range.");
107+
108+
return command[iIndex];
109+
}
110+
};
111+
100112
void export_command()
101113
{
102114
enum_<CommandReturn>("CommandReturn")
@@ -122,13 +134,13 @@ void export_command()
122134
)
123135

124136
.def("__getitem__",
125-
&CCommand::operator[],
137+
&CCommandExt::GetArg,
126138
"Gets the value of the argument at the given index",
127139
args("index")
128140
)
129141

130142
.def("get_arg",
131-
&CCommand::operator[],
143+
&CCommandExt::GetArg,
132144
"Gets the value of the argument at the given index",
133145
args("index")
134146
)

src/core/modules/keyvalues/keyvalues_wrap_python.cpp

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,18 @@ class KeyValuesExt
6161
{
6262
pKeyValues->SetInt(szName, bValue);
6363
}
64+
65+
static bool LoadFromFile(KeyValues* pKeyValues, const char * szFile)
66+
{
67+
return pKeyValues->LoadFromFile(filesystem, szFile);
68+
}
69+
70+
static bool SaveToFile(KeyValues* pKeyValues, const char * szFile)
71+
{
72+
return pKeyValues->SaveToFile(filesystem, szFile);
73+
}
6474
};
6575

66-
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(load_from_file_overload, LoadFromFile, 2, 3);
67-
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(save_to_file_overload, SaveToFile, 2, 3);
6876
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(find_key_overload, FindKey, 1, 2);
6977
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(get_int_overload, GetInt, 0, 2);
7078
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(get_uint64_overload, GetUint64, 0, 2);
@@ -117,19 +125,15 @@ void export_keyvalues()
117125
)
118126

119127
.def("load_from_file",
120-
&KeyValues::LoadFromFile,
121-
load_from_file_overload(
122-
"Loads KeyValues data from a file into this CKeyValues instance.",
123-
args("filesystem", "resource_name", "path_id")
124-
)
128+
&KeyValuesExt::LoadFromFile,
129+
"Loads KeyValues data from a file into this CKeyValues instance.",
130+
args("file_name")
125131
)
126132

127133
.def("save_to_file",
128-
&KeyValues::SaveToFile,
129-
save_to_file_overload(
130-
args("filesystem", "resource_name", "path_id"),
131-
"Saves the data in this CKeyValues instance to the given file path."
132-
)
134+
&KeyValuesExt::SaveToFile,
135+
args("file_name"),
136+
"Saves the data in this CKeyValues instance to the given file path."
133137
)
134138

135139
.def("find_key",

0 commit comments

Comments
 (0)