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.Properties;
28  
29  /**
30   * The JsoupSanitizer uses the jsoup library http://jsoup.org/ to check against malicious code.
31   */
32  public class JsoupSanitizer implements Sanitizer {
33  
34    private static final Logger LOG = LoggerFactory.getLogger(JsoupSanitizer.class);
35  
36    private Whitelist whitelist;
37    private String whitelistName;
38  
39    private boolean unmodifiable = false;
40  
41    @Override
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    @Override
52    public void setProperties(final Properties configuration) {
53      checkLocked();
54  
55      unmodifiable = true;
56  
57      for (final String key : configuration.stringPropertyNames()) {
58        if ("whitelist".equals(key)) {
59          whitelistName = configuration.getProperty(key);
60          if ("basic".equals(whitelistName)) {
61            whitelist = Whitelist.basic();
62          } else if ("basicWithImages".equals(whitelistName)) {
63            whitelist = Whitelist.basicWithImages();
64          } else if ("none".equals(whitelistName)) {
65            whitelist = Whitelist.none();
66          } else if ("relaxed".equals(whitelistName)) {
67            whitelist = Whitelist.relaxed();
68          } else if ("simpleText".equals(whitelistName)) {
69            whitelist = Whitelist.simpleText();
70          } else {
71            throw new RuntimeException(
72                "Unknown configuration value for 'whitelist' in tobago-config.xml found! value='" + whitelistName + "'");
73          }
74        } else {
75          throw new RuntimeException("Unknown configuration key in tobago-config.xml found! key='" + key + "'");
76        }
77      }
78  
79      if (LOG.isInfoEnabled()) {
80        LOG.warn("Using whitelist '" + whitelistName + "' for sanitizing!");
81      }
82    }
83  
84    private void checkLocked() throws IllegalStateException {
85      if (unmodifiable) {
86        throw new RuntimeException("The configuration must not be changed after initialization!");
87      }
88    }
89  
90    @Override
91    public String toString() {
92      return getClass().getSimpleName() + " whitelist='" + whitelistName + "'";
93    }
94  
95  }