JDK-8214178 : JFileChooser on Windows causes broken link messages when a SecurityManager is defined
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 8u191,11.0.1,12
  • Priority: P3
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_7
  • CPU: x86_64
  • Submitted: 2018-11-19
  • Updated: 2019-01-17
  • Resolved: 2018-11-23
Related Reports
Duplicate :  
Relates :  
Description
ADDITIONAL SYSTEM INFORMATION :
Observed at least on Microsoft Windows 7 64-bit, and Microsoft Windows 10 64-bit, always with build 1.8.0_191. The problem does not occur with 1.8.0_181 or 1.8.0_171.

A DESCRIPTION OF THE PROBLEM :
On Microsoft Windows systems with b191, when a SecurityManager is defined, opening a JFileChooser in a folder with broken shortcuts causes system popup messages to appear: "The item <somefile> that this shortcut refers to has been changed or moved, so this shortcut will no longer work properly.  Do you want to delete this shortcut?" 

The problem occurs in sun.awt.shell.Win32ShellFolderManager2.checkFile(File file, SecurityManager sm). In revision http://hg.openjdk.java.net/jdk8u/jdk8u/jdk/rev/12e8aa264316 code was added to check link targets, which ends up calling Win32ShellFolder2.getLinkLocation(true). The call to getLinkLocation with resolve=true causes the popup messages.


REGRESSION : Last worked in version 8u181

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Create a file on Windows desktop. Use Windows menus to create a shortcut to that file, also on Windows desktop. Delete the first file, leaving the shortcut.
Run a Java program, where a security manager has been defined with System.setSecurityManager, and create an instance of JFileChooser.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
No popup messages.
ACTUAL -
A system popup message appears: "The item <somefile> that this shortcut refers to has been changed or moved, so this shortcut will no longer work properly.  Do you want to delete this shortcut?" 

---------- BEGIN SOURCE ----------
import java.io.*;
import javax.swing.JFileChooser;

/////////////////////////////////////
//
// NOTE:
// There must be a broken shortcut in the Windows desktop before running this program.
//
public class BrokenLinkBug {
    public static void main(String[] args) throws Exception {

        // Creates a temporary security policy
        final File tempPolicyFile = File.createTempFile("broken-links-bug-", ".policy");
        tempPolicyFile.deleteOnExit();
        try (final PrintStream policyStream = new PrintStream(new FileOutputStream(tempPolicyFile))) {
            policyStream.println("grant { permission java.security.AllPermission; };");
        }
        System.setProperty("java.security.policy", tempPolicyFile.getAbsolutePath());

        // Trigger the problem
        System.setSecurityManager(new SecurityManager());
        JFileChooser chooser = new JFileChooser();

        chooser.showDialog(null, "Open"); // not really needed
    }
}

---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
To circumvent the issue, define a security manager where checkRead throws SecurityException for broken shortcuts. In order to detect a broken shortcut, use a Win32ShellFolderManager32 to create an instance of Win32ShellFolder for the shortcut file, then invoke getLinkLocation(false) (via reflection as the method is not public) and confirm that the resulting target exists.

FREQUENCY : always



Comments
This bug and JDK-8213583 have the same root cause. Closing this one as dupe of JDK-8213583.
23-11-2018

Checked this for reported versions as well as subsequent and could confirm the issue. This looks similar to JDK-8213595, but test case confirms the issue exists with JDK 11.0.1 as well as 12 ea build. Results: ======= 8u181: OK 8u191: Fail 9: OK 11: Ok 11.0.1: Fail 12 ea b20: Fail To verify, - Create a sample file (sample.txt) in Desktop. - Create a shortcut to this file (sample.txt - Shortcut) at same location. - Delete the original file (sample.txt). - Run the attached test case with respective version. With JDK 8u191, 11.0.1 and 12 ea b20, the problem with Shortcut dialog start appearing.
21-11-2018