View Javadoc

1   /*
2    * Copyright 2002-2009 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package net.sf.json.util;
18  
19  /**
20   * Defines base implementations for preventing WebHijack in AJAX applications.
21   * The default implementations are:
22   * <ul>
23   * <li>COMMENTS - wraps the string with /* *\/</li>
24   * <li>INFINITE_LOOP - prepends "while(1);"</li>
25   * </ul>
26   *
27   * @author Andres Almiray <aalmiray@users.sourceforge.net>
28   */
29  public abstract class WebHijackPreventionStrategy {
30     /** Wraps the string with /* *\/ */
31     public static final WebHijackPreventionStrategy COMMENTS = new CommentWebHijackPreventionStrategy();
32     /** Prepends "while(1);" */
33     public static final WebHijackPreventionStrategy INFINITE_LOOP = new InfiniteLoopWebHijackPreventionStrategy();
34  
35     /**
36      * Transforms the input with the desired strategy.<br>
37      *
38      * @param str a json string
39      * @return String - the transformed json string
40      */
41     public abstract String protect( String str );
42  
43     private static final class CommentWebHijackPreventionStrategy extends
44           WebHijackPreventionStrategy {
45        public String protect( String str ) {
46           return "/*" + str + "*/";
47        }
48     }
49  
50     private static final class InfiniteLoopWebHijackPreventionStrategy extends
51           WebHijackPreventionStrategy {
52        public String protect( String str ) {
53           return "while(1);" + str;
54        }
55     }
56  }