Skip to content

Commit 325366e

Browse files
committed
8345141: Remove uses of SecurityManager in ShellFolder related classes
Reviewed-by: azvegint, honkar
1 parent d88c7b3 commit 325366e

File tree

5 files changed

+25
-138
lines changed

5 files changed

+25
-138
lines changed

src/java.desktop/share/classes/sun/awt/shell/ShellFolder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ public Image getIcon(int width, int height) {
232232
managerClass = null;
233233
}
234234
// swallow the exceptions below and use default shell folder
235-
} catch (ClassNotFoundException | SecurityException | NullPointerException e) {
235+
} catch (ClassNotFoundException | NullPointerException e) {
236236
}
237237

238238
if (managerClass == null) {

src/java.desktop/share/classes/sun/awt/shell/ShellFolderManager.java

Lines changed: 4 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@
2828
import java.io.File;
2929
import java.io.FileNotFoundException;
3030
import java.util.concurrent.Callable;
31-
import java.util.stream.Stream;
32-
3331

3432
/**
3533
* @author Michael Martak
@@ -70,13 +68,13 @@ public Object get(String key) {
7068
// Return the default shellfolder for a new filechooser
7169
File homeDir = new File(System.getProperty("user.home"));
7270
try {
73-
return checkFile(createShellFolder(homeDir));
71+
return createShellFolder(homeDir);
7472
} catch (FileNotFoundException e) {
75-
return checkFile(homeDir);
73+
return homeDir;
7674
}
7775
} else if (key.equals("roots")) {
7876
// The root(s) of the displayable hierarchy
79-
return checkFiles(File.listRoots());
77+
return File.listRoots();
8078
} else if (key.equals("fileChooserComboBoxFolders")) {
8179
// Return an array of ShellFolders representing the list to
8280
// show by default in the file chooser's combobox
@@ -86,44 +84,12 @@ public Object get(String key) {
8684
// folders, such as Desktop, Documents, History, Network, Home, etc.
8785
// This is used in the shortcut panel of the filechooser on Windows 2000
8886
// and Windows Me
89-
return checkFiles(new File[] { (File)get("fileChooserDefaultFolder") });
87+
return new File[] { (File)get("fileChooserDefaultFolder") };
9088
}
9189

9290
return null;
9391
}
9492

95-
private static File checkFile(File f) {
96-
@SuppressWarnings("removal")
97-
SecurityManager sm = System.getSecurityManager();
98-
return (sm == null || f == null) ? f : checkFile(f, sm);
99-
}
100-
101-
private static File checkFile(File f, @SuppressWarnings("removal") SecurityManager sm) {
102-
try {
103-
sm.checkRead(f.getPath());
104-
if (f instanceof ShellFolder) {
105-
ShellFolder sf = (ShellFolder)f;
106-
if (sf.isLink()) {
107-
sm.checkRead(sf.getLinkLocation().getPath());
108-
}
109-
}
110-
return f;
111-
} catch (SecurityException | FileNotFoundException e) {
112-
return null;
113-
}
114-
}
115-
116-
private static File[] checkFiles(File[] fs) {
117-
@SuppressWarnings("removal")
118-
SecurityManager sm = System.getSecurityManager();
119-
return (sm == null || fs == null) ? fs : checkFiles(Stream.of(fs), sm);
120-
}
121-
122-
private static File[] checkFiles(Stream<File> fs, @SuppressWarnings("removal") SecurityManager sm) {
123-
return fs.filter(f -> f != null && checkFile(f, sm) != null)
124-
.toArray(File[]::new);
125-
}
126-
12793
/**
12894
* Does {@code dir} represent a "computer" such as a node on the network, or
12995
* "My Computer" on the desktop.

src/java.desktop/share/classes/sun/swing/FilePane.java

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2119,24 +2119,20 @@ public boolean canWrite(File f) {
21192119
return false;
21202120
}
21212121

2122-
try {
2123-
if (f instanceof ShellFolder) {
2124-
return f.canWrite();
2125-
} else {
2126-
if (usesShellFolder(getFileChooser())) {
2127-
try {
2128-
return ShellFolder.getShellFolder(f).canWrite();
2129-
} catch (FileNotFoundException ex) {
2130-
// File doesn't exist
2131-
return false;
2132-
}
2133-
} else {
2134-
// Ordinary file
2135-
return f.canWrite();
2122+
if (f instanceof ShellFolder) {
2123+
return f.canWrite();
2124+
} else {
2125+
if (usesShellFolder(getFileChooser())) {
2126+
try {
2127+
return ShellFolder.getShellFolder(f).canWrite();
2128+
} catch (FileNotFoundException ex) {
2129+
// File doesn't exist
2130+
return false;
21362131
}
2132+
} else {
2133+
// Ordinary file
2134+
return f.canWrite();
21372135
}
2138-
} catch (SecurityException e) {
2139-
return false;
21402136
}
21412137
}
21422138

src/java.desktop/windows/classes/sun/awt/shell/Win32ShellFolder2.java

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -666,13 +666,6 @@ public String call() throws IOException {
666666
return getFileSystemPath0(csidl);
667667
}
668668
}, IOException.class);
669-
if (path != null) {
670-
@SuppressWarnings("removal")
671-
SecurityManager security = System.getSecurityManager();
672-
if (security != null) {
673-
security.checkRead(path);
674-
}
675-
}
676669
return path;
677670
}
678671

@@ -750,11 +743,6 @@ private native long getEnumObjects(long pIShellFolder, boolean isDesktop,
750743
* {@code null} if this shellfolder does not denote a directory.
751744
*/
752745
public File[] listFiles(final boolean includeHiddenFiles) {
753-
@SuppressWarnings("removal")
754-
SecurityManager security = System.getSecurityManager();
755-
if (security != null) {
756-
security.checkRead(getPath());
757-
}
758746

759747
try {
760748
File[] files = invoke(new Callable<File[]>() {
@@ -813,7 +801,7 @@ && pidlsEqual(pIShellFolder, childPIDL, personal.disposer.relativePIDL)) {
813801
}
814802
}, InterruptedException.class);
815803

816-
return Win32ShellFolderManager2.checkFiles(files);
804+
return files;
817805
} catch (InterruptedException e) {
818806
return new File[0];
819807
}

src/java.desktop/windows/classes/sun/awt/shell/Win32ShellFolderManager2.java

Lines changed: 7 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
import java.util.concurrent.ThreadFactory;
4343
import java.util.concurrent.ThreadPoolExecutor;
4444
import java.util.concurrent.TimeUnit;
45-
import java.util.stream.Stream;
4645

4746
import sun.awt.OSInfo;
4847
import sun.awt.util.ThreadGroupUtils;
@@ -167,9 +166,6 @@ static Win32ShellFolder2 getDesktop() {
167166
if (desktop == null) {
168167
try {
169168
desktop = new Win32ShellFolder2(DESKTOP);
170-
} catch (final SecurityException ignored) {
171-
// Ignore, the message may have sensitive information, not
172-
// accessible other ways
173169
} catch (IOException | InterruptedException e) {
174170
if (log.isLoggable(PlatformLogger.Level.WARNING)) {
175171
log.warning("Cannot access 'Desktop'", e);
@@ -183,9 +179,6 @@ static Win32ShellFolder2 getDrives() {
183179
if (drives == null) {
184180
try {
185181
drives = new Win32ShellFolder2(DRIVES);
186-
} catch (final SecurityException ignored) {
187-
// Ignore, the message may have sensitive information, not
188-
// accessible other ways
189182
} catch (IOException | InterruptedException e) {
190183
if (log.isLoggable(PlatformLogger.Level.WARNING)) {
191184
log.warning("Cannot access 'Drives'", e);
@@ -202,9 +195,6 @@ static Win32ShellFolder2 getRecent() {
202195
if (path != null) {
203196
recent = createShellFolder(getDesktop(), new File(path));
204197
}
205-
} catch (final SecurityException ignored) {
206-
// Ignore, the message may have sensitive information, not
207-
// accessible other ways
208198
} catch (InterruptedException | IOException e) {
209199
if (log.isLoggable(PlatformLogger.Level.WARNING)) {
210200
log.warning("Cannot access 'Recent'", e);
@@ -218,9 +208,6 @@ static Win32ShellFolder2 getNetwork() {
218208
if (network == null) {
219209
try {
220210
network = new Win32ShellFolder2(NETWORK);
221-
} catch (final SecurityException ignored) {
222-
// Ignore, the message may have sensitive information, not
223-
// accessible other ways
224211
} catch (IOException | InterruptedException e) {
225212
if (log.isLoggable(PlatformLogger.Level.WARNING)) {
226213
log.warning("Cannot access 'Network'", e);
@@ -244,9 +231,6 @@ static Win32ShellFolder2 getPersonal() {
244231
personal.setIsPersonal();
245232
}
246233
}
247-
} catch (final SecurityException ignored) {
248-
// Ignore, the message may have sensitive information, not
249-
// accessible other ways
250234
} catch (InterruptedException | IOException e) {
251235
if (log.isLoggable(PlatformLogger.Level.WARNING)) {
252236
log.warning("Cannot access 'Personal'", e);
@@ -287,7 +271,7 @@ public Object get(String key) {
287271
if (file == null) {
288272
file = getDesktop();
289273
}
290-
return checkFile(file);
274+
return file;
291275
} else if (key.equals("roots")) {
292276
// Should be "History" and "Desktop" ?
293277
if (roots == null) {
@@ -298,11 +282,11 @@ public Object get(String key) {
298282
roots = (File[])super.get(key);
299283
}
300284
}
301-
return checkFiles(roots);
285+
return roots;
302286
} else if (key.equals("fileChooserComboBoxFolders")) {
303287
Win32ShellFolder2 desktop = getDesktop();
304288

305-
if (desktop != null && checkFile(desktop) != null) {
289+
if (desktop != null) {
306290
ArrayList<File> folders = new ArrayList<File>();
307291
Win32ShellFolder2 drives = getDrives();
308292

@@ -313,15 +297,15 @@ public Object get(String key) {
313297

314298
folders.add(desktop);
315299
// Add all second level folders
316-
File[] secondLevelFolders = checkFiles(desktop.listFiles());
300+
File[] secondLevelFolders = desktop.listFiles();
317301
Arrays.sort(secondLevelFolders);
318302
for (File secondLevelFolder : secondLevelFolders) {
319303
Win32ShellFolder2 folder = (Win32ShellFolder2) secondLevelFolder;
320304
if (!folder.isFileSystem() || (folder.isDirectory() && !folder.isLink())) {
321305
folders.add(folder);
322306
// Add third level for "My Computer"
323307
if (folder.equals(drives)) {
324-
File[] thirdLevelFolders = checkFiles(folder.listFiles());
308+
File[] thirdLevelFolders = folder.listFiles();
325309
if (thirdLevelFolders != null && thirdLevelFolders.length > 0) {
326310
List<File> thirdLevelFoldersList = Arrays.asList(thirdLevelFolders);
327311

@@ -331,7 +315,7 @@ public Object get(String key) {
331315
}
332316
}
333317
}
334-
return checkFiles(folders);
318+
return folders.toArray(new File[folders.size()]);
335319
} else {
336320
return super.get(key);
337321
}
@@ -374,7 +358,7 @@ public Object get(String key) {
374358
}
375359
}
376360
}
377-
return checkFiles(folders);
361+
return folders.toArray(new File[folders.size()]);
378362
} else if (key.startsWith("fileChooserIcon ")) {
379363
String name = key.substring(key.indexOf(" ") + 1);
380364

@@ -421,53 +405,6 @@ public Object get(String key) {
421405
return null;
422406
}
423407

424-
private static File checkFile(File file) {
425-
@SuppressWarnings("removal")
426-
SecurityManager sm = System.getSecurityManager();
427-
return (sm == null || file == null) ? file : checkFile(file, sm);
428-
}
429-
430-
private static File checkFile(File file, @SuppressWarnings("removal") SecurityManager sm) {
431-
try {
432-
sm.checkRead(file.getPath());
433-
434-
if (file instanceof Win32ShellFolder2) {
435-
Win32ShellFolder2 f = (Win32ShellFolder2)file;
436-
if (f.isLink()) {
437-
Win32ShellFolder2 link = (Win32ShellFolder2)f.getLinkLocation();
438-
if (link != null)
439-
sm.checkRead(link.getPath());
440-
}
441-
}
442-
return file;
443-
} catch (SecurityException se) {
444-
return null;
445-
}
446-
}
447-
448-
static File[] checkFiles(File[] files) {
449-
@SuppressWarnings("removal")
450-
SecurityManager sm = System.getSecurityManager();
451-
if (sm == null || files == null || files.length == 0) {
452-
return files;
453-
}
454-
return checkFiles(Arrays.stream(files), sm);
455-
}
456-
457-
private static File[] checkFiles(List<File> files) {
458-
@SuppressWarnings("removal")
459-
SecurityManager sm = System.getSecurityManager();
460-
if (sm == null || files.isEmpty()) {
461-
return files.toArray(new File[files.size()]);
462-
}
463-
return checkFiles(files.stream(), sm);
464-
}
465-
466-
private static File[] checkFiles(Stream<File> filesStream, @SuppressWarnings("removal") SecurityManager sm) {
467-
return filesStream.filter((file) -> checkFile(file, sm) != null)
468-
.toArray(File[]::new);
469-
}
470-
471408
/**
472409
* Does {@code dir} represent a "computer" such as a node on the network, or
473410
* "My Computer" on the desktop.

0 commit comments

Comments
 (0)