001package ca.uhn.fhir.validation;
002
003/*
004 * #%L
005 * HAPI FHIR - Core Library
006 * %%
007 * Copyright (C) 2014 - 2020 University Health Network
008 * %%
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 *
013 *      http://www.apache.org/licenses/LICENSE-2.0
014 *
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 * #L%
021 */
022
023import ca.uhn.fhir.context.ConfigurationException;
024import ca.uhn.fhir.context.FhirContext;
025import ca.uhn.fhir.rest.api.EncodingEnum;
026import ca.uhn.fhir.util.ClasspathUtil;
027import org.hl7.fhir.instance.model.api.IBaseResource;
028import org.w3c.dom.ls.LSInput;
029import org.w3c.dom.ls.LSResourceResolver;
030import org.xml.sax.SAXException;
031import org.xml.sax.SAXNotRecognizedException;
032import org.xml.sax.SAXParseException;
033
034import javax.xml.XMLConstants;
035import javax.xml.transform.Source;
036import javax.xml.transform.stream.StreamSource;
037import javax.xml.validation.Schema;
038import javax.xml.validation.SchemaFactory;
039import javax.xml.validation.Validator;
040import java.io.ByteArrayInputStream;
041import java.io.IOException;
042import java.io.StringReader;
043import java.util.Collections;
044import java.util.HashMap;
045import java.util.HashSet;
046import java.util.Map;
047import java.util.Set;
048
049public class SchemaBaseValidator implements IValidatorModule {
050        public static final String RESOURCES_JAR_NOTE = "Note that as of HAPI FHIR 1.2, DSTU2 validation files are kept in a separate JAR (hapi-fhir-validation-resources-XXX.jar) which must be added to your classpath. See the HAPI FHIR download page for more information.";
051
052        private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(SchemaBaseValidator.class);
053        private static final Set<String> SCHEMA_NAMES;
054
055        static {
056                HashSet<String> sn = new HashSet<>();
057                sn.add("xml.xsd");
058                sn.add("xhtml1-strict.xsd");
059                sn.add("fhir-single.xsd");
060                sn.add("fhir-xhtml.xsd");
061                sn.add("tombstone.xsd");
062                sn.add("opensearch.xsd");
063                sn.add("opensearchscore.xsd");
064                sn.add("xmldsig-core-schema.xsd");
065                SCHEMA_NAMES = Collections.unmodifiableSet(sn);
066        }
067
068        private final Map<String, Schema> myKeyToSchema = new HashMap<>();
069        private FhirContext myCtx;
070
071        public SchemaBaseValidator(FhirContext theContext) {
072                myCtx = theContext;
073        }
074
075        private void doValidate(IValidationContext<?> theContext) {
076                Schema schema = loadSchema();
077
078                try {
079                        Validator validator = schema.newValidator();
080                        MyErrorHandler handler = new MyErrorHandler(theContext);
081                        validator.setErrorHandler(handler);
082                        String encodedResource;
083                        if (theContext.getResourceAsStringEncoding() == EncodingEnum.XML) {
084                                encodedResource = theContext.getResourceAsString();
085                        } else {
086                                encodedResource = theContext.getFhirContext().newXmlParser().encodeResourceToString((IBaseResource) theContext.getResource());
087                        }
088
089                        try {
090                                /*
091                                 * See https://github.com/jamesagnew/hapi-fhir/issues/339
092                                 * https://www.owasp.org/index.php/XML_External_Entity_(XXE)_Processing
093                                 */
094                                validator.setProperty(XMLConstants.ACCESS_EXTERNAL_DTD, "");
095                                validator.setProperty(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "");
096                        } catch (SAXNotRecognizedException ex) {
097                                ourLog.warn("Jaxp 1.5 Support not found.", ex);
098                        }
099
100                        validator.validate(new StreamSource(new StringReader(encodedResource)));
101                } catch (SAXParseException e) {
102                        SingleValidationMessage message = new SingleValidationMessage();
103                        message.setLocationLine(e.getLineNumber());
104                        message.setLocationCol(e.getColumnNumber());
105                        message.setMessage(e.getLocalizedMessage());
106                        message.setSeverity(ResultSeverityEnum.FATAL);
107                        theContext.addValidationMessage(message);
108                } catch (SAXException | IOException e) {
109                        // Catch all
110                        throw new ConfigurationException("Could not load/parse schema file", e);
111                }
112        }
113
114        private Schema loadSchema() {
115                String key = "fhir-single.xsd";
116
117                synchronized (myKeyToSchema) {
118                        Schema schema = myKeyToSchema.get(key);
119                        if (schema != null) {
120                                return schema;
121                        }
122
123                        Source baseSource = loadXml("fhir-single.xsd");
124
125                        SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
126                        schemaFactory.setResourceResolver(new MyResourceResolver());
127
128                        try {
129                                try {
130                                        /*
131                                         * See https://github.com/jamesagnew/hapi-fhir/issues/339
132                                         * https://www.owasp.org/index.php/XML_External_Entity_(XXE)_Processing
133                                         */
134                                        schemaFactory.setProperty(XMLConstants.ACCESS_EXTERNAL_DTD, "");
135                                } catch (SAXNotRecognizedException e) {
136                                        ourLog.warn("Jaxp 1.5 Support not found.", e);
137                                }
138                                schema = schemaFactory.newSchema(new Source[]{baseSource});
139                        } catch (SAXException e) {
140                                throw new ConfigurationException("Could not load/parse schema file: " + "fhir-single.xsd", e);
141                        }
142                        myKeyToSchema.put(key, schema);
143                        return schema;
144                }
145        }
146
147        Source loadXml(String theSchemaName) {
148                String pathToBase = myCtx.getVersion().getPathToSchemaDefinitions() + '/' + theSchemaName;
149                ourLog.debug("Going to load resource: {}", pathToBase);
150
151                String contents = ClasspathUtil.loadResource(pathToBase, ClasspathUtil.withBom());
152                return new StreamSource(new StringReader(contents), null);
153        }
154
155        @Override
156        public void validateResource(IValidationContext<IBaseResource> theContext) {
157                doValidate(theContext);
158        }
159
160        private final class MyResourceResolver implements LSResourceResolver {
161                private MyResourceResolver() {
162                }
163
164                @Override
165                public LSInput resolveResource(String theType, String theNamespaceURI, String thePublicId, String theSystemId, String theBaseURI) {
166                        if (theSystemId != null && SCHEMA_NAMES.contains(theSystemId)) {
167                                LSInputImpl input = new LSInputImpl();
168                                input.setPublicId(thePublicId);
169                                input.setSystemId(theSystemId);
170                                input.setBaseURI(theBaseURI);
171                                String pathToBase = myCtx.getVersion().getPathToSchemaDefinitions() + '/' + theSystemId;
172
173                                ourLog.debug("Loading referenced schema file: " + pathToBase);
174
175                                byte[] bytes = ClasspathUtil.loadResourceAsByteArray(pathToBase);
176                                input.setByteStream(new ByteArrayInputStream(bytes));
177                                return input;
178
179                        }
180
181                        throw new ConfigurationException("Unknown schema: " + theSystemId);
182                }
183        }
184
185        private static class MyErrorHandler implements org.xml.sax.ErrorHandler {
186
187                private IValidationContext<?> myContext;
188
189                MyErrorHandler(IValidationContext<?> theContext) {
190                        myContext = theContext;
191                }
192
193                private void addIssue(SAXParseException theException, ResultSeverityEnum theSeverity) {
194                        SingleValidationMessage message = new SingleValidationMessage();
195                        message.setLocationLine(theException.getLineNumber());
196                        message.setLocationCol(theException.getColumnNumber());
197                        message.setMessage(theException.getLocalizedMessage());
198                        message.setSeverity(theSeverity);
199                        myContext.addValidationMessage(message);
200                }
201
202                @Override
203                public void error(SAXParseException theException) {
204                        addIssue(theException, ResultSeverityEnum.ERROR);
205                }
206
207                @Override
208                public void fatalError(SAXParseException theException) {
209                        addIssue(theException, ResultSeverityEnum.FATAL);
210                }
211
212                @Override
213                public void warning(SAXParseException theException) {
214                        addIssue(theException, ResultSeverityEnum.WARNING);
215                }
216
217        }
218
219}