41a6c3
diff --git a/modules/filters/mod_deflate.c b/modules/filters/mod_deflate.c
41a6c3
index 79f6f8d..6c415c8 100644
41a6c3
--- a/modules/filters/mod_deflate.c
41a6c3
+++ b/modules/filters/mod_deflate.c
41a6c3
@@ -37,6 +37,7 @@
41a6c3
 #include "httpd.h"
41a6c3
 #include "http_config.h"
41a6c3
 #include "http_log.h"
41a6c3
+#include "http_core.h"
41a6c3
 #include "apr_lib.h"
41a6c3
 #include "apr_strings.h"
41a6c3
 #include "apr_general.h"
41a6c3
@@ -52,6 +53,9 @@
41a6c3
 static const char deflateFilterName[] = "DEFLATE";
41a6c3
 module AP_MODULE_DECLARE_DATA deflate_module;
41a6c3
 
41a6c3
+#define AP_INFLATE_RATIO_LIMIT 200
41a6c3
+#define AP_INFLATE_RATIO_BURST 3
41a6c3
+
41a6c3
 typedef struct deflate_filter_config_t
41a6c3
 {
41a6c3
     int windowSize;
41a6c3
@@ -63,6 +67,12 @@ typedef struct deflate_filter_config_t
41a6c3
     char *note_output_name;
41a6c3
 } deflate_filter_config;
41a6c3
 
41a6c3
+typedef struct deflate_dirconf_t {
41a6c3
+    apr_off_t inflate_limit;
41a6c3
+    int ratio_limit,
41a6c3
+        ratio_burst;
41a6c3
+} deflate_dirconf_t;
41a6c3
+
41a6c3
 /* RFC 1952 Section 2.3 defines the gzip header:
41a6c3
  *
41a6c3
  * +---+---+---+---+---+---+---+---+---+---+
41a6c3
@@ -204,6 +214,14 @@ static void *create_deflate_server_config(apr_pool_t *p, server_rec *s)
41a6c3
     return c;
41a6c3
 }
41a6c3
 
41a6c3
+static void *create_deflate_dirconf(apr_pool_t *p, char *dummy)
41a6c3
+{
41a6c3
+    deflate_dirconf_t *dc = apr_pcalloc(p, sizeof(*dc));
41a6c3
+    dc->ratio_limit = AP_INFLATE_RATIO_LIMIT;
41a6c3
+    dc->ratio_burst = AP_INFLATE_RATIO_BURST;
41a6c3
+    return dc;
41a6c3
+}
41a6c3
+
41a6c3
 static const char *deflate_set_window_size(cmd_parms *cmd, void *dummy,
41a6c3
                                            const char *arg)
41a6c3
 {
41a6c3
@@ -295,6 +313,55 @@ static const char *deflate_set_compressionlevel(cmd_parms *cmd, void *dummy,
41a6c3
     return NULL;
41a6c3
 }
41a6c3
 
41a6c3
+
41a6c3
+static const char *deflate_set_inflate_limit(cmd_parms *cmd, void *dirconf,
41a6c3
+                                      const char *arg)
41a6c3
+{
41a6c3
+    deflate_dirconf_t *dc = (deflate_dirconf_t*) dirconf;
41a6c3
+    char *errp;
41a6c3
+
41a6c3
+    if (APR_SUCCESS != apr_strtoff(&dc->inflate_limit, arg, &errp, 10)) {
41a6c3
+        return "DeflateInflateLimitRequestBody is not parsable.";
41a6c3
+    }
41a6c3
+    if (*errp || dc->inflate_limit < 0) {
41a6c3
+        return "DeflateInflateLimitRequestBody requires a non-negative integer.";
41a6c3
+    }
41a6c3
+
41a6c3
+    return NULL;
41a6c3
+}
41a6c3
+
41a6c3
+static const char *deflate_set_inflate_ratio_limit(cmd_parms *cmd,
41a6c3
+                                                   void *dirconf,
41a6c3
+                                                   const char *arg)
41a6c3
+{
41a6c3
+    deflate_dirconf_t *dc = (deflate_dirconf_t*) dirconf;
41a6c3
+    int i;
41a6c3
+
41a6c3
+    i = atoi(arg);
41a6c3
+    if (i <= 0)
41a6c3
+        return "DeflateInflateRatioLimit must be positive";
41a6c3
+
41a6c3
+    dc->ratio_limit = i;
41a6c3
+
41a6c3
+    return NULL;
41a6c3
+}
41a6c3
+
41a6c3
+static const char *deflate_set_inflate_ratio_burst(cmd_parms *cmd,
41a6c3
+                                                   void *dirconf,
41a6c3
+                                                   const char *arg)
41a6c3
+{
41a6c3
+    deflate_dirconf_t *dc = (deflate_dirconf_t*) dirconf;
41a6c3
+    int i;
41a6c3
+
41a6c3
+    i = atoi(arg);
41a6c3
+    if (i <= 0)
41a6c3
+        return "DeflateInflateRatioBurst must be positive";
41a6c3
+
41a6c3
+    dc->ratio_burst = i;
41a6c3
+
41a6c3
+    return NULL;
41a6c3
+}
41a6c3
+
41a6c3
 typedef struct deflate_ctx_t
41a6c3
 {
41a6c3
     z_stream stream;
41a6c3
@@ -304,6 +371,8 @@ typedef struct deflate_ctx_t
41a6c3
     int (*libz_end_func)(z_streamp);
41a6c3
     unsigned char *validation_buffer;
41a6c3
     apr_size_t validation_buffer_length;
41a6c3
+    int ratio_hits;
41a6c3
+    apr_off_t inflate_total;
41a6c3
     unsigned int inflate_init:1;
41a6c3
     unsigned int filter_init:1;
41a6c3
     unsigned int done:1;
41a6c3
@@ -422,6 +491,22 @@ static void deflate_check_etag(request_rec *r, const char *transform)
41a6c3
     }
41a6c3
 }
41a6c3
 
41a6c3
+/* Check whether the (inflate) ratio exceeds the configured limit/burst. */
41a6c3
+static int check_ratio(request_rec *r, deflate_ctx *ctx,
41a6c3
+                       const deflate_dirconf_t *dc)
41a6c3
+{
41a6c3
+    if (ctx->stream.total_in) {
41a6c3
+        int ratio = ctx->stream.total_out / ctx->stream.total_in;
41a6c3
+        if (ratio < dc->ratio_limit) {
41a6c3
+            ctx->ratio_hits = 0;
41a6c3
+        }
41a6c3
+        else if (++ctx->ratio_hits > dc->ratio_burst) {
41a6c3
+            return 0;
41a6c3
+        }
41a6c3
+    }
41a6c3
+    return 1;
41a6c3
+}
41a6c3
+
41a6c3
 static int have_ssl_compression(request_rec *r)
