From 4d898aa451db5b57601c088cdc3c947827f0bc08 Mon Sep 17 00:00:00 2001 From: Phil Race Date: Mon, 25 Nov 2024 19:31:22 +0000 Subject: [PATCH] 8344896: Remove obsolete checks for AWTPermission accessClipboard Reviewed-by: azvegint --- .../macosx/classes/sun/lwawt/LWToolkit.java | 6 - .../share/classes/java/awt/TextComponent.java | 15 -- .../classes/java/awt/event/InputEvent.java | 23 +-- .../javax/swing/text/DefaultCaret.java | 15 +- .../sun/awt/dnd/SunDropTargetContextPeer.java | 12 -- .../classes/sun/swing/SwingUtilities2.java | 144 +----------------- .../unix/classes/sun/awt/X11/XToolkit.java | 12 -- .../sun/awt/windows/WTextComponentPeer.java | 9 -- .../classes/sun/awt/windows/WToolkit.java | 6 - .../libawt/windows/awt_TextComponent.cpp | 47 +----- .../native/libawt/windows/awt_TextComponent.h | 1 - 11 files changed, 10 insertions(+), 280 deletions(-) diff --git a/src/java.desktop/macosx/classes/sun/lwawt/LWToolkit.java b/src/java.desktop/macosx/classes/sun/lwawt/LWToolkit.java index 0de6b213299..5c8148a668f 100644 --- a/src/java.desktop/macosx/classes/sun/lwawt/LWToolkit.java +++ b/src/java.desktop/macosx/classes/sun/lwawt/LWToolkit.java @@ -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(); diff --git a/src/java.desktop/share/classes/java/awt/TextComponent.java b/src/java.desktop/share/classes/java/awt/TextComponent.java index 9c33172d9f5..20c129795a2 100644 --- a/src/java.desktop/share/classes/java/awt/TextComponent.java +++ b/src/java.desktop/share/classes/java/awt/TextComponent.java @@ -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. */ diff --git a/src/java.desktop/share/classes/java/awt/event/InputEvent.java b/src/java.desktop/share/classes/java/awt/event/InputEvent.java index 58adec9f635..3da01fd639c 100644 --- a/src/java.desktop/share/classes/java/awt/event/InputEvent.java +++ b/src/java.desktop/share/classes/java/awt/event/InputEvent.java @@ -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(); } /** diff --git a/src/java.desktop/share/classes/javax/swing/text/DefaultCaret.java b/src/java.desktop/share/classes/javax/swing/text/DefaultCaret.java index cf8220efb75..bed18fc3fc3 100644 --- a/src/java.desktop/share/classes/javax/swing/text/DefaultCaret.java +++ b/src/java.desktop/share/classes/javax/swing/text/DefaultCaret.java @@ -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) { diff --git a/src/java.desktop/share/classes/sun/awt/dnd/SunDropTargetContextPeer.java b/src/java.desktop/share/classes/sun/awt/dnd/SunDropTargetContextPeer.java index b0c362acc9e..c6346fddebd 100644 --- a/src/java.desktop/share/classes/sun/awt/dnd/SunDropTargetContextPeer.java +++ b/src/java.desktop/share/classes/sun/awt/dnd/SunDropTargetContextPeer.java @@ -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; diff --git a/src/java.desktop/share/classes/sun/swing/SwingUtilities2.java b/src/java.desktop/share/classes/sun/swing/SwingUtilities2.java index eb35920aad0..7c2b5517e15 100644 --- a/src/java.desktop/share/classes/sun/swing/SwingUtilities2.java +++ b/src/java.desktop/share/classes/sun/swing/SwingUtilities2.java @@ -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,123 +1454,14 @@ 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; + return !GraphicsEnvironment.isHeadless(); } - /** - * 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; - } - } /** * Utility method that throws SecurityException if SecurityManager is set @@ -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) { diff --git a/src/java.desktop/unix/classes/sun/awt/X11/XToolkit.java b/src/java.desktop/unix/classes/sun/awt/X11/XToolkit.java index 5a2a849b757..bc6275aaacc 100644 --- a/src/java.desktop/unix/classes/sun/awt/X11/XToolkit.java +++ b/src/java.desktop/unix/classes/sun/awt/X11/XToolkit.java @@ -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"); diff --git a/src/java.desktop/windows/classes/sun/awt/windows/WTextComponentPeer.java b/src/java.desktop/windows/classes/sun/awt/windows/WTextComponentPeer.java index 762672ad48b..e725e7f3754 100644 --- a/src/java.desktop/windows/classes/sun/awt/windows/WTextComponentPeer.java +++ b/src/java.desktop/windows/classes/sun/awt/windows/WTextComponentPeer.java @@ -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; diff --git a/src/java.desktop/windows/classes/sun/awt/windows/WToolkit.java b/src/java.desktop/windows/classes/sun/awt/windows/WToolkit.java index f2fbfa9b77d..1ede804d449 100644 --- a/src/java.desktop/windows/classes/sun/awt/windows/WToolkit.java +++ b/src/java.desktop/windows/classes/sun/awt/windows/WToolkit.java @@ -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(); diff --git a/src/java.desktop/windows/native/libawt/windows/awt_TextComponent.cpp b/src/java.desktop/windows/native/libawt/windows/awt_TextComponent.cpp index fff4237911c..81d44b9d760 100644 --- a/src/java.desktop/windows/native/libawt/windows/awt_TextComponent.cpp +++ b/src/java.desktop/windows/native/libawt/windows/awt_TextComponent.cpp @@ -53,7 +53,6 @@ struct EnableEditingStruct { * AwtTextComponent fields */ -jmethodID AwtTextComponent::canAccessClipboardMID; AwtTextComponent::OleCallback AwtTextComponent::sm_oleCallback; WNDPROC AwtTextComponent::sm_pDefWindowProc = NULL; @@ -392,31 +391,10 @@ AwtTextComponent::HandleEvent(MSG *msg, BOOL synthetic) return returnVal; } -/* - * If this Paste is occurring because of a synthetic Java event (e.g., - * a synthesized -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; - } + return mrDoDefault; } //im --- override to over the spot composition @@ -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. */ diff --git a/src/java.desktop/windows/native/libawt/windows/awt_TextComponent.h b/src/java.desktop/windows/native/libawt/windows/awt_TextComponent.h index 2a9eecc4e64..6bc81e3ef45 100644 --- a/src/java.desktop/windows/native/libawt/windows/awt_TextComponent.h +++ b/src/java.desktop/windows/native/libawt/windows/awt_TextComponent.h @@ -41,7 +41,6 @@ class AwtTextComponent : public AwtComponent { public: - static jmethodID canAccessClipboardMID; AwtTextComponent();