View Javadoc
1   /*
2    * Copyright (C) 2003-2012 eXo Platform SAS.
3    *
4    * This program is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Affero General Public License
6    * as published by the Free Software Foundation; either version 3
7    * of the License, or (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU General Public License
15   * along with this program; if not, see<http://www.gnu.org/licenses/>.
16   */
17  package org.exoplatform.ecm.webui.form.validator;
18  
19  import org.apache.commons.lang.StringUtils;
20  
21  import org.exoplatform.commons.utils.HTMLSanitizer;
22  import org.exoplatform.web.application.ApplicationMessage;
23  import org.exoplatform.webui.exception.MessageException;
24  import org.exoplatform.webui.form.UIFormInput;
25  import org.exoplatform.webui.form.validator.Validator;
26  
27  /**
28   * Created by The eXo Platform SAS Author : Tran Hung Phong
29   * phongth@exoplatform.com Jul 24, 2012
30   */
31  public class XSSValidator implements Validator {
32  
33    @Override
34    public void validate(UIFormInput uiInput) throws Exception {
35      String inputValue = ((String) uiInput.getValue());
36      if (inputValue == null || inputValue.trim().length() == 0) {
37        return;
38      }
39  
40      inputValue = HTMLSanitizer.sanitize(inputValue);
41      if (StringUtils.isEmpty(inputValue)) {
42        String message = "UIActionForm.msg.xss-vulnerability-character";
43        if (uiInput.getLabel() == null)
44          message = "UIActionForm.msg.xss-vulnerability-character-wo-label";
45        Object[] args = { uiInput.getLabel() };
46        throw new MessageException(new ApplicationMessage(message, args, ApplicationMessage.WARNING));
47      }
48    }
49  }