mirror of
https://git.haproxy.org/git/haproxy.git/
synced 2025-08-07 07:37:02 +02:00
MINOR: uri_normalizer: Add support for supressing leading ../
for dotdot normalizer
This adds an option to supress `../` at the start of the resulting path.
This commit is contained in:
parent
9982fc2bbd
commit
560e1a6352
@ -6012,7 +6012,7 @@ http-request early-hint <name> <fmt> [ { if | unless } <condition> ]
|
|||||||
See RFC 8297 for more information.
|
See RFC 8297 for more information.
|
||||||
|
|
||||||
http-request normalize-uri <normalizer> [ { if | unless } <condition> ]
|
http-request normalize-uri <normalizer> [ { if | unless } <condition> ]
|
||||||
http-request normalize-uri dotdot [ { if | unless } <condition> ]
|
http-request normalize-uri dotdot [ full ] [ { if | unless } <condition> ]
|
||||||
http-request normalize-uri merge-slashes [ { if | unless } <condition> ]
|
http-request normalize-uri merge-slashes [ { if | unless } <condition> ]
|
||||||
|
|
||||||
Performs normalization of the request's URI. The following normalizers are
|
Performs normalization of the request's URI. The following normalizers are
|
||||||
@ -6028,8 +6028,16 @@ http-request normalize-uri merge-slashes [ { if | unless } <condition> ]
|
|||||||
- /foo/../bar/ -> /bar/
|
- /foo/../bar/ -> /bar/
|
||||||
- /foo/bar/../ -> /foo/
|
- /foo/bar/../ -> /foo/
|
||||||
- /../bar/ -> /../bar/
|
- /../bar/ -> /../bar/
|
||||||
|
- /bar/../../ -> /../
|
||||||
- /foo//../ -> /foo/
|
- /foo//../ -> /foo/
|
||||||
|
|
||||||
|
If the "full" option is specified then "../" at the beginning will be
|
||||||
|
removed as well:
|
||||||
|
|
||||||
|
Example:
|
||||||
|
- /../bar/ -> /bar/
|
||||||
|
- /bar/../../ -> /
|
||||||
|
|
||||||
- merge-slashes: Merges adjacent slashes within the "path" component into a
|
- merge-slashes: Merges adjacent slashes within the "path" component into a
|
||||||
single slash.
|
single slash.
|
||||||
|
|
||||||
|
@ -104,6 +104,7 @@ enum act_timeout_name {
|
|||||||
enum act_normalize_uri {
|
enum act_normalize_uri {
|
||||||
ACT_NORMALIZE_URI_MERGE_SLASHES,
|
ACT_NORMALIZE_URI_MERGE_SLASHES,
|
||||||
ACT_NORMALIZE_URI_DOTDOT,
|
ACT_NORMALIZE_URI_DOTDOT,
|
||||||
|
ACT_NORMALIZE_URI_DOTDOT_FULL,
|
||||||
};
|
};
|
||||||
|
|
||||||
/* NOTE: if <.action_ptr> is defined, the referenced function will always be
|
/* NOTE: if <.action_ptr> is defined, the referenced function will always be
|
||||||
|
@ -18,7 +18,7 @@
|
|||||||
|
|
||||||
#include <haproxy/uri_normalizer-t.h>
|
#include <haproxy/uri_normalizer-t.h>
|
||||||
|
|
||||||
enum uri_normalizer_err uri_normalizer_path_dotdot(const struct ist path, struct ist *dst);
|
enum uri_normalizer_err uri_normalizer_path_dotdot(const struct ist path, int full, struct ist *dst);
|
||||||
enum uri_normalizer_err uri_normalizer_path_merge_slashes(const struct ist path, struct ist *dst);
|
enum uri_normalizer_err uri_normalizer_path_merge_slashes(const struct ist path, struct ist *dst);
|
||||||
|
|
||||||
#endif /* _HAPROXY_URI_NORMALIZER_H */
|
#endif /* _HAPROXY_URI_NORMALIZER_H */
|
||||||
|
@ -36,8 +36,13 @@ haproxy h1 -conf {
|
|||||||
http-request normalize-uri dotdot
|
http-request normalize-uri dotdot
|
||||||
http-request set-var(txn.after) url
|
http-request set-var(txn.after) url
|
||||||
|
|
||||||
|
http-request set-uri %[var(txn.before)]
|
||||||
|
http-request normalize-uri dotdot full
|
||||||
|
http-request set-var(txn.after_full) url
|
||||||
|
|
||||||
http-response add-header before %[var(txn.before)]
|
http-response add-header before %[var(txn.before)]
|
||||||
http-response add-header after %[var(txn.after)]
|
http-response add-header after %[var(txn.after)]
|
||||||
|
http-response add-header after-full %[var(txn.after_full)]
|
||||||
|
|
||||||
default_backend be
|
default_backend be
|
||||||
|
|
||||||
@ -103,54 +108,65 @@ client c2 -connect ${h1_fe_dotdot_sock} {
|
|||||||
rxresp
|
rxresp
|
||||||
expect resp.http.before == "/foo/bar"
|
expect resp.http.before == "/foo/bar"
|
||||||
expect resp.http.after == "/foo/bar"
|
expect resp.http.after == "/foo/bar"
|
||||||
|
expect resp.http.after-full == "/foo/bar"
|
||||||
|
|
||||||
txreq -url "/foo/.."
|
txreq -url "/foo/.."
|
||||||
rxresp
|
rxresp
|
||||||
expect resp.http.before == "/foo/.."
|
expect resp.http.before == "/foo/.."
|
||||||
expect resp.http.after == "/"
|
expect resp.http.after == "/"
|
||||||
|
expect resp.http.after-full == "/"
|
||||||
|
|
||||||
txreq -url "/foo/../"
|
txreq -url "/foo/../"
|
||||||
rxresp
|
rxresp
|
||||||
expect resp.http.before == "/foo/../"
|
expect resp.http.before == "/foo/../"
|
||||||
expect resp.http.after == "/"
|
expect resp.http.after == "/"
|
||||||
|
expect resp.http.after-full == "/"
|
||||||
|
|
||||||
txreq -url "/foo/bar/../"
|
txreq -url "/foo/bar/../"
|
||||||
rxresp
|
rxresp
|
||||||
expect resp.http.before == "/foo/bar/../"
|
expect resp.http.before == "/foo/bar/../"
|
||||||
expect resp.http.after == "/foo/"
|
expect resp.http.after == "/foo/"
|
||||||
|
expect resp.http.after-full == "/foo/"
|
||||||
|
|
||||||
txreq -url "/foo/../bar"
|
txreq -url "/foo/../bar"
|
||||||
rxresp
|
rxresp
|
||||||
expect resp.http.before == "/foo/../bar"
|
expect resp.http.before == "/foo/../bar"
|
||||||
expect resp.http.after == "/bar"
|
expect resp.http.after == "/bar"
|
||||||
|
expect resp.http.after-full == "/bar"
|
||||||
|
|
||||||
txreq -url "/foo/../bar/"
|
txreq -url "/foo/../bar/"
|
||||||
rxresp
|
rxresp
|
||||||
expect resp.http.before == "/foo/../bar/"
|
expect resp.http.before == "/foo/../bar/"
|
||||||
expect resp.http.after == "/bar/"
|
expect resp.http.after == "/bar/"
|
||||||
|
expect resp.http.after-full == "/bar/"
|
||||||
|
|
||||||
txreq -url "/foo/../../bar/"
|
txreq -url "/foo/../../bar/"
|
||||||
rxresp
|
rxresp
|
||||||
expect resp.http.before == "/foo/../../bar/"
|
expect resp.http.before == "/foo/../../bar/"
|
||||||
expect resp.http.after == "/../bar/"
|
expect resp.http.after == "/../bar/"
|
||||||
|
expect resp.http.after-full == "/bar/"
|
||||||
|
|
||||||
txreq -url "/foo//../../bar/"
|
txreq -url "/foo//../../bar/"
|
||||||
rxresp
|
rxresp
|
||||||
expect resp.http.before == "/foo//../../bar/"
|
expect resp.http.before == "/foo//../../bar/"
|
||||||
expect resp.http.after == "/bar/"
|
expect resp.http.after == "/bar/"
|
||||||
|
expect resp.http.after-full == "/bar/"
|
||||||
|
|
||||||
txreq -url "/foo/?bar=/foo/../"
|
txreq -url "/foo/?bar=/foo/../"
|
||||||
rxresp
|
rxresp
|
||||||
expect resp.http.before == "/foo/?bar=/foo/../"
|
expect resp.http.before == "/foo/?bar=/foo/../"
|
||||||
expect resp.http.after == "/foo/?bar=/foo/../"
|
expect resp.http.after == "/foo/?bar=/foo/../"
|
||||||
|
expect resp.http.after-full == "/foo/?bar=/foo/../"
|
||||||
|
|
||||||
txreq -url "/foo/../?bar=/foo/../"
|
txreq -url "/foo/../?bar=/foo/../"
|
||||||
rxresp
|
rxresp
|
||||||
expect resp.http.before == "/foo/../?bar=/foo/../"
|
expect resp.http.before == "/foo/../?bar=/foo/../"
|
||||||
expect resp.http.after == "/?bar=/foo/../"
|
expect resp.http.after == "/?bar=/foo/../"
|
||||||
|
expect resp.http.after-full == "/?bar=/foo/../"
|
||||||
|
|
||||||
txreq -req OPTIONS -url "*"
|
txreq -req OPTIONS -url "*"
|
||||||
rxresp
|
rxresp
|
||||||
expect resp.http.before == "*"
|
expect resp.http.before == "*"
|
||||||
expect resp.http.after == "*"
|
expect resp.http.after == "*"
|
||||||
|
expect resp.http.after-full == "*"
|
||||||
} -run
|
} -run
|
||||||
|
@ -232,14 +232,15 @@ static enum act_return http_action_normalize_uri(struct act_rule *rule, struct p
|
|||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case ACT_NORMALIZE_URI_DOTDOT: {
|
case ACT_NORMALIZE_URI_DOTDOT:
|
||||||
|
case ACT_NORMALIZE_URI_DOTDOT_FULL: {
|
||||||
const struct ist path = http_get_path(uri);
|
const struct ist path = http_get_path(uri);
|
||||||
struct ist newpath = ist2(replace->area, replace->size);
|
struct ist newpath = ist2(replace->area, replace->size);
|
||||||
|
|
||||||
if (!isttest(path))
|
if (!isttest(path))
|
||||||
goto leave;
|
goto leave;
|
||||||
|
|
||||||
err = uri_normalizer_path_dotdot(iststop(path, '?'), &newpath);
|
err = uri_normalizer_path_dotdot(iststop(path, '?'), rule->action == ACT_NORMALIZE_URI_DOTDOT_FULL, &newpath);
|
||||||
|
|
||||||
if (err != URI_NORMALIZER_ERR_NONE)
|
if (err != URI_NORMALIZER_ERR_NONE)
|
||||||
break;
|
break;
|
||||||
@ -317,7 +318,17 @@ static enum act_parse_ret parse_http_normalize_uri(const char **args, int *orig_
|
|||||||
else if (strcmp(args[cur_arg], "dotdot") == 0) {
|
else if (strcmp(args[cur_arg], "dotdot") == 0) {
|
||||||
cur_arg++;
|
cur_arg++;
|
||||||
|
|
||||||
rule->action = ACT_NORMALIZE_URI_DOTDOT;
|
if (strcmp(args[cur_arg], "full") == 0) {
|
||||||
|
cur_arg++;
|
||||||
|
rule->action = ACT_NORMALIZE_URI_DOTDOT_FULL;
|
||||||
|
}
|
||||||
|
else if (!*args[cur_arg]) {
|
||||||
|
rule->action = ACT_NORMALIZE_URI_DOTDOT;
|
||||||
|
}
|
||||||
|
else if (strcmp(args[cur_arg], "if") != 0 && strcmp(args[cur_arg], "unless") != 0) {
|
||||||
|
memprintf(err, "unknown argument '%s' for 'dotdot' normalizer", args[cur_arg]);
|
||||||
|
return ACT_RET_PRS_ERR;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
memprintf(err, "unknown normalizer '%s'", args[cur_arg]);
|
memprintf(err, "unknown normalizer '%s'", args[cur_arg]);
|
||||||
|
@ -15,8 +15,13 @@
|
|||||||
#include <haproxy/api.h>
|
#include <haproxy/api.h>
|
||||||
#include <haproxy/uri_normalizer.h>
|
#include <haproxy/uri_normalizer.h>
|
||||||
|
|
||||||
/* Merges `/../` with preceding path segments. */
|
/* Merges `/../` with preceding path segments.
|
||||||
enum uri_normalizer_err uri_normalizer_path_dotdot(const struct ist path, struct ist *dst)
|
*
|
||||||
|
* If `full` is set to `0` then `/../` will be printed at the start of the resulting
|
||||||
|
* path if the number of `/../` exceeds the number of other segments. If `full` is
|
||||||
|
* set to `1` these will not be printed.
|
||||||
|
*/
|
||||||
|
enum uri_normalizer_err uri_normalizer_path_dotdot(const struct ist path, int full, struct ist *dst)
|
||||||
{
|
{
|
||||||
enum uri_normalizer_err err;
|
enum uri_normalizer_err err;
|
||||||
|
|
||||||
@ -79,13 +84,15 @@ enum uri_normalizer_err uri_normalizer_path_dotdot(const struct ist path, struct
|
|||||||
/* Prepend a trailing slash. */
|
/* Prepend a trailing slash. */
|
||||||
*(--head) = '/';
|
*(--head) = '/';
|
||||||
|
|
||||||
/* Prepend unconsumed `/..`. */
|
if (!full) {
|
||||||
do {
|
/* Prepend unconsumed `/..`. */
|
||||||
*(--head) = '.';
|
do {
|
||||||
*(--head) = '.';
|
*(--head) = '.';
|
||||||
*(--head) = '/';
|
*(--head) = '.';
|
||||||
up--;
|
*(--head) = '/';
|
||||||
} while (up > 0);
|
up--;
|
||||||
|
} while (up > 0);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
*dst = ist2(head, tail - head);
|
*dst = ist2(head, tail - head);
|
||||||
|
Loading…
Reference in New Issue
Block a user