-
Notifications
You must be signed in to change notification settings - Fork 3.1k
[MNG-5862] Support XML entities and XInclude #1205
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
fcc2c9f
3fffae9
a3f2bd6
84e4e03
0679917
5629c20
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -235,7 +235,7 @@ void testReadInvalidPom() throws Exception { | |
|
|
||
| // single project build entry point | ||
| Exception ex = assertThrows(Exception.class, () -> projectBuilder.build(pomFile, configuration)); | ||
| assertThat(ex.getMessage(), containsString("Received non-all-whitespace CHARACTERS or CDATA event")); | ||
| assertThat(ex.getMessage(), containsString("expected START_TAG or END_TAG, not CHARACTERS")); | ||
|
gnodet marked this conversation as resolved.
Outdated
|
||
|
|
||
| // multi projects build entry point | ||
| ProjectBuildingException pex = assertThrows( | ||
|
gnodet marked this conversation as resolved.
|
||
|
|
@@ -246,8 +246,7 @@ void testReadInvalidPom() throws Exception { | |
| assertThat(pex.getResults().get(0).getProblems().size(), greaterThan(0)); | ||
| assertThat( | ||
| pex.getResults(), | ||
| contains(projectBuildingResultWithProblemMessage( | ||
| "Received non-all-whitespace CHARACTERS or CDATA event in nextTag()"))); | ||
| contains(projectBuildingResultWithProblemMessage("expected START_TAG or END_TAG, not CHARACTERS"))); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wouldn't go further than asserting the message is non-null. Messages are informative, not part of the stable API |
||
| } | ||
|
|
||
| @Test | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,6 +25,8 @@ | |
| import javax.xml.stream.XMLInputFactory; | ||
| import javax.xml.stream.XMLStreamException; | ||
| import javax.xml.stream.XMLStreamReader; | ||
| import javax.xml.transform.Source; | ||
| import javax.xml.transform.stream.StreamSource; | ||
|
|
||
| import java.io.File; | ||
| import java.io.IOException; | ||
|
|
@@ -39,6 +41,8 @@ | |
| import org.apache.maven.model.Model; | ||
| import org.apache.maven.model.building.ModelSourceTransformer; | ||
| import org.apache.maven.model.v4.MavenStaxReader; | ||
| import org.apache.maven.stax.xinclude.XInclude; | ||
| import org.codehaus.stax2.io.Stax2FileSource; | ||
|
|
||
| /** | ||
| * Handles deserialization of a model from some kind of textual format like XML. | ||
|
|
@@ -100,14 +104,39 @@ private Path getRootDirectory(Map<String, ?> options) { | |
| return (Path) value; | ||
| } | ||
|
|
||
| private boolean getXInclude(Map<String, ?> options) { | ||
| Object value = (options != null) ? options.get(XINCLUDE) : null; | ||
| return value instanceof Boolean && (Boolean) value; | ||
| } | ||
|
|
||
| private Model read(InputStream input, Path pomFile, Map<String, ?> options) throws IOException { | ||
| try { | ||
| XMLInputFactory factory = new com.ctc.wstx.stax.WstxInputFactory(); | ||
| factory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, false); | ||
| XMLStreamReader parser = factory.createXMLStreamReader(input); | ||
|
|
||
| InputSource source = getSource(options); | ||
| boolean strict = isStrict(options); | ||
| Path rootDirectory = getRootDirectory(options); | ||
|
|
||
| Source xmlSource; | ||
| if (pomFile != null) { | ||
| if (input != null) { | ||
| xmlSource = new StaxPathInputSource(pomFile, input); | ||
| } else { | ||
| xmlSource = new Stax2FileSource(pomFile.toFile()); | ||
| } | ||
| } else { | ||
| xmlSource = new StreamSource(input); | ||
| } | ||
|
|
||
| XMLStreamReader parser; | ||
| // We only support xml entities and xinclude when reading a file in strict mode | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: external general entities and XInclude |
||
| if (pomFile != null && strict && getXInclude(options)) { | ||
| parser = XInclude.xinclude(xmlSource, new LocalXmlResolver(rootDirectory)); | ||
| } else { | ||
| XMLInputFactory factory = new com.ctc.wstx.stax.WstxInputFactory(); | ||
| factory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, false); | ||
| factory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false); | ||
|
gnodet marked this conversation as resolved.
|
||
| parser = factory.createXMLStreamReader(xmlSource); | ||
| } | ||
|
|
||
| MavenStaxReader mr = new MavenStaxReader(); | ||
| mr.setAddLocationInformation(source != null); | ||
| Model model = new Model(mr.read(parser, strict, source != null ? source.toApiSource() : null)); | ||
|
|
@@ -127,7 +156,6 @@ private Model read(InputStream input, Path pomFile, Map<String, ?> options) thro | |
| private Model read(Reader reader, Path pomFile, Map<String, ?> options) throws IOException { | ||
| try { | ||
| XMLInputFactory factory = new com.ctc.wstx.stax.WstxInputFactory(); | ||
| factory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, false); | ||
| XMLStreamReader parser = factory.createXMLStreamReader(reader); | ||
|
|
||
| InputSource source = getSource(options); | ||
|
|
@@ -147,4 +175,18 @@ private Model read(Reader reader, Path pomFile, Map<String, ?> options) throws I | |
| throw new IOException("Unable to transform pom", e); | ||
| } | ||
| } | ||
|
|
||
| private static class StaxPathInputSource extends Stax2FileSource { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. a comment explaining the point here might be useful. Maybe this associates a file with an input stream that doesn't come from the file? or at least not only from the file? I'm not sure
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I removed that class as it was not really needed. |
||
| private final InputStream input; | ||
|
|
||
| StaxPathInputSource(Path pomFile, InputStream input) { | ||
| super(pomFile.toFile()); | ||
| this.input = input; | ||
| } | ||
|
|
||
| @Override | ||
| public InputStream constructInputStream() throws IOException { | ||
| return input; | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
| package org.apache.maven.model.io; | ||
|
|
||
| import javax.xml.stream.XMLResolver; | ||
| import javax.xml.stream.XMLStreamException; | ||
| import javax.xml.transform.stream.StreamSource; | ||
|
|
||
| import java.io.IOException; | ||
| import java.net.URI; | ||
| import java.net.URISyntaxException; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.nio.file.Paths; | ||
|
|
||
| public class LocalXmlResolver implements XMLResolver { | ||
|
|
||
| private final Path rootDirectory; | ||
|
|
||
| public LocalXmlResolver(Path rootDirectory) { | ||
| this.rootDirectory = rootDirectory != null ? rootDirectory.normalize() : null; | ||
| } | ||
|
|
||
| @Override | ||
| public Object resolveEntity(String publicID, String systemID, String baseURI, String namespace) | ||
| throws XMLStreamException { | ||
| if (rootDirectory == null) { | ||
| return null; | ||
| } | ||
| if (systemID == null) { | ||
| throw new XMLStreamException("systemID is null"); | ||
| } | ||
| if (baseURI == null) { | ||
| throw new XMLStreamException("baseURI is null"); | ||
| } | ||
| URI baseUri; | ||
| try { | ||
| baseUri = new URI(baseURI).normalize(); | ||
| } catch (URISyntaxException e) { | ||
| throw new XMLStreamException("Invalid syntax for baseURI URI: " + baseURI, e); | ||
| } | ||
| URI sysUri; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not ewhat I first thought it was. Rename systemIDUrl |
||
| try { | ||
| sysUri = new URI(systemID).normalize(); | ||
| } catch (URISyntaxException e) { | ||
| throw new XMLStreamException("Invalid syntax for systemID URI: " + systemID, e); | ||
| } | ||
| if (sysUri.getScheme() != null) { | ||
| throw new XMLStreamException("systemID must be a relative URI: " + systemID); | ||
| } | ||
| Path base = Paths.get(baseUri).normalize(); | ||
| if (!base.startsWith(rootDirectory)) { | ||
| return null; | ||
| } | ||
| Path sys = Paths.get(sysUri.getSchemeSpecificPart()).normalize(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sys --> systemIDPath |
||
| if (sys.isAbsolute()) { | ||
| throw new XMLStreamException("systemID must be a relative path: " + systemID); | ||
| } | ||
| Path res = base.resolveSibling(sys).normalize(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what is res? Please use a non-abbreviated name. |
||
| if (!res.startsWith(rootDirectory)) { | ||
| throw new XMLStreamException("systemID cannot refer to outside rootDirectory: " + systemID); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. refer to --> refer to a path |
||
| } | ||
| try { | ||
| return new StreamSource(Files.newInputStream(res), res.toUri().toASCIIString()); | ||
| } catch (IOException e) { | ||
| throw new XMLStreamException("Unable to create Source for " + systemID + ": " + e.getMessage(), e); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -45,6 +45,12 @@ public interface ModelReader { | |
| */ | ||
| String INPUT_SOURCE = "org.apache.maven.model.io.inputSource"; | ||
|
|
||
| /** | ||
| * Name of the property used to store a boolean {@code true} if XInclude supports | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. supports --> support |
||
| * is needed. | ||
| */ | ||
| String XINCLUDE = "xinclude"; | ||
|
|
||
| /** | ||
| * Name of the property used to store the project's root directory to use with | ||
| * XInclude support. | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would still advocate for this being off by default (opt-in) instead of on by default (opt-out) since XML parsers have a long history of security vulnerabilities surrounding this stuff.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done