001package org.hl7.fhir.utilities.http; 002 003/* 004 Copyright (c) 2011+, HL7, Inc. 005 All rights reserved. 006 007 Redistribution and use in source and binary forms, with or without modification, 008 are permitted provided that the following conditions are met: 009 010 * Redistributions of source code must retain the above copyright notice, this 011 list of conditions and the following disclaimer. 012 * Redistributions in binary form must reproduce the above copyright notice, 013 this list of conditions and the following disclaimer in the documentation 014 and/or other materials provided with the distribution. 015 * Neither the name of HL7 nor the names of its contributors may be used to 016 endorse or promote products derived from this software without specific 017 prior written permission. 018 019 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 020 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 021 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 022 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 023 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 024 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 025 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 026 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 027 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 028 POSSIBILITY OF SUCH DAMAGE. 029 030 */ 031 032 033 034import java.io.IOException; 035import java.net.URI; 036import java.net.URISyntaxException; 037import java.util.*; 038 039import lombok.Getter; 040 041import org.hl7.fhir.utilities.Utilities; 042import org.hl7.fhir.utilities.settings.FhirSettings; 043import org.hl7.fhir.utilities.settings.ServerDetailsPOJO; 044 045/** 046 * see security.md - manages web access by the FHIR HAPI Core library 047 * <p/> 048 * By using accessPolicy, allowedDomains and accessor, a host java application can control 049 * whether this library has direct access to the web (and which domains it is allowed to access), 050 * or whether the host application provides controlled access, or whether no access is allowed at all 051 * (in which case other information providers need to be provided) 052 * 053 * @author Grahame 054 * 055 */ 056public class ManagedWebAccess { 057 058 public interface IWebAccessor { 059 HTTPResult get(Iterable<String> serverTypes, String url, String accept, Map<String, String> headers) throws IOException; 060 HTTPResult post(Iterable<String> serverTypes, String url, byte[] bytes, String contentType, String accept, Map<String, String> headers) throws IOException; 061 HTTPResult put(Iterable<String> serverTypes, String url, byte[] bytes, String contentType, String accept, Map<String, String> headers) throws IOException; 062 } 063 064 public interface IFhirWebAccessor { 065 HTTPResult httpCall(HTTPRequest httpRequest); 066 } 067 068 public enum WebAccessPolicy { 069 DIRECT, // open access to the web, though access can be restricted only to domains in AllowedDomains 070 MANAGED, // no access except by the IWebAccessor 071 PROHIBITED, // no access at all to the web 072 } 073 074 private static WebAccessPolicy accessPolicy = WebAccessPolicy.DIRECT; // for legacy reasons 075 //TODO get this from fhir settings 076 private static List<String> allowedDomains = new ArrayList<>(); 077 @Getter 078 private static IWebAccessor accessor; 079 080 @Getter 081 private static IFhirWebAccessor fhirWebAccessor; 082 083 @Getter 084 private static String userAgent; 085 private static List<ServerDetailsPOJO> serverAuthDetails; 086 087 public static WebAccessPolicy getAccessPolicy() { 088 return accessPolicy; 089 } 090 091 public static void setAccessPolicy(WebAccessPolicy accessPolicy) { 092 ManagedWebAccess.accessPolicy = accessPolicy; 093 } 094 095 static boolean inAllowedPaths(String pathname) { 096 if (allowedDomains.isEmpty()) { 097 return true; 098 } 099 for (String s : allowedDomains) { 100 if (pathname.startsWith(s)) { 101 return true; 102 } 103 } 104 return false; 105 } 106 107 public static void setUserAgent(String userAgent) { 108 ManagedWebAccess.userAgent = userAgent; 109 } 110 111 public static ManagedWebAccessor accessor(Iterable<String> serverTypes) { 112 return new ManagedWebAccessor(serverTypes, userAgent, serverAuthDetails); 113 } 114 115 public static ManagedFhirWebAccessor fhirAccessor() { 116 return new ManagedFhirWebAccessor(userAgent, serverAuthDetails); 117 } 118 119 public static HTTPResult get(Iterable<String> serverTypes, String url) throws IOException { 120 return accessor(serverTypes).get(url); 121 } 122 123 public static HTTPResult get(Iterable<String> serverTypes, String url, String accept) throws IOException { 124 return accessor(serverTypes).get(url, accept); 125 } 126 127 public static HTTPResult post(Iterable<String> serverTypes, String url, byte[] content, String contentType, String accept) throws IOException { 128 return accessor(serverTypes).post(url, content, contentType, accept); 129 } 130 131 public static HTTPResult put(Iterable<String> serverTypes, String url, byte[] content, String contentType, String accept) throws IOException { 132 return accessor(serverTypes).put(url, content, contentType, accept); 133 } 134 135 public static HTTPResult httpCall(HTTPRequest httpRequest) throws IOException { 136 return fhirAccessor().httpCall(httpRequest); 137 } 138 139 public static void loadFromFHIRSettings() { 140 setAccessPolicy(FhirSettings.isProhibitNetworkAccess() ? WebAccessPolicy.PROHIBITED : WebAccessPolicy.DIRECT); 141 setUserAgent("hapi-fhir-tooling-client"); 142 serverAuthDetails = new ArrayList<>(); 143 serverAuthDetails.addAll(FhirSettings.getServers()); 144 } 145 146 public static void loadFromFHIRSettings(FhirSettings settings) { 147 setAccessPolicy(settings.isProhibitNetworkAccess() ? WebAccessPolicy.PROHIBITED : WebAccessPolicy.DIRECT); 148 setUserAgent("hapi-fhir-tooling-client"); 149 serverAuthDetails = new ArrayList<>(); 150 serverAuthDetails.addAll(settings.getServers()); 151 } 152 153 public static String makeSecureRef(String url) { 154 if (url == null || !url.startsWith("http://") || isLocal(url)) { 155 return url; 156 } else { 157 return url.replace("http://", "https://"); 158 } 159 } 160 161 private static boolean isLocal(String url) { 162 URI uri; 163 try { 164 uri = new URI(url); 165 // todo: check in serverAuthDetails to see if policy is set for this server 166 return Utilities.existsInList(uri.getHost(), "localhost", "local.fhir.org", "127.0.0.1", "[::1]") || (uri.getHost() != null && uri.getHost().endsWith(".localhost")); 167 } catch (URISyntaxException e) { 168 return false; 169 } 170 } 171}