6a034d
diff --git a/docs/manual/rewrite/flags.html.en b/docs/manual/rewrite/flags.html.en
6a034d
index 80d0759..9673094 100644
6a034d
--- a/docs/manual/rewrite/flags.html.en
6a034d
+++ b/docs/manual/rewrite/flags.html.en
6a034d
@@ -85,10 +85,6 @@ of how you might use them.

6a034d
 

B (escape backreferences)

6a034d
 

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

6a034d
 characters before applying the transformation.

6a034d
-

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

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

6a034d
 
6a034d
 

mod_rewrite has to unescape URLs before mapping them,

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

6a034d
 
6a034d
 

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

6a034d
 strings in the encoded form.

6a034d
+
6a034d
+

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

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

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

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

6a034d
diff --git a/modules/mappers/mod_rewrite.c b/modules/mappers/mod_rewrite.c
6a034d
index 38dbb24..c3937ad 100644
6a034d
--- a/modules/mappers/mod_rewrite.c
6a034d
+++ b/modules/mappers/mod_rewrite.c
6a034d
@@ -168,6 +168,7 @@ static const char* really_last_key = "rewrite_really_last";
6a034d
 #define RULEFLAG_END                (1<<17)
6a034d
 #define RULEFLAG_ESCAPENOPLUS       (1<<18)
6a034d
 #define RULEFLAG_QSLAST             (1<<19)
6a034d
+#define RULEFLAG_QSNONE             (1<<20) /* programattic only */
6a034d
 
6a034d
 /* return code of the rewrite rule
6a034d
  * the result may be escaped - or not
6a034d
@@ -761,15 +762,24 @@ static char *escape_absolute_uri(apr_pool_t *p, char *uri, unsigned scheme)
6a034d
                        ap_escape_uri(p, cp), NULL);
6a034d
 }
6a034d
 
6a034d
+
6a034d
 /*
6a034d
  * split out a QUERY_STRING part from
6a034d
  * the current URI string
6a034d
  */
