Skip to content

Commit 10232a0

Browse files
committed
mod_mime_magic: Disable decompression by default, add config option
to enable it. * modules/metadata/mod_mime_magic.c (magic_server_config_rec): Add decompression_enabled field. (create_magic_server_config, merge_magic_server_config): Initialize and merge decompression_enabled. (set_decompression): New function. (magic_cmds): Add MimeMagicDecompression directive. (tryit): Only decompress files when decompression is enabled. * docs/manual/mod/mod_mime_magic.xml: Document MimeMagicDecompression directive. PR: 69985 Submitted by: Gordon Messmer <gmessmer redhat.com> Github: closes #625 git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/trunk@1932902 13f79535-47bb-0310-9956-ffa450edef68
1 parent 140c0bc commit 10232a0

3 files changed

Lines changed: 95 additions & 3 deletions

File tree

changes-entries/pr69985.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
*) mod_mime_magic: Add MimeMagicDecompression directive to control
2+
decompression of compressed files for MIME type detection.
3+
Decompression is now disabled by default. PR 69985.
4+
[Gordon Messmer <gmessmer redhat.com>]

docs/manual/mod/mod_mime_magic.xml

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,4 +271,64 @@ using the specified magic file</description>
271271
</usage>
272272
</directivesynopsis>
273273

274+
<directivesynopsis>
275+
<name>MimeMagicDecompression</name>
276+
<description>Enable decompression of compressed files for MIME type detection</description>
277+
<syntax>MimeMagicDecompression On|Off</syntax>
278+
<default>MimeMagicDecompression Off</default>
279+
<contextlist><context>server config</context><context>virtual host</context>
280+
</contextlist>
281+
282+
<usage>
283+
<p>The <directive>MimeMagicDecompression</directive> directive controls
284+
whether <module>mod_mime_magic</module> will attempt to decompress files
285+
that appear to be compressed (gzip, compress, etc.) in order to determine
286+
the MIME type of the uncompressed content. This feature is <strong>disabled
287+
by default</strong> and should only be enabled if you understand the
288+
significant drawbacks. It exists to maintain backward compatibility with
289+
previous releases of httpd, but its use is discouraged.</p>
290+
291+
<note type="warning"><title>Security and Compatibility Issues</title>
292+
<p>This feature has several serious flaws and is disabled by default:</p>
293+
<ol>
294+
<li><strong>Not RFC-compliant:</strong> Standards documents consistently
295+
recommend against setting Content-Encoding for files that are already
296+
compressed (such as .zip or .gz files). See
297+
<a href="https://www.rfc-editor.org/rfc/rfc9110.html#name-content-encoding">RFC 9110</a>.</li>
298+
299+
<li><strong>Breaks content integrity:</strong> When Content-Encoding is set,
300+
most HTTP clients will decompress the file before writing it to disk. This
301+
causes the downloaded file to have a different size and checksum than the
302+
original, breaking signature verification and checksum validation. Software
303+
distribution sites will find this particularly problematic.</li>
304+
305+
<li><strong>Unpredictable behavior:</strong> This feature only applies to
306+
files that don't match a MIME type via file extension. This can lead to
307+
inconsistent behavior where some files in a directory are affected and
308+
others are not, making problems difficult to diagnose.</li>
309+
310+
<li><strong>Performance impact:</strong> Decompression requires forking and
311+
executing an external <code>gzip</code> process for each compressed file,
312+
which adds significant overhead.</li>
313+
314+
<li><strong>Security risk:</strong> Passing untrusted uploaded file data to
315+
an external binary (<code>gzip</code>) could potentially expose the server to
316+
compression bombs, resource exhaustion, or remote code execution
317+
vulnerabilities in the decompression tool.</li>
318+
</ol>
319+
</note>
320+
321+
<example><title>Example (not recommended)</title>
322+
<highlight language="config">
323+
# Only enable if you fully understand the risks
324+
MimeMagicDecompression On
325+
</highlight>
326+
</example>
327+
328+
<p>In most cases, it is better to ensure files have proper extensions
329+
that can be mapped via <module>mod_mime</module> rather than relying on
330+
this feature.</p>
331+
</usage>
332+
</directivesynopsis>
333+
274334
</modulesynopsis>

