On 25/Nov/10 21:50, Sam Varshavchik wrote:
> Alessandro Vesely writes:
>> How about writing something different than spaces, e.g. <noscript/> or
>> <!-- -->?
>
> That's a very good idea. I should've thought of it myself…
I attach a tentative patch. The largest chunk is for conditional
standalone compiling (so I've lightly tested it, although I haven't
installed it yet.)
As it turns out, MS office produces an <html> tag full of
xmlns-definition attributes, which is about 2Kb long and thus puts the
sanitizer in DOS attack mode for the whole entity. I've changed it so
as to affect the current tag only.
Besides that and the new nuke() function, you'll find a couple of
paranoid out-of-memory checks, conversion of funny <o:p>'s (office
paragraph), and removal of "class" attributes. Not thoroughly good,
but I hope I haven't worsened it either. How does it look like?
--
--- webmail/html.original.c 2009-02-06 03:04:50.000000000 +0100
+++ webmail/html.c 2010-11-26 18:59:00.000000000 +0100
@@ -90,7 +90,7 @@
The ONLOAD, ONMOUSEOVER, and all other ON* attributes are removed.
Attributes BACKGROUND, STYLE, TARGET, CODE, ACTION, CODETYPE and
- LANGUAGE are removed.
+ LANGUAGE are removed. Also CLASS, since we import no foreign
stylesheet.
TARGET=_blank is added to all <A> tags.
HREF, SRC, or LOWSRC attributes that do not specify a URL of http:,
@@ -101,7 +101,7 @@
static void addtagbuf(struct htmlfilter_info *info, int c)
{
- if (info->tagbufsize >= 1024) return; /* DOS attack - get rid of the
tag */
+ if (info->tagbuflen >= 1023) return; /* DOS attack - get rid of the tag
*/
if (info->tagbuflen >= info->tagbufsize)
{
@@ -117,6 +117,20 @@
info->tagbuf[info->tagbuflen++]=c;
}
+static void nuke(struct htmlfilter_info *info)
+{
+ if (info->tagbuf)
+ {
+ size_t l = strlen(info->tagbuf);
+ memset(info->tagbuf, ' ', l);
+ if (l > 5)
+ {
+ memcpy(info->tagbuf, "!--", 3);
+ info->tagbuf[l-2] = info->tagbuf[l-1] = '-';
+ }
+ }
+}
+
/* Parse the contents of tagbuf into individual attributes. If argument is
** NULL, just the count of attributes is returned. That's the first pass.
** On the second pass the argument points to a struct tagattrinfo array which
@@ -155,10 +169,8 @@
tai->tagvaluelen=0;
}
while (*p && isspace((int)(unsigned char)*p)) p++;
- if (*p == '=')
- memset(info->tagbuf, ' ',
- strlen(info->tagbuf));
-
+ if (*p == '=')
+ nuke(info);
}
else
{
@@ -182,13 +194,13 @@
if (*p) p++;
else
{
- memset(info->tagbuf, ' ',
strlen(info->tagbuf));
+ nuke(info);
}
}
else
{
if (c == 0)
- memset(info->tagbuf, ' ',
strlen(info->tagbuf));
+ nuke(info);
if (tai)
{
@@ -219,6 +231,8 @@
{
char *p;
+ info->tagbuf[info->tagbuflen] = 0; // if it was too long
+
while ((p=strchr(info->tagbuf, '<')) != NULL)
*p=' ';
@@ -265,6 +279,20 @@
info->tagattr=newta;
}
info->tagattrlen=parseattr(info, info->tagattr);
+
+ /* get rid of namespace, if any */
+ if ((p = info->tagattr->tagname) != NULL)
+ {
+ size_t l = info->tagattr->tagnamelen;
+ char *q = memchr(p, ':', l);
+ if (q)
+ {
+ size_t diff = ++q - p, rest = l - diff;
+ memmove(p, q, rest);
+ memset(p + rest, ' ', diff);
+ info->tagattr->tagnamelen = diff;
+ }
+ }
}
/* See if this attribute is the one we're looking for */
@@ -298,9 +326,9 @@
will call htmlfiltered() with the filtered text, more or less on a
one to one basis.
- htmlfilter_init() must be called before the first invocation of
+ htmlfilter_alloc() must be called before the first invocation of
htmlfilter(). Because the HTML can be fed in arbitrary quantities,
- htmlfilter() implements a state machine which htmlfilter_init()
+ htmlfilter() implements a state machine which htmlfilter_alloc()
initializes.
*/
@@ -409,7 +437,6 @@
/* We're in a tag (not a <!-- comment)
collect the contents in tagbuf, until > is seen */
- info->cur_state=intag;
if (p[l] == '>')
{
start=l+1;
@@ -509,7 +536,7 @@
size_t plen;
size_t i;
- if (!tagattrp->tagvalue)
+ if (!tagattrp->tagvalue || info->tagbuf)
return;
plen=tagattrp->tagvalue - info->tagbuf;
@@ -645,6 +672,9 @@
size_t l;
int open_window=0;
+ if (!info->tagbuf)
+ return;
+
addtagbuf(info, 0);
parsetagbuf(info);
@@ -794,7 +824,8 @@
for (l=1; l<info->tagattrlen; l++)
{
- if (is_attr(info->tagattr+l, "target") ||
+ if (is_attr(info->tagattr+l, "class") ||
+ is_attr(info->tagattr+l, "target") ||
is_attr(info->tagattr+l, "code") ||
is_attr(info->tagattr+l, "language") ||
is_attr(info->tagattr+l, "action") ||
@@ -888,3 +919,72 @@
(*info->htmlfiltered_func)(">", 1);
}
+
+#if defined TEST_MAIN
+
+/*
+* For testing, compile as a standalone that will get an html file
+* (i.e. not qp-encoded) on stdin and produce filtered output on stdout.
+* compile e.g. as
+*
+* gcc -o testhtml -DTEST_MAIN -DHAVE_CONFIG -Wall -O0 -g -I.. html.c \
+* -L../rfc2045/.libs -L../cgi/.libs -L../rfc822/.libs -L../unicode/.libs \
+* -lcgi -lrfc2045 -lrfc822 -lunicode -lidn
+*/
+
+static void text_to_stdout(const char *p, size_t n)
+{
+ while (n)
+ {
+ --n;
+ putchar(*p++);
+ }
+}
+
+static char *convertcid(const char *cidurl, void *voidp)
+{
+ char *p = (char*)voidp;
+ char *q = malloc(strlen(cidurl) + strlen(p) + 4);
+ if (q)
+ sprintf(q, "%s(%s)", p, cidurl);
+ return q;
+}
+
+static char linebuf[4096];
+
+int main(int argc, char *argv[])
+{
+ struct htmlfilter_info *hf_info;
+
+ hf_info=htmlfilter_alloc(&text_to_stdout);
+
+ if (hf_info)
+ {
+ htmlfilter_washlink(hf_info, "htmlfilter_washlink");
+ htmlfilter_convertcid(hf_info, &convertcid,
"htmlfilter_convertcid");
+ htmlfilter_contentbase(hf_info, "htmlfilter_contentbase");
+ htmlfilter_washlinkmailto(hf_info, "htmlfilter_washlinkmailto");
+
+ while (fgets(linebuf, sizeof linebuf, stdin))
+ htmlfilter(hf_info, linebuf, strlen(linebuf));
+
+ htmlfilter_free(hf_info);
+ }
+
+ return 0;
+}
+
+// unused references
+#include <assert.h>
+void fake_exit(int n)
+{
+ assert(0);
+ exit(n);
+}
+
+void rfc2045_error(const char *p)
+{
+ assert(0);
+}
+
+#endif
------------------------------------------------------------------------------
Increase Visibility of Your 3D Game App & Earn a Chance To Win $500!
Tap into the largest installed PC base & get more eyes on your game by
optimizing for Intel(R) Graphics Technology. Get started today with the
Intel(R) Software Partner Program. Five $500 cash prizes are up for grabs.
http://p.sf.net/sfu/intelisp-dev2dev
_______________________________________________
courier-users mailing list
[email protected]
Unsubscribe: https://lists.sourceforge.net/lists/listinfo/courier-users