@Test public void shouldNotGetResourceByAbsolutePathNotInOpenmrsApplicationDirectory() throws Exception { File testFile = new File(new File(testDir, "configuration"), FILE_NAME); try { FileUtils.writeStringToFile(testFile, TEST_CONTENTS, "UTF-8"); Assert.assertNotEquals(OpenmrsUtil.getApplicationDataDirectory(), testDir.getCanonicalPath()); Assert.assertNull(provider.getResource(FILE_NAME)); Assert.assertNull(provider.getResource(testFile.getAbsolutePath())); } finally { if (testFile.exists()) { testFile.delete(); } } }
@Override public File getResource(String path) { if (path == null) { return null; } final String configurationDirectory; try { configurationDirectory = OpenmrsUtil.getDirectoryInApplicationDataDirectory("configuration") .getCanonicalPath(); } catch (IOException e) { throw new APIException("Could not determine canonical path for configuration directory", e); } final File resourceFile; final File file = new File(path); if (file.isAbsolute()) { resourceFile = file; } else { String firstDir = null; int idx = path.indexOf(File.separatorChar, 1); if (idx > 0) { int start = path.charAt(0) == File.separatorChar ? 1 : 0; firstDir = path.substring(start, idx); } if (!"configuration".equals(firstDir)) { resourceFile = new File(configurationDirectory, path); } else { resourceFile = new File(OpenmrsUtil.getApplicationDataDirectory(), path); } } try { if (!resourceFile.getCanonicalPath().startsWith(configurationDirectory)) { log.warn("Attempted to load invalid resource: {}", file); return null; } } catch (IOException e) { log.error("Error occurred while trying to load file: {}", path, e); return null; } return resourceFile.exists() ? resourceFile : null; }
ConfigurationResourceProvider implements ResourceProvider { @Override public File getResource(String path) { if (path == null) { return null; } final String configurationDirectory; try { configurationDirectory = OpenmrsUtil.getDirectoryInApplicationDataDirectory("configuration") .getCanonicalPath(); } catch (IOException e) { throw new APIException("Could not determine canonical path for configuration directory", e); } final File resourceFile; final File file = new File(path); if (file.isAbsolute()) { resourceFile = file; } else { String firstDir = null; int idx = path.indexOf(File.separatorChar, 1); if (idx > 0) { int start = path.charAt(0) == File.separatorChar ? 1 : 0; firstDir = path.substring(start, idx); } if (!"configuration".equals(firstDir)) { resourceFile = new File(configurationDirectory, path); } else { resourceFile = new File(OpenmrsUtil.getApplicationDataDirectory(), path); } } try { if (!resourceFile.getCanonicalPath().startsWith(configurationDirectory)) { log.warn("Attempted to load invalid resource: {}", file); return null; } } catch (IOException e) { log.error("Error occurred while trying to load file: {}", path, e); return null; } return resourceFile.exists() ? resourceFile : null; } }
ConfigurationResourceProvider implements ResourceProvider { @Override public File getResource(String path) { if (path == null) { return null; } final String configurationDirectory; try { configurationDirectory = OpenmrsUtil.getDirectoryInApplicationDataDirectory("configuration") .getCanonicalPath(); } catch (IOException e) { throw new APIException("Could not determine canonical path for configuration directory", e); } final File resourceFile; final File file = new File(path); if (file.isAbsolute()) { resourceFile = file; } else { String firstDir = null; int idx = path.indexOf(File.separatorChar, 1); if (idx > 0) { int start = path.charAt(0) == File.separatorChar ? 1 : 0; firstDir = path.substring(start, idx); } if (!"configuration".equals(firstDir)) { resourceFile = new File(configurationDirectory, path); } else { resourceFile = new File(OpenmrsUtil.getApplicationDataDirectory(), path); } } try { if (!resourceFile.getCanonicalPath().startsWith(configurationDirectory)) { log.warn("Attempted to load invalid resource: {}", file); return null; } } catch (IOException e) { log.error("Error occurred while trying to load file: {}", path, e); return null; } return resourceFile.exists() ? resourceFile : null; }  }
ConfigurationResourceProvider implements ResourceProvider { @Override public File getResource(String path) { if (path == null) { return null; } final String configurationDirectory; try { configurationDirectory = OpenmrsUtil.getDirectoryInApplicationDataDirectory("configuration") .getCanonicalPath(); } catch (IOException e) { throw new APIException("Could not determine canonical path for configuration directory", e); } final File resourceFile; final File file = new File(path); if (file.isAbsolute()) { resourceFile = file; } else { String firstDir = null; int idx = path.indexOf(File.separatorChar, 1); if (idx > 0) { int start = path.charAt(0) == File.separatorChar ? 1 : 0; firstDir = path.substring(start, idx); } if (!"configuration".equals(firstDir)) { resourceFile = new File(configurationDirectory, path); } else { resourceFile = new File(OpenmrsUtil.getApplicationDataDirectory(), path); } } try { if (!resourceFile.getCanonicalPath().startsWith(configurationDirectory)) { log.warn("Attempted to load invalid resource: {}", file); return null; } } catch (IOException e) { log.error("Error occurred while trying to load file: {}", path, e); return null; } return resourceFile.exists() ? resourceFile : null; }  @Override File getResource(String path); }
ConfigurationResourceProvider implements ResourceProvider { @Override public File getResource(String path) { if (path == null) { return null; } final String configurationDirectory; try { configurationDirectory = OpenmrsUtil.getDirectoryInApplicationDataDirectory("configuration") .getCanonicalPath(); } catch (IOException e) { throw new APIException("Could not determine canonical path for configuration directory", e); } final File resourceFile; final File file = new File(path); if (file.isAbsolute()) { resourceFile = file; } else { String firstDir = null; int idx = path.indexOf(File.separatorChar, 1); if (idx > 0) { int start = path.charAt(0) == File.separatorChar ? 1 : 0; firstDir = path.substring(start, idx); } if (!"configuration".equals(firstDir)) { resourceFile = new File(configurationDirectory, path); } else { resourceFile = new File(OpenmrsUtil.getApplicationDataDirectory(), path); } } try { if (!resourceFile.getCanonicalPath().startsWith(configurationDirectory)) { log.warn("Attempted to load invalid resource: {}", file); return null; } } catch (IOException e) { log.error("Error occurred while trying to load file: {}", path, e); return null; } return resourceFile.exists() ? resourceFile : null; }  @Override File getResource(String path); static final String RESOURCE_KEY; }