]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: uri_normalizer: Add a `merge-slashes` normalizer to http-request normalize-uri
authorTim Duesterhus <tim@bastelstu.be>
Thu, 15 Apr 2021 19:45:58 +0000 (21:45 +0200)
committerChristopher Faulet <cfaulet@haproxy.com>
Mon, 19 Apr 2021 07:05:57 +0000 (09:05 +0200)
This normalizer merges adjacent slashes into a single slash, thus removing
empty path segments.

See GitHub Issue #714.

doc/configuration.txt
include/haproxy/action-t.h
include/haproxy/uri_normalizer.h
reg-tests/http-rules/normalize_uri.vtc [new file with mode: 0644]
src/http_act.c
src/uri_normalizer.c

index 4241f9227dedcbe64225e8883ba293119b1ff4b7..4dec9c858c2dd49bf7b4c03dab8d6ab6b9ae6c7b 100644 (file)
@@ -6011,6 +6011,19 @@ http-request early-hint <name> <fmt> [ { if | unless } <condition> ]
 
   See RFC 8297 for more information.
 
+http-request normalize-uri <normalizer> [ { if | unless } <condition> ]
+http-request normalize-uri merge-slashes [ { if | unless } <condition> ]
+
+  Performs normalization of the request's URI. The following normalizers are
+  available:
+
+  - merge-slashes: Merges adjacent slashes within the "path" component into a
+      single slash.
+
+      Example:
+      - //        -> /
+      - /foo//bar -> /foo/bar
+
 http-request redirect <rule> [ { if | unless } <condition> ]
 
   This performs an HTTP redirection based on a redirect rule. This is exactly