modules/metadata/mod_mime_magic.c

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@
118118

119119
#define MAXMIMESTRING 256
120120

121+
#define UNSET -1
122+
121123
/* HOWMANY must be at least 4096 to make gzip -dcq work */
122124
#define HOWMANY 4096
123125
/* SMALL_HOWMANY limits how much work we do to figure out text files */
@@ -456,6 +458,7 @@ typedef struct {
456458
const char *magicfile; /* where magic be found */
457459
struct magic *magic; /* head of magic config list */
458460
struct magic *last;
461+
int decompression_enabled; /* whether to decompress files for content detection */
459462
} magic_server_config_rec;
460463

461464
/* per-request info */
@@ -472,8 +475,11 @@ module AP_MODULE_DECLARE_DATA mime_magic_module;
472475

473476
static void *create_magic_server_config(apr_pool_t *p, server_rec *d)
474477
{
478+
magic_server_config_rec *conf;
475479
/* allocate the config - use pcalloc because it needs to be zeroed */
476-
return apr_pcalloc(p, sizeof(magic_server_config_rec));
480+
conf = apr_pcalloc(p, sizeof(magic_server_config_rec));
481+
conf->decompression_enabled = UNSET;
482+
return conf;
477483
}
478484

479485
static void *merge_magic_server_config(apr_pool_t *p, void *basev, void *addv)
@@ -484,6 +490,8 @@ static void *merge_magic_server_config(apr_pool_t *p, void *basev, void *addv)
484490
apr_palloc(p, sizeof(magic_server_config_rec));
485491

486492
new->magicfile = add->magicfile ? add->magicfile : base->magicfile;
493+
new->decompression_enabled = (add->decompression_enabled != UNSET) ?
494+
add->decompression_enabled : base->decompression_enabled;
487495
new->magic = NULL;
488496
new->last = NULL;
489497
return new;
@@ -502,6 +510,16 @@ static const char *set_magicfile(cmd_parms *cmd, void *dummy, const char *arg)
502510
return NULL;
503511
}
504512

513+
static const char *set_decompression(cmd_parms *cmd, void *dummy, int arg)
514+
{
515+
magic_server_config_rec *conf = (magic_server_config_rec *)
516+
ap_get_module_config(cmd->server->module_config,
517+
&mime_magic_module);
518+
519+
conf->decompression_enabled = arg;
520+
return NULL;
521+
}
522+
505523
/*
506524
* configuration file commands - exported to Apache API
507525
*/
@@ -510,6 +528,13 @@ static const command_rec mime_magic_cmds[] =
510528
{
511529
AP_INIT_TAKE1("MimeMagicFile", set_magicfile, NULL, RSRC_CONF,
512530
"Path to MIME Magic file (in file(1) format)"),
531+
AP_INIT_FLAG("MimeMagicDecompression", set_decompression, NULL, RSRC_CONF,
532+
"Enable decompression of compressed files for content type detection "
533+
"(Off by default). WARNING: This feature is NOT RFC-compliant, can be "
534+
"unpredictable, breaks content integrity (clients will decompress files "
535+
"causing checksum mismatches), impacts performance (fork/exec overhead), "
536+
"and is unsafe (passes untrusted data to external gzip binary). "
537+
"Use only if you understand these risks."),
513538
{NULL}
514539
};
515540

@@ -878,10 +903,13 @@ static int magic_process(request_rec *r)
878903
static int tryit(request_rec *r, unsigned char *buf, apr_size_t nb,
879904
int checkzmagic)
880905
{
906+
magic_server_config_rec *conf = (magic_server_config_rec *)
907+
ap_get_module_config(r->server->module_config, &mime_magic_module);
908+
881909
/*
882-
* Try compression stuff
910+
* Try compression stuff (only if decompression is enabled)
883911
*/
884-
if (checkzmagic == 1) {
912+
if (checkzmagic == 1 && conf && conf->decompression_enabled == 1) {
885913
if (zmagic(r, buf, nb) == 1)
886914
return OK;
887915
}

0 commit comments

Comments
 (0)