Skip to content

Commit 8bf40cd

Browse files
LucaGuerrapoiana
authored andcommitted
update(engine): port decode_uri in falco engine
Signed-off-by: Luca Guerra <[email protected]>
1 parent 22cc2e5 commit 8bf40cd

File tree

3 files changed

+80
-2
lines changed

3 files changed

+80
-2
lines changed

Diff for: userspace/engine/falco_utils.cpp

+76
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ limitations under the License.
2020
#include <cstring>
2121
#include <iomanip>
2222

23+
#include "falco_common.h"
2324
#include "falco_utils.h"
2425
#include "utils.h"
2526

@@ -159,6 +160,81 @@ void readfile(const std::string& filename, std::string& data)
159160

160161
return;
161162
}
163+
164+
// URI-decodes the given string by replacing percent-encoded
165+
// characters with the actual character. Returns the decoded string.
166+
//
167+
// When plus_as_space is true, non-encoded plus signs in the query are decoded as spaces.
168+
// (http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.1)
169+
std::string decode_uri(const std::string& str, bool plus_as_space)
170+
{
171+
std::string decoded_str;
172+
bool in_query = false;
173+
std::string::const_iterator it = str.begin();
174+
std::string::const_iterator end = str.end();
175+
while(it != end)
176+
{
177+
char c = *it++;
178+
if(c == '?')
179+
{
180+
in_query = true;
181+
}
182+
// spaces may be encoded as plus signs in the query
183+
if(in_query && plus_as_space && c == '+')
184+
{
185+
c = ' ';
186+
}
187+
else if(c == '%')
188+
{
189+
if (it == end)
190+
{
191+
throw falco_exception("URI encoding: no hex digit following percent sign in " + str);
192+
}
193+
char hi = *it++;
194+
if (it == end)
195+
{
196+
throw falco_exception("URI encoding: two hex digits must follow percent sign in " + str);
197+
}
198+
char lo = *it++;
199+
if (hi >= '0' && hi <= '9')
200+
{
201+
c = hi - '0';
202+
}
203+
else if (hi >= 'A' && hi <= 'F')
204+
{
205+
c = hi - 'A' + 10;
206+
}
207+
else if (hi >= 'a' && hi <= 'f')
208+
{
209+
c = hi - 'a' + 10;
210+
}
211+
else
212+
{
213+
throw falco_exception("URI encoding: not a hex digit found in " + str);
214+
}
215+
c *= 16;
216+
if (lo >= '0' && lo <= '9')
217+
{
218+
c += lo - '0';
219+
}
220+
else if (lo >= 'A' && lo <= 'F')
221+
{
222+
c += lo - 'A' + 10;
223+
}
224+
else if (lo >= 'a' && lo <= 'f')
225+
{
226+
c += lo - 'a' + 10;
227+
}
228+
else
229+
{
230+
throw falco_exception("URI encoding: not a hex digit");
231+
}
232+
}
233+
decoded_str += c;
234+
}
235+
return decoded_str;
236+
}
237+
162238
namespace network
163239
{
164240
bool is_unix_scheme(const std::string& url)

Diff for: userspace/engine/falco_utils.h

+2
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ void readfile(const std::string& filename, std::string& data);
5252

5353
uint32_t hardware_concurrency();
5454

55+
std::string decode_uri(const std::string& str, bool plus_as_space);
56+
5557
namespace network
5658
{
5759
static const std::string UNIX_SCHEME("unix://");

Diff for: userspace/engine/json_evt.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ limitations under the License.
1717

1818
#include <ctype.h>
1919

20-
#include "uri.h"
2120
#include "utils.h"
2221

2322
#include "falco_common.h"
2423
#include "json_evt.h"
24+
#include "falco_utils.h"
2525

2626
using json = nlohmann::json;
2727
using namespace std;
@@ -1006,7 +1006,7 @@ bool k8s_audit_filter_check::extract_query_param(const nlohmann::json &j,
10061006
{
10071007
std::vector<std::string> param_parts = sinsp_split(part, '=');
10081008

1009-
if(param_parts.size() == 2 && uri::decode(param_parts[0], true) == jchk.idx())
1009+
if(param_parts.size() == 2 && falco::utils::decode_uri(param_parts[0], true) == jchk.idx())
10101010
{
10111011
jchk.add_extracted_value(param_parts[1]);
10121012
return true;

0 commit comments

Comments
 (0)