Skip to content

Commit 2b5434e

Browse files
committed
Consistent local vs external resolution of https schema references
Closes gh-22504
1 parent cebd899 commit 2b5434e

File tree

8 files changed

+87
-59
lines changed

8 files changed

+87
-59
lines changed

spring-beans/src/main/java/org/springframework/beans/factory/xml/BeansDtdResolver.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.beans.factory.xml;
1818

19+
import java.io.FileNotFoundException;
1920
import java.io.IOException;
2021

2122
import org.apache.commons.logging.Log;
@@ -76,7 +77,7 @@ public InputSource resolveEntity(String publicId, String systemId) throws IOExce
7677
}
7778
return source;
7879
}
79-
catch (IOException ex) {
80+
catch (FileNotFoundException ex) {
8081
if (logger.isDebugEnabled()) {
8182
logger.debug("Could not resolve beans DTD [" + systemId + "]: not found in classpath", ex);
8283
}

spring-beans/src/main/java/org/springframework/beans/factory/xml/DelegatingEntityResolver.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -86,6 +86,8 @@ else if (systemId.endsWith(XSD_SUFFIX)) {
8686
return this.schemaResolver.resolveEntity(publicId, systemId);
8787
}
8888
}
89+
90+
// Fall back to the parser's default behavior.
8991
return null;
9092
}
9193

spring-beans/src/main/java/org/springframework/beans/factory/xml/PluggableSchemaResolver.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ public InputSource resolveEntity(String publicId, String systemId) throws IOExce
110110

111111
if (systemId != null) {
112112
String resourceLocation = getSchemaMappings().get(systemId);
113+
if (resourceLocation == null && systemId.startsWith("https:")) {
114+
// Retrieve canonical http schema mapping even for https declaration
115+
resourceLocation = getSchemaMappings().get("http:" + systemId.substring(6));
116+
}
113117
if (resourceLocation != null) {
114118
Resource resource = new ClassPathResource(resourceLocation, this.classLoader);
115119
try {

spring-beans/src/main/java/org/springframework/beans/factory/xml/ResourceEntityResolver.java

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -30,9 +30,9 @@
3030
import org.springframework.core.io.ResourceLoader;
3131

3232
/**
33-
* EntityResolver implementation that tries to resolve entity references
33+
* {@code EntityResolver} implementation that tries to resolve entity references
3434
* through a {@link org.springframework.core.io.ResourceLoader} (usually,
35-
* relative to the resource base of an ApplicationContext), if applicable.
35+
* relative to the resource base of an {@code ApplicationContext}), if applicable.
3636
* Extends {@link DelegatingEntityResolver} to also provide DTD and XSD lookup.
3737
*
3838
* <p>Allows to use standard XML entities to include XML snippets into an
@@ -72,6 +72,7 @@ public ResourceEntityResolver(ResourceLoader resourceLoader) {
7272
@Override
7373
public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
7474
InputSource source = super.resolveEntity(publicId, systemId);
75+
7576
if (source == null && systemId != null) {
7677
String resourcePath = null;
7778
try {
@@ -103,7 +104,27 @@ public InputSource resolveEntity(String publicId, String systemId) throws SAXExc
103104
logger.debug("Found XML entity [" + systemId + "]: " + resource);
104105
}
105106
}
107+
else if (systemId.endsWith(DTD_SUFFIX) || systemId.endsWith(XSD_SUFFIX)) {
108+
// External dtd/xsd lookup via https even for canonical http declaration
109+
String url = systemId;
110+
if (url.startsWith("http:")) {
111+
url = "https:" + url.substring(5);
112+
}
113+
try {
114+
source = new InputSource(new URL(url).openStream());
115+
source.setPublicId(publicId);
116+
source.setSystemId(systemId);
117+
}
118+
catch (IOException ex) {
119+
if (logger.isDebugEnabled()) {
120+
logger.debug("Could not resolve XML entity [" + systemId + "] through URL [" + url + "]", ex);
121+
}
122+
// Fall back to the parser's default behavior.
123+
source = null;
124+
}
125+
}
106126
}
127+
107128
return source;
108129
}
109130

0 commit comments

Comments
 (0)