41a6c3
 {
41a6c3
     const char *comp;
41a6c3
@@ -897,6 +982,8 @@ static apr_status_t deflate_in_filter(ap_filter_t *f,
41a6c3
     int zRC;
41a6c3
     apr_status_t rv;
41a6c3
     deflate_filter_config *c;
41a6c3
+    deflate_dirconf_t *dc;
41a6c3
+    apr_off_t inflate_limit;
41a6c3
 
41a6c3
     /* just get out of the way of things we don't want. */
41a6c3
     if (mode != AP_MODE_READBYTES) {
41a6c3
@@ -904,6 +991,7 @@ static apr_status_t deflate_in_filter(ap_filter_t *f,
41a6c3
     }
41a6c3
 
41a6c3
     c = ap_get_module_config(r->server->module_config, &deflate_module);
41a6c3
+    dc = ap_get_module_config(r->per_dir_config, &deflate_module);
41a6c3
 
41a6c3
     if (!ctx) {
41a6c3
         char deflate_hdr[10];
41a6c3
@@ -994,6 +1082,12 @@ static apr_status_t deflate_in_filter(ap_filter_t *f,
41a6c3
         apr_brigade_cleanup(ctx->bb);
41a6c3
     }
41a6c3
 
41a6c3
+    inflate_limit = dc->inflate_limit; 
41a6c3
+    if (inflate_limit == 0) { 
41a6c3
+        /* The core is checking the deflated body, we'll check the inflated */
41a6c3
+        inflate_limit = ap_get_limit_req_body(f->r);
41a6c3
+    }
41a6c3
+
41a6c3
     if (APR_BRIGADE_EMPTY(ctx->proc_bb)) {
41a6c3
         rv = ap_get_brigade(f->next, ctx->bb, mode, block, readbytes);
41a6c3
 
41a6c3
@@ -1038,6 +1132,17 @@ static apr_status_t deflate_in_filter(ap_filter_t *f,
41a6c3
 
41a6c3
                 ctx->stream.next_out = ctx->buffer;
41a6c3
                 len = c->bufferSize - ctx->stream.avail_out;
41a6c3
+ 
41a6c3
+                ctx->inflate_total += len;
41a6c3
+                if (inflate_limit && ctx->inflate_total > inflate_limit) { 
41a6c3
+                    inflateEnd(&ctx->stream);
41a6c3
+                    ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, APLOGNO(02647)
41a6c3
+                            "Inflated content length of %" APR_OFF_T_FMT
41a6c3
+                            " is larger than the configured limit"
41a6c3
+                            " of %" APR_OFF_T_FMT, 
41a6c3
+                            ctx->inflate_total, inflate_limit);
41a6c3
+                    return APR_ENOSPC;
41a6c3
+                }
41a6c3
 
41a6c3
                 ctx->crc = crc32(ctx->crc, (const Bytef *)ctx->buffer, len);
41a6c3
                 tmp_heap = apr_bucket_heap_create((char *)ctx->buffer, len,
41a6c3
@@ -1073,6 +1178,26 @@ static apr_status_t deflate_in_filter(ap_filter_t *f,
41a6c3
                     ctx->stream.next_out = ctx->buffer;
41a6c3
                     len = c->bufferSize - ctx->stream.avail_out;
41a6c3
 
41a6c3
+                    ctx->inflate_total += len;
41a6c3
+                    if (inflate_limit && ctx->inflate_total > inflate_limit) { 
41a6c3
+                        inflateEnd(&ctx->stream);
41a6c3
+                        ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, APLOGNO(02648)
41a6c3
+                                "Inflated content length of %" APR_OFF_T_FMT
41a6c3
+                                " is larger than the configured limit"
41a6c3
+                                " of %" APR_OFF_T_FMT, 
41a6c3
+                                ctx->inflate_total, inflate_limit);
41a6c3
+                        return APR_ENOSPC;
41a6c3
+                    }
41a6c3
+
41a6c3
+                    if (!check_ratio(r, ctx, dc)) {
41a6c3
+                        inflateEnd(&ctx->stream);
41a6c3
+                        ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, APLOGNO(02649)
41a6c3
+                                "Inflated content ratio is larger than the "
41a6c3
+                                "configured limit %i by %i time(s)",
41a6c3
+                                dc->ratio_limit, dc->ratio_burst);
41a6c3
+                        return APR_EINVAL;
41a6c3
+                    }
41a6c3
+
41a6c3
                     ctx->crc = crc32(ctx->crc, (const Bytef *)ctx->buffer, len);
41a6c3
                     tmp_heap = apr_bucket_heap_create((char *)ctx->buffer, len,
41a6c3
                                                       NULL, f->c->bucket_alloc);
41a6c3
@@ -1193,6 +1318,7 @@ static apr_status_t inflate_out_filter(ap_filter_t *f,
41a6c3
     int zRC;
41a6c3
     apr_status_t rv;
41a6c3
     deflate_filter_config *c;
41a6c3
+    deflate_dirconf_t *dc;
41a6c3
 
41a6c3
     /* Do nothing if asked to filter nothing. */
41a6c3
     if (APR_BRIGADE_EMPTY(bb)) {
41a6c3
@@ -1200,6 +1326,7 @@ static apr_status_t inflate_out_filter(ap_filter_t *f,
41a6c3
     }
41a6c3
 
41a6c3
     c = ap_get_module_config(r->server->module_config, &deflate_module);
41a6c3
+    dc = ap_get_module_config(r->per_dir_config, &deflate_module);
41a6c3
 
41a6c3
     if (!ctx) {
41a6c3
 
41a6c3
@@ -1462,6 +1589,14 @@ static apr_status_t inflate_out_filter(ap_filter_t *f,
41a6c3
         while (ctx->stream.avail_in != 0) {
41a6c3
             if (ctx->stream.avail_out == 0) {
41a6c3
 
41a6c3
+                if (!check_ratio(r, ctx, dc)) {
41a6c3
+                    ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, APLOGNO(02650)
41a6c3
+                            "Inflated content ratio is larger than the "
41a6c3
+                            "configured limit %i by %i time(s)",
41a6c3
+                            dc->ratio_limit, dc->ratio_burst);
41a6c3
+                    return APR_EINVAL;
41a6c3
+                }
41a6c3
+
41a6c3
                 ctx->stream.next_out = ctx->buffer;
41a6c3
                 len = c->bufferSize - ctx->stream.avail_out;
41a6c3
 
41a6c3
@@ -1548,12 +1683,20 @@ static const command_rec deflate_filter_cmds[] = {
41a6c3
                   "Set the Deflate Memory Level (1-9)"),
41a6c3
     AP_INIT_TAKE1("DeflateCompressionLevel", deflate_set_compressionlevel, NULL, RSRC_CONF,
41a6c3
                   "Set the Deflate Compression Level (1-9)"),
41a6c3
+    AP_INIT_TAKE1("DeflateInflateLimitRequestBody", deflate_set_inflate_limit, NULL, OR_ALL,
41a6c3
+                  "Set a limit on size of inflated input"),
41a6c3
+    AP_INIT_TAKE1("DeflateInflateRatioLimit", deflate_set_inflate_ratio_limit, NULL, OR_ALL,
41a6c3
+                  "Set the inflate ratio limit above which inflation is "
41a6c3
+                  "aborted (default: " APR_STRINGIFY(AP_INFLATE_RATIO_LIMIT) ")"),
41a6c3
+    AP_INIT_TAKE1("DeflateInflateRatioBurst", deflate_set_inflate_ratio_burst, NULL, OR_ALL,
41a6c3
+                  "Set the maximum number of following inflate ratios above limit "
41a6c3
+                  "(default: " APR_STRINGIFY(AP_INFLATE_RATIO_BURST) ")"),
41a6c3
     {NULL}
41a6c3
 };
41a6c3
 
41a6c3
 AP_DECLARE_MODULE(deflate) = {
41a6c3
     STANDARD20_MODULE_STUFF,
41a6c3
-    NULL,                         /* dir config creater */
41a6c3
+    create_deflate_dirconf,       /* dir config creater */
41a6c3
     NULL,                         /* dir merger --- default is to override */
41a6c3
     create_deflate_server_config, /* server config */
41a6c3
     NULL,                         /* merge server config */