Changeset 7137
- Timestamp:
- Apr 22, 2016, 1:31:25 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/3.7-stable/src/core/net/sf/basedb/util/StaticCache.java
r6898 r7137 46 46 import java.util.regex.Pattern; 47 47 48 import net.sf.basedb.core.BaseException; 48 49 import net.sf.basedb.core.InvalidDataException; 49 50 … … 89 90 public static boolean isValidKey(String key) 90 91 { 91 return validKey.matcher(key).matches() ;92 return validKey.matcher(key).matches() && !key.contains("../"); 92 93 } 93 94 … … 103 104 { 104 105 Pattern invalid = Pattern.compile("[^\\w\\/\\.\\-]"); 105 return invalid.matcher(key).replaceAll(replacement) ;106 return invalid.matcher(key).replaceAll(replacement).replace("../", replacement); 106 107 } 107 108 108 109 private final File root; 110 private final String rootPath; 109 111 private final Map<String, LockEntry> locks; 110 112 private boolean disabled; … … 118 120 { 119 121 this.root = root; 122 try 123 { 124 this.rootPath = root.getCanonicalPath(); 125 } 126 catch (IOException ex) 127 { 128 throw new BaseException(ex); 129 } 120 130 this.locks = new WeakHashMap<String, LockEntry>(); 121 131 log.info("Creating static cache in directory " + root); … … 216 226 { 217 227 validateKey(key); 218 File f = new File(root,key);228 File f = fileFromKey(key); 219 229 return f.exists(); 220 230 } … … 229 239 { 230 240 validateKey(key); 231 File f = new File(root,key);241 File f = fileFromKey(key); 232 242 return f.exists() ? f.length() : -1; 233 243 } … … 452 462 validateKey(key); 453 463 log.debug("Delete request for static cache: " + key); 454 File f = new File(root,key);464 File f = fileFromKey(key); 455 465 if (!f.exists()) 456 466 { … … 478 488 } 479 489 490 private File fileFromKey(String key) 491 { 492 File f = new File(root, key); 493 // The key must result in a file path that is a child to the root path! 494 try 495 { 496 String keyPath = f.getCanonicalPath(); 497 if (!keyPath.startsWith(rootPath)) 498 { 499 throw new InvalidDataException("Invalid path to cache entry: "+ keyPath); 500 } 501 } 502 catch (IOException ex) 503 { 504 throw new InvalidDataException("Invalid cache key: "+ key, ex); 505 } 506 return f; 507 } 508 480 509 /** 481 510 Get a lock-safe input stream. … … 486 515 validateKey(key); 487 516 log.debug("Read request for static cache: " + key); 488 File f = new File(root,key);517 File f = fileFromKey(key); 489 518 if (!f.exists()) 490 519 { … … 523 552 try 524 553 { 525 File f = new File(root,key);554 File f = fileFromKey(key); 526 555 File dir = f.getParentFile(); 527 556 if (!dir.mkdirs() && !dir.isDirectory())
Note: See TracChangeset
for help on using the changeset viewer.