/modules/core/portal-security-pacl/src/main/java/com/liferay/portal/security/pacl/checker/ReflectChecker.java

http://github.com/liferay/liferay-portal · Java · 123 lines · 74 code · 33 blank · 16 comment · 12 complexity · 1ea103a97aeb63223da760ef03b41423 MD5 · raw file

  1. /**
  2. * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
  3. *
  4. * This library is free software; you can redistribute it and/or modify it under
  5. * the terms of the GNU Lesser General Public License as published by the Free
  6. * Software Foundation; either version 2.1 of the License, or (at your option)
  7. * any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  12. * details.
  13. */
  14. package com.liferay.portal.security.pacl.checker;
  15. import com.liferay.portal.kernel.log.Log;
  16. import com.liferay.portal.kernel.log.LogFactoryUtil;
  17. import com.liferay.portal.security.pacl.Reflection;
  18. import java.security.Permission;
  19. /**
  20. * @author Brian Wing Shun Chan
  21. */
  22. public class ReflectChecker extends BaseChecker {
  23. @Override
  24. public void afterPropertiesSet() {
  25. initSuppressAccessChecks();
  26. }
  27. @Override
  28. public AuthorizationProperty generateAuthorizationProperty(
  29. Object... arguments) {
  30. if ((arguments == null) || (arguments.length != 1) ||
  31. !(arguments[0] instanceof Permission)) {
  32. return null;
  33. }
  34. Permission permission = (Permission)arguments[0];
  35. String name = permission.getName();
  36. String key = null;
  37. String value = null;
  38. if (name.startsWith(RUNTIME_PERMISSION_SUPPRESS_ACCESS_CHECKS)) {
  39. key = "security-manager-suppress-access-checks";
  40. value = Boolean.TRUE.toString();
  41. }
  42. else {
  43. return null;
  44. }
  45. AuthorizationProperty authorizationProperty =
  46. new AuthorizationProperty();
  47. authorizationProperty.setKey(key);
  48. authorizationProperty.setValue(value);
  49. return authorizationProperty;
  50. }
  51. @Override
  52. public boolean implies(Permission permission) {
  53. String name = permission.getName();
  54. if (name.startsWith(RUNTIME_PERMISSION_SUPPRESS_ACCESS_CHECKS)) {
  55. if (!hasSuppressAccessChecks(permission)) {
  56. logSecurityException(
  57. _log, "Attempted to suppess access checks");
  58. return false;
  59. }
  60. }
  61. else {
  62. int stackIndex = Reflection.getStackIndex(3, 2);
  63. Class<?> callerClass = Reflection.getCallerClass(stackIndex);
  64. if (isTrustedCaller(callerClass, permission)) {
  65. return true;
  66. }
  67. logSecurityException(_log, "Attempted to reflect");
  68. return false;
  69. }
  70. return true;
  71. }
  72. protected boolean hasSuppressAccessChecks(Permission permission) {
  73. if (_suppressAccessChecks) {
  74. return true;
  75. }
  76. int stackIndex = Reflection.getStackIndex(4, 3);
  77. Class<?> callerClass = Reflection.getCallerClass(stackIndex);
  78. if (isTrustedCaller(callerClass, permission)) {
  79. return true;
  80. }
  81. logSecurityException(_log, "Attempted to reflect");
  82. return false;
  83. }
  84. protected void initSuppressAccessChecks() {
  85. _suppressAccessChecks = getPropertyBoolean(
  86. "security-manager-suppress-access-checks");
  87. }
  88. private static final Log _log = LogFactoryUtil.getLog(ReflectChecker.class);
  89. private boolean _suppressAccessChecks;
  90. }