Skip to content

Commit d977370

Browse files
committed
Perform NormalisePath & NormalisePathWin transformations in-place
1 parent de3b2a0 commit d977370

File tree

3 files changed

+24
-58
lines changed

3 files changed

+24
-58
lines changed

src/actions/transformations/normalise_path.cc

Lines changed: 22 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -25,53 +25,37 @@ NormalisePath::NormalisePath(const std::string &action)
2525
}
2626

2727
bool NormalisePath::transform(std::string &value, const Transaction *trans) const {
28-
int _changed = 0;
29-
30-
char *tmp = reinterpret_cast<char *>(
31-
malloc(sizeof(char) * value.size() + 1));
32-
memcpy(tmp, value.c_str(), value.size() + 1);
33-
tmp[value.size()] = '\0';
34-
35-
int i = normalize_path_inplace((unsigned char *)tmp,
36-
value.size(), 0, &_changed);
37-
38-
std::string ret("");
39-
ret.assign(tmp, i);
40-
free(tmp);
41-
42-
const auto changed = ret != value;
43-
value = ret;
44-
return changed;
28+
return normalize_path_inplace(value, false);
4529
}
4630

4731

4832
/**
4933
*
5034
* IMP1 Assumes NUL-terminated
5135
*/
52-
int NormalisePath::normalize_path_inplace(unsigned char *input, int input_len,
53-
int win, int *changed) {
36+
bool NormalisePath::normalize_path_inplace(std::string &val, const bool win) {
5437
unsigned char *src;
5538
unsigned char *dst;
5639
unsigned char *end;
57-
int ldst = 0;
5840
int hitroot = 0;
5941
int done = 0;
6042
int relative;
6143
int trailing;
6244

63-
*changed = 0;
45+
bool changed = false;
6446

6547
/* Need at least one byte to normalize */
66-
if (input_len <= 0) return 0;
48+
if(val.empty()) return false;
49+
50+
auto input = reinterpret_cast<unsigned char*>(val.data());
51+
const auto input_len = val.length();
6752

6853
/*
6954
* ENH: Deal with UNC and drive letters?
7055
*/
7156

7257
src = dst = input;
7358
end = input + (input_len - 1);
74-
ldst = 1;
7559

7660
relative = ((*input == '/') || (win && (*input == '\\'))) ? 0 : 1;
7761
trailing = ((*end == '/') || (win && (*end == '\\'))) ? 1 : 0;
@@ -82,11 +66,11 @@ int NormalisePath::normalize_path_inplace(unsigned char *input, int input_len,
8266
if (win) {
8367
if (*src == '\\') {
8468
*src = '/';
85-
*changed = 1;
69+
changed = true;
8670
}
8771
if ((src < end) && (*(src + 1) == '\\')) {
8872
*(src + 1) = '/';
89-
*changed = 1;
73+
changed = true;
9074
}
9175
}
9276

@@ -104,7 +88,7 @@ int NormalisePath::normalize_path_inplace(unsigned char *input, int input_len,
10488
/* Could it be an empty path segment? */
10589
if ((src != end) && *src == '/') {
10690
/* Ignore */
107-
*changed = 1;
91+
changed = true;
10892
goto copy; /* Copy will take care of this. */
10993
} else if (*src == '.') {
11094
/* Could it be a back or self reference? */
@@ -141,25 +125,25 @@ int NormalisePath::normalize_path_inplace(unsigned char *input, int input_len,
141125
}
142126
}
143127

144-
if (done) goto length; /* Skip the copy. */
128+
if (done) goto skip_copy; /* Skip the copy. */
145129
src++;
146130

147-
*changed = 1;
131+
changed = true;
148132
} else if (dst == input) {
149133
/* Relative Self-reference? */
150-
*changed = 1;
134+
changed = true;
151135

152136
/* Ignore. */
153137

154-
if (done) goto length; /* Skip the copy. */
138+
if (done) goto skip_copy; /* Skip the copy. */
155139
src++;
156140
} else if (*(dst - 1) == '/') {
157141
/* Self-reference? */
158-
*changed = 1;
142+
changed = true;
159143

160144
/* Ignore. */
161145

162-
if (done) goto length; /* Skip the copy. */
146+
if (done) goto skip_copy; /* Skip the copy. */
163147
dst--;
164148
src++;
165149
}
@@ -179,35 +163,35 @@ int NormalisePath::normalize_path_inplace(unsigned char *input, int input_len,
179163
&& ((*(src + 1) == '/') || (win && (*(src + 1) == '\\'))) ) {
180164
src++;
181165
}
182-
if (oldsrc != src) *changed = 1;
166+
if (oldsrc != src) changed = true;
183167

184168
/* Do not copy the forward slash to the root
185169
* if it is not a relative path. Instead
186170
* move over the slash to the next segment.
187171
*/
188172
if (relative && (dst == input)) {
189173
src++;
190-
goto length; /* Skip the copy */
174+
goto skip_copy; /* Skip the copy */
191175
}
192176
}
193177

194178
*(dst++) = *(src++);
195179

196-
length:
197-
ldst = (dst - input);
180+
skip_copy:
181+
; // nop for the goto label to work
198182
}
199183
/* Make sure that there is not a trailing slash in the
200184
* normalized form if there was not one in the original form.
201185
*/
202186
if (!trailing && (dst > input) && *(dst - 1) == '/') {
203-
ldst--;
204187
dst--;
205188
}
206189

207190
/* Always NUL terminate */
208191
*dst = '\0';
209192

210-
return ldst;
193+
val.resize(dst - input);
194+
return changed;
211195
}
212196

213197

src/actions/transformations/normalise_path.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ class NormalisePath : public Transformation {
2626

2727
bool transform(std::string &value, const Transaction *trans) const override;
2828

29-
static int normalize_path_inplace(unsigned char *input, int input_len,
30-
int win, int *changed);
29+
static bool normalize_path_inplace(std::string &val, const bool win);
3130
};
3231

3332
} // namespace modsecurity::actions::transformations

src/actions/transformations/normalise_path_win.cc

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,7 @@ namespace modsecurity::actions::transformations {
2222

2323

2424
bool NormalisePathWin::transform(std::string &value, const Transaction *trans) const {
25-
int _changed;
26-
27-
char *tmp = reinterpret_cast<char *>(
28-
malloc(sizeof(char) * value.size() + 1));
29-
memcpy(tmp, value.c_str(), value.size() + 1);
30-
tmp[value.size()] = '\0';
31-
32-
int i = NormalisePath::normalize_path_inplace(
33-
reinterpret_cast<unsigned char *>(tmp),
34-
value.size(), 1, &_changed);
35-
36-
std::string ret("");
37-
ret.assign(tmp, i);
38-
free(tmp);
39-
40-
const auto changed = ret != value;
41-
value = ret;
42-
return changed;
25+
return NormalisePath::normalize_path_inplace(value, true);
4326
}
4427

4528

0 commit comments

Comments
 (0)