diff --git a/changes-entries/proxy-html-doctype-libxml2.txt b/changes-entries/proxy-html-doctype-libxml2.txt
new file mode 100644
index 00000000000..ca5552f6df9
--- /dev/null
+++ b/changes-entries/proxy-html-doctype-libxml2.txt
@@ -0,0 +1,3 @@
+ *) mod_proxy_html: Reject "ProxyHTMLDocType HTML|XHTML" if built with
+ libxml2 2.14 or later, with which HTML 4 documents can no longer
+ be cleaned up. [Joe Orton]
diff --git a/changes-entries/proxy-html-maxbufsize.txt b/changes-entries/proxy-html-maxbufsize.txt
new file mode 100644
index 00000000000..7f58f2a7de1
--- /dev/null
+++ b/changes-entries/proxy-html-maxbufsize.txt
@@ -0,0 +1,3 @@
+ *) mod_proxy_html: Add ProxyHTMLMaxBufSize, 1M by default; a larger
+ inline script, stylesheet or comment is passed through without
+ being rewritten by ProxyHTMLExtended. [Joe Orton]
diff --git a/changes-entries/proxy-html-urlmap-regex.txt b/changes-entries/proxy-html-urlmap-regex.txt
new file mode 100644
index 00000000000..ac8b8e88b7d
--- /dev/null
+++ b/changes-entries/proxy-html-urlmap-regex.txt
@@ -0,0 +1,3 @@
+ *) mod_proxy_html: Fix a crash if a ProxyHTMLURLMap regex using
+ interpolated variables was invalid, and fail at startup for any
+ other invalid regex. [Joe Orton]
diff --git a/docs/manual/mod/mod_proxy_html.xml b/docs/manual/mod/mod_proxy_html.xml
index 70d731b0a81..d9ac598020f 100644
--- a/docs/manual/mod/mod_proxy_html.xml
+++ b/docs/manual/mod/mod_proxy_html.xml
@@ -322,6 +322,11 @@ will also clean up the HTML to the specified standard. It cannot
fix every error, but it will strip out bogus elements and attributes.
It will also optionally log other errors at LogLevel Debug.
+The first form relies on the description of HTML 4 which
+libxml2 provided up to version 2.13. If mod_proxy_html
+is built with libxml2 2.14 or later, the first form is not available and
+is rejected as a configuration error. To declare such a document type
+without the clean-up, give it in the second form.
@@ -424,6 +429,31 @@ size and avoid the need to resize the buffer dynamically during a request.
+
+ProxyHTMLMaxBufSize
+Sets the maximum size of an inline script, stylesheet or
+comment which is rewritten.
+ProxyHTMLMaxBufSize bytes
+ProxyHTMLMaxBufSize 1M
+server config
+virtual hostdirectory
+
+Available in version 2.5.1 and later.
+
+With ProxyHTMLExtended
+enabled, mod_proxy_html reads each inline script,
+stylesheet or comment into memory in full before rewriting it. This
+directive limits the size of that buffer. A script, stylesheet or comment
+larger than bytes is passed through as received from the
+backend, without any ProxyHTMLURLMap rule applied to it, and a warning is
+logged.
+The value may be followed by K, M or
+G for kilobytes, megabytes or gigabytes. A value of
+0 removes the limit.
+
+
+
ProxyHTMLEvents
Specify attributes to treat as scripting events.
diff --git a/modules/filters/mod_proxy_html.c b/modules/filters/mod_proxy_html.c
index 3dffa5c8771..4ab45417487 100644
--- a/modules/filters/mod_proxy_html.c
+++ b/modules/filters/mod_proxy_html.c
@@ -109,6 +109,8 @@ typedef struct {
const char *etag;
unsigned int flags;
int bufsz;
+ apr_size_t maxbufsz;
+ int maxbufsz_set;
apr_hash_t *links;
apr_array_header_t *events;
const char *charset_out;
@@ -130,10 +132,39 @@ typedef struct {
char rbuf[4];
apr_size_t rlen;
apr_size_t rmin;
+ int overflow; /* buffered text went over the limit */
} saxctxt;
#define DEFAULT_BUFSZ (8192)
+/* Cleaning up documents to HTML 4.01 or XHTML 1.0 relies on the element
+ * and attribute tables which libxml2 2.14 no longer has.
+ */
+#if LIBXML_VERSION < 21400
+#define PROXY_HTML_ENFORCE 1
+#endif
+
+#ifndef PROXY_HTML_ENFORCE
+/* Elements which have no content and no end tag. */
+static const char *const empty_elts[] = {
+ "area", "base", "basefont", "bgsound", "br", "col", "embed", "frame",
+ "hr", "img", "input", "isindex", "keygen", "link", "meta", "param",
+ "source", "track", "wbr", NULL
+};
+
+static int is_empty_elt(const char *name)
+{
+ const char *const *e;
+
+ for (e = empty_elts; *e; e++) {
+ if (!ap_cstr_casecmp(name, *e))
+ return 1;
+ }
+ return 0;
+}
+#endif
+#define DEFAULT_MAXBUFSZ (1024 * 1024)
+
#define NORM_LC 0x1
#define NORM_MSSLASH 0x2
#define NORM_RESET 0x4
@@ -141,6 +172,7 @@ static htmlSAXHandler sax;
typedef enum { ATTR_IGNORE, ATTR_URI, ATTR_EVENT } rewrite_t;
+#ifdef PROXY_HTML_ENFORCE
static const char *const fpi_html =
"\n";
static const char *const fpi_html_legacy =
@@ -149,6 +181,7 @@ static const char *const fpi_xhtml =
"\n";
static const char *const fpi_xhtml_legacy =
"\n";
+#endif
static const char *const fpi_html5 = "\n";
static const char *const html_etag = ">";
static const char *const xhtml_etag = " />";
@@ -356,12 +389,40 @@ static void pinternalSubset(void* ctxt, const xmlChar *name,
}
ap_fputs(ctx->f->next, ctx->bb, ">\n");
}
+
+/* Buffer text which is rewritten when the element ends. If the buffer
+ * would grow beyond ProxyHTMLMaxBufSize, write out what it holds and
+ * pass through the rest of the text up to the end of the element,
+ * none of it rewritten.
+ */
+static void buffer_text(saxctxt *ctx, const char *chars, apr_size_t length)
+{
+ apr_size_t max = ctx->cfg->maxbufsz;
+
+ if (!ctx->overflow && max
+ && (ctx->vb.strlen > max || length > max - ctx->vb.strlen)) {
+ ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, ctx->f->r, APLOGNO(10706)
+ "Inline script, stylesheet or comment larger than "
+ "ProxyHTMLMaxBufSize (%" APR_SIZE_T_FMT
+ " bytes), not rewritten", max);
+ AP_fwrite(ctx, ctx->vb.buf, ctx->vb.strlen, 0);
+ ctx->vb.strlen = 0;
+ ctx->overflow = 1;
+ }
+ if (ctx->overflow) {
+ AP_fwrite(ctx, chars, length, 0);
+ }
+ else {
+ ap_varbuf_strmemcat(&ctx->vb, chars, length);
+ }
+}
+
static void pcdata(void *ctxt, const xmlChar *uchars, int length)
{
const char *chars = (const char*) uchars;
saxctxt *ctx = (saxctxt*) ctxt;
if (ctx->cfg->extfix) {
- ap_varbuf_strmemcat(&ctx->vb, chars, length);
+ buffer_text(ctx, chars, length);
}
else {
/* not sure if this should force-flush
@@ -378,9 +439,9 @@ static void pcomment(void *ctxt, const xmlChar *uchars)
return;
if (ctx->cfg->extfix) {
- ap_varbuf_strcat(&ctx->vb, "");
+ buffer_text(ctx, "", 3);
}
else {
ap_fputs(ctx->f->next, ctx->bb, "'
+ f'