mirror of
https://github.com/owasp-modsecurity/ModSecurity.git
synced 2026-01-13 15:07:10 +03:00
Merge in trunk changes for 2.5.0-dev2.
This commit is contained in:
@@ -1,17 +1,14 @@
|
||||
/*
|
||||
* ModSecurity for Apache 2.x, http://www.modsecurity.org/
|
||||
* Copyright (c) 2004-2006 Thinking Stone (http://www.thinkingstone.com)
|
||||
*
|
||||
* $Id$
|
||||
* Copyright (c) 2004-2007 Breach Security, Inc. (http://www.breach.com/)
|
||||
*
|
||||
* You should have received a copy of the licence along with this
|
||||
* program (stored in the file "LICENSE"). If the file is missing,
|
||||
* or if you have any other questions related to the licence, please
|
||||
* write to Thinking Stone at contact@thinkingstone.com.
|
||||
* write to Breach Security, Inc. at support@breach.com.
|
||||
*
|
||||
*/
|
||||
#include "modsecurity.h"
|
||||
// #include "apache2.h"
|
||||
#include "pdf_protect.h"
|
||||
|
||||
#include <ctype.h>
|
||||
@@ -225,17 +222,21 @@ apr_status_t pdfp_output_filter(ap_filter_t *f, apr_bucket_brigade *bb_in) {
|
||||
|
||||
if (msr == NULL) {
|
||||
ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_NOERRNO, 0, f->r->server,
|
||||
"ModSecurity: Internal Error: msr is null in PDF output filter.");
|
||||
"ModSecurity: Internal Error: Unable to retrieve context in PDF output filter.");
|
||||
|
||||
ap_remove_output_filter(f);
|
||||
|
||||
return send_error_bucket(f, HTTP_INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
|
||||
if (msr->txcfg->pdfp_enabled == 1) {
|
||||
// TODO Should we look at err_headers_out too?
|
||||
const char *h_content_type = apr_table_get(f->r->headers_out, "Content-Type");
|
||||
|
||||
if (msr->txcfg->debuglog_level >= 9) {
|
||||
msr_log(msr, 9, "PdfProtect: r->content_type=%s, header C-T=%s",
|
||||
f->r->content_type, h_content_type);
|
||||
log_escape_nq(msr->mp, f->r->content_type),
|
||||
log_escape_nq(msr->mp, h_content_type));
|
||||
}
|
||||
|
||||
/* Have we been asked to tweak the headers? */
|
||||
@@ -255,6 +256,10 @@ apr_status_t pdfp_output_filter(ap_filter_t *f, apr_bucket_brigade *bb_in) {
|
||||
}
|
||||
|
||||
/* Proceed to detect dynamically-generated PDF files. */
|
||||
|
||||
// TODO application/x-pdf, application/vnd.fdf, application/vnd.adobe.xfdf,
|
||||
// application/vnd.adobe.xdp+xml, application/vnd.adobe.xfd+xml, application/vnd.pdf
|
||||
// application/acrobat, text/pdf, text/x-pdf ???
|
||||
if (((f->r->content_type != NULL)&&(strcasecmp(f->r->content_type, "application/pdf") == 0))
|
||||
|| ((h_content_type != NULL)&&(strcasecmp(h_content_type, "application/pdf") == 0)))
|
||||
{
|
||||
@@ -263,16 +268,35 @@ apr_status_t pdfp_output_filter(ap_filter_t *f, apr_bucket_brigade *bb_in) {
|
||||
|
||||
if (msr->txcfg->debuglog_level >= 9) {
|
||||
msr_log(msr, 9, "PdfProtect: Detected a dynamically-generated PDF in %s",
|
||||
r->uri);
|
||||
log_escape_nq(msr->mp, r->uri));
|
||||
}
|
||||
|
||||
/* If we are configured with ForcedDownload protection method then we
|
||||
* can do our thing here and finish early.
|
||||
*/
|
||||
if (msr->txcfg->pdfp_method == PDF_PROTECT_METHOD_FORCED_DOWNLOAD) {
|
||||
if (msr->txcfg->debuglog_level >= 9) {
|
||||
msr_log(msr, 9, "PdfProtect: Forcing download of a dynamically "
|
||||
"generated PDF file.");
|
||||
}
|
||||
|
||||
apr_table_set(f->r->headers_out, "Content-Disposition", DISPOSITION_VALUE);
|
||||
f->r->content_type = ATTACHMENT_MIME_TYPE;
|
||||
|
||||
ap_remove_output_filter(f);
|
||||
|
||||
return ap_pass_brigade(f->next, bb_in);
|
||||
}
|
||||
|
||||
/* If we are here that means TokenRedirection is the desired protection method. */
|
||||
|
||||
/* Is this a non-GET request? */
|
||||
if ((f->r->method_number != M_GET)&&
|
||||
((msr->txcfg->pdfp_only_get == 1)||(msr->txcfg->pdfp_only_get == -1))
|
||||
) {
|
||||
/* This is a non-GET request and we have been configured
|
||||
* not to intercept it. We are not going to do that but
|
||||
* we are going to tweak the headers to force download.
|
||||
* not to intercept it. So we are going to tweak the headers
|
||||
* to force download.
|
||||
*/
|
||||
if (msr->txcfg->debuglog_level >= 9) {
|
||||
msr_log(msr, 9, "PdfProtect: Forcing download of a dynamically "
|
||||
@@ -283,6 +307,7 @@ apr_status_t pdfp_output_filter(ap_filter_t *f, apr_bucket_brigade *bb_in) {
|
||||
f->r->content_type = ATTACHMENT_MIME_TYPE;
|
||||
|
||||
ap_remove_output_filter(f);
|
||||
|
||||
return ap_pass_brigade(f->next, bb_in);
|
||||
}
|
||||
|
||||
@@ -298,10 +323,11 @@ apr_status_t pdfp_output_filter(ap_filter_t *f, apr_bucket_brigade *bb_in) {
|
||||
/* Redirect user to the new URI. */
|
||||
if (msr->txcfg->debuglog_level >= 9) {
|
||||
msr_log(msr, 9, "PdfProtect: PDF request without a token - "
|
||||
"redirecting to %s.", new_uri);
|
||||
"redirecting to %s.", log_escape_nq(msr->mp, new_uri));
|
||||
}
|
||||
|
||||
apr_table_set(r->headers_out, "Location", new_uri);
|
||||
|
||||
return send_error_bucket(f, REDIRECT_STATUS);
|
||||
}
|
||||
} else { /* Token found. */
|
||||
@@ -353,6 +379,16 @@ int pdfp_check(modsec_rec *msr) {
|
||||
if (msr->txcfg->debuglog_level >= 4) {
|
||||
msr_log(msr, 4, "PdfProtect: Not enabled here.");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (msr->txcfg->pdfp_method != PDF_PROTECT_METHOD_TOKEN_REDIRECTION) {
|
||||
if (msr->txcfg->debuglog_level >= 4) {
|
||||
msr_log(msr, 4, "PdfProtect: Configured with ForcedDownload as protection method, "
|
||||
"skipping detection on the inbound.");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -365,17 +401,18 @@ int pdfp_check(modsec_rec *msr) {
|
||||
if (msr->txcfg->debuglog_level >= 4) {
|
||||
msr_log(msr, 4, "PdfProtect: Unable to inspect URI because it is NULL.");
|
||||
}
|
||||
/* TODO Should we return -1 instead? */
|
||||
return 0;
|
||||
|
||||
return -1; /* Error. */
|
||||
}
|
||||
|
||||
if (msr->txcfg->debuglog_level >= 9) {
|
||||
msr_log(msr, 9, "PdfProtect: URI=%s, filename=%s, QUERY_STRING=%s.",
|
||||
msr->r->uri, msr->r->filename, msr->r->args);
|
||||
log_escape_nq(msr->mp, msr->r->uri), log_escape_nq(msr->mp, msr->r->filename),
|
||||
log_escape_nq(msr->mp, msr->r->args));
|
||||
}
|
||||
|
||||
uri = apr_pstrdup(msr->mp, msr->r->uri);
|
||||
if (uri == NULL) return -1;
|
||||
if (uri == NULL) return -1; /* Error. */
|
||||
ap_str_tolower(uri);
|
||||
|
||||
/* Attempt to figure out if this is a request for a PDF file. We are
|
||||
@@ -389,17 +426,19 @@ int pdfp_check(modsec_rec *msr) {
|
||||
msr_log(msr, 4, "PdfProtect: No indication in the URI this is a "
|
||||
"request for a PDF file.");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Ignore request methods other than GET if
|
||||
/* Ignore request methods other than GET and HEAD if
|
||||
* configured to do so.
|
||||
*/
|
||||
if ((msr->r->method_number != M_GET)&&(cfg->pdfp_only_get != 0)) {
|
||||
if (msr->txcfg->debuglog_level >= 4) {
|
||||
msr_log(msr, 4, "PdfProtect: Configured not to intercept non-GET requests "
|
||||
"(method=%s/%i).", msr->r->method, msr->r->method_number);
|
||||
msr_log(msr, 4, "PdfProtect: Not intercepting a GET/HEAD request "
|
||||
"(method=%s/%i).", log_escape_nq(msr->mp, msr->r->method), msr->r->method_number);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -421,7 +460,7 @@ int pdfp_check(modsec_rec *msr) {
|
||||
/* Redirect user to the new URI. */
|
||||
if (msr->txcfg->debuglog_level >= 9) {
|
||||
msr_log(msr, 9, "PdfProtect: PDF request without a token - redirecting to %s.",
|
||||
new_uri);
|
||||
log_escape_nq(msr->mp, new_uri));
|
||||
}
|
||||
|
||||
apr_table_set(msr->r->headers_out, "Location", new_uri);
|
||||
@@ -437,6 +476,7 @@ int pdfp_check(modsec_rec *msr) {
|
||||
msr_log(msr, 9, "PdfProtect: PDF request with a valid token - "
|
||||
"serving PDF file normally.");
|
||||
}
|
||||
|
||||
return 0;
|
||||
} else { /* Not valid. */
|
||||
/* The token is not valid. We will tweak the response
|
||||
|
||||
Reference in New Issue
Block a user