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 @Override
public final Clipboard getSystemClipboard() { public final Clipboard getSystemClipboard() {
@SuppressWarnings("removal")
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkPermission(AWTPermissions.ACCESS_CLIPBOARD_PERMISSION);
}
synchronized (this) { synchronized (this) {
if (clipboard == null) { if (clipboard == null) {
clipboard = createPlatformClipboard(); clipboard = createPlatformClipboard();

View File

@ -44,7 +44,6 @@ import javax.accessibility.AccessibleStateSet;
import javax.accessibility.AccessibleText; import javax.accessibility.AccessibleText;
import javax.swing.text.AttributeSet; import javax.swing.text.AttributeSet;
import sun.awt.AWTPermissions;
import sun.awt.InputMethodSupport; import sun.awt.InputMethodSupport;
/** /**
@ -744,20 +743,6 @@ public sealed class TextComponent extends Component implements Accessible
return str + ",selection=" + getSelectionStart() + "-" + getSelectionEnd(); 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. * Serialization support.
*/ */

View File

@ -33,7 +33,6 @@ import java.io.Serial;
import java.util.Arrays; import java.util.Arrays;
import sun.awt.AWTAccessor; import sun.awt.AWTAccessor;
import sun.awt.AWTPermissions;
import sun.util.logging.PlatformLogger; 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 * A flag that indicates that this instance can be used to access
* the system clipboard. * the system clipboard.
* This should be false in a headless environment, true in a headful one.
*/ */
private transient boolean canAccessSystemClipboard; private transient boolean canAccessSystemClipboard;
@ -385,26 +385,7 @@ public abstract sealed class InputEvent extends ComponentEvent
} }
private boolean canAccessSystemClipboard() { private boolean canAccessSystemClipboard() {
boolean b = false; return !GraphicsEnvironment.isHeadless();
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;
} }
/** /**

View File

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

View File

@ -224,18 +224,6 @@ public abstract class SunDropTargetContextPeer implements DropTargetContextPeer,
InvalidDnDOperationException 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; Long lFormat = null;
Transferable localTransferable = local; Transferable localTransferable = local;

View File

@ -189,10 +189,6 @@ public class SwingUtilities2 {
public static final StringUIClientPropertyKey BASICMENUITEMUI_MAX_TEXT_OFFSET = public static final StringUIClientPropertyKey BASICMENUITEMUI_MAX_TEXT_OFFSET =
new StringUIClientPropertyKey ("maxTextOffset"); new StringUIClientPropertyKey ("maxTextOffset");
// security stuff
private static final String UntrustedClipboardAccess =
"UNTRUSTED_CLIPBOARD_ACCESS_KEY";
//all access to charsBuffer is to be synchronized on charsBufferLock //all access to charsBuffer is to be synchronized on charsBufferLock
private static final int CHAR_BUFFER_SIZE = 100; private static final int CHAR_BUFFER_SIZE = 100;
private static final Object charsBufferLock = new Object(); 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 * checks if the system clipboard can be accessed.
* * This is true in a headful environment, false in a headless one
* for untrusted context (see isTrustedContext) checks the
* permissions for the current event being handled
* *
*/ */
public static boolean canAccessSystemClipboard() { public static boolean canAccessSystemClipboard() {
boolean canAccess = false; return !GraphicsEnvironment.isHeadless();
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;
}
} }
/** /**
@ -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) { public static String displayPropertiesToCSS(Font font, Color fg) {
StringBuilder rule = new StringBuilder("body {"); StringBuilder rule = new StringBuilder("body {");
if (font != null) { if (font != null) {

View File

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

View File

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

View File

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

View File

@ -53,7 +53,6 @@ struct EnableEditingStruct {
* AwtTextComponent fields * AwtTextComponent fields
*/ */
jmethodID AwtTextComponent::canAccessClipboardMID;
AwtTextComponent::OleCallback AwtTextComponent::sm_oleCallback; AwtTextComponent::OleCallback AwtTextComponent::sm_oleCallback;
WNDPROC AwtTextComponent::sm_pDefWindowProc = NULL; WNDPROC AwtTextComponent::sm_pDefWindowProc = NULL;
@ -392,31 +391,10 @@ AwtTextComponent::HandleEvent(MSG *msg, BOOL synthetic)
return returnVal; 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 MsgRouting
AwtTextComponent::WmPaste() 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; return mrDoDefault;
}
} }
//im --- override to over the spot composition //im --- override to over the spot composition
@ -890,29 +868,6 @@ Java_sun_awt_windows_WTextComponentPeer_enableEditing(JNIEnv *env,
CATCH_BAD_ALLOC; 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. * Inner class OleCallback definition.
*/ */

View File

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