Author: toad
Date: 2006-03-17 20:35:08 +0000 (Fri, 17 Mar 2006)
New Revision: 8268

Added:
   trunk/freenet/src/freenet/clients/http/filter/
   trunk/freenet/src/freenet/clients/http/filter/CharsetExtractor.java
   trunk/freenet/src/freenet/clients/http/filter/ContentDataFilter.java
   trunk/freenet/src/freenet/clients/http/filter/ContentFilter.java
   trunk/freenet/src/freenet/clients/http/filter/FilterCallback.java
   trunk/freenet/src/freenet/clients/http/filter/MIMEType.java
Log:
Beginnings of content anonymity filter.

Added: trunk/freenet/src/freenet/clients/http/filter/CharsetExtractor.java
===================================================================
--- trunk/freenet/src/freenet/clients/http/filter/CharsetExtractor.java 
2006-03-17 20:14:47 UTC (rev 8267)
+++ trunk/freenet/src/freenet/clients/http/filter/CharsetExtractor.java 
2006-03-17 20:35:08 UTC (rev 8268)
@@ -0,0 +1,13 @@
+package freenet.clients.http.filter;
+
+import freenet.support.Bucket;
+
+/**
+ * For a specific text/-based MIME type, extracts the charset if
+ * possible.
+ */
+public interface CharsetExtractor {
+       
+       String getCharset(Bucket data);
+
+}

Added: trunk/freenet/src/freenet/clients/http/filter/ContentDataFilter.java
===================================================================
--- trunk/freenet/src/freenet/clients/http/filter/ContentDataFilter.java        
2006-03-17 20:14:47 UTC (rev 8267)
+++ trunk/freenet/src/freenet/clients/http/filter/ContentDataFilter.java        
2006-03-17 20:35:08 UTC (rev 8268)
@@ -0,0 +1,12 @@
+package freenet.clients.http.filter;
+
+import freenet.support.Bucket;
+
+/**
+ * Data filter for a specific MIME type.
+ */
+public interface ContentDataFilter {
+       
+       public Bucket filter(Bucket data, String charset, FilterCallback cb);
+
+}

Added: trunk/freenet/src/freenet/clients/http/filter/ContentFilter.java
===================================================================
--- trunk/freenet/src/freenet/clients/http/filter/ContentFilter.java    
2006-03-17 20:14:47 UTC (rev 8267)
+++ trunk/freenet/src/freenet/clients/http/filter/ContentFilter.java    
2006-03-17 20:35:08 UTC (rev 8268)
@@ -0,0 +1,89 @@
+package freenet.clients.http.filter;
+
+import java.util.Hashtable;
+
+/**
+ * Freenet content filter. This doesn't actually do any filtering,
+ * it organizes everything and maintains the database.
+ */
+public class ContentFilter {
+
+       static final Hashtable mimeTypesByName = new Hashtable();
+       
+       static {
+               init();
+       }
+       
+       public static void init() {
+               // Register known MIME types
+               
+               // Plain text
+               register(new MIMEType("text/plain", "txt", new String[0], new 
String[] { "text", "pot" },
+                               true, true, null, null, false, false, false, 
false, false, false, 
+                               "Plain text - not dangerous unless your browser 
is stupid (e.g. Internet Explorer)",
+                               "Plain text - not dangerous unless you include 
compromizing information",
+                               true, "iso-8859-1", null));
+               
+               // GIF - probably safe - FIXME check this out, write filters 
+               register(new MIMEType("image/gif", "gif", new String[0], new 
String[0], 
+                               true, false, null, null, false, false, false, 
false, false, false,
+                               "GIF image - probably not dangerous",
+                               "GIF image - probably not dangerous but you 
should wipe any comments",
+                               false, null, null));
+               
+               // JPEG - probably safe - FIXME check this out, write filters
+               register(new MIMEType("image/jpeg", "jpeg", new String[0], new 
String[] { "jpg" },
+                               true, true, null, null, false, false, false, 
false, false, false,
+                               "JPEG image - probably not dangerous",
+                               "JPEG image - probably not dangerous", false, 
null, null));
+               
+               // PNG - probably safe - FIXME check this out, write filters
+               register(new MIMEType("image/png", "png", new String[0], new 
String[0],
+                               true, false, null, null, false, false, false, 
false, true, false,
+                               "PNG image - probably not dangerous",
+                               "PNG image - probably not dangerous but you 
should wipe any comments or text blocks",
+                               false, null, null));
+               
+               // PDF - very dangerous - FIXME ideally we would have a filter, 
this is such a common format...
+               register(new MIMEType("application/pdf", "pdf", new String[] { 
"application/x-pdf" }, new String[0],
+                               false, false, null, null, true, true, true, 
true, true, true,
+                               "Adobe(R) PDF document - VERY DANGEROUS!",
+                               "Adobe(R) PDF document - VERY DANGEROUS!",
+                               false, null, null));
+               
+               // HTML - dangerous if not filtered
+               register(new MIMEType("text/html", "html", new String[] { 
"text/xhtml", "text/xml+xhtml" }, new String[] { "htm" },
+                               false, false /* maybe? */, new 
HTMLReadFilter(), new HTMLWriteFilter(), 
+                               true, true, true, true, true, true, false,
+                               "HTML - not dangerous if filtered",
+                               "HTML - may contain dangerous metadata etc; 
suggest you check it by hand",
+                               true, "iso-8859-1", new 
HTMLCharsetExtractor()));
+               
+               // CSS - danagerous if not filtered, not sure about the filter
+               register(new MIMEType("text/css", "css", new String[0], new 
String[0],
+                               false, false /* unknown */, new 
CSSReadFilter(), null,
+                               true, true, true, true, true, false,
+                               "CSS (cascading style sheet, usually used with 
HTML) - probably not dangerous if filtered, but the filter is not a whitelist 
filter so take care",
+                               "CSS (cascading style sheet, usually used with 
HTML) - this can probably contain metadata, check it by hand",
+                               true, "iso-8859-1", new CSSCharsetExtractor()));
+               
+       }
+       
+       public static void register(MIMEType mimeType) {
+               synchronized(mimeTypesByName) {
+                       mimeTypesByName.put(mimeType.primaryMimeType, mimeType);
+                       String[] alt = mimeType.alternateMimeTypes;
+                       if(alt != null && alt.length > 0) {
+                               for(int i=0;i<alt.length;i++)
+                                       mimeTypesByName.put(alt[i], mimeType);
+                       }
+               }
+       }
+
+       public static MIMEType getMIMEType(String mimeType) {
+               return (MIMEType) mimeTypesByName.get(mimeType);
+       }
+       
+       public static 
+       
+}

