0de8c9
diff --git a/docs/manual/rewrite/flags.html.en b/docs/manual/rewrite/flags.html.en
0de8c9
index 7cd4990..2242312 100644
0de8c9
--- a/docs/manual/rewrite/flags.html.en
0de8c9
+++ b/docs/manual/rewrite/flags.html.en
0de8c9
@@ -85,10 +85,6 @@ of how you might use them.

0de8c9
 

B (escape backreferences)

0de8c9
 

The [B] flag instructs RewriteRule to escape non-alphanumeric

0de8c9
 characters before applying the transformation.

0de8c9
-

In 2.4.26 and later, you can limit the escaping to specific characters

0de8c9
-in backreferences by listing them: [B=#?;]. Note: The space
0de8c9
-character can be used in the list of characters to escape, but it cannot be
0de8c9
-the last character in the list.

0de8c9
 
0de8c9
 

mod_rewrite has to unescape URLs before mapping them,

0de8c9
 so backreferences are unescaped at the time they are applied.
0de8c9
@@ -120,6 +116,16 @@ when the backend may break if presented with an unescaped URL.

0de8c9
 
0de8c9
 

An alternative to this flag is using a RewriteCond to capture against %{THE_REQUEST} which will capture

0de8c9
 strings in the encoded form.

0de8c9
+
0de8c9
+

In 2.4.26 and later, you can limit the escaping to specific characters

0de8c9
+in backreferences by listing them: [B=#?;]. Note: The space
0de8c9
+character can be used in the list of characters to escape, but you must quote
0de8c9
+the entire third argument of RewriteRule
0de8c9
+and the space must not be the last character in the list.

0de8c9
+
0de8c9
+
# Escape spaces and question marks.
0de8c9
+RewriteRule "^search/(.*)$" "/search.php?term=$1" "[B= ?]"
0de8c9
+
0de8c9
 
top
0de8c9
 
0de8c9
 

BNP|backrefnoplus (don't escape space to +)

0de8c9
diff --git a/modules/mappers/mod_rewrite.c b/modules/mappers/mod_rewrite.c
0de8c9
index 9439965..5195cee 100644
0de8c9
--- a/modules/mappers/mod_rewrite.c
0de8c9
+++ b/modules/mappers/mod_rewrite.c
0de8c9
@@ -173,6 +173,7 @@ static const char* really_last_key = "rewrite_really_last";
0de8c9
 #define RULEFLAG_END                (1<<17)
0de8c9
 #define RULEFLAG_ESCAPENOPLUS       (1<<18)
0de8c9
 #define RULEFLAG_QSLAST             (1<<19)
0de8c9
+#define RULEFLAG_QSNONE             (1<<20) /* programattic only */
0de8c9
 
0de8c9
 /* return code of the rewrite rule
0de8c9
  * the result may be escaped - or not
0de8c9
@@ -769,11 +770,19 @@ static char *escape_absolute_uri(apr_pool_t *p, char *uri, unsigned scheme)
0de8c9
  * split out a QUERY_STRING part from
0de8c9
  * the current URI string
0de8c9
  */
0de8c9
-static void splitout_queryargs(request_rec *r, int qsappend, int qsdiscard, 
0de8c9
-                               int qslast)
0de8c9
+static void splitout_queryargs(request_rec *r, int flags)
0de8c9
 {
0de8c9
     char *q;
0de8c9
     int split, skip;
0de8c9
+    int qsappend = flags & RULEFLAG_QSAPPEND;
0de8c9
+    int qsdiscard = flags & RULEFLAG_QSDISCARD;
0de8c9
+    int qslast = flags & RULEFLAG_QSLAST;
0de8c9
+
0de8c9
+    if (flags & RULEFLAG_QSNONE) {
0de8c9
+        rewritelog((r, 2, NULL, "discarding query string, no parse from substitution"));
0de8c9
+        r->args = NULL;
0de8c9
+        return;
0de8c9
+    }
0de8c9
 
0de8c9
     /* don't touch, unless it's a scheme for which a query string makes sense.
0de8c9
      * See RFC 1738 and RFC 2368.
0de8c9
@@ -798,7 +807,7 @@ static void splitout_queryargs(request_rec *r, int qsappend, int qsdiscard,
0de8c9
         olduri = apr_pstrdup(r->pool, r->filename);
0de8c9
         *q++ = '\0';
0de8c9
         if (qsappend) {
0de8c9
-            if (*q) { 
0de8c9
+            if (*q) {
0de8c9
                 r->args = apr_pstrcat(r->pool, q, "&" , r->args, NULL);
0de8c9
             }
0de8c9
         }
0de8c9
@@ -806,9 +815,9 @@ static void splitout_queryargs(request_rec *r, int qsappend, int qsdiscard,
0de8c9
             r->args = apr_pstrdup(r->pool, q);
0de8c9
         }
0de8c9
 
0de8c9
-        if (r->args) { 
0de8c9
+        if (r->args) {
0de8c9
            len = strlen(r->args);
0de8c9
-      
0de8c9
+
0de8c9
            if (!len) {
0de8c9
                r->args = NULL;
0de8c9
            }
0de8c9
@@ -2761,7 +2770,7 @@ static apr_status_t rewritelock_remove(void *data)
0de8c9
  * XXX: what an inclined parser. Seems we have to leave it so
0de8c9
  *      for backwards compat. *sigh*
0de8c9
  */
0de8c9
-static int parseargline(char *str, char **a1, char **a2, char **a3)
0de8c9
+static int parseargline(char *str, char **a1, char **a2, char **a2_end, char **a3)
0de8c9
 {
0de8c9
     char quote;
0de8c9
 
0de8c9
@@ -2812,8 +2821,10 @@ static int parseargline(char *str, char **a1, char **a2, char **a3)
0de8c9
 
0de8c9
     if (!*str) {
0de8c9
         *a3 = NULL; /* 3rd argument is optional */
0de8c9
+        *a2_end = str;
0de8c9
         return 0;
0de8c9
     }
0de8c9
+    *a2_end = str;
0de8c9
     *str++ = '\0';
0de8c9
 
0de8c9
     while (apr_isspace(*str)) {
0de8c9
@@ -3353,7 +3364,7 @@ static const char *cmd_rewritecond(cmd_parms *cmd, void *in_dconf,
0de8c9
     rewrite_server_conf *sconf;
0de8c9
     rewritecond_entry *newcond;
0de8c9
     ap_regex_t *regexp;
0de8c9
-    char *a1 = NULL, *a2 = NULL, *a3 = NULL;
0de8c9
+    char *a1 = NULL, *a2 = NULL, *a2_end, *a3 = NULL;
0de8c9
     const char *err;
0de8c9
 
0de8c9
     sconf = ap_get_module_config(cmd->server->module_config, &rewrite_module);
0de8c9
@@ -3371,7 +3382,7 @@ static const char *cmd_rewritecond(cmd_parms *cmd, void *in_dconf,
0de8c9
      * of the argument line. So we can use a1 .. a3 without
0de8c9
      * copying them again.
0de8c9
      */
0de8c9
-    if (parseargline(str, &a1, &a2, &a3)) {
0de8c9
+    if (parseargline(str, &a1, &a2, &a2_end, &a3)) {
0de8c9
         return apr_pstrcat(cmd->pool, "RewriteCond: bad argument line '", str,
0de8c9
                            "'", NULL);
0de8c9
     }
0de8c9
@@ -3779,7 +3790,7 @@ static const char *cmd_rewriterule(cmd_parms *cmd, void *in_dconf,
0de8c9
     rewrite_server_conf *sconf;
0de8c9
     rewriterule_entry *newrule;
0de8c9
     ap_regex_t *regexp;
0de8c9
-    char *a1 = NULL, *a2 = NULL, *a3 = NULL;
0de8c9
+    char *a1 = NULL, *a2 = NULL, *a2_end, *a3 = NULL;
0de8c9
     const char *err;
0de8c9
 
0de8c9
     sconf = ap_get_module_config(cmd->server->module_config, &rewrite_module);
0de8c9
@@ -3793,7 +3804,7 @@ static const char *cmd_rewriterule(cmd_parms *cmd, void *in_dconf,
0de8c9
     }
0de8c9
 
0de8c9
     /*  parse the argument line ourself */
0de8c9
-    if (parseargline(str, &a1, &a2, &a3)) {
0de8c9
+    if (parseargline(str, &a1, &a2, &a2_end, &a3)) {
0de8c9
         return apr_pstrcat(cmd->pool, "RewriteRule: bad argument line '", str,
0de8c9
                            "'", NULL);
0de8c9
     }
0de8c9
@@ -3840,6 +3851,16 @@ static const char *cmd_rewriterule(cmd_parms *cmd, void *in_dconf,
0de8c9
         newrule->flags |= RULEFLAG_NOSUB;
0de8c9
     }
0de8c9
 
0de8c9
+    if (*(a2_end-1) == '?') {
0de8c9
+        /* a literal ? at the end of the unsubstituted rewrite rule */
0de8c9
+        newrule->flags |= RULEFLAG_QSNONE;
0de8c9
+    }
0de8c9
+    else if (newrule->flags & RULEFLAG_QSDISCARD) {
0de8c9
+        if (NULL == ap_strchr(newrule->output, '?')) {
0de8c9
+            newrule->flags |= RULEFLAG_QSNONE;
0de8c9
+        }
0de8c9
+    }
0de8c9
+
0de8c9
     /* now, if the server or per-dir config holds an
0de8c9
      * array of RewriteCond entries, we take it for us
0de8c9
      * and clear the array
0de8c9
@@ -4245,9 +4266,7 @@ static int apply_rewrite_rule(rewriterule_entry *p, rewrite_ctx *ctx)
0de8c9
         r->path_info = NULL;
0de8c9
     }
0de8c9
 
0de8c9
-    splitout_queryargs(r, p->flags & RULEFLAG_QSAPPEND, 
0de8c9
-                          p->flags & RULEFLAG_QSDISCARD, 
0de8c9
-                          p->flags & RULEFLAG_QSLAST);
0de8c9
+    splitout_queryargs(r, p->flags);
0de8c9
 
0de8c9
     /* Add the previously stripped per-directory location prefix, unless
0de8c9
      * (1) it's an absolute URL path and
0de8c9
@@ -4729,6 +4748,17 @@ static int hook_uri2file(request_rec *r)
0de8c9
         unsigned skip;
0de8c9
         apr_size_t flen;
0de8c9
 
0de8c9
+        if (r->args && *(ap_scan_vchar_obstext(r->args))) {
0de8c9
+            /*
0de8c9
+             * We have a raw control character or a ' ' in r->args.
0de8c9
+             * Correct encoding was missed.
0de8c9
+             */
0de8c9
+            ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(10410)
0de8c9
+                          "Rewritten query string contains control "
0de8c9
+                          "characters or spaces");
0de8c9
+            return HTTP_FORBIDDEN;
0de8c9
+        }
0de8c9
+
0de8c9
         if (ACTION_STATUS == rulestatus) {
0de8c9
             int n = r->status;
0de8c9
 
0de8c9
@@ -5013,6 +5043,17 @@ static int hook_fixup(request_rec *r)
0de8c9
     if (rulestatus) {
0de8c9
         unsigned skip;
0de8c9
 
0de8c9
+        if (r->args && *(ap_scan_vchar_obstext(r->args))) {
0de8c9
+            /*
0de8c9
+             * We have a raw control character or a ' ' in r->args.
0de8c9
+             * Correct encoding was missed.
0de8c9
+             */
0de8c9
+            ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(10411)
0de8c9
+                          "Rewritten query string contains control "
0de8c9
+                          "characters or spaces");
0de8c9
+            return HTTP_FORBIDDEN;
0de8c9
+        }
0de8c9
+
0de8c9
         if (ACTION_STATUS == rulestatus) {
0de8c9
             int n = r->status;
0de8c9
 
0de8c9
diff --git a/modules/proxy/mod_proxy.c b/modules/proxy/mod_proxy.c
0de8c9
index 5759513..d64739b 100644
0de8c9
--- a/modules/proxy/mod_proxy.c
0de8c9
+++ b/modules/proxy/mod_proxy.c
0de8c9
@@ -960,6 +960,8 @@ PROXY_DECLARE(int) ap_proxy_trans_match(request_rec *r, struct proxy_alias *ent,
0de8c9
     }
0de8c9
 
0de8c9
     if (found) {
0de8c9
+        unsigned int encoded = ent->flags & PROXYPASS_MAP_ENCODED;
0de8c9
+
0de8c9
         /* A proxy module is assigned this URL, check whether it's interested
0de8c9
          * in the request itself (e.g. proxy_wstunnel cares about Upgrade
0de8c9
          * requests only, and could hand over to proxy_http otherwise).
0de8c9
@@ -979,6 +981,9 @@ PROXY_DECLARE(int) ap_proxy_trans_match(request_rec *r, struct proxy_alias *ent,
0de8c9
         if (ent->flags & PROXYPASS_NOQUERY) {
0de8c9
             apr_table_setn(r->notes, "proxy-noquery", "1");
0de8c9
         }
0de8c9
+        if (encoded) {
0de8c9
+            apr_table_setn(r->notes, "proxy-noencode", "1");
0de8c9
+        }
0de8c9
 
0de8c9
         if (servlet_uri) {
0de8c9
             ap_log_rerror(APLOG_MARK, APLOG_TRACE1, 0, r, APLOGNO(10248)
0de8c9
@@ -992,13 +997,13 @@ PROXY_DECLARE(int) ap_proxy_trans_match(request_rec *r, struct proxy_alias *ent,
0de8c9
              */
0de8c9
             AP_DEBUG_ASSERT(strlen(r->uri) >= strlen(servlet_uri));
0de8c9
             strcpy(r->uri, servlet_uri);
0de8c9
-            return DONE;
0de8c9
         }
0de8c9
-
0de8c9
-        ap_log_rerror(APLOG_MARK, APLOG_TRACE1, 0, r, APLOGNO(03464)
0de8c9
-                      "URI path '%s' matches proxy handler '%s'", r->uri,
0de8c9
-                      found);
0de8c9
-        return OK;
0de8c9
+        else {
0de8c9
+            ap_log_rerror(APLOG_MARK, APLOG_TRACE1, 0, r, APLOGNO(03464)
0de8c9
+                          "URI path '%s' matches proxy handler '%s'", r->uri,
0de8c9
+                          found);
0de8c9
+        }
0de8c9
+        return (encoded) ? DONE : OK;
0de8c9
     }
0de8c9
 
0de8c9
     return HTTP_CONTINUE;
0de8c9
diff --git a/modules/proxy/mod_proxy_ajp.c b/modules/proxy/mod_proxy_ajp.c
0de8c9
index d34fc57..1978425 100644
0de8c9
--- a/modules/proxy/mod_proxy_ajp.c
0de8c9
+++ b/modules/proxy/mod_proxy_ajp.c
0de8c9
@@ -65,11 +65,25 @@ static int proxy_ajp_canon(request_rec *r, char *url)
0de8c9
     if (apr_table_get(r->notes, "proxy-nocanon")) {
0de8c9
         path = url;   /* this is the raw path */
0de8c9
     }
0de8c9
+    else if (apr_table_get(r->notes, "proxy-noencode")) {
0de8c9
+        path = url;   /* this is the encoded path already */
0de8c9
+        search = r->args;
0de8c9
+    }
0de8c9
     else {
0de8c9
         path = ap_proxy_canonenc(r->pool, url, strlen(url), enc_path, 0,
0de8c9
                                  r->proxyreq);
0de8c9
         search = r->args;
0de8c9
     }
0de8c9
+    if (search && *ap_scan_vchar_obstext(search)) {
0de8c9
+        /*
0de8c9
+         * We have a raw control character or a ' ' in r->args.
0de8c9
+         * Correct encoding was missed.
0de8c9
+         */
0de8c9
+         ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(10406)
0de8c9
+                       "To be forwarded query string contains control "
0de8c9
+                       "characters or spaces");
0de8c9
+         return HTTP_FORBIDDEN;
0de8c9
+    }
0de8c9
     if (path == NULL)
0de8c9
         return HTTP_BAD_REQUEST;
0de8c9
 
0de8c9
diff --git a/modules/proxy/mod_proxy_balancer.c b/modules/proxy/mod_proxy_balancer.c
0de8c9
index 3304c93..f1a3c62 100644
0de8c9
--- a/modules/proxy/mod_proxy_balancer.c
0de8c9
+++ b/modules/proxy/mod_proxy_balancer.c
0de8c9
@@ -102,11 +102,25 @@ static int proxy_balancer_canon(request_rec *r, char *url)
0de8c9
     if (apr_table_get(r->notes, "proxy-nocanon")) {
0de8c9
         path = url;   /* this is the raw path */
0de8c9
     }
0de8c9
+    else if (apr_table_get(r->notes, "proxy-noencode")) {
0de8c9
+        path = url;   /* this is the encoded path already */
0de8c9
+        search = r->args;
0de8c9
+    }
0de8c9
     else {
0de8c9
         path = ap_proxy_canonenc(r->pool, url, strlen(url), enc_path, 0,
0de8c9
                                  r->proxyreq);
0de8c9
         search = r->args;
0de8c9
     }
0de8c9
+    if (search && *ap_scan_vchar_obstext(search)) {
0de8c9
+        /*
0de8c9
+         * We have a raw control character or a ' ' in r->args.
0de8c9
+         * Correct encoding was missed.
0de8c9
+         */
0de8c9
+         ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(10407)
0de8c9
+                       "To be forwarded query string contains control "
0de8c9
+                       "characters or spaces");
0de8c9
+         return HTTP_FORBIDDEN;
0de8c9
+    }
0de8c9
     if (path == NULL)
0de8c9
         return HTTP_BAD_REQUEST;
0de8c9
 
0de8c9
diff --git a/modules/proxy/mod_proxy_fcgi.c b/modules/proxy/mod_proxy_fcgi.c
0de8c9
index 3382b9b..a89b9a9 100644
0de8c9
--- a/modules/proxy/mod_proxy_fcgi.c
0de8c9
+++ b/modules/proxy/mod_proxy_fcgi.c
0de8c9
@@ -92,8 +92,9 @@ static int proxy_fcgi_canon(request_rec *r, char *url)
0de8c9
         host = apr_pstrcat(r->pool, "[", host, "]", NULL);
0de8c9
     }
0de8c9
 
0de8c9
-    if (apr_table_get(r->notes, "proxy-nocanon")) {
0de8c9
-        path = url;   /* this is the raw path */
0de8c9
+    if (apr_table_get(r->notes, "proxy-nocanon")
0de8c9
+        || apr_table_get(r->notes, "proxy-noencode")) {
0de8c9
+        path = url;   /* this is the raw/encoded path */
0de8c9
     }
0de8c9
     else {
0de8c9
         path = ap_proxy_canonenc(r->pool, url, strlen(url), enc_path, 0,
0de8c9
diff --git a/modules/proxy/mod_proxy_http.c b/modules/proxy/mod_proxy_http.c
0de8c9
index 0392ac7..c4d7db0 100644
0de8c9
--- a/modules/proxy/mod_proxy_http.c
0de8c9
+++ b/modules/proxy/mod_proxy_http.c
0de8c9
@@ -121,11 +121,25 @@ static int proxy_http_canon(request_rec *r, char *url)
0de8c9
         if (apr_table_get(r->notes, "proxy-nocanon")) {
0de8c9
             path = url;   /* this is the raw path */
0de8c9
         }
0de8c9
+        else if (apr_table_get(r->notes, "proxy-noencode")) {
0de8c9
+            path = url;   /* this is the encoded path already */
0de8c9
+            search = r->args;
0de8c9
+        }
0de8c9
         else {
0de8c9
             path = ap_proxy_canonenc(r->pool, url, strlen(url),
0de8c9
                                      enc_path, 0, r->proxyreq);
0de8c9
             search = r->args;
0de8c9
         }
0de8c9
+        if (search && *ap_scan_vchar_obstext(search)) {
0de8c9
+            /*
0de8c9
+             * We have a raw control character or a ' ' in r->args.
0de8c9
+             * Correct encoding was missed.
0de8c9
+             */
0de8c9
+            ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(10408)
0de8c9
+                          "To be forwarded query string contains control "
0de8c9
+                          "characters or spaces");
0de8c9
+            return HTTP_FORBIDDEN;
0de8c9
+        }
0de8c9
         break;
0de8c9
     case PROXYREQ_PROXY:
0de8c9
         path = url;
0de8c9
diff --git a/modules/proxy/mod_proxy_uwsgi.c b/modules/proxy/mod_proxy_uwsgi.c
0de8c9
index e02450e..1b23904 100644
0de8c9
--- a/modules/proxy/mod_proxy_uwsgi.c
0de8c9
+++ b/modules/proxy/mod_proxy_uwsgi.c
0de8c9
@@ -84,8 +84,14 @@ static int uwsgi_canon(request_rec *r, char *url)
0de8c9
         host = apr_pstrcat(r->pool, "[", host, "]", NULL);
0de8c9
     }
0de8c9
 
0de8c9
-    path = ap_proxy_canonenc(r->pool, url, strlen(url), enc_path, 0,
0de8c9
-                             r->proxyreq);
0de8c9
+    if (apr_table_get(r->notes, "proxy-nocanon")
0de8c9
+        || apr_table_get(r->notes, "proxy-noencode")) {
0de8c9
+        path = url;   /* this is the raw/encoded path */
0de8c9
+    }
0de8c9
+    else {
0de8c9
+        path = ap_proxy_canonenc(r->pool, url, strlen(url), enc_path, 0,
0de8c9
+                                 r->proxyreq);
0de8c9
+    }
0de8c9
     if (!path) {
0de8c9
         return HTTP_BAD_REQUEST;
0de8c9
     }
0de8c9
diff --git a/modules/proxy/mod_proxy_wstunnel.c b/modules/proxy/mod_proxy_wstunnel.c
0de8c9
index c29ded1..3a68b85 100644
0de8c9
--- a/modules/proxy/mod_proxy_wstunnel.c
0de8c9
+++ b/modules/proxy/mod_proxy_wstunnel.c
0de8c9
@@ -111,11 +111,25 @@ static int proxy_wstunnel_canon(request_rec *r, char *url)
0de8c9
     if (apr_table_get(r->notes, "proxy-nocanon")) {
0de8c9
         path = url;   /* this is the raw path */
0de8c9
     }
0de8c9
+    else if (apr_table_get(r->notes, "proxy-noencode")) {
0de8c9
+        path = url;   /* this is the encoded path already */
0de8c9
+        search = r->args;
0de8c9
+    }
0de8c9
     else {
0de8c9
         path = ap_proxy_canonenc(r->pool, url, strlen(url), enc_path, 0,
0de8c9
                                  r->proxyreq);
0de8c9
         search = r->args;
0de8c9
     }
0de8c9
+    if (search && *ap_scan_vchar_obstext(search)) {
0de8c9
+        /*
0de8c9
+         * We have a raw control character or a ' ' in r->args.
0de8c9
+         * Correct encoding was missed.
0de8c9
+         */
0de8c9
+        ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(10409)
0de8c9
+                      "To be forwarded query string contains control "
0de8c9
+                      "characters or spaces");
0de8c9
+        return HTTP_FORBIDDEN;
0de8c9
+    }
0de8c9
     if (path == NULL)
0de8c9
         return HTTP_BAD_REQUEST;
0de8c9