RhinoClassShutter.kt 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
  3. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4. *
  5. * This code is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 only, as
  7. * published by the Free Software Foundation. Oracle designates this
  8. * particular file as subject to the "Classpath" exception as provided
  9. * by Oracle in the LICENSE file that accompanied this code.
  10. *
  11. * This code is distributed in the hope that it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  14. * version 2 for more details (a copy is included in the LICENSE file that
  15. * accompanied this code).
  16. *
  17. * You should have received a copy of the GNU General Public License version
  18. * 2 along with this work; if not, write to the Free Software Foundation,
  19. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20. *
  21. * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22. * or visit www.oracle.com if you need additional information or have any
  23. * questions.
  24. */
  25. package com.script.rhino
  26. import org.mozilla.javascript.ClassShutter
  27. /**
  28. * This class prevents script access to certain sensitive classes.
  29. * Note that this class checks over and above SecurityManager. i.e., although
  30. * a SecurityManager would pass, class shutter may still prevent access.
  31. *
  32. * @author A. Sundararajan
  33. * @since 1.6
  34. */
  35. object RhinoClassShutter : ClassShutter {
  36. private val protectedClasses by lazy {
  37. hashSetOf(
  38. "dalvik.system",
  39. "java.lang.Class",
  40. "java.lang.ClassLoader",
  41. "java.net.URLClassLoader",
  42. "cn.hutool.core.lang.JarClassLoader",
  43. "org.mozilla.javascript.DefiningClassLoader",
  44. "java.lang.Runtime",
  45. "java.io.File",
  46. "java.security.AccessController",
  47. "java.nio.file.Paths",
  48. "java.nio.file.Files",
  49. "io.legado.app.data.AppDatabaseKt",
  50. "io.legado.app.utils.ContextExtensionsKt",
  51. "android.content.Intent",
  52. "androidx.core.content.FileProvider",
  53. "android.provider.Settings",
  54. "androidx.sqlite.db",
  55. "splitties.init.AppCtxKt",
  56. "android.app.ActivityThread",
  57. "android.app.AppGlobals"
  58. )
  59. }
  60. override fun visibleToScripts(fullClassName: String): Boolean {
  61. if (!protectedClasses.contains(fullClassName)) {
  62. var className = fullClassName
  63. while (className.contains(".")) {
  64. className = className.substringBeforeLast(".")
  65. if (protectedClasses.contains(className)) {
  66. return false
  67. }
  68. }
  69. return true
  70. }
  71. return false
  72. }
  73. }