blob: 4fd783d4a664f7644f067089e5fe00c53f6322b6 [file] [log] [blame]
Tim Duesterhusdbd25c32021-04-15 21:45:55 +02001/*
2 * HTTP request URI normalization.
3 *
4 * Copyright 2021 Tim Duesterhus <tim@bastelstu.be>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
Tim Duesterhusd371e992021-04-15 21:45:58 +020013#include <import/ist.h>
14
Tim Duesterhusdbd25c32021-04-15 21:45:55 +020015#include <haproxy/api.h>
Tim Duesterhusd7b89be2021-04-15 21:46:01 +020016#include <haproxy/buf.h>
17#include <haproxy/chunk.h>
Tim Duesterhusa4071932021-04-15 21:46:02 +020018#include <haproxy/tools.h>
Tim Duesterhusdbd25c32021-04-15 21:45:55 +020019#include <haproxy/uri_normalizer.h>
20
Tim Duesterhus2e4a18e2021-04-21 21:20:36 +020021/* Returns 1 if the given character is part of the 'unreserved' set in the
22 * RFC 3986 ABNF.
23 * Returns 0 if not.
24 */
25static int is_unreserved_character(unsigned char c)
26{
27 switch (c) {
28 case 'A'...'Z': /* ALPHA */
29 case 'a'...'z': /* ALPHA */
30 case '0'...'9': /* DIGIT */
31 case '-':
32 case '.':
33 case '_':
34 case '~':
35 return 1;
36 default:
37 return 0;
38 }
39}
40
41/* Decodes percent encoded characters that are part of the 'unreserved' set.
42 *
43 * RFC 3986, section 2.3:
44 * > URIs that differ in the replacement of an unreserved character with
45 * > its corresponding percent-encoded US-ASCII octet are equivalent [...]
46 * > when found in a URI, should be decoded to their corresponding unreserved
47 * > characters by URI normalizers.
48 *
49 * If `strict` is set to 0 then percent characters that are not followed by a
50 * hexadecimal digit are returned as-is without performing any decoding.
51 * If `strict` is set to 1 then `URI_NORMALIZER_ERR_INVALID_INPUT` is returned
52 * for invalid sequences.
53 */
54enum uri_normalizer_err uri_normalizer_percent_decode_unreserved(const struct ist input, int strict, struct ist *dst)
55{
56 enum uri_normalizer_err err;
57
58 const size_t size = istclear(dst);
59 struct ist output = *dst;
60
61 struct ist scanner = input;
62
63 /* The output will either be shortened or have the same length. */
64 if (size < istlen(input)) {
65 err = URI_NORMALIZER_ERR_ALLOC;
66 goto fail;
67 }
68
69 while (istlen(scanner)) {
70 const char current = istshift(&scanner);
71
72 if (current == '%') {
73 if (istlen(scanner) >= 2) {
74 if (ishex(istptr(scanner)[0]) && ishex(istptr(scanner)[1])) {
75 char hex1, hex2, c;
76
77 hex1 = istshift(&scanner);
78 hex2 = istshift(&scanner);
79 c = (hex2i(hex1) << 4) + hex2i(hex2);
80
81 if (is_unreserved_character(c)) {
82 output = __istappend(output, c);
83 }
84 else {
85 output = __istappend(output, current);
86 output = __istappend(output, hex1);
87 output = __istappend(output, hex2);
88 }
89
90 continue;
91 }
92 }
93
94 if (strict) {
95 err = URI_NORMALIZER_ERR_INVALID_INPUT;
96 goto fail;
97 }
98 else {
99 output = __istappend(output, current);
100 }
101 }
102 else {
103 output = __istappend(output, current);
104 }
105 }
106
107 *dst = output;
108
109 return URI_NORMALIZER_ERR_NONE;
110
111 fail:
112
113 return err;
114}
115
Tim Duesterhusa4071932021-04-15 21:46:02 +0200116/* Uppercases letters used in percent encoding.
117 *
118 * If `strict` is set to 0 then percent characters that are not followed by a
119 * hexadecimal digit are returned as-is without modifying the following letters.
120 * If `strict` is set to 1 then `URI_NORMALIZER_ERR_INVALID_INPUT` is returned
121 * for invalid sequences.
122 */
123enum uri_normalizer_err uri_normalizer_percent_upper(const struct ist input, int strict, struct ist *dst)
124{
125 enum uri_normalizer_err err;
126
127 const size_t size = istclear(dst);
128 struct ist output = *dst;
129
130 struct ist scanner = input;
131
132 /* The output will have the same length. */
133 if (size < istlen(input)) {
134 err = URI_NORMALIZER_ERR_ALLOC;
135 goto fail;
136 }
137
138 while (istlen(scanner)) {
139 const char current = istshift(&scanner);
140
141 if (current == '%') {
142 if (istlen(scanner) >= 2) {
143 if (ishex(istptr(scanner)[0]) && ishex(istptr(scanner)[1])) {
144 output = __istappend(output, current);
145 output = __istappend(output, toupper(istshift(&scanner)));
146 output = __istappend(output, toupper(istshift(&scanner)));
147 continue;
148 }
149 }
150
151 if (strict) {
152 err = URI_NORMALIZER_ERR_INVALID_INPUT;
153 goto fail;
154 }
155 else {
156 output = __istappend(output, current);
157 }
158 }
159 else {
160 output = __istappend(output, current);
161 }
162 }
163
164 *dst = output;
165
166 return URI_NORMALIZER_ERR_NONE;
167
168 fail:
169
170 return err;
171}
172
Maximilian Maderff3bb8b2021-04-21 00:22:50 +0200173/* Removes `/./` from the given path. */
174enum uri_normalizer_err uri_normalizer_path_dot(const struct ist path, struct ist *dst)
175{
176 enum uri_normalizer_err err;
177
178 const size_t size = istclear(dst);
179 struct ist newpath = *dst;
180
181 struct ist scanner = path;
182
183 /* The path will either be shortened or have the same length. */
184 if (size < istlen(path)) {
185 err = URI_NORMALIZER_ERR_ALLOC;
186 goto fail;
187 }
188
189 while (istlen(scanner) > 0) {
190 const struct ist segment = istsplit(&scanner, '/');
191
192 if (!isteq(segment, ist("."))) {
193 if (istcat(&newpath, segment, size) < 0) {
194 /* This is impossible, because we checked the size of the destination buffer. */
195 my_unreachable();
196 err = URI_NORMALIZER_ERR_INTERNAL_ERROR;
197 goto fail;
198 }
199
200 if (istend(segment) != istend(scanner))
201 newpath = __istappend(newpath, '/');
202 }
203 }
204
205 *dst = newpath;
206
207 return URI_NORMALIZER_ERR_NONE;
208
209 fail:
210
211 return err;
212}
213
Tim Duesterhus560e1a62021-04-15 21:46:00 +0200214/* Merges `/../` with preceding path segments.
215 *
216 * If `full` is set to `0` then `/../` will be printed at the start of the resulting
217 * path if the number of `/../` exceeds the number of other segments. If `full` is
218 * set to `1` these will not be printed.
219 */
220enum uri_normalizer_err uri_normalizer_path_dotdot(const struct ist path, int full, struct ist *dst)
Tim Duesterhus9982fc22021-04-15 21:45:59 +0200221{
222 enum uri_normalizer_err err;
223
224 const size_t size = istclear(dst);
225 char * const tail = istptr(*dst) + size;
226 char *head = tail;
227
228 ssize_t offset = istlen(path) - 1;
229
230 int up = 0;
231
232 /* The path will either be shortened or have the same length. */
233 if (size < istlen(path)) {
234 err = URI_NORMALIZER_ERR_ALLOC;
235 goto fail;
236 }
237
238 /* Handle `/..` at the end of the path without a trailing slash. */
239 if (offset >= 2 && istmatch(istadv(path, offset - 2), ist("/.."))) {
240 up++;
241 offset -= 2;
242 }
243
244 while (offset >= 0) {
245 if (offset >= 3 && istmatch(istadv(path, offset - 3), ist("/../"))) {
246 up++;
247 offset -= 3;
248 continue;
249 }
250
251 if (up > 0) {
252 /* Skip the slash. */
253 offset--;
254
255 /* First check whether we already reached the start of the path,
256 * before popping the current `/../`.
257 */
258 if (offset >= 0) {
259 up--;
260
261 /* Skip the current path segment. */
262 while (offset >= 0 && istptr(path)[offset] != '/')
263 offset--;
264 }
265 }
266 else {
267 /* Prepend the slash. */
268 *(--head) = istptr(path)[offset];
269 offset--;
270
271 /* Prepend the current path segment. */
272 while (offset >= 0 && istptr(path)[offset] != '/') {
273 *(--head) = istptr(path)[offset];
274 offset--;
275 }
276 }
277 }
278
279 if (up > 0) {
280 /* Prepend a trailing slash. */
281 *(--head) = '/';
282
Tim Duesterhus560e1a62021-04-15 21:46:00 +0200283 if (!full) {
284 /* Prepend unconsumed `/..`. */
285 do {
286 *(--head) = '.';
287 *(--head) = '.';
288 *(--head) = '/';
289 up--;
290 } while (up > 0);
291 }
Tim Duesterhus9982fc22021-04-15 21:45:59 +0200292 }
293
294 *dst = ist2(head, tail - head);
295
296 return URI_NORMALIZER_ERR_NONE;
297
298 fail:
299
300 return err;
301}
302
Tim Duesterhusd371e992021-04-15 21:45:58 +0200303/* Merges adjacent slashes in the given path. */
304enum uri_normalizer_err uri_normalizer_path_merge_slashes(const struct ist path, struct ist *dst)
305{
306 enum uri_normalizer_err err;
307
308 const size_t size = istclear(dst);
309 struct ist newpath = *dst;
310
311 struct ist scanner = path;
312
313 /* The path will either be shortened or have the same length. */
314 if (size < istlen(path)) {
315 err = URI_NORMALIZER_ERR_ALLOC;
316 goto fail;
317 }
318
319 while (istlen(scanner) > 0) {
320 const char current = istshift(&scanner);
321
322 if (current == '/') {
323 while (istlen(scanner) > 0 && *istptr(scanner) == '/')
324 scanner = istnext(scanner);
325 }
326
327 newpath = __istappend(newpath, current);
328 }
329
330 *dst = newpath;
331
332 return URI_NORMALIZER_ERR_NONE;
333
334 fail:
335
336 return err;
337}
338
Tim Duesterhusd7b89be2021-04-15 21:46:01 +0200339/* Compares two query parameters by name. Query parameters are ordered
340 * as with memcmp. Shorter parameter names are ordered lower. Identical
341 * parameter names are compared by their pointer to maintain a stable
342 * sort.
343 */
344static int query_param_cmp(const void *a, const void *b)
345{
346 const struct ist param_a = *(struct ist*)a;
347 const struct ist param_b = *(struct ist*)b;
348 const struct ist param_a_name = iststop(param_a, '=');
349 const struct ist param_b_name = iststop(param_b, '=');
350
351 int cmp = istdiff(param_a_name, param_b_name);
352
353 if (cmp != 0)
354 return cmp;
355
356 /* The contents are identical: Compare the pointer. */
357 if (istptr(param_a) < istptr(param_b))
358 return -1;
359
360 if (istptr(param_a) > istptr(param_b))
361 return 1;
362
363 return 0;
364}
365
366/* Sorts the parameters within the given query string. */
367enum uri_normalizer_err uri_normalizer_query_sort(const struct ist query, const char delim, struct ist *dst)
368{
369 enum uri_normalizer_err err;
370
371 const size_t size = istclear(dst);
372 struct ist newquery = *dst;
373
374 struct ist scanner = query;
375
376 const struct buffer *trash = get_trash_chunk();
377 struct ist *params = (struct ist *)b_orig(trash);
Maximilian Maderc9c79572021-04-21 00:22:49 +0200378 const size_t max_param = b_size(trash) / sizeof(*params);
Tim Duesterhusd7b89be2021-04-15 21:46:01 +0200379 size_t param_count = 0;
380
381 size_t i;
382
383 /* The query will have the same length. */
384 if (size < istlen(query)) {
385 err = URI_NORMALIZER_ERR_ALLOC;
386 goto fail;
387 }
388
389 /* Handle the leading '?'. */
390 newquery = __istappend(newquery, istshift(&scanner));
391
392 while (istlen(scanner) > 0) {
393 const struct ist param = istsplit(&scanner, delim);
394
395 if (param_count + 1 > max_param) {
396 err = URI_NORMALIZER_ERR_ALLOC;
397 goto fail;
398 }
399
400 params[param_count] = param;
401 param_count++;
402 }
403
404 qsort(params, param_count, sizeof(*params), query_param_cmp);
405
406 for (i = 0; i < param_count; i++) {
407 if (i > 0)
Maximilian Mader11f6f852021-04-21 00:22:48 +0200408 newquery = __istappend(newquery, delim);
Tim Duesterhusd7b89be2021-04-15 21:46:01 +0200409
410 if (istcat(&newquery, params[i], size) < 0) {
411 /* This is impossible, because we checked the size of the destination buffer. */
412 my_unreachable();
413 err = URI_NORMALIZER_ERR_INTERNAL_ERROR;
414 goto fail;
415 }
416 }
417
418 *dst = newquery;
419
420 return URI_NORMALIZER_ERR_NONE;
421
422 fail:
423
424 return err;
425}
Tim Duesterhusd371e992021-04-15 21:45:58 +0200426
Tim Duesterhusdbd25c32021-04-15 21:45:55 +0200427/*
428 * Local variables:
429 * c-indent-level: 8
430 * c-basic-offset: 8
431 * End:
432 */