Added: trunk/freenet/src/freenet/clients/http/filter/FilterCallback.java
===================================================================
--- trunk/freenet/src/freenet/clients/http/filter/FilterCallback.java   
2006-03-17 20:14:47 UTC (rev 8267)
+++ trunk/freenet/src/freenet/clients/http/filter/FilterCallback.java   
2006-03-17 20:35:08 UTC (rev 8268)
@@ -0,0 +1,26 @@
+package freenet.clients.http.filter;
+
+import freenet.keys.FreenetURI;
+
+/**
+ * Callback to be provided to a content filter.
+ */
+public interface FilterCallback {
+
+       /**
+        * Process a URI.
+        * If it cannot be turned into something sufficiently safe, then return 
null.
+        */
+       public FreenetURI processURI(FreenetURI uri);
+
+       /**
+        * Should we allow GET forms?
+        */
+       public boolean allowGetForms();
+       
+       /**
+        * Should we allow POST forms?
+        */
+       public boolean allowPostForms();
+       
+}

Added: trunk/freenet/src/freenet/clients/http/filter/MIMEType.java
===================================================================
--- trunk/freenet/src/freenet/clients/http/filter/MIMEType.java 2006-03-17 
20:14:47 UTC (rev 8267)
+++ trunk/freenet/src/freenet/clients/http/filter/MIMEType.java 2006-03-17 
20:35:08 UTC (rev 8268)
@@ -0,0 +1,79 @@
+package freenet.clients.http.filter;
+
+/**
+ * A MIME type, for purposes of the filter.
+ */
+public class MIMEType {
+       
+       final String primaryMimeType;
+       final String[] alternateMimeTypes;
+       
+       final String primaryExtension;
+       final String[] alternateExtensions;
+       
+       /** Is the data safe to read as-is? This is true for text/plain. */
+       final boolean safeToRead;
+       
+       /** Is the data safe to write as-is? */
+       final boolean safeToWrite;
+       
+       /** Content filter to make data safe to read */
+       final ContentDataFilter readFilter;
+       
+       /** Content filter to make data safe to write */
+       final ContentDataFilter writeFilter;
+
+       // Detail. Not necessarily an exhaustive list.
+       
+       final boolean dangerousLinks;
+       
+       final boolean dangerousInlines;
+       
+       final boolean dangerousScripting;
+       
+       final boolean dangerousReadMetadata;
+       
+       final boolean dangerousWriteMetadata;
+       
+       final boolean dangerousToWriteEvenWithFilter;
+       
+       // These are in addition to the above
+       
+       final String readDescription;
+       
+       final String writeDescription;
+       
+       final boolean takesACharset;
+       
+       final String defaultCharset;
+       
+       final CharsetExtractor charsetExtractor;
+       
+       MIMEType(String type, String ext, String[] extraTypes, String[] 
extraExts,
+                       boolean safeToRead, boolean safeToWrite, 
ContentDataFilter readFilter,
+                       ContentDataFilter writeFilter, boolean dangerousLinks, 
boolean dangerousInlines,
+                       boolean dangerousScripting, boolean 
dangerousReadMetadata, 
+                       boolean dangerousWriteMetadata, boolean 
dangerousToWriteEvenWithFilter, 
+                       String readDescription, String writeDescription, 
boolean takesACharset, 
+                       String defaultCharset, CharsetExtractor 
charsetExtractor) {
+               this.primaryMimeType = type;
+               this.primaryExtension = ext;
+               this.alternateMimeTypes = extraTypes;
+               this.alternateExtensions = extraExts;
+               this.safeToRead = safeToRead;
+               this.safeToWrite = safeToWrite;
+               this.readFilter = readFilter;
+               this.writeFilter = writeFilter;
+               this.dangerousLinks = dangerousLinks;
+               this.dangerousInlines = dangerousInlines;
+               this.dangerousScripting = dangerousScripting;
+               this.dangerousReadMetadata = dangerousReadMetadata;
+               this.dangerousWriteMetadata = dangerousWriteMetadata;
+               this.dangerousToWriteEvenWithFilter = 
dangerousToWriteEvenWithFilter;
+               this.readDescription = readDescription;
+               this.writeDescription = writeDescription;
+               this.takesACharset = takesACharset;
+               this.defaultCharset = defaultCharset;
+               this.charsetExtractor = charsetExtractor;
+       }
+}


Reply via email to