diff --git a/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/uriresolver/CacheResourceFileNotFoundException.java b/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/uriresolver/CacheResourceFileNotFoundException.java new file mode 100644 index 000000000..c575a31f6 --- /dev/null +++ b/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/uriresolver/CacheResourceFileNotFoundException.java @@ -0,0 +1,9 @@ +package org.eclipse.lemminx.uriresolver; + +public class CacheResourceFileNotFoundException extends CacheResourceException { + + public CacheResourceFileNotFoundException(String resourceURI, String message) { + super(resourceURI, message); + } + +} diff --git a/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/uriresolver/CacheResourcesManager.java b/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/uriresolver/CacheResourcesManager.java index 90823d30b..6c7d4a43c 100644 --- a/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/uriresolver/CacheResourcesManager.java +++ b/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/uriresolver/CacheResourcesManager.java @@ -14,6 +14,7 @@ import static org.eclipse.lemminx.utils.ExceptionUtils.getRootCause; +import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; @@ -26,6 +27,7 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.text.MessageFormat; import java.util.HashMap; import java.util.HashSet; import java.util.Map; @@ -165,6 +167,16 @@ public Path getResource(final String resourceURI) throws IOException { CacheResourceDownloadingError.DOWNLOAD_DISABLED, null, null); } + URI resourceUriParsed = URI.create(resourceURI); + if ("file".equals(resourceUriParsed.getScheme())) { + // UNC path + Path path = Path.of(resourceUriParsed); + if (!Files.exists(path)) { + throw new CacheResourceFileNotFoundException(path.toString(), "The network-accessible file '" + path.toString() + "' doesn't exist"); + } + return path; + } + if (!FilesUtils.isIncludedInDeployedPath(resourceCachePath)) { throw new CacheResourceDownloadingException(resourceURI, resourceCachePath, CacheResourceDownloadingError.RESOURCE_NOT_IN_DEPLOYED_PATH, null, null); @@ -309,6 +321,7 @@ public static Path getResourceCachePath(String resourceURI) throws IOException { } public static Path getResourceCachePath(URI uri) throws IOException { + // Eliminate all path traversals URI normalizedUri = uri.normalize(); diff --git a/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/extensions/contentmodel/XMLValidationExternalResourcesBasedOnXSDTest.java b/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/extensions/contentmodel/XMLValidationExternalResourcesBasedOnXSDTest.java index 42e43e33e..efd4c64eb 100644 --- a/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/extensions/contentmodel/XMLValidationExternalResourcesBasedOnXSDTest.java +++ b/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/extensions/contentmodel/XMLValidationExternalResourcesBasedOnXSDTest.java @@ -180,6 +180,31 @@ public void schemaLocationDownloadDisabledUNC() throws Exception { fileURI))); } + @Test + public void schemaLocationFileDoesntExistUNC() throws Exception { + + XMLValidationRootSettings validation = new XMLValidationRootSettings(); + validation.setResolveExternalEntities(true); + + XMLLanguageService ls = new XMLLanguageService(); + ls.initializeIfNeeded(); + + String xml = "\r\n" + // + "\r\n" + // + " \r\n" + // + "\r\n"; + + String fileURI = "test.xml"; + + Diagnostic d = new Diagnostic(r(0, 18, 0, 47), "The network-accessible file '\\\\myserver\\myshare\\schema.xsd' doesn't exist", + DiagnosticSeverity.Error, "xml", XMLSchemaErrorCode.schema_reference_4.getCode()); + + // Test diagnostics + XMLAssert.testPublishDiagnosticsFor(xml, fileURI, validation, ls, pd(fileURI, d, // + new Diagnostic(r(1, 1, 1, 13), "cvc-elt.1.a: Cannot find the declaration of element 'root-element'.", + DiagnosticSeverity.Error, "xml", XMLSchemaErrorCode.cvc_elt_1_a.getCode()))); + } + @Test public void schemaLocationDownloadDisabledUNC2() throws Exception {