View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.myfaces.tobago.sanitizer;
21  
22  import org.jsoup.Jsoup;
23  import org.jsoup.safety.Whitelist;
24  import org.slf4j.Logger;
25  import org.slf4j.LoggerFactory;
26  
27  import java.util.Enumeration;
28  import java.util.Properties;
29  
30  /**
31   * The JsoupSanitizer uses the jsoup library http://jsoup.org/ to check against malicious code.
32   */
33  public class JsoupSanitizer implements Sanitizer {
34  
35    private static final Logger LOG = LoggerFactory.getLogger(JsoupSanitizer.class);
36  
37    private Whitelist whitelist;
38    private String whitelistName;
39  
40    private boolean unmodifiable = false;
41  
42    public String sanitize(final String html) {
43  
44      final String safe = Jsoup.clean(html, whitelist);
45      if (LOG.isDebugEnabled()) {
46        LOG.debug("Sanitized: " + safe);
47      }
48      return safe;
49    }
50  
51    public void setProperties(final Properties configuration) {
52      checkLocked();
53  
54      unmodifiable = true;
55  
56      final Enumeration<String> enumeration = (Enumeration<String>) configuration.propertyNames();
57      while (enumeration.hasMoreElements()) {
58        String key =  enumeration.nextElement();
59        if ("whitelist".equals(key)) {
60          whitelistName = configuration.getProperty(key);
61          if ("basic".equals(whitelistName)) {
62            whitelist = Whitelist.basic();
63          } else if ("basicWithImages".equals(whitelistName)) {
64            whitelist = Whitelist.basicWithImages();
65          } else if ("none".equals(whitelistName)) {
66            whitelist = Whitelist.none();
67          } else if ("relaxed".equals(whitelistName)) {
68            whitelist = Whitelist.relaxed();
69          } else if ("simpleText".equals(whitelistName)) {
70            whitelist = Whitelist.simpleText();
71          } else {
72            throw new RuntimeException(
73                "Unknown configuration value for 'whitelist' in tobago-config.xml found! value='" + whitelistName + "'");
74          }
75        } else {
76          throw new RuntimeException("Unknown configuration key in tobago-config.xml found! key='" + key + "'");
77        }
78      }
79  
80      if (LOG.isInfoEnabled()) {
81        LOG.warn("Using whitelist '" + whitelistName + "' for sanitizing!");
82      }
83    }
84  
85    private void checkLocked() throws IllegalStateException {
86      if (unmodifiable) {
87        throw new RuntimeException("The configuration must not be changed after initialization!");
88      }
89    }
90  
91    @Override
92    public String toString() {
93      return getClass().getSimpleName() + " whitelist='" + whitelistName + "'";
94    }
95  
96  }