8344896: Remove obsolete checks for AWTPermission accessClipboard

Reviewed-by: azvegint
This commit is contained in:
Phil Race 2024-11-25 19:31:22 +00:00
parent 08dfc4a42e
commit 4d898aa451
11 changed files with 10 additions and 280 deletions

View File

@ -407,12 +407,6 @@ public abstract class LWToolkit extends SunToolkit implements Runnable {
@Override
public final Clipboard getSystemClipboard() {
@SuppressWarnings("removal")
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkPermission(AWTPermissions.ACCESS_CLIPBOARD_PERMISSION);
}
synchronized (this) {
if (clipboard == null) {
clipboard = createPlatformClipboard();

View File

@ -44,7 +44,6 @@ import javax.accessibility.AccessibleStateSet;
import javax.accessibility.AccessibleText;
import javax.swing.text.AttributeSet;
import sun.awt.AWTPermissions;
import sun.awt.InputMethodSupport;
/**
@ -744,20 +743,6 @@ public sealed class TextComponent extends Component implements Accessible
return str + ",selection=" + getSelectionStart() + "-" + getSelectionEnd();
}
/**
* Assigns a valid value to the canAccessClipboard instance variable.
*/
private boolean canAccessClipboard() {
@SuppressWarnings("removal")
SecurityManager sm = System.getSecurityManager();
if (sm == null) return true;
try {
sm.checkPermission(AWTPermissions.ACCESS_CLIPBOARD_PERMISSION);
return true;
} catch (SecurityException e) {}
return false;
}
/*
* Serialization support.
*/

View File

@ -33,7 +33,6 @@ import java.io.Serial;
import java.util.Arrays;
import sun.awt.AWTAccessor;
import sun.awt.AWTPermissions;
import sun.util.logging.PlatformLogger;
/**
@ -313,6 +312,7 @@ public abstract sealed class InputEvent extends ComponentEvent
/*
* A flag that indicates that this instance can be used to access
* the system clipboard.
* This should be false in a headless environment, true in a headful one.
*/
private transient boolean canAccessSystemClipboard;
@ -385,26 +385,7 @@ public abstract sealed class InputEvent extends ComponentEvent
}
private boolean canAccessSystemClipboard() {
boolean b = false;
if (!GraphicsEnvironment.isHeadless()) {
@SuppressWarnings("removal")
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
try {
sm.checkPermission(AWTPermissions.ACCESS_CLIPBOARD_PERMISSION);
b = true;
} catch (SecurityException se) {
if (logger.isLoggable(PlatformLogger.Level.FINE)) {
logger.fine("InputEvent.canAccessSystemClipboard() got SecurityException ", se);
}
}
} else {
b = true;
}
}
return b;
return !GraphicsEnvironment.isHeadless();
}
/**

View File

@ -467,12 +467,10 @@ public class DefaultCaret extends Rectangle implements Caret, FocusListener, Mou
// mouse 1 behavior
if(nclicks == 1) {
selectedWordEvent = null;
} else if(nclicks == 2
&& SwingUtilities2.canEventAccessSystemClipboard(e)) {
} else if (nclicks == 2) {
selectWord(e);
selectedWordEvent = null;
} else if(nclicks == 3
&& SwingUtilities2.canEventAccessSystemClipboard(e)) {
} else if (nclicks == 3) {
Action a = null;
ActionMap map = getComponent().getActionMap();
if (map != null) {
@ -489,8 +487,7 @@ public class DefaultCaret extends Rectangle implements Caret, FocusListener, Mou
}
} else if (SwingUtilities.isMiddleMouseButton(e)) {
// mouse 2 behavior
if (nclicks == 1 && component.isEditable() && component.isEnabled()
&& SwingUtilities2.canEventAccessSystemClipboard(e)) {
if (nclicks == 1 && component.isEditable() && component.isEnabled()) {
// paste system selection, if it exists
JTextComponent c = (JTextComponent) e.getSource();
if (c != null) {
@ -547,8 +544,7 @@ public class DefaultCaret extends Rectangle implements Caret, FocusListener, Mou
} else {
shouldHandleRelease = false;
adjustCaretAndFocus(e);
if (nclicks == 2
&& SwingUtilities2.canEventAccessSystemClipboard(e)) {
if (nclicks == 2) {
selectWord(e);
}
}
@ -1394,9 +1390,6 @@ public class DefaultCaret extends Rectangle implements Caret, FocusListener, Mou
}
private void updateSystemSelection() {
if ( ! SwingUtilities2.canCurrentEventAccessSystemClipboard() ) {
return;
}
if (this.dot != this.mark && component != null && component.hasFocus()) {
Clipboard clip = getSystemSelection();
if (clip != null) {

View File

@ -224,18 +224,6 @@ public abstract class SunDropTargetContextPeer implements DropTargetContextPeer,
InvalidDnDOperationException
{
@SuppressWarnings("removal")
SecurityManager sm = System.getSecurityManager();
try {
if (!dropInProcess && sm != null) {
sm.checkPermission(AWTPermissions.ACCESS_CLIPBOARD_PERMISSION);
}
} catch (Exception e) {
Thread currentThread = Thread.currentThread();
currentThread.getUncaughtExceptionHandler().uncaughtException(currentThread, e);
return null;
}
Long lFormat = null;
Transferable localTransferable = local;

View File

@ -189,10 +189,6 @@ public class SwingUtilities2 {
public static final StringUIClientPropertyKey BASICMENUITEMUI_MAX_TEXT_OFFSET =
new StringUIClientPropertyKey ("maxTextOffset");
// security stuff
private static final String UntrustedClipboardAccess =
"UNTRUSTED_CLIPBOARD_ACCESS_KEY";
//all access to charsBuffer is to be synchronized on charsBufferLock
private static final int CHAR_BUFFER_SIZE = 100;
private static final Object charsBufferLock = new Object();
@ -1458,122 +1454,13 @@ public class SwingUtilities2 {
}
}
/*
* here goes the fix for 4856343 [Problem with applet interaction
* with system selection clipboard]
*
* NOTE. In case isTrustedContext() no checking
* are to be performed
*/
/**
* checks the security permissions for accessing system clipboard
*
* for untrusted context (see isTrustedContext) checks the
* permissions for the current event being handled
* checks if the system clipboard can be accessed.
* This is true in a headful environment, false in a headless one
*
*/
public static boolean canAccessSystemClipboard() {
boolean canAccess = false;
if (!GraphicsEnvironment.isHeadless()) {
@SuppressWarnings("removal")
SecurityManager sm = System.getSecurityManager();
if (sm == null) {
canAccess = true;
} else {
try {
sm.checkPermission(AWTPermissions.ACCESS_CLIPBOARD_PERMISSION);
canAccess = true;
} catch (SecurityException e) {
}
if (canAccess && ! isTrustedContext()) {
canAccess = canCurrentEventAccessSystemClipboard(true);
}
}
}
return canAccess;
}
/**
* Returns true if EventQueue.getCurrentEvent() has the permissions to
* access the system clipboard
*/
public static boolean canCurrentEventAccessSystemClipboard() {
return isTrustedContext()
|| canCurrentEventAccessSystemClipboard(false);
}
/**
* Returns true if the given event has permissions to access the
* system clipboard
*
* @param e AWTEvent to check
*/
public static boolean canEventAccessSystemClipboard(AWTEvent e) {
return isTrustedContext()
|| canEventAccessSystemClipboard(e, false);
}
/**
* Returns true if the given event is current gesture for
* accessing clipboard
*
* @param ie InputEvent to check
*/
@SuppressWarnings("deprecation")
private static boolean isAccessClipboardGesture(InputEvent ie) {
boolean allowedGesture = false;
if (ie instanceof KeyEvent) { //we can validate only keyboard gestures
KeyEvent ke = (KeyEvent)ie;
int keyCode = ke.getKeyCode();
int keyModifiers = ke.getModifiers();
switch(keyCode) {
case KeyEvent.VK_C:
case KeyEvent.VK_V:
case KeyEvent.VK_X:
allowedGesture = (keyModifiers == InputEvent.CTRL_MASK);
break;
case KeyEvent.VK_INSERT:
allowedGesture = (keyModifiers == InputEvent.CTRL_MASK ||
keyModifiers == InputEvent.SHIFT_MASK);
break;
case KeyEvent.VK_COPY:
case KeyEvent.VK_PASTE:
case KeyEvent.VK_CUT:
allowedGesture = true;
break;
case KeyEvent.VK_DELETE:
allowedGesture = ( keyModifiers == InputEvent.SHIFT_MASK);
break;
}
}
return allowedGesture;
}
/**
* Returns true if e has the permissions to
* access the system clipboard and if it is allowed gesture (if
* checkGesture is true)
*
* @param e AWTEvent to check
* @param checkGesture boolean
*/
private static boolean canEventAccessSystemClipboard(AWTEvent e,
boolean checkGesture) {
if (EventQueue.isDispatchThread()) {
/*
* Checking event permissions makes sense only for event
* dispatching thread
*/
if (e instanceof InputEvent
&& (! checkGesture || isAccessClipboardGesture((InputEvent)e))) {
return AWTAccessor.getInputEventAccessor().
canAccessSystemClipboard((InputEvent) e);
} else {
return false;
}
} else {
return true;
}
return !GraphicsEnvironment.isHeadless();
}
/**
@ -1590,31 +1477,6 @@ public class SwingUtilities2 {
}
}
/**
* Returns true if EventQueue.getCurrentEvent() has the permissions to
* access the system clipboard and if it is allowed gesture (if
* checkGesture true)
*
* @param checkGesture boolean
*/
private static boolean canCurrentEventAccessSystemClipboard(boolean
checkGesture) {
AWTEvent event = EventQueue.getCurrentEvent();
return canEventAccessSystemClipboard(event, checkGesture);
}
/**
* see RFE 5012841 [Per AppContect security permissions] for the
* details
*
*/
@SuppressWarnings("removal")
private static boolean isTrustedContext() {
return (System.getSecurityManager() == null)
|| (AppContext.getAppContext().
get(UntrustedClipboardAccess) == null);
}
public static String displayPropertiesToCSS(Font font, Color fg) {
StringBuilder rule = new StringBuilder("body {");
if (font != null) {

View File

@ -114,7 +114,6 @@ import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;
@ -129,7 +128,6 @@ import javax.swing.LookAndFeel;
import javax.swing.UIDefaults;
import sun.awt.AWTAccessor;
import sun.awt.AWTPermissions;
import sun.awt.AppContext;
import sun.awt.DisplayChangedListener;
import sun.awt.LightweightFrame;
@ -1233,11 +1231,6 @@ public final class XToolkit extends UNIXToolkit implements Runnable {
@Override
public Clipboard getSystemClipboard() {
@SuppressWarnings("removal")
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkPermission(AWTPermissions.ACCESS_CLIPBOARD_PERMISSION);
}
synchronized (this) {
if (clipboard == null) {
clipboard = new XClipboard("System", "CLIPBOARD");
@ -1248,11 +1241,6 @@ public final class XToolkit extends UNIXToolkit implements Runnable {
@Override
public Clipboard getSystemSelection() {
@SuppressWarnings("removal")
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkPermission(AWTPermissions.ACCESS_CLIPBOARD_PERMISSION);
}
synchronized (this) {
if (selection == null) {
selection = new XClipboard("Selection", "PRIMARY");

View File

@ -32,10 +32,6 @@ import java.awt.event.TextEvent;
abstract
class WTextComponentPeer extends WComponentPeer implements TextComponentPeer {
static {
initIDs();
}
// TextComponentPeer implementation
@Override
@ -107,11 +103,6 @@ class WTextComponentPeer extends WComponentPeer implements TextComponentPeer {
postEvent(new TextEvent(target, TextEvent.TEXT_VALUE_CHANGED));
}
/**
* Initialize JNI field and method IDs
*/
private static native void initIDs();
@Override
public boolean shouldClearRectBeforePaint() {
return false;

View File

@ -123,7 +123,6 @@ import javax.swing.text.JTextComponent;
import sun.awt.AWTAccessor;
import sun.awt.AWTAutoShutdown;
import sun.awt.AWTPermissions;
import sun.awt.AppContext;
import sun.awt.DisplayChangedListener;
import sun.awt.LightweightFrame;
@ -678,11 +677,6 @@ public final class WToolkit extends SunToolkit implements Runnable {
@Override
public Clipboard getSystemClipboard() {
@SuppressWarnings("removal")
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkPermission(AWTPermissions.ACCESS_CLIPBOARD_PERMISSION);
}
synchronized (this) {
if (clipboard == null) {
clipboard = new WClipboard();

View File

@ -53,7 +53,6 @@ struct EnableEditingStruct {
* AwtTextComponent fields
*/
jmethodID AwtTextComponent::canAccessClipboardMID;
AwtTextComponent::OleCallback AwtTextComponent::sm_oleCallback;
WNDPROC AwtTextComponent::sm_pDefWindowProc = NULL;
@ -392,32 +391,11 @@ AwtTextComponent::HandleEvent(MSG *msg, BOOL synthetic)
return returnVal;
}
/*
* If this Paste is occurring because of a synthetic Java event (e.g.,
* a synthesized <CTRL>-V KeyEvent), then verify that the TextComponent
* has permission to access the Clipboard before pasting. If permission
* is denied, we should throw a SecurityException, but currently do not
* because when we detect the security violation, we are in the Toolkit
* thread, not the thread which dispatched the illegal event.
*/
MsgRouting
AwtTextComponent::WmPaste()
{
if (m_synthetic) {
JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
if (env->EnsureLocalCapacity(1) < 0) {
return mrConsume;
}
jobject target = GetTarget(env);
jboolean canAccessClipboard =
env->CallBooleanMethod (target, AwtTextComponent::canAccessClipboardMID);
env->DeleteLocalRef(target);
return (canAccessClipboard) ? mrDoDefault : mrConsume;
}
else {
return mrDoDefault;
}
}
//im --- override to over the spot composition
void AwtTextComponent::SetCompositionWindow(RECT& rc)
@ -890,29 +868,6 @@ Java_sun_awt_windows_WTextComponentPeer_enableEditing(JNIEnv *env,
CATCH_BAD_ALLOC;
}
/*
* Class: sun_awt_windows_WTextComponentPeer
* Method: initIDs
* Signature: ()V
*/
JNIEXPORT void JNICALL
Java_sun_awt_windows_WTextComponentPeer_initIDs(JNIEnv *env, jclass cls)
{
TRY;
jclass textComponentClassID = env->FindClass("java/awt/TextComponent");
CHECK_NULL(textComponentClassID);
AwtTextComponent::canAccessClipboardMID =
env->GetMethodID(textComponentClassID, "canAccessClipboard", "()Z");
env->DeleteLocalRef(textComponentClassID);
DASSERT(AwtTextComponent::canAccessClipboardMID != NULL);
CATCH_BAD_ALLOC;
}
/************************************************************************
* Inner class OleCallback definition.
*/

View File

@ -41,7 +41,6 @@
class AwtTextComponent : public AwtComponent {
public:
static jmethodID canAccessClipboardMID;
AwtTextComponent();