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   *   https://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.bcel.util;
21  
22  import static org.junit.jupiter.api.Assertions.assertThrows;
23  import static org.junit.jupiter.api.Assertions.assertTrue;
24  
25  import java.io.File;
26  import java.io.IOException;
27  
28  import org.apache.bcel.Const;
29  import org.apache.bcel.classfile.JavaClass;
30  import org.apache.bcel.generic.ClassGen;
31  import org.junit.jupiter.api.Test;
32  
33  /**
34   * Output file paths are built as "dir + className + suffix" from the attacker-controlled this_class constant.
35   * Names carrying Windows separators, drive designators or ".." must be rejected, or a crafted class file writes
36   * its HTML outside the target directory.
37   */
38  class Class2HTMLTraversalTest {
39  
40      private static File outputDir() {
41          final File outputDir = new File("target/test-output/html-traversal");
42          if (!outputDir.mkdirs()) {
43              assertTrue(outputDir.isDirectory());
44          }
45          return outputDir;
46      }
47  
48      @Test
49      void testRejectsClassNameWithDriveDesignator() {
50          final JavaClass jc = new ClassGen("C:pwn", "java.lang.Object", "pwn.java", Const.ACC_PUBLIC, null).getJavaClass();
51          assertThrows(IOException.class, () -> new Class2HTML(jc, outputDir().getAbsolutePath() + File.separator));
52      }
53  
54      @Test
55      void testRejectsClassNameWithWindowsTraversal() {
56          final JavaClass jc = new ClassGen("..\\..\\pwn", "java.lang.Object", "pwn.java", Const.ACC_PUBLIC, null).getJavaClass();
57          assertThrows(IOException.class, () -> new Class2HTML(jc, outputDir().getAbsolutePath() + File.separator));
58      }
59  }