-
Notifications
You must be signed in to change notification settings - Fork 858
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Move SecurityManager relevant parts to SecurityBridge #1068
Changes from all commits
80e7470
7be1971
01ee5e9
3d27878
60e784c
846e8e6
fdfdd23
3edcb08
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,11 @@ | |
* domain of the script that triggered the current action. It is required for JavaAdapters to have | ||
* the same <code>ProtectionDomain</code> as the script code that created them. Embeddings that | ||
* implement their own SecurityManager can use this as base class. | ||
* | ||
* @deprecated This class is only useful in conjunction with {@link SecurityManager}, which is | ||
* deprecated since JDK17 and subject to removal in a future release | ||
*/ | ||
@Deprecated | ||
public class RhinoSecurityManager extends SecurityManager { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Class made deprecated. No other code changes |
||
|
||
/** | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,7 +19,12 @@ | |
import java.util.Map; | ||
import java.util.WeakHashMap; | ||
|
||
/** @author Attila Szegedi */ | ||
/** | ||
* @author Attila Szegedi | ||
* @deprecated This class depends on {@link AccessController}, which is deprecated since JDK17 and | ||
* subject to removal in a future release | ||
*/ | ||
@Deprecated | ||
public abstract class SecureCaller { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Class made deprecated. No other code changes (Note: this class is used nowhere in Rhino, so possible used only by embedders) |
||
private static final byte[] secureCallerImplBytecode = loadBytecode(); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
package org.mozilla.javascript; | ||
|
||
import java.security.ProtectionDomain; | ||
|
||
/** | ||
* Bridge to security relevant operations, that have to be handled with SecurityManager up to JDK | ||
* 17. | ||
* | ||
* <p>Notice: With JEP411, the SecurityManager is deprecated. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't the tag be closed? |
||
* | ||
* @author Roland Praml | ||
*/ | ||
interface SecurityBridge { | ||
|
||
/** @see SecurityUtilities#getSystemProperty(String) */ | ||
public String getSystemProperty(final String name); | ||
|
||
/** @see SecurityUtilities#getProtectionDomain(Class) */ | ||
public ProtectionDomain getProtectionDomain(final Class<?> clazz); | ||
|
||
/** @see SecurityUtilities#getScriptProtectionDomain() */ | ||
public ProtectionDomain getScriptProtectionDomain(); | ||
|
||
/** @see SecurityUtilities#getSecurityContext() */ | ||
Object getSecurityContext(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
package org.mozilla.javascript; | ||
|
||
import java.security.ProtectionDomain; | ||
|
||
/** This is a "no-op" implementation of SecurityBridge and should work for JDK17 and beyond. */ | ||
public class SecurityBridge_NoOp implements SecurityBridge { | ||
|
||
@Override | ||
public String getSystemProperty(final String name) { | ||
return System.getProperty(name); | ||
} | ||
|
||
@Override | ||
public ProtectionDomain getProtectionDomain(final Class<?> clazz) { | ||
return clazz.getProtectionDomain(); | ||
} | ||
|
||
@Override | ||
public ProtectionDomain getScriptProtectionDomain() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public Object getSecurityContext() { | ||
return null; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
package org.mozilla.javascript; | ||
|
||
import java.security.AccessControlContext; | ||
import java.security.AccessController; | ||
import java.security.AllPermission; | ||
import java.security.Permission; | ||
import java.security.PrivilegedAction; | ||
import java.security.ProtectionDomain; | ||
|
||
/** | ||
* Code moved from {@link SecurityUtilities}. This implementation makes use of {@link | ||
* SecurityManager} and {@link AccessController} and so on, which is deprecated with JDK17 (see <a | ||
* href='https://openjdk.java.net/jeps/411'>JEP411</a>) - so all related 'java.security' stuff | ||
* should be routed over this class, so that it could be easily replaced by an other implementation | ||
* like {@link SecurityBridge_NoOp}. | ||
* | ||
* <p>This implementation should be work up to JDK17 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. According to JEP 411, the only API that will not work out the box in Java 18 will be I'd suggest setting |
||
* | ||
* @author Attila Szegedi | ||
* @author Roland Praml, FOCONIS AG | ||
*/ | ||
@Deprecated | ||
public class SecurityBridge_SecurityManager implements SecurityBridge { | ||
private static final Permission allPermission = new AllPermission(); | ||
/** | ||
* Retrieves a system property within a privileged block. Use it only when the property is used | ||
* from within Rhino code and is not passed out of it. | ||
* | ||
* @param name the name of the system property | ||
* @return the value of the system property | ||
*/ | ||
@Override | ||
public String getSystemProperty(final String name) { | ||
return AccessController.doPrivileged( | ||
new PrivilegedAction<String>() { | ||
@Override | ||
public String run() { | ||
return System.getProperty(name); | ||
} | ||
}); | ||
} | ||
Comment on lines
+29
to
+47
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The Probably not a big deal, but I wonder if the security bridge classes could be made package-visible. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could be really a problem |
||
|
||
@Override | ||
public ProtectionDomain getProtectionDomain(final Class<?> clazz) { | ||
return AccessController.doPrivileged( | ||
new PrivilegedAction<ProtectionDomain>() { | ||
@Override | ||
public ProtectionDomain run() { | ||
return clazz.getProtectionDomain(); | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* Look up the top-most element in the current stack representing a script and return its | ||
* protection domain. This relies on the system-wide SecurityManager being an instance of {@link | ||
* RhinoSecurityManager}, otherwise it returns <code>null</code>. | ||
* | ||
* @return The protection of the top-most script in the current stack, or null | ||
*/ | ||
@Override | ||
public ProtectionDomain getScriptProtectionDomain() { | ||
final SecurityManager securityManager = System.getSecurityManager(); | ||
if (securityManager instanceof RhinoSecurityManager) { | ||
return AccessController.doPrivileged( | ||
new PrivilegedAction<ProtectionDomain>() { | ||
@Override | ||
public ProtectionDomain run() { | ||
Class<?> c = | ||
SecurityUtilities.getCurrentScriptClass( | ||
(RhinoSecurityManager) securityManager); | ||
return c == null ? null : c.getProtectionDomain(); | ||
} | ||
}); | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
public Object getSecurityContext() { | ||
Object sec = null; | ||
SecurityManager sm = System.getSecurityManager(); | ||
if (sm != null) { | ||
sec = sm.getSecurityContext(); | ||
if (sec instanceof AccessControlContext) { | ||
try { | ||
((AccessControlContext) sec).checkPermission(allPermission); | ||
// if we have allPermission, we do not need to store the | ||
// security object in the cache key | ||
return null; | ||
} catch (SecurityException e) { | ||
} | ||
} | ||
} | ||
return sec; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Class made deprecated. No other code changes