altarch-user / rpms / httpd

Forked from rpms/httpd 2 years ago
Clone

Blame SOURCES/httpd-2.4.6-CVE-2016-8743.patch

008793
008793
https://bugzilla.redhat.com/show_bug.cgi?id=CVE-2016-8743
008793
008793
diff -uap httpd-2.4.6/docs/manual/mod/core.html.en.cve8743 httpd-2.4.6/docs/manual/mod/core.html.en
008793
--- httpd-2.4.6/docs/manual/mod/core.html.en.cve8743
008793
+++ httpd-2.4.6/docs/manual/mod/core.html.en
008793
@@ -67,6 +67,7 @@
008793
 
  • ForceType
  • 008793
     
  • GprofDir
  • 008793
     
  • HostnameLookups
  • 008793
    +
  • HttpProtocolOptions
  • 008793
     
  • <If>
  • 008793
     
  • <IfDefine>
  • 008793
     
  • <IfModule>
  • 008793
    @@ -93,6 +94,7 @@
    008793
     
  • NameVirtualHost
  • 008793
     
  • Options
  • 008793
     
  • Protocol
  • 008793
    +
  • RegisterHttpMethod
  • 008793
     
  • RLimitCPU
  • 008793
     
  • RLimitMEM
  • 008793
     
  • RLimitNPROC
  • 008793
    @@ -1918,6 +1920,74 @@
    008793
     
    008793
     
    008793
     
    top
    008793
    +
    008793
    +
    008793
    +Description:Modify restrictions on HTTP Request Messages
    008793
    +Syntax:HttpProtocolOptions [Strict|Unsafe] [RegisteredMethods|LenientMethods]
    008793
    + [Allow0.9|Require1.0]
    008793
    +Default:HttpProtocolOptions Strict LenientMethods Allow0.9
    008793
    +Context:server config, virtual host
    008793
    +Status:Core
    008793
    +Module:core
    008793
    +Compatibility:2.2.32 or 2.4.24 and later
    008793
    +
    008793
    +    

    This directive changes the rules applied to the HTTP Request Line

    008793
    +    (RFC 7230 §3.1.1) and the HTTP Request Header Fields
    008793
    +    (RFC 7230 §3.2), which are now applied by default or using
    008793
    +    the Strict option. Due to legacy modules, applications or
    008793
    +    custom user-agents which must be deperecated the Unsafe
    008793
    +    option has been added to revert to the legacy behaviors. These rules
    008793
    +    are applied prior to request processing, so must be configured at the
    008793
    +    global or default (first) matching virtual host section, by IP/port
    008793
    +    interface (and not by name) to be honored.

    008793
    +
    008793
    +    

    Prior to the introduction of this directive, the Apache HTTP Server

    008793
    +    request message parsers were tolerant of a number of forms of input
    008793
    +    which did not conform to the protocol.
    008793
    +    RFC 7230 §9.4 Request Splitting and
    008793
    +    §9.5 Response Smuggling call out only two of the potential
    008793
    +    risks of accepting non-conformant request messages, while
    008793
    +    RFC 7230 §3.5 "Message Parsing Robustness" identify the
    008793
    +    risks of accepting obscure whitespace and request message formatting. 
    008793
    +    As of the introduction of this directive, all grammer rules of the
    008793
    +    specification are enforced in the default Strict operating
    008793
    +    mode, and the strict whitespace suggested by section 3.5 is enforced
    008793
    +    and cannot be relaxed.

    008793
    +
    008793
    +    

    Users are strongly cautioned against toggling the Unsafe

    008793
    +    mode of operation, particularly on outward-facing, publicly accessible
    008793
    +    server deployments.  If an interface is required for faulty monitoring
    008793
    +    or other custom service consumers running on an intranet, users should
    008793
    +    toggle the Unsafe option only on a specific virtual host configured
    008793
    +    to service their internal private network.

    008793
    +
    008793
    +    

    Reviewing the messages logged to the ErrorLog,

    008793
    +    configured with LogLevel debug level,
    008793
    +    can help identify such faulty requests along with their origin.
    008793
    +    Users should pay particular attention to the 400 responses in the access
    008793
    +    log for invalid requests which were unexpectedly rejected.

    008793
    +
    008793
    +    

    RFC 7231 §4.1 "Request Methods" "Overview" requires that

    008793
    +    origin servers shall respond with an error when an unsupported method
    008793
    +    is encountered in the request line. This already happens when the
    008793
    +    LenientMethods option is used, but administrators may wish
    008793
    +    to toggle the RegisteredMethods option and register any
    008793
    +    non-standard methods using the RegisterHttpMethod
    008793
    +    directive, particularly if the Unsafe option has been toggled.
    008793
    +    The RegisteredMethods option should not
    008793
    +    be toggled for forward proxy hosts, as the methods supported by the
    008793
    +    origin servers are unknown to the proxy server.

    008793
    +
    008793
    +    

    RFC 2616 §19.6 "Compatibility With Previous Versions" had

    008793
    +    encouraged HTTP servers to support legacy HTTP/0.9 requests. RFC 7230
    008793
    +    superceeds this with "The expectation to support HTTP/0.9 requests has
    008793
    +    been removed" and offers additional comments in 
    008793
    +    RFC 7230 Appendix A. The Require1.0 option allows
    008793
    +    the user to remove support of the default Allow0.9 option's
    008793
    +    behavior.

    008793
    +
    008793
    +
    008793
    +
    top
    008793
     
    008793
     
    008793
     Description:Contains directives that apply only if a condition is
    008793
    @@ -3541,6 +3611,23 @@
    008793
     
    008793
     
    008793
     
    top
    008793
    +
    008793
    +
    008793
    +Description:Register non-standard HTTP methods
    008793
    +Syntax:RegisterHttpMethod method [method [...]]
    008793
    +Context:server config
    008793
    +Status:Core
    008793
    +Module:core
    008793
    +
    008793
    +

    HTTP Methods that are not conforming to the relvant RFCs are normally

    008793
    +rejected by request processing in Apache HTTPD. To avoid this, modules
    008793
    +can register non-standard HTTP methods they support.
    008793
    +The RegisterHttpMethod allows to register such
    008793
    +methods manually. This can be useful for if such methods are forwared
    008793
    +for external processing, e.g. to a CGI script.

    008793
    +
    008793
    +
    008793
    +
    top
    008793
     
    008793
     
    008793
     Description:Limits the CPU consumption of processes launched
    008793
    diff -uap httpd-2.4.6/include/http_core.h.cve8743 httpd-2.4.6/include/http_core.h
    008793
    --- httpd-2.4.6/include/http_core.h.cve8743
    008793
    +++ httpd-2.4.6/include/http_core.h
    008793
    @@ -668,6 +668,21 @@
    008793
     #define AP_MERGE_TRAILERS_DISABLE  2
    008793
         int merge_trailers;
    008793
     
    008793
    +#define AP_HTTP09_UNSET   0
    008793
    +#define AP_HTTP09_ENABLE  1
    008793
    +#define AP_HTTP09_DISABLE 2
    008793
    +    char http09_enable;
    008793
    +
    008793
    +#define AP_HTTP_CONFORMANCE_UNSET     0
    008793
    +#define AP_HTTP_CONFORMANCE_UNSAFE    1
    008793
    +#define AP_HTTP_CONFORMANCE_STRICT    2
    008793
    +    char http_conformance;
    008793
    +
    008793
    +#define AP_HTTP_METHODS_UNSET         0
    008793
    +#define AP_HTTP_METHODS_LENIENT       1
    008793
    +#define AP_HTTP_METHODS_REGISTERED    2
    008793
    +    char http_methods;
    008793
    +
    008793
     } core_server_config;
    008793
     
    008793
     /* for AddOutputFiltersByType in core.c */
    008793
    diff -uap httpd-2.4.6/include/httpd.h.cve8743 httpd-2.4.6/include/httpd.h
    008793
    --- httpd-2.4.6/include/httpd.h.cve8743
    008793
    +++ httpd-2.4.6/include/httpd.h
    008793
    @@ -1584,6 +1584,28 @@
    008793
      */
    008793
     AP_DECLARE(int) ap_unescape_url(char *url);
    008793
     
    008793
    +/* Scan a string for field content chars, as defined by RFC7230 section 3.2
    008793
    + * including VCHAR/obs-text, as well as HT and SP
    008793
    + * @param ptr The string to scan
    008793
    + * @return A pointer to the first (non-HT) ASCII ctrl character.
    008793
    + * @note lws and trailing whitespace are scanned, the caller is responsible
    008793
    + * for trimming leading and trailing whitespace
    008793
    + */
    008793
    +AP_DECLARE(const char *) ap_scan_http_field_content(const char *ptr);
    008793
    +
    008793
    +/* Scan a string for token characters, as defined by RFC7230 section 3.2.6
    008793
    + * @param ptr The string to scan
    008793
    + * @return A pointer to the first non-token character.
    008793
    + */
    008793
    +AP_DECLARE(const char *) ap_scan_http_token(const char *ptr);
    008793
    +
    008793
    +/* Scan a string for visible ASCII (0x21-0x7E) or obstext (0x80+)
    008793
    + * and return a pointer to the first SP/CTL/NUL character encountered.
    008793
    + * @param ptr The string to scan
    008793
    + * @return A pointer to the first SP/CTL character.
    008793
    + */
    008793
    +AP_DECLARE(const char *) ap_scan_vchar_obstext(const char *ptr);
    008793
    +
    008793
     /**
    008793
      * Unescape a URL, but leaving %2f (slashes) escaped
    008793
      * @param url The url to unescape
    008793
    diff -uap httpd-2.4.6/include/http_protocol.h.cve8743 httpd-2.4.6/include/http_protocol.h
    008793
    --- httpd-2.4.6/include/http_protocol.h.cve8743
    008793
    +++ httpd-2.4.6/include/http_protocol.h
    008793
    @@ -582,17 +582,22 @@
    008793
      */
    008793
     AP_CORE_DECLARE(void) ap_parse_uri(request_rec *r, const char *uri);
    008793
     
    008793
    +#define AP_GETLINE_FOLD 1 /* Whether to merge continuation lines */
    008793
    +#define AP_GETLINE_CRLF 2 /*Whether line ends must be in the form CR LF */
    008793
    +
    008793
     /**
    008793
      * Get the next line of input for the request
    008793
      * @param s The buffer into which to read the line
    008793
      * @param n The size of the buffer
    008793
      * @param r The request
    008793
    - * @param fold Whether to merge continuation lines
    008793
    + * @param flags Bit flag of multiple parsing options
    008793
    + *              AP_GETLINE_FOLD Whether to merge continuation lines
    008793
    + *              AP_GETLINE_CRLF Whether line ends must be in the form CR LF
    008793
      * @return The length of the line, if successful
    008793
      *         n, if the line is too big to fit in the buffer
    008793
      *         -1 for miscellaneous errors
    008793
      */
    008793
    -AP_DECLARE(int) ap_getline(char *s, int n, request_rec *r, int fold);
    008793
    +AP_DECLARE(int) ap_getline(char *s, int n, request_rec *r, int flags);
    008793
     
    008793
     /**
    008793
      * Get the next line of input for the request
    008793
    @@ -610,7 +615,9 @@
    008793
      * @param n The size of the buffer
    008793
      * @param read The length of the line.
    008793
      * @param r The request
    008793
    - * @param fold Whether to merge continuation lines
    008793
    + * @param flags Bit flag of multiple parsing options
    008793
    + *              AP_GETLINE_FOLD Whether to merge continuation lines
    008793
    + *              AP_GETLINE_CRLF Whether line ends must be in the form CR LF
    008793
      * @param bb Working brigade to use when reading buckets
    008793
      * @return APR_SUCCESS, if successful
    008793
      *         APR_ENOSPC, if the line is too big to fit in the buffer
    008793
    @@ -619,7 +626,7 @@
    008793
     #if APR_CHARSET_EBCDIC
    008793
     AP_DECLARE(apr_status_t) ap_rgetline(char **s, apr_size_t n,
    008793
                                          apr_size_t *read,
    008793
    -                                     request_rec *r, int fold,
    008793
    +                                     request_rec *r, int flags,
    008793
                                          apr_bucket_brigade *bb);
    008793
     #else /* ASCII box */
    008793
     #define ap_rgetline(s, n, read, r, fold, bb) \
    008793
    @@ -629,7 +636,7 @@
    008793
     /** @see ap_rgetline */
    008793
     AP_DECLARE(apr_status_t) ap_rgetline_core(char **s, apr_size_t n,
    008793
                                               apr_size_t *read,
    008793
    -                                          request_rec *r, int fold,
    008793
    +                                          request_rec *r, int flags,
    008793
                                               apr_bucket_brigade *bb);
    008793
     
    008793
     /**
    008793
    diff -uap httpd-2.4.6/modules/http/http_filters.c.cve8743 httpd-2.4.6/modules/http/http_filters.c
    008793
    --- httpd-2.4.6/modules/http/http_filters.c.cve8743
    008793
    +++ httpd-2.4.6/modules/http/http_filters.c
    008793
    @@ -126,14 +126,15 @@
    008793
     
    008793
     /**
    008793
      * Parse a chunk line with optional extension, detect overflow.
    008793
    - * There are two error cases:
    008793
    - *  1) If the conversion would require too many bits, APR_EGENERAL is returned.
    008793
    - *  2) If the conversion used the correct number of bits, but an overflow
    008793
    + * There are several error cases:
    008793
    + *  1) If the chunk link is misformatted, APR_EINVAL is returned.
    008793
    + *  2) If the conversion would require too many bits, APR_EGENERAL is returned.
    008793
    + *  3) If the conversion used the correct number of bits, but an overflow
    008793
      *     caused only the sign bit to flip, then APR_ENOSPC is returned.
    008793
    - * In general, any negative number can be considered an overflow error.
    008793
    + * A negative chunk length always indicates an overflow error.
    008793
      */
    008793
     static apr_status_t parse_chunk_size(http_ctx_t *ctx, const char *buffer,
    008793
    -                                     apr_size_t len, int linelimit)
    008793
    +                                     apr_size_t len, int linelimit, int strict)
    008793
     {
    008793
         apr_size_t i = 0;
    008793
     
    008793
    @@ -146,6 +147,12 @@
    008793
             if (ctx->state == BODY_CHUNK_END
    008793
                     || ctx->state == BODY_CHUNK_END_LF) {
    008793
                 if (c == LF) {
    008793
    +                if (strict && (ctx->state != BODY_CHUNK_END_LF)) {
    008793
    +                    /*
    008793
    +                     * CR missing before LF.
    008793
    +                     */
    008793
    +                    return APR_EINVAL;
    008793
    +                }
    008793
                     ctx->state = BODY_CHUNK;
    008793
                 }
    008793
                 else if (c == CR && ctx->state == BODY_CHUNK_END) {
    008793
    @@ -153,7 +160,7 @@
    008793
                 }
    008793
                 else {
    008793
                     /*
    008793
    -                 * LF expected.
    008793
    +                 * CRLF expected.
    008793
                      */
    008793
                     return APR_EINVAL;
    008793
                 }
    008793
    @@ -180,6 +187,12 @@
    008793
             }
    008793
     
    008793
             if (c == LF) {
    008793
    +            if (strict && (ctx->state != BODY_CHUNK_LF)) {
    008793
    +                /*
    008793
    +                 * CR missing before LF.
    008793
    +                 */
    008793
    +                return APR_EINVAL;
    008793
    +            }
    008793
                 if (ctx->remaining) {
    008793
                     ctx->state = BODY_CHUNK_DATA;
    008793
                 }
    008793
    @@ -201,14 +214,17 @@
    008793
             }
    008793
             else if (ctx->state == BODY_CHUNK_EXT) {
    008793
                 /*
    008793
    -             * Control chars (but tabs) are invalid.
    008793
    +             * Control chars (excluding tabs) are invalid.
    008793
    +             * TODO: more precisely limit input
    008793
                  */
    008793
                 if (c != '\t' && apr_iscntrl(c)) {
    008793
                     return APR_EINVAL;
    008793
                 }
    008793
             }
    008793
             else if (c == ' ' || c == '\t') {
    008793
    -            /* Be lenient up to 10 BWS (term from rfc7230 - 3.2.3).
    008793
    +            /* Be lenient up to 10 implied *LWS, a legacy of RFC 2616,
    008793
    +             * and noted as errata to RFC7230;
    008793
    +             * https://www.rfc-editor.org/errata_search.php?rfc=7230&eid=4667
    008793
                  */
    008793
                 ctx->state = BODY_CHUNK_CR;
    008793
                 if (++ctx->chunk_bws > 10) {
    008793
    @@ -324,7 +340,10 @@
    008793
                                 ap_input_mode_t mode, apr_read_type_e block,
    008793
                                 apr_off_t readbytes)
    008793
     {
    008793
    -    core_server_config *conf;
    008793
    +    core_server_config *conf =
    008793
    +        (core_server_config *) ap_get_module_config(f->r->server->module_config,
    008793
    +                                                    &core_module);
    008793
    +    int strict = (conf->http_conformance != AP_HTTP_CONFORMANCE_UNSAFE);
    008793
         apr_bucket *e;
    008793
         http_ctx_t *ctx = f->ctx;
    008793
         apr_status_t rv;
    008793
    @@ -332,9 +351,6 @@
    008793
         apr_bucket_brigade *bb;
    008793
         int again;
    008793
     
    008793
    -    conf = (core_server_config *)
    008793
    -        ap_get_module_config(f->r->server->module_config, &core_module);
    008793
    -
    008793
         /* just get out of the way of things we don't want. */
    008793
         if (mode != AP_MODE_READBYTES && mode != AP_MODE_GETLINE) {
    008793
             return ap_get_brigade(f->next, b, mode, block, readbytes);
    008793
    @@ -526,7 +542,7 @@
    008793
                         if (rv == APR_SUCCESS) {
    008793
                             parsing = 1;
    008793
                             rv = parse_chunk_size(ctx, buffer, len,
    008793
    -                                f->r->server->limit_req_fieldsize);
    008793
    +                                f->r->server->limit_req_fieldsize, strict);
    008793
                         }
    008793
                         if (rv != APR_SUCCESS) {
    008793
                             ap_log_rerror(APLOG_MARK, APLOG_INFO, rv, f->r, APLOGNO(01590)
    008793
    @@ -668,14 +684,121 @@
    008793
         return APR_SUCCESS;
    008793
     }
    008793
     
    008793
    +struct check_header_ctx {
    008793
    +    request_rec *r;
    008793
    +    int strict;
    008793
    +};
    008793
    +
    008793
    +/* check a single header, to be used with apr_table_do() */
    008793
    +static int check_header(struct check_header_ctx *ctx,
    008793
    +                        const char *name, const char **val)
    008793
    +{
    008793
    +    const char *pos, *end;
    008793
    +    char *dst = NULL;
    008793
    +
    008793
    +    if (name[0] == '\0') {
    008793
    +        ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, ctx->r, APLOGNO(02428)
    008793
    +                      "Empty response header name, aborting request");
    008793
    +        return 0;
    008793
    +    }
    008793
    +
    008793
    +    if (ctx->strict) {
    008793
    +        end = ap_scan_http_token(name);
    008793
    +    }
    008793
    +    else {
    008793
    +        end = ap_scan_vchar_obstext(name);
    008793
    +    }
    008793
    +    if (*end) {
    008793
    +        ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, ctx->r, APLOGNO(02429)
    008793
    +                      "Response header name '%s' contains invalid "
    008793
    +                      "characters, aborting request",
    008793
    +                      name);
    008793
    +        return 0;
    008793
    +    }
    008793
    +
    008793
    +    for (pos = *val; *pos; pos = end) {
    008793
    +        end = ap_scan_http_field_content(pos);
    008793
    +        if (*end) {
    008793
    +            if (end[0] != CR || end[1] != LF || (end[2] != ' ' &&
    008793
    +                                                 end[2] != '\t')) {
    008793
    +                ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, ctx->r, APLOGNO(02430)
    008793
    +                              "Response header '%s' value of '%s' contains "
    008793
    +                              "invalid characters, aborting request",
    008793
    +                              name, pos);
    008793
    +                return 0;
    008793
    +            }
    008793
    +            if (!dst) {
    008793
    +                *val = dst = apr_palloc(ctx->r->pool, strlen(*val) + 1);
    008793
    +            }
    008793
    +        }
    008793
    +        if (dst) {
    008793
    +            memcpy(dst, pos, end - pos);
    008793
    +            dst += end - pos;
    008793
    +            if (*end) {
    008793
    +                /* skip folding and replace with a single space */
    008793
    +                end += 3 + strspn(end + 3, "\t ");
    008793
    +                *dst++ = ' ';
    008793
    +            }
    008793
    +        }
    008793
    +    }
    008793
    +    if (dst) {
    008793
    +        *dst = '\0';
    008793
    +    }
    008793
    +    return 1;
    008793
    +}
    008793
    +
    008793
    +static int check_headers_table(apr_table_t *t, struct check_header_ctx *ctx)
    008793
    +{
    008793
    +    const apr_array_header_t *headers = apr_table_elts(t);
    008793
    +    apr_table_entry_t *header;
    008793
    +    int i;
    008793
    +
    008793
    +    for (i = 0; i < headers->nelts; ++i) {
    008793
    +        header = &APR_ARRAY_IDX(headers, i, apr_table_entry_t);
    008793
    +        if (!header->key) {
    008793
    +            continue;
    008793
    +        }
    008793
    +        if (!check_header(ctx, header->key, (const char **)&header->val)) {
    008793
    +            return 0;
    008793
    +        }
    008793
    +    }
    008793
    +    return 1;
    008793
    +}
    008793
    +
    008793
    +/**
    008793
    + * Check headers for HTTP conformance
    008793
    + * @return 1 if ok, 0 if bad
    008793
    + */
    008793
    +static APR_INLINE int check_headers(request_rec *r)
    008793
    +{
    008793
    +    struct check_header_ctx ctx;
    008793
    +    core_server_config *conf =
    008793
    +            ap_get_core_module_config(r->server->module_config);
    008793
    +
    008793
    +    ctx.r = r;
    008793
    +    ctx.strict = (conf->http_conformance != AP_HTTP_CONFORMANCE_UNSAFE);
    008793
    +    return check_headers_table(r->headers_out, &ctx) &&
    008793
    +           check_headers_table(r->err_headers_out, &ctx;;
    008793
    +}
    008793
    +
    008793
    +static int check_headers_recursion(request_rec *r)
    008793
    +{
    008793
    +    void *check = NULL;
    008793
    +    apr_pool_userdata_get(&check, "check_headers_recursion", r->pool);
    008793
    +    if (check) {
    008793
    +        return 1;
    008793
    +    }
    008793
    +    apr_pool_userdata_setn("true", "check_headers_recursion", NULL, r->pool);
    008793
    +    return 0;
    008793
    +}
    008793
    +
    008793
     typedef struct header_struct {
    008793
         apr_pool_t *pool;
    008793
         apr_bucket_brigade *bb;
    008793
     } header_struct;
    008793
     
    008793
     /* Send a single HTTP header field to the client.  Note that this function
    008793
    - * is used in calls to table_do(), so their interfaces are co-dependent.
    008793
    - * In other words, don't change this one without checking table_do in alloc.c.
    008793
    + * is used in calls to apr_table_do(), so don't change its interface.
    008793
      * It returns true unless there was a write error of some kind.
    008793
      */
    008793
     static int form_header_field(header_struct *h,
    008793
    @@ -1160,6 +1283,7 @@
    008793
     
    008793
     typedef struct header_filter_ctx {
    008793
         int headers_sent;
    008793
    +    int headers_error;
    008793
     } header_filter_ctx;
    008793
     
    008793
     AP_CORE_DECLARE_NONSTD(apr_status_t) ap_http_header_filter(ap_filter_t *f,
    008793
    @@ -1175,19 +1299,23 @@
    008793
         header_filter_ctx *ctx = f->ctx;
    008793
         const char *ctype;
    008793
         ap_bucket_error *eb = NULL;
    008793
    +    apr_bucket *eos = NULL;
    008793
     
    008793
         AP_DEBUG_ASSERT(!r->main);
    008793
     
    008793
    -    if (r->header_only) {
    008793
    -        if (!ctx) {
    008793
    -            ctx = f->ctx = apr_pcalloc(r->pool, sizeof(header_filter_ctx));
    008793
    -        }
    008793
    -        else if (ctx->headers_sent) {
    008793
    +    if (!ctx) {
    008793
    +        ctx = f->ctx = apr_pcalloc(r->pool, sizeof(header_filter_ctx));
    008793
    +    }
    008793
    +    if (ctx->headers_sent) {
    008793
    +        /* Eat body if response must not have one. */
    008793
    +        if (r->header_only || r->status == HTTP_NO_CONTENT) {
    008793
                 apr_brigade_cleanup(b);
    008793
    -            return OK;
    008793
    +            return APR_SUCCESS;
    008793
             }
    008793
         }
    008793
    -
    008793
    +    else if (!ctx->headers_error && !check_headers(r)) {
    008793
    +        ctx->headers_error = 1;
    008793
    +    }
    008793
         for (e = APR_BRIGADE_FIRST(b);
    008793
              e != APR_BRIGADE_SENTINEL(b);
    008793
              e = APR_BUCKET_NEXT(e))
    008793
    @@ -1204,10 +1332,44 @@
    008793
                 ap_remove_output_filter(f);
    008793
                 return ap_pass_brigade(f->next, b);
    008793
             }
    008793
    +        if (ctx->headers_error && APR_BUCKET_IS_EOS(e)) {
    008793
    +            eos = e;
    008793
    +        }
    008793
         }
    008793
    -    if (eb) {
    008793
    -        int status;
    008793
    +    if (ctx->headers_error) {
    008793
    +        if (!eos) {
    008793
    +            /* Eat body until EOS */
    008793
    +            apr_brigade_cleanup(b);
    008793
    +            return APR_SUCCESS;
    008793
    +        }
    008793
     
    008793
    +        /* We may come back here from ap_die() below,
    008793
    +         * so clear anything from this response.
    008793
    +         */
    008793
    +        ctx->headers_error = 0;
    008793
    +        apr_table_clear(r->headers_out);
    008793
    +        apr_table_clear(r->err_headers_out);
    008793
    +
    008793
    +        /* Don't recall ap_die() if we come back here (from its own internal
    008793
    +         * redirect or error response), otherwise we can end up in infinite
    008793
    +         * recursion; better fall through with 500, minimal headers and an
    008793
    +         * empty body (EOS only).
    008793
    +         */
    008793
    +        if (!check_headers_recursion(r)) {
    008793
    +            apr_brigade_cleanup(b);
    008793
    +            ap_die(HTTP_INTERNAL_SERVER_ERROR, r);
    008793
    +            return AP_FILTER_ERROR;
    008793
    +        }
    008793
    +        APR_BUCKET_REMOVE(eos);
    008793
    +        apr_brigade_cleanup(b);
    008793
    +        APR_BRIGADE_INSERT_TAIL(b, eos);
    008793
    +        r->status = HTTP_INTERNAL_SERVER_ERROR;
    008793
    +        r->content_type = r->content_encoding = NULL;
    008793
    +        r->content_languages = NULL;
    008793
    +        ap_set_content_length(r, 0);
    008793
    +    }
    008793
    +    else if (eb) {
    008793
    +        int status;
    008793
             status = eb->status;
    008793
             apr_brigade_cleanup(b);
    008793
             ap_die(status, r);
    008793
    @@ -1264,6 +1426,10 @@
    008793
             apr_table_unset(r->headers_out, "Content-Length");
    008793
         }
    008793
     
    008793
    +    if (r->status == HTTP_NO_CONTENT) {
    008793
    +        apr_table_unset(r->headers_out, "Content-Length");
    008793
    +    }
    008793
    +
    008793
         ctype = ap_make_content_type(r, r->content_type);
    008793
         if (ctype) {
    008793
             apr_table_setn(r->headers_out, "Content-Type", ctype);
    008793
    @@ -1352,11 +1518,11 @@
    008793
         terminate_header(b2);
    008793
     
    008793
         ap_pass_brigade(f->next, b2);
    008793
    +    ctx->headers_sent = 1;
    008793
     
    008793
    -    if (r->header_only) {
    008793
    +    if (r->header_only || r->status == HTTP_NO_CONTENT) {
    008793
             apr_brigade_cleanup(b);
    008793
    -        ctx->headers_sent = 1;
    008793
    -        return OK;
    008793
    +        return APR_SUCCESS;
    008793
         }
    008793
     
    008793
         r->sent_bodyct = 1;         /* Whatever follows is real body stuff... */
    008793
    diff -uap httpd-2.4.6/server/core.c.cve8743 httpd-2.4.6/server/core.c
    008793
    --- httpd-2.4.6/server/core.c.cve8743
    008793
    +++ httpd-2.4.6/server/core.c
    008793
    @@ -506,6 +506,15 @@
    008793
         if (virt->trace_enable != AP_TRACE_UNSET)
    008793
             conf->trace_enable = virt->trace_enable;
    008793
     
    008793
    +    if (virt->http09_enable != AP_HTTP09_UNSET)
    008793
    +        conf->http09_enable = virt->http09_enable;
    008793
    +
    008793
    +    if (virt->http_conformance != AP_HTTP_CONFORMANCE_UNSET)
    008793
    +        conf->http_conformance = virt->http_conformance;
    008793
    +
    008793
    +    if (virt->http_methods != AP_HTTP_METHODS_UNSET)
    008793
    +        conf->http_methods = virt->http_methods;
    008793
    +
    008793
         /* no action for virt->accf_map, not allowed per-vhost */
    008793
     
    008793
         if (virt->protocol)
    008793
    @@ -3632,6 +3641,57 @@
    008793
         return NULL;
    008793
     }
    008793
     
    008793
    +static const char *set_http_protocol_options(cmd_parms *cmd, void *dummy,
    008793
    +                                             const char *arg)
    008793
    +{
    008793
    +    core_server_config *conf =
    008793
    +        ap_get_core_module_config(cmd->server->module_config);
    008793
    +
    008793
    +    if (strcasecmp(arg, "allow0.9") == 0)
    008793
    +        conf->http09_enable |= AP_HTTP09_ENABLE;
    008793
    +    else if (strcasecmp(arg, "require1.0") == 0)
    008793
    +        conf->http09_enable |= AP_HTTP09_DISABLE;
    008793
    +    else if (strcasecmp(arg, "strict") == 0)
    008793
    +        conf->http_conformance |= AP_HTTP_CONFORMANCE_STRICT;
    008793
    +    else if (strcasecmp(arg, "unsafe") == 0)
    008793
    +        conf->http_conformance |= AP_HTTP_CONFORMANCE_UNSAFE;
    008793
    +    else if (strcasecmp(arg, "registeredmethods") == 0)
    008793
    +        conf->http_methods |= AP_HTTP_METHODS_REGISTERED;
    008793
    +    else if (strcasecmp(arg, "lenientmethods") == 0)
    008793
    +        conf->http_methods |= AP_HTTP_METHODS_LENIENT;
    008793
    +    else
    008793
    +        return "HttpProtocolOptions accepts "
    008793
    +               "'Unsafe' or 'Strict' (default), "
    008793
    +               "'RegisteredMethods' or 'LenientMethods' (default), and "
    008793
    +               "'Require1.0' or 'Allow0.9' (default)";
    008793
    +
    008793
    +    if ((conf->http09_enable & AP_HTTP09_ENABLE)
    008793
    +            && (conf->http09_enable & AP_HTTP09_DISABLE))
    008793
    +        return "HttpProtocolOptions 'Allow0.9' and 'Require1.0'"
    008793
    +               " are mutually exclusive";
    008793
    +
    008793
    +    if ((conf->http_conformance & AP_HTTP_CONFORMANCE_STRICT)
    008793
    +            && (conf->http_conformance & AP_HTTP_CONFORMANCE_UNSAFE))
    008793
    +        return "HttpProtocolOptions 'Strict' and 'Unsafe'"
    008793
    +               " are mutually exclusive";
    008793
    +
    008793
    +    if ((conf->http_methods & AP_HTTP_METHODS_REGISTERED)
    008793
    +            && (conf->http_methods & AP_HTTP_METHODS_LENIENT))
    008793
    +        return "HttpProtocolOptions 'RegisteredMethods' and 'LenientMethods'"
    008793
    +               " are mutually exclusive";
    008793
    +
    008793
    +    return NULL;
    008793
    +}
    008793
    +
    008793
    +static const char *set_http_method(cmd_parms *cmd, void *conf, const char *arg)
    008793
    +{
    008793
    +    const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
    008793
    +    if (err != NULL)
    008793
    +        return err;
    008793
    +    ap_method_register(cmd->pool, arg);
    008793
    +    return NULL;
    008793
    +}
    008793
    +
    008793
     static apr_hash_t *errorlog_hash;
    008793
     
    008793
     static int log_constant_item(const ap_errorlog_info *info, const char *arg,
    008793
    @@ -4143,6 +4203,13 @@
    008793
                   "'on' (default), 'off' or 'extended' to trace request body content"),
    008793
     AP_INIT_FLAG("MergeTrailers", set_merge_trailers, NULL, RSRC_CONF,
    008793
                   "merge request trailers into request headers or not"),
    008793
    +AP_INIT_ITERATE("HttpProtocolOptions", set_http_protocol_options, NULL, RSRC_CONF,
    008793
    +                "'Allow0.9' or 'Require1.0' (default); "
    008793
    +                "'RegisteredMethods' or 'LenientMethods' (default); "
    008793
    +                "'Unsafe' or 'Strict' (default). Sets HTTP acceptance rules")
    008793
    +,
    008793
    +AP_INIT_ITERATE("RegisterHttpMethod", set_http_method, NULL, RSRC_CONF,
    008793
    +                "Registers non-standard HTTP methods"),
    008793
     { NULL }
    008793
     };
    008793
     
    008793
    diff -uap httpd-2.4.6/server/gen_test_char.c.cve8743 httpd-2.4.6/server/gen_test_char.c
    008793
    --- httpd-2.4.6/server/gen_test_char.c.cve8743
    008793
    +++ httpd-2.4.6/server/gen_test_char.c
    008793
    @@ -16,11 +16,11 @@
    008793
     
    008793
     #ifdef CROSS_COMPILE
    008793
     
    008793
    +#include <ctype.h>
    008793
     #define apr_isalnum(c) (isalnum(((unsigned char)(c))))
    008793
     #define apr_isalpha(c) (isalpha(((unsigned char)(c))))
    008793
     #define apr_iscntrl(c) (iscntrl(((unsigned char)(c))))
    008793
     #define apr_isprint(c) (isprint(((unsigned char)(c))))
    008793
    -#include <ctype.h>
    008793
     #define APR_HAVE_STDIO_H 1
    008793
     #define APR_HAVE_STRING_H 1
    008793
     
    008793
    @@ -52,11 +52,13 @@
    008793
     #define T_ESCAPE_LOGITEM      (0x10)
    008793
     #define T_ESCAPE_FORENSIC     (0x20)
    008793
     #define T_ESCAPE_URLENCODED   (0x40)
    008793
    +#define T_HTTP_CTRLS          (0x80)
    008793
    +#define T_VCHAR_OBSTEXT      (0x100)
    008793
     
    008793
     int main(int argc, char *argv[])
    008793
     {
    008793
         unsigned c;
    008793
    -    unsigned char flags;
    008793
    +    unsigned short flags;
    008793
     
    008793
         printf("/* this file is automatically generated by gen_test_char, "
    008793
                "do not edit */\n"
    008793
    @@ -67,19 +69,23 @@
    008793
                "#define T_ESCAPE_LOGITEM       (%u)\n"
    008793
                "#define T_ESCAPE_FORENSIC      (%u)\n"
    008793
                "#define T_ESCAPE_URLENCODED    (%u)\n"
    008793
    +           "#define T_HTTP_CTRLS           (%u)\n"
    008793
    +           "#define T_VCHAR_OBSTEXT        (%u)\n"
    008793
                "\n"
    008793
    -           "static const unsigned char test_char_table[256] = {",
    008793
    +           "static const unsigned short test_char_table[256] = {",
    008793
                T_ESCAPE_SHELL_CMD,
    008793
                T_ESCAPE_PATH_SEGMENT,
    008793
                T_OS_ESCAPE_PATH,
    008793
                T_HTTP_TOKEN_STOP,
    008793
                T_ESCAPE_LOGITEM,
    008793
                T_ESCAPE_FORENSIC,
    008793
    -           T_ESCAPE_URLENCODED);
    008793
    +           T_ESCAPE_URLENCODED,
    008793
    +           T_HTTP_CTRLS,
    008793
    +           T_VCHAR_OBSTEXT);
    008793
     
    008793
         for (c = 0; c < 256; ++c) {
    008793
             flags = 0;
    008793
    -        if (c % 20 == 0)
    008793
    +        if (c % 8 == 0)
    008793
                 printf("\n    ");
    008793
     
    008793
             /* escape_shell_cmd */
    008793
    @@ -107,7 +113,7 @@
    008793
                 flags |= T_ESCAPE_PATH_SEGMENT;
    008793
             }
    008793
     
    008793
    -        if (!apr_isalnum(c) && !strchr("$-_.+!*'(),:@&=/~", c)) {
    008793
    +        if (!apr_isalnum(c) && !strchr("$-_.+!*'(),:;@&=/~", c)) {
    008793
                 flags |= T_OS_ESCAPE_PATH;
    008793
             }
    008793
     
    008793
    @@ -115,11 +121,32 @@
    008793
                 flags |= T_ESCAPE_URLENCODED;
    008793
             }
    008793
     
    008793
    -        /* these are the "tspecials" (RFC2068) or "separators" (RFC2616) */
    008793
    -        if (c && (apr_iscntrl(c) || strchr(" \t()<>@,;:\\\"/[]?={}", c))) {
    008793
    +        /* Stop for any non-'token' character, including ctrls, obs-text,
    008793
    +         * and "tspecials" (RFC2068) a.k.a. "separators" (RFC2616), which
    008793
    +         * is easer to express as characters remaining in the ASCII token set
    008793
    +         */
    008793
    +        if (!c || !(apr_isalnum(c) || strchr("!#$%&'*+-.^_`|~", c))) {
    008793
                 flags |= T_HTTP_TOKEN_STOP;
    008793
             }
    008793
     
    008793
    +        /* Catch CTRLs other than VCHAR, HT and SP, and obs-text (RFC7230 3.2)
    008793
    +         * This includes only the C0 plane, not C1 (which is obs-text itself.)
    008793
    +         * XXX: We should verify that all ASCII C0 ctrls/DEL corresponding to
    008793
    +         * the current EBCDIC translation are captured, and ASCII C1 ctrls
    008793
    +         * corresponding are all permitted (as they fall under obs-text rule)
    008793
    +         */
    008793
    +        if (!c || (apr_iscntrl(c) && c != '\t')) {
    008793
    +            flags |= T_HTTP_CTRLS;
    008793
    +        }
    008793
    +
    008793
    +        /* From RFC3986, the specific sets of gen-delims, sub-delims (2.2),
    008793
    +         * and unreserved (2.3) that are possible somewhere within a URI.
    008793
    +         * Spec requires all others to be %XX encoded, including obs-text.
    008793
    +         */
    008793
    +        if (c && !apr_iscntrl(c) && c != ' ') {
    008793
    +            flags |= T_VCHAR_OBSTEXT;
    008793
    +        }
    008793
    +
    008793
             /* For logging, escape all control characters,
    008793
              * double quotes (because they delimit the request in the log file)
    008793
              * backslashes (because we use backslash for escaping)
    008793
    @@ -137,7 +164,7 @@
    008793
                 flags |= T_ESCAPE_FORENSIC;
    008793
             }
    008793
     
    008793
    -        printf("%u%c", flags, (c < 255) ? ',' : ' ');
    008793
    +        printf("0x%03x%c", flags, (c < 255) ? ',' : ' ');
    008793
         }
    008793
     
    008793
         printf("\n};\n");
    008793
    diff -uap httpd-2.4.6/server/protocol.c.cve8743 httpd-2.4.6/server/protocol.c
    008793
    --- httpd-2.4.6/server/protocol.c.cve8743
    008793
    +++ httpd-2.4.6/server/protocol.c
    008793
    @@ -189,6 +189,10 @@
    008793
      * caused by MIME folding (or broken clients) if fold != 0, and place it
    008793
      * in the buffer s, of size n bytes, without the ending newline.
    008793
      *
    008793
    + * Pulls from r->proto_input_filters instead of r->input_filters for
    008793
    + * stricter protocol adherence and better input filter behavior during
    008793
    + * chunked trailer processing (for http).
    008793
    + *
    008793
      * If s is NULL, ap_rgetline_core will allocate necessary memory from r->pool.
    008793
      *
    008793
      * Returns APR_SUCCESS if there are no problems and sets *read to be
    008793
    @@ -197,7 +201,7 @@
    008793
      * APR_ENOSPC is returned if there is not enough buffer space.
    008793
      * Other errors may be returned on other errors.
    008793
      *
    008793
    - * The LF is *not* returned in the buffer.  Therefore, a *read of 0
    008793
    + * The [CR]LF are *not* returned in the buffer.  Therefore, a *read of 0
    008793
      * indicates that an empty line was read.
    008793
      *
    008793
      * Notes: Because the buffer uses 1 char for NUL, the most we can return is
    008793
    @@ -208,13 +212,15 @@
    008793
      */
    008793
     AP_DECLARE(apr_status_t) ap_rgetline_core(char **s, apr_size_t n,
    008793
                                               apr_size_t *read, request_rec *r,
    008793
    -                                          int fold, apr_bucket_brigade *bb)
    008793
    +                                          int flags, apr_bucket_brigade *bb)
    008793
     {
    008793
         apr_status_t rv;
    008793
         apr_bucket *e;
    008793
         apr_size_t bytes_handled = 0, current_alloc = 0;
    008793
         char *pos, *last_char = *s;
    008793
         int do_alloc = (*s == NULL), saw_eos = 0;
    008793
    +    int fold = flags & AP_GETLINE_FOLD;
    008793
    +    int crlf = flags & AP_GETLINE_CRLF;
    008793
     
    008793
         /*
    008793
          * Initialize last_char as otherwise a random value will be compared
    008793
    @@ -226,13 +232,15 @@
    008793
     
    008793
         for (;;) {
    008793
             apr_brigade_cleanup(bb);
    008793
    -        rv = ap_get_brigade(r->input_filters, bb, AP_MODE_GETLINE,
    008793
    +        rv = ap_get_brigade(r->proto_input_filters, bb, AP_MODE_GETLINE,
    008793
                                 APR_BLOCK_READ, 0);
    008793
             if (rv != APR_SUCCESS) {
    008793
                 return rv;
    008793
             }
    008793
     
    008793
    -        /* Something horribly wrong happened.  Someone didn't block! */
    008793
    +        /* Something horribly wrong happened.  Someone didn't block! 
    008793
    +         * (this also happens at the end of each keepalive connection)
    008793
    +         */
    008793
             if (APR_BRIGADE_EMPTY(bb)) {
    008793
                 return APR_EGENERAL;
    008793
             }
    008793
    @@ -318,6 +326,13 @@
    008793
             }
    008793
         }
    008793
     
    008793
    +    if (crlf && (last_char <= *s || last_char[-1] != APR_ASCII_CR)) {
    008793
    +        *last_char = '\0';
    008793
    +        bytes_handled = last_char - *s;
    008793
    +        *read = bytes_handled;
    008793
    +        return APR_EINVAL;
    008793
    +    }
    008793
    +
    008793
         /* Now NUL-terminate the string at the end of the line;
    008793
          * if the last-but-one character is a CR, terminate there */
    008793
         if (last_char > *s && last_char[-1] == APR_ASCII_CR) {
    008793
    @@ -340,7 +355,7 @@
    008793
                 apr_brigade_cleanup(bb);
    008793
     
    008793
                 /* We only care about the first byte. */
    008793
    -            rv = ap_get_brigade(r->input_filters, bb, AP_MODE_SPECULATIVE,
    008793
    +            rv = ap_get_brigade(r->proto_input_filters, bb, AP_MODE_SPECULATIVE,
    008793
                                     APR_BLOCK_READ, 1);
    008793
                 if (rv != APR_SUCCESS) {
    008793
                     return rv;
    008793
    @@ -391,7 +406,8 @@
    008793
                          */
    008793
                         if (do_alloc) {
    008793
                             tmp = NULL;
    008793
    -                    } else {
    008793
    +                    }
    008793
    +                    else {
    008793
                             /* We're null terminated. */
    008793
                             tmp = last_char;
    008793
                         }
    008793
    @@ -461,7 +477,7 @@
    008793
     }
    008793
     #endif
    008793
     
    008793
    -AP_DECLARE(int) ap_getline(char *s, int n, request_rec *r, int fold)
    008793
    +AP_DECLARE(int) ap_getline(char *s, int n, request_rec *r, int flags)
    008793
     {
    008793
         char *tmp_s = s;
    008793
         apr_status_t rv;
    008793
    @@ -469,7 +485,7 @@
    008793
         apr_bucket_brigade *tmp_bb;
    008793
     
    008793
         tmp_bb = apr_brigade_create(r->pool, r->connection->bucket_alloc);
    008793
    -    rv = ap_rgetline(&tmp_s, n, &len, r, fold, tmp_bb);
    008793
    +    rv = ap_rgetline(&tmp_s, n, &len, r, flags, tmp_bb);
    008793
         apr_brigade_destroy(tmp_bb);
    008793
     
    008793
         /* Map the out-of-space condition to the old API. */
    008793
    @@ -549,16 +565,29 @@
    008793
         }
    008793
     }
    008793
     
    008793
    -static int read_request_line(request_rec *r, apr_bucket_brigade *bb)
    008793
    +/* get the length of the field name for logging, but no more than 80 bytes */
    008793
    +#define LOG_NAME_MAX_LEN 80
    008793
    +static int field_name_len(const char *field)
    008793
     {
    008793
    -    const char *ll;
    008793
    -    const char *uri;
    008793
    -    const char *pro;
    008793
    +    const char *end = ap_strchr_c(field, ':');
    008793
    +    if (end == NULL || end - field > LOG_NAME_MAX_LEN)
    008793
    +        return LOG_NAME_MAX_LEN;
    008793
    +    return end - field;
    008793
    +}
    008793
     
    008793
    -    int major = 1, minor = 0;   /* Assume HTTP/1.0 if non-"HTTP" protocol */
    008793
    -    char http[5];
    008793
    +static int read_request_line(request_rec *r, apr_bucket_brigade *bb)
    008793
    +{
    008793
    +    enum {
    008793
    +        rrl_none, rrl_badmethod, rrl_badwhitespace, rrl_excesswhitespace,
    008793
    +        rrl_missinguri, rrl_baduri, rrl_badprotocol, rrl_trailingtext,
    008793
    +        rrl_badmethod09, rrl_reject09
    008793
    +    } deferred_error = rrl_none;
    008793
    +    char *ll;
    008793
    +    char *uri;
    008793
         apr_size_t len;
    008793
         int num_blank_lines = 0;
    008793
    +    core_server_config *conf = ap_get_core_module_config(r->server->module_config);
    008793
    +    int strict = (conf->http_conformance != AP_HTTP_CONFORMANCE_UNSAFE);
    008793
         int max_blank_lines = r->server->limit_req_fields;
    008793
     
    008793
         if (max_blank_lines <= 0) {
    008793
    @@ -588,7 +617,7 @@
    008793
              */
    008793
             r->the_request = NULL;
    008793
             rv = ap_rgetline(&(r->the_request), (apr_size_t)(r->server->limit_req_line + 2),
    008793
    -                         &len, r, 0, bb);
    008793
    +                         &len, r, strict ? AP_GETLINE_CRLF : 0, bb);
    008793
     
    008793
             if (rv != APR_SUCCESS) {
    008793
                 r->request_time = apr_time_now();
    008793
    @@ -599,8 +628,6 @@
    008793
                  */
    008793
                 if (APR_STATUS_IS_ENOSPC(rv)) {
    008793
                     r->status    = HTTP_REQUEST_URI_TOO_LARGE;
    008793
    -                r->proto_num = HTTP_VERSION(1,0);
    008793
    -                r->protocol  = apr_pstrdup(r->pool, "HTTP/1.0");
    008793
                 }
    008793
                 else if (APR_STATUS_IS_TIMEUP(rv)) {
    008793
                     r->status = HTTP_REQUEST_TIME_OUT;
    008793
    @@ -608,6 +635,8 @@
    008793
                 else if (APR_STATUS_IS_EINVAL(rv)) {
    008793
                     r->status = HTTP_BAD_REQUEST;
    008793
                 }
    008793
    +            r->proto_num = HTTP_VERSION(1,0);
    008793
    +            r->protocol  = apr_pstrdup(r->pool, "HTTP/1.0");
    008793
                 return 0;
    008793
             }
    008793
         } while ((len <= 0) && (++num_blank_lines < max_blank_lines));
    008793
    @@ -619,46 +648,263 @@
    008793
         }
    008793
     
    008793
         r->request_time = apr_time_now();
    008793
    -    ll = r->the_request;
    008793
    -    r->method = ap_getword_white(r->pool, &ll);
    008793
    +    r->method = r->the_request;
    008793
    +
    008793
    +    /* If there is whitespace before a method, skip it and mark in error */
    008793
    +    if (apr_isspace(*r->method)) {
    008793
    +        deferred_error = rrl_badwhitespace;
    008793
    +        for ( ; apr_isspace(*r->method); ++r->method)
    008793
    +            ;
    008793
    +    }
    008793
     
    008793
    -    uri = ap_getword_white(r->pool, &ll);
    008793
    +    /* Scan the method up to the next whitespace, ensure it contains only
    008793
    +     * valid http-token characters, otherwise mark in error
    008793
    +     */
    008793
    +    if (strict) {
    008793
    +        ll = (char*) ap_scan_http_token(r->method);
    008793
    +    }
    008793
    +    else {
    008793
    +        ll = (char*) ap_scan_vchar_obstext(r->method);
    008793
    +    }
    008793
     
    008793
    -    /* Provide quick information about the request method as soon as known */
    008793
    +    if (((ll == r->method) || (*ll && !apr_isspace(*ll)))
    008793
    +            && deferred_error == rrl_none) {
    008793
    +        deferred_error = rrl_badmethod;
    008793
    +        ll = strpbrk(ll, "\t\n\v\f\r ");
    008793
    +    }
    008793
     
    008793
    -    r->method_number = ap_method_number_of(r->method);
    008793
    -    if (r->method_number == M_GET && r->method[0] == 'H') {
    008793
    -        r->header_only = 1;
    008793
    +    /* Verify method terminated with a single SP, or mark as specific error */
    008793
    +    if (!ll) {
    008793
    +        if (deferred_error == rrl_none)
    008793
    +            deferred_error = rrl_missinguri;
    008793
    +        r->protocol = uri = "";
    008793
    +        len = 0;
    008793
    +        goto rrl_done;
    008793
    +    }
    008793
    +    else if (strict && ll[0] && apr_isspace(ll[1])
    008793
    +             && deferred_error == rrl_none) {
    008793
    +        deferred_error = rrl_excesswhitespace;
    008793
         }
    008793
     
    008793
    -    ap_parse_uri(r, uri);
    008793
    +    /* Advance uri pointer over leading whitespace, NUL terminate the method
    008793
    +     * If non-SP whitespace is encountered, mark as specific error
    008793
    +     */
    008793
    +    for (uri = ll; apr_isspace(*uri); ++uri)
    008793
    +        if (*uri != ' ' && deferred_error == rrl_none)
    008793
    +            deferred_error = rrl_badwhitespace;
    008793
    +    *ll = '\0';
    008793
    +
    008793
    +    if (!*uri && deferred_error == rrl_none)
    008793
    +        deferred_error = rrl_missinguri;
    008793
    +
    008793
    +    /* Scan the URI up to the next whitespace, ensure it contains no raw
    008793
    +     * control characters, otherwise mark in error
    008793
    +     */
    008793
    +    ll = (char*) ap_scan_vchar_obstext(uri);
    008793
    +    if (ll == uri || (*ll && !apr_isspace(*ll))) {
    008793
    +        deferred_error = rrl_baduri;
    008793
    +        ll = strpbrk(ll, "\t\n\v\f\r ");
    008793
    +    }
    008793
     
    008793
    -    if (ll[0]) {
    008793
    +    /* Verify URI terminated with a single SP, or mark as specific error */
    008793
    +    if (!ll) {
    008793
    +        r->protocol = "";
    008793
    +        len = 0;
    008793
    +        goto rrl_done;
    008793
    +    }
    008793
    +    else if (strict && ll[0] && apr_isspace(ll[1])
    008793
    +             && deferred_error == rrl_none) {
    008793
    +        deferred_error = rrl_excesswhitespace;
    008793
    +    }
    008793
    +
    008793
    +    /* Advance protocol pointer over leading whitespace, NUL terminate the uri
    008793
    +     * If non-SP whitespace is encountered, mark as specific error
    008793
    +     */
    008793
    +    for (r->protocol = ll; apr_isspace(*r->protocol); ++r->protocol)
    008793
    +        if (*r->protocol != ' ' && deferred_error == rrl_none)
    008793
    +            deferred_error = rrl_badwhitespace;
    008793
    +    *ll = '\0';
    008793
    +
    008793
    +    /* Scan the protocol up to the next whitespace, validation comes later */
    008793
    +    if (!(ll = (char*) ap_scan_vchar_obstext(r->protocol))) {
    008793
    +        len = strlen(r->protocol);
    008793
    +        goto rrl_done;
    008793
    +    }
    008793
    +    len = ll - r->protocol;
    008793
    +
    008793
    +    /* Advance over trailing whitespace, if found mark in error,
    008793
    +     * determine if trailing text is found, unconditionally mark in error,
    008793
    +     * finally NUL terminate the protocol string
    008793
    +     */
    008793
    +    if (*ll && !apr_isspace(*ll)) {
    008793
    +        deferred_error = rrl_badprotocol;
    008793
    +    }
    008793
    +    else if (strict && *ll) {
    008793
    +        deferred_error = rrl_excesswhitespace;
    008793
    +    }
    008793
    +    else {
    008793
    +        for ( ; apr_isspace(*ll); ++ll)
    008793
    +            if (*ll != ' ' && deferred_error == rrl_none)
    008793
    +                deferred_error = rrl_badwhitespace;
    008793
    +        if (*ll && deferred_error == rrl_none)
    008793
    +            deferred_error = rrl_trailingtext;
    008793
    +    }
    008793
    +    *((char *)r->protocol + len) = '\0';
    008793
    +
    008793
    +rrl_done:
    008793
    +    /* For internal integrety and palloc efficiency, reconstruct the_request
    008793
    +     * in one palloc, using only single SP characters, per spec.
    008793
    +     */
    008793
    +    r->the_request = apr_pstrcat(r->pool, r->method, *uri ? " " : NULL, uri,
    008793
    +                                 *r->protocol ? " " : NULL, r->protocol, NULL);
    008793
    +
    008793
    +    if (len == 8
    008793
    +            && r->protocol[0] == 'H' && r->protocol[1] == 'T'
    008793
    +            && r->protocol[2] == 'T' && r->protocol[3] == 'P'
    008793
    +            && r->protocol[4] == '/' && apr_isdigit(r->protocol[5])
    008793
    +            && r->protocol[6] == '.' && apr_isdigit(r->protocol[7])
    008793
    +            && r->protocol[5] != '0') {
    008793
    +        r->assbackwards = 0;
    008793
    +        r->proto_num = HTTP_VERSION(r->protocol[5] - '0', r->protocol[7] - '0');
    008793
    +    }
    008793
    +    else if (len == 8
    008793
    +                 && (r->protocol[0] == 'H' || r->protocol[0] == 'h')
    008793
    +                 && (r->protocol[1] == 'T' || r->protocol[1] == 't')
    008793
    +                 && (r->protocol[2] == 'T' || r->protocol[2] == 't')
    008793
    +                 && (r->protocol[3] == 'P' || r->protocol[3] == 'p')
    008793
    +                 && r->protocol[4] == '/' && apr_isdigit(r->protocol[5])
    008793
    +                 && r->protocol[6] == '.' && apr_isdigit(r->protocol[7])
    008793
    +                 && r->protocol[5] != '0') {
    008793
             r->assbackwards = 0;
    008793
    -        pro = ll;
    008793
    -        len = strlen(ll);
    008793
    -    } else {
    008793
    +        r->proto_num = HTTP_VERSION(r->protocol[5] - '0', r->protocol[7] - '0');
    008793
    +        if (strict && deferred_error == rrl_none)
    008793
    +            deferred_error = rrl_badprotocol;
    008793
    +        else
    008793
    +            memcpy((char*)r->protocol, "HTTP", 4);
    008793
    +    } else if (r->protocol[0]) {
    008793
    +        r->proto_num = HTTP_VERSION(0, 9);
    008793
    +        /* Defer setting the r->protocol string till error msg is composed */
    008793
    +        if (deferred_error == rrl_none)
    008793
    +            deferred_error = rrl_badprotocol;
    008793
    +    }
    008793
    +    else {
    008793
             r->assbackwards = 1;
    008793
    -        pro = "HTTP/0.9";
    008793
    -        len = 8;
    008793
    +        r->protocol  = apr_pstrdup(r->pool, "HTTP/0.9");
    008793
    +        r->proto_num = HTTP_VERSION(0, 9);
    008793
         }
    008793
    -    r->protocol = apr_pstrmemdup(r->pool, pro, len);
    008793
     
    008793
    -    /* Avoid sscanf in the common case */
    008793
    -    if (len == 8
    008793
    -        && pro[0] == 'H' && pro[1] == 'T' && pro[2] == 'T' && pro[3] == 'P'
    008793
    -        && pro[4] == '/' && apr_isdigit(pro[5]) && pro[6] == '.'
    008793
    -        && apr_isdigit(pro[7])) {
    008793
    -        r->proto_num = HTTP_VERSION(pro[5] - '0', pro[7] - '0');
    008793
    -    }
    008793
    -    else if (3 == sscanf(r->protocol, "%4s/%u.%u", http, &major, &minor)
    008793
    -             && (strcasecmp("http", http) == 0)
    008793
    -             && (minor < HTTP_VERSION(1, 0)) ) /* don't allow HTTP/0.1000 */
    008793
    -        r->proto_num = HTTP_VERSION(major, minor);
    008793
    -    else
    008793
    -        r->proto_num = HTTP_VERSION(1, 0);
    008793
    +    /* Determine the method_number and parse the uri prior to invoking error
    008793
    +     * handling, such that these fields are available for subsitution
    008793
    +     */
    008793
    +    r->method_number = ap_method_number_of(r->method);
    008793
    +    if (r->method_number == M_GET && r->method[0] == 'H')
    008793
    +        r->header_only = 1;
    008793
    +
    008793
    +    ap_parse_uri(r, uri);
    008793
    +
    008793
    +    /* With the request understood, we can consider HTTP/0.9 specific errors */
    008793
    +    if (r->proto_num == HTTP_VERSION(0, 9) && deferred_error == rrl_none) {
    008793
    +        if (conf->http09_enable == AP_HTTP09_DISABLE)
    008793
    +            deferred_error = rrl_reject09;
    008793
    +        else if (strict && (r->method_number != M_GET || r->header_only))
    008793
    +            deferred_error = rrl_badmethod09;
    008793
    +    }
    008793
    +
    008793
    +    /* Now that the method, uri and protocol are all processed,
    008793
    +     * we can safely resume any deferred error reporting
    008793
    +     */
    008793
    +    if (deferred_error != rrl_none) {
    008793
    +        if (deferred_error == rrl_badmethod)
    008793
    +            ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(03445)
    008793
    +                          "HTTP Request Line; Invalid method token: '%.*s'",
    008793
    +                          field_name_len(r->method), r->method);
    008793
    +        else if (deferred_error == rrl_badmethod09)
    008793
    +            ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(03444)
    008793
    +                          "HTTP Request Line; Invalid method token: '%.*s'"
    008793
    +                          " (only GET is allowed for HTTP/0.9 requests)",
    008793
    +                          field_name_len(r->method), r->method);
    008793
    +        else if (deferred_error == rrl_missinguri)
    008793
    +            ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(03446)
    008793
    +                          "HTTP Request Line; Missing URI");
    008793
    +        else if (deferred_error == rrl_baduri)
    008793
    +            ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(03454)
    008793
    +                          "HTTP Request Line; URI incorrectly encoded: '%.*s'",
    008793
    +                          field_name_len(r->uri), r->uri);
    008793
    +        else if (deferred_error == rrl_badwhitespace)
    008793
    +            ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(03447)
    008793
    +                          "HTTP Request Line; Invalid whitespace");
    008793
    +        else if (deferred_error == rrl_excesswhitespace)
    008793
    +            ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(03448)
    008793
    +                          "HTTP Request Line; Excess whitespace "
    008793
    +                          "(disallowed by HttpProtocolOptions Strict");
    008793
    +        else if (deferred_error == rrl_trailingtext)
    008793
    +            ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(03449)
    008793
    +                          "HTTP Request Line; Extraneous text found '%.*s' "
    008793
    +                          "(perhaps whitespace was injected?)",
    008793
    +                          field_name_len(ll), ll);
    008793
    +        else if (deferred_error == rrl_reject09)
    008793
    +            ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(02401)
    008793
    +                          "HTTP Request Line; Rejected HTTP/0.9 request");
    008793
    +        else if (deferred_error == rrl_badprotocol)
    008793
    +            ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(02418)
    008793
    +                          "HTTP Request Line; Unrecognized protocol '%.*s' "
    008793
    +                          "(perhaps whitespace was injected?)",
    008793
    +                          field_name_len(r->protocol), r->protocol);
    008793
    +        r->status = HTTP_BAD_REQUEST;
    008793
    +        goto rrl_failed;
    008793
    +    }
    008793
    +
    008793
    +    if (conf->http_methods == AP_HTTP_METHODS_REGISTERED
    008793
    +            && r->method_number == M_INVALID) {
    008793
    +        ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(02423)
    008793
    +                      "HTTP Request Line; Unrecognized HTTP method: '%.*s' "
    008793
    +                      "(disallowed by RegisteredMethods)",
    008793
    +                      field_name_len(r->method), r->method);
    008793
    +        r->status = HTTP_NOT_IMPLEMENTED;
    008793
    +        /* This can't happen in an HTTP/0.9 request, we verified GET above */
    008793
    +        return 0;
    008793
    +    }
    008793
    +
    008793
    +    if (r->status != HTTP_OK) {
    008793
    +        ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(03450)
    008793
    +                      "HTTP Request Line; Unable to parse URI: '%.*s'",
    008793
    +                      field_name_len(r->uri), r->uri);
    008793
    +        goto rrl_failed;
    008793
    +    }
    008793
    +
    008793
    +    if (strict) {
    008793
    +        if (r->parsed_uri.fragment) {
    008793
    +            /* RFC3986 3.5: no fragment */
    008793
    +            ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(02421)
    008793
    +                          "HTTP Request Line; URI must not contain a fragment");
    008793
    +            r->status = HTTP_BAD_REQUEST;
    008793
    +            goto rrl_failed;
    008793
    +        }
    008793
    +        if (r->parsed_uri.user || r->parsed_uri.password) {
    008793
    +            ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(02422)
    008793
    +                          "HTTP Request Line; URI must not contain a "
    008793
    +                          "username/password");
    008793
    +            r->status = HTTP_BAD_REQUEST;
    008793
    +            goto rrl_failed;
    008793
    +        }
    008793
    +    }
    008793
     
    008793
         return 1;
    008793
    +rrl_failed:
    008793
    +    if (r->proto_num == HTTP_VERSION(0, 9)) {
    008793
    +        /* Send all parsing and protocol error response with 1.x behavior,
    008793
    +         * and reserve 505 errors for actual HTTP protocols presented.
    008793
    +         * As called out in RFC7230 3.5, any errors parsing the protocol
    008793
    +         * from the request line are nearly always misencoded HTTP/1.x
    008793
    +         * requests. Only a valid 0.9 request with no parsing errors
    008793
    +         * at all may be treated as a simple request, if allowed.
    008793
    +         */
    008793
    +        r->assbackwards = 0;
    008793
    +        r->connection->keepalive = AP_CONN_CLOSE;
    008793
    +        r->proto_num = HTTP_VERSION(1, 0);
    008793
    +        r->protocol  = apr_pstrdup(r->pool, "HTTP/1.0");
    008793
    +    }
    008793
    +    return 0;
    008793
     }
    008793
     
    008793
     static int table_do_fn_check_lengths(void *r_, const char *key,
    008793
    @@ -670,26 +916,13 @@
    008793
     
    008793
         r->status = HTTP_BAD_REQUEST;
    008793
         apr_table_setn(r->notes, "error-notes",
    008793
    -                   apr_pstrcat(r->pool, "Size of a request header field "
    008793
    -                               "after merging exceeds server limit.
    "
    008793
    -                               "\n
    \n",
    008793
    -                               ap_escape_html(r->pool, key),
    008793
    -                               "\n", NULL));
    008793
    -    ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(00560) "Request header "
    008793
    -                  "exceeds LimitRequestFieldSize after merging: %s", key);
    008793
    +                   "Size of a request header field exceeds server limit.");
    008793
    +    ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(00560) "Request "
    008793
    +                  "header exceeds LimitRequestFieldSize after merging: %.*s",
    008793
    +                  field_name_len(key), key);
    008793
         return 0;
    008793
     }
    008793
     
    008793
    -/* get the length of the field name for logging, but no more than 80 bytes */
    008793
    -#define LOG_NAME_MAX_LEN 80
    008793
    -static int field_name_len(const char *field)
    008793
    -{
    008793
    -    const char *end = ap_strchr_c(field, ':');
    008793
    -    if (end == NULL || end - field > LOG_NAME_MAX_LEN)
    008793
    -        return LOG_NAME_MAX_LEN;
    008793
    -    return end - field;
    008793
    -}
    008793
    -
    008793
     AP_DECLARE(void) ap_get_mime_headers_core(request_rec *r, apr_bucket_brigade *bb)
    008793
     {
    008793
         char *last_field = NULL;
    008793
    @@ -700,6 +933,8 @@
    008793
         apr_size_t len;
    008793
         int fields_read = 0;
    008793
         char *tmp_field;
    008793
    +    core_server_config *conf = ap_get_core_module_config(r->server->module_config);
    008793
    +    int strict = (conf->http_conformance != AP_HTTP_CONFORMANCE_UNSAFE);
    008793
     
    008793
         /*
    008793
          * Read header lines until we get the empty separator line, a read error,
    008793
    @@ -707,11 +942,10 @@
    008793
          */
    008793
         while(1) {
    008793
             apr_status_t rv;
    008793
    -        int folded = 0;
    008793
     
    008793
             field = NULL;
    008793
             rv = ap_rgetline(&field, r->server->limit_req_fieldsize + 2,
    008793
    -                         &len, r, 0, bb);
    008793
    +                         &len, r, strict ? AP_GETLINE_CRLF : 0, bb);
    008793
     
    008793
             if (rv != APR_SUCCESS) {
    008793
                 if (APR_STATUS_IS_TIMEUP(rv)) {
    008793
    @@ -728,153 +962,217 @@
    008793
                  * exceeds the configured limit for a field size.
    008793
                  */
    008793
                 if (rv == APR_ENOSPC) {
    008793
    -                const char *field_escaped;
    008793
    -                if (field) {
    008793
    -                    /* ensure ap_escape_html will terminate correctly */
    008793
    -                    field[len - 1] = '\0';
    008793
    -                    field_escaped = ap_escape_html(r->pool, field);
    008793
    -                }
    008793
    -                else {
    008793
    -                    field_escaped = field = "";
    008793
    -                }
    008793
    -
    008793
                     apr_table_setn(r->notes, "error-notes",
    008793
    -                               apr_psprintf(r->pool,
    008793
    -                                           "Size of a request header field "
    008793
    -                                           "exceeds server limit.
    \n"
    008793
    -                                           "
    \n%.*s\n
    \n",
    008793
    -                                           field_name_len(field_escaped),
    008793
    -                                           field_escaped));
    008793
    +                               "Size of a request header field "
    008793
    +                               "exceeds server limit.");
    008793
                     ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(00561)
    008793
                                   "Request header exceeds LimitRequestFieldSize%s"
    008793
                                   "%.*s",
    008793
    -                              *field ? ": " : "",
    008793
    -                              field_name_len(field), field);
    008793
    +                              (field && *field) ? ": " : "",
    008793
    +                              (field) ? field_name_len(field) : 0,
    008793
    +                              (field) ? field : "");
    008793
                 }
    008793
                 return;
    008793
             }
    008793
     
    008793
    -        if (last_field != NULL) {
    008793
    -            if ((len > 0) && ((*field == '\t') || *field == ' ')) {
    008793
    -                /* This line is a continuation of the preceding line(s),
    008793
    -                 * so append it to the line that we've set aside.
    008793
    -                 * Note: this uses a power-of-two allocator to avoid
    008793
    -                 * doing O(n) allocs and using O(n^2) space for
    008793
    -                 * continuations that span many many lines.
    008793
    -                 */
    008793
    -                apr_size_t fold_len = last_len + len + 1; /* trailing null */
    008793
     
    008793
    -                if (fold_len >= (apr_size_t)(r->server->limit_req_fieldsize)) {
    008793
    -                    r->status = HTTP_BAD_REQUEST;
    008793
    -                    /* report what we have accumulated so far before the
    008793
    -                     * overflow (last_field) as the field with the problem
    008793
    -                     */
    008793
    -                    apr_table_setn(r->notes, "error-notes",
    008793
    -                                   apr_psprintf(r->pool,
    008793
    -                                               "Size of a request header field "
    008793
    -                                               "after folding "
    008793
    -                                               "exceeds server limit.
    \n"
    008793
    -                                               "
    \n%.*s\n
    \n",
    008793
    -                                               field_name_len(last_field), 
    008793
    -                                               ap_escape_html(r->pool, last_field)));
    008793
    -                    ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(00562)
    008793
    -                                  "Request header exceeds LimitRequestFieldSize "
    008793
    -                                  "after folding: %.*s",
    008793
    -                                  field_name_len(last_field), last_field);
    008793
    -                    return;
    008793
    -                }
    008793
    +        /* For all header values, and all obs-fold lines, the presence of
    008793
    +         * additional whitespace is a no-op, so collapse trailing whitespace
    008793
    +         * to save buffer allocation and optimize copy operations.
    008793
    +         * Do not remove the last single whitespace under any condition.
    008793
    +         */
    008793
    +        while (len > 1 && (field[len-1] == '\t' || field[len-1] == ' ')) {
    008793
    +            field[--len] = '\0';
    008793
    +        }
    008793
    +
    008793
    +        if (*field == '\t' || *field == ' ') {
    008793
    +            /* Append any newly-read obs-fold line onto the preceding
    008793
    +             * last_field line we are processing
    008793
    +             */
    008793
    +            apr_size_t fold_len;
    008793
     
    008793
    +            if (last_field == NULL) {
    008793
    +                r->status = HTTP_BAD_REQUEST;
    008793
    +                ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(03442)
    008793
    +                              "Line folding encountered before first"
    008793
    +                              " header line");
    008793
    +                return;
    008793
    +            }
    008793
    +
    008793
    +            if (field[1] == '\0') {
    008793
    +                r->status = HTTP_BAD_REQUEST;
    008793
    +                ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(03443)
    008793
    +                              "Empty folded line encountered");
    008793
    +                return;
    008793
    +            }
    008793
    +
    008793
    +            /* Leading whitespace on an obs-fold line can be
    008793
    +             * similarly discarded */
    008793
    +            while (field[1] == '\t' || field[1] == ' ') {
    008793
    +                ++field; --len;
    008793
    +            }
    008793
    +
    008793
    +            /* This line is a continuation of the preceding line(s),
    008793
    +             * so append it to the line that we've set aside.
    008793
    +             * Note: this uses a power-of-two allocator to avoid
    008793
    +             * doing O(n) allocs and using O(n^2) space for
    008793
    +             * continuations that span many many lines.
    008793
    +             */
    008793
    +            fold_len = last_len + len + 1; /* trailing null */
    008793
    +
    008793
    +            if (fold_len >= (apr_size_t)(r->server->limit_req_fieldsize)) {
    008793
    +                r->status = HTTP_BAD_REQUEST;
    008793
    +                /* report what we have accumulated so far before the
    008793
    +                 * overflow (last_field) as the field with the problem
    008793
    +                 */
    008793
    +                apr_table_setn(r->notes, "error-notes",
    008793
    +                               "Size of a request header field "
    008793
    +                               "exceeds server limit.");
    008793
    +                ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(00562)
    008793
    +                              "Request header exceeds LimitRequestFieldSize "
    008793
    +                              "after folding: %.*s",
    008793
    +                              field_name_len(last_field), last_field);
    008793
    +                return;
    008793
    +            }
    008793
    +
    008793
    +            if (fold_len > alloc_len) {
    008793
    +                char *fold_buf;
    008793
    +                alloc_len += alloc_len;
    008793
                     if (fold_len > alloc_len) {
    008793
    -                    char *fold_buf;
    008793
    -                    alloc_len += alloc_len;
    008793
    -                    if (fold_len > alloc_len) {
    008793
    -                        alloc_len = fold_len;
    008793
    -                    }
    008793
    -                    fold_buf = (char *)apr_palloc(r->pool, alloc_len);
    008793
    -                    memcpy(fold_buf, last_field, last_len);
    008793
    -                    last_field = fold_buf;
    008793
    +                    alloc_len = fold_len;
    008793
                     }
    008793
    -                memcpy(last_field + last_len, field, len +1); /* +1 for nul */
    008793
    -                last_len += len;
    008793
    -                folded = 1;
    008793
    -            }
    008793
    -            else /* not a continuation line */ {
    008793
    +                fold_buf = (char *)apr_palloc(r->pool, alloc_len);
    008793
    +                memcpy(fold_buf, last_field, last_len);
    008793
    +                last_field = fold_buf;
    008793
    +            }
    008793
    +            memcpy(last_field + last_len, field, len +1); /* +1 for nul */
    008793
    +            /* Replace obs-fold w/ SP per RFC 7230 3.2.4 */
    008793
    +            last_field[last_len] = ' ';
    008793
    +            last_len += len;
    008793
     
    008793
    -                if (r->server->limit_req_fields
    008793
    +            /* We've appended this obs-fold line to last_len, proceed to
    008793
    +             * read the next input line
    008793
    +             */
    008793
    +            continue;
    008793
    +        }
    008793
    +        else if (last_field != NULL) {
    008793
    +            /* Process the previous last_field header line with all obs-folded
    008793
    +             * segments already concatinated (this is not operating on the
    008793
    +             * most recently read input line).
    008793
    +             */
    008793
    +            if (r->server->limit_req_fields
    008793
                         && (++fields_read > r->server->limit_req_fields)) {
    008793
    -                    r->status = HTTP_BAD_REQUEST;
    008793
    -                    apr_table_setn(r->notes, "error-notes",
    008793
    -                                   "The number of request header fields "
    008793
    -                                   "exceeds this server's limit.");
    008793
    -                    ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(00563)
    008793
    -                                  "Number of request headers exceeds "
    008793
    -                                  "LimitRequestFields");
    008793
    -                    return;
    008793
    -                }
    008793
    +                r->status = HTTP_BAD_REQUEST;
    008793
    +                apr_table_setn(r->notes, "error-notes",
    008793
    +                               "The number of request header fields "
    008793
    +                               "exceeds this server's limit.");
    008793
    +                ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(00563)
    008793
    +                               "Number of request headers exceeds "
    008793
    +                               "LimitRequestFields");
    008793
    +                return;
    008793
    +            }
    008793
     
    008793
    -                if (!(value = strchr(last_field, ':'))) { /* Find ':' or    */
    008793
    -                    r->status = HTTP_BAD_REQUEST;      /* abort bad request */
    008793
    -                    apr_table_setn(r->notes, "error-notes",
    008793
    -                                   apr_psprintf(r->pool,
    008793
    -                                               "Request header field is "
    008793
    -                                               "missing ':' separator.
    \n"
    008793
    -                                               "
    \n%.*s
    \n",
    008793
    -                                               (int)LOG_NAME_MAX_LEN,
    008793
    -                                               ap_escape_html(r->pool,
    008793
    -                                                              last_field)));
    008793
    -                    ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(00564)
    008793
    +            if (!strict)
    008793
    +            {
    008793
    +                /* Not Strict ('Unsafe' mode), using the legacy parser */
    008793
    +
    008793
    +                if (!(value = strchr(last_field, ':'))) { /* Find ':' or */
    008793
    +                    r->status = HTTP_BAD_REQUEST;   /* abort bad request */
    008793
    +                    ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00564)
    008793
                                       "Request header field is missing ':' "
    008793
                                       "separator: %.*s", (int)LOG_NAME_MAX_LEN,
    008793
                                       last_field);
    008793
    +
    008793
                         return;
    008793
                     }
    008793
     
    008793
    -                tmp_field = value - 1; /* last character of field-name */
    008793
    +                /* last character of field-name */
    008793
    +                tmp_field = value - (value > last_field ? 1 : 0);
    008793
     
    008793
                     *value++ = '\0'; /* NUL-terminate at colon */
    008793
     
    008793
    +                if (strpbrk(last_field, "\t\n\v\f\r ")) {
    008793
    +                    r->status = HTTP_BAD_REQUEST;
    008793
    +                    ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(03452)
    008793
    +                                  "Request header field name presented"
    008793
    +                                  " invalid whitespace");
    008793
    +                    return;
    008793
    +                }
    008793
    +
    008793
                     while (*value == ' ' || *value == '\t') {
    008793
    -                    ++value;            /* Skip to start of value   */
    008793
    +                     ++value;            /* Skip to start of value   */
    008793
    +                }
    008793
    +
    008793
    +                if (strpbrk(value, "\n\v\f\r")) {
    008793
    +                    r->status = HTTP_BAD_REQUEST;
    008793
    +                    ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(03451)
    008793
    +                                  "Request header field value presented"
    008793
    +                                  " bad whitespace");
    008793
    +                    return;
    008793
                     }
    008793
     
    008793
    -                /* Strip LWS after field-name: */
    008793
    -                while (tmp_field > last_field
    008793
    -                       && (*tmp_field == ' ' || *tmp_field == '\t')) {
    008793
    -                    *tmp_field-- = '\0';
    008793
    +                if (tmp_field == last_field) {
    008793
    +                    r->status = HTTP_BAD_REQUEST;
    008793
    +                    ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(03453)
    008793
    +                                  "Request header field name was empty");
    008793
    +                    return;
    008793
    +                }
    008793
    +            }
    008793
    +            else /* Using strict RFC7230 parsing */
    008793
    +            {
    008793
    +                /* Ensure valid token chars before ':' per RFC 7230 3.2.4 */
    008793
    +                value = (char *)ap_scan_http_token(last_field);
    008793
    +                if ((value == last_field) || *value != ':') {
    008793
    +                    r->status = HTTP_BAD_REQUEST;
    008793
    +                    ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(02426)
    008793
    +                                  "Request header field name is malformed: "
    008793
    +                                  "%.*s", (int)LOG_NAME_MAX_LEN, last_field);
    008793
    +                    return;
    008793
                     }
    008793
     
    008793
    -                /* Strip LWS after field-value: */
    008793
    -                tmp_field = last_field + last_len - 1;
    008793
    -                while (tmp_field > value
    008793
    -                       && (*tmp_field == ' ' || *tmp_field == '\t')) {
    008793
    -                    *tmp_field-- = '\0';
    008793
    +                *value++ = '\0'; /* NUL-terminate last_field name at ':' */
    008793
    +
    008793
    +                while (*value == ' ' || *value == '\t') {
    008793
    +                    ++value;     /* Skip LWS of value */
    008793
                     }
    008793
     
    008793
    -                apr_table_addn(r->headers_in, last_field, value);
    008793
    +                /* Find invalid, non-HT ctrl char, or the trailing NULL */
    008793
    +                tmp_field = (char *)ap_scan_http_field_content(value);
    008793
     
    008793
    -                /* reset the alloc_len so that we'll allocate a new
    008793
    -                 * buffer if we have to do any more folding: we can't
    008793
    -                 * use the previous buffer because its contents are
    008793
    -                 * now part of r->headers_in
    008793
    +                /* Reject value for all garbage input (CTRLs excluding HT)
    008793
    +                 * e.g. only VCHAR / SP / HT / obs-text are allowed per
    008793
    +                 * RFC7230 3.2.6 - leave all more explicit rule enforcement
    008793
    +                 * for specific header handler logic later in the cycle
    008793
                      */
    008793
    -                alloc_len = 0;
    008793
    +                if (*tmp_field != '\0') {
    008793
    +                    r->status = HTTP_BAD_REQUEST;
    008793
    +                    ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(02427)
    008793
    +                                  "Request header value is malformed: "
    008793
    +                                  "%.*s", (int)LOG_NAME_MAX_LEN, value);
    008793
    +                    return;
    008793
    +                }
    008793
    +            }
    008793
    +
    008793
    +            apr_table_addn(r->headers_in, last_field, value);
    008793
     
    008793
    -            } /* end if current line is not a continuation starting with tab */
    008793
    +            /* This last_field header is now stored in headers_in,
    008793
    +             * resume processing of the current input line.
    008793
    +             */
    008793
             }
    008793
     
    008793
    -        /* Found a blank line, stop. */
    008793
    +        /* Found the terminating empty end-of-headers line, stop. */
    008793
             if (len == 0) {
    008793
                 break;
    008793
             }
    008793
     
    008793
    -        /* Keep track of this line so that we can parse it on
    008793
    -         * the next loop iteration.  (In the folded case, last_field
    008793
    -         * has been updated already.)
    008793
    +        /* Keep track of this new header line so that we can extend it across
    008793
    +         * any obs-fold or parse it on the next loop iteration. We referenced
    008793
    +         * our previously allocated buffer in r->headers_in,
    008793
    +         * so allocate a fresh buffer if required.
    008793
              */
    008793
    -        if (!folded) {
    008793
    -            last_field = field;
    008793
    -            last_len = len;
    008793
    -        }
    008793
    +        alloc_len = 0;
    008793
    +        last_field = field;
    008793
    +        last_len = len;
    008793
         }
    008793
     
    008793
         /* Combine multiple message-header fields with the same
    008793
    @@ -899,7 +1197,7 @@
    008793
         request_rec *r;
    008793
         apr_pool_t *p;
    008793
         const char *expect;
    008793
    -    int access_status = HTTP_OK;
    008793
    +    int access_status;
    008793
         apr_bucket_brigade *tmp_bb;
    008793
         apr_socket_t *csd;
    008793
         apr_interval_time_t cur_timeout;
    008793
    @@ -958,35 +1256,39 @@
    008793
     
    008793
         /* Get the request... */
    008793
         if (!read_request_line(r, tmp_bb)) {
    008793
    -        if (r->status == HTTP_REQUEST_URI_TOO_LARGE
    008793
    -            || r->status == HTTP_BAD_REQUEST) {
    008793
    +        switch (r->status) {
    008793
    +        case HTTP_REQUEST_URI_TOO_LARGE:
    008793
    +        case HTTP_BAD_REQUEST:
    008793
    +        case HTTP_VERSION_NOT_SUPPORTED:
    008793
    +        case HTTP_NOT_IMPLEMENTED:
    008793
                 if (r->status == HTTP_REQUEST_URI_TOO_LARGE) {
    008793
                     ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(00565)
    008793
                                   "request failed: client's request-line exceeds LimitRequestLine (longer than %d)",
    008793
                                   r->server->limit_req_line);
    008793
                 }
    008793
                 else if (r->method == NULL) {
    008793
    -                ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(00566)
    008793
    -                              "request failed: invalid characters in URI");
    008793
    +                ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00566)
    008793
    +                              "request failed: malformed request line");
    008793
                 }
    008793
    -            ap_send_error_response(r, 0);
    008793
    +            access_status = r->status;
    008793
    +            r->status = HTTP_OK;
    008793
    +            ap_die(access_status, r);
    008793
                 ap_update_child_status(conn->sbh, SERVER_BUSY_LOG, r);
    008793
                 ap_run_log_transaction(r);
    008793
    +            r = NULL;
    008793
                 apr_brigade_destroy(tmp_bb);
    008793
                 goto traceout;
    008793
    -        }
    008793
    -        else if (r->status == HTTP_REQUEST_TIME_OUT) {
    008793
    -            ap_update_child_status(conn->sbh, SERVER_BUSY_LOG, r);
    008793
    -            if (!r->connection->keepalives) {
    008793
    +        case HTTP_REQUEST_TIME_OUT:
    008793
    +            ap_update_child_status(conn->sbh, SERVER_BUSY_LOG, NULL);
    008793
    +            if (!r->connection->keepalives)
    008793
                     ap_run_log_transaction(r);
    008793
    -            }
    008793
                 apr_brigade_destroy(tmp_bb);
    008793
                 goto traceout;
    008793
    +        default:
    008793
    +            apr_brigade_destroy(tmp_bb);
    008793
    +            r = NULL;
    008793
    +            goto traceout;
    008793
             }
    008793
    -
    008793
    -        apr_brigade_destroy(tmp_bb);
    008793
    -        r = NULL;
    008793
    -        goto traceout;
    008793
         }
    008793
     
    008793
         /* We may have been in keep_alive_timeout mode, so toggle back
    008793
    @@ -1003,7 +1305,7 @@
    008793
         if (!r->assbackwards) {
    008793
             ap_get_mime_headers_core(r, tmp_bb);
    008793
             if (r->status != HTTP_OK) {
    008793
    -            ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(00567)
    008793
    +            ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00567)
    008793
                               "request failed: error reading the headers");
    008793
                 ap_send_error_response(r, 0);
    008793
                 ap_update_child_status(conn->sbh, SERVER_BUSY_LOG, r);
    008793
    @@ -1021,25 +1323,6 @@
    008793
                 apr_table_unset(r->headers_in, "Content-Length");
    008793
             }
    008793
         }
    008793
    -    else {
    008793
    -        if (r->header_only) {
    008793
    -            /*
    008793
    -             * Client asked for headers only with HTTP/0.9, which doesn't send
    008793
    -             * headers! Have to dink things just to make sure the error message
    008793
    -             * comes through...
    008793
    -             */
    008793
    -            ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(00568)
    008793
    -                          "client sent invalid HTTP/0.9 request: HEAD %s",
    008793
    -                          r->uri);
    008793
    -            r->header_only = 0;
    008793
    -            r->status = HTTP_BAD_REQUEST;
    008793
    -            ap_send_error_response(r, 0);
    008793
    -            ap_update_child_status(conn->sbh, SERVER_BUSY_LOG, r);
    008793
    -            ap_run_log_transaction(r);
    008793
    -            apr_brigade_destroy(tmp_bb);
    008793
    -            goto traceout;
    008793
    -        }
    008793
    -    }
    008793
     
    008793
         apr_brigade_destroy(tmp_bb);
    008793
     
    008793
    @@ -1071,7 +1354,7 @@
    008793
              * a Host: header, and the server MUST respond with 400 if it doesn't.
    008793
              */
    008793
             access_status = HTTP_BAD_REQUEST;
    008793
    -        ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(00569)
    008793
    +        ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00569)
    008793
                           "client sent HTTP/1.1 request without hostname "
    008793
                           "(see RFC2616 section 14.23): %s", r->uri);
    008793
         }
    008793
    diff -uap httpd-2.4.6/server/util.c.cve8743 httpd-2.4.6/server/util.c
    008793
    --- httpd-2.4.6/server/util.c.cve8743
    008793
    +++ httpd-2.4.6/server/util.c
    008793
    @@ -79,7 +79,7 @@
    008793
      * char in here and get it to work, because if char is signed then it
    008793
      * will first be sign extended.
    008793
      */
    008793
    -#define TEST_CHAR(c, f)        (test_char_table[(unsigned)(c)] & (f))
    008793
    +#define TEST_CHAR(c, f)        (test_char_table[(unsigned char)(c)] & (f))
    008793
     
    008793
     /* Win32/NetWare/OS2 need to check for both forward and back slashes
    008793
      * in ap_getparents() and ap_escape_url.
    008793
    @@ -1449,6 +1449,37 @@
    008793
         return find_list_item(p, line, tok, AP_ETAG_WEAK);
    008793
     }
    008793
     
    008793
    +/* Scan a string for HTTP VCHAR/obs-text characters including HT and SP
    008793
    + * (as used in header values, for example, in RFC 7230 section 3.2)
    008793
    + * returning the pointer to the first non-HT ASCII ctrl character.
    008793
    + */
    008793
    +AP_DECLARE(const char *) ap_scan_http_field_content(const char *ptr)
    008793
    +{
    008793
    +    for ( ; !TEST_CHAR(*ptr, T_HTTP_CTRLS); ++ptr) ;
    008793
    +
    008793
    +    return ptr;
    008793
    +}
    008793
    +
    008793
    +/* Scan a string for HTTP token characters, returning the pointer to
    008793
    + * the first non-token character.
    008793
    + */
    008793
    +AP_DECLARE(const char *) ap_scan_http_token(const char *ptr)
    008793
    +{
    008793
    +    for ( ; !TEST_CHAR(*ptr, T_HTTP_TOKEN_STOP); ++ptr) ;
    008793
    +
    008793
    +    return ptr;
    008793
    +}
    008793
    +
    008793
    +/* Scan a string for visible ASCII (0x21-0x7E) or obstext (0x80+)
    008793
    + * and return a pointer to the first ctrl/space character encountered.
    008793
    + */
    008793
    +AP_DECLARE(const char *) ap_scan_vchar_obstext(const char *ptr)
    008793
    +{
    008793
    +    for ( ; TEST_CHAR(*ptr, T_VCHAR_OBSTEXT); ++ptr) ;
    008793
    +
    008793
    +    return ptr;
    008793
    +}
    008793
    +
    008793
     /* Retrieve a token, spacing over it and returning a pointer to
    008793
      * the first non-white byte afterwards.  Note that these tokens
    008793
      * are delimited by semis and commas; and can also be delimited
    008793
    diff -uap httpd-2.4.6/server/vhost.c.cve8743 httpd-2.4.6/server/vhost.c
    008793
    --- httpd-2.4.6/server/vhost.c.cve8743
    008793
    +++ httpd-2.4.6/server/vhost.c
    008793
    @@ -685,6 +685,116 @@
    008793
      * run-time vhost matching functions
    008793
      */
    008793
     
    008793
    +static apr_status_t fix_hostname_v6_literal(request_rec *r, char *host)
    008793
    +{
    008793
    +    char *dst;
    008793
    +    int double_colon = 0;
    008793
    +
    008793
    +    for (dst = host; *dst; dst++) {
    008793
    +        if (apr_isxdigit(*dst)) {
    008793
    +            if (apr_isupper(*dst)) {
    008793
    +                *dst = apr_tolower(*dst);
    008793
    +            }
    008793
    +        }
    008793
    +        else if (*dst == ':') {
    008793
    +            if (*(dst + 1) == ':') {
    008793
    +                if (double_colon)
    008793
    +                    return APR_EINVAL;
    008793
    +                double_colon = 1;
    008793
    +            }
    008793
    +            else if (*(dst + 1) == '.') {
    008793
    +                return APR_EINVAL;
    008793
    +            }
    008793
    +        }
    008793
    +        else if (*dst == '.') {
    008793
    +            /* For IPv4-mapped IPv6 addresses like ::FFFF:129.144.52.38 */
    008793
    +            if (*(dst + 1) == ':' || *(dst + 1) == '.')
    008793
    +                return APR_EINVAL;
    008793
    +        }
    008793
    +        else {
    008793
    +            return APR_EINVAL;
    008793
    +        }
    008793
    +    }
    008793
    +    return APR_SUCCESS;
    008793
    +}
    008793
    +
    008793
    +static apr_status_t fix_hostname_non_v6(request_rec *r, char *host)
    008793
    +{
    008793
    +    char *dst;
    008793
    +
    008793
    +    for (dst = host; *dst; dst++) {
    008793
    +        if (apr_islower(*dst)) {
    008793
    +            /* leave char unchanged */
    008793
    +        }
    008793
    +        else if (*dst == '.') {
    008793
    +            if (*(dst + 1) == '.') {
    008793
    +                return APR_EINVAL;
    008793
    +            }
    008793
    +        }
    008793
    +        else if (apr_isupper(*dst)) {
    008793
    +            *dst = apr_tolower(*dst);
    008793
    +        }
    008793
    +        else if (*dst == '/' || *dst == '\\') {
    008793
    +            return APR_EINVAL;
    008793
    +        }
    008793
    +    }
    008793
    +    /* strip trailing gubbins */
    008793
    +    if (dst > host && dst[-1] == '.') {
    008793
    +        dst[-1] = '\0';
    008793
    +    }
    008793
    +    return APR_SUCCESS;
    008793
    +}
    008793
    +
    008793
    +/*
    008793
    + * If strict mode ever becomes the default, this should be folded into
    008793
    + * fix_hostname_non_v6()
    008793
    + */
    008793
    +static apr_status_t strict_hostname_check(request_rec *r, char *host)
    008793
    +{
    008793
    +    char *ch;
    008793
    +    int is_dotted_decimal = 1, leading_zeroes = 0, dots = 0;
    008793
    +
    008793
    +    for (ch = host; *ch; ch++) {
    008793
    +        if (!apr_isascii(*ch)) {
    008793
    +            goto bad;
    008793
    +        }
    008793
    +        else if (apr_isalpha(*ch) || *ch == '-') {
    008793
    +            is_dotted_decimal = 0;
    008793
    +        }
    008793
    +        else if (ch[0] == '.') {
    008793
    +            dots++;
    008793
    +            if (ch[1] == '0' && apr_isdigit(ch[2]))
    008793
    +                leading_zeroes = 1;
    008793
    +        }
    008793
    +        else if (!apr_isdigit(*ch)) {
    008793
    +           /* also takes care of multiple Host headers by denying commas */
    008793
    +            goto bad;
    008793
    +        }
    008793
    +    }
    008793
    +    if (is_dotted_decimal) {
    008793
    +        if (host[0] == '.' || (host[0] == '0' && apr_isdigit(host[1])))
    008793
    +            leading_zeroes = 1;
    008793
    +        if (leading_zeroes || dots != 3) {
    008793
    +            /* RFC 3986 7.4 */
    008793
    +            goto bad;
    008793
    +        }
    008793
    +    }
    008793
    +    else {
    008793
    +        /* The top-level domain must start with a letter (RFC 1123 2.1) */
    008793
    +        while (ch > host && *ch != '.')
    008793
    +            ch--;
    008793
    +        if (ch[0] == '.' && ch[1] != '\0' && !apr_isalpha(ch[1]))
    008793
    +            goto bad;
    008793
    +    }
    008793
    +    return APR_SUCCESS;
    008793
    +
    008793
    +bad:
    008793
    +    ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(02415)
    008793
    +                  "[strict] Invalid host name '%s'%s%.6s",
    008793
    +                  host, *ch ? ", problem near: " : "", ch);
    008793
    +    return APR_EINVAL;
    008793
    +}
    008793
    +
    008793
     /* Lowercase and remove any trailing dot and/or :port from the hostname,
    008793
      * and check that it is sane.
    008793
      *
    008793
    @@ -698,79 +808,90 @@
    008793
      * Instead we just check for filesystem metacharacters: directory
    008793
      * separators / and \ and sequences of more than one dot.
    008793
      */
    008793
    -static void fix_hostname(request_rec *r)
    008793
    +static int fix_hostname(request_rec *r, const char *host_header,
    008793
    +                        unsigned http_conformance)
    008793
     {
    008793
    +    const char *src;
    008793
         char *host, *scope_id;
    008793
    -    char *dst;
    008793
         apr_port_t port;
    008793
         apr_status_t rv;
    008793
         const char *c;
    008793
    +    int is_v6literal = 0;
    008793
    +    int strict = (http_conformance != AP_HTTP_CONFORMANCE_UNSAFE);
    008793
     
    008793
    -    /* According to RFC 2616, Host header field CAN be blank. */
    008793
    -    if (!*r->hostname) {
    008793
    -        return;
    008793
    +    src = host_header ? host_header : r->hostname;
    008793
    +
    008793
    +    /* According to RFC 2616, Host header field CAN be blank */
    008793
    +    if (!*src) {
    008793
    +        return is_v6literal;
    008793
         }
    008793
     
    008793
         /* apr_parse_addr_port will interpret a bare integer as a port
    008793
          * which is incorrect in this context.  So treat it separately.
    008793
          */
    008793
    -    for (c = r->hostname; apr_isdigit(*c); ++c);
    008793
    -    if (!*c) {  /* pure integer */
    008793
    -        return;
    008793
    +    for (c = src; apr_isdigit(*c); ++c);
    008793
    +    if (!*c) {
    008793
    +        /* pure integer */
    008793
    +        if (strict) {
    008793
    +            /* RFC 3986 7.4 */
    008793
    +            ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(02416)
    008793
    +                         "[strict] purely numeric host names not allowed: %s",
    008793
    +                         src);
    008793
    +            goto bad_nolog;
    008793
    +        }
    008793
    +        r->hostname = src;
    008793
    +        return is_v6literal;
    008793
    +    }
    008793
    +
    008793
    +    if (host_header) {
    008793
    +        rv = apr_parse_addr_port(&host, &scope_id, &port, src, r->pool);
    008793
    +        if (rv != APR_SUCCESS || scope_id)
    008793
    +            goto bad;
    008793
    +        if (port) {
    008793
    +            /* Don't throw the Host: header's port number away:
    008793
    +               save it in parsed_uri -- ap_get_server_port() needs it! */
    008793
    +            /* @@@ XXX there should be a better way to pass the port.
    008793
    +             *         Like r->hostname, there should be a r->portno
    008793
    +             */
    008793
    +            r->parsed_uri.port = port;
    008793
    +            r->parsed_uri.port_str = apr_itoa(r->pool, (int)port);
    008793
    +        }
    008793
    +        if (host_header[0] == '[')
    008793
    +            is_v6literal = 1;
    008793
    +    }
    008793
    +    else {
    008793
    +        /*
    008793
    +         * Already parsed, surrounding [ ] (if IPv6 literal) and :port have
    008793
    +         * already been removed.
    008793
    +         */
    008793
    +        host = apr_pstrdup(r->pool, r->hostname);
    008793
    +        if (ap_strchr(host, ':') != NULL)
    008793
    +            is_v6literal = 1;
    008793
         }
    008793
     
    008793
    -    rv = apr_parse_addr_port(&host, &scope_id, &port, r->hostname, r->pool);
    008793
    -    if (rv != APR_SUCCESS || scope_id) {
    008793
    -        goto bad;
    008793
    +    if (is_v6literal) {
    008793
    +        rv = fix_hostname_v6_literal(r, host);
    008793
         }
    008793
    -
    008793
    -    if (port) {
    008793
    -        /* Don't throw the Host: header's port number away:
    008793
    -           save it in parsed_uri -- ap_get_server_port() needs it! */
    008793
    -        /* @@@ XXX there should be a better way to pass the port.
    008793
    -         *         Like r->hostname, there should be a r->portno
    008793
    -         */
    008793
    -        r->parsed_uri.port = port;
    008793
    -        r->parsed_uri.port_str = apr_itoa(r->pool, (int)port);
    008793
    +    else {
    008793
    +        rv = fix_hostname_non_v6(r, host);
    008793
    +        if (strict && rv == APR_SUCCESS)
    008793
    +            rv = strict_hostname_check(r, host);
    008793
         }
    008793
    +    if (rv != APR_SUCCESS)
    008793
    +        goto bad;
    008793
     
    008793
    -    /* if the hostname is an IPv6 numeric address string, it was validated
    008793
    -     * already; otherwise, further validation is needed
    008793
    -     */
    008793
    -    if (r->hostname[0] != '[') {
    008793
    -        for (dst = host; *dst; dst++) {
    008793
    -            if (apr_islower(*dst)) {
    008793
    -                /* leave char unchanged */
    008793
    -            }
    008793
    -            else if (*dst == '.') {
    008793
    -                if (*(dst + 1) == '.') {
    008793
    -                    goto bad;
    008793
    -                }
    008793
    -            }
    008793
    -            else if (apr_isupper(*dst)) {
    008793
    -                *dst = apr_tolower(*dst);
    008793
    -            }
    008793
    -            else if (*dst == '/' || *dst == '\\') {
    008793
    -                goto bad;
    008793
    -            }
    008793
    -        }
    008793
    -        /* strip trailing gubbins */
    008793
    -        if (dst > host && dst[-1] == '.') {
    008793
    -            dst[-1] = '\0';
    008793
    -        }
    008793
    -    }
    008793
         r->hostname = host;
    008793
    -    return;
    008793
    +    return is_v6literal;
    008793
     
    008793
     bad:
    008793
    -    r->status = HTTP_BAD_REQUEST;
    008793
         ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00550)
    008793
                       "Client sent malformed Host header: %s",
    008793
    -                  r->hostname);
    008793
    -    return;
    008793
    +                  src);
    008793
    +bad_nolog:
    008793
    +    r->status = HTTP_BAD_REQUEST;
    008793
    +    return is_v6literal;
    008793
     }
    008793
     
    008793
    -
    008793
     /* return 1 if host matches ServerName or ServerAliases */
    008793
     static int matches_aliases(server_rec *s, const char *host)
    008793
     {
    008793
    @@ -980,15 +1101,76 @@
    008793
         }
    008793
     }
    008793
     
    008793
    +static APR_INLINE const char *construct_host_header(request_rec *r,
    008793
    +                                                    int is_v6literal)
    008793
    +{
    008793
    +    struct iovec iov[5];
    008793
    +    apr_size_t nvec = 0;
    008793
    +    /*
    008793
    +     * We cannot use ap_get_server_name/port here, because we must
    008793
    +     * ignore UseCanonicalName/Port.
    008793
    +     */
    008793
    +    if (is_v6literal) {
    008793
    +        iov[nvec].iov_base = "[";
    008793
    +        iov[nvec].iov_len = 1;
    008793
    +        nvec++;
    008793
    +    }
    008793
    +    iov[nvec].iov_base = (void *)r->hostname;
    008793
    +    iov[nvec].iov_len = strlen(r->hostname);
    008793
    +    nvec++;
    008793
    +    if (is_v6literal) {
    008793
    +        iov[nvec].iov_base = "]";
    008793
    +        iov[nvec].iov_len = 1;
    008793
    +        nvec++;
    008793
    +    }
    008793
    +    if (r->parsed_uri.port_str) {
    008793
    +        iov[nvec].iov_base = ":";
    008793
    +        iov[nvec].iov_len = 1;
    008793
    +        nvec++;
    008793
    +        iov[nvec].iov_base = r->parsed_uri.port_str;
    008793
    +        iov[nvec].iov_len = strlen(r->parsed_uri.port_str);
    008793
    +        nvec++;
    008793
    +    }
    008793
    +    return apr_pstrcatv(r->pool, iov, nvec, NULL);
    008793
    +}
    008793
     
    008793
     AP_DECLARE(void) ap_update_vhost_from_headers(request_rec *r)
    008793
     {
    008793
    -    /* must set this for HTTP/1.1 support */
    008793
    -    if (r->hostname || (r->hostname = apr_table_get(r->headers_in, "Host"))) {
    008793
    -        fix_hostname(r);
    008793
    -        if (r->status != HTTP_OK)
    008793
    -            return;
    008793
    +    core_server_config *conf = ap_get_core_module_config(r->server->module_config);
    008793
    +    const char *host_header = apr_table_get(r->headers_in, "Host");
    008793
    +    int is_v6literal = 0;
    008793
    +    int have_hostname_from_url = 0;
    008793
    +
    008793
    +    if (r->hostname) {
    008793
    +        /*
    008793
    +         * If there was a host part in the Request-URI, ignore the 'Host'
    008793
    +         * header.
    008793
    +         */
    008793
    +        have_hostname_from_url = 1;
    008793
    +        is_v6literal = fix_hostname(r, NULL, conf->http_conformance);
    008793
    +    }
    008793
    +    else if (host_header != NULL) {
    008793
    +        is_v6literal = fix_hostname(r, host_header, conf->http_conformance);
    008793
    +    }
    008793
    +    if (r->status != HTTP_OK)
    008793
    +        return;
    008793
    +
    008793
    +    if (conf->http_conformance != AP_HTTP_CONFORMANCE_UNSAFE) {
    008793
    +        /*
    008793
    +         * If we have both hostname from an absoluteURI and a Host header,
    008793
    +         * we must ignore the Host header (RFC 2616 5.2).
    008793
    +         * To enforce this, we reset the Host header to the value from the
    008793
    +         * request line.
    008793
    +         */
    008793
    +        if (have_hostname_from_url && host_header != NULL) {
    008793
    +            const char *repl = construct_host_header(r, is_v6literal);
    008793
    +            apr_table_set(r->headers_in, "Host", repl);
    008793
    +            ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(02417)
    008793
    +                          "Replacing host header '%s' with host '%s' given "
    008793
    +                          "in the request uri", host_header, repl);
    008793
    +        }
    008793
         }
    008793
    +
    008793
         /* check if we tucked away a name_chain */
    008793
         if (r->connection->vhost_lookup_data) {
    008793
             if (r->hostname)