Author: toad
Date: 2006-05-04 12:50:19 +0000 (Thu, 04 May 2006)
New Revision: 8602

Modified:
   trunk/freenet/src/freenet/clients/http/filter/HTMLFilter.java
   trunk/freenet/src/freenet/node/Version.java
Log:
672:
Improvements to HTML filtering.

Modified: trunk/freenet/src/freenet/clients/http/filter/HTMLFilter.java
===================================================================
--- trunk/freenet/src/freenet/clients/http/filter/HTMLFilter.java       
2006-05-03 22:01:19 UTC (rev 8601)
+++ trunk/freenet/src/freenet/clients/http/filter/HTMLFilter.java       
2006-05-04 12:50:19 UTC (rev 8602)
@@ -135,6 +135,7 @@
                         * </p>
                         */
                        StringBuffer b = new StringBuffer(100);
+                       StringBuffer balt = new StringBuffer(4000);
                        Vector splitTag = new Vector();
                        char pprevC = 0;
                        char prevC = 0;
@@ -161,21 +162,30 @@
                                                        if (c == '<') {
                                                                saveText(b, w, 
this);
                                                                b.setLength(0);
+                                                               
balt.setLength(0);
                                                                mode = INTAG;
                                                        } else {
                                                                b.append(c);
                                                        }
                                                        break;
                                                case INTAG :
+                                                       balt.append(c);
                                                        if 
(HTMLDecoder.isWhitespace(c)) {
                                                                
splitTag.add(b.toString());
                                                                mode = 
INTAGWHITESPACE;
                                                                b.setLength(0);
+                                                       } else if (c == '<' && 
Character.isWhitespace(balt.charAt(0))) {
+                                                               // Previous was 
an un-escaped < in a script.
+                                                               saveText(balt, 
w, this);
+                                                               
balt.setLength(0);
+                                                               b.setLength(0);
+                                                               
splitTag.clear();
                                                        } else if (c == '>') {
                                                                
splitTag.add(b.toString());
                                                                b.setLength(0);
                                                                
processTag(splitTag, w, this);
                                                                
splitTag.clear();
+                                                               
balt.setLength(0);
                                                                mode = INTEXT;
                                                        } else if (
                                                                b.length() == 2
@@ -210,6 +220,7 @@
                                                                                
splitTag.clear();
                                                                                
b.setLength(0);
                                                                                
mode = INTEXT;
+                                                                               
balt.setLength(0);
                                                                                
// End tag now
                                                                        } else {
                                                                                
killTag = true;
@@ -237,6 +248,7 @@
                                                                                
        "<!-- Tags in string attribute -->");
                                                                                
splitTag.clear();
                                                                                
b.setLength(0);
+                                                                               
balt.setLength(0);
                                                                                
mode = INTEXT;
                                                                                
// End tag now
                                                                        } else {
@@ -298,7 +310,16 @@
                                                                        
processTag(splitTag, w, this);
                                                                killTag = false;
                                                                
splitTag.clear();
+                                                               b.setLength(0);
+                                                               
balt.setLength(0);
                                                                mode = INTEXT;
+                                                       } else if (c == '<' && 
Character.isWhitespace(balt.charAt(0))) {
+                                                               // Previous was 
an un-escaped < in a script.
+                                                               saveText(balt, 
w, this);
+                                                               
balt.setLength(0);
+                                                               b.setLength(0);
+                                                               
splitTag.clear();
+                                                               mode = INTAG;
                                                        } else if 
(HTMLDecoder.isWhitespace(c)) {
                                                                // More 
whitespace, what fun
                                                        } else {
@@ -334,6 +355,7 @@

        void saveText(StringBuffer s, Writer w, HTMLParseContext pc)
                throws IOException {
+               Logger.minor(this, "Saving text: "+s.toString());
                if (pc.killText) {
                        return;
                }
@@ -353,7 +375,16 @@
                        pc.currentStyleScriptChunk += style;
                        return; // is parsed and written elsewhere
                }
-               w.write(style);
+               StringBuffer out = new StringBuffer(s.length()*2);
+               for(int i=0;i<s.length();i++) {
+                       char c = s.charAt(i);
+                       if(c == '<') {
+                               out.append("&lt;");
+                       } else {
+                               out.append(c);
+                       }
+               }
+               w.write(out.toString());
        }

        void processTag(Vector splitTag, Writer w, HTMLParseContext pc)
@@ -390,6 +421,14 @@

        void saveComment(StringBuffer s, Writer w, HTMLParseContext pc)
                throws IOException {
+               if(s.length() > 3 && s.charAt(0) == '!' && s.charAt(1) == '-' 
&& s.charAt(2) == '-') {
+                       s.delete(0, 3);
+                       if(s.charAt(s.length()-1) == '-')
+                               s.setLength(s.length()-1);
+                       if(s.charAt(s.length()-1) == '-')
+                               s.setLength(s.length()-1);
+               }
+               Logger.minor(this, "Saving comment: "+s.toString());
                if (pc.expectingBadComment)
                        return; // ignore it

@@ -413,9 +452,9 @@
                        }
                }
                s = sb;
-               w.write('<');
+               w.write("<!-- ");
                w.write(s.toString());
-               w.write('>');
+               w.write(" -->");
        }

        static void throwFilterException(String s) throws DataFilterException {
@@ -1711,12 +1750,13 @@
        }

        static String sanitizeStyle(String style, FilterCallback cb) throws 
DataFilterException {
-               Logger.debug(
-                       HTMLFilter.class,
-                       "Sanitizing style: " + style);
                if(style == null) return null;
                Reader r = new StringReader(style);
                Writer w = new StringWriter();
+               style = style.trim();
+               Logger.minor(
+                               HTMLFilter.class,
+                               "Sanitizing style: " + style);
                CSSParser pc = new CSSParser(r, w, false, cb);
                try {
                        pc.parse();

Modified: trunk/freenet/src/freenet/node/Version.java
===================================================================
--- trunk/freenet/src/freenet/node/Version.java 2006-05-03 22:01:19 UTC (rev 
8601)
+++ trunk/freenet/src/freenet/node/Version.java 2006-05-04 12:50:19 UTC (rev 
8602)
@@ -20,7 +20,7 @@
        public static final String protocolVersion = "1.0";

        /** The build number of the current revision */
-       private static final int buildNumber = 671;
+       private static final int buildNumber = 672;

        /** Oldest build of Fred we will talk to */
        private static final int lastGoodBuild = 591;


Reply via email to