1+ /**
2+ * Logback: the reliable, generic, fast and flexible logging framework.
3+ * Copyright (C) 1999-2023, QOS.ch. All rights reserved.
4+ *
5+ * This program and the accompanying materials are dual-licensed under
6+ * either the terms of the Eclipse Public License v1.0 as published by
7+ * the Eclipse Foundation
8+ *
9+ * or (per the licensee's choosing)
10+ *
11+ * under the terms of the GNU Lesser General Public License version 2.1
12+ * as published by the Free Software Foundation.
13+ */
114package ch .qos .logback .core .net ;
215
16+ import ch .qos .logback .core .util .EnvUtil ;
17+
318import java .io .IOException ;
419import java .io .InputStream ;
520import java .io .InvalidClassException ;
621import java .io .ObjectInputStream ;
722import java .io .ObjectStreamClass ;
23+ import java .lang .reflect .InvocationTargetException ;
24+ import java .lang .reflect .Method ;
825import java .util .ArrayList ;
926import java .util .List ;
1027
@@ -22,10 +39,12 @@ public class HardenedObjectInputStream extends ObjectInputStream {
2239
2340 final List <String > whitelistedClassNames ;
2441 final static String [] JAVA_PACKAGES = new String [] { "java.lang" , "java.util" };
42+ final private static int DEPTH_LIMIT = 16 ;
43+ final private static int ARRAY_LIMIT = 10000 ;
2544
2645 public HardenedObjectInputStream (InputStream in , String [] whilelist ) throws IOException {
2746 super (in );
28-
47+ initObjectFilter ();
2948 this .whitelistedClassNames = new ArrayList <String >();
3049 if (whilelist != null ) {
3150 for (int i = 0 ; i < whilelist .length ; i ++) {
@@ -36,11 +55,43 @@ public HardenedObjectInputStream(InputStream in, String[] whilelist) throws IOEx
3655
3756 public HardenedObjectInputStream (InputStream in , List <String > whitelist ) throws IOException {
3857 super (in );
39-
58+ initObjectFilter ();
4059 this .whitelistedClassNames = new ArrayList <String >();
4160 this .whitelistedClassNames .addAll (whitelist );
4261 }
4362
63+ private void initObjectFilter () {
64+
65+ // invoke the following code by reflection
66+ // this.setObjectInputFilter(ObjectInputFilter.Config.createFilter(
67+ // "maxarray=" + ARRAY_LIMIT + ";maxdepth=" + DEPTH_LIMIT + ";"
68+ // ));
69+ if (EnvUtil .isJDK9OrHigher ()) {
70+ try {
71+ ClassLoader classLoader = this .getClass ().getClassLoader ();
72+
73+ Class oifClass = classLoader .loadClass ("java.io.ObjectInputFilter" );
74+ Class oifConfigClass = classLoader .loadClass ("java.io.ObjectInputFilter$Config" );
75+ Method setObjectInputFilterMethod = this .getClass ().getMethod ("setObjectInputFilter" , oifClass );
76+
77+ Method createFilterMethod = oifConfigClass .getMethod ("createFilter" , String .class );
78+ Object filter = createFilterMethod .invoke (null , "maxarray=" + ARRAY_LIMIT + ";maxdepth=" + DEPTH_LIMIT + ";" );
79+ setObjectInputFilterMethod .invoke (this , filter );
80+ } catch (ClassNotFoundException e ) {
81+ // this code should be unreachable
82+ throw new RuntimeException ("Failed to initialize object filter" , e );
83+ } catch (InvocationTargetException e ) {
84+ // this code should be unreachable
85+ throw new RuntimeException ("Failed to initialize object filter" , e );
86+ } catch (NoSuchMethodException e ) {
87+ // this code should be unreachable
88+ throw new RuntimeException ("Failed to initialize object filter" , e );
89+ } catch (IllegalAccessException e ) {
90+ // this code should be unreachable
91+ throw new RuntimeException ("Failed to initialize object filter" , e );
92+ }
93+ }
94+ }
4495 @ Override
4596 protected Class <?> resolveClass (ObjectStreamClass anObjectStreamClass ) throws IOException , ClassNotFoundException {
4697
0 commit comments