index 2909b0da20fb52491621184dbf44d3408dad3c21..4a3e3f8bdd86c0e68cb0c67d40950e523c721e2b 100644 (file)
@@ -102,7 +102,7 @@ enum act_timeout_name {
 };
 
 enum act_normalize_uri {
-       ACT_NORMALIZE_URI_PLACEHOLDER,
+       ACT_NORMALIZE_URI_MERGE_SLASHES,
 };
 
 /* NOTE: if <.action_ptr> is defined, the referenced function will always be
index 20341a90762a7b6d37a3d2fc69a4dcc4e6f0aa7b..416f5b7c5ec921d1a9d8e4fe5403349b5e51db2b 100644 (file)
 #ifndef _HAPROXY_URI_NORMALIZER_H
 #define _HAPROXY_URI_NORMALIZER_H
 
+#include <import/ist.h>
+
 #include <haproxy/uri_normalizer-t.h>
 
+enum uri_normalizer_err uri_normalizer_path_merge_slashes(const struct ist path, struct ist *dst);
+
 #endif /* _HAPROXY_URI_NORMALIZER_H */
 
 /*
diff --git a/reg-tests/http-rules/normalize_uri.vtc b/reg-tests/http-rules/normalize_uri.vtc
new file mode 100644 (file)
index 0000000..3303760
--- /dev/null
@@ -0,0 +1,87 @@
+varnishtest "normalize-uri tests"
+#REQUIRE_VERSION=2.4
+
+# This reg-test tests the http-request normalize-uri action.
+
+feature ignore_unknown_macro
+
+server s1 {
+    rxreq
+    txresp
+} -repeat 10 -start
+
+haproxy h1 -conf {
+    defaults
+        mode http
+        timeout connect 1s
+        timeout client  1s
+        timeout server  1s
+
+    frontend fe_merge_slashes
+        bind "fd@${fe_merge_slashes}"
+
+        http-request set-var(txn.before) url
+        http-request normalize-uri merge-slashes
+        http-request set-var(txn.after) url
+
+        http-response add-header before  %[var(txn.before)]
+        http-response add-header after  %[var(txn.after)]
+
+        default_backend be
+
+    backend be
+        server s1 ${s1_addr}:${s1_port}
+
+} -start
+
+client c1 -connect ${h1_fe_merge_slashes_sock} {
+    txreq -url "/foo/bar"
+    rxresp
+    expect resp.http.before == "/foo/bar"
+    expect resp.http.after == "/foo/bar"
+
+    txreq -url "/foo//bar"
+    rxresp
+    expect resp.http.before == "/foo//bar"
+    expect resp.http.after == "/foo/bar"
+
+    txreq -url "/foo///bar"
+    rxresp
+    expect resp.http.before == "/foo///bar"
+    expect resp.http.after == "/foo/bar"
+
+    txreq -url "///foo///bar"
+    rxresp
+    expect resp.http.before == "///foo///bar"
+    expect resp.http.after == "/foo/bar"
+
+    txreq -url "///foo/bar"
+    rxresp
+    expect resp.http.before == "///foo/bar"
+    expect resp.http.after == "/foo/bar"
+
+    txreq -url "///foo///bar///"
+    rxresp
+    expect resp.http.before == "///foo///bar///"
+    expect resp.http.after == "/foo/bar/"
+
+    txreq -url "///"
+    rxresp
+    expect resp.http.before == "///"
+    expect resp.http.after == "/"
+
+    txreq -url "/foo?bar=///"
+    rxresp
+    expect resp.http.before == "/foo?bar=///"
+    expect resp.http.after == "/foo?bar=///"
+
+    txreq -url "//foo?bar=///"
+    rxresp
+    expect resp.http.before == "//foo?bar=///"
+    expect resp.http.after == "/foo?bar=///"
+
+    txreq -req OPTIONS -url "*"
+    rxresp
+    expect resp.http.before == "*"
+    expect resp.http.after == "*"
+} -run
index 134c9037bdf0db013ebb45ee6a6ed857e44a1793..2af4d471a48aa579ffa913c70fa41a661f592b05 100644 (file)
@@ -215,8 +215,23 @@ static enum act_return http_action_normalize_uri(struct act_rule *rule, struct p
                goto fail_alloc;
 
        switch ((enum act_normalize_uri) rule->action) {
-               case ACT_NORMALIZE_URI_PLACEHOLDER:
-                       (void) uri;
+               case ACT_NORMALIZE_URI_MERGE_SLASHES: {
+                       const struct ist path = http_get_path(uri);
+                       struct ist newpath = ist2(replace->area, replace->size);
+
+                       if (!isttest(path))
+                               goto leave;
+
+                       err = uri_normalizer_path_merge_slashes(iststop(path, '?'), &newpath);
+
+                       if (err != URI_NORMALIZER_ERR_NONE)
+                               break;
+
+                       if (!http_replace_req_path(htx, newpath, 0))
+                               goto fail_rewrite;
+
+                       break;
+               }
        }
 
        switch (err) {
@@ -277,8 +292,10 @@ static enum act_parse_ret parse_http_normalize_uri(const char **args, int *orig_
                return ACT_RET_PRS_ERR;
        }
 
-       if (0) {
+       if (strcmp(args[cur_arg], "merge-slashes") == 0) {
+               cur_arg++;
 
+               rule->action = ACT_NORMALIZE_URI_MERGE_SLASHES;
        }
        else {
                memprintf(err, "unknown normalizer '%s'", args[cur_arg]);
index 7db47d19806e962773ec626ad4c05416d3b55c98..abc029be59dafa7e70005c3b82d64c758d3e8b0b 100644 (file)
  *
  */
 
+#include <import/ist.h>
+
 #include <haproxy/api.h>
 #include <haproxy/uri_normalizer.h>
 
+/* Merges adjacent slashes in the given path. */
+enum uri_normalizer_err uri_normalizer_path_merge_slashes(const struct ist path, struct ist *dst)
+{
+       enum uri_normalizer_err err;
+
+       const size_t size = istclear(dst);
+       struct ist newpath = *dst;
+
+       struct ist scanner = path;
+
+       /* The path will either be shortened or have the same length. */
+       if (size < istlen(path)) {
+               err = URI_NORMALIZER_ERR_ALLOC;
+               goto fail;
+       }
+
+       while (istlen(scanner) > 0) {
+               const char current = istshift(&scanner);
+
+               if (current == '/') {
+                       while (istlen(scanner) > 0 && *istptr(scanner) == '/')
+                               scanner = istnext(scanner);
+               }
+
+               newpath = __istappend(newpath, current);
+       }
+
+       *dst = newpath;
+
+       return URI_NORMALIZER_ERR_NONE;
+
+  fail:
+
+       return err;
+}
+
+
 /*
  * Local variables:
  *  c-indent-level: 8