6a034d
-static void splitout_queryargs(request_rec *r, int qsappend, int qsdiscard, 
6a034d
-                               int qslast)
6a034d
+static void splitout_queryargs(request_rec *r, int flags)
6a034d
 {
6a034d
     char *q;
6a034d
     int split;
6a034d
+    int qsappend = flags & RULEFLAG_QSAPPEND;
6a034d
+    int qsdiscard = flags & RULEFLAG_QSDISCARD;
6a034d
+    int qslast = flags & RULEFLAG_QSLAST;
6a034d
+
6a034d
+    if (flags & RULEFLAG_QSNONE) {
6a034d
+        rewritelog((r, 2, NULL, "discarding query string, no parse from substitution"));
6a034d
+        r->args = NULL;
6a034d
+        return;
6a034d
+    }
6a034d
 
6a034d
     /* don't touch, unless it's a scheme for which a query string makes sense.
6a034d
      * See RFC 1738 and RFC 2368.
6a034d
@@ -794,7 +804,7 @@ static void splitout_queryargs(request_rec *r, int qsappend, int qsdiscard,
6a034d
         olduri = apr_pstrdup(r->pool, r->filename);
6a034d
         *q++ = '\0';
6a034d
         if (qsappend) {
6a034d
-            if (*q) { 
6a034d
+            if (*q) {
6a034d
                 r->args = apr_pstrcat(r->pool, q, "&" , r->args, NULL);
6a034d
             }
6a034d
         }
6a034d
@@ -802,9 +812,9 @@ static void splitout_queryargs(request_rec *r, int qsappend, int qsdiscard,
6a034d
             r->args = apr_pstrdup(r->pool, q);
6a034d
         }
6a034d
 
6a034d
-        if (r->args) { 
6a034d
+        if (r->args) {
6a034d
            len = strlen(r->args);
6a034d
-      
6a034d
+
6a034d
            if (!len) {
6a034d
                r->args = NULL;
6a034d
            }
6a034d
@@ -2733,7 +2743,7 @@ static apr_status_t rewritelock_remove(void *data)
6a034d
  * XXX: what an inclined parser. Seems we have to leave it so
6a034d
  *      for backwards compat. *sigh*
6a034d
  */
6a034d
-static int parseargline(char *str, char **a1, char **a2, char **a3)
6a034d
+static int parseargline(char *str, char **a1, char **a2, char **a2_end, char **a3)
6a034d
 {
6a034d
     char quote;
6a034d
 
6a034d
@@ -2784,8 +2794,10 @@ static int parseargline(char *str, char **a1, char **a2, char **a3)
6a034d
 
6a034d
     if (!*str) {
6a034d
         *a3 = NULL; /* 3rd argument is optional */
6a034d
+        *a2_end = str;
6a034d
         return 0;
6a034d
     }
6a034d
+    *a2_end = str;
6a034d
     *str++ = '\0';
6a034d
 
6a034d
     while (apr_isspace(*str)) {
6a034d
@@ -3323,7 +3335,7 @@ static const char *cmd_rewritecond(cmd_parms *cmd, void *in_dconf,
6a034d
     rewrite_server_conf *sconf;
6a034d
     rewritecond_entry *newcond;
6a034d
     ap_regex_t *regexp;
6a034d
-    char *a1 = NULL, *a2 = NULL, *a3 = NULL;
6a034d
+    char *a1 = NULL, *a2 = NULL, *a2_end, *a3 = NULL;
6a034d
     const char *err;
6a034d
 
6a034d
     sconf = ap_get_module_config(cmd->server->module_config, &rewrite_module);
6a034d
@@ -3341,7 +3353,7 @@ static const char *cmd_rewritecond(cmd_parms *cmd, void *in_dconf,
6a034d
      * of the argument line. So we can use a1 .. a3 without
6a034d
      * copying them again.
6a034d
      */
6a034d
-    if (parseargline(str, &a1, &a2, &a3)) {
6a034d
+    if (parseargline(str, &a1, &a2, &a2_end, &a3)) {
6a034d
         return apr_pstrcat(cmd->pool, "RewriteCond: bad argument line '", str,
6a034d
                            "'", NULL);
6a034d
     }
6a034d
@@ -3749,7 +3761,7 @@ static const char *cmd_rewriterule(cmd_parms *cmd, void *in_dconf,
6a034d
     rewrite_server_conf *sconf;
6a034d
     rewriterule_entry *newrule;
6a034d
     ap_regex_t *regexp;
6a034d
-    char *a1 = NULL, *a2 = NULL, *a3 = NULL;
6a034d
+    char *a1 = NULL, *a2 = NULL, *a2_end, *a3 = NULL;
6a034d
     const char *err;
6a034d
 
6a034d
     sconf = ap_get_module_config(cmd->server->module_config, &rewrite_module);
6a034d
@@ -3763,7 +3775,7 @@ static const char *cmd_rewriterule(cmd_parms *cmd, void *in_dconf,
6a034d
     }
6a034d
 
6a034d
     /*  parse the argument line ourself */
6a034d
-    if (parseargline(str, &a1, &a2, &a3)) {
6a034d
+    if (parseargline(str, &a1, &a2, &a2_end, &a3)) {
6a034d
         return apr_pstrcat(cmd->pool, "RewriteRule: bad argument line '", str,
6a034d
                            "'", NULL);
6a034d
     }
6a034d
@@ -3810,6 +3822,16 @@ static const char *cmd_rewriterule(cmd_parms *cmd, void *in_dconf,
6a034d
         newrule->flags |= RULEFLAG_NOSUB;
6a034d
     }
6a034d
 
6a034d
+    if (*(a2_end-1) == '?') {
6a034d
+        /* a literal ? at the end of the unsubstituted rewrite rule */
6a034d
+        newrule->flags |= RULEFLAG_QSNONE;
6a034d
+    }
6a034d
+    else if (newrule->flags & RULEFLAG_QSDISCARD) {
6a034d
+        if (NULL == ap_strchr(newrule->output, '?')) {
6a034d
+            newrule->flags |= RULEFLAG_QSNONE;
6a034d
+        }
6a034d
+    }
6a034d
+
6a034d
     /* now, if the server or per-dir config holds an
6a034d
      * array of RewriteCond entries, we take it for us
6a034d
      * and clear the array
6a034d
@@ -4215,9 +4237,7 @@ static int apply_rewrite_rule(rewriterule_entry *p, rewrite_ctx *ctx)
6a034d
         r->path_info = NULL;
6a034d
     }
6a034d
 
6a034d
-    splitout_queryargs(r, p->flags & RULEFLAG_QSAPPEND, 
6a034d
-                          p->flags & RULEFLAG_QSDISCARD, 
6a034d
-                          p->flags & RULEFLAG_QSLAST);
6a034d
+    splitout_queryargs(r, p->flags);
6a034d
 
6a034d
     /* Add the previously stripped per-directory location prefix, unless
6a034d
      * (1) it's an absolute URL path and
6a034d
@@ -4699,6 +4719,17 @@ static int hook_uri2file(request_rec *r)
6a034d
         unsigned skip;
6a034d
         apr_size_t flen;
6a034d
 
6a034d
+        if (r->args && *(ap_scan_vchar_obstext(r->args))) {
6a034d
+            /*
6a034d
+             * We have a raw control character or a ' ' in r->args.
6a034d
+             * Correct encoding was missed.
6a034d
+             */
6a034d
+            ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(10410)
6a034d
+                          "Rewritten query string contains control "
6a034d
+                          "characters or spaces");
6a034d
+            return HTTP_FORBIDDEN;
6a034d
+        }
6a034d
+
6a034d
         if (ACTION_STATUS == rulestatus) {
6a034d
             int n = r->status;
6a034d
 
6a034d
@@ -4983,6 +5014,17 @@ static int hook_fixup(request_rec *r)
6a034d
     if (rulestatus) {
6a034d
         unsigned skip;
6a034d
 
6a034d
+        if (r->args && *(ap_scan_vchar_obstext(r->args))) {
6a034d
+            /*
6a034d
+             * We have a raw control character or a ' ' in r->args.
6a034d
+             * Correct encoding was missed.
6a034d
+             */
6a034d
+            ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(10411)
6a034d
+                          "Rewritten query string contains control "
6a034d
+                          "characters or spaces");
6a034d
+            return HTTP_FORBIDDEN;
6a034d
+        }
6a034d
+
6a034d
         if (ACTION_STATUS == rulestatus) {
6a034d
             int n = r->status;
6a034d
 
6a034d
diff --git a/modules/proxy/mod_proxy_ajp.c b/modules/proxy/mod_proxy_ajp.c
6a034d
index 058b03f..a529c02 100644
6a034d
--- a/modules/proxy/mod_proxy_ajp.c
6a034d
+++ b/modules/proxy/mod_proxy_ajp.c
6a034d
@@ -69,6 +69,16 @@ static int proxy_ajp_canon(request_rec *r, char *url)
6a034d
         path = ap_proxy_canonenc(r->pool, url, strlen(url), enc_path, 0,
6a034d
                                  r->proxyreq);
6a034d
         search = r->args;
6a034d
+        if (search && *(ap_scan_vchar_obstext(search))) {
6a034d
+            /*
6a034d
+             * We have a raw control character or a ' ' in r->args.
6a034d
+             * Correct encoding was missed.
6a034d
+             */
6a034d
+             ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(10406)
6a034d
+                           "To be forwarded query string contains control "
6a034d
+                           "characters or spaces");
6a034d
+             return HTTP_FORBIDDEN;
6a034d
+        }
6a034d
     }
6a034d
     if (path == NULL)
6a034d
         return HTTP_BAD_REQUEST;
6a034d
diff --git a/modules/proxy/mod_proxy_balancer.c b/modules/proxy/mod_proxy_balancer.c
6a034d
index 3a28038..c599e1a 100644
6a034d
--- a/modules/proxy/mod_proxy_balancer.c
6a034d
+++ b/modules/proxy/mod_proxy_balancer.c
6a034d
@@ -106,6 +106,16 @@ static int proxy_balancer_canon(request_rec *r, char *url)
6a034d
         path = ap_proxy_canonenc(r->pool, url, strlen(url), enc_path, 0,
6a034d
                                  r->proxyreq);
6a034d
         search = r->args;
6a034d
+        if (search && *(ap_scan_vchar_obstext(search))) {
6a034d
+            /*
6a034d
+             * We have a raw control character or a ' ' in r->args.
6a034d
+             * Correct encoding was missed.
6a034d
+             */
6a034d
+             ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(10407)
6a034d
+                           "To be forwarded query string contains control "
6a034d
+                           "characters or spaces");
6a034d
+             return HTTP_FORBIDDEN;
6a034d
+        }
6a034d
     }
6a034d
     if (path == NULL)
6a034d
         return HTTP_BAD_REQUEST;
6a034d
diff --git a/modules/proxy/mod_proxy_http.c b/modules/proxy/mod_proxy_http.c
6a034d
index c1c591a..58a8c86 100644
6a034d
--- a/modules/proxy/mod_proxy_http.c
6a034d
+++ b/modules/proxy/mod_proxy_http.c
6a034d
@@ -90,6 +90,16 @@ static int proxy_http_canon(request_rec *r, char *url)
6a034d
             path = ap_proxy_canonenc(r->pool, url, strlen(url),
6a034d
                                      enc_path, 0, r->proxyreq);
6a034d
             search = r->args;
6a034d
+            if (search && *(ap_scan_vchar_obstext(search))) {
6a034d
+                /*
6a034d
+                 * We have a raw control character or a ' ' in r->args.
6a034d
+                 * Correct encoding was missed.
6a034d
+                 */
6a034d
+                ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(10408)
6a034d
+                              "To be forwarded query string contains control "
6a034d
+                              "characters or spaces");
6a034d
+                return HTTP_FORBIDDEN;
6a034d
+            }
6a034d
         }
6a034d
         break;
6a034d
     case PROXYREQ_PROXY:
6a034d
diff --git a/modules/proxy/mod_proxy_wstunnel.c b/modules/proxy/mod_proxy_wstunnel.c
6a034d
index e005a94..f5e27d9 100644
6a034d
--- a/modules/proxy/mod_proxy_wstunnel.c
6a034d
+++ b/modules/proxy/mod_proxy_wstunnel.c
6a034d
@@ -77,6 +77,16 @@ static int proxy_wstunnel_canon(request_rec *r, char *url)
6a034d
         path = ap_proxy_canonenc(r->pool, url, strlen(url), enc_path, 0,
6a034d
                                  r->proxyreq);
6a034d
         search = r->args;
6a034d
+        if (search && *(ap_scan_vchar_obstext(search))) {
6a034d
+            /*
6a034d
+             * We have a raw control character or a ' ' in r->args.
6a034d
+             * Correct encoding was missed.
6a034d
+             */
6a034d
+            ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(10409)
6a034d
+                          "To be forwarded query string contains control "
6a034d
+                          "characters or spaces");
6a034d
+            return HTTP_FORBIDDEN;
6a034d
+        }
6a034d
     }
6a034d
     if (path == NULL)
6a034d
         return HTTP_BAD_REQUEST;