8182041: File Chooser Shortcut Panel folders under on JDK 9 8062648: FileSystemView.getDefaultDirectory() should check read access on Unix systems

Reviewed-by: serb, prr, psadhukhan
This commit is contained in:
Krishna Addepalli 2018-10-05 14:35:24 +05:30
parent fc807d0914
commit 995f6963b3
6 changed files with 154 additions and 21 deletions

View file

@ -583,18 +583,29 @@ public abstract class FileSystemView {
} }
/** /**
* Returns an array of files representing the values to show by default in * Returns an array of files representing the values which will be shown
* the file chooser selector. * in the file chooser selector.
* *
* @return an array of {@code File} objects. * @return an array of {@code File} objects. The array returned may be
* @throws SecurityException if the caller does not have necessary * possibly empty if there are no appropriate permissions.
* permissions
* @since 9 * @since 9
*/ */
public File[] getChooserComboBoxFiles() { public File[] getChooserComboBoxFiles() {
return (File[]) ShellFolder.get("fileChooserComboBoxFolders"); return (File[]) ShellFolder.get("fileChooserComboBoxFolders");
} }
/**
* Returns an array of files representing the values to show by default in
* the file chooser shortcuts panel.
*
* @return an array of {@code File} objects. The array returned may be
* possibly empty if there are no appropriate permissions.
* @since 12
*/
final public File[] getChooserShortcutPanelFiles() {
return (File[]) ShellFolder.get("fileChooserShortcutPanelFolders");
}
/** /**
* Returns whether the specified file denotes a shell interpreted link which * Returns whether the specified file denotes a shell interpreted link which
* can be obtained by the {@link #getLinkLocation(File)}. * can be obtained by the {@link #getLinkLocation(File)}.

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -28,6 +28,8 @@ package sun.awt.shell;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
import java.util.stream.Stream;
/** /**
* @author Michael Martak * @author Michael Martak
@ -68,13 +70,13 @@ class ShellFolderManager {
// Return the default shellfolder for a new filechooser // Return the default shellfolder for a new filechooser
File homeDir = new File(System.getProperty("user.home")); File homeDir = new File(System.getProperty("user.home"));
try { try {
return createShellFolder(homeDir); return checkFile(createShellFolder(homeDir));
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
return homeDir; return checkFile(homeDir);
} }
} else if (key.equals("roots")) { } else if (key.equals("roots")) {
// The root(s) of the displayable hierarchy // The root(s) of the displayable hierarchy
return File.listRoots(); return checkFiles(File.listRoots());
} else if (key.equals("fileChooserComboBoxFolders")) { } else if (key.equals("fileChooserComboBoxFolders")) {
// Return an array of ShellFolders representing the list to // Return an array of ShellFolders representing the list to
// show by default in the file chooser's combobox // show by default in the file chooser's combobox
@ -84,11 +86,42 @@ class ShellFolderManager {
// folders, such as Desktop, Documents, History, Network, Home, etc. // folders, such as Desktop, Documents, History, Network, Home, etc.
// This is used in the shortcut panel of the filechooser on Windows 2000 // This is used in the shortcut panel of the filechooser on Windows 2000
// and Windows Me // and Windows Me
return new File[] { (File)get("fileChooserDefaultFolder") }; return checkFiles(new File[] { (File)get("fileChooserDefaultFolder") });
} }
return null; return null;
} }
private static File checkFile(File f) {
SecurityManager sm = System.getSecurityManager();
return (sm == null || f == null) ? f : checkFile(f, sm);
}
private static File checkFile(File f, SecurityManager sm) {
try {
sm.checkRead(f.getPath());
if (f instanceof ShellFolder) {
ShellFolder sf = (ShellFolder)f;
if (sf.isLink()) {
sm.checkRead(sf.getLinkLocation().getPath());
}
}
return f;
} catch (SecurityException | FileNotFoundException e) {
return null;
}
}
private static File[] checkFiles(File[] fs) {
SecurityManager sm = System.getSecurityManager();
return (sm == null || fs == null) ? fs : checkFiles(Stream.of(fs), sm);
}
private static File[] checkFiles(Stream<File> fs, SecurityManager sm) {
return fs.filter(f -> f != null && checkFile(f, sm) != null)
.toArray(File[]::new);
}
/** /**
* Does {@code dir} represent a "computer" such as a node on the network, or * Does {@code dir} represent a "computer" such as a node on the network, or
* "My Computer" on the desktop. * "My Computer" on the desktop.

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2003, 2014, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -24,19 +24,36 @@
*/ */
package sun.swing; package sun.swing;
import java.awt.*; import java.awt.Dimension;
import java.awt.event.*; import java.awt.Insets;
import java.awt.Color;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener; import java.beans.PropertyChangeListener;
import java.io.*;
import java.io.File;
import java.security.AccessController; import java.security.AccessController;
import java.security.PrivilegedAction; import java.security.PrivilegedAction;
import javax.swing.*; import javax.swing.JToolBar;
import javax.swing.border.*; import javax.swing.JFileChooser;
import javax.swing.filechooser.*; import javax.swing.JToggleButton;
import javax.swing.ButtonGroup;
import javax.swing.UIManager;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.Box;
import sun.awt.shell.*; import javax.swing.border.EmptyBorder;
import javax.swing.border.BevelBorder;
import javax.swing.filechooser.FileSystemView;
import sun.awt.shell.ShellFolder;
import sun.awt.OSInfo; import sun.awt.OSInfo;
/** /**
@ -81,7 +98,7 @@ public class WindowsPlacesBar extends JToolBar
setBackground(bgColor); setBackground(bgColor);
FileSystemView fsv = fc.getFileSystemView(); FileSystemView fsv = fc.getFileSystemView();
files = (File[]) ShellFolder.get("fileChooserShortcutPanelFolders"); files = fsv.getChooserShortcutPanelFiles();
buttons = new JToggleButton[files.length]; buttons = new JToggleButton[files.length];
buttonGroup = new ButtonGroup(); buttonGroup = new ButtonGroup();

View file

@ -0,0 +1,59 @@
/*
* Copyright (c) 2018 Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 8182041
* @summary Tests if the files(Shortcut Panle files, FileChooser ComboBox files)
* are filtered out when run with SecurityManager enabled.
* @run main/othervm/policy=shellfolderqueries.policy ShellFolderQueriesSecurityManagerTest
*/
import javax.swing.filechooser.FileSystemView;
import java.io.File;
import java.util.Arrays;
public class ShellFolderQueriesSecurityManagerTest {
static final FileSystemView fsv = FileSystemView.getFileSystemView();
public static void main(String[] args) throws Exception {
try {
File[] shortcuts = fsv.getChooserShortcutPanelFiles();
Arrays.asList(shortcuts).forEach(System.out::println);
if (shortcuts.length != 0) {
throw new RuntimeException("Shortcut panel files leaked from SecurityManager.");
}
File[] cbFiles = fsv.getChooserComboBoxFiles();
Arrays.asList(cbFiles).forEach(System.out::println);
if (cbFiles.length != 0) {
throw new RuntimeException("Combobox Files leaked from SecurityManager.");
}
System.out.println("ok");
} catch (SecurityException e) {
throw new RuntimeException(e);
}
}
}

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2016, 2018 Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -23,7 +23,7 @@
/* /*
* @test * @test
* @bug 8081722 * @bug 8081722 8182041
* @summary Provide public API for file hierarchy provided by * @summary Provide public API for file hierarchy provided by
* sun.awt.shell.ShellFolder * sun.awt.shell.ShellFolder
* @author Semyon Sadetsky * @author Semyon Sadetsky
@ -53,6 +53,7 @@ public class ShellFolderQueriesTest {
System.out.println("Windows detected: will run shortcut test"); System.out.println("Windows detected: will run shortcut test");
testGet(); testGet();
testLink(); testLink();
testShortcutPanelFiles();
} else { } else {
testGet(); testGet();
} }
@ -119,4 +120,11 @@ public class ShellFolderQueriesTest {
} }
} }
} }
private static void testShortcutPanelFiles() {
File[] shortcuts = fsv.getChooserShortcutPanelFiles();
if (shortcuts.length == 0) {
throw new RuntimeException("No shortcut panel files found.");
}
}
} }

View file

@ -0,0 +1,5 @@
grant {
permission java.util.PropertyPermission "user.home", "read";
permission java.util.PropertyPermission "os.name", "read";
permission java.util.PropertyPermission "os.version", "read";
};