changeset 480:5e70dbac6a7d

6985453: Font.createFont may expose some system properties in exception text Reviewed-by: prr, hawtin
author bae
date Fri, 29 Oct 2010 23:55:34 +0400
parents f3dff5c1b9c2
children db9f076b2aed
files src/share/classes/sun/font/FileFont.java src/share/classes/sun/font/TrueTypeFont.java src/share/classes/sun/font/Type1Font.java
diffstat 3 files changed, 52 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/sun/font/FileFont.java	Wed Oct 27 13:03:30 2010 -0700
+++ b/src/share/classes/sun/font/FileFont.java	Fri Oct 29 23:55:34 2010 +0400
@@ -48,6 +48,9 @@
 import java.util.HashSet;
 import java.util.HashMap;
 import java.awt.Font;
+import java.security.AccessController;
+import java.security.PrivilegedActionException;
+import java.security.PrivilegedExceptionAction;
 
 public abstract class FileFont extends PhysicalFont {
 
@@ -284,4 +287,49 @@
             });
         }
     }
+
+    protected String getPublicFileName() {
+        SecurityManager sm = System.getSecurityManager();
+        if (sm == null) {
+            return platName;
+        }
+        boolean canReadProperty = true;
+
+        try {
+            sm.checkPropertyAccess("java.io.tmpdir");
+        } catch (SecurityException e) {
+            canReadProperty = false;
+        }
+
+        if (canReadProperty) {
+            return platName;
+        }
+
+        final File f = new File(platName);
+
+         Boolean isTmpFile = Boolean.FALSE;
+         try {
+             isTmpFile = AccessController.doPrivileged(
+                 new PrivilegedExceptionAction<Boolean>() {
+                     public Boolean run() {
+                         File tmp = new File(System.getProperty("java.io.tmpdir"));
+                         try {
+                             String tpath = tmp.getCanonicalPath();
+                             String fpath = f.getCanonicalPath();
+
+                             return (fpath == null) || fpath.startsWith(tpath);
+                         } catch (IOException e) {
+                             return Boolean.TRUE;
+                         }
+                     }
+                 }
+             );
+         } catch (PrivilegedActionException e) {
+             // unable to verify whether value of java.io.tempdir will be
+             // exposed, so return only a name of the font file.
+             isTmpFile = Boolean.TRUE;
+         }
+
+         return  isTmpFile ? "temp file" : platName;
+     }
 }
--- a/src/share/classes/sun/font/TrueTypeFont.java	Wed Oct 27 13:03:30 2010 -0700
+++ b/src/share/classes/sun/font/TrueTypeFont.java	Fri Oct 29 23:55:34 2010 +0400
@@ -504,7 +504,8 @@
                 break;
 
             default:
-                throw new FontFormatException("Unsupported sfnt " + platName);
+                throw new FontFormatException("Unsupported sfnt " +
+                                              getPublicFileName());
             }
 
             /* Now have the offset of this TT font (possibly within a TTC)
@@ -1369,6 +1370,6 @@
 
     public String toString() {
         return "** TrueType Font: Family="+familyName+ " Name="+fullName+
-            " style="+style+" fileName="+platName;
+            " style="+style+" fileName="+getPublicFileName();
     }
 }
--- a/src/share/classes/sun/font/Type1Font.java	Wed Oct 27 13:03:30 2010 -0700
+++ b/src/share/classes/sun/font/Type1Font.java	Fri Oct 29 23:55:34 2010 +0400
@@ -677,7 +677,7 @@
 
     public String toString() {
         return "** Type1 Font: Family="+familyName+ " Name="+fullName+
-            " style="+style+" fileName="+platName;
+            " style="+style+" fileName="+getPublicFileName();
     }
 
 }