]> git.ipfire.org Git - thirdparty/apache/httpd.git/commitdiff
factor out IS_SLASH, perdir fix
authorEric Covener <covener@apache.org>
Wed, 26 Jun 2024 10:09:29 +0000 (10:09 +0000)
committerEric Covener <covener@apache.org>
Wed, 26 Jun 2024 10:09:29 +0000 (10:09 +0000)
in per-dir, the filename will be internally redirected, so / is OK too.

git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/trunk@1918651 13f79535-47bb-0310-9956-ffa450edef68

include/ap_mmn.h
include/httpd.h
modules/mappers/mod_rewrite.c
server/util.c

index 4e942ada1ded0daa80502d3fc03ba83719f559ad..ad4b77bd33a01f4c93251a96773939e71804ef04 100644 (file)
  * 20211221.23 (2.5.1-dev) Add ap_set_content_type_ex(), ap_filepath_merge(),
  *                         and AP_REQUEST_TRUSTED_CT BNOTE.
  * 20211221.24 (2.5.1-dev) Add ap_proxy_fixup_uds_filename()
+ * 20211221.25 (2.5.1-dev) AP_SLASHES and AP_IS_SLASH
  */
 
 #define MODULE_MAGIC_COOKIE 0x41503235UL /* "AP25" */
 #ifndef MODULE_MAGIC_NUMBER_MAJOR
 #define MODULE_MAGIC_NUMBER_MAJOR 20211221
 #endif
-#define MODULE_MAGIC_NUMBER_MINOR 24             /* 0...n */
+#define MODULE_MAGIC_NUMBER_MINOR 25             /* 0...n */
 
 /**
  * Determine if the server's current MODULE_MAGIC_NUMBER is at least a
index 02ad3238937d3026f18b515d6a44cf844c079269..c02b3b7849bb2f43a14782ef39e4f28eac658348 100644 (file)
@@ -2879,6 +2879,17 @@ AP_DECLARE(apr_status_t) ap_filepath_merge(char **newpath,
 #define apr_filepath_merge  ap_filepath_merge
 #endif
 
+/* Win32/NetWare/OS2 need to check for both forward and back slashes
+ * in ap_normalize_path() and ap_escape_url().
+ */
+#ifdef CASE_BLIND_FILESYSTEM
+#define AP_IS_SLASH(s) ((s == '/') || (s == '\\'))
+#define AP_SLASHES "/\\"
+#else
+#define AP_IS_SLASH(s) (s == '/')
+#define AP_SLASHES "/"
+#endif
+
 #ifdef __cplusplus
 }
 #endif
index 0d4ecbc9dd3ee4e5c0d8d453753a9e6ee1eafffb..2e7d18cc6052703f1c514446bae4b5e3c72d3135 100644 (file)
@@ -688,11 +688,8 @@ static int is_absolute_path(const char *path)
 #ifndef WIN32
     return (path[0] == '/');
 #else
-#define IS_SLASH(c) ((c) == '/' || (c) == '\\')
-    /* "//", "\\", "x:/" and "x:\" are absolute paths on Windows */
-    return ((IS_SLASH(path[0]) && path[1] == path[0])
-            || (apr_isalpha(path[0]) && path[1] == ':' && IS_SLASH(path[2])));
-#undef IS_SLASH
+    return ((AP_IS_SLASH(path[0]) && path[1] == path[0])
+            || (apr_isalpha(path[0]) && path[1] == ':' && AP_IS_SLASH(path[2])));
 #endif
 }
 
@@ -4413,10 +4410,15 @@ static rule_return_type apply_rewrite_rule(rewriterule_entry *p,
         && !is_absolute_path(newuri)
         && !is_absolute_uri(newuri, NULL)) {
         if (ctx->perdir) {
-            rewritelog(r, 3, ctx->perdir, "add per-dir prefix: %s -> %s%s",
-                       newuri, ctx->perdir, newuri);
+            if (!AP_IS_SLASH(*newuri)) {
+                /* perdir, the newuri will be internally redirected, so
+                 * leading slash is enough even if it's an ambiguous fs path
+                 */
+                rewritelog(r, 3, ctx->perdir, "add per-dir prefix: %s -> %s%s",
+                           newuri, ctx->perdir, newuri);
 
-            newuri = apr_pstrcat(r->pool, ctx->perdir, newuri, NULL);
+                newuri = apr_pstrcat(r->pool, ctx->perdir, newuri, NULL);
+            }
         }
         else if (!(p->flags & (RULEFLAG_PROXY | RULEFLAG_FORCEREDIRECT))) {
             /* Not an absolute URI-path and the scheme (if any) is unknown,
index 04f40a235a277e12533442b4235b6c854da8be0c..bca2636fd2ba3aea259c657b05903eec6afee4f1 100644 (file)
  */
 #include "test_char.h"
 
-/* Win32/NetWare/OS2 need to check for both forward and back slashes
- * in ap_normalize_path() and ap_escape_url().
- */
-#ifdef CASE_BLIND_FILESYSTEM
-#define IS_SLASH(s) ((s == '/') || (s == '\\'))
-#define SLASHES "/\\"
-#else
-#define IS_SLASH(s) (s == '/')
-#define SLASHES "/"
-#endif
-
 /* we know core's module_index is 0 */
 #undef APLOG_MODULE_INDEX
 #define APLOG_MODULE_INDEX AP_CORE_MODULE_INDEX
@@ -494,7 +483,7 @@ AP_DECLARE(apr_status_t) ap_pregsub_ex(apr_pool_t *p, char **result,
 /* Forward declare */
 static char x2c(const char *what);
 
-#define IS_SLASH_OR_NUL(s) (s == '\0' || IS_SLASH(s))
+#define IS_SLASH_OR_NUL(s) (s == '\0' || AP_IS_SLASH(s))
 
 /*
  * Inspired by mod_jk's jk_servlet_normalize().
@@ -506,7 +495,7 @@ AP_DECLARE(int) ap_normalize_path(char *path, unsigned int flags)
     int decode_unreserved = (flags & AP_NORMALIZE_DECODE_UNRESERVED) != 0;
     int merge_slashes = (flags & AP_NORMALIZE_MERGE_SLASHES) != 0;
 
-    if (!IS_SLASH(path[0])) {
+    if (!AP_IS_SLASH(path[0])) {
         /* Besides "OPTIONS *", a request-target should start with '/'
          * per RFC 7230 section 5.3, so anything else is invalid.
          */
@@ -547,12 +536,12 @@ AP_DECLARE(int) ap_normalize_path(char *path, unsigned int flags)
             }
         }
 
-        if (w == 0 || IS_SLASH(path[w - 1])) {
+        if (w == 0 || AP_IS_SLASH(path[w - 1])) {
             /* Collapse ///// sequences to / */
-            if (merge_slashes && IS_SLASH(path[l])) {
+            if (merge_slashes && AP_IS_SLASH(path[l])) {
                 do {
                     l++;
-                } while (IS_SLASH(path[l]));
+                } while (AP_IS_SLASH(path[l]));
                 continue;
             }
 
@@ -581,7 +570,7 @@ AP_DECLARE(int) ap_normalize_path(char *path, unsigned int flags)
                     if (w > 1) {
                         do {
                             w--;
-                        } while (w && !IS_SLASH(path[w - 1]));
+                        } while (w && !AP_IS_SLASH(path[w - 1]));
                     }
                     else {
                         /* Already at root, ignore and return a failure
@@ -1921,7 +1910,7 @@ static int unescape_url(char *url, const char *forbid, const char *reserved,
                 char decoded;
                 decoded = x2c(y + 1);
                 if ((decoded == '\0')
-                    || (forbid_slashes && IS_SLASH(decoded))
+                    || (forbid_slashes && AP_IS_SLASH(decoded))
                     || (forbid && ap_strchr_c(forbid, decoded))) {
                     badpath = 1;
                     *x = decoded;
@@ -1929,7 +1918,7 @@ static int unescape_url(char *url, const char *forbid, const char *reserved,
                 }
                 else if ((keep_unreserved && TEST_CHAR(decoded,
                                                        T_URI_UNRESERVED))
-                         || (keep_slashes && IS_SLASH(decoded))
+                         || (keep_slashes && AP_IS_SLASH(decoded))
                          || (reserved && ap_strchr_c(reserved, decoded))) {
                     *x++ = *y++;
                     *x++ = *y++;
@@ -1956,7 +1945,7 @@ static int unescape_url(char *url, const char *forbid, const char *reserved,
 AP_DECLARE(int) ap_unescape_url(char *url)
 {
     /* Traditional */
-    return unescape_url(url, SLASHES, NULL, 0);
+    return unescape_url(url, AP_SLASHES, NULL, 0);
 }
 AP_DECLARE(int) ap_unescape_url_keep2f(char *url, int decode_slashes)
 {
@@ -1966,7 +1955,7 @@ AP_DECLARE(int) ap_unescape_url_keep2f(char *url, int decode_slashes)
         return unescape_url(url, NULL, NULL, 0);
     } else {
         /* reserve (do not decode) encoded slashes */
-        return unescape_url(url, NULL, SLASHES, 0);
+        return unescape_url(url, NULL, AP_SLASHES, 0);
     }
 }
 AP_DECLARE(int) ap_unescape_url_ex(char *url, unsigned